File: GUITranslatorGL.h

package info (click to toggle)
transcend 0.3.dfsg2-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 5,768 kB
  • sloc: cpp: 26,891; ansic: 693; sh: 210; makefile: 96; perl: 67
file content (326 lines) | stat: -rw-r--r-- 7,658 bytes parent folder | download | duplicates (6)
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
/*
 * Modification History
 *
 * 2001-September-15		Jason Rohrer
 * Created.
 *
 * 2001-September-16		Jason Rohrer
 * Changed so that translation preserves the aspect
 * ration of GUI components.
 * Fixed a bug with inverted y components of mouse coordinates.
 *
 * 2001-October-29		Jason Rohrer
 * Added support for focus.
 *
 * 2001-November-3		Jason Rohrer
 * Changed translation methods to be public.
 */
 
 
#ifndef GUI_TRANSLATOR_GL_INCLUDED
#define GUI_TRANSLATOR_GL_INCLUDED 

#include "GUIComponentGL.h"

#include <GL/gl.h>

#include "minorGems/graphics/openGL/MouseHandlerGL.h"
#include "minorGems/graphics/openGL/KeyboardHandlerGL.h"
#include "minorGems/graphics/openGL/RedrawListenerGL.h"
#include "minorGems/graphics/openGL/ScreenGL.h"

/**
 * A class that translates coordinates for a gui component.
 *
 * Notes on intended use:
 * This class is best used as a wrapper for the entire
 * GL-based gui.  In this case, the component passed to the
 * constructor would be a GUIContainerGL containing the
 * entire GUI.  This class can be thought of as a wrapper
 * for interfacing size-independent GUI components to the
 * screen.
 *
 * @author Jason Rohrer
 */
class GUITranslatorGL : public MouseHandlerGL,
					  public KeyboardHandlerGL,
					  public RedrawListenerGL {


	public:

		/**
		 * Constructs a translator.
		 *
		 * @param inComponent the component for which events
		 *   will be translated and delegated.  All events will
		 *   be delegated to inComponent, regardless of whether the
		 *   events pass the inComponent->isInside() test.
		 *   Will be destroyed when this class is destroyed.
		 * @param inScreen the screen whose size is used during
		 *   event translation.  Must be destroyed by caller after
		 *   this class is destroyed.
		 */
		GUITranslatorGL( GUIComponentGL *inComponent, ScreenGL *inScreen );
	

		
		~GUITranslatorGL();


		
		/**
		 * Translates screen coordinates to gui coordinates.
		 *
		 * @param inX the x value of the screen coordinate.
		 * @param inY the y value of the screen coordinate.
		 * @param outX a pointer to an value in which the gui x
		 *  component will be returned.
		 * @param outY a pointer to an value in which the gui y
		 *  component will be returned.
		 */
		void translate( int inX, int inY, double *outX, double *outY );

		

		/**
		 * Translates screen coordinates to gui coordinates.
		 *
		 * @param inX the x value of the screen coordinate.
		 * 
		 * @return the gui x component will be returned.
		 */
		double translateX( int inX );

		

		/**
		 * Translates screen coordinates to gui coordinates.
		 *
		 * @param inX the x value of the screen coordinate.
		 * 
		 * @return the gui x component will be returned.
		 */
		double translateY( int inY );


		
		// implements the MouseHandlerGL interface
		virtual void mouseMoved( int inX, int inY );
		virtual void mouseDragged( int inX, int inY );
		virtual void mousePressed( int inX, int inY );
		virtual void mouseReleased( int inX, int inY );
		
		// implements the KeyboardHandlerGL interface
		virtual char isFocused();
		virtual void keyPressed( unsigned char inKey, int inX, int inY );
		virtual void specialKeyPressed( int inKey, int inX, int inY );
		virtual void keyReleased( unsigned char inKey, int inX, int inY );
		virtual void specialKeyReleased( int inKey, int inX, int inY );
		
		// implements the RedrawListener interface
		virtual void fireRedraw();


		
	protected:
		
		GUIComponentGL *mComponent;
		ScreenGL *mScreen;



		/**
		 * Gets the factor by which screen coordinates need
		 * to be multiplied to get gui coordinates.
		 *
		 * @return the translation factor to multiply by.
		 */
		double getTranslationFactor();


		
	};



inline GUITranslatorGL::GUITranslatorGL( GUIComponentGL *inComponent,
										 ScreenGL *inScreen )
	: mComponent( inComponent ), mScreen( inScreen ) {

	}



inline GUITranslatorGL::~GUITranslatorGL() {

	delete mComponent;
	}



inline void GUITranslatorGL::translate( int inX, int inY,
										double *outX, double *outY ) {
	
	*outX = translateX( inX );
	*outY = translateY( inY );
	}
		


inline double GUITranslatorGL::translateX( int inX ) {
	return inX * getTranslationFactor();
	}



inline double GUITranslatorGL::translateY( int inY ) {
	int height = mScreen->getHeight();
	
	return ( height - inY ) * getTranslationFactor();
	}







inline double GUITranslatorGL::getTranslationFactor() {
	double width = (double)( mScreen->getWidth() );
	double height = (double)( mScreen->getHeight() );

	if( width <= height ) {
		return 1.0 / width;
		}
	else {
		return 1.0 / height;
		}
	}



inline void GUITranslatorGL::mouseMoved( int inX, int inY ) {
	mComponent->mouseMoved( translateX( inX ), translateY( inY ) );
	}



inline void GUITranslatorGL::mouseDragged( int inX, int inY ) {
	mComponent->mouseDragged( translateX( inX ), translateY( inY ) );
	}



inline void GUITranslatorGL::mousePressed( int inX, int inY ) {
	mComponent->mousePressed( translateX( inX ), translateY( inY ) );
	}



inline void GUITranslatorGL::mouseReleased( int inX, int inY ) {
	mComponent->mouseReleased( translateX( inX ), translateY( inY ) );
	}



inline char GUITranslatorGL::isFocused() {
	return mComponent->isFocused();
	}


		
inline void GUITranslatorGL::keyPressed(
	unsigned char inKey, int inX, int inY ) {
	mComponent->keyPressed( inKey, translateX( inX ), translateY( inY ) );
	}



inline void GUITranslatorGL::specialKeyPressed(
	int inKey, int inX, int inY ) {
	mComponent->specialKeyPressed( inKey,
								   translateX( inX ), translateY( inY ) );
	}



inline void GUITranslatorGL::keyReleased(
	unsigned char inKey, int inX, int inY ) {
	mComponent->keyReleased( inKey, translateX( inX ), translateY( inY ) );
	}



inline void GUITranslatorGL::specialKeyReleased(
	int inKey, int inX, int inY ) {
	mComponent->specialKeyReleased( inKey,
									translateX( inX ), translateY( inY ) );
	}


		
inline void GUITranslatorGL::fireRedraw() {
	// setup orthographic projection matrices before
	// telling our component to draw itself
	// (thus, the component can draw in [0.0, 1.0] space
	// with no regard to screen size)

	// Ack:
	// some of this code was adapted from NeHe's tutorial #17,
	// which can be found at http://nehe.gamedev.net

	glMatrixMode( GL_PROJECTION );   // Select The Projection Matrix
	glPushMatrix();   // Store The Projection Matrix
	
	glLoadIdentity();   // Reset The Projection Matrix
	
	// setup an orthographic projection for our 2d gui

	// the shorter screen dimension gets mapped to the range [0,1],
	// and the longer dimension extends beyond 1.0
	double translationFactor = getTranslationFactor();
	double guiScreenWidth = mScreen->getWidth() * translationFactor;
	double guiScreenHeight = mScreen->getHeight() * translationFactor;

	// we don't care about z for 2d gui's, so just select
	// the region around 0
	double zStart = -1;
	double zEnd = 1;

	// set the projection matrix to an orthographic projection
	glOrtho( 0, guiScreenWidth, 0, guiScreenHeight, zStart, zEnd );

	glMatrixMode( GL_MODELVIEW );  // Select The Modelview Matrix
	glPushMatrix();  // Store The Modelview Matrix
	// set the model view matrix to the identity
	glLoadIdentity();


	glDisable( GL_DEPTH_TEST );
	glDisable( GL_CULL_FACE );


	
	// draw our gui
	mComponent->fireRedraw();


	
	glEnable( GL_DEPTH_TEST );
	glEnable( GL_CULL_FACE );
	
	// restore the matrices
	glMatrixMode( GL_MODELVIEW );  // Select The Modelview Matrix
	glPopMatrix();  // Restore The Old Modelview Matrix
	
	glMatrixMode( GL_PROJECTION );  // Select The Projection Matrix
	glPopMatrix();  // Restore The Old Projection Matrix
	
	}



#endif