-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbutton.cpp
More file actions
111 lines (96 loc) · 2.18 KB
/
button.cpp
File metadata and controls
111 lines (96 loc) · 2.18 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
/**
* Implementation of the button class
*/
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include "button.h"
#include <stdio.h>
#include <cstring>
/**
* Returns the width of a string if displayed in screen.
* Move to util
*/
float Button::str_width(const char* s) {
int len = strlen(s);
int total_width = 0;
for(int i = 0; i < len; i++) {
total_width += glutStrokeWidth(GLUT_STROKE_ROMAN, s[i]) ;
}
return total_width;
}
/**
* Draws text on screen.
* Transformations should be done before the function call.
* Move to util.
*/
void Button::draw_text(const char* s) {
int len = strlen(s);
for (int i = 0; i < len; i++) {
glutStrokeCharacter(GLUT_STROKE_ROMAN, s[i]);
}
}
/**
* Initialises a Button object with top, bottom, left, and right limits,
* a Destination representing the effect of the button, a name and a text size.
*/
Button::Button(int t, int b, int l, int r, Destination d, const char* n, float t_s) {
top = t;
bot = b;
left = l;
right = r;
dest = d;
next = NULL;
name = n;
text_size = t_s;
}
/**
* Releases memory.
* No memory being allocated at the moment.
*/
void Button::Delete() {}
/**
* Set the button to be displayed under this button.
*/
void Button::SetNext(Button* b) {
next = b;
}
/**
* Draws the button outline based on set limits and draws the centered
* button text.
*/
void Button::DrawButton() {
glBegin(GL_LINE_LOOP);
glVertex2f(left, top);
glVertex2f(right, top);
glVertex2f(right, bot);
glVertex2f(left, bot);
glEnd();
glPushMatrix();
float h_center_offset = -str_width(name)/(2/text_size);
glTranslatef(.0f + h_center_offset,
(top + bot) / 2 - (2/text_size),.0f);
glScalef(text_size, text_size, 1.0f);
draw_text(name);
glPopMatrix();
}
int Button::GetTop() {
return top;
}
int Button::GetBot() {
return bot;
}
int Button::GetLeft() {
return left;
}
int Button::GetRight() {
return right;
}
Button* Button::GetNext() {
return next;
}
Destination Button::GetDestination() {
return dest;
}