1 /*
2  * Modification History
3  *
4  * 2006-July-3		Jason Rohrer
5  * Created.
6  *
7  * 2007-March-17    Jason Rohrer
8  * Enabled blending (why was it off?)
9  */
10 
11 
12 #ifndef IMAGE_BUTTON_GL_INCLUDED
13 #define IMAGE_BUTTON_GL_INCLUDED
14 
15 #include "ButtonGL.h"
16 
17 #include "minorGems/graphics/Image.h"
18 
19 #include "minorGems/graphics/openGL/SingleTextureGL.h"
20 
21 #include <GL/gl.h>
22 
23 
24 /**
25  * A textured button for GL-based GUIs.
26  *
27  * @author Jason Rohrer
28  */
29 class ImageButtonGL : public ButtonGL {
30 
31 
32 	public:
33 
34 
35 
36 		/**
37 		 * Constructs a button.
38 		 *
39 		 * @param inAnchorX the x position of the upper left corner
40 		 *   of this component.
41 		 * @param inAnchorY the y position of the upper left corner
42 		 *   of this component.
43 		 * @param inWidth the width of this component.
44 		 * @param inHeight the height of this component.
45 		 * @param inUnpressedImage the image to display
46 		 *   when this button is unpressed.  Must have dimensions
47 		 *   that are powers of 2.
48 		 *   Will be destroyed when this class is destroyed.
49 		 * @param inPressedImage the image to display
50 		 *   when this button is pressed.  Must have dimensions
51 		 *   that are powers of 2.
52 		 *   Will be destroyed when this class is destroyed.
53 		 */
54 		ImageButtonGL(
55 			double inAnchorX, double inAnchorY, double inWidth,
56 			double inHeight, Image *inUnpressedImage,
57 			Image *inPressedImage );
58 
59 
60 
61 		~ImageButtonGL();
62 
63 
64         // implements the ButtonGL interface
65         virtual void drawPressed();
66         virtual void drawUnpressed();
67 
68 
69 
70 	protected:
71 		Image *mUnpressedImage;
72 		Image *mPressedImage;
73 
74 		SingleTextureGL *mUnpressedTexture;
75 		SingleTextureGL *mPressedTexture;
76 
77 		SingleTextureGL *mCurrentTexture;
78 
79 
80         /**
81          * Draw this button using a specific texture.
82          *
83          * @param inTexture the texture.
84          *   Destroyed by caller.
85          */
86         void drawWithTexture( SingleTextureGL *inTexture );
87 
88 	};
89 
90 
91 
ImageButtonGL(double inAnchorX,double inAnchorY,double inWidth,double inHeight,Image * inUnpressedImage,Image * inPressedImage)92 inline ImageButtonGL::ImageButtonGL(
93 	double inAnchorX, double inAnchorY, double inWidth,
94 	double inHeight, Image *inUnpressedImage,
95 	Image *inPressedImage  )
96 	: ButtonGL( inAnchorX, inAnchorY, inWidth, inHeight ),
97 	  mUnpressedImage( inUnpressedImage ),
98 	  mPressedImage( inPressedImage ) {
99 
100 	mUnpressedTexture = new SingleTextureGL( mUnpressedImage );
101 	mPressedTexture = new SingleTextureGL( mPressedImage );
102 
103 	mCurrentTexture = mUnpressedTexture;
104 	}
105 
106 
107 
~ImageButtonGL()108 inline ImageButtonGL::~ImageButtonGL() {
109 	delete mUnpressedImage;
110 	delete mPressedImage;
111 
112 	delete mUnpressedTexture;
113 	delete mPressedTexture;
114 	}
115 
116 
117 
drawPressed()118 inline void ImageButtonGL::drawPressed() {
119     drawWithTexture( mPressedTexture );
120     }
121 
122 
123 
drawUnpressed()124 inline void ImageButtonGL::drawUnpressed() {
125     drawWithTexture( mUnpressedTexture );
126     }
127 
128 
129 
drawWithTexture(SingleTextureGL * inTexture)130 inline void ImageButtonGL::drawWithTexture( SingleTextureGL *inTexture ) {
131 
132     // set our texture
133 	inTexture->enable();
134 
135 	glColor3f( 1.0f, 1.0f, 1.0f );
136 
137 	//glDisable( GL_BLEND );
138 
139 
140 	glBegin( GL_QUADS ); {
141 
142 		glTexCoord2f( 0, 1.0f );
143 		glVertex2d( mAnchorX, mAnchorY );
144 
145 		glTexCoord2f( 1.0f, 1.0f );
146 		glVertex2d( mAnchorX + mWidth, mAnchorY );
147 
148 		glTexCoord2f( 1.0f, 0 );
149 		glVertex2d( mAnchorX + mWidth, mAnchorY + mHeight );
150 
151 		glTexCoord2f( 0, 0 );
152 		glVertex2d( mAnchorX, mAnchorY + mHeight );
153 		}
154 	glEnd();
155 
156 
157 	inTexture->disable();
158     }
159 
160 
161 
162 #endif
163 
164 
165 
166