File: SliderGL.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 (355 lines) | stat: -rw-r--r-- 8,070 bytes parent folder | download | duplicates (12)
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
/*
 * Modification History
 *
 * 2001-September-17		Jason Rohrer
 * Created.
 *
 * 2001-September-21		Jason Rohrer
 * Fixed a bug in thumb positioning in response to mouse position.
 *
 * 2001-September-23		Jason Rohrer
 * Fixed a bug in icon color clearing.
 */
 
 
#ifndef SLIDER_GL_INCLUDED
#define SLIDER_GL_INCLUDED 

#include "GUIComponentGL.h"

#include "minorGems/graphics/Image.h"
#include "minorGems/graphics/Color.h"

#include "minorGems/graphics/openGL/SingleTextureGL.h"

#include "minorGems/ui/event/ActionListenerList.h"

#include <GL/gl.h>


/**
 * A textured slider for GL-based GUIs.
 *
 * @author Jason Rohrer
 */
class SliderGL : public GUIComponentGL, public ActionListenerList {


	public:



		/**
		 * Constructs a slider.
		 *
		 * @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 inIconImage the image to display
		 *   to the left of this slider.  Must have dimensions
		 *   that are powers of 2.
		 *   Will be destroyed when this class is destroyed.
		 *   If set to NULL, then no icon will be drawn.
		 * @param inIconWidthFraction the fraction of the slider's
		 *   width that should be taken up by the icon.
		 * @param inBarStartColor the color on the left end of
		 *   the slider bar.
		 *   Will be destroyed when this class is destroyed.
		 * @param inBarEndColor the color on the right end of
		 *   the slider bar.
		 *   Will be destroyed when this class is destroyed.
		 * @param inThumbColor the color of the slider thumb.
		 *   Will be destroyed when this class is destroyed.
		 * @param inBorderColor the color of the slider border.
		 *   Will be destroyed when this class is destroyed.
		 */
		SliderGL(
			double inAnchorX, double inAnchorY, double inWidth,
			double inHeight, Image *inIconImage,
			double inIconWidthFraction,
			Color *inBarStartColor, Color *inBarEndColor,
			Color *inThumbColor, Color *inBorderColor );


		
		~SliderGL();


		
		/**
		 * Gets the position of the slider thumb.
		 *
		 * @return the thumb position, in [0,1.0].
		 */
		double getThumbPosition();



		/**
		 * Sets the position of the slider thumb.
		 *
		 * Note that if the slider thumb position changes
		 * as a result of this call, then an action
		 * will be fired to all registered listeners.
		 *
		 * @param inPosition the thumb position, in [0,1.0].
		 */
		void setThumbPosition( double inPosition );

		
		
		// override funxtions in GUIComponentGL
		virtual void mouseDragged( double inX, double inY );
		virtual void mousePressed( double inX, double inY );
		virtual void mouseReleased( double inX, double inY );
		virtual void fireRedraw();


		
	protected:

		SingleTextureGL *mIconTexture;
		double mIconWidthFraction;
		
		Color *mBarStartColor;
		Color *mBarEndColor;
		Color *mThumbColor;
		Color *mBorderColor; 

		double mThumbPosition;

		// true iff the slider is currently being dragged
		char mDragging;
	};



inline SliderGL::SliderGL(
	double inAnchorX, double inAnchorY, double inWidth,
	double inHeight, Image *inIconImage, double inIconWidthFraction,
	Color *inBarStartColor, Color *inBarEndColor,
	Color *inThumbColor, Color *inBorderColor )
	: GUIComponentGL( inAnchorX, inAnchorY, inWidth, inHeight ),
	  mIconWidthFraction( inIconWidthFraction ),
	  mBarStartColor( inBarStartColor ),
	  mBarEndColor( inBarEndColor ),
	  mThumbColor( inThumbColor ),
	  mBorderColor( inBorderColor ),
	  mThumbPosition( 0.5 ), mDragging( false ) {

	if( inIconImage != NULL ) {
		mIconTexture = new SingleTextureGL( inIconImage );
		// the image is no longer needed
		delete inIconImage;
		}
	else {
		mIconTexture = NULL;
		}
	}



inline SliderGL::~SliderGL() {
	if( mIconTexture != NULL ) {
		delete mIconTexture;
		}

	delete mBarStartColor;
	delete mBarEndColor;
	delete mThumbColor;
	delete mBorderColor; 
	}



inline double SliderGL::getThumbPosition() {
	return mThumbPosition;
	}



inline void SliderGL::setThumbPosition( double inPosition ) {
	if( mThumbPosition != inPosition ) {
		mThumbPosition = inPosition;
		fireActionPerformed( this );
		}
	}



inline void SliderGL::mouseDragged( double inX, double inY ) {
	if( mDragging ) {

		double barWidth = mWidth * ( 1 - mIconWidthFraction );
		double iconEndX = mAnchorX + mWidth - barWidth;

		double thumbWidth = 0.1 * barWidth;

		// we want the mouse centered over the thumb
		double thumbUsableBarWidth = barWidth - thumbWidth;

		
		mThumbPosition = ( inX - ( iconEndX + 0.5 * thumbWidth ) )
			/ thumbUsableBarWidth;

		if( mThumbPosition > 1 ) {
			mThumbPosition = 1;
			}
		else if( mThumbPosition < 0 ) {
			mThumbPosition = 0;
			}

		// fire to listeners
		fireActionPerformed( this );
		}
	}



inline void SliderGL::mousePressed( double inX, double inY ) {
	mDragging = true;
	}



inline void SliderGL::mouseReleased( double inX, double inY ) {
	// always stop dragging on a release
	mDragging = false;
	}


		
inline void SliderGL::fireRedraw() {

	// these values will change below if there is an icon
	double barStartX = mAnchorX;
	double barWidth = mWidth;

	if( mIconTexture != NULL ){
		// first, draw the icon

	
		// set our texture
		mIconTexture->enable();

		double textureStartX = mAnchorX;
		double textureStartY = mAnchorY;

		double textureEndX = mAnchorX + mIconWidthFraction * mWidth;
		double textureEndY = mAnchorY + mHeight;
	
		glBegin( GL_QUADS ); {
			// make sure our color is set to white for our texture
			// (to avoid leftover colors)
			glColor3f( 1.0, 1.0, 1.0 );
			
			glTexCoord2f( 0, 1.0f );
			glVertex2d( textureStartX, textureStartY );

			glTexCoord2f( 1.0f, 1.0f );
			glVertex2d( textureEndX, textureStartY ); 

			glTexCoord2f( 1.0f, 0 );
			glVertex2d( textureEndX, textureEndY );
		
			glTexCoord2f( 0, 0 );
			glVertex2d( textureStartX, textureEndY );
			}
		glEnd();

		mIconTexture->disable();


		barWidth = mWidth * ( 1 - mIconWidthFraction );
		barStartX = textureEndX;

		} // end check for a non-NULL icon texture

	
	// now, draw the slider bar

	
	double barHeight = 0.5 * mHeight;

	// center the bar vertically
	double barStartY = mAnchorY + ( mHeight - barHeight ) * 0.5;
	
	// draw its gradient-filled center
	glBegin( GL_QUADS ); {
		
		// start of bar
		glColor3f( mBarStartColor->r,
				   mBarStartColor->g, mBarStartColor->b );
		glVertex2d( barStartX, barStartY );		
		glVertex2d( barStartX, barStartY + barHeight );

		// end of bar
		glColor3f( mBarEndColor->r,
				   mBarEndColor->g, mBarEndColor->b );
		glVertex2d( barStartX + barWidth, barStartY + barHeight );
		glVertex2d( barStartX + barWidth, barStartY ); 

		}
	glEnd();


	
	// draw it's border
	glColor3f( mBorderColor->r,
			   mBorderColor->g, mBorderColor->b );
	glBegin( GL_LINE_LOOP ); {
		glVertex2d( barStartX, barStartY );		
		glVertex2d( barStartX, barStartY + barHeight );
		
		glVertex2d( barStartX + barWidth, barStartY + barHeight );
		glVertex2d( barStartX + barWidth, barStartY ); 
		}
	glEnd();

	// now draw the thumb

	// draw a thumb that is 1/10th as wide as the bar
	double thumbWidth = 0.1 * barWidth;

	// we don't want the thumb going off the ends of the bar
	double thumbUsableBarWidth = barWidth - thumbWidth;
	
	double thumbStartX = mThumbPosition * thumbUsableBarWidth
		+ barStartX;
	double thumbEndX = thumbStartX + thumbWidth;

	glColor3f( mThumbColor->r,
				   mThumbColor->g, mThumbColor->b );
	glBegin( GL_QUADS ); {
		glVertex2d( thumbStartX, mAnchorY );		
		glVertex2d( thumbStartX, mAnchorY + mHeight );

		glVertex2d( thumbEndX, mAnchorY + mHeight );
		glVertex2d( thumbEndX, mAnchorY ); 

		}
	glEnd();

	// draw it's border
	glColor3f( mBorderColor->r,
			   mBorderColor->g, mBorderColor->b );
	glBegin( GL_LINE_LOOP ); {
		glVertex2d( thumbStartX, mAnchorY );		
		glVertex2d( thumbStartX, mAnchorY + mHeight );

		glVertex2d( thumbEndX, mAnchorY + mHeight );
		glVertex2d( thumbEndX, mAnchorY );
		}
	glEnd();

	}



#endif