-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController.cpp
More file actions
197 lines (172 loc) · 4.2 KB
/
Controller.cpp
File metadata and controls
197 lines (172 loc) · 4.2 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
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
/*
* Controller.cpp
* 3d Ink
*
* Created by David Roberts on 26/07/2009.
*
*/
#include "Controller.h"
#if !defined(GLUT_WHEEL_UP)
# define GLUT_WHEEL_UP 3
# define GLUT_WHEEL_DOWN 4
#endif
bool Controller::pressed = false;
bool Controller::scrollDown = false;
bool Controller::scrollUp = false;
GLfloat Controller::z = 700;
int Controller::xMouse = 0;
int Controller::yMouse = 0;
int Controller::xTablet = 0;
int Controller::yTablet = 0;
int Controller::noTabletButtons = 0;
bool newLine = false;
//if true, pen, else eraser
bool Controller::penType = true;
bool Controller::shape = false;
GLfloat Controller::pointSize = 3.0f;
Shape Controller::shapeType = Undefined;
Point Controller::startRect;
Point Controller::endRect;
Controller::Controller() {
}
Controller::~Controller() {
}
/* Handles mouse button events */
void Controller::processMouseButton(int button, int state, int x, int y) {
switch (button) {
case GLUT_LEFT_BUTTON:
//printf("Mouse x=%d y=%d \n", x, y);
if(state==GLUT_DOWN) {
pressed = true;
newLine = true;
// If user has selected shape, then need to get start and end points
if (shape) {
switch (shapeType) {
case Rectangle:
printf("Tried to make a rectangle \n");
startRect = scaleCoords(x, y);
break;
default:
break;
}
}
}
if(state==GLUT_UP) {
pressed = false;
// When mouse is lifted, put a blank space
// we can then use GL_LINE when drawing
if(newLine==true&&Settings::dataFlag==LIST&&!shape) {
Point p;
p.blankPoint = true;
Model::getInstance()->addPoint(p);
newLine = false;
}
else if (shape) {
endRect = scaleCoords(x, y);
printf("Got to the end of the rect \n");
Model::getInstance()->addRectangle(startRect, endRect);
}
}
break;
case GLUT_WHEEL_UP:
scrollUp = true;
break;
case GLUT_WHEEL_DOWN:
scrollDown = true;
break;
default:
break;
}
}
/* Handles movement of the mouse when a button is clicked */
void Controller::processMouseActiveMotion(int x, int y) {
xMouse=x;
yMouse=y;
// If drawing with pen and no shape
if(pressed &!shape) {
Point p;
p = scaleCoords(x, y);
if(penType) {
//Add point to the model
Model::getInstance()->addPoint(p);
}
else {
Model::getInstance()->deletePoint(&p);
}
}
// If scrolling up, go into screen else go out
if(scrollUp) {
z-=10;
scrollUp = false;
}
if(scrollDown) {
z+=10;
scrollDown = false;
}
}
/* Handles movement of a mouse when no button is clicked */
void Controller::processMousePassiveMotion(int x, int y) {
xMouse = x;
yMouse = y;
}
/* Handles standard keyboard events */
void Controller::handleKeyboard(unsigned char key, int xpos, int ypos) {
// elegent eh?
if(key==27)
exit(0);
}
/* Handles special keyboard events (ALT, UP, etc...) */
void Controller::handleSpecialKeys(int key, int x, int y) {
switch (key) {
case GLUT_KEY_UP:
if (z==(Settings::viewingDistance+Settings::gpdInFront-10)) {
z = z;
}
else {
z+=5;
}
//printf("Z value is %f \n", z);
break;
case GLUT_KEY_DOWN:
if (z==(Settings::viewingDistance-Settings::gpdBehind)) {
z = z;
}
else {
z-=5;
}
//printf("Z value is %f \n", z);
break;
default:
break;
}
}
/* Handles tablet button events */
void Controller::processTabletButton(int button, int state, int x, int y) {
//TODO: implement processTabletButton
// GLUT has bad tablet handling!
}
/* Handles tablet movement */
void Controller::processTabletMotion(int x, int y) {
xTablet=x;
yTablet=y;
Point p;
p = scaleCoords(x, y);
// Add the point to the Model
Model::getInstance()->addPoint(p);
}
/* Handles scaling of coordinates */
Point Controller::scaleCoords(int x, int y) {
Point p;
GLdouble scale = (((z*tan(Model::getInstance()->vAngle/2))*2)/Settings::displayWidth);
p.x = (((Settings::pixelWidth/2)-xMouse)/
(Settings::pixelWidth/Settings::displayWidth))*scale;
p.y = (((Settings::pixelHeight/2)-yMouse)/
(Settings::pixelWidth/Settings::displayWidth))*scale;
p.z = z;
p.blankPoint = false;
p.pointSize = Controller::pointSize;
//printf("Point added is x=%f y=%f z=%f \n", p.x, p.y, p.z);
p.pixelX = x;
p.pixelY = y;
return p;
}