Skip to content

Commit 0ab23ca

Browse files
committed
Merge pull request #2 from Zebbeni/clicking
Clicking
2 parents d983dc0 + 015515f commit 0ab23ca

13 files changed

+324
-278
lines changed

button.pde

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,24 @@
1-
public class Button extends Panel
1+
public class Button extends ChildPanel
22
{
3-
PGraphics drawPG;
4-
5-
Button ( int xx , int yy , int ww , int hh, PGraphics pGraph )
3+
Button ( String nm , int xx , int yy , int ww , int hh )
64
{
5+
name = nm;
76
x = xx;
87
y = yy;
98
wid = ww;
109
hei = hh;
11-
drawPG = pGraph;
12-
}
13-
14-
void drawButton()
15-
{
16-
updatePosition();
17-
drawPG.fill(150);
18-
drawPG.rect( x , y , wid , hei );
10+
drawPG = createGraphics( wid, hei );
1911
}
2012

21-
void updatePosition()
13+
public void clickThis()
2214
{
23-
15+
println("clicked ", name);
2416
}
2517

26-
void updateDraw()
18+
public void updateThis()
2719
{
20+
drawPG.strokeWeight(2);
21+
drawPG.fill(150);
22+
drawPG.rect( 0 , 0 , wid , hei );
2823
}
2924
}

content_handler.pde

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,4 @@ public class ContentHandler
1616
{
1717
table = loadTable(filename, "header");
1818
}
19-
20-
2119
}

global_functions.pde

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
void zoom(float zm)
2+
{
3+
zoom *= zm;
4+
canvas.x += int(25 * ( zm - 1 ));
5+
canvas.y += int(25 * ( zm - 1 ));
6+
canvas.wid = int(canvas.canvasWid * zoom);
7+
canvas.hei = int(canvas.canvasHei * zoom);
8+
constrainOffsets();
9+
}
10+
11+
void constrainOffsets()
12+
{
13+
canvas.x = constrain(canvas.x , int(-0.8 * canvas.wid), width - 50);
14+
canvas.y = constrain(canvas.y , int(-0.8 * canvas.hei), height - 50);
15+
}
16+
17+
void addElement()
18+
{
19+
Element e = new Element("New Element", IMG, 2, 2, 200, 250);
20+
listbox.addItem(e);
21+
content.table.addColumn(e.name);
22+
}
23+
24+
void removeElement()
25+
{
26+
int selectedId = listbox.selectedItem;
27+
if (selectedId != NONE)
28+
{
29+
Element e = listbox.items.get(selectedId);
30+
content.table.removeColumn(e.name);
31+
listbox.removeItem(e);
32+
}
33+
}
34+
35+
public void handleArrowPress( )
36+
{
37+
int selectedId = listbox.selectedItem;
38+
canvas.handleArrowPress( selectedId );
39+
}
40+
41+
public void toggleDrawHighlights()
42+
{
43+
canvas.toggleHighlight();
44+
}
45+
46+
void toggleDrawContent()
47+
{
48+
canvas.toggleContent();
49+
}
50+
51+
void saveTemplate()
52+
{
53+
template.saveTemplate();
54+
}
55+
56+
public void itemClicked ( int i, Object item )
57+
{
58+
lastItemClicked = item;
59+
}

constants.pde renamed to global_variables.pde

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@ int BORDER_RIGHT = 4;
99

1010
int border_select_width = 5;
1111
int move_step = 1;
12+
13+
Object lastItemClicked;
14+
float zoom = 0.6;

panel.pde

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,33 @@ public abstract class Panel
44
int y;
55
int wid;
66
int hei;
7+
PGraphics drawPG;
8+
String name = "No Name";
9+
10+
public abstract void clickThis();
11+
public abstract boolean click( int mx , int my );
712

13+
public abstract void updateThis();
814
public abstract void updateDraw();
15+
public abstract void drawToBuffer( PGraphics parentPG);
16+
17+
public void updateDrawPG()
18+
{
19+
drawPG = createGraphics( wid, hei );
20+
drawPG.beginDraw();
21+
updateDraw();
22+
drawPG.endDraw();
23+
}
24+
25+
public boolean isInPanel( int mx , int my )
26+
{
27+
/**
28+
* Returns true if the given mouse X and mouse Y lie in this panel
29+
*/
30+
boolean isHere = (mx > x && mx < (x + wid) && my > y && y < (my + hei));
31+
return isHere;
32+
}
933
}
34+
35+
36+

panel_child.pde

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
public abstract class ChildPanel extends Panel
2+
{
3+
public boolean click( int mx , int my )
4+
{
5+
/**
6+
* Returns true if the click occurred in this panel
7+
* calls class-specific click function with mouse X and mouse Y
8+
*/
9+
boolean clickedInHere = isInPanel( mx , my );
10+
if ( clickedInHere )
11+
{
12+
clickThis();
13+
}
14+
return clickedInHere;
15+
}
16+
17+
public void updateDraw()
18+
{
19+
updateThis();
20+
}
21+
22+
public void drawToBuffer( PGraphics parentPG )
23+
{
24+
/*
25+
* draws thisPG to the parentPG
26+
*/
27+
parentPG.image( drawPG , x , y );
28+
}
29+
}

panel_parent.pde

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
public abstract class ParentPanel extends Panel
2+
{
3+
ArrayList<Panel> childPanels = new ArrayList<Panel>();
4+
5+
public boolean click( int mx , int my )
6+
{
7+
/*
8+
* Passes relative click info to all children until it finds one clicked
9+
* by the mouse.
10+
*/
11+
boolean clickedInHere = isInPanel( mx , my );
12+
13+
if( clickedInHere )
14+
{
15+
boolean clickedChild = false;
16+
for( int i = 0; i < childPanels.size() && !clickedChild ; i++ )
17+
{
18+
clickedChild = childPanels.get(i).click( mx - x, my - y);
19+
}
20+
if( !clickedChild )
21+
{
22+
clickThis();
23+
}
24+
}
25+
26+
return clickedInHere;
27+
}
28+
29+
public void updateDraw()
30+
{
31+
updateThis();
32+
for( Panel p : childPanels)
33+
{
34+
p.updateDrawPG();
35+
}
36+
}
37+
38+
public void drawToBuffer( PGraphics parentPG )
39+
{
40+
/*
41+
* Begins drawing drawPG and passes this to children to
42+
* draw on, before passing the final image to its own parent
43+
*/
44+
drawPG.beginDraw();
45+
for( Panel p : childPanels )
46+
{
47+
p.drawToBuffer( drawPG );
48+
}
49+
drawPG.endDraw();
50+
parentPG.image( drawPG , x , y );
51+
}
52+
}

template_canvas.pde renamed to parent_canvas.pde

Lines changed: 23 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
1-
public class TemplateCanvas extends Panel
1+
public class TemplateCanvas extends ParentPanel
22
{
3-
PGraphics drawPG;
43
PGraphics fontPlacer;
54
PImage cardPic;
65
PFont drawFont;
76

87
ArrayList<Element> elements;
98
Table contents;
109

11-
// int offsetX = 300;
12-
// int offsetY = 100;
13-
// float zoom = 0.5;
14-
int pgWidth;
15-
int pgHeight;
10+
int canvasWid;
11+
int canvasHei;
1612

1713
int contentExample = 0;
1814

@@ -28,30 +24,25 @@ public class TemplateCanvas extends Panel
2824
int lastMX;
2925
int lastMY;
3026

31-
TemplateCanvas (ArrayList<Element> e, Table conts, int xx, int yy, int canW, int canH)
27+
TemplateCanvas (String nm, ArrayList<Element> e, Table conts, int xx, int yy, int canW, int canH)
3228
{
33-
x = xx;
34-
y = yy;
29+
name = nm; x = xx; y = yy; wid = int(canW * zoom); hei = int(canH * zoom);
3530
elements = e;
36-
wid = canW;
37-
hei = canH;
31+
canvasWid = canW;
32+
canvasHei = canH;
3833
contents = conts;
34+
drawPG = createGraphics(wid,hei);
3935
}
4036

41-
void drawAll()
37+
public void updateThis()
4238
{
43-
drawPG = createGraphics(wid,hei);
44-
drawPG.beginDraw();
39+
wid = int(canvasWid * zoom);
40+
hei = int(canvasHei * zoom);
4541
drawCanvas();
4642
drawElements();
47-
drawPG.endDraw();
48-
}
49-
50-
void updateDraw()
51-
{
5243
}
5344

54-
void drawCanvas()
45+
public void drawCanvas()
5546
{
5647
drawPG.fill(150);
5748
drawPG.rect(0, 0, wid, hei);
@@ -63,10 +54,10 @@ public class TemplateCanvas extends Panel
6354
{
6455
e.updatePosition();
6556

66-
int cx = int(e.x);
67-
int cy = int(e.y);
68-
int cwid = int(e.wid);
69-
int chei = int(e.hei);
57+
int cx = int(e.x * zoom);
58+
int cy = int(e.y * zoom);
59+
int cwid = int(e.wid * zoom);
60+
int chei = int(e.hei * zoom);
7061

7162
drawPG.noStroke();
7263
if (doContent && contentExample != NONE)
@@ -96,7 +87,7 @@ public class TemplateCanvas extends Panel
9687
{
9788
drawPG.fill(100,100,255,alpha);
9889
}
99-
drawPG.rect(e.x, e.y, e.wid, e.hei);
90+
drawPG.rect( int(e.x * zoom), int(e.y * zoom), int(e.wid * zoom), int(e.hei * zoom));
10091
}
10192

10293
void drawContent( Element e , int cx , int cy , int cwid , int chei )
@@ -117,7 +108,7 @@ public class TemplateCanvas extends Panel
117108
fontPlacer = createGraphics(int(cwid/hSquish),chei);
118109
fontPlacer.beginDraw();
119110
fontPlacer.textFont(e.font);
120-
fontPlacer.textSize(e.fontSize);
111+
fontPlacer.textSize(constrain(e.fontSize * zoom,0,chei));
121112
fontPlacer.textAlign(LEFT,TOP);
122113
fontPlacer.fill(e.col);
123114
try {
@@ -264,14 +255,18 @@ public class TemplateCanvas extends Panel
264255
lastMY = my;
265256
}
266257

267-
void handleReleased(int mx, int my)
258+
void clickThis()
268259
{
269260
if( isDragging )
270261
{
271262
isDragging = false;
272263
resizing[0] = NONE;
273264
resizing[1] = NONE;
274265
}
266+
else
267+
{
268+
println("Clicked ", name);
269+
}
275270
}
276271

277272
void handleArrowPress(int selectId)
@@ -310,28 +305,6 @@ public class TemplateCanvas extends Panel
310305
}
311306
}
312307

313-
// void zoomIn()
314-
// {
315-
// zoom *= 1.2;
316-
// offsetX -= int(50 * canvas.zoom);
317-
// offsetY -= int(50 * canvas.zoom);
318-
// constrainOffsets();
319-
// }
320-
//
321-
// void zoomOut()
322-
// {
323-
// canvas.zoom /= 1.2;
324-
// canvas.offsetX += int(50 * canvas.zoom);
325-
// canvas.offsetY += int(50 * canvas.zoom);
326-
// constrainOffsets();
327-
// }
328-
//
329-
// void constrainOffsets()
330-
// {
331-
// offsetX = constrain(offsetX , int(wid * zoom * -1) + 200, width - 200);
332-
// offsetY = constrain(offsetY , int(hei * zoom * -1) + 200, height - 200);
333-
// }
334-
335308
void setResizing( Element e, int mx, int my)
336309
{
337310
resizing[0] = NONE;

0 commit comments

Comments
 (0)