mElite  1.0
An Elite clone based on TextElite by Jan-Philipp Kappmeier and Melanie Schmidt.
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Button.cpp
Go to the documentation of this file.
1 
6 #include "Button.h"
7 #include "Shapes.h"
8 #include "ColorConstants.h"
9 
10 #ifndef max
11 #define max(a,b) (((a) > (b)) ? (a) : (b))
12 #endif
13 
14 #ifndef min
15 #define min(a,b) (((a) < (b)) ? (a) : (b))
16 #endif
17 
19 
31 Button::Button(int innerBorder, int outerBorder, MouseHandler * mouse, std::string text, TextureFont * tf, unsigned int minLength,
32  unsigned int centerx, unsigned int centery, int windowWidth, int windowHeight)
33  : ClickableObject()
34 {
35  this->centerx = centerx;
36  this->centery = centery;
37  this->mouse = mouse;
38  this->text = text;
39  this->windowWidth = windowWidth;
40  this->windowHeight = windowHeight;
41  this->outerBorder = outerBorder;
42  this->innerBorder = innerBorder;
43 
44  memcpy(mouseOverColor,colorYellow,4*sizeof(GLfloat));
45  memcpy(normalColor,colorGreen,4*sizeof(GLfloat));
46  memcpy(inactiveColor,colorWhite,4*sizeof(GLfloat));
47 
48  memcpy(normalFrameColor,colorLightGrey,4*sizeof(GLfloat));
49  memcpy(clickedFrameColor,colorBlue,4*sizeof(GLfloat));
50  memcpy(inactiveFrameColor,colorDarkblue,4*sizeof(GLfloat));
51 
52  this->tf = tf;
53  /*mouseOver = false; */
54  clicked = false;
55  messageHeight = tf->getSize();
56  messageWidth = ( max(text.length(),minLength)+1)*(tf->getWidth());
57 
58  switchOn();
60 
61 }
62 
63 
65 {
66 }
67 
68 void Button::setCenter(unsigned int centerx, unsigned int centery)
69 {
70  this->centerx = centerx;
71  this->centery = centery;
72 }
73 
74 void Button::setWindowDimensions(int wwidth, int wheight)
75 {
76  this->windowWidth = wwidth;
77  this->windowHeight = wheight;
78 }
79 
81 {
82  if (!on) return false;
83  bool oldStatus = mouse->isMouseHoveringActive();
84  mouse->setMouseHovering( true );
85  return mouse->isMouseOverRectCenter( centerx, windowHeight-centery, messageWidth, messageHeight);
86  mouse->setMouseHovering( oldStatus );
87  /*mouseOver= */ //return mouseOver;
88 }
89 
91 {
92  if (!on || !active) return;
93  if (isMouseOverObject())
94  clicked = !clicked;
95 }
96 
98 {
99  return 2*innerBorder+2*outerBorder+messageWidth;
100 }
101 
103 {
104  return 2*innerBorder+2*outerBorder+messageHeight;
105 }
106 
108 {
109  return messageWidth;
110 }
111 
113 {
114  return messageHeight;
115 }
116 
118 {
119  if (!on) return;
120 
121  bool mouseOver = isMouseOverObject();
122 
123  glPushAttrib( GL_ALL_ATTRIB_BITS );
124 
125  glDisable( GL_TEXTURE_2D);
126  glDisable( GL_LIGHTING );
127  glDisable( GL_BLEND );
128 
129  glColor3fv( colorDarkGrey );
130  Shapes::drawRect( centerx, centery, 0, messageWidth+2*innerBorder+2*outerBorder, messageHeight+2*innerBorder+2*outerBorder );
131 
132  glColor3fv( normalFrameColor);
133 
134  if (clicked)
135  glColor3fv( clickedFrameColor);
136 
137  if (!active)
138  glColor3fv( inactiveFrameColor);
139 
140  Shapes::drawRect( centerx, centery, 0, messageWidth+2*innerBorder, messageHeight+2*innerBorder);
141 
142  glColor3fv( colorBlack );
143  Shapes::drawRect( centerx, centery, 0, messageWidth, messageHeight);
144 
145  glColor3fv(normalColor);
146 
147  if (mouseOver)
148  glColor3fv(mouseOverColor);
149  if (!active)
150  glColor3fv(inactiveColor);
151 
152 
153  const char * chartext = text.c_str();
154  glEnable( GL_BLEND );
155  glEnable( GL_TEXTURE_2D );
156 
157  tf->print(centerx-messageWidth/2, centery-messageHeight/2, chartext);
158 
159  glPopAttrib();
160 
161 }
162 
164 {
165  return (on && active && clicked);
166 }
167 
169 {
170  clicked = false;
171 }
172 
174 {
175  active = true;
176 }
177 
179 {
180  active = false;
181  clicked = false;
182 }
183 
185 {
186  on = true;
187 }
188 
190 {
191  on = false;
192 }
193 
195 {
196  return on;
197 }
198 
200 {
201  return active;
202 }