GLWScrollW.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/GLWScrollW.h>
00022 #include <GLEXT/GLState.h>
00023 
00024 REGISTER_CLASS_SOURCE(GLWScrollW);
00025 
00026 GLWScrollWI::~GLWScrollWI()
00027 {
00028 
00029 }
00030 
00031 GLWScrollW::GLWScrollW(float x, float y, float h, int min, int max, int see, GLWScrollWI *handler) :
00032         GLWidget(x, y, 15.0f, h),
00033         min_(min), max_(max), see_(see), current_(min),
00034         handler_(handler), dragCurrent_(0),
00035         bottomButton_(x_ + 2.0f, y_ + 2.0f, w_ - 4.0f, w_ - 4.0f),
00036         topButton_(x_ + 2.0f, y_ + h_ - w_ + 2.0f, w_ - 4.0f, w_ - 4.0f),
00037         middleButton_(x_ + 2.0f, 0.0f, w_ - 4.0f, 0.0f),
00038         backButtonTop_(x_ + 2.0f, y_ + 2.0f, w_ - 4.0f, h_ - 4.0f),
00039         backButtonBot_(x_ + 2.0f, y_ + 2.0f, w_ - 4.0f, h_ - 4.0f)
00040 {
00041         bottomButton_.setHandler(this);
00042         topButton_.setHandler(this);
00043         backButtonTop_.setHandler(this);
00044         backButtonBot_.setHandler(this);
00045         middleButton_.setScrollHandler(this);
00046         bottomButton_.setRepeatMode();
00047         topButton_.setRepeatMode();
00048 }
00049 
00050 GLWScrollW::~GLWScrollW()
00051 {
00052 
00053 }
00054 
00055 void GLWScrollW::setX(float x)
00056 {
00057         GLWidget::setX(x);
00058 
00059         topButton_.setX(x_+2.0f);
00060         bottomButton_.setX(x_+2.0f);
00061         middleButton_.setX(x_+2.0f);
00062         backButtonTop_.setX(x_+2.0f);
00063         backButtonBot_.setX(x_+2.0f);
00064 }
00065 
00066 void GLWScrollW::setY(float y)
00067 {
00068         GLWidget::setY(y);
00069 
00070         bottomButton_.setY(y_ + 2.0f);
00071         topButton_.setY(y_ + h_ - w_ + 2.0f);
00072         middleButton_.setY(0.0f);
00073         backButtonTop_.setY(y_ + 2.0f);
00074         backButtonBot_.setY(y_ + 2.0f);
00075 }
00076 
00077 void GLWScrollW::setW(float w)
00078 {
00079         GLWidget::setW(w);
00080 }
00081 
00082 void GLWScrollW::setH(float h)
00083 {
00084         GLWidget::setH(h);
00085 
00086         topButton_.setY(y_ + h_ - w_ + 2.0f);
00087 }
00088 
00089 void GLWScrollW::draw()
00090 {
00091         const float buttonWidth = w_ - 4.0f;
00092         const int possibilites = (max_ - min_) - see_;
00093 
00094         // Draw scroll indicator
00095         if (possibilites <= 0)
00096         {
00097                 // Can see all possibilites
00098                 middleButton_.setY(y_ + buttonWidth + 4.0f);
00099                 middleButton_.setH(h_ - buttonWidth - buttonWidth - 8.0f);
00100 
00101                 backButtonTop_.setH(0);
00102                 backButtonBot_.setH(0);
00103         }
00104         else
00105         {
00106                 float totalheight = (h_ - buttonWidth - buttonWidth - 8.0f);
00107                 float percentageSeen = float(see_) / float(max_ - min_);
00108                 float barSize = percentageSeen  * totalheight;
00109 
00110                 float heightLeft = totalheight - barSize;
00111                 float heightParts = heightLeft / possibilites;
00112                 float pos = heightParts * current_;
00113 
00114                 float midY = y_ + buttonWidth + 4.0f + pos;
00115                 float midH = barSize;
00116                 middleButton_.setY(midY);
00117                 middleButton_.setH(midH);
00118 
00119                 backButtonTop_.setY(midY);
00120                 backButtonTop_.setH(y_ + h_ - midY);
00121                 backButtonBot_.setY(y_);
00122                 backButtonBot_.setH(midY - y_);
00123         }
00124 
00125         bottomButton_.draw();
00126         topButton_.draw();
00127         middleButton_.draw();
00128         // backButton_.draw(); // Should be hidden
00129 
00130         glBegin(GL_LINE_LOOP);
00131                 // Draw surround
00132                 drawShadedRoundBox(x_, y_, w_, h_, 4.0f, false);
00133         glEnd();
00134 
00135         glColor3f(0.2f, 0.2f, 0.2f);
00136         float bottomOffset = 0.0f;
00137         if (bottomButton_.getPressed()) bottomOffset = 1.0f; 
00138         float topOffset = 0.0f;
00139         if (topButton_.getPressed()) topOffset = 1.0f; 
00140         glBegin(GL_TRIANGLES);
00141                 // Bottom Triangle
00142                 glVertex2f(x_ + (buttonWidth / 2.0f) + 2.0f + bottomOffset, y_ + 4.0f - bottomOffset);
00143                 glVertex2f(x_ + buttonWidth + bottomOffset, y_ + buttonWidth - 2.0f - bottomOffset);
00144                 glVertex2f(x_ + 4 + bottomOffset, y_ + buttonWidth - 2.0f - bottomOffset);
00145 
00146                 // Top Triangle
00147                 glVertex2f(x_ + buttonWidth + topOffset, y_ + 2.0f + h_ - buttonWidth - topOffset);
00148                 glVertex2f(x_ + (buttonWidth / 2.0f) + 2.0f + topOffset, y_ - 4.0f + h_ - topOffset);
00149                 glVertex2f(x_ + 4 + topOffset, y_ + 2.0f + h_ - buttonWidth - topOffset);
00150         glEnd();
00151 }
00152 
00153 void GLWScrollW::simulate(float frameTime)
00154 {
00155         topButton_.simulate(frameTime);
00156         bottomButton_.simulate(frameTime);
00157 }
00158 
00159 void GLWScrollW::mouseDown(int button, float x, float y, bool &skipRest)
00160 {
00161         topButton_.mouseDown(button, x, y, skipRest);
00162         if (skipRest) return;
00163 
00164         bottomButton_.mouseDown(button, x, y, skipRest);
00165         if (skipRest) return;
00166 
00167         middleButton_.mouseDown(button, x, y, skipRest);
00168         if (skipRest) return;
00169 
00170         backButtonTop_.mouseDown(button, x, y, skipRest);
00171         if (skipRest) return;
00172 
00173         backButtonBot_.mouseDown(button, x, y, skipRest);
00174 }
00175 
00176 void GLWScrollW::mouseUp(int button, float x, float y, bool &skipRest)
00177 {
00178         topButton_.mouseUp(button, x, y, skipRest);
00179         if (skipRest) return;
00180 
00181         bottomButton_.mouseUp(button, x, y, skipRest);
00182         if (skipRest) return;
00183 
00184         middleButton_.mouseUp(button, x, y, skipRest);
00185         if (skipRest) return;
00186 
00187         backButtonTop_.mouseUp(button, x, y, skipRest);
00188         if (skipRest) return;
00189 
00190         backButtonBot_.mouseUp(button, x, y, skipRest);
00191 }
00192 
00193 void GLWScrollW::mouseDrag(int button, float mx, float my, float x, float y, bool &skipRest)
00194 {
00195         topButton_.mouseDrag(button, mx, my, x, y, skipRest);
00196         if (skipRest) return;
00197 
00198         bottomButton_.mouseDrag(button, mx, my, x, y, skipRest);
00199         if (skipRest) return;
00200 
00201         middleButton_.mouseDrag(button, mx, my, x, y, skipRest);
00202         if (skipRest) return;
00203 
00204         backButtonTop_.mouseDrag(button, mx, my, x, y, skipRest);
00205         if (skipRest) return;
00206 
00207         backButtonBot_.mouseDrag(button, mx, my, x, y, skipRest);
00208 }
00209 
00210 void GLWScrollW::mouseWheel(float x, float y, float z, bool &skipRest)
00211 {
00212         if (inBox(x, y, x_, y_, w_, h_))
00213         {
00214                 skipRest = true;
00215 
00216                 if (z < 0.0f) GLWScrollW::setCurrent(current_ + 1);
00217                 else GLWScrollW::setCurrent(current_ - 1);
00218         }
00219 }
00220 
00221 void GLWScrollW::startDrag(unsigned int id)
00222 {
00223         dragCurrent_ = current_;
00224 }
00225 
00226 void GLWScrollW::setCurrent(int newCurrent)
00227 {
00228         if (newCurrent < min_) newCurrent = min_;
00229         else if (newCurrent > max_ - see_) newCurrent = max_ - see_;
00230         if (newCurrent < min_) newCurrent = min_; // Incase the max line changed this
00231 
00232         int diffCurrent = current_ - newCurrent;
00233         current_ = newCurrent;
00234         if (handler_) handler_->positionChange(getId(), current_, -diffCurrent);
00235 }
00236 
00237 void GLWScrollW::buttonDrag(unsigned int id, float x, float y)
00238 {
00239         const int possibilites = (max_ - min_) - see_;
00240         if (possibilites > 0)
00241         {
00242                 const float buttonWidth = w_ - 4.0f;
00243                 float totalheight = (h_ - buttonWidth - buttonWidth - 8.0f);
00244                 float percentageSeen = float(see_) / float(max_ - min_);
00245                 float barSize = percentageSeen  * totalheight;
00246 
00247                 float heightLeft = totalheight - barSize;
00248                 float eachPosSize = heightLeft / float(possibilites);
00249 
00250                 int diff = int(y / eachPosSize);
00251                 int newCurrent = dragCurrent_ + diff;
00252 
00253                 if (newCurrent < min_) newCurrent = min_;
00254                 else if (newCurrent > max_ - see_) newCurrent = max_ - see_;
00255 
00256                 if (newCurrent != current_)
00257                 {
00258                         GLWScrollW::setCurrent(newCurrent);
00259                 }
00260         }
00261 }
00262 
00263 void GLWScrollW::buttonDown(unsigned int id)
00264 {
00265         if (id == bottomButton_.getId())
00266         {
00267                 const int possibilites = (max_ - min_) - see_;
00268                 if (possibilites > 0)
00269                 {
00270                         if (current_ > min_) 
00271                         {
00272                                 current_--;
00273                                 if (handler_) handler_->positionChange(getId(), current_, -1);
00274                         }
00275                 }
00276         }
00277         else if (id == topButton_.getId())
00278         {
00279                 const int possibilites = (max_ - min_) - see_;
00280                 if (possibilites > 0)
00281                 {
00282                         if (current_ < max_ - see_) 
00283                         {
00284                                 current_++;
00285                                 if (handler_) handler_->positionChange(getId(), current_, +1);
00286                         }
00287                 }
00288         }
00289         else if (id == backButtonTop_.getId())
00290         {
00291                 const int possibilites = (max_ - min_) - see_;
00292                 if (possibilites > 0)
00293                 {
00294                         int oldcurrent = current_;
00295                         if (current_ < max_ - see_)
00296                         {
00297                                 current_ += see_;
00298                                 if (current_ > max_ - see_) current_ = max_ - see_;
00299                                 if (handler_) handler_->
00300                                         positionChange(getId(), current_, current_ - oldcurrent);
00301                         }
00302                 }
00303         }
00304         else if (id == backButtonBot_.getId())
00305         {
00306                 const int possibilites = (max_ - min_) - see_;
00307                 if (possibilites > 0)
00308                 {
00309                         int oldcurrent = current_;
00310                         if (current_ > min_)
00311                         {
00312                                 current_ -= see_;
00313                                 if (current_ < min_) current_ = min_;
00314                                 if (handler_) handler_->
00315                                         positionChange(getId(), current_, current_ - oldcurrent);
00316                         }
00317                 }
00318         }
00319 }

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