-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculator.pde
200 lines (172 loc) · 5.67 KB
/
Calculator.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
HashMap<String, PFont> fonts; //<>//
color darkestGrey;
color lightGrey;
color white;
color red;
color colorOfDot;
color colorOfEqual;
color colorOfMinus;
color colorOfMultiplication;
color colorOfPlus;
color colorOfDivision;
int DELAY_IN_SECONDS = 1;
int clickTimeOnDot, endTimeForDot, clickTimeOnPlusMinus, endTimeForPlusMinus, clickTimeOnEqual, endTimeForEqual;
PImage plusOrMinusDarkestGrey;
PImage plusOrMinusRed;
PImage plusOrMinus;
void setup() {
size(465, 620);
clickTimeOnDot = Integer.MIN_VALUE;
endTimeForDot = Integer.MIN_VALUE;
clickTimeOnPlusMinus = Integer.MIN_VALUE;
endTimeForPlusMinus = Integer.MIN_VALUE;
clickTimeOnEqual = Integer.MIN_VALUE;
endTimeForEqual = Integer.MIN_VALUE;
// loading custom fonts
fonts = new HashMap<String, PFont>();
StringList fontFileNames = new StringList("OpenSans-Regular.ttf", "OpenSans-Bold.ttf", "OpenSans-Light.ttf", "OpenSans-Semibold.ttf");
StringList fontNames = new StringList("openSans", "openSansBold", "openSansLight", "openSansSemibold");
String fontFilesRootPath = "fonts/";
for (int counter = 0; counter < fontNames.size(); counter++) {
fonts.put(fontNames.get(counter), createFont(fontFilesRootPath + fontFileNames.get(counter), 32, true));
}
// all colors
darkestGrey = #313131;
lightGrey = #E6E7E6;
white = #FFFFFF;
red = #FF4D19;
colorOfDot = darkestGrey;
// loading symbols
plusOrMinusDarkestGrey = loadImage("symbols/plus_or_minus_darkest_grey.png");
plusOrMinusRed = loadImage("symbols/plus_or_minus_red.png");
plusOrMinus = plusOrMinusDarkestGrey;
}
void draw() {
background(255);
drawRectangle(0, 563, 465, 57, #CECACE);
drawText("Clear", 15, 575, 25, fonts.get("openSansSemibold"), darkestGrey);
drawText("Clear All", 351, 575, 25, fonts.get("openSansSemibold"), darkestGrey);
drawRectangle(0, 313, 465, 250, lightGrey);
// point
noStroke();
drawCircle(33, 465, 70, 70, white);
setColorOfDot();
drawCircle(65, 497, 7, 7, colorOfDot);
// plus or minus
drawCircle(362, 465, 70, 70, white);
setColorOfPlusMinus();
image(plusOrMinus, 376.4, 465.6);
// common operator area
stroke(white);
strokeWeight(39);
drawCircle(138 + (39 / 2), 345 + (39 / 2), 151, 151, lightGrey);
// operators
setColorOfEqual();
drawText("=", 210, 386, 80, fonts.get("openSansLight"), colorOfEqual);
drawText("−", 220, 330, 48, fonts.get("openSans"), colorOfMinus);
drawText("×", 295, 408, 48, fonts.get("openSans"), colorOfMultiplication);
drawText("+", 220, 482, 48, fonts.get("openSans"), colorOfPlus);
drawText("÷", 144, 408, 48, fonts.get("openSans"), colorOfDivision);
if (isMouseInsideCircle(33 + 35, 465 + 35, 35)) {
cursor(HAND);
} else if (isMouseInsideCircle(362 + 35, 465 + 35, 35)) {
cursor(HAND);
} else if (isMouseInsideRectangle(213, 431, 38, 23)) {
cursor(HAND);
} else if (isMouseInsideRectangle(220, 352, 26, 26)) {
cursor(HAND);
} else if (isMouseInsideRectangle(295, 430, 26, 26)) {
cursor(HAND);
} else if (isMouseInsideRectangle(220, 504, 26, 26)) {
cursor(HAND);
} else if (isMouseInsideRectangle(144, 430, 26, 26)) {
cursor(HAND);
}
// in normal cases, cursor is always of type 'ARROW'
else {
cursor(ARROW);
}
}
void drawRectangle(float positionX, float positionY, float width, float height, color fill) {
noStroke();
fill(fill);
rect(positionX, positionY, width, height);
}
void drawText(String text, float positionX, float positionY, float fontSize, PFont font, color fill) {
textFont(font);
textSize(fontSize);
textAlign(LEFT, TOP);
fill(fill);
text(text, positionX, positionY);
}
void drawCircle(float coordinateX, float coordinateY, float width, float height, color fill) {
ellipseMode(CORNER);
fill(fill);
ellipse(coordinateX, coordinateY, width, height);
}
private boolean isMouseInsideCircle (float coordinateX, float coordinateY, int radius) {
float distance = sqrt(pow((mouseX - coordinateX), 2) + pow((mouseY - coordinateY), 2));
return distance <= radius ? true : false;
}
private boolean isMouseInsideRectangle (float coordinateX, float coordinateY, float width, float height) {
if (mouseX >= coordinateX && mouseX <= (coordinateX + width) && mouseY >= coordinateY && mouseY <= (coordinateY + height)) {
return true;
} else {
return false;
}
}
void mousePressed() {
// when pressed on dot
if (isMouseInsideCircle(33 + 35, 465 + 35, 35)) {
clickTimeOnDot = second();
endTimeForDot = getEndTime(clickTimeOnDot);
}
// when pressed on plus or minus
if (isMouseInsideCircle(362 + 35, 465 + 35, 35)) {
clickTimeOnPlusMinus = second();
endTimeForPlusMinus = getEndTime(clickTimeOnPlusMinus);
}
// when pressed on equal
if (isMouseInsideRectangle(213, 431, 38, 23)) {
clickTimeOnEqual = second();
endTimeForEqual = getEndTime(clickTimeOnEqual);
}
}
private void setColorOfDot() {
if (clickTimeOnDot >= 0) {
if (second() != endTimeForDot) {
colorOfDot = red;
} else {
colorOfDot = darkestGrey;
clickTimeOnDot = Integer.MIN_VALUE;
}
}
}
private void setColorOfEqual() {
if (clickTimeOnEqual >= 0) {
if (second() != endTimeForEqual) {
colorOfEqual = red;
} else {
colorOfEqual = darkestGrey;
clickTimeOnEqual = Integer.MIN_VALUE;
}
}
}
private void setColorOfPlusMinus() {
if (clickTimeOnPlusMinus >= 0) {
if (second() != endTimeForPlusMinus) {
plusOrMinus = plusOrMinusRed;
} else {
plusOrMinus = plusOrMinusDarkestGrey;
clickTimeOnPlusMinus = Integer.MIN_VALUE;
}
}
}
private int getEndTime(int clickTime) {
int endTime = clickTime + DELAY_IN_SECONDS;
if (endTime > 59) {
int remainder = endTime % 59;
endTime = remainder - 1;
}
return endTime;
}