GLWDropDown.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #include <GLW/GLWDropDown.h>
00022 #include <GLW/GLWTranslate.h>
00023 #include <GLW/GLWToolTip.h>
00024 #include <client/ScorchedClient.h>
00025 
00026 GLWDropDownI::~GLWDropDownI()
00027 {
00028 
00029 }
00030 
00031 REGISTER_CLASS_SOURCE(GLWDropDown);
00032 
00033 GLWDropDown::GLWDropDown(float x, float y, float w) :
00034         GLWidget(x, y, w, 25.0f), 
00035         button_(x + w - 22.0f, y + 2.0f, 20.0f, 21.0f),
00036         handler_(0), current_(0)
00037 {
00038         button_.setHandler(this);
00039 }
00040 
00041 GLWDropDown::~GLWDropDown()
00042 {
00043 
00044 }
00045 
00046 void GLWDropDown::setHandler(GLWDropDownI *handler)
00047 {
00048         handler_ = handler;
00049 }
00050 
00051 void GLWDropDown::clear()
00052 {
00053         current_ = 0;
00054         texts_.clear();
00055 }
00056 
00057 GLWSelectorEntry *GLWDropDown::getCurrentEntry()
00058 {
00059         return current_;
00060 }
00061 
00062 int GLWDropDown::getCurrentPosition()
00063 {
00064         std::list<GLWSelectorEntry>::iterator itor;
00065         int pos = 0;
00066         for (itor = texts_.begin();
00067                 itor != texts_.end();
00068                 itor++, pos++)
00069         {
00070                 GLWSelectorEntry &entry = *itor;
00071                 if (current_ == &entry)
00072                 {
00073                         return pos;
00074                 }
00075         }
00076 
00077         return -1;
00078 }
00079 
00080 void GLWDropDown::setCurrentPosition(int pos)
00081 {
00082         int position = 0;
00083         std::list<GLWSelectorEntry>::iterator itor;
00084         for (itor = texts_.begin();
00085                 itor != texts_.end();
00086                 itor++)
00087         {
00088                 GLWSelectorEntry &entry = *itor;
00089                 current_ = &entry;
00090                 if (position++ >= pos) break;
00091         }
00092 
00093         if (handler_)
00094         {
00095                 handler_->select(id_, pos, *current_);
00096         }
00097 }
00098 
00099 void GLWDropDown::addEntry(GLWSelectorEntry text)
00100 {
00101         texts_.push_back(text);
00102         if (!current_)
00103         {
00104                 current_ = &texts_.back();
00105         }
00106 }
00107 
00108 void GLWDropDown::draw()
00109 {
00110         GLWidget::draw();
00111 
00112         float mouseX = float(ScorchedClient::instance()->getGameState().getMouseX());
00113         mouseX -= GLWTranslate::getPosX();
00114         float mouseY = float(ScorchedClient::instance()->getGameState().getMouseY());
00115         mouseY -= GLWTranslate::getPosY();
00116 
00117         glBegin(GL_LINE_LOOP);
00118                 drawShadedRoundBox(x_, y_, w_, h_, 10.0f, false);
00119         glEnd();
00120 
00121         button_.draw();
00122         float offset = 0.0f;
00123         if(button_.getPressed()) offset = 1.0f;
00124 
00125         glColor3f(0.2f, 0.2f, 0.2f);
00126         glBegin(GL_TRIANGLES);
00127                 glVertex2d(x_ + w_ - 6.0f + offset, y_ + 17.0f - offset);
00128                 glVertex2d(x_ + w_ - 17.0f + offset, y_ + 17.0f - offset);
00129                 glVertex2d(x_ + w_ - 12.0f + offset, y_ + 7.0f - offset);
00130         glEnd();
00131 }
00132 
00133 void GLWDropDown::buttonDown(unsigned int id)
00134 {
00135         if (button_.getPressed())
00136         {
00137                 GLWSelector::instance()->showSelector(
00138                         this, 
00139                         GLWTranslate::getPosX() + x_, 
00140                         GLWTranslate::getPosY() + y_ - 7.0f, 
00141                         texts_,
00142                         0,
00143                         false);
00144         }
00145 }
00146 
00147 void GLWDropDown::buttonUp(unsigned int id)
00148 {
00149 }
00150 
00151 void GLWDropDown::mouseDown(int button, float x, float y, bool &skipRest)
00152 {
00153         button_.mouseDown(button, x, y, skipRest);
00154         if (!skipRest)
00155         {
00156                 if (inBox(x, y, x_, y_, w_, h_))
00157                 {
00158                         skipRest = true;
00159                         button_.getPressed() = true;
00160                         buttonDown(0);
00161                 }
00162         }
00163 }
00164 
00165 void GLWDropDown::setX(float x)
00166 {
00167         GLWidget::setX(x);
00168         button_.setX(x + w_ - 22.0f);
00169 }
00170 
00171 void GLWDropDown::setY(float y)
00172 {
00173         GLWidget::setY(y);
00174         button_.setY(y + 2.0f); 
00175 }
00176 
00177 void GLWDropDown::itemSelected(GLWSelectorEntry *entry, int pos)
00178 {
00179         button_.getPressed() = false;
00180 
00181         int position = 0;
00182         std::list<GLWSelectorEntry>::iterator itor;
00183         for (itor = texts_.begin();
00184                 itor != texts_.end();
00185                 itor++)
00186         {
00187                 GLWSelectorEntry &entry = *itor;
00188                 current_ = &entry;
00189                 if (position++ >= pos) break;
00190         }
00191 
00192         if (handler_)
00193         {
00194                 handler_->select(id_, position, *current_);
00195         }
00196 }
00197 
00198 void GLWDropDown::noItemSelected()
00199 {
00200         button_.getPressed() = false;
00201 }
00202 
00203 void GLWDropDown::mouseUp(int button, float x, float y, bool &skipRest)
00204 {
00205         button_.mouseUp(button, x, y, skipRest);
00206 }

Generated on Mon Feb 16 15:14:45 2009 for Scorched3D by  doxygen 1.5.3