GLMenuEntry.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 <client/ScorchedClient.h>
00022 #include <GLEXT/GLState.h>
00023 #include <GLEXT/GLMenuEntry.h>
00024 #include <GLEXT/GLTexture.h>
00025 #include <image/Image.h>
00026 #include <GLW/GLWidget.h>
00027 #include <GLW/GLWFont.h>
00028 #include <lang/LangResource.h>
00029 
00030 static Vector color(0.9f, 0.9f, 1.0f);
00031 static Vector itemcolor(0.1f, 0.1f, 0.4f);
00032 static const float menuItemHeight = 20.0f;
00033 
00034 GLMenuEntry::GLMenuEntry(
00035         const LangString &menuName,
00036         char *menuNameInternal, 
00037         const LangString &menuDescription,
00038         float width, 
00039         unsigned int state,
00040         GLMenuI *callback,
00041         Image *icon,
00042         unsigned int flags) :
00043         left_(0.0f), width_(width), height_(0.0f),
00044         callback_(callback),
00045         menuName_(menuName), menuNameInternal_(menuNameInternal), 
00046         menuDescription_(menuDescription),
00047         state_(state), selected_(false),
00048         texture_(0), icon_(icon), flags_(flags),
00049         toolTip_(ToolTip::ToolTipHelp, menuName, menuDescription)
00050 {
00051         toolTip_.setHandler(this);
00052 }
00053 
00054 GLMenuEntry::~GLMenuEntry()
00055 {
00056         delete icon_;
00057         delete texture_;
00058 }
00059 
00060 void GLMenuEntry::draw(float currentTop, float currentLeft)
00061 {
00062         left_ = currentLeft;
00063         top_ = currentTop;
00064         height_ = 0.0f;
00065 
00066         if (icon_)
00067         {
00068                 drawIcon();
00069         }
00070         else
00071         {
00072                 drawText();
00073         }
00074 }
00075 
00076 void GLMenuEntry::drawIcon()
00077 {
00078         if (!texture_)
00079         {
00080                 texture_ = new GLTexture();
00081                 texture_->create(*icon_, false);
00082         }
00083 
00084         GLState state(GLState::TEXTURE_ON | GLState::BLEND_ON | GLState::DEPTH_OFF);
00085 
00086         height_ = 32.0f;
00087         glColor4f(1.0f, 1.0f, 1.0f, selected_?1.0f:0.5f);
00088         texture_->draw();
00089         glBegin(GL_QUADS);
00090                 glTexCoord2f(0.0f, 0.0f);
00091                 glVertex2f(left_, top_ - 32.0f);
00092                 glTexCoord2f(1.0f, 0.0f);
00093                 glVertex2f(left_ + 32.0f, top_ - 32.0f);
00094                 glTexCoord2f(1.0f, 1.0f);
00095                 glVertex2f(left_ + 32.0f, top_);
00096                 glTexCoord2f(0.0f, 1.0f);
00097                 glVertex2f(left_, top_);
00098         glEnd();
00099 }
00100 
00101 void GLMenuEntry::drawText()
00102 {
00103 
00104         // Draw the menu backdrops
00105         height_ = menuItemHeight;
00106         {
00107                 GLState currentStateBlend(GLState::BLEND_ON);
00108                 glColor4f(0.4f, 0.6f, 0.8f, 0.6f);
00109                 glBegin(GL_TRIANGLE_FAN);
00110                         glVertex2f(left_ + 10.0f, top_ - 10.0f);
00111                         glVertex2f(left_ + 10.0f, top_ - menuItemHeight);
00112                         GLWidget::drawRoundBox(left_, top_ - menuItemHeight, 
00113                                 width_, menuItemHeight, 10.0f);
00114                         glVertex2f(left_ + 10.0f, top_ - menuItemHeight);
00115                 glEnd();
00116 
00117                 glColor4f(0.0f, 0.0f, 0.0f, 0.8f);
00118                 glLineWidth(2.0f);
00119                 glBegin(GL_LINE_LOOP);
00120                         GLWidget::drawRoundBox(left_, top_ - menuItemHeight, 
00121                                 width_, menuItemHeight, 10.0f);
00122                 glEnd();
00123                 glLineWidth(1.0f);
00124         }
00125 
00126         // Get and print the menu title text
00127         LangString *menuTitle = callback_->getMenuText(menuNameInternal_.c_str());
00128         if (!menuTitle)
00129         {
00130                 // Else default to the name of the menu
00131                 menuTitle = &menuName_;
00132         }
00133 
00134         GLWFont::instance()->getGameFont()->
00135                 draw((selected_?color:itemcolor), 12, left_ + 5.0f, 
00136                         top_ - 15.0f, 0.0f, *menuTitle);
00137 }
00138 
00139 bool GLMenuEntry::inMenu(float currentTop, int x, int y)
00140 {
00141         float height = menuItemHeight;
00142         if (icon_) height = 32.0f;
00143         if (y > currentTop - height &&
00144                 x>left_ && x<left_ + width_) 
00145         {
00146                 return true;
00147         }
00148         return false;
00149 }
00150 
00151 bool GLMenuEntry::click(float currentTop, int x, int y)
00152 {
00153         float height = menuItemHeight;
00154         if (icon_) height = 32.0f;
00155 
00156         if (y > currentTop - height &&
00157                 x>left_ && x<left_ + width_) 
00158         {
00159                 if (callback_->menuOpened(menuNameInternal_.c_str()))
00160                 {
00161                         selected_ = true;
00162                         
00163                         // Get the contents of the menu
00164                         std::list<GLMenuItem> tmpMenuItems;
00165                         if (callback_->getMenuItems(menuNameInternal_.c_str(), tmpMenuItems))
00166                         {
00167                                 menuItems_ = tmpMenuItems;
00168                         }
00169                 
00170                         // Show the menu
00171                         std::list<GLWSelectorEntry> entries;
00172                         std::list<GLMenuItem>::iterator itor;
00173                         for (itor = menuItems_.begin();
00174                                 itor != menuItems_.end();
00175                                 itor++)
00176                         {
00177                                 GLMenuItem &item = (*itor);
00178                                 entries.push_back(
00179                                         GLWSelectorEntry(
00180                                                 item.getText(),
00181                                                 item.getToolTip(),
00182                                                 item.getSelected(),
00183                                                 item.getTexture(),
00184                                                 item.getUserData()
00185                                                 )
00186                                         );
00187                                 if (item.getSeperator()) entries.back().setSeperator();
00188                         }
00189                         GLWSelector::instance()->showSelector(
00190                                 this, 
00191                                 left_, currentTop - (height + 10.0f), 
00192                                 entries,
00193                                 state_);
00194                 }
00195                 return true;
00196         }
00197 
00198         return false;
00199 }
00200 
00201 void GLMenuEntry::addMenuItem(GLMenuItem &item)
00202 {
00203         menuItems_.push_back(item);
00204 }
00205 
00206 void GLMenuEntry::noItemSelected()
00207 {
00208         selected_ = false;
00209 }
00210 
00211 void GLMenuEntry::itemSelected(GLWSelectorEntry *entry, int position)
00212 {
00213         selected_ = false;
00214         GLMenuItem item(LANG_RESOURCE("NONE", "None"));
00215 
00216         int pos = 0;
00217         std::list<GLMenuItem>::iterator itor;
00218         for (itor = menuItems_.begin();
00219                 itor != menuItems_.end();
00220                 itor++, pos++)
00221         {
00222                 if (pos == position)
00223                 {
00224                         item = (*itor);
00225                 }
00226         }
00227 
00228         callback_->menuSelection(menuNameInternal_.c_str(), position, item);
00229 }
00230 
00231 void GLMenuEntry::populateCalled(unsigned int id)
00232 {
00233         if (callback_)
00234         {
00235                 LangStringStorage *text = callback_->getMenuToolTip(menuNameInternal_.c_str());
00236                 if (text) toolTip_.setText(ToolTip::ToolTipHelp, menuName_, text);
00237         }
00238 }

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