00001
00026 #ifndef _CGUILIST_
00027 #define _CGUILIST_
00028
00029 #include "GUIFrame.h"
00030 #include "GUIListOption.h"
00031
00032 template<typename T>
00033 class CGUIList: public CGUIFrame {
00034
00035 public:
00036 CGUIList();
00037
00043 int getActive() {
00044 return m_active;
00045 }
00046
00047 T getActiveValue() {
00048
00049 if (m_active >= 0) {
00050 return dynamic_cast<CGUIListOption<CGUIList, T>*> (this->get(
00051 m_active))->getValue();
00052 }
00053
00054 return 0;
00055 }
00056
00061 void setActive(int id) {
00062
00063 if (id >= this->size() || id < 0) {
00064 return;
00065 }
00066
00067 if (m_active >= 0) {
00068 dynamic_cast<CGUIListOption<CGUIList, T>*> (this->get(m_active))->nonactive();
00069 }
00070
00071 dynamic_cast<CGUIListOption<CGUIList, T>*> (this->get(id))->active();
00072
00073 m_active = id;
00074
00075 }
00076
00081 void setCountVisible(int count) {
00082 m_countVisible = count;
00083 }
00084
00089 int getCountVisible() {
00090 return m_countVisible;
00091 }
00092
00096 void scrollUp();
00097
00101 void scrollDown();
00102
00103 void mouseButtons(int button, int state, int x, int y) {
00104 CGUIFrame::mouseButtons(button, state, x, y);
00105 buttonUp.mouseButtons(button, state, x, y);
00106 buttonDown.mouseButtons(button, state, x, y);
00107 }
00108
00113 void addOption(CGUIListOption<CGUIList, T>* option) {
00114
00115 this->addChild(option);
00116 option->setPosition(getPositionX(),
00117 getPositionY() + getLineHeight() * this->size());
00118 option->setID(this->size() - 1);
00119 option->setParent(this);
00120 option->setParentOnActivated(&CGUIList::setActive);
00121
00122 this->setHeight(this->getLineHeight());
00123
00124 }
00125
00131 void removeOption(int index) {
00132 if (this->removeChild()) {
00133 if (index == getActive()) {
00134 setActive(-1);
00135 }
00136 }
00137 }
00138
00144 void addOption(T value, string label) {
00145 CGUIListOption<CGUIList, T>* option = new CGUIListOption<CGUIList, T> ;
00146 option->setValue(value);
00147 option->setString(label);
00148 addOption(option);
00149 }
00150
00151 void render() {
00152
00153 CGUIElement* curr = NULL;
00154
00155 for (int i = m_start; i < m_start + m_countVisible; i++) {
00156 if ((curr = getChild(i)) != NULL) {
00157 curr->setPosition(getPositionX(),
00158 getPositionY() + getLineHeight() * (i - m_start));
00159 curr->render();
00160 }
00161 }
00162
00163 buttonUp.setPositionY(getPositionY());
00164 buttonUp.render();
00165
00166 buttonDown.setPositionY(getPositionY());
00167 buttonDown.render();
00168 }
00169
00170 void mouseMove(int x, int y) {
00171 CGUIFrame::mouseMove(x, y);
00172 buttonUp.mouseMove(x, y);
00173 buttonDown.mouseMove(x, y);
00174 }
00175
00176 void centered();
00177
00178 protected:
00179 int m_active;
00180
00181 int m_start;
00182 int m_countVisible;
00183
00184 CGUIButton<CGUIList> buttonUp;
00185 CGUIButton<CGUIList> buttonDown;
00186
00187 };
00188
00189 template<typename T>
00190 CGUIList<T>::CGUIList() {
00191 m_active = -1;
00192 m_countVisible = 2;
00193 m_start = 0;
00194
00195 setDimension(100, 50);
00196
00197 buttonUp.setString("UP");
00198 buttonDown.setString("DOWN");
00199
00200 buttonUp.setDimension(20, 20);
00201 buttonDown.setDimension(40, 20);
00202
00203 buttonUp.setCallback(this, &CGUIList::scrollUp, NULL);
00204 buttonDown.setCallback(this, &CGUIList::scrollDown, NULL);
00205 }
00206
00207 template<typename T>
00208 void CGUIList<T>::scrollUp() {
00209 if (m_start > 0) {
00210 m_start--;
00211 }
00212 }
00213
00214 template<typename T>
00215 void CGUIList<T>::scrollDown() {
00216 if (m_start + m_countVisible < this->size()) {
00217 m_start++;
00218 }
00219 }
00220
00221 template<typename T>
00222 void CGUIList<T>::centered() {
00223
00224 GLint viewport[4];
00225 glGetIntegerv(GL_VIEWPORT, viewport);
00226
00227 GLfloat positionX = (viewport[2] / 2) - (this->getWidth() / 2);
00228 GLfloat positionY = (viewport[3] / 2) - (this->getHeight() / 2);
00229
00230 this->setPosition(positionX, positionY);
00231
00232 this->buttonUp.setPosition(this->getPositionX() - buttonUp.getWidth() - 10,
00233 positionY);
00234 this->buttonDown.setPosition(this->getPositionX() + this->getWidth(),
00235 positionY);
00236
00237 CGUIElement* curr = NULL;
00238
00239 for (int i = 1; i < size(); i++) {
00240 if ((curr = getChild(i)) != NULL) {
00241 GLfloat px = positionX + (getWidth() / 2) - curr->getWidth() / 2;
00242 curr->setPositionX(px);
00243 curr->setPositionY(getPositionY() + getLineHeight() * i);
00244 }
00245 }
00246
00247 }
00248
00249 #endif