~ubuntu-branches/ubuntu/edgy/glui/edgy

« back to all changes in this revision

Viewing changes to glui_translation.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Marcelo E. Magallon
  • Date: 2001-09-23 12:57:28 UTC
  • Revision ID: james.westby@ubuntu.com-20010923125728-qbts7xit2eg1ogo2
Tags: upstream-2.1.0.beta
ImportĀ upstreamĀ versionĀ 2.1.0.beta

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
  
 
3
  GLUI User Interface Toolkit
 
4
  ---------------------------
 
5
 
 
6
     glui_translation - GLUI_Translation control class
 
7
 
 
8
 
 
9
          --------------------------------------------------
 
10
 
 
11
  Copyright (c) 1998 Paul Rademacher
 
12
 
 
13
  This program is freely distributable without licensing fees and is
 
14
  provided without guarantee or warrantee expressed or implied. This
 
15
  program is -not- in the public domain.
 
16
 
 
17
*****************************************************************************/
 
18
 
 
19
#include "glui.h"
 
20
#include "stdinc.h"
 
21
#include "algebra3.h"
 
22
 
 
23
/********************** GLUI_Translation::iaction_mouse_down_handler() ***/
 
24
/*  These are really in local coords (5/10/99)                            */
 
25
 
 
26
int    GLUI_Translation::iaction_mouse_down_handler( int local_x, 
 
27
                                                     int local_y )
 
28
{
 
29
  int center_x, center_y;
 
30
 
 
31
  down_x = local_x;
 
32
  down_y = local_y;
 
33
 
 
34
  if ( trans_type == GLUI_TRANSLATION_XY ) {
 
35
    orig_x = float_array_val[0];
 
36
    orig_y = float_array_val[1];
 
37
 
 
38
    /** Check if the Alt key is down, which means lock to an axis **/
 
39
 
 
40
    center_x = w/2;
 
41
    center_y = (h-18)/2;
 
42
 
 
43
    if ( glui->curr_modifiers & GLUT_ACTIVE_ALT ) {
 
44
      if ( ABS(local_y-center_y) > ABS(local_x-center_x) ) {
 
45
        locked = GLUI_TRANSLATION_LOCK_Y;
 
46
        glutSetCursor( GLUT_CURSOR_UP_DOWN );
 
47
      }
 
48
      else {
 
49
        locked = GLUI_TRANSLATION_LOCK_X;
 
50
        glutSetCursor( GLUT_CURSOR_LEFT_RIGHT );
 
51
      }
 
52
    }
 
53
    else {
 
54
      locked = GLUI_TRANSLATION_LOCK_NONE;
 
55
      glutSetCursor( GLUT_CURSOR_SPRAY );
 
56
    }
 
57
  }
 
58
  else if ( trans_type == GLUI_TRANSLATION_X ) {
 
59
    glutSetCursor( GLUT_CURSOR_LEFT_RIGHT );
 
60
    orig_x = float_array_val[0];
 
61
  }
 
62
  else if ( trans_type == GLUI_TRANSLATION_Y ) {
 
63
    glutSetCursor( GLUT_CURSOR_UP_DOWN );
 
64
    orig_y = float_array_val[0];
 
65
  }
 
66
  else if ( trans_type == GLUI_TRANSLATION_Z ) {
 
67
    glutSetCursor( GLUT_CURSOR_UP_DOWN );
 
68
    orig_z = float_array_val[0];
 
69
  }
 
70
 
 
71
  trans_mouse_code = 1;
 
72
  translate_and_draw_front();
 
73
 
 
74
  return false;
 
75
}
 
76
 
 
77
 
 
78
/*********************** GLUI_Translation::iaction_mouse_up_handler() **********/
 
79
 
 
80
int    GLUI_Translation::iaction_mouse_up_handler( int local_x, int local_y, 
 
81
                                                   int inside )
 
82
{
 
83
  trans_mouse_code = GLUI_TRANSLATION_MOUSE_NONE;
 
84
  locked = GLUI_TRANSLATION_LOCK_NONE;
 
85
 
 
86
  translate_and_draw_front();
 
87
 
 
88
  return false;
 
89
}
 
90
 
 
91
 
 
92
/******************* GLUI_Translation::iaction_mouse_held_down_handler() ******/
 
93
 
 
94
int    GLUI_Translation::iaction_mouse_held_down_handler( int local_x, int local_y,
 
95
                                                          int inside)
 
96
{  
 
97
  float x_off, y_off;
 
98
  float off_array[2];
 
99
 
 
100
  x_off = scale_factor * (float)(local_x - down_x);
 
101
  y_off = -scale_factor * (float)(local_y - down_y);
 
102
 
 
103
  if ( glui->curr_modifiers & GLUT_ACTIVE_SHIFT ) {
 
104
    x_off *= 100.0f;
 
105
    y_off *= 100.0f;
 
106
  }
 
107
  else if ( glui->curr_modifiers & GLUT_ACTIVE_CTRL ) {
 
108
    x_off *= .01f;
 
109
    y_off *= .01f;
 
110
  }
 
111
                
 
112
 
 
113
  if ( trans_type == GLUI_TRANSLATION_XY ) {
 
114
 
 
115
    if ( locked == GLUI_TRANSLATION_LOCK_X )
 
116
      y_off = 0.0;
 
117
    else if ( locked == GLUI_TRANSLATION_LOCK_Y )
 
118
      x_off = 0.0;
 
119
 
 
120
    off_array[0] = x_off + orig_x;
 
121
    off_array[1] = y_off + orig_y;
 
122
  }
 
123
  else if ( trans_type == GLUI_TRANSLATION_X ) {
 
124
    off_array[0] = x_off + orig_x;
 
125
  }
 
126
  else if ( trans_type == GLUI_TRANSLATION_Y ) {
 
127
    off_array[0] = y_off + orig_y;
 
128
  }
 
129
  else if ( trans_type == GLUI_TRANSLATION_Z ) {
 
130
    off_array[0] = y_off + orig_z;
 
131
  }
 
132
 
 
133
  set_float_array_val( (float*) &off_array[0] );
 
134
 
 
135
  return false;
 
136
}
 
137
 
 
138
 
 
139
/******************** GLUI_Translation::iaction_draw_active_area_persp() **************/
 
140
 
 
141
void    GLUI_Translation::iaction_draw_active_area_persp( void )
 
142
{
 
143
  if ( NOT can_draw() )
 
144
    return;
 
145
}
 
146
 
 
147
 
 
148
/******************** GLUI_Translation::iaction_draw_active_area_ortho() **********/
 
149
 
 
150
void    GLUI_Translation::iaction_draw_active_area_ortho( void )
 
151
{
 
152
  if ( NOT can_draw() )
 
153
    return;
 
154
 
 
155
  /********* Draw emboss circles around arcball control *********/
 
156
  float radius;
 
157
  radius = (float)(h-22)/2.0;  /*  MIN((float)w/2.0, (float)h/2.0); */
 
158
  glLineWidth( 1.0 );
 
159
 
 
160
  draw_emboss_box( (int) -radius-2, (int)radius+2, 
 
161
                   (int)-radius-2, (int)radius+2 );
 
162
 
 
163
  glMatrixMode( GL_MODELVIEW );
 
164
  glPushMatrix();
 
165
  glTranslatef( .5, .5, .5 );
 
166
  /*  glScalef( radius-1.0, radius-1.0, radius-1.0 ); */
 
167
  if ( trans_type == GLUI_TRANSLATION_Z )
 
168
    draw_2d_z_arrows((int)radius-1);
 
169
  else if ( trans_type == GLUI_TRANSLATION_XY )
 
170
    draw_2d_xy_arrows((int)radius-1);
 
171
  else if ( trans_type == GLUI_TRANSLATION_X )
 
172
    draw_2d_x_arrows((int)radius-1);
 
173
  else if ( trans_type == GLUI_TRANSLATION_Y )
 
174
    draw_2d_y_arrows((int)radius-1);
 
175
 
 
176
  glPopMatrix();
 
177
}
 
178
 
 
179
 
 
180
/******************************** GLUI_Translation::iaction_dump() **********/
 
181
 
 
182
void     GLUI_Translation::iaction_dump( FILE *output )
 
183
{
 
184
}
 
185
 
 
186
 
 
187
/******************** GLUI_Translation::iaction_special_handler() **********/
 
188
 
 
189
int    GLUI_Translation::iaction_special_handler( int key,int modifiers )
 
190
{
 
191
 
 
192
  return false;
 
193
}
 
194
 
 
195
 
 
196
 
 
197
/*************************** GLUI_Translation::draw_2d_z_arrows() **************/
 
198
 
 
199
void    GLUI_Translation::draw_2d_z_arrows( int radius )
 
200
{
 
201
  if ( trans_mouse_code != GLUI_TRANSLATION_MOUSE_NONE ) {
 
202
    draw_2d_arrow(radius, true, 2);
 
203
    draw_2d_arrow(radius, true, 0);
 
204
  }
 
205
  else {
 
206
    draw_2d_arrow(radius, false, 2);
 
207
    draw_2d_arrow(radius, false, 0);
 
208
  }
 
209
}
 
210
 
 
211
 
 
212
/*************************** GLUI_Translation::draw_2d_x_arrows() **************/
 
213
 
 
214
void    GLUI_Translation::draw_2d_x_arrows( int radius )
 
215
{
 
216
  if ( trans_mouse_code != GLUI_TRANSLATION_MOUSE_NONE ) {
 
217
    draw_2d_arrow(radius, true, 1);
 
218
    draw_2d_arrow(radius, true, 3);
 
219
  }
 
220
  else {
 
221
    draw_2d_arrow(radius, false, 1);
 
222
    draw_2d_arrow(radius, false, 3);
 
223
  }
 
224
}
 
225
 
 
226
 
 
227
/*************************** GLUI_Translation::draw_2d_y_arrows() **************/
 
228
 
 
229
void    GLUI_Translation::draw_2d_y_arrows( int radius )
 
230
{
 
231
  if ( trans_mouse_code != GLUI_TRANSLATION_MOUSE_NONE ) {
 
232
    draw_2d_arrow(radius, true, 0);
 
233
    draw_2d_arrow(radius, true, 2);
 
234
  }
 
235
  else {
 
236
    draw_2d_arrow(radius, false, 0);
 
237
    draw_2d_arrow(radius, false, 2);
 
238
  }
 
239
}
 
240
 
 
241
 
 
242
/************************** GLUI_Translation::draw_2d_xy_arrows() **************/
 
243
 
 
244
void    GLUI_Translation::draw_2d_xy_arrows( int radius)
 
245
{
 
246
  if ( trans_mouse_code != GLUI_TRANSLATION_MOUSE_NONE ) {
 
247
    if ( locked == GLUI_TRANSLATION_LOCK_X ) {
 
248
      draw_2d_arrow(radius, false, 0);
 
249
      draw_2d_arrow(radius, false, 2);
 
250
      draw_2d_arrow(radius, true, 1);
 
251
      draw_2d_arrow(radius, true, 3);
 
252
    }
 
253
    else if ( locked == GLUI_TRANSLATION_LOCK_Y ) {
 
254
      draw_2d_arrow(radius, false, 1);
 
255
      draw_2d_arrow(radius, false, 3);
 
256
      draw_2d_arrow(radius, true, 0);
 
257
      draw_2d_arrow(radius, true, 2);
 
258
    }
 
259
    else {
 
260
      draw_2d_arrow(radius, true, 0);
 
261
      draw_2d_arrow(radius, true, 1);
 
262
      draw_2d_arrow(radius, true, 2);
 
263
      draw_2d_arrow(radius, true, 3);
 
264
    }
 
265
  }
 
266
  else {
 
267
    draw_2d_arrow(radius, false, 0);
 
268
    draw_2d_arrow(radius, false, 1);
 
269
    draw_2d_arrow(radius, false, 2);
 
270
    draw_2d_arrow(radius, false, 3);
 
271
  }
 
272
 
 
273
  return;
 
274
}
 
275
 
 
276
 
 
277
/*************************** GLUI_Translation::draw_2d_arrow() **************/
 
278
/* ori: 0=up, 1=left, 2=down, 3=right                                       */
 
279
/*                                                                          */
 
280
/*                                                                          */
 
281
/*                           0, y2                                          */
 
282
/*                      /            \                                      */
 
283
/*                     /              \                                     */
 
284
/*                    /                \                                    */
 
285
/*                   /                  \                                   */
 
286
/*                  /                    \                                  */
 
287
/*                 /                      \                                 */
 
288
/*                /                        \                                */
 
289
/*               /                          \                               */
 
290
/*            -x2,y1   -x1b,y1   x1b,y1     x2,y1                           */
 
291
/*                        |        |                                        */
 
292
/*                        |        |                                        */
 
293
/*                        |        |                                        */
 
294
/*                        |        |                                        */
 
295
/*                        |        |                                        */
 
296
/*                    -x1a,y0    x1a,y0                                     */
 
297
/*                                                                          */
 
298
 
 
299
 
 
300
void    GLUI_Translation::draw_2d_arrow( int radius, int filled, int orientation )
 
301
{
 
302
  float x1 = .2, x2 = .4, y1 = .54, y2 = .94, y0;
 
303
  float x1a, x1b;
 
304
  vec3  col1( 0.0, 0.0, 0.0 ), col2( .45, .45, .45 ), 
 
305
    col3( .7, .7, .7 ), col4( 1.0, 1.0, 1.0 );
 
306
  vec3  c1, c2, c3, c4, c5, c6;
 
307
  vec3  white(1.0,1.0,1.0), black(0.0,0.0,0.0), gray(.45,.45,.45), 
 
308
    bkgd(.7,.7,.7);
 
309
  int   c_off; /* color index offset */
 
310
 
 
311
  if ( glui )
 
312
    bkgd.set(glui->bkgd_color_f[0],
 
313
             glui->bkgd_color_f[1],
 
314
             glui->bkgd_color_f[2]);
 
315
 
 
316
  /*    bkgd[0] = 255.0; bkgd[1] = 0;              */
 
317
 
 
318
  /** The following 8 colors define the shading of an octagon, in
 
319
    clockwise order, starting from the upstroke on the left  **/
 
320
  /** This is for an outside and inside octagons **/
 
321
  vec3 colors_out[]={white, white, white, gray, black, black, black, gray};
 
322
  vec3 colors_in[] ={bkgd,white,bkgd,gray,gray,gray,gray,gray};
 
323
 
 
324
#define SET_COL_OUT(i) glColor3fv((float*) &colors_out[(i)%8][0]);
 
325
#define SET_COL_IN(i) glColor3fv((float*) &colors_in[(i)%8][0]);
 
326
 
 
327
  x1 = (float)radius * .2;
 
328
  x2 = x1 * 2;
 
329
  y1 = (float)radius * .54;
 
330
  y2 = y1 + x2;
 
331
  x1a = x1;
 
332
  x1b = x1;
 
333
 
 
334
  glMatrixMode(GL_MODELVIEW);
 
335
  glPushMatrix();
 
336
 
 
337
#define DRAW_SEG( xa,ya,xb,yb ) glVertex2f(xa,ya); glVertex2f(xb,yb);
 
338
 
 
339
  glScalef( -1.0, 1.0, 1.0 );
 
340
        
 
341
  if ( orientation == 2 ) {
 
342
    c_off = 4;
 
343
  }
 
344
  else if ( orientation == 0 ) {
 
345
    c_off = 0;
 
346
    glRotatef( 180.0, 0.0, 0.0, 1.0 );
 
347
  }
 
348
  else if ( orientation == 1 ) {
 
349
    c_off = 2;
 
350
    glRotatef( 90.0, 0.0, 0.0, 1.0 );
 
351
  }
 
352
  else if ( orientation == 3 ) {
 
353
    c_off = 6;
 
354
    glRotatef( -90.0, 0.0, 0.0, 1.0 );
 
355
  }
 
356
 
 
357
  if ( trans_type == GLUI_TRANSLATION_Z )
 
358
    y0 = 0.0;
 
359
  else if ( trans_type == GLUI_TRANSLATION_XY )
 
360
    y0 = x1;
 
361
  else
 
362
    y0 = 0.0;
 
363
 
 
364
        
 
365
  if ( trans_type == GLUI_TRANSLATION_Z ) {
 
366
    if ( orientation == 0 ) {
 
367
      y1 += 2.0;
 
368
      y2 += 0.0;
 
369
 
 
370
      x1b -= 2.0;
 
371
      x2  -= 2.0;
 
372
      x1a += 2.0;
 
373
    }
 
374
    else if ( orientation == 2 ) {
 
375
      y1 -= 6.0;
 
376
      x1a += 2.0;
 
377
      x1b += 4.0;
 
378
      x2  += 6.0; 
 
379
    }
 
380
  }
 
381
 
 
382
  /*** Fill in inside of arrow  ***/
 
383
  if ( NOT filled ) {  /*** Means button is up - control is not clicked ***/
 
384
    /*glColor3f( .8, .8, .8 );              */
 
385
    set_to_bkgd_color();
 
386
    glColor3f( bkgd[0]+.07, bkgd[1]+.07, bkgd[2]+.07 );
 
387
  }
 
388
  else {               /*** Button is down on control ***/
 
389
    glColor3f( .6, .6, .6 );
 
390
    c_off += 4;  /* Indents the shadows - goes from a raised look to embossed */
 
391
  }
 
392
 
 
393
  /*** Check if control is enabled or not ***/
 
394
  if ( NOT enabled ) {
 
395
    set_to_bkgd_color();
 
396
    /*c_off += 4;  -- Indents the shadows - goes from a raised look to embossed */              
 
397
    colors_out[0] = colors_out[1] = colors_out[2] = colors_out[7] = gray;
 
398
    colors_out[3] = colors_out[4] = colors_out[5] = colors_out[6] = white;
 
399
    colors_in[0] = colors_in[1] = colors_in[2] = colors_in[7] = white;
 
400
    colors_in[3] = colors_in[4] = colors_in[5] = colors_in[6] = gray;
 
401
        
 
402
  }
 
403
 
 
404
  glBegin( GL_POLYGON );
 
405
  glVertex2f( 0.0, 0.0  );  glVertex2f( -x1a, 0.0 );
 
406
  glVertex2f( -x1a, 0.0   );  glVertex2f( -x1b, y1 );
 
407
  glVertex2f( x1b, y1);      glVertex2f( x1a, 0.0 );
 
408
  glVertex2f( x1a, 0.0 );     glVertex2f( 0.0, 0.0  );
 
409
  glEnd();
 
410
  glBegin( GL_TRIANGLES );
 
411
  glVertex2f( -x2, y1 ); glVertex2f( 0.0, y2 ); glVertex2f( x2, y1 );
 
412
  glEnd();
 
413
 
 
414
  glLineWidth( 1.0 );
 
415
  /*** Draw arrow outline ***/
 
416
  glBegin( GL_LINES );
 
417
 
 
418
  SET_COL_IN(1+c_off);  DRAW_SEG( 0.0, y2-1.0, -x2, y1-1.0 );
 
419
  SET_COL_IN(6+c_off);  DRAW_SEG( -x2+2.0, y1+1.0, -x1b+1.0, y1+1.0 );
 
420
  SET_COL_IN(0+c_off);  DRAW_SEG( -x1b+1.0, y1+1.0, -x1a+1.0, y0 );
 
421
  SET_COL_IN(3+c_off);  DRAW_SEG( 0.0, y2-1.0, x2, y1-1.0 );
 
422
  SET_COL_IN(6+c_off);  DRAW_SEG( x2-1.0, y1+1.0, x1b-1.0, y1+1.0 );
 
423
  SET_COL_IN(4+c_off);  DRAW_SEG( x1b-1.0, y1+1.0, x1a-1.0, y0 );
 
424
 
 
425
  SET_COL_OUT(0+c_off);  DRAW_SEG( -x1a, y0, -x1b, y1  );
 
426
  SET_COL_OUT(6+c_off);  DRAW_SEG( -x1b, y1,  -x2, y1  );
 
427
  SET_COL_OUT(1+c_off);  DRAW_SEG( -x2, y1,  0.0, y2  );
 
428
  SET_COL_OUT(3+c_off);  DRAW_SEG( 0.0, y2,   x2, y1  );
 
429
  SET_COL_OUT(6+c_off);  DRAW_SEG(  x2, y1,   x1b, y1  );
 
430
  SET_COL_OUT(4+c_off);  DRAW_SEG(  x1b, y1,   x1a, y0 );
 
431
 
 
432
  glEnd();
 
433
 
 
434
#undef DRAW_SEG
 
435
 
 
436
  glPopMatrix();
 
437
}
 
438
 
 
439
 
 
440
/*************************** GLUI_Translation::get_mouse_code() *************/
 
441
 
 
442
int    GLUI_Translation::get_mouse_code( int x, int y )
 
443
{
 
444
  if ( x == 0 AND y < 0 )
 
445
    return GLUI_TRANSLATION_MOUSE_DOWN;
 
446
  else if ( x == 0 AND y > 0 )
 
447
    return GLUI_TRANSLATION_MOUSE_UP;           
 
448
  else if ( x > 0 AND y == 0 )
 
449
    return GLUI_TRANSLATION_MOUSE_LEFT;         
 
450
  else if ( x < 0 AND y == 0 )
 
451
    return GLUI_TRANSLATION_MOUSE_RIGHT;                
 
452
  else if ( x < 0 AND y < 0 )
 
453
    return GLUI_TRANSLATION_MOUSE_DOWN_LEFT;            
 
454
  else if ( x < 0 AND y > 0 )
 
455
    return GLUI_TRANSLATION_MOUSE_DOWN_RIGHT;
 
456
  else if ( x > 0 AND y < 0 )
 
457
    return GLUI_TRANSLATION_MOUSE_UP_LEFT;
 
458
  else if ( x > 0 AND y > 0 )
 
459
    return GLUI_TRANSLATION_MOUSE_UP_RIGHT;     
 
460
 
 
461
 
 
462
  return GLUI_TRANSLATION_MOUSE_NONE;
 
463
}
 
464
 
 
465
 
 
466
/*********************************** GLUI_Translation::set_x() ******/
 
467
 
 
468
void  GLUI_Translation::set_x( float val )
 
469
{
 
470
  set_one_val( val, 0 );
 
471
}
 
472
 
 
473
 
 
474
/*********************************** GLUI_Translation::set_y() ******/
 
475
 
 
476
void  GLUI_Translation::set_y( float val )
 
477
{
 
478
  if ( trans_type == GLUI_TRANSLATION_XY )                      
 
479
    set_one_val( val, 1 );
 
480
  else
 
481
    set_one_val( val, 0 );
 
482
}
 
483
 
 
484
 
 
485
/*********************************** GLUI_Translation::set_z() ******/
 
486
 
 
487
void  GLUI_Translation::set_z( float val )
 
488
{
 
489
  set_one_val( val, 0 );
 
490
}
 
491
 
 
492
 
 
493
/******************************* GLUI_Translation::set_one_val() ****/
 
494
 
 
495
void  GLUI_Translation::set_one_val( float val, int index )
 
496
{
 
497
  float *fp;
 
498
 
 
499
  float_array_val[index] = val;   /* set value in array              */
 
500
 
 
501
  /*** The code below is like output_live, except it only operates on
 
502
    a single member of the float array (given by 'index') instead of
 
503
    outputting the entire array   ****/
 
504
        
 
505
  if ( ptr_val == NULL OR NOT live_inited )
 
506
    return;
 
507
 
 
508
  fp = (float*) ptr_val;
 
509
  fp[index]                    = float_array_val[index];
 
510
  last_live_float_array[index] = float_array_val[index];
 
511
 
 
512
  /** Update the main gfx window? **/
 
513
  if ( this->glui != NULL ) {
 
514
    this->glui->post_update_main_gfx();
 
515
  }
 
516
}