forked from nigels-com/glui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
glui_mouse_iaction.cpp
225 lines (159 loc) · 6.24 KB
/
glui_mouse_iaction.cpp
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
/****************************************************************************
GLUI User Interface Toolkit
---------------------------
glui_mouse_iaction - GLUI Mouse Interaction control class
--------------------------------------------------
Copyright (c) 1998 Paul Rademacher
WWW: https://github.com/libglui/glui
Issues: https://github.com/libglui/glui/issues
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*****************************************************************************/
#include "glui_internal_control.h"
#include "tinyformat.h"
GLUI_Mouse_Interaction::GLUI_Mouse_Interaction()
{
name = tfm::format("Mouse_Interaction: %p", this );
w = GLUI_MOUSE_INTERACTION_WIDTH;
h = GLUI_MOUSE_INTERACTION_HEIGHT;
can_activate = true;
live_type = GLUI_LIVE_NONE;
alignment = GLUI_ALIGN_CENTER;
draw_active_area_only = false;
}
/********************** GLUI_Mouse_Interaction::mouse_down_handler() ******/
int GLUI_Mouse_Interaction::mouse_down_handler( int local_x, int local_y )
{
/* int win_h = glutGet( GLUT_WINDOW_HEIGHT ); */
/* iaction_mouse_down_handler( local_x, local_y ); */
iaction_mouse_down_handler( local_x-x_abs, local_y-y_abs );
/*local_x-x_abs, ((glui->h-local_y)-y_abs) ); */
redraw();
return false;
}
/**************************** GLUI_Mouse_Interaction::mouse_up_handler() */
int GLUI_Mouse_Interaction::mouse_up_handler( int local_x, int local_y, bool inside )
{
iaction_mouse_up_handler( local_x-x_abs, local_y-y_abs, inside );
return false;
}
/****************************** GLUI_Mouse_Interaction::mouse_held_down_handler() ******/
int GLUI_Mouse_Interaction::mouse_held_down_handler( int local_x, int local_y,
bool inside)
{
iaction_mouse_held_down_handler( local_x-x_abs, local_y-y_abs , inside );
redraw();
/** Tell the main graphics window to update iteself **/
if( glui )
glui->post_update_main_gfx();
execute_callback();
return false;
}
/****************************** GLUI_Mouse_Interaction::draw() **********/
void GLUI_Mouse_Interaction::draw( int x, int y )
{
GLUI_DRAWINGSENTINAL_IDIOM
int text_width = string_width( this->name );
int x_left = this->w/2 - text_width/2;
if ( NOT draw_active_area_only ) {
draw_name( x_left, h-4 );
draw_active_box( x_left-4, x_left+string_width( name )+4,
h, h-14 );
}
draw_active_area();
}
/************************************ GLUI_Mouse_Interaction::update_size() **********/
void GLUI_Mouse_Interaction::update_size()
{
if ( NOT glui )
return;
int text_width = string_width( this->name );
if ( w < text_width+6 )
w = text_width+6;
if ( h - 18 > w )
w = h - 18;
iaction_init();
}
/****************************** GLUI_Mouse_Interaction::special_handler() **********/
int GLUI_Mouse_Interaction::special_handler( int key,int modifiers )
{
int center_x, center_y;
int drag_x, drag_y;
center_x = w/2;
center_y = (h-18)/2;
drag_x = 0;
drag_y = 0;
if ( key == GLUT_KEY_LEFT )
drag_x = -6;
else if ( key == GLUT_KEY_RIGHT )
drag_x = 6;
else if ( key == GLUT_KEY_UP )
drag_y = -6;
else if ( key == GLUT_KEY_DOWN )
drag_y = 6;
if ( drag_x != 0 OR drag_y != 0 ) {
mouse_down_handler( center_x, center_y );
mouse_held_down_handler( center_x + drag_x, center_y + drag_y,true );
mouse_up_handler( center_x + drag_x, center_y + drag_y, true );
}
return false;
}
/****************************** GLUI_Mouse_Interaction::draw_active_area() **********/
void GLUI_Mouse_Interaction::draw_active_area()
{
int win_h = glutGet( GLUT_WINDOW_HEIGHT ), win_w = glutGet(GLUT_WINDOW_WIDTH);
int text_height = 18; /* what a kludge */
int viewport_size = h-text_height; /*MIN(w,h); */
glMatrixMode( GL_MODELVIEW );
glPushMatrix();
glLoadIdentity();
glTranslatef( (float) win_w/2.0, (float) win_h/2.0, 0.0 );
glRotatef( 180.0, 0.0, 1.0, 0.0 );
glRotatef( 180.0, 0.0, 0.0, 1.0 );
glTranslatef( (float) -win_w/2.0, (float) -win_h/2.0, 0.0 );
glTranslatef( (float) this->x_abs + .5, (float) this->y_abs + .5, 0.0 );
glTranslatef( (float)this->w/2.0, (float)viewport_size/2.0 + 2.0 , 0.0 );
/*** Draw the interaction control's orthographic elements ***/
iaction_draw_active_area_ortho();
/*** Setup and draw the interaction control's perspective elements ***/
/*** Set the viewport to just the square of the drawing area ***/
/* glViewport( this->x_abs , glui->main_panel->h - this->y_abs - this->h,*/
/*glViewport( this->x_abs+1+(this->w/2-viewport_size/2),
this->h-this->y_abs-viewport_size-1,
viewport_size, viewport_size );*/
viewport_size -= 4;
int offset = 0;
if ( ((this->w-viewport_size) % 2) == 1 )
offset = 1;
glViewport( this->x_abs + (this->w-viewport_size)/2 + offset,
win_h - this->y_abs - this->h + text_height,
viewport_size, viewport_size );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
double xy=1.00,zc=50.0; /* X-Y size, and Z origin */
glFrustum( -1.0*xy, 1.0*xy, -xy, xy, zc*0.7, zc*1.3 );
glMatrixMode( GL_MODELVIEW );
glPushMatrix();
glLoadIdentity();
glTranslatef( 0.0, 0.0, -zc );
glScalef(xy,xy,1.0); // xy);
/* glutSolidTeapot( 1.0 ); */
iaction_draw_active_area_persp();
glMatrixMode( GL_MODELVIEW );
glPopMatrix();
glui->set_viewport();
glui->set_ortho_projection();
glMatrixMode( GL_MODELVIEW );
glPopMatrix();
}