-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGUIItems.pde
146 lines (119 loc) · 3.13 KB
/
GUIItems.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
/** An itemBox contains several clickable buttons
*/
class ItemBox{
int x,y = 10;
int boxWidth = 100;
int boxHeight = 100;
Button[] buildingButtons;
int numberOfBuildingButtons;
int buttonSize = 50;
ItemBox(){
this(50);
}
ItemBox(int buttonSize){
this.buttonSize=buttonSize;
boxWidth = buttonSize;
x= width -10 - boxWidth;
numberOfBuildingButtons = buildingPlans.length;
boxHeight = buttonSize * numberOfBuildingButtons;
buildingButtons = new Button[numberOfBuildingButtons];
for( int i= 0; i < numberOfBuildingButtons; i++){ // zeilen
// for( int j= 0; j<1; j++){ // spalten
buildingButtons[i] = new BuildingButton(x,y+buttonSize*i, buttonSize, buildingPlans[i]);
}
// }
}
void draw(){
for( int i= 0; i < numberOfBuildingButtons; i++){ // draw all buttons
buildingButtons[i].draw();
}
}
}
/** generic class to create a square button, that changes color on mouseover */
class Button{
int buttonSize = 50;
int x, y; // on screen position
color buttonColor;
color borderColor;
color highlightColor;
color currentColor;
boolean over = false;
Button(int x, int y){
this(x,y,50);
}
Button(int x, int y, int buttonSize){
this.buttonSize = buttonSize;
this.x=x;
this.y=y;
buttonColor = color(200);
borderColor = color(255);
highlightColor = color(100);
currentColor = buttonColor;
}
void draw(){
this.update();
stroke(borderColor);
fill(currentColor);
rect(x,y,buttonSize, buttonSize);
}
void update() // update color depending on mouseover
{
if(over()) {
currentColor = highlightColor;
}
else {
currentColor = buttonColor;
}
}
boolean over(){ // test for mouse over
if (mouseX >= x && mouseX <= x+buttonSize &&
mouseY >= y && mouseY <= y+buttonSize) {
return true;
}
else {
return false;
}
}
}
class BuildingButton extends Button{
Building building;
PShape shape;
BuildingButton(int x, int y){
this(x,y,50);
}
BuildingButton(int x, int y, int buttonSize){
super(x,y, buttonSize);
building = new Building();
}
BuildingButton(int x, int y, int buttonSize, Building building){
super(x,y, buttonSize);
this.building = building;
}
void draw(){
super.draw();
// println("Drawing: " + building.name);
float ratio = building.buildingShape.height/building.buildingShape.width;
float drawX;
float drawY;
float distY;
float distX;
if (ratio < 1){ // width > height
drawX = buttonSize-10;
drawY = drawX*ratio;
}
else{ // width < height
drawY = buttonSize-10;
drawX = drawY/ratio;
}
distX = x + buttonSize/2.0 - drawX/2.0;
distY = y + buttonSize/2.0 - drawY/2.0;
shape(building.buildingShape,distX,distY,drawX,drawY);
}
void update(){
super.update();
if (over() && mousePressed){ // test if this button is clicked
draggedBuilding = building.clone(); // create draggable building
draggedBuilding.setCenter(mouseX,mouseY);
}
}
}