-
Notifications
You must be signed in to change notification settings - Fork 2
/
Painting program - Unlimited Undo Example.bb
279 lines (257 loc) · 5.49 KB
/
Painting program - Unlimited Undo Example.bb
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
Global win = CreateWindow("Painting program - Unlimited Undo Example",100,100,800,600,0,1)
Global can = CreateCanvas(0,20,800,600,win)
Type unre
Field map[48*48]
Field cols[12*3]
End Type
Global undoredo.unre = New unre
Dim cols(10,3);the colors
Global mw = 48;wapwidth
Global mh = 48;height
Global tw = 640/mw;tilewidth
Global th = 480/mh;height
Dim map(mw,mh);create map
Global canim = CreateImage(800,600);the image (speedup)
Global brushindex=5;brushcolor
Global brushsize=4
Global cmx;mouse x pos
Global cmy; mouse y pos
Global tileim = CreateImage(tw,th,11)
Global tileimbig = CreateImage(32,32,11)
; create the colors and images
refreshtileimages(True)
; our area of operation
Global screen$="canvas"
Global explode = False
addundo
addundo
updateinterface
.main
Repeat
we = WaitEvent()
If we=$102;keyup
If screen = "canvas"
If EventData()>=2 And EventData()<=10
brushsize = EventData()-1
updateinterface
End If
If EventData()=18;e
If explode = True Then explode = False Else explode = True
End If
If EventData()=22;u undo
undoback
refreshtileimages
updateinterface
End If
End If
End If
If we=$201;mosuedown
End If
If we=$202;mouseup
If EventSource() = can
If EventData() = 1
If RectsOverlap(cmx,cmy,1,1,680,0,32,11*32)
brushindex=cmy/32
updateinterface
End If
If RectsOverlap(cmx,cmy,1,1,0,0,(mw+1)*tw,(mh+1)*th)
brushdown(cmx,cmy,brushindex)
updateinterface
End If
End If
If EventData() = 2
If RectsOverlap(cmx,cmy,1,1,680,0,32,11*32)
If RequestColor()=True
brushindex=cmy/32
cols(brushindex,0) = RequestedRed()
cols(brushindex,1) = RequestedGreen()
cols(brushindex,2) = RequestedBlue()
refreshtileimages
updateinterface
addundo
DebugLog "added"
End If
End If
If RectsOverlap(cmx,cmy,1,1,0,0,(mw+1)*tw,(mh+1)*th)
brushdown(cmx,cmy,0)
updateinterface
End If
End If
If RectsOverlap(cmx,cmy,1,1,0,0,(mw+1)*tw,(mh+1)*th)
addundo
DebugLog "added"
End If
End If
End If
If we=$203;mousemove
If EventSource()=can
cmx = EventX()
cmy = EventY()
If MouseDown(1) = True
If RectsOverlap(cmx,cmy,1,1,0,0,(mw+1)*tw,(mh+1)*th)
brushdown(cmx,cmy,brushindex)
updateinterface
End If
End If
If MouseDown(2) = True
If RectsOverlap(cmx,cmy,1,1,0,0,(mw+1)*tw,(mh+1)*th)
brushdown(cmx,cmy,0)
updateinterface
End If
End If
updateinterface
End If
End If
If we=$401;gadgetaction
End If
If we=$4001
End If
If we=$803 Then Exit
Forever
End
.funcs
Function updateinterface()
SetBuffer ImageBuffer(canim)
Cls
Color 255,255,255
For y=0 To mh-1
For x=0 To mw-1
DrawImage tileim,x*tw,y*th,map(x,y)
Next
Next
For y=0 To 10
DrawImage tileimbig,680,y*32,y
If brushindex = y
Rect 681,y*32,31,31,False
End If
Next
For y=0 To 47
For x=0 To 47
a = map(x,y)
Color cols(a,0),cols(a,1),cols(a,2)
Rect x*2+660,y*2+370,2,2
Next
Next
; draw the brush view
Color 255,255,255
Rect cmx-brushsize*tw/2,cmy-brushsize*th/2,brushsize*tw,brushsize*th,False
If explode = True
Text 10,480,"Explode brush is On (key e)"
Else
Text 10,480,"Explode brush is Off (key e)"
End If
Text 10,520,"press u to undo last move (unlimited)"
SetBuffer CanvasBuffer(can)
Cls
DrawImage canim,0,0
FlipCanvas can
End Function
Function refreshtileimages(init=False)
For i = 0 To 10
SetBuffer ImageBuffer(tileim,i)
If init=True
Color 200+i*3,10+i*20,10
cols(i,0) = ColorRed()
cols(i,1) = ColorGreen()
cols(i,2) = ColorBlue()
End If
Color cols(i,0),cols(i,1),cols(i,2)
Rect 0,0,tw,th,True
SetBuffer ImageBuffer(tileimbig,i)
Color cols(i,0),cols(i,1),cols(i,2)
Rect 0,0,32,32
For x=-1 To 1
For y=-1 To 1
Color 4,4,4
Text tw/2+x,th/2+y,i,1,1
Next
Next
Color 255,255,255
Text tw/2,th/2,i,1,1
Next
End Function
.brushfuncs
Function brushdown(cmx,cmy,ind)
If brushsize=1 Then map(cmx/tw,cmy/th) = ind
If brushsize>1 Then
If explode = True
For y=-brushsize/2 To brushsize/2
For x=-brushsize/2 To brushsize/2
If cmx/tw+x >=0 And cmx/tw+x <=mw
If cmy/th+y >=0 And cmy/th+y <=mh
If Rnd(brushsize)<2
b = Rnd(cmx/tw-brushsize/2,cmx/tw+brushsize/2)
c = Rnd(cmy/th-brushsize/2,cmy/th+brushsize/2)
If b>=0 And b<=mw And c>=0 And c<=mh
a = map(b,c)
map(cmx/tw+x,cmy/th+y) = a
End If
End If
End If
End If
Next
Next
End If
If explode = False
For y=-brushsize/2 To brushsize/2
For x=-brushsize/2 To brushsize/2
If cmx/tw+x >=0 And cmx/tw+x <=mw
If cmy/th+y >=0 And cmy/th+y <=mh
map(cmx/tw+x,cmy/th+y) = ind
End If
End If
Next
Next
End If
End If
End Function
Function addundo()
undoredo.unre = New unre
cnt=0
For y=0 To 47
For x=0 To 47
undoredo\map[cnt] = map(x,y)
cnt=cnt+1
Next
Next
cnt3=0
For i=0 To 11*3
undoredo\cols[cnt3] = cols(cnt1,cnt2)
cnt2=cnt2+1
If cnt2>3 Then cnt2 =0:cnt1=cnt1+1
cnt3=cnt3+1
Next
cnt=0
For ii.unre = Each unre
cnt=cnt+1
Next
DebugLog "num undo "+cnt
End Function
Function undoback()
Local c=0
For ii.unre = Each unre
c=c+1
Next
DebugLog "initialize undo "+c
If c<3 Then Return
DebugLog "undoing'
Local cnt=0
undoredo.unre = Last unre
Delete undoredo
undoredo.unre = Last unre
For y=0 To 47
For x=0 To 47
map(x,y) = undoredo\map[cnt]
cnt=cnt+1
Next
Next
cnt1=0
cnt2=0
cnt3=0
For i=0 To 11*3
cols(cnt1,cnt2) = undoredo\cols[cnt3]
cnt2=cnt2+1
If cnt2>3 Then cnt2 =0:cnt1=cnt1+1
cnt3=cnt3+1
Next
End Function