GLWSelector.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/GLWSelector.h>
00022 #include <GLW/GLWSelectorPart.h>
00023 #include <GLEXT/GLViewPort.h>
00024 #include <client/ScorchedClient.h>
00025 #include <common/Defines.h>
00026 #include <graph/OptionsDisplay.h>
00027 
00028 GLWSelectorEntry::GLWSelectorEntry(const LangString &text, 
00029         ToolTip *tooltip, 
00030         bool selected,
00031         GLTexture *icon,
00032         void *userData,
00033         const std::string &dataText) : 
00034         text_(text),
00035         tip_(tooltip),
00036         selected_(selected),
00037         icon_(icon),
00038         userData_(userData),
00039         dataText_(dataText),
00040         color_(1.0f, 1.0f, 1.0f),
00041         textureWidth_(0),
00042         seperator_(false)
00043 {
00044 
00045 }
00046 
00047 GLWSelector *GLWSelector::instance_ = 0;
00048 
00049 GLWSelector *GLWSelector::instance()
00050 {
00051         if (!instance_)
00052         {
00053                 instance_ = new GLWSelector();
00054         }
00055         return instance_;
00056 }
00057 
00058 GLWSelector::GLWSelector() :
00059         GLWWindow("", 0.0f, 0.0f, 0.0f, 0.0f, 0, ""), 
00060         showState_(0)
00061 {
00062         visible_ = false;
00063         windowLevel_ = 1000;
00064 }
00065 
00066 GLWSelector::~GLWSelector()
00067 {
00068 }
00069 
00070 void GLWSelector::showSelector(GLWSelectorI *user,
00071         float x, float y,
00072         std::list<GLWSelectorEntry> &entries,
00073         unsigned int showState,
00074         bool transparent)
00075 
00076 {
00077         hideSelector();
00078 
00079         showState_ = showState;
00080         visible_ = true;
00081         w_ = 100000.0f;
00082         h_ = 100000.0f; 
00083         user_ = user;
00084         
00085         int basePosition = 0;
00086         float left = x;
00087         std::list<GLWSelectorEntry> partEntries;
00088         std::list<GLWSelectorEntry>::iterator itor;
00089         for (itor = entries.begin();
00090                 itor != entries.end();
00091                 itor++)
00092         {
00093                 partEntries.push_back(*itor);
00094                 if ((int) partEntries.size() * 20 >= GLViewPort::getHeight() - 40)
00095                 {
00096                         GLWSelectorPart *part = new GLWSelectorPart(
00097                                 user, basePosition, 
00098                                 left, y, 
00099                                 partEntries,
00100                                 transparent,
00101                                 0, 0); // Parent
00102                         left += part->getSelectedWidth() + 10.0f;
00103                         addPart(part);
00104                         basePosition += (int) partEntries.size();
00105                         partEntries.clear();
00106                 }
00107         }
00108         if (partEntries.size() > 0)
00109         {
00110                 GLWSelectorPart *part = new GLWSelectorPart(
00111                         user, basePosition,
00112                         left, y, 
00113                         partEntries,
00114                         transparent,
00115                         0, 0); // Parent
00116                 addPart(part);
00117         }
00118 }
00119 
00120 void GLWSelector::hideSelector()
00121 {
00122         visible_ = false;
00123         w_ = 0;
00124         h_ = 0; 
00125         user_ = 0;
00126         
00127         while (!parts_.empty())
00128         {
00129                 GLWSelectorPart *part = parts_.front();
00130                 parts_.pop_front();
00131                 delete part;
00132         }
00133 }
00134 
00135 void GLWSelector::addPart(GLWSelectorPart *part)
00136 {
00137         parts_.push_back(part);
00138 }
00139 
00140 void GLWSelector::rmPart(GLWSelectorPart *part)
00141 {
00142         if (part->getChild()) rmPart(part->getChild());
00143         parts_.remove(part);
00144         delete part;
00145 }
00146 
00147 void GLWSelector::draw()
00148 {
00149         if (!visible_) return;
00150         
00151         if (showState_ != 0 &&
00152                 ScorchedClient::instance()->getGameState().getState() != showState_)
00153         {
00154                 if (user_) user_->noItemSelected();
00155                 hideSelector();
00156                 return;
00157         }
00158 
00159         unsigned int state = 0;
00160         if (OptionsDisplay::instance()->getSmoothLines())
00161         {
00162                 state |= GLState::BLEND_ON;
00163                 glEnable(GL_LINE_SMOOTH);
00164         }
00165         GLState currentState(state);
00166 
00167         std::list<GLWSelectorPart *>::iterator itor;
00168         for (itor = parts_.begin();
00169                 itor != parts_.end();
00170                 itor++)
00171         {
00172                 GLWSelectorPart *part = (*itor);
00173                 part->draw();
00174         }
00175 
00176         if (OptionsDisplay::instance()->getSmoothLines())
00177         {
00178                 glDisable(GL_LINE_SMOOTH);
00179         }
00180 }
00181 
00182 void GLWSelector::mouseDown(int button, float mouseX, float mouseY, bool &hitMenu)
00183 {
00184         // Override default window behaviour
00185         if (!visible_) return;
00186         
00187         hitMenu = true;
00188         
00189         if ((showState_ != 0) &&
00190                 (ScorchedClient::instance()->getGameState().getState() != showState_))
00191         {
00192                 if (user_) user_->noItemSelected();
00193                 hideSelector();
00194                 return;
00195         }
00196         
00197         bool hit = false;
00198         std::list<GLWSelectorPart *>::reverse_iterator itor;
00199         for (itor = parts_.rbegin();
00200                 itor != parts_.rend();
00201                 itor++)
00202         {
00203                 GLWSelectorPart *part = (*itor);
00204                 part->mouseDown(mouseX, mouseY, hit);
00205                 if (hit) break;
00206         }
00207         
00208         if (!hit && user_) user_->noItemSelected();
00209 
00210         hideSelector();
00211 }
00212 
00213 void GLWSelector::mouseUp(int button, float x, float y, bool &skipRest)
00214 {
00215         // Override and disable default window behaviour
00216 }
00217 
00218 void GLWSelector::mouseDrag(int button, float mx, float my, float x, float y, bool &skipRest)
00219 {
00220         // Override and disable default window behaviour
00221 }
00222 
00223 void GLWSelector::keyDown(char *buffer, unsigned int keyState, 
00224                 KeyboardHistory::HistoryElement *history, int hisCount, 
00225                 bool &skipRest)
00226 {
00227         // Override and disable default window behaviour
00228 }

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