/
tabdraw.py
236 lines (215 loc) · 7.99 KB
/
tabdraw.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# @Author: Daniel Grooms
# @Description: Tab drawing class for the main application,
# to be implemented in the View Screen
import os
from functools import partial
from tab import Tab
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.popup import Popup
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy.properties import ObjectProperty
from kivy.atlas import Atlas
from kivy.graphics import Rectangle
from kivy.lang import Builder
from kivy.uix.actionbar import ActionBar
# Global variable to hold the toggle button pressed on the edit toolbar
toggle_button = 'Null'
temp = ''
m = 0
# The widget that holds the drawing of the tab
class TabCanvas(Widget):
pass
# The area holding the tab canvas and the slider
class TabArea(BoxLayout):
tabCanvas = ObjectProperty(None)
slide = ObjectProperty(None)
scrlv = ObjectProperty(None)
mainBoxLayout = ObjectProperty(None)
# Constant declaration
CHAR_WIDTH = 32
CHAR_HEIGHT = CHAR_WIDTH
ATLAS_PREFIX = 'atlas://Assets/main/'
ACCEPT_CHARS = ['|', '-', 'x', 'X', '\\', '/', '~', '*', 'h', 'p', 'b', 'r', ]
for num in range(25):
ACCEPT_CHARS.append(str(num))
# Determines if tab can be edited
editable = False
inputText = ''
def scroll_change(self, scrl, instance, value):
scrl.scroll_x = value
def slider_change(self, slide, instance, value):
if value >= 0:
slide.value = value
# Set the tab as editable or not editable
# Allows user to edit the tab if True
def setEditable(self, setter=True):
# Adds the 'extend tab' button to the right of the tab
if not self.editable and setter:
self.mainBoxLayout.add_widget(Button(text='Extend Tab',size_hint=(0.1, 0.5),on_release=self.extendTab))
self.mainBoxLayout.add_widget(Button(text='Reduce Tab',size_hint=(0.1, 0.5),on_release=self.reduceTab))
self.editable = setter
# Extend each line of the tab by one blank bar
def extendTab(self, kivyAction):
self.tab.extendByOneBar()
self.drawtab()
# Reduce each line of the tab by one bar
def reduceTab(self, kivyAction):
self.tab.reduceByOneBar()
self.drawtab()
# A callback function to get the user input from the textInput module
def setInputText(self, instance, value):
self.inputText = str(value)
# Detects a user click on the tab, and allows editing if the option is set
def tabTouched(self, touch):
if self.editable:
# Get the indices of the corresponding character of the tab
xpos = int(touch.x)
ypos = int(touch.y)
xIndex = (xpos // 32)
yIndex = self.tabNumRows - (ypos // 32)
print("Coords: " + str(yIndex) + " " + str(xIndex))
print('This is what your toggle button is: ', toggle_button)
curr_val = self.tab.getTabData(yIndex, xIndex)
double_digit = False
modifier = True
if len(curr_val[1]) != 1:
double_digit = True
print('this is the eval of double_digit: ', double_digit)
if double_digit == True:
double_val = self.tab.getTabData(yIndex,xIndex + 2)
print('her is solksdflasf: ',self.tab.getTabData(yIndex,xIndex + 1), ' ', self.tab.getTabData(yIndex,xIndex), ' ', self.tab.getTabData(yIndex,xIndex + 2))
if self.tab.getTabData(yIndex,xIndex) in ('bar' , 'tbar' , 'plusbar' , 'mute' , 'slide' , 'vibrato') or self.tab.getTabData(yIndex,xIndex)[0] == 'norm':
modifier = False
# if ((double_val in ('bar' , 'tbar' , 'plusbar' , 'mute' , 'slide' , 'vibrato')) or double_val[1].isdigit()):
# modifier = False
else:
print('her is solksdflasf: ',self.tab.getTabData(yIndex,xIndex + 1), ' ', self.tab.getTabData(yIndex,xIndex)[0], ' ', self.tab.getTabData(yIndex,xIndex + 2))
next_val = self.tab.getTabData(yIndex,xIndex + 1)
if self.tab.getTabData(yIndex,xIndex) in ('bar' , 'tbar' , 'plusbar' , 'mute' , 'slide' , 'vibrato') or self.tab.getTabData(yIndex,xIndex)[0] == 'norm':
modifier = False
# if ((next_val in ('bar' , 'tbar' , 'plusbar' , 'mute' , 'slide' , 'vibrato')) or next_val[1].isdigit()):
# modifier = False
print('Result of modifier: ', modifier)
if toggle_button == 'slide':
self.inputText = '/'
print('This is the inputText: ', self.inputText)
self.writeToTab(0,yIndex,xIndex)
if double_digit == True:
self.inputText = '-'
self.writeToTab(0,yIndex,xIndex + 1)
if modifier == True:
self.writeToTab(0,yIndex,xIndex + 2)
else:
self.inputText = '-'
if modifier == True:
self.writeToTab(0,yIndex,xIndex)
elif toggle_button == 'vibrato':
self.inputText = '~'
print('This is the inputText: ', self.inputText)
self.writeToTab(0,yIndex,xIndex)
if double_digit == True:
self.inputText = '-'
self.writeToTab(0,yIndex,xIndex + 1)
if modifier == True:
self.writeToTab(0,yIndex,xIndex + 2)
else:
self.inputText = '-'
if modifier == True:
self.writeToTab(0,yIndex,xIndex)
elif toggle_button == 'squealie':
self.inputText = '*'
print('This is the inputText: ', self.inputText)
if double_digit == False:
self.writeToTab(0,yIndex,xIndex + 1)
else:
self.writeToTab(0,yIndex,xIndex + 2)
elif toggle_button == 'hammeron':
self.inputText = 'h'
print('This is the inputText: ', self.inputText)
if double_digit == False:
self.writeToTab(0,yIndex,xIndex + 1)
else:
self.writeToTab(0,yIndex,xIndex + 2)
elif toggle_button == 'pulloff':
self.inputText = 'p'
print('This is the inputText: ', self.inputText)
if double_digit == False:
self.writeToTab(0,yIndex,xIndex + 1)
else:
self.writeToTab(0,yIndex,xIndex + 2)
else:
next_val = self.tab.getTabData(yIndex, xIndex + 1)
if double_digit == False:
if not (next_val[1].isdigit() or next_val[0] == '-'):
self.inputText = '-'
self.writeToTab(0,yIndex,xIndex + 1)
else:
self.inputText = '-'
self.writeToTab(0,yIndex,xIndex + 1)
if ((self.tab.getTabData(yIndex,xIndex + 2) not in ('bar' , 'tbar' , 'plusbar' , 'mute' , 'slide' , 'vibrato')) or not self.tab.getTabData(yIndex,xIndex + 2)[1].isdigit()):
self.writeToTab(0,yIndex,xIndex + 2)
textbox = TextInput(text='', multiline=False)
inputPopup = Popup(
title='Fret Number',
content = textbox,
size_hint = (None, None),
size = (150, 100),
#pos = (xpos, ypos)
)
textbox.bind(on_text_validate=inputPopup.dismiss)
textbox.bind(text=self.setInputText)
inputPopup.bind(
on_dismiss=partial(self.writeToTab,row = yIndex, col = xIndex)
)
inputPopup.open()
# call tab to rewrite file at (yIndex, xIndex) with input if valid
# Write input to the tab using tab's write function
def writeToTab(self, instance, row, col):
if self.editable:
# Validate input
if self.inputText in self.ACCEPT_CHARS:
# Write to tab
print('We are writing ', self.inputText, ' to the tab!')
self.tab.write(row, col, self.inputText)
self.drawtab()
# Draws the tab to the screen from the given file using a Tab object
def drawtab(self, filename=''):
if filename != '':
self.tabFile = filename
self.tab = Tab(self.tabFile)
grid = self.tab.getTabData()
self.tabNumRows = len(grid) - 1
# Clear canvas in case a bar was removed
self.tabCanvas.canvas.clear()
with self.tabCanvas.canvas:
for i in range(len(grid)):
for j in range(len(grid[i])):
if isinstance(grid[i][j], str):
Rectangle(
source=self.ATLAS_PREFIX+grid[i][j],
pos=(j*self.CHAR_WIDTH, (self.tabNumRows-i)*self.CHAR_HEIGHT),
size=(self.CHAR_WIDTH, self.CHAR_HEIGHT)
)
elif isinstance(grid[i][j], list):
for item in grid[i][j]:
Rectangle(
source=self.ATLAS_PREFIX+item,
pos=(j*self.CHAR_WIDTH, (self.tabNumRows-i)*self.CHAR_HEIGHT),
size=(self.CHAR_WIDTH, self.CHAR_HEIGHT)
)
class Edittoolbar(ActionBar):
Builder.load_file("screens/edittoolbar.kv")
def checkButtons(self,value):
#print(value)
global toggle_button, temp, m
if m > 0:
temp = toggle_button
toggle_button = value
if temp == toggle_button:
toggle_button = ''
print(toggle_button)
m += 1
return value