Debian Codegraph @

src/guibutton.cpp

raw symbols SCIP
1 /***************************************************************************
2 guibutton.cpp - description
3 -------------------
4 begin : march 22th, 2004
5 copyright : (C) 2004-2008 by Duong Khang NGUYEN
6 email : neoneurone @ gmail com
7
8 $Id: guibutton.cpp 375 2008-10-28 14:47:15Z neoneurone $
9 ***************************************************************************/
10
11 /***************************************************************************
12 * *
13 * This program is free software; you can redistribute it and/or modify *
14 * it under the terms of the GNU General Public License as published by *
15 * the Free Software Foundation; either version 2 of the License, or *
16 * any later version. *
17 * *
18 ***************************************************************************/
19
20 // OpenCity headers
21 #include "guibutton.h"
22 #include "guicontainer.h"
23
24
25 /*=====================================================================*/
26 GUIButton::GUIButton():
27 _uiNumberState(0)
28 {
29 OPENCITY_DEBUG( "Dctor" );
30 }
31
32
33 /*=====================================================================*/
34 GUIButton::GUIButton
35 (
36 const int& rciX,
37 const int& rciY,
38 const uint& rcuiW,
39 const uint& rcuiH,
40 const string& strFile,
41 uint numberState
42 ):
43 _uiNumberState(numberState)
44 {
45 OPENCITY_DEBUG( "Pctor" );
46
47 // Safe
48 _pctr = NULL;
49
50 // Initialize the position of the button
51 // NOTE: it's relative to the postion of the container
52 _iX = rciX;
53 _iY = rciY;
54
55 // The GUI button's W,H may be smaller than the image's W,H
56 _uiWidth = rcuiW;
57 _uiHeight = rcuiH;
58
59 // Load the texture from the image
60 moTextureNormal = Texture( strFile + ".png" );
61 if (_uiNumberState == 2) {
62 moTextureOver = Texture( strFile + "_over.png" );
63 }
64
65 // Set the default colors
66 _cForeground = OPENCITY_PALETTE[ Color::OC_BLACK ];
67 _cBackground = OPENCITY_PALETTE[ Color::OC_BLACK ];
68
69 // By default the button is visible and displayed with alpha blending
70 Set( OC_GUIMAIN_VISIBLE | OC_GUIMAIN_BLENDING );
71 }
72
73
74 /*=====================================================================*/
75 GUIButton::~GUIButton()
76 {
77 OPENCITY_DEBUG( "Dtor" );
78 }
79
80
81 /*=====================================================================*/
82 void
83 GUIButton::SetBackground( const Color& color )
84 {
85 _cBackground = color;
86 }
87
88
89 /*=====================================================================*/
90 void
91 GUIButton::SetForeground( const Color& color )
92 {
93 _cForeground = color;
94 }
95
96
97 /*=====================================================================*/
98 void
99 GUIButton::Display() const
100 {
101 // Return immediatly if this is NOT visible
102 if ( IsSet( OC_GUIMAIN_VISIBLE ) == false )
103 return;
104
105 // IF there is nothing to display THEN return
106 if (!glIsTexture( moTextureNormal.GetName() ))
107 return;
108
109 // Translate the button to the correct position
110 glPushAttrib( GL_ENABLE_BIT );
111 glDisable( GL_LIGHTING );
112 glPushMatrix();
113 glTranslatef( _iX, _iY, 0.0 );
114
115 // Activate the texture 2D processing
116 glEnable( GL_TEXTURE_2D );
117
118 if ( IsSet( OC_GUIMAIN_BLENDING ) == true ) {
119 // Activate the alpha blending
120 glEnable( GL_BLEND );
121 // glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ); // Already choosen
122 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
123 }
124 else {
125 glColor4ub(
126 _cBackground.r,
127 _cBackground.g,
128 _cBackground.b,
129 _cBackground.a );
130 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
131 }
132
133 // Select the appropriate texture
134 // IF the mouse is over THEN choose the over texture
135 // ELSE, use the normal texture
136 if ( IsSet(OC_GUIMAIN_MOUSEOVER) && _uiNumberState > 1 )
137 glBindTexture( GL_TEXTURE_2D, moTextureOver.GetName() );
138 else
139 glBindTexture( GL_TEXTURE_2D, moTextureNormal.GetName() );
140
141 glBegin( GL_QUADS );
142 glTexCoord2i( 0, 0 ); glVertex2i( 0, 0 );
143 glTexCoord2i( 1, 0 ); glVertex2i( _uiWidth-1, 0 );
144 glTexCoord2i( 1, 1 ); glVertex2i( _uiWidth-1, _uiHeight-1 );
145 glTexCoord2i( 0, 1 ); glVertex2i( 0, _uiHeight-1 );
146 glEnd();
147
148 // Restore the old matrix and attribs
149 glPopMatrix();
150 glPopAttrib();
151 }
152
153
154 /*=====================================================================*/
155 void
156 GUIButton::Keyboard( const SDL_KeyboardEvent& rcEvent )
157 {}
158
159
160 /*=====================================================================*/
161 void
162 GUIButton::MouseMotion( const SDL_MouseMotionEvent& rcsMouseEvent )
163 {
164 static int realX, realY;
165 static int winW, winH;
166 static int myX, myY;
167
168 // Return immediatly if this is NOT visible
169 if ( IsSet( OC_GUIMAIN_VISIBLE ) == false )
170 return;
171
172 // The button must be contained in a guicontainer !
173 assert( _pctr != NULL );
174
175 // Calculate the real X,Y from the mouse X,Y;
176 ((GUIContainer*)_pctr)->GetWinWH( winW, winH );
177 realX = rcsMouseEvent.x;
178 realY = winH - rcsMouseEvent.y - 1;
179
180 // Calculate my absolute coordinates via the container
181 _pctr->GetLocation( myX, myY );
182 myX += _iX;
183 myY += _iY;
184
185 //debug
186 //cout << "realX: " << realX << " / realY: " << realY << endl;
187
188 // if the mouse position is within the range, turn on boolMouseOver
189 if ( (realX >= myX) && (realX <= (int)(myX + _uiWidth))
190 && (realY >= myY) && (realY <= (int)(myY + _uiHeight))) {
191 Set( OC_GUIMAIN_MOUSEOVER );
192 }
193 else {
194 Unset( OC_GUIMAIN_MOUSEOVER | OC_GUIMAIN_CLICKED );
195 }
196 }
197
198
199 /*=====================================================================*/
200 void
201 GUIButton::MouseButton( const SDL_MouseButtonEvent& buttonEvent )
202 {
203 // Return immediatly if this is NOT visible
204 if ( IsSet( OC_GUIMAIN_VISIBLE ) == false )
205 return;
206
207 // IF the user clicked the LMB when the mouse is over the control
208 // THEN turn on the onClick state
209 // otherwise turn it off
210 if ( buttonEvent.state == SDL_PRESSED ) {
211 if ( buttonEvent.button == SDL_BUTTON_LEFT ) {
212 if ( IsSet( OC_GUIMAIN_MOUSEOVER ) == true )
213 Set( OC_GUIMAIN_CLICKED );
214 else
215 Unset( OC_GUIMAIN_CLICKED );
216 }
217 }
218 }
219
220
221 /*=====================================================================*/
222 void
223 GUIButton::Expose( const SDL_ExposeEvent& rcEvent )
224 {
225 this->Display();
226 }
227
228
229 /*=====================================================================*/
230 void
231 GUIButton::Resize( const SDL_ResizeEvent& rcEvent )
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