File: ButtonGL.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 (172 lines) | stat: -rw-r--r-- 3,548 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
/*
 * Modification History
 *
 * 2001-September-15		Jason Rohrer
 * Created.
 */
 
 
#ifndef BUTTON_GL_INCLUDED
#define BUTTON_GL_INCLUDED 

#include "GUIComponentGL.h"

#include "minorGems/graphics/Image.h"

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

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

#include <GL/gl.h>


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


	public:



		/**
		 * Constructs a button.
		 *
		 * @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 inUnpressedImage the image to display
		 *   when this button is unpressed.  Must have dimensions
		 *   that are powers of 2.
		 *   Will be destroyed when this class is destroyed.
		 * @param inPressedImage the image to display
		 *   when this button is pressed.  Must have dimensions
		 *   that are powers of 2.
		 *   Will be destroyed when this class is destroyed.
		 */
		ButtonGL(
			double inAnchorX, double inAnchorY, double inWidth,
			double inHeight, Image *inUnpressedImage,
			Image *inPressedImage );


		
		~ButtonGL();
		
		// 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:
		Image *mUnpressedImage;
		Image *mPressedImage;

		SingleTextureGL *mUnpressedTexture;
		SingleTextureGL *mPressedTexture;		

		SingleTextureGL *mCurrentTexture;
	};



inline ButtonGL::ButtonGL(
	double inAnchorX, double inAnchorY, double inWidth,
	double inHeight, Image *inUnpressedImage,
	Image *inPressedImage  )
	: GUIComponentGL( inAnchorX, inAnchorY, inWidth, inHeight ),
	  mUnpressedImage( inUnpressedImage ),
	  mPressedImage( inPressedImage ) {

	mUnpressedTexture = new SingleTextureGL( mUnpressedImage );
	mPressedTexture = new SingleTextureGL( mPressedImage );

	mCurrentTexture = mUnpressedTexture;
	}



inline ButtonGL::~ButtonGL() {
	delete mUnpressedImage;
	delete mPressedImage;

	delete mUnpressedTexture;
	delete mPressedTexture;
	}



inline void ButtonGL::mouseDragged( double inX, double inY ) {
	if( mCurrentTexture == mPressedTexture
		&& !isInside( inX, inY ) ) {
		// the mouse has been dragged outside of us, so unpress
		mCurrentTexture = mUnpressedTexture;
		}
	}



inline void ButtonGL::mousePressed( double inX, double inY ) {
	// we'll only get pressed events if the mouse is pressed on us
	mCurrentTexture = mPressedTexture;
	}



inline void ButtonGL::mouseReleased( double inX, double inY ) {
	// always unpress on a release
	mCurrentTexture = mUnpressedTexture;
	
	if( isInside( inX, inY ) ) {
		// fire an event
		fireActionPerformed( this );
		}
	}


		
inline void ButtonGL::fireRedraw() {
	
	// set our texture
	mCurrentTexture->enable();

	glColor3f( 1.0f, 1.0f, 1.0f ); 

	glDisable( GL_BLEND );

	
	glBegin( GL_QUADS ); {
		
		glTexCoord2f( 0, 1.0f );
		glVertex2d( mAnchorX, mAnchorY );

		glTexCoord2f( 1.0f, 1.0f );
		glVertex2d( mAnchorX + mWidth, mAnchorY ); 

		glTexCoord2f( 1.0f, 0 );
		glVertex2d( mAnchorX + mWidth, mAnchorY + mHeight );
		
		glTexCoord2f( 0, 0 );
		glVertex2d( mAnchorX, mAnchorY + mHeight );
		}
	glEnd();

	
	mCurrentTexture->disable();
	}



#endif