File: TextFieldGL.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 (396 lines) | stat: -rw-r--r-- 8,468 bytes parent folder | download | duplicates (7)
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
 * Modification History
 *
 * 2001-October-29	Jason Rohrer
 * Created.
 *
 * 2001-November-2	Jason Rohrer
 * Added support for cursor position.
 * Added support for clicking to reposition the cursor.
 * Fixed an array out-of-bounds bug in the keyReleased function.
 */
 
 
#ifndef TEXT_FIELD_GL_INCLUDED
#define TEXT_FIELD_GL_INCLUDED 

#include "GUIComponentGL.h"
#include "TextGL.h"

#include "minorGems/graphics/Color.h"



/**
 * A text field for OpenGL-based GUIs.
 *
 * @author Jason Rohrer
 */
class TextFieldGL : public GUIComponentGL {


	public:



		/**
		 * Constructs a text field.
		 *
		 * @param inAnchorX the x position of the upper left corner
		 *   of this component.
		 * @param inAnchorY the y position of the upper left corner
		 *   of this component.
		 * @param inWidth the width of this component.
		 * @param inHeight the height of this component.
		 * @param inString the text to display in this text field.
		 *    Is copied internally, so must be destroyed
		 *    by caller if not const.
		 * @param inText the text object to use when drawing
		 *   this textField.  Must be destroyed by caller after
		 *   this class is destroyed.
		 * @param inBorderColor the color of this field's border.
		 *   Will be destroyed when this class is destroyed.
		 * @param inFocusedBorderColor the color of this field's border
		 *   when the field is focused.
		 *   Will be destroyed when this class is destroyed.
		 * @param inBackgroundColor the color of this field's background.
		 *   Will be destroyed when this class is destroyed.
		 * @param inCursorColor the color of this field's cursor line.
		 *   Will be destroyed when this class is destroyed.
		 */
		TextFieldGL(
			double inAnchorX, double inAnchorY, double inWidth,
			double inHeight, char *inString, TextGL *inText,
			Color *inBorderColor, Color *inFocusedBorderColor,
			Color *inBackgroundColor, Color *inCursorColor );


		
		~TextFieldGL();


		
		/**
		 * Sets the text displayed by this text field.
		 *
		 * @param inString the text to display in this text field.
		 *    Is copied internally, so must be destroyed
		 *    by caller if not const.
		 */
		void setText( char *inString );


		
		/**
		 * Gets the text displayed by this text field.
		 *
		 * @return the text to display in this textField.
		 *    Must not be destroyed or modified by caller.
		 */
		char *getText();


		
		// override functions in GUIComponentGL
		virtual void setFocus( char inFocus );
		virtual char isFocused();
		virtual void keyPressed( unsigned char inKey, double inX, double inY );
		virtual void specialKeyPressed( int inKey, double inX, double inY );
		virtual void keyReleased( unsigned char inKey,
								  double inX, double inY );
		virtual void specialKeyReleased( int inKey, double inX, double inY );

		virtual void mouseReleased( double inX, double inY );
		
		virtual void fireRedraw();


		
	protected:


		
		/**
		 * Adjusts the cursor position to bring it in bounds of the
		 * current string.
		 */
		void fixCursorPosition();

		
		
		TextGL *mText;

		char *mString;

		Color *mBorderColor;
		Color *mFocusedBorderColor;
		Color *mBackgroundColor;
		Color *mCursorColor;
		
		Color *mCurrentBorderColor;
		
		char mFocused;

		// cursor position, in number of characters
		int mCursorPosition;
	};



inline TextFieldGL::TextFieldGL(
	double inAnchorX, double inAnchorY, double inWidth,
	double inHeight, char *inString, TextGL *inText,
	Color *inBorderColor, Color *inFocusedBorderColor,
	Color *inBackgroundColor, Color *inCursorColor )
	: GUIComponentGL( inAnchorX, inAnchorY, inWidth, inHeight ),
	  mText( inText ), mString( NULL ),
	  mBorderColor( inBorderColor ),
	  mFocusedBorderColor( inFocusedBorderColor ),
	  mBackgroundColor( inBackgroundColor ),
	  mCursorColor( inCursorColor ),
	  mFocused( false ),
	  mCurrentBorderColor( inBorderColor ),
	  mCursorPosition( 0 ) {

	setText( inString );
	}



inline TextFieldGL::~TextFieldGL() {
	if( mString != NULL ) {
		delete [] mString;
		}

	delete mBorderColor;
	delete mFocusedBorderColor;
	delete mBackgroundColor;
	delete mCursorColor;
	}



inline void TextFieldGL::setText( char *inString ) {
	if( mString != NULL ) {
		delete [] mString;
		}
	int length = strlen( inString ) + 1;
	
	mString = new char[ length ];

	memcpy( mString, inString, length );
	}



inline char *TextFieldGL::getText() {
	return mString;
	}



inline void TextFieldGL::setFocus( char inFocus ) {
	mFocused = inFocus;

	if( mFocused ) {
		mCurrentBorderColor = mFocusedBorderColor;
		}
	else {
		mCurrentBorderColor = mBorderColor;
		}
	}



inline char TextFieldGL::isFocused() {
	return mFocused;
	}



inline void TextFieldGL::keyPressed(
	unsigned char inKey, double inX, double inY ) {
	}



inline void TextFieldGL::specialKeyPressed(
	int inKey, double inX, double inY ) {
	}



inline void TextFieldGL::keyReleased(
	unsigned char inKey, double inX, double inY ) {

	//printf( "key press: %d\n", inKey );
	
	// backspace and delete
	if( inKey == 127 || inKey == 8 ) {
		if( mCursorPosition != 0 ) {
			int length = strlen( mString );

			if( length != 0 ) {
				char *newString = new char[ length ];

				// copy mString up to the last char before the deleted char
				memcpy( newString, mString, mCursorPosition-1 );

				// copy the portion of mString after the deleted char
				// this will include the trailing \0
				memcpy( &( newString[mCursorPosition-1] ),
						&( mString[mCursorPosition] ),
						length - mCursorPosition + 1 );
			
				setText( newString );
			
				delete [] newString;

				mCursorPosition--;
				}
			}
		}
	// allowable character key, from space up to tilde
	else if( inKey >= 32 && inKey <= 126 ) {
		// add a character to our string

		int oldStringLength = strlen( mString ) + 2; 
		int length = oldStringLength + 2;
		
		char *newString = new char[ length ];

		
		if( mCursorPosition != 0 ) {
			// copy chars up to cursor position
			memcpy( newString, mString, mCursorPosition );
			}
		// copy chars after cursor position, including trailing \0
		memcpy( &( newString[mCursorPosition+1] ),
				&( mString[mCursorPosition] ),
				oldStringLength - mCursorPosition + 1 );

		// now stick in the inserted char
		newString[ mCursorPosition ] = inKey;
		
		setText( newString );
		
		delete [] newString;

		mCursorPosition++;
		}
	
	// else a non-valid key hit
	}



inline void TextFieldGL::specialKeyReleased(
	int inKey, double inX, double inY ) {

	switch( inKey ) {
		case GLUT_KEY_RIGHT:
			mCursorPosition++;
			break;
		case GLUT_KEY_LEFT:
			mCursorPosition--;
		default:
			break;
		}

	fixCursorPosition();
	}



inline void TextFieldGL::mouseReleased(
	double inX, double inY ) {

	if( isInside( inX, inY ) ) {

		mCursorPosition = (int)( 0.5 + strlen( mString ) *
								 ( inX - mAnchorX ) / mWidth );
		
		}

	}



inline void TextFieldGL::fixCursorPosition() {
	if( mCursorPosition < 0 ) {
		mCursorPosition = 0;
		}
	else {
		int stringLength = strlen( mString );
		if( mCursorPosition > stringLength ) {
			mCursorPosition = stringLength;
			}
		}
	}


		
inline void TextFieldGL::fireRedraw() {

	// draw the background 
	glColor3f( mBackgroundColor->r,
			   mBackgroundColor->g, mBackgroundColor->b );
	glBegin( GL_QUADS ); {
		glVertex2d( mAnchorX, mAnchorY );		
		glVertex2d( mAnchorX, mAnchorY + mHeight );

		glVertex2d( mAnchorX + mWidth, mAnchorY + mHeight );
		glVertex2d( mAnchorX + mWidth, mAnchorY ); 

		}
	glEnd();

	
	// draw the text
	mText->drawText( mString, mAnchorX, mAnchorY,
					 mWidth, mHeight );

	if( mFocused ) {
		// draw the cursor
		glColor3f( mCursorColor->r,
				   mCursorColor->g, mCursorColor->b );
		glBegin( GL_LINES ); {
			// here we assume that fonts are fixed-width
			double cursorViewX;
			int stringLength = strlen( mString );
			if( stringLength != 0 ) { 
				cursorViewX =
					mAnchorX +
					mWidth * ( mCursorPosition / (double)( stringLength ) );
				}
			else {
				cursorViewX = mAnchorX;
				}
			glVertex2d( cursorViewX, mAnchorY );		
			glVertex2d( cursorViewX, mAnchorY + mHeight );
			}
		glEnd();
		}
	
	
	// draw the border on top of the text and cursor
	glColor3f( mCurrentBorderColor->r,
			   mCurrentBorderColor->g, mCurrentBorderColor->b );
	glBegin( GL_LINE_LOOP ); {
		glVertex2d( mAnchorX, mAnchorY );		
		glVertex2d( mAnchorX, mAnchorY + mHeight );

		glVertex2d( mAnchorX + mWidth, mAnchorY + mHeight );
		glVertex2d( mAnchorX + mWidth, mAnchorY );
		}
	glEnd();

	
	}



#endif