-
Notifications
You must be signed in to change notification settings - Fork 0
/
paint.py
258 lines (220 loc) · 8.41 KB
/
paint.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import time, displayio, vectorio, os
from effect import Effect
class Effect(Effect):
def __init__(self, device:Device):
self.name = 'Paint'
super().__init__(device, self.name)
self.device = device
device.clearDisplayGroup(device.effect_group)
self.fillbg = 0
self.menu = [
{
'label': 'Fill BG',
'set': self.setFillBackground,
'get': self.getFillBackground
},{
'label': 'New',
'set': self.setNew,
'get': lambda: '<Press>'
},{
'label': 'Load',
'set': lambda d: (self.loadFile(self.safeCycleFilenames(d))),
'get': lambda: str(self.currentSaveFilename)
},{
'label': 'Save',
'set': self.saveCurrentToDisk,
'get': lambda: '<Press>'
},{
'label': 'PlaySpd',
'set': self.setPlayspeed,
'get': self.getPlayspeed
}
]
self.p = displayio.Palette(10)
self.p[0] = device.hls(0, 0, 0) # black
self.p[1] = device.hls(.99, .3, .85) # red
self.p[2] = device.hls(.05, .35, .85) # orange
self.p[3] = device.hls(.12, .35, .85) # yellow
self.p[4] = device.hls(.25, .3, .85) # grass green
self.p[5] = device.hls(.4, .3, .85) # teal
self.p[6] = device.hls(.6, .3, .85) # blue
self.p[7] = device.hls(.7, .3, .85) # purple
self.p[8] = device.hls(.8, .3, .85) # violet
self.p[9] = device.hls(0, 1, 0) #white
self.caretPos = [0,0]
self.selectedColor = 1
# Create a bitmap with the number of colors in the selected palette
self.bitmap = displayio.Bitmap(device.display.width, device.display.height, 9)
self.nextframe = displayio.Bitmap(device.display.width, device.display.height, 9)
self.undo = displayio.Bitmap(device.display.width, device.display.height, 9)
self.bitmapgrid = displayio.TileGrid(self.bitmap, pixel_shader=self.p)
self.caret = vectorio.Polygon(pixel_shader=self.p, points=[(0,0),(0,4),(4,0)], x=0, y=0, color_index=1)
self.marker = vectorio.Polygon(pixel_shader=self.p, points=[(0,0),(4,0),(4,4),(0,4)], x=0, y=0, color_index=9)
self.caretGroup = displayio.Group()
self.caretGroup.hidden = True
self.caretGroup.append(self.caret)
self.caretGroup.append(self.marker)
self.setScale(4)
self.painting = False
self.playspeed = 0
# Create a TileGrid using the Bitmap and Palette
self.paintGroup = displayio.Group()
device.clearDisplayGroup(device.effect_group)
self.paintGroup.append(self.bitmapgrid)
self.paintGroup.append(self.caretGroup)
device.effect_group.append(self.paintGroup)
self.lastblink = 0
self.lastload = 0
self.blinkstate = False
self.allSaveFilenames = self.getAllFilenames()
self.loadFile(self.allSaveFilenames[0])
def getAllFilenames(self):
filenames = [filename.split('.')[0] for filename in os.listdir('/paintsaves/')]
return sorted(filenames)
def play(self):
if (self.device.limitStep(.25, self.lastblink)):
if self.blinkstate == False:
self.caret.color_index = self.selectedColor
self.marker.color_index = self.selectedColor
self.blinkstate = True
else:
if self.selectedColor == 9:
self.caret.color_index = 0
self.marker.color_index = 0
else:
self.caret.color_index = 9
self.marker.color_index = 9
self.blinkstate = False
self.lastblink = time.monotonic()
if (self.playspeed != 0 and self.device.limitStep(self.playspeed, self.lastload)):
self.loadFile(self.safeCycleFilenames(1))
self.lastload = time.monotonic()
elif self.playspeed == 0:
self.lastload = time.monotonic()
def safeCycleFilenames(self, direction:int=1):
# wrapper for cycleOption to account for unsaved new image
# if we are on an unsaved new image and are abandoning it, skip to either the last or first saved image
if self.currentSaveFilename == 'Unnamed' and direction == -1:
self.currentSaveFilename = self.allSaveFilenames[len(self.allSaveFilenames)-1]
elif self.currentSaveFilename == 'Unnamed' and direction == 1:
self.currentSaveFilename = self.allSaveFilenames[0]
else:
self.currentSaveFilename = self.device.cycleOption(self.allSaveFilenames, self.currentSaveFilename, direction)
return self.currentSaveFilename
def handleRemote(self, key:str):
if key == 'PlayPause':
pass
elif key == 'VolDown':
self.loadFile(self.safeCycleFilenames(-1))
locals()['menu'].showOverlay(self.currentSaveFilename)
elif key == 'VolUp':
self.loadFile(self.safeCycleFilenames(1))
locals()['menu'].showOverlay(self.currentSaveFilename)
else:
self.caretGroup.hidden = False # show cursor on all other button presses
if key == 'Left':
self.caretPos[0] = [lambda: self.device.display.width-self.scale, lambda: self.caretPos[0] - self.scale][self.caretPos[0] > 0]()
self.paint()
elif key == 'Right':
self.caretPos[0] = [lambda: 0, lambda: self.caretPos[0] + self.scale][self.caretPos[0] + self.scale < self.device.display.width]()
self.paint()
elif key == 'Up':
self.caretPos[1] = [lambda: self.device.display.height-self.scale, lambda: self.caretPos[1] - self.scale][self.caretPos[1] > 0]()
self.paint()
elif key == 'Down':
self.caretPos[1] = [lambda: 0, lambda: self.caretPos[1] + self.scale][self.caretPos[1] + self.scale < self.device.display.height]()
self.paint()
elif key == 'Back':
self.switchPainting(False)
self.bitmap.blit(0,0,self.undo)
elif len(key) == 1:
self.selectedColor = int(key)
self.caret.color_index = int(key)
self.paint()
elif key == 'Enter' and self.painting == True:
self.switchPainting(False)
elif key == 'Enter' and self.painting == False:
self.switchPainting(True)
self.paint()
elif key == 'StopMode':
self.setScale(self.device.cycleOption([4,2,1], self.scale, 1))
def paint(self):
self.caretGroup.x = self.caretPos[0]
self.caretGroup.y = self.caretPos[1]
if self.painting:
self.undo.blit(0,0,self.bitmap)
y=0
while y<self.scale:
x=0
while x<self.scale:
self.bitmap[(self.caretPos[0]+x,self.caretPos[1]+y)] = self.selectedColor
x += 1
y+=1
def switchPainting(self, b):
if b:
self.painting = True
self.marker.hidden = False
self.caret.hidden = True
else:
self.painting = False
self.marker.hidden = True
self.caret.hidden = False
def setScale(self, scale):
self.scale = scale
self.caretPos[0] = self.caretPos[0] - (self.caretPos[0] % scale)
self.caretPos[1] = self.caretPos[1] - (self.caretPos[1] % scale)
if self.caretPos[0] > self.device.display.width-1:
self.caretPos[0] = 0
if self.caretPos[1] > self.device.display.height-1:
self.caretPos[1] = 0
self.caretGroup.x = self.caretPos[0]
self.caretGroup.y = self.caretPos[1]
self.caret.points = [(0,0),(0,scale),(scale,0)]
self.marker.points = [(0,0),(scale,0),(scale,scale),(0,scale)]
def loadFile(self, filename):
self.currentSaveFilename = filename
saveContents = self.device.loadData('/paintsaves/'+self.currentSaveFilename+'.json')
self.loadSaveContents(saveContents)
def loadSaveContents(self, save):
self.switchPainting(False)
n = 0
for y in range(0, self.device.display.height):
for x in range(0, self.device.display.width):
self.nextframe[x,y] = int(save['data'][n])
n += 1
self.bitmap.blit(0,0,self.nextframe)
self.undo.blit(0,0,self.nextframe)
self.caretGroup.hidden = True
self.lastload = time.monotonic()
self.device.gc(1)
def saveCurrentToDisk(self, direction:int=0):
output = ""
for y in range(0, self.device.display.height):
for x in range(0, self.device.display.width):
output = output + str(self.bitmap[x,y])
newData = {"data": output}
if self.currentSaveFilename == 'Unnamed':
new = True
self.currentSaveFilename = str(int(self.allSaveFilenames[len(self.allSaveFilenames)-1])+1)
else:
new = False
print(self.currentSaveFilename)
self.device.writeData(newData, '/paintsaves/'+self.currentSaveFilename+'.json')
if new:
self.allSaveFilenames = self.getAllFilenames()
print(self.allSaveFilenames)
locals()['menu'].hideMenu()
def setNew(self, direction:int=0):
locals()['menu'].hideMenu()
self.currentSaveFilename = "Unnamed"
newSave = {"data":'0'*(self.device.display.height*self.device.display.width)}
self.loadSaveContents(newSave)
def getFillBackground(self):
return str(self.fillbg)
def setFillBackground(self, direction:int):
self.fillbg = self.device.cycleOption([0,1,2,3,4,5,6,7,8,9], self.fillbg, direction)
self.bitmap.fill(self.fillbg)
def setPlayspeed(self, direction:int):
self.playspeed = self.device.cycleOption([0,.5,1,5,10,30,60], self.playspeed, direction)
def getPlayspeed(self):
return str(self.playspeed) + 'sec'