GLWButton.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 <GLEXT/GLState.h>
00022 #include <common/Keyboard.h>
00023 #include <GLW/GLWButton.h>
00024 
00025 GLWButtonI::~GLWButtonI()
00026 {
00027 
00028 }
00029 
00030 REGISTER_CLASS_SOURCE(GLWButton);
00031 
00032 GLWButton::GLWButton(float x, float y, float w, float h, GLWButtonI *handler,
00033                                          unsigned flags) : 
00034         handler_(handler),
00035         GLWidget(x, y, w, h),
00036         flags_(flags), pressed_(false), startdrag_(false),
00037         repeatMode_(false), repeatTime_(0.0f),
00038         enabled_(true)
00039 {
00040 
00041 }
00042 
00043 GLWButton::~GLWButton()
00044 {
00045 
00046 }
00047 
00048 void GLWButton::setEnabled(bool enabled)
00049 {
00050         enabled_ = enabled;
00051 }
00052 
00053 void GLWButton::setHandler(GLWButtonI *handler)
00054 { 
00055         handler_ = handler; 
00056 }
00057 
00058 void GLWButton::draw()
00059 {
00060         GLWidget::draw();
00061 
00062         float size = 10.0f;
00063         if (w_ < 16 || h_ < 16) size = 6.0f;
00064         else if (w_ < 12 || h_ < 12) size = 4.0f;
00065 
00066         glLineWidth((flags_&ButtonFlagOk)?2.0f:1.0f);
00067         if (flags_&ButtonSquare)
00068         {
00069                 glBegin(GL_LINE_LOOP);
00070                         drawBox(x_, y_, w_, h_, !pressed_);
00071                 glEnd();
00072         }
00073         else
00074         {
00075                 glBegin(GL_LINE_LOOP);
00076                         drawShadedRoundBox(x_, y_, w_, h_, size, !pressed_);
00077                 glEnd();
00078         }
00079         glLineWidth(1.0f);
00080 }
00081 
00082 void GLWButton::simulate(float frameTime)
00083 {
00084         repeatTime_ += frameTime;
00085         if (repeatMode_)
00086         {
00087                 const float RepeatTimeInterval = 0.15f;
00088                 while (repeatTime_ > RepeatTimeInterval)
00089                 {
00090                         repeatTime_ -= RepeatTimeInterval;
00091                         if (pressed_ && handler_) handler_->buttonDown(getId());
00092                 }
00093         }
00094 }
00095 
00096 void GLWButton::mouseDown(int button, float x, float y, bool &skipRest)
00097 {
00098         if (enabled_ &&
00099                 inBox(x, y, x_, y_, w_, h_))
00100         {
00101                 skipRest = true;
00102                 startdrag_ = true;
00103                 pressed_ = true;
00104 
00105                 if (repeatMode_)
00106                 {
00107                         repeatTime_ = -0.85f;
00108                         if (handler_) handler_->buttonDown(getId());
00109                 }
00110         }
00111 }
00112 
00113 void GLWButton::mouseDrag(int button, float mx, float my, float x, float y, bool &skipRest)
00114 {
00115         if (startdrag_)
00116         {
00117                 if (inBox(mx, my, x_, y_, w_, h_))
00118                 {
00119                         pressed_ = true;
00120                         skipRest = true;
00121                 }
00122                 else
00123                 {
00124                         pressed_ = false;
00125                 }
00126         }
00127 }
00128 
00129 void GLWButton::mouseUp(int button, float x, float y, bool &skipRest)
00130 {
00131         startdrag_ = false;
00132         if (pressed_)
00133         {
00134                 pressed_ = false;       
00135                 skipRest = true;
00136 
00137                 if (!repeatMode_)
00138                 {
00139                         if (handler_) handler_->buttonDown(getId());
00140                 }
00141         }
00142 }
00143 
00144 void GLWButton::keyDown(char *buffer, unsigned int keyState, 
00145                 KeyboardHistory::HistoryElement *history, int hisCount, 
00146                 bool &skipRest)
00147 {
00148         if (enabled_ && handler_ && 
00149                 ((flags_&ButtonFlagOk) || (flags_&ButtonFlagCancel)))
00150         {
00151                 for (int i=0; i<hisCount; i++)
00152                 {
00153                         unsigned int dik = history[i].sdlKey;
00154                         switch (dik)
00155                         {
00156                                 case SDLK_KP_ENTER:
00157                                 case SDLK_RETURN:
00158                                         if ((flags_&ButtonFlagOk)) 
00159                                         { 
00160                                                 handler_->buttonDown(getId()); 
00161                                                 skipRest = true; 
00162                                         }
00163                                         break;
00164                                 case SDLK_ESCAPE:
00165                                         if ((flags_&ButtonFlagCancel)) 
00166                                         { 
00167                                                 handler_->buttonDown(getId()); 
00168                                                 skipRest = true; 
00169                                         }
00170                                         break;  
00171                         }
00172                 }
00173         }
00174 }

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