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
|
/************************************************************************
This is a simple program which demonstrates the use of the MxGUI
minimalist GUI framework. The application presents a window with a
rotating square that can be moved around with the mouse.
by Michael Garland, 1999.
$Id: t-gui.cxx 400 2004-02-16 16:31:35Z garland $
************************************************************************/
#include <gfx/gfx.h>
#include <gfx/gui.h>
#include <gfx/gltools.h>
class GUI : public MxGUI
{
public:
float angle, opt_theta, center[2];
bool dragging;
public:
virtual void setup_for_drawing();
virtual void draw_contents();
virtual void update_animation();
virtual bool mouse_down(int *where, int which);
virtual bool mouse_up(int *where, int which);
virtual bool mouse_drag(int *where, int *last, int which);
};
GUI gui;
void GUI::setup_for_drawing()
{
glClearColor(0.65f, 0.65f, 0.65f, 0.0f);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(-1.0, 1.0, -1.0, 1.0);
}
void GUI::draw_contents()
{
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_LINES);
glVertex2f(-1.0, 0.0);
glVertex2f(1.0, 0.0);
glVertex2f(0.0, -1.0);
glVertex2f(0.0, 1.0);
glEnd();
glTranslatef(center[0], center[1], 0);
glRotatef(angle, 0, 0, 1);
glEnable(GL_BLEND);
glColor4d(0.8, 0.15, 0.15, 0.85);
glBegin(dragging?GL_LINE_LOOP:GL_POLYGON);
glBegin(GL_POLYGON);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, 0.5);
glVertex2f(0.5, 0.5);
glVertex2f(0.5, -0.5);
glEnd();
glDisable(GL_BLEND);
glPopMatrix();
}
void GUI::update_animation()
{
angle += opt_theta;
}
static
bool center_on_click(float *ctr, int *where)
{
double world[3];
unproject_pixel(where, world);
ctr[0] = (float)world[0];
ctr[1] = (float)world[1];
return true;
}
bool GUI::mouse_down(int *where, int which)
{
status("Clicked mouse %d at %d,%d", which, where[0], where[1]);
if( which==1 )
{
dragging = true;
return center_on_click(center, where);
}
else return false;
}
bool GUI::mouse_up(int *where, int which)
{
status("Released mouse %d at %d,%d", which, where[0], where[1]);
dragging = false;
return (which==1);
}
bool GUI::mouse_drag(int *where, int *last, int which)
{
if( which==1 )
return center_on_click(center, where);
else
return false;
}
int main(int argc, char **argv)
{
gui.opt_theta = 10.0f;
gui.angle = 0.0f;
gui.dragging = false;
gui.center[0] = gui.center[1] = 0.0f;
gui.initialize(argc, argv);
gui.toplevel->label("Simple GUI Example");
return gui.run();
}
|