GLWSelectorPart.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/GLWSelectorPart.h>
00022 #include <GLW/GLWSelector.h>
00023 #include <GLW/GLWFont.h>
00024 #include <GLEXT/GLState.h>
00025 #include <GLEXT/GLViewPort.h>
00026 #include <client/ScorchedClient.h>
00027 #include <common/Defines.h>
00028 
00029 GLWSelectorPart::GLWSelectorPart(GLWSelectorI *user,
00030         int basePosition,
00031         float x, float y,
00032         std::list<GLWSelectorEntry> &entries,
00033         bool transparent,
00034         GLWSelectorPart *parent,
00035         int parentPosition) :
00036         user_(user),
00037         entries_(entries),
00038         transparent_(transparent),
00039         basePosition_(basePosition),
00040         parent_(parent), parentPosition_(parentPosition),
00041         child_(0)
00042 {
00043         calculateDimensions(x, y);
00044 }
00045 
00046 GLWSelectorPart::~GLWSelectorPart()
00047 {
00048 }
00049 
00050 void GLWSelectorPart::calculateDimensions(float drawX, float drawY)
00051 {
00052         // Get the height and width of the selector
00053         GLFont2d &font = *GLWFont::instance()->getGameFont();
00054         float selectedHeight = 10.0f; // Padding
00055         float selectedWidth = 0.0f;
00056         float iconWidth = 0.0f;
00057         hasSelectedEntry_ = false;
00058         hasPopupEntry_ = false;
00059         std::list<GLWSelectorEntry>::iterator itor;
00060         for (itor =     entries_.begin();
00061                 itor != entries_.end();
00062                 itor++)
00063         {
00064                 // Get height
00065                 GLWSelectorEntry &item = (*itor);
00066                 if (item.getSeperator()) selectedHeight += 8.0f;
00067                 else selectedHeight += 18.0f;
00068 
00069                 // Get width
00070                 float currentwidth = 10.0f;
00071                 if (item.getText()[0])
00072                 {
00073                         currentwidth = (float) font.getWidth(12, item.getText()) + 20.0f;
00074                 }
00075                 if (item.getSelected()) hasSelectedEntry_ = true;
00076                 if (!item.getPopups().empty()) hasPopupEntry_ = true;
00077                 if (item.getIcon()) iconWidth = item.getTextureWidth() + 16.0f;
00078                 if (currentwidth > selectedWidth) selectedWidth = currentwidth;
00079         }
00080         float indent = 0.0f;
00081         if (hasSelectedEntry_) indent += 10.0f;
00082         if (hasPopupEntry_) selectedWidth += 10.0f;
00083         indent += iconWidth;
00084         selectedWidth += indent;
00085 
00086         float selectedX = drawX;
00087         float selectedY = drawY;
00088         if (selectedX + selectedWidth > GLViewPort::getWidth())
00089         {
00090                 selectedX -= (selectedX + selectedWidth) - GLViewPort::getWidth();
00091         }
00092         else if (selectedX < 0.0f) selectedX = 0.0f;
00093         if (selectedY - selectedHeight < 0)
00094         {
00095                 selectedY -= (selectedY - selectedHeight);
00096         }
00097 
00098         selectedWidth_ = selectedWidth;
00099         selectedHeight_ = selectedHeight;
00100         selectedX_ = selectedX;
00101         selectedY_ = selectedY;
00102         selectedIndent_ = indent;
00103 }
00104 
00105 void GLWSelectorPart::draw()
00106 {
00107         GLState currentStateBlend(GLState::TEXTURE_OFF | GLState::DEPTH_OFF | GLState::BLEND_ON);
00108 
00109         GLFont2d &font = *GLWFont::instance()->getGameFont();
00110         int mouseX = ScorchedClient::instance()->getGameState().getMouseX();
00111         int mouseY = ScorchedClient::instance()->getGameState().getMouseY();
00112 
00113         // Draw the background
00114         {
00115                 if (transparent_) glColor4f(0.4f, 0.6f, 0.8f, 0.6f);
00116                 else glColor3f(0.8f, 0.8f, 1.0f);
00117 
00118                 glBegin(GL_TRIANGLE_FAN);
00119                         glVertex2f(selectedX_ + 20.0f, 
00120                                 selectedY_ - 25.0f + 5.0f);
00121                         glVertex2f(selectedX_ + 20.0f, 
00122                                 selectedY_ - selectedHeight_ + 5.0f);
00123                         GLWidget::drawRoundBox(
00124                                 selectedX_, selectedY_ - selectedHeight_ + 5.0f, 
00125                                 selectedWidth_, selectedHeight_, 10.0f);
00126                         glVertex2f(selectedX_ + 20.0f, 
00127                                 selectedY_ - selectedHeight_ + 5.0f);
00128                 glEnd();
00129 
00130                 glColor4f(0.0f, 0.0f, 0.0f, 0.8f);
00131                 glLineWidth(2.0f);
00132                 glBegin(GL_LINE_LOOP);
00133                         if (transparent_)
00134                         {
00135                                 GLWidget::drawRoundBox(
00136                                         selectedX_, selectedY_ - selectedHeight_ + 5.0f, 
00137                                         selectedWidth_, selectedHeight_, 10.0f);
00138                         }
00139                         else
00140                         {
00141                                 GLWidget::drawShadedRoundBox(
00142                                         selectedX_, selectedY_ - selectedHeight_ + 5.0f, 
00143                                         selectedWidth_, selectedHeight_, 10.0f, true);
00144                         }
00145                 glEnd();
00146                 glLineWidth(1.0f);
00147         }
00148 
00149         GLWToolTip::instance()->clearToolTip(
00150                 selectedX_, selectedY_ - selectedHeight_ + 5.0f, 
00151                 selectedWidth_, selectedHeight_);
00152 
00153         // Draw the menu items
00154         float currentTop = selectedY_;
00155         int position = 1;
00156         std::list<GLWSelectorEntry>::iterator itor;
00157         for (itor =     entries_.begin();
00158                 itor != entries_.end();
00159                 itor++, position++)
00160         {
00161                 GLWSelectorEntry &item = (*itor);
00162 
00163                 // Check if the item is a seperator
00164                 if (item.getSeperator())
00165                 {
00166                         // Draw a seperator
00167                         glBegin(GL_LINES);
00168                                 glColor3f(0.0f, 0.0f, 0.0f);
00169                                 glVertex2f(selectedX_ + 5.0f, currentTop - 5.0f);
00170                                 glVertex2f(selectedX_ + selectedWidth_ - 5.0f, currentTop - 5.0f);
00171                         glEnd();
00172                         currentTop -= 8.0f;
00173                 }
00174                 else
00175                 {
00176                         // Draw the actual line
00177                         bool selected = false;
00178                         if (selectedX_ < mouseX && mouseX < selectedX_ + selectedWidth_ &&
00179                                 currentTop - 18.0f <= mouseY && mouseY < currentTop)
00180                         {
00181                                 // This item is selected
00182                                 selected = true;
00183 
00184                                 // Check popup status
00185                                 if (!item.getPopups().empty())
00186                                 {
00187                                         // This is a popup entry, make sure
00188                                         // 1) This popup is shown
00189                                         // 2) Any popups that have this as a parent are removed
00190                                         if (child_ && child_->getParentPosition() != position)
00191                                         {
00192                                                 // This child is incorrect, remove it
00193                                                 GLWSelector::instance()->rmPart(child_);
00194                                                 child_ = 0;
00195                                         }
00196                                         if (!child_)
00197                                         {
00198                                                 // There is no child, create it
00199                                                 child_ = new GLWSelectorPart(
00200                                                         GLWSelector::instance()->getUser(),
00201                                                         10000, 
00202                                                         selectedX_ + selectedWidth_, currentTop,
00203                                                         item.getPopups(),
00204                                                         transparent_,
00205                                                         this, 
00206                                                         position);
00207                                                 GLWSelector::instance()->addPart(child_);
00208                                         }
00209                                 }
00210                                 else
00211                                 {
00212                                         // This is not a popup entry, make sure
00213                                         // 1) Any popups that have this as a parent are removed
00214                                         if (child_)
00215                                         {
00216                                                 GLWSelector::instance()->rmPart(child_);
00217                                                 child_ = 0;
00218                                         }
00219                                 }
00220                         }
00221                         if (child_ && child_->getParentPosition() == position)
00222                         {
00223                                 selected = true;
00224                         }
00225 
00226                         if (item.getToolTip())
00227                         {
00228                                 GLWToolTip::instance()->addToolTip(item.getToolTip(),
00229                                         selectedX_, currentTop - 18.0f, selectedWidth_, 18.0f);
00230                         }
00231 
00232                         if (item.getIcon())
00233                         {
00234                                 glColor3f(item.getColor()[0], item.getColor()[1], item.getColor()[2]);
00235                                 GLState textureOn(GLState::TEXTURE_ON);
00236                                 item.getIcon()->draw();
00237 
00238                                 float x = selectedX_ + (hasSelectedEntry_?15.0f:5.0f);
00239                                 float y = currentTop - 19.0f;
00240                                 glBegin(GL_QUADS);
00241                                         glTexCoord2f(0.0f, 0.0f);
00242                                         glVertex2f(x, y);
00243                                         glTexCoord2f(1.0f, 0.0f);
00244                                         glVertex2f(x + item.getTextureWidth() + 16.0f, y);
00245                                         glTexCoord2f(1.0f, 1.0f);
00246                                         glVertex2f(x + item.getTextureWidth() + 16.0f, y + 16.0f);
00247                                         glTexCoord2f(0.0f, 1.0f);
00248                                         glVertex2f(x, y + 16.0f);
00249                                 glEnd();
00250                         }
00251 
00252                         static Vector color(0.9f, 0.9f, 1.0f);
00253                         static Vector itemcolor(0.1f, 0.1f, 0.4f);
00254                         static Vector selectedColor(0.3f, 0.3f, 0.7f);
00255                         Vector *c = 0;
00256                         if (transparent_) c = (selected?&color:&itemcolor);
00257                         else c = (selected?&selectedColor:&GLWFont::widgetFontColor);
00258 
00259                         if (item.getSelected())
00260                         {
00261                                 font.draw(*c, 12, selectedX_ + 5.0f, 
00262                                         currentTop - 16.0f, 0.0f, "x");
00263                         }
00264                         font.draw(*c, 12, selectedX_ + selectedIndent_ + 10.0f, 
00265                                 currentTop - 16.0f, 0.0f, item.getText());
00266                         if (!item.getPopups().empty())
00267                         {
00268                                 font.draw(*c, 12, selectedX_ + selectedWidth_ - 15.0f, 
00269                                         currentTop - 16.0f, 0.0f, ">");                         
00270                         }
00271                         currentTop -= 18.0f;
00272                 }
00273         }
00274 }
00275 
00276 void GLWSelectorPart::mouseDown(float mouseX, float mouseY, bool &hit)
00277 {
00278         bool thisMenu = (mouseX > selectedX_ && mouseX < selectedX_ + selectedWidth_ && 
00279                 mouseY < selectedY_ && mouseY > selectedY_ - selectedHeight_);
00280         if (thisMenu)
00281         {
00282                 // Draw the menu items
00283                 int position = 0;
00284                 float currentTop = selectedY_;
00285                 std::list<GLWSelectorEntry>::iterator itor;
00286                 for (itor =     entries_.begin();
00287                          itor != entries_.end();
00288                          itor++)
00289                 {
00290                         GLWSelectorEntry &item = (*itor);
00291                 
00292                         // Check if the item is a seperator
00293                         if (item.getSeperator())
00294                         {
00295                                 position++;
00296                                 currentTop -= 8.0f;
00297                         }
00298                         else
00299                         {
00300                                 // Check if the item is selected
00301                                 if (selectedX_ < mouseX && mouseX < selectedX_ + selectedWidth_ &&
00302                                         currentTop - 18.0f <= mouseY && mouseY < currentTop)
00303                                 {
00304                                         hit = true;
00305                                         if (user_) user_->itemSelected(&item, 
00306                                                 basePosition_ + position);
00307                                 }
00308 
00309                                 position++;
00310                                 currentTop -= 18.0f;
00311                         }
00312                 }
00313         }
00314 }

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