00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <GLW/GLWIconList.h>
00022 #include <GLEXT/GLState.h>
00023 #include <common/DefinesString.h>
00024
00025 GLWIconList::GLWIconList(
00026 float x, float y, float w, float h,
00027 float squaresHeight,
00028 unsigned int flags) :
00029 GLWidget(x, y, w, h),
00030 squaresHeight_(squaresHeight),
00031 scrollBar_(x + w_ - 17.0f, y + 2.0f, h_ - 4.0f, 0, 0, int(h / squaresHeight)),
00032 selected_(-1), handler_(0), flags_(flags)
00033 {
00034 }
00035
00036 GLWIconList::~GLWIconList()
00037 {
00038 }
00039
00040 void GLWIconList::addItem(GLWIconListItem *item)
00041 {
00042 if (selected_ == -1)
00043 {
00044 selected_ = 0;
00045 if (handler_) handler_->selected(getId(), selected_);
00046 }
00047
00048 items_.push_back(item);
00049 scrollBar_.setMax((int) items_.size());
00050 scrollBar_.setCurrent(0);
00051 }
00052
00053 void GLWIconList::clear()
00054 {
00055 selected_ = -1;
00056 while (!items_.empty())
00057 {
00058 GLWIconListItem *item = items_.back();
00059 items_.pop_back();
00060 delete item;
00061 }
00062 }
00063
00064 GLWIconListItem *GLWIconList::getSelected()
00065 {
00066 if (selected_ < 0 || selected_ >= (int) items_.size()) return 0;
00067 return items_[selected_];
00068 }
00069
00070 void GLWIconList::simulate(float frameTime)
00071 {
00072 scrollBar_.simulate(frameTime);
00073 }
00074
00075 void GLWIconList::draw()
00076 {
00077 GLWidget::draw();
00078 scrollBar_.draw();
00079
00080 glBegin(GL_LINE_LOOP);
00081 drawShadedRoundBox(x_, y_, w_, h_, 5.0f, false);
00082 glEnd();
00083
00084 int min = MIN((int) items_.size(), scrollBar_.getCurrent());
00085 int max = MIN((int) items_.size(), scrollBar_.getCurrent() + scrollBar_.getSee());
00086
00087 float x = x_ + 5.0f;
00088 float y = y_ + h_;
00089 for (int i=min; i<max; i++)
00090 {
00091 y -= squaresHeight_;
00092
00093 if (!(flags_ & eNoDrawSelected) && selected_ == i)
00094 {
00095 float SelectW = w_ - 27.0f;
00096 float SelectH = squaresHeight_ - 5.0f;
00097 glColor3f(0.4f, 0.4f, 0.6f);
00098 glBegin(GL_LINE_LOOP);
00099 glVertex2f(x, y);
00100 glVertex2f(x + SelectW, y);
00101 glVertex2f(x + SelectW, y + SelectH);
00102 glVertex2f(x, y + SelectH);
00103 glEnd();
00104 }
00105
00106 GLWIconListItem *item = items_[i];
00107 item->draw(x, y, w_ - 30.0f);
00108 }
00109 }
00110
00111 void GLWIconList::mouseDown(int button, float x, float y, bool &skipRest)
00112 {
00113 scrollBar_.mouseDown(button, x, y, skipRest);
00114 if (!skipRest)
00115 {
00116 if (inBox(x, y, x_, y_, w_ - 20.0f, h_))
00117 {
00118 skipRest = true;
00119
00120 int pos = int((y_ + h_ - y) / squaresHeight_) + scrollBar_.getCurrent();
00121 if (pos >=0 && pos < (int) items_.size())
00122 {
00123 selected_ = pos;
00124
00125 if (handler_) handler_->selected(getId(), selected_);
00126
00127 if (button == GameState::MouseButtonLeftDoubleClick)
00128 {
00129 if (handler_) handler_->chosen(getId(), selected_);
00130 }
00131 }
00132 }
00133 }
00134 }
00135
00136 void GLWIconList::mouseDrag(int button, float mx, float my, float x, float y, bool &skipRest)
00137 {
00138 scrollBar_.mouseDrag(button, mx, my, x, y, skipRest);
00139 }
00140
00141 void GLWIconList::mouseUp(int button, float x, float y, bool &skipRest)
00142 {
00143 scrollBar_.mouseUp(button, x, y, skipRest);
00144 }
00145
00146 void GLWIconList::mouseWheel(float x, float y, float z, bool &skipRest)
00147 {
00148 if (inBox(x, y, x_, y_, w_, h_))
00149 {
00150 skipRest = true;
00151 scrollBar_.mouseWheel(scrollBar_.getX() + 1, scrollBar_.getY() + 1, z, skipRest);
00152 }
00153 }