1 //  ---------------------------------------------------------------------------
2 //
3 //  @file       TwEventGLUT.c
4 //  @brief      Helper:
5 //              translate and re-send mouse and keyboard events
6 //              from GLUT event callbacks to AntTweakBar
7 //
8 //  @author     Philippe Decaudin
9 //  @date       2006/05/10
10 //  @license    This file is part of the AntTweakBar library.
11 //              For conditions of distribution and use, see License.txt
12 //
13 //  ---------------------------------------------------------------------------
14 
15 
16 #define GLUT_NO_LIB_PRAGMA          // we do not want to force linkage with glut
17 #ifdef _MSC_VER
18 #   pragma warning(disable: 4505)   // glut generates 'unreferenced function' warnings
19 #   pragma warning(disable: 4100)   // unreferenced parameter
20 #endif // _MSC_VER
21 
22 // #include <GL/glut.h>
23 #include "MiniGLUT.h" // a subset of glut.h needed to compile TwEventGLUT.c
24 // note: AntTweakBar.dll does not need to link with GLUT,
25 // it just needs some definitions for its helper functions.
26 
27 #include <AntTweakBar.h>
28 
29 
TwEventMouseButtonGLUT(int glutButton,int glutState,int mouseX,int mouseY)30 int TW_GLUT_CALL TwEventMouseButtonGLUT(int glutButton, int glutState, int mouseX, int mouseY)
31 {
32     TwMouseAction action = (glutState==GLUT_DOWN) ? TW_MOUSE_PRESSED : TW_MOUSE_RELEASED;
33 
34     TwMouseMotion(mouseX, mouseY);
35     switch( glutButton )
36     {
37     case GLUT_LEFT_BUTTON:
38         return TwMouseButton(action, TW_MOUSE_LEFT);
39     case GLUT_RIGHT_BUTTON:
40         return TwMouseButton(action, TW_MOUSE_RIGHT);
41     case GLUT_MIDDLE_BUTTON:
42         return TwMouseButton(action, TW_MOUSE_MIDDLE);
43     default:
44         return 0;
45     }
46 }
47 
TwEventMouseMotionGLUT(int mouseX,int mouseY)48 int TW_GLUT_CALL TwEventMouseMotionGLUT(int mouseX, int mouseY)
49 {
50     return TwMouseMotion(mouseX, mouseY);
51 }
52 
53 
54 //  GLUT does not send modifiers state to 'Key' and 'Special' callbacks,
55 //  and we cannot call glutGetModifiers here because we do not want to link
56 //  AntTweakBar with glut, so the following function is used to store
57 //  a pointer to the glutGetModifiers function of the calling application.
58 //  It must be called at initialisation of the application.
59 
60 int (TW_CALL *g_GLUTGetModifiers)(void) = NULL;
61 
TwGLUTModifiersFunc(int (TW_CALL * glutGetModifiersFunc)(void))62 int TW_CALL TwGLUTModifiersFunc(int (TW_CALL *glutGetModifiersFunc)(void))
63 {
64     g_GLUTGetModifiers = glutGetModifiersFunc;
65     return (g_GLUTGetModifiers==NULL) ? 0 : 1;
66 }
67 
68 
TwEventKeyboardGLUT(unsigned char glutKey,int mouseX,int mouseY)69 int TW_GLUT_CALL TwEventKeyboardGLUT(unsigned char glutKey, int mouseX, int mouseY)
70 {
71     int kmod = 0;
72 
73     if( g_GLUTGetModifiers!=NULL )
74     {
75         int glutMod = g_GLUTGetModifiers();
76 
77         if( glutMod&GLUT_ACTIVE_SHIFT )
78             kmod |= TW_KMOD_SHIFT;
79         if( glutMod&GLUT_ACTIVE_CTRL )
80             kmod |= TW_KMOD_CTRL;
81         if( glutMod&GLUT_ACTIVE_ALT )
82             kmod |= TW_KMOD_ALT;
83     }
84 
85     if( (kmod&TW_KMOD_CTRL) && (glutKey>0 && glutKey<27) )  // CTRL special case
86         glutKey += 'a'-1;
87 
88     return TwKeyPressed((int)glutKey, kmod);
89 }
90 
91 
TwEventSpecialGLUT(int glutKey,int mouseX,int mouseY)92 int TW_GLUT_CALL TwEventSpecialGLUT(int glutKey, int mouseX, int mouseY)
93 {
94     int k = 0, kmod = 0;
95 
96     if( g_GLUTGetModifiers!=NULL )
97     {
98         int glutMod = g_GLUTGetModifiers();
99 
100         if( glutMod&GLUT_ACTIVE_SHIFT )
101             kmod |= TW_KMOD_SHIFT;
102         if( glutMod&GLUT_ACTIVE_CTRL )
103             kmod |= TW_KMOD_CTRL;
104         if( glutMod&GLUT_ACTIVE_ALT )
105             kmod |= TW_KMOD_ALT;
106     }
107 
108     if( glutKey>=GLUT_KEY_F1 && glutKey<=GLUT_KEY_F12 )
109         k = TW_KEY_F1 + (glutKey-GLUT_KEY_F1);
110     else
111     {
112         switch( glutKey )
113         {
114         case GLUT_KEY_LEFT:
115             k = TW_KEY_LEFT;
116             break;
117         case GLUT_KEY_UP:
118             k = TW_KEY_UP;
119             break;
120         case GLUT_KEY_RIGHT:
121             k = TW_KEY_RIGHT;
122             break;
123         case GLUT_KEY_DOWN:
124             k = TW_KEY_DOWN;
125             break;
126         case GLUT_KEY_PAGE_UP:
127             k = TW_KEY_PAGE_UP;
128             break;
129         case GLUT_KEY_PAGE_DOWN:
130             k = TW_KEY_PAGE_DOWN;
131             break;
132         case GLUT_KEY_HOME:
133             k = TW_KEY_HOME;
134             break;
135         case GLUT_KEY_END:
136             k = TW_KEY_END;
137             break;
138         case GLUT_KEY_INSERT:
139             k = TW_KEY_INSERT;
140             break;
141         }
142     }
143 
144     if( k>0 && k<TW_KEY_LAST )
145         return TwKeyPressed(k, kmod);
146     else
147         return 0;
148 }
149 
150 
151