00001
00025 #ifndef _CGUIMENU_H_
00026 #define _CGUIMENU_H_
00027
00028 #define ALIGN_LEFT 0
00029 #define ALIGN_RIGHT 1
00030 #define ALIGN_CENTER 2
00031
00032 #include "GameObject2D.h"
00033 #include "Collection.h"
00034 #include "GUIMenuItem.h"
00035
00036 template<class T>
00037 class CGUIMenu: public CCollection<T> , public CGUIElement {
00038 public:
00039 CGUIMenu();
00040 virtual ~CGUIMenu();
00041
00042 void render();
00043 void update();
00044
00045 void mouseButtons(int button, int state, int x, int y);
00046 void mouseMove(int x, int y);
00047
00048
00049
00050 void setFontId(int id) {
00051 m_fontId = id;
00052 m_optimized = false;
00053 }
00054 ;
00055 void getFontId() {
00056 return m_fontId;
00057 m_optimized = false;
00058 }
00059
00060 void setAlign(int align) {
00061 m_align = align;
00062 }
00063 ;
00064
00065 protected:
00066
00067 void optimize();
00068
00069 bool m_optimized;
00070
00071 int m_fontId;
00072 int m_align;
00073
00074 };
00075
00076 template<class T>
00077 CGUIMenu<T>::CGUIMenu() {
00078 m_optimized = false;
00079 m_align = ALIGN_LEFT;
00080
00081 m_paddingLeft = 10;
00082 m_paddingTop = 10;
00083 m_lineHeight = 22;
00084
00085 this->setBackgroundColor(0, 0, 0, 128);
00086 this->setBackgroundHoverColor(255, 0, 0, 255);
00087
00088 }
00089
00090 template<class T>
00091 CGUIMenu<T>::~CGUIMenu() {
00092
00093 }
00094
00095 template<class T>
00096 void CGUIMenu<T>::render() {
00097
00098 GLfloat px = 0;
00099 T* curr = NULL;
00100
00101 glPushMatrix();
00102
00103 glTranslatef(getPositionX(), getPositionY(), 0);
00104
00105 Color4f backgroundColor = this->getCurrentBackgroundColor();
00106
00107 glColor4f(backgroundColor.r, backgroundColor.g, backgroundColor.b,
00108 backgroundColor.alpha);
00109
00110 glBegin(GL_QUADS);
00111 glVertex2f(0, 0);
00112 glVertex2f(getWidth(), 0);
00113 glVertex2f(getWidth(), getHeight());
00114 glVertex2f(0, getHeight());
00115 glEnd();
00116
00117 glPopMatrix();
00118
00119 for (int i = 0; i < this->size(); i++) {
00120
00121 curr = this->get(i);
00122 if (curr != NULL) {
00123 if (curr->isVisible()) {
00124
00125 switch (m_align) {
00126 case ALIGN_LEFT:
00127 px = getPositionX();
00128
00129 break;
00130
00131 case ALIGN_RIGHT:
00132 px = getPositionX() + getWidth() - curr->getString().size();
00133
00134 cout << curr->getString().size() << endl;
00135
00136 break;
00137 case ALIGN_CENTER:
00138 break;
00139 }
00140
00141 curr->setPosition(getPaddingLeft() + px,
00142 getPaddingTop() + getPositionY() + i * m_lineHeight);
00143
00144 curr->render();
00145 }
00146 }
00147 }
00148
00149 }
00150
00151 template<class T>
00152 void CGUIMenu<T>::mouseButtons(int button, int state, int x, int y) {
00153
00154 CGameObject2D* curr = NULL;
00155
00156 for (int i = 0; i < this->size(); i++) {
00157
00158 curr = this->get(i);
00159 if (curr != NULL) {
00160 if (curr->isVisible()) {
00161 curr->mouseButtons(button, state, x, y);
00162 }
00163 }
00164 }
00165 }
00166
00167 template<class T>
00168 void CGUIMenu<T>::mouseMove(int x, int y) {
00169
00170 CGameObject2D* curr = NULL;
00171
00172 for (int i = 0; i < this->size(); i++) {
00173
00174 curr = this->get(i);
00175 if (curr != NULL) {
00176 if (curr->isVisible()) {
00177 curr->mouseMove(x, y);
00178 }
00179 }
00180 }
00181 }
00182
00183 template<class T>
00184 void CGUIMenu<T>::update() {
00185
00186 CGameObject2D* curr = NULL;
00187
00188 for (int i = 0; i < this->size(); i++) {
00189
00190 curr = this->get(i);
00191 if (curr != NULL) {
00192 if (curr->isVisible()) {
00193 curr->update();
00194 }
00195 }
00196 }
00197 }
00198
00199 template<class T>
00200 void CGUIMenu<T>::optimize() {
00201
00202 CGUIMenuItem<T>* curr = NULL;
00203
00204 for (int i = 0; i < this->size(); i++) {
00205
00206 curr = this->get(i);
00207 if (curr != NULL) {
00208 curr->setPosition(getPositionX(), getPositionY() + i * m_lineHeight);
00209
00210 }
00211 }
00212
00213 }
00214
00215 #endif