-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathButton.cpp
More file actions
612 lines (524 loc) · 13.8 KB
/
Button.cpp
File metadata and controls
612 lines (524 loc) · 13.8 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
/*----------------------------------------------------------------------------------------
*
* project: Comp 315 Game Dev
* author: Prashaan Pillay
/*----------------------------------------------------------------------------------------
* Includes
*/
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
using namespace std;
#ifdef WIN32
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#endif
#include <GL/glut.h>
/*----------------------------------------------------------------------------------------
* Global Variables
*/
/*
* A structure to represent the mouse information
*/
struct Mouse
{
int x; /* the x coordinate of the mouse cursor */
int y; /* the y coordinate of the mouse cursor */
int lmb; /* is the left button pressed? */
int mmb; /* is the middle button pressed? */
int rmb; /* is the right button pressed? */
int xpress; /* stores the x-coord of when the first button press occurred */
int ypress; /* stores the y-coord of when the first button press occurred */
};
/*
* rename the structure from "struct Mouse" to just "Mouse"
*/
typedef struct Mouse Mouse;
/*
* Create a global mouse structure to hold the mouse information.
*/
Mouse TheMouse = {0,0,0,0,0};
/*
* Variables to hold the current size of the window.
*/
int winw = 640;
int winh = 480;
/*----------------------------------------------------------------------------------------
* Button Stuff
*/
/*
* We will define a function pointer type. ButtonCallback is a pointer to a function that
* looks a bit like this :
*
* void func() {
* }
*/
typedef void (*ButtonCallback)();
/*
* This is a simple structure that holds a button.
*/
struct Button
{
int x; /* top left x coord of the button */
int y; /* top left y coord of the button */
int w; /* the width of the button */
int h; /* the height of the button */
int state; /* the state, 1 if pressed, 0 otherwise */
int highlighted; /* is the mouse cursor over the control? */
char* label; /* the text label of the button */
ButtonCallback callbackFunction; /* A pointer to a function to call if the button is pressed */
};
typedef struct Button Button;
void TheButtonCallback()
{
printf("I have been called\n");
}
//Define Button Objects
Button MyButton1 = {5,5, 100,25, 0,0, "Button", TheButtonCallback };
Button MyButton2 = {5,40, 100,25, 0,0, "Enter", TheButtonCallback };
/*----------------------------------------------------------------------------------------
* \brief This function draws a text string to the screen using glut bitmap fonts.
* \param font - the font to use. it can be one of the following :
*
* GLUT_BITMAP_9_BY_15
* GLUT_BITMAP_8_BY_13
* GLUT_BITMAP_TIMES_ROMAN_10
* GLUT_BITMAP_TIMES_ROMAN_24
* GLUT_BITMAP_HELVETICA_10
* GLUT_BITMAP_HELVETICA_12
* GLUT_BITMAP_HELVETICA_18
*
* \param text - the text string to output
* \param x - the x co-ordinate
* \param y - the y co-ordinate
*/
void Font(void *font,char *text,int x,int y)
{
glRasterPos2i(x, y);
while( *text != '\0' )
{
glutBitmapCharacter( font, *text );
++text;
}
}
/*----------------------------------------------------------------------------------------
* \brief This function is used to see if a mouse click or event is within a button
* client area.
* \param b - a pointer to the button to test
* \param x - the x coord to test
* \param y - the y-coord to test
*/
int ButtonClickTest(Button* b,int x,int y)
{
if( b)
{
/*
* If clicked within button area, then return true
*/
if( x > b->x &&
x < b->x+b->w &&
y > b->y &&
y < b->y+b->h ) {
return 1;
}
}
/*
* otherwise false.
*/
return 0;
}
/*----------------------------------------------------------------------------------------
* \brief This function draws the specified button.
* \param b - a pointer to the button to check.
* \param x - the x location of the mouse cursor.
* \param y - the y location of the mouse cursor.
*/
void ButtonRelease(Button *b,int x,int y)
{
if(b)
{
/*
* If the mouse button was pressed within the button area
* as well as being released on the button.....
*/
if( ButtonClickTest(b,TheMouse.xpress,TheMouse.ypress) &&
ButtonClickTest(b,x,y) )
{
/*
* Then if a callback function has been set, call it.
*/
if (b->callbackFunction) {
b->callbackFunction();
}
}
/*
* Set state back to zero.
*/
b->state = 0;
}
}
/*----------------------------------------------------------------------------------------
* \brief This function draws the specified button.
* \param b - a pointer to the button to check.
* \param x - the x location of the mouse cursor.
* \param y - the y location of the mouse cursor.
*/
void ButtonPress(Button *b,int x,int y)
{
if(b)
{
/*
* if the mouse click was within the buttons client area,
* set the state to true.
*/
if( ButtonClickTest(b,x,y) )
{
b->state = 1;
}
}
}
/*----------------------------------------------------------------------------------------
* \brief This function draws the specified button.
* \param b - a pointer to the button to check.
* \param x - the x location of the mouse cursor.
* \param y - the y location of the mouse cursor.
*/
void ButtonPassive(Button *b,int x,int y)
{
if(b)
{
/*
* if the mouse moved over the control
*/
if( ButtonClickTest(b,x,y) )
{
/*
* If the cursor has just arrived over the control, set the highlighted flag
* and force a redraw. The screen will not be redrawn again until the mouse
* is no longer over this control
*/
if( b->highlighted == 0 ) {
b->highlighted = 1;
glutPostRedisplay();
}
}
else
/*
* If the cursor is no longer over the control, then if the control
* is highlighted (ie, the mouse has JUST moved off the control) then
* we set the highlighting back to false, and force a redraw.
*/
if( b->highlighted == 1 )
{
b->highlighted = 0;
glutPostRedisplay();
}
}
}
/*----------------------------------------------------------------------------------------
* \brief This function draws the specified button.
* \param b - a pointer to the button to draw.
*/
void ButtonDraw(Button *b)
{
int fontx;
int fonty;
if(b)
{
/*
* We will indicate that the mouse cursor is over the button by changing its
* colour.
*/
if (b->highlighted)
glColor3f(0.7f,0.7f,0.8f);
else
glColor3f(0.6f,0.6f,0.6f);
/*
* draw background for the button.
*/
glBegin(GL_QUADS);
glVertex2i( b->x , b->y );
glVertex2i( b->x , b->y+b->h );
glVertex2i( b->x+b->w, b->y+b->h );
glVertex2i( b->x+b->w, b->y );
glEnd();
/*
* Draw an outline around the button with width 3
*/
glLineWidth(3);
/*
* The colours for the outline are reversed when the button.
*/
if (b->state)
glColor3f(0.4f,0.4f,0.4f);
else
glColor3f(0.8f,0.8f,0.8f);
glBegin(GL_LINE_STRIP);
glVertex2i( b->x+b->w, b->y );
glVertex2i( b->x , b->y );
glVertex2i( b->x , b->y+b->h );
glEnd();
if (b->state)
glColor3f(0.8f,0.8f,0.8f);
else
glColor3f(0.4f,0.4f,0.4f);
glBegin(GL_LINE_STRIP);
glVertex2i( b->x , b->y+b->h );
glVertex2i( b->x+b->w, b->y+b->h );
glVertex2i( b->x+b->w, b->y );
glEnd();
glLineWidth(1);
/*
* Calculate the x and y coords for the text string in order to center it.
*/
string str = b->label;
fontx = b->x + (b->w - glutBitmapLength(GLUT_BITMAP_HELVETICA_10,(const unsigned char *)str.c_str())) / 2 ;
fonty = b->y + (b->h+10)/2;
/*
* if the button is pressed, make it look as though the string has been pushed
* down. It's just a visual thing to help with the overall look....
*/
if (b->state) {
fontx+=2;
fonty+=2;
}
/*
* If the cursor is currently over the button we offset the text string and draw a shadow
*/
if(b->highlighted)
{
glColor3f(0,0,0);
Font(GLUT_BITMAP_HELVETICA_10,b->label,fontx,fonty);
fontx--;
fonty--;
}
glColor3f(1,1,1);
Font(GLUT_BITMAP_HELVETICA_10,b->label,fontx,fonty);
}
}
/*----------------------------------------------------------------------------------------
* \brief This function is called to initialise opengl.
*/
void Init()
{
glEnable(GL_LIGHT0);
}
/*----------------------------------------------------------------------------------------
* This function will be used to draw the 3D scene
*/
void Draw3D()
{
gluLookAt(0,1,5,0,0,0,0,1,0);
}
/*----------------------------------------------------------------------------------------
* This function will be used to draw an overlay over the 3D scene.
* This will be used to draw our fonts, buttons etc......
*/
void Draw2D()
{
ButtonDraw(&MyButton1);
ButtonDraw(&MyButton2);
}
/*----------------------------------------------------------------------------------------
* This is the main display callback function. It sets up the drawing for
* The 3D scene first then calls the Draw3D() function. After that it switches to
* an orthographic projection and calls Draw2D().
*/
void Draw()
{
/*
* Clear the background
*/
glClear( GL_COLOR_BUFFER_BIT |
GL_DEPTH_BUFFER_BIT );
/*
* Enable lighting and the z-buffer
*/
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
/*
* Set perspective viewing transformation
*/
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45,(winh==0)?(1):((float)winw/winh),1,100);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
/*
* Draw the 3D elements in the scene
*/
Draw3D();
/*
* Disable depth test and lighting for 2D elements
*/
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
/*
* Set the orthographic viewing transformation
*/
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,winw,winh,0,-1,1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
/*
* Draw the 2D overlay
*/
Draw2D();
/*
* Bring the back buffer to the front and vice-versa.
*/
glutSwapBuffers();
}
/*----------------------------------------------------------------------------------------
* This function is called when the window is resized. All this does is simply
* store the new width and height of the window which are then referenced by
* the draw function to set the correct viewing transforms
*/
void Resize(int w, int h)
{
winw = w;
winh = h;
/*
* Allow drawing in full region of the screen
*/
glViewport(0,0,w,h);
}
/*----------------------------------------------------------------------------------------
* \brief This function is called whenever a mouse button is pressed or released
* \param button - GLUT_LEFT_BUTTON, GLUT_RIGHT_BUTTON, or GLUT_MIDDLE_BUTTON
* \param state - GLUT_UP or GLUT_DOWN depending on whether the mouse was released
* or pressed respectivly.
* \param x - the x-coord of the mouse cursor.
* \param y - the y-coord of the mouse cursor.
*/
void MouseButton(int button,int state,int x, int y)
{
/*
* update the mouse position
*/
TheMouse.x = x;
TheMouse.y = y;
/*
* has the button been pressed or released?
*/
if (state == GLUT_DOWN)
{
/*
* This holds the location of the first mouse click
*/
if ( !(TheMouse.lmb || TheMouse.mmb || TheMouse.rmb) ) {
TheMouse.xpress = x;
TheMouse.ypress = y;
}
/*
* Which button was pressed?
*/
switch(button)
{
case GLUT_LEFT_BUTTON:
TheMouse.lmb = 1;
ButtonPress(&MyButton1,x,y);
ButtonPress(&MyButton2,x,y);
case GLUT_MIDDLE_BUTTON:
TheMouse.mmb = 1;
break;
case GLUT_RIGHT_BUTTON:
TheMouse.rmb = 1;
break;
}
}
else
{
/*
* Which button was released?
*/
switch(button)
{
case GLUT_LEFT_BUTTON:
TheMouse.lmb = 0;
ButtonRelease(&MyButton1,x,y);
ButtonRelease(&MyButton2,x,y);
break;
case GLUT_MIDDLE_BUTTON:
TheMouse.mmb = 0;
break;
case GLUT_RIGHT_BUTTON:
TheMouse.rmb = 0;
break;
}
}
/*
* Force a redraw of the screen. If we later want interactions with the mouse
* and the 3D scene, we will need to redraw the changes.
*/
glutPostRedisplay();
}
/*----------------------------------------------------------------------------------------
* \brief This function is called whenever the mouse cursor is moved AND A BUTTON IS HELD.
* \param x - the new x-coord of the mouse cursor.
* \param y - the new y-coord of the mouse cursor.
*/
void MouseMotion(int x, int y)
{
/*
* Calculate how much the mouse actually moved
*/
int dx = x - TheMouse.x;
int dy = y - TheMouse.y;
/*
* update the mouse position
*/
TheMouse.x = x;
TheMouse.y = y;
/*
* Check MyButton to see if we should highlight it cos the mouse is over it
*/
ButtonPassive(&MyButton1,x,y);
ButtonPassive(&MyButton2,x,y);
/*
* Force a redraw of the screen
*/
glutPostRedisplay();
}
/*----------------------------------------------------------------------------------------
* \brief This function is called whenever the mouse cursor is moved AND NO BUTTONS ARE HELD.
* \param x - the new x-coord of the mouse cursor.
* \param y - the new y-coord of the mouse cursor.
*/
void MousePassiveMotion(int x, int y)
{
/*
* Calculate how much the mouse actually moved
*/
int dx = x - TheMouse.x;
int dy = y - TheMouse.y;
/*
* update the mouse position
*/
TheMouse.x = x;
TheMouse.y = y;
/*
* Check MyButton to see if we should highlight it cos the mouse is over it
*/
ButtonPassive(&MyButton1,x,y);
ButtonPassive(&MyButton2,x,y);
}
/*----------------------------------------------------------------------------------------
*
*/
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DEPTH|GLUT_DOUBLE);
glutInitWindowSize(winw,winh);
glutInitWindowPosition(200,100);
glutCreateWindow("Button Engine");
glutDisplayFunc(Draw);
glutReshapeFunc(Resize);
glutMouseFunc(MouseButton);
glutMotionFunc(MouseMotion);
glutPassiveMotionFunc(MousePassiveMotion);
Init();
glutMainLoop();
}