-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathButton2D.cpp
More file actions
123 lines (108 loc) · 2.41 KB
/
Button2D.cpp
File metadata and controls
123 lines (108 loc) · 2.41 KB
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
#include "Button2D.h"
#include <ctime> // used for randomness
#include <algorithm>
#include <fstream>
#ifdef __APPLE__
# include <GLUT/glut.h>
#else
# include <GL/glut.h>
#endif
Button2D::Button2D()
{
xCenter = 0.0; yCenter = 0.0; zCenter = 0.0;
}
// Create a Button from the given coordinates
Button2D::Button2D(float x, float y, float z,
float width, float height,
char *n)
{
hWidth = width / 2; hHeight = height / 2;
xCenter = x; yCenter = y; zCenter = z;
left = xCenter - hWidth;
right = xCenter + hWidth;
top = yCenter + hHeight;
bottom = yCenter - hHeight;
text = n;
}
// Create a Button From the Given Boundaries
Button2D::Button2D(float l, float r, float b, float t, char *n, float z, char action)
{
left = l; right = r; bottom = b; top = t;
xCenter = (right + left) / 2;
yCenter = (top + bottom) / 2;
zCenter = z;
hWidth = (right - left) / 2;
hHeight = (top - bottom) / 2;
text = n;
this->action = action;
}
Button2D::~Button2D()
{
}
void Button2D::draw(bool blueGreen, float r, float g, float b)
{
glColor3f(1.0, 1.0, 1.0);
glRasterPos3f(xCenter - 0.2 * hWidth, yCenter, zCenter);
writeBitmapString(GLUT_BITMAP_HELVETICA_18, text);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
if (blueGreen)
{
// Flip the green & blue values for this time
if (green == 1)
green = 0;
else
green = 1;
if (blue == 1)
blue = 0;
else
blue = 1;
glColor3f(0.0, green, blue);
}
else
glColor3f(r, g, b);
glBegin(GL_TRIANGLE_FAN);
glVertex3f(left, bottom, zCenter);
glVertex3f(left, top, zCenter);
glVertex3f(right, top, zCenter);
glVertex3f(right, bottom, zCenter);
glVertex3f(left, bottom, zCenter);
glEnd();
}
void Button2D::updatePosition(float l, float r, float b, float t, float z)
{
left = l; right = r; bottom = b; top = t;
xCenter = (right + left) / 2;
yCenter = (top + bottom) / 2;
zCenter = z;
hWidth = (right - left) / 2;
hHeight = (top - bottom) / 2;
}
bool Button2D::containsCoordinate(float x, float y)
{
if (x >= left && x <= right)
{
if (y <= top && y >= bottom)
return true;
}
return false;
}
void Button2D::incrementOrDecrement(int &affectedNumber)
{
switch (action)
{
case 'i':
affectedNumber++;
break;
case 'd':
affectedNumber--;
break;
default:
break;
}
}
// Routine to draw a bitmap character string.
void Button2D::writeBitmapString(void *font, char *string)
{
char *c;
for (c = string; *c != '\0'; c++) glutBitmapCharacter(font, *c);
}