forked from andrewalbers/CardCreator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparent_canvas.pde
346 lines (315 loc) · 7.57 KB
/
parent_canvas.pde
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
public class TemplateCanvas extends ParentPanel
{
PGraphics fontPlacer;
PImage cardPic;
PFont drawFont;
ArrayList<Element> elements;
Table contents;
int canvasWid;
int canvasHei;
int contentExample = 0;
boolean doHighlight = true;
boolean doContent = true;
boolean isDragging = false;
int[] resizing = {NONE,NONE}; //horizontal direction, vertical direction
float widHeiRatio;
float prevX2;
float prevY2;
int lastMX;
int lastMY;
TemplateCanvas (String nm, ArrayList<Element> e, Table conts, int xx, int yy, int canW, int canH)
{
name = nm; x = xx; y = yy; wid = int(canW * zoom); hei = int(canH * zoom);
elements = e;
canvasWid = canW;
canvasHei = canH;
contents = conts;
drawPG = createGraphics(wid,hei);
}
public void updateThis()
{
wid = int(canvasWid * zoom);
hei = int(canvasHei * zoom);
drawCanvas();
drawElements();
}
public void drawCanvas()
{
drawPG.fill(150);
drawPG.rect(0, 0, wid, hei);
}
void drawElements()
{
for ( Element e : elements )
{
e.updatePosition();
int cx = int(e.x * zoom);
int cy = int(e.y * zoom);
int cwid = int(e.wid * zoom);
int chei = int(e.hei * zoom);
drawPG.noStroke();
if (doContent && contentExample != NONE)
{
drawContent(e , cx , cy , cwid , chei);
}
if (doHighlight)
{
drawHighlight(e , cx , cy , cwid , chei);
}
drawSelectBorder(e , cx , cy , cwid , chei);
}
}
void drawHighlight( Element e , int cx , int cy , int cwid , int chei )
{
int alpha = 50;
if ( e.hovered )
{
drawPG.fill(255,255,255,100);
}
else if ( e.type == IMG )
{
drawPG.fill(100,255,100,alpha);
}
else if ( e.type == TXT )
{
drawPG.fill(100,100,255,alpha);
}
drawPG.rect( int(e.x * zoom), int(e.y * zoom), int(e.wid * zoom), int(e.hei * zoom));
}
void drawContent( Element e , int cx , int cy , int cwid , int chei )
{
TableRow conRow = contents.getRow(contentExample);
if ( e.type == IMG )
{
try {
String imgFile = conRow.getString(e.name);
cardPic = loadImage(imgFile);
drawPG.image(cardPic, cx, cy, cwid, chei);
} catch ( Exception ex ) {
}
}
else if ( e.type == TEXT )
{
float hSquish = e.hSquish;
fontPlacer = createGraphics(int(cwid/hSquish),chei);
fontPlacer.beginDraw();
fontPlacer.textFont(e.font);
fontPlacer.textSize(constrain(e.fontSize * zoom,0,chei));
fontPlacer.textAlign(LEFT,TOP);
fontPlacer.fill(e.col);
try {
String textToPlace = conRow.getString(e.name);
fontPlacer.text( textToPlace , 0 , 0 , int(cwid/hSquish) , chei);
} catch ( Exception ex ) {
}
fontPlacer.endDraw();
drawPG.image( fontPlacer.get() , cx , cy , cwid, chei);
}
}
void drawSelectBorder( Element e , int cx , int cy , int cwid , int chei )
{
if ( e.selected )
{
drawPG.stroke(200);
drawPG.strokeWeight(2);
drawPG.fill(255,255,255,50);
if( isDragging )
{
drawPG.stroke(200,200,200,255);
drawPG.strokeWeight(1);
drawPG.fill(255,255,255,100);
}
drawPG.rect( cx, cy, cwid, chei);
}
}
boolean handleDragged(int mx, int my , int selectedId)
{
if (selectedId != NONE)
{
if (resizing[0] != NONE || resizing[1] != NONE)
{
resizeElement(elements.get(selectedId) , mx, my );
}
else if ( isDragging )
{
dragElement(elements.get(selectedId), mx, my);
}
else
{
setResizing(elements.get(selectedId), mx, my);
if(resizing[0] != NONE || resizing[1] != NONE)
{
beginResizing(elements.get(selectedId), mx, my);
}
else
{
beginDragging(mx, my);
isDragging = true;
}
}
}
else if( isDragging )
{
dragCanvas(mx,my);
}
else
{
beginDragging(mx, my);
}
return isDragging;
}
void beginResizing(Element e, int mx, int my)
{
widHeiRatio = e.realWid/e.realHei; //keep in mind in case user hits SHIFT
prevX2 = e.realX + e.realWid;
prevY2 = e.realY + e.realHei;
beginDragging(mx,my);
}
void beginDragging(int mx, int my)
{
isDragging = true;
resetLastMouse(mx,my);
}
void resizeElement(Element e , int mx, int my )
{
if(resizing[1] == BORDER_TOP)
{
e.realY += (my - lastMY);
e.realHei -= (my - lastMY);
if(keyPressed && keyCode == SHIFT)
{
e.realWid = e.realHei * widHeiRatio;
e.realY = prevY2 - e.realHei;
}
}
else if(resizing[1] == BORDER_BOTTOM)
{
e.realHei += (my - lastMY);
if(keyPressed && keyCode == SHIFT)
{
e.realWid = e.realHei * widHeiRatio;
}
}
if(resizing[0] == BORDER_RIGHT)
{
e.realWid += (mx - lastMX);
if(keyPressed && keyCode == SHIFT)
{
e.realHei = e.realWid / widHeiRatio;
}
}
else if(resizing[0] == BORDER_LEFT)
{
e.realX += (mx - lastMX);
e.realWid -= (mx - lastMX);
if(keyPressed && keyCode == SHIFT)
{
e.realHei = e.realWid / widHeiRatio;
e.realX = prevX2 - e.realWid;
}
}
resetLastMouse(mx,my);
e.realWid = constrain(e.realWid, 3, 3000);
e.realHei = constrain(e.realHei, 3, 3000);
println("e.realWid = ", e.realWid, " e.realHei = ", e.realHei);
}
void dragCanvas(int mx, int my)
{
x += (mx - lastMX);
y += (my - lastMY);
resetLastMouse(mx,my);
constrainOffsets();
}
void dragElement(Element e, int mx, int my)
{
e.realX += (mx - lastMX);
e.realY += (my - lastMY);
resetLastMouse(mx,my);
}
void resetLastMouse(int mx, int my)
{
lastMX = mx;
lastMY = my;
}
void clickThis()
{
if( isDragging )
{
isDragging = false;
resizing[0] = NONE;
resizing[1] = NONE;
}
else
{
println("Clicked ", name);
}
}
void handleArrowPress(int selectId)
{
if(selectId != NONE)
{
Element e = elements.get(selectId);
if(keyCode == UP)
{
e.move(0, -1 * ceil( move_step ) );
println("moving up");
}
else if(keyCode == DOWN)
{
e.move(0, ceil(move_step ));
}
else if(keyCode == LEFT)
{
e.move( -1 * ceil(move_step ) , 0);
}
else
{
e.move( ceil(move_step ) , 0);
}
}
else if(doContent)
{
if(keyCode == LEFT)
{
contentExample = (contentExample + contents.getRowCount() -1) % contents.getRowCount();
}
else if(keyCode == RIGHT)
{
contentExample = (contentExample + 1) % contents.getRowCount();
}
}
}
void setResizing( Element e, int mx, int my)
{
resizing[0] = NONE;
resizing[1] = NONE;
int dispX = int(e.x * zoom);
int dispY = int(e.y * zoom);
int dispW = int(e.wid * zoom);
int dispH = int(e.hei * zoom);
if( abs(mx - dispX) < border_select_width)
{
resizing[0] = BORDER_LEFT;
}
else if( abs(mx - (dispX + dispW)) < border_select_width )
{
resizing[0] = BORDER_RIGHT;
}
if( abs(my - dispY) < border_select_width)
{
resizing[1] = BORDER_TOP;
}
else if( abs(my - (dispY + dispH)) < border_select_width )
{
resizing[1] = BORDER_BOTTOM;
}
}
void toggleHighlight()
{
doHighlight = !doHighlight;
}
void toggleContent()
{
doContent = !doContent;
}
}