00001
00027 #ifndef _CGUIBUTTON_
00028 #define _CGUIBUTTON_
00029
00030 #include "GUIElement.h"
00031
00032 template<class T>
00033 class CGUIButton : public CGUIElement {
00034
00035 public:
00036 CGUIButton();
00037 CGUIButton(T* _instance);
00038
00042 void render();
00043
00047 void update();
00048
00056 void mouseButtons(int button, int state, int x, int y);
00057
00058
00059
00065 void setMouseUpCallback(void(T::*_func)(void)) {
00066 m_mouseUpCallback = _func;
00067 }
00068
00069 void setMouseDownCallback(void(T::*_func)(void)) {
00070 m_mouseDownCallback = _func;
00071 }
00072
00073 void setCallbackClass(T* _instance) {
00074 m_instance = _instance;
00075 }
00076
00077 void setCallback(T* _instance, void(T::*_funcMouseDown)(void),
00078 void(T::*_funcMouseUp)(void)) {
00079 m_instance = _instance;
00080 m_mouseDownCallback = _funcMouseDown;
00081 m_mouseUpCallback = _funcMouseUp;
00082 }
00083
00084 protected:
00085
00086 T* m_instance;
00087
00088 void (T::*m_mouseUpCallback)(void);
00089 void (T::*m_mouseDownCallback)(void);
00090
00091 };
00092
00093 template<class T>
00094 CGUIButton<T>::CGUIButton() {
00095 m_instance = NULL;
00096
00097 m_mouseUpCallback = NULL;
00098 m_mouseDownCallback = NULL;
00099
00100 this->setBackgroundColor(0,0,0,0);
00101 this->setBackgroundHoverColor(0,0,0,0);
00102 this->setTextColor(255, 255, 255, 255);
00103 this->setTextHoverColor(100, 100, 100, 255);
00104 }
00105
00106 template<class T>
00107 CGUIButton<T>::CGUIButton(T* _instance) {
00108 m_instance = _instance;
00109 m_mouseUpCallback = NULL;
00110 m_mouseDownCallback = NULL;
00111
00112 this->setBackgroundColor(0,0,0,0);
00113 this->setBackgroundHoverColor(0,0,0,0);
00114 this->setTextColor(255, 255, 255, 255);
00115 this->setTextHoverColor(200, 200, 200, 255);
00116
00117 }
00118
00119 template<class T>
00120 void CGUIButton<T>::render() {
00121
00122 glPushMatrix();
00123
00124 glTranslatef(getPositionX(), getPositionY(), 0);
00125
00126 Color4f backgroundColor = this->getCurrentBackgroundColor();
00127
00128 glColor4f(backgroundColor.r, backgroundColor.g, backgroundColor.b,
00129 backgroundColor.alpha);
00130
00131 glBegin(GL_QUADS);
00132 glVertex2f(0, 0);
00133 glVertex2f(getWidth(), 0);
00134 glVertex2f(getWidth(), getHeight());
00135 glVertex2f(0, getHeight());
00136 glEnd();
00137
00138 glPopMatrix();
00139
00140 CFontPrinter::getInstance()->print(getFontId(), getPositionX(),
00141 getPositionY(), this->getCurrentTextColor(), this->getString());
00142
00143 }
00144
00145 template<class T>
00146 void CGUIButton<T>::mouseButtons(int button, int state, int x, int y) {
00147
00148 if (this->isHover()) {
00149 if (m_instance != NULL) {
00150 if (state == GLUT_UP && this->m_mouseUpCallback != NULL) {
00151 (this->m_instance->*m_mouseUpCallback)();
00152
00153 } else if (state == GLUT_DOWN && this->m_mouseDownCallback != NULL) {
00154 (this->m_instance->*m_mouseDownCallback)();
00155 }
00156 }
00157 }
00158
00159 }
00160
00161 template<class T>
00162 void CGUIButton<T>::update() {
00163
00164 }
00165
00166 #endif