Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

CEGUIThumb.cpp

Go to the documentation of this file.
00001 /************************************************************************
00002         filename:       CEGUIThumb.cpp
00003         created:        25/4/2004
00004         author:         Paul D Turner
00005         
00006         purpose:        Implements common parts of the Thumb base class widget
00007 *************************************************************************/
00008 /*************************************************************************
00009     Crazy Eddie's GUI System (http://www.cegui.org.uk)
00010     Copyright (C)2004 - 2005 Paul D Turner (paul@cegui.org.uk)
00011 
00012     This library is free software; you can redistribute it and/or
00013     modify it under the terms of the GNU Lesser General Public
00014     License as published by the Free Software Foundation; either
00015     version 2.1 of the License, or (at your option) any later version.
00016 
00017     This library is distributed in the hope that it will be useful,
00018     but WITHOUT ANY WARRANTY; without even the implied warranty of
00019     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020     Lesser General Public License for more details.
00021 
00022     You should have received a copy of the GNU Lesser General Public
00023     License along with this library; if not, write to the Free Software
00024     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025 *************************************************************************/
00026 #include "elements/CEGUIThumb.h"
00027 
00028 // Start of CEGUI namespace section
00029 namespace CEGUI
00030 {
00031 const String Thumb::EventNamespace("Thumb");
00032 
00033 /*************************************************************************
00034         Static Properties for this class
00035 *************************************************************************/
00036 ThumbProperties::HotTracked     Thumb::d_hotTrackedProperty;
00037 ThumbProperties::VertFree       Thumb::d_vertFreeProperty;
00038 ThumbProperties::HorzFree       Thumb::d_horzFreeProperty;
00039 ThumbProperties::VertRange      Thumb::d_vertRangeProperty;
00040 ThumbProperties::HorzRange      Thumb::d_horzRangeProperty;
00041 
00042 
00043 /*************************************************************************
00044         Event name constants
00045 *************************************************************************/
00046 // generated internally by Window
00047 const String Thumb::EventThumbPositionChanged( (utf8*)"ThumbPosChanged" );
00048 const String Thumb::EventThumbTrackStarted( (utf8*)"ThumbTrackStarted" );
00049 const String Thumb::EventThumbTrackEnded( (utf8*)"ThumbTrackEnded" );
00050 
00051 
00052 /*************************************************************************
00053         Constructor for Thumb objects
00054 *************************************************************************/
00055 Thumb::Thumb(const String& type, const String& name) :
00056         PushButton(type, name),
00057         d_hotTrack(true),
00058         d_vertFree(false),
00059         d_horzFree(false),
00060         d_vertMin(0.0f),
00061         d_vertMax(1.0f),
00062     d_horzMin(0.0f),
00063         d_horzMax(1.0f),
00064     d_beingDragged(false)
00065 {
00066         addThumbEvents();
00067         addThumbProperties();
00068 }
00069 
00070 
00071 /*************************************************************************
00072         Destructor for Thumb objects    
00073 *************************************************************************/
00074 Thumb::~Thumb(void)
00075 {
00076 }
00077 
00078 
00079 /*************************************************************************
00080         set the movement range of the thumb for the vertical axis.      
00081 *************************************************************************/
00082 void Thumb::setVertRange(float min, float max)
00083 {
00084         // ensure min <= max, swap if not.
00085         if (min > max)
00086         {
00087                 float tmp = min;
00088                 max = min;
00089                 min = tmp;
00090         }
00091 
00092         d_vertMax = max;
00093         d_vertMin = min;
00094 
00095         // validate current position.
00096         float cp = getYPosition();
00097 
00098         if (cp < min)
00099         {
00100                 setYPosition(min);
00101         }
00102         else if (cp > max)
00103         {
00104                 setYPosition(max);
00105         }
00106 
00107 }
00108 
00109 
00110 /*************************************************************************
00111         set the movement range of the thumb for the horizontal axis.
00112 *************************************************************************/
00113 void Thumb::setHorzRange(float min, float max)
00114 {
00115         // ensure min <= max, swap if not.
00116         if (min > max)
00117         {
00118                 float tmp = min;
00119                 max = min;
00120                 min = tmp;
00121         }
00122 
00123         d_horzMax = max;
00124         d_horzMin = min;
00125 
00126         // validate current position.
00127         float cp = getXPosition();
00128 
00129         if (cp < min)
00130         {
00131                 setXPosition(min);
00132         }
00133         else if (cp > max)
00134         {
00135                 setXPosition(max);
00136         }
00137 
00138 }
00139 
00140 
00141 /*************************************************************************
00142         Add thumb specific events       
00143 *************************************************************************/
00144 void Thumb::addThumbEvents(void)
00145 {
00146         addEvent(EventThumbPositionChanged);
00147         addEvent(EventThumbTrackStarted);
00148         addEvent(EventThumbTrackEnded);
00149 }
00150 
00151 
00152 /*************************************************************************
00153         event triggered internally when the position of the thumb       
00154 *************************************************************************/
00155 void Thumb::onThumbPositionChanged(WindowEventArgs& e)
00156 {
00157         fireEvent(EventThumbPositionChanged, e, EventNamespace);
00158 }
00159 
00160 
00161 /*************************************************************************
00162         Handler triggered when the user begins to drag the thumb.       
00163 *************************************************************************/
00164 void Thumb::onThumbTrackStarted(WindowEventArgs& e)
00165 {
00166         fireEvent(EventThumbTrackStarted, e, EventNamespace);
00167 }
00168 
00169 
00170 /*************************************************************************
00171         Handler triggered when the thumb is released
00172 *************************************************************************/
00173 void Thumb::onThumbTrackEnded(WindowEventArgs& e)
00174 {
00175         fireEvent(EventThumbTrackEnded, e, EventNamespace);
00176 }
00177 
00178 
00179 /*************************************************************************
00180         Handler for mouse movement events
00181 *************************************************************************/
00182 void Thumb::onMouseMove(MouseEventArgs& e)
00183 {
00184         // default processing
00185         PushButton::onMouseMove(e);
00186 
00187         // only react if we are being dragged
00188         if (d_beingDragged)
00189         {
00190                 Vector2 delta;
00191                 float hmin, hmax, vmin, vmax;
00192 
00193                 // get some values as absolute pixel offsets
00194                 if (getMetricsMode() == Relative)
00195                 {
00196                         delta = relativeToAbsolute(screenToWindow(e.position));
00197 
00198                         hmax = relativeToAbsoluteX_impl(d_parent, d_horzMax);
00199                         hmin = relativeToAbsoluteX_impl(d_parent, d_horzMin);
00200                         vmax = relativeToAbsoluteY_impl(d_parent, d_vertMax);
00201                         vmin = relativeToAbsoluteY_impl(d_parent, d_vertMin);
00202                 }
00203                 else
00204                 {
00205                         delta = screenToWindow(e.position);
00206 
00207                         hmin = d_horzMin;
00208                         hmax = d_horzMax;
00209                         vmin = d_vertMin;
00210                         vmax = d_vertMax;
00211                 }
00212 
00213                 // calculate amount of movement in pixels
00214                 delta -= d_dragPoint;
00215 
00216                 //
00217                 // Calculate new (pixel) position for thumb
00218                 //
00219                 Point newPos(getAbsolutePosition());
00220 
00221                 if (d_horzFree)
00222                 {
00223                         newPos.d_x += delta.d_x;
00224 
00225                         // limit value to within currently set range
00226                         newPos.d_x = (newPos.d_x < hmin) ? hmin : (newPos.d_x > hmax) ? hmax : newPos.d_x;
00227                 }
00228 
00229                 if (d_vertFree)
00230                 {
00231                         newPos.d_y += delta.d_y;
00232 
00233                         // limit new position to within currently set range
00234                         newPos.d_y = (newPos.d_y < vmin) ? vmin : (newPos.d_y > vmax) ? vmax : newPos.d_y;
00235                 }
00236 
00237                 // update thumb position if needed
00238                 if (newPos != getAbsolutePosition())
00239                 {
00240                         if (getMetricsMode() == Relative)
00241                         {
00242                                 newPos = absoluteToRelative_impl(d_parent, newPos);
00243                         }
00244 
00245                         setPosition(newPos);
00246 
00247                         // send notification as required
00248                         if (d_hotTrack)
00249                         {
00250                                 WindowEventArgs args(this);
00251                                 onThumbPositionChanged(args);
00252                         }
00253 
00254                 }
00255 
00256         }
00257 
00258         e.handled = true;
00259 }
00260 
00261 
00262 /*************************************************************************
00263         Handler for mouse button down events
00264 *************************************************************************/
00265 void Thumb::onMouseButtonDown(MouseEventArgs& e)
00266 {
00267         // default processing
00268         PushButton::onMouseButtonDown(e);
00269 
00270         if (e.button == LeftButton)
00271         {
00272                 // initialise the dragging state
00273                 d_beingDragged = true;
00274                 d_dragPoint = screenToWindow(e.position);
00275 
00276                 if (getMetricsMode() == Relative)
00277                 {
00278                         d_dragPoint = relativeToAbsolute(d_dragPoint);
00279                 }
00280 
00281                 // trigger tracking started event
00282                 WindowEventArgs args(this);
00283                 onThumbTrackStarted(args);
00284 
00285                 e.handled = true;
00286         }
00287 
00288 }
00289 
00290 
00291 /*************************************************************************
00292         Handler for event triggered when we lose capture of mouse input
00293 *************************************************************************/
00294 void Thumb::onCaptureLost(WindowEventArgs& e)
00295 {
00296         // default handling
00297         PushButton::onCaptureLost(e);
00298 
00299         d_beingDragged = false;
00300 
00301         // trigger tracking ended event
00302         WindowEventArgs args(this);
00303         onThumbTrackEnded(args);
00304 
00305         // send notification whenever thumb is released
00306         onThumbPositionChanged(args);
00307 }
00308 
00309 
00310 /*************************************************************************
00311         Return a std::pair that describes the current range set for the
00312         vertical movement.      
00313 *************************************************************************/
00314 std::pair<float, float> Thumb::getVertRange(void) const
00315 {
00316         return std::make_pair(d_vertMin, d_vertMax);
00317 }
00318 
00319 
00320 /*************************************************************************
00321         Return a std::pair that describes the current range set for the
00322         horizontal movement.    
00323 *************************************************************************/
00324 std::pair<float, float> Thumb::getHorzRange(void) const
00325 {
00326         return std::make_pair(d_horzMin, d_horzMax);
00327 }
00328 
00329 
00330 /*************************************************************************
00331         Add thumb specifiec properties
00332 *************************************************************************/
00333 void Thumb::addThumbProperties(void)
00334 {
00335         addProperty(&d_hotTrackedProperty);
00336         addProperty(&d_vertFreeProperty);
00337         addProperty(&d_horzFreeProperty);
00338         addProperty(&d_vertRangeProperty);
00339         addProperty(&d_horzRangeProperty);
00340 }
00341 
00342 
00343 } // End of  CEGUI namespace section

Generated on Wed Sep 7 09:56:34 2005 for Crazy Eddies GUI System by  doxygen 1.4.3