00001 /************************************************************************ 00002 filename: CEGUISlider.cpp 00003 created: 13/4/2004 00004 author: Paul D Turner 00005 00006 purpose: Implementation of Slider widget base class 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/CEGUISlider.h" 00027 #include "elements/CEGUIThumb.h" 00028 00029 // Start of CEGUI namespace section 00030 namespace CEGUI 00031 { 00032 const String Slider::EventNamespace("Slider"); 00033 00034 /************************************************************************* 00035 Definition of Properties for this class 00036 *************************************************************************/ 00037 SliderProperties::CurrentValue Slider::d_currentValueProperty; 00038 SliderProperties::MaximumValue Slider::d_maximumValueProperty; 00039 SliderProperties::ClickStepSize Slider::d_clickStepSizeProperty; 00040 00041 00042 /************************************************************************* 00043 Event name constants 00044 *************************************************************************/ 00045 const String Slider::EventValueChanged( (utf8*)"ValueChanged" ); 00046 const String Slider::EventThumbTrackStarted( (utf8*)"ThumbTrackStarted" ); 00047 const String Slider::EventThumbTrackEnded( (utf8*)"ThumbTrackEnded" ); 00048 00049 00050 /************************************************************************* 00051 Slider base class constructor 00052 *************************************************************************/ 00053 Slider::Slider(const String& type, const String& name) : 00054 Window(type, name), 00055 d_value(0.0f), 00056 d_maxValue(1.0f), 00057 d_step(0.01f), 00058 d_thumb(NULL) 00059 { 00060 addSliderEvents(); 00061 addSliderProperties(); 00062 } 00063 00064 00065 /************************************************************************* 00066 Slider base class destructor 00067 *************************************************************************/ 00068 Slider::~Slider(void) 00069 { 00070 } 00071 00072 00073 /************************************************************************* 00074 Initialises the Window based object ready for use. 00075 *************************************************************************/ 00076 void Slider::initialise(void) 00077 { 00078 // create and attach thumb 00079 d_thumb = createThumb(); 00080 addChildWindow(d_thumb); 00081 00082 // bind handler to thumb events 00083 d_thumb->subscribeEvent(Thumb::EventThumbPositionChanged, Event::Subscriber(&CEGUI::Slider::handleThumbMoved, this)); 00084 d_thumb->subscribeEvent(Thumb::EventThumbTrackStarted, Event::Subscriber(&CEGUI::Slider::handleThumbTrackStarted, this)); 00085 d_thumb->subscribeEvent(Thumb::EventThumbTrackEnded, Event::Subscriber(&CEGUI::Slider::handleThumbTrackEnded, this)); 00086 00087 layoutComponentWidgets(); 00088 } 00089 00090 00091 /************************************************************************* 00092 set the maximum value for the slider. 00093 Note that the minimum value is fixed at 0. 00094 *************************************************************************/ 00095 void Slider::setMaxValue(float maxVal) 00096 { 00097 d_maxValue = maxVal; 00098 00099 float oldval = d_value; 00100 00101 // limit current value to be within new max 00102 if (d_value > d_maxValue) { 00103 d_value = d_maxValue; 00104 } 00105 00106 updateThumb(); 00107 00108 // send notification if slider value changed. 00109 if (d_value != oldval) 00110 { 00111 WindowEventArgs args(this); 00112 onValueChanged(args); 00113 } 00114 00115 } 00116 00117 00118 /************************************************************************* 00119 set the current slider value. 00120 *************************************************************************/ 00121 void Slider::setCurrentValue(float value) 00122 { 00123 float oldval = d_value; 00124 00125 // range for value: 0 <= value <= maxValue 00126 d_value = (value >= 0.0f) ? ((value <= d_maxValue) ? value : d_maxValue) : 0.0f; 00127 00128 updateThumb(); 00129 00130 // send notification if slider value changed. 00131 if (d_value != oldval) 00132 { 00133 WindowEventArgs args(this); 00134 onValueChanged(args); 00135 } 00136 00137 } 00138 00139 00140 /************************************************************************* 00141 Add slider specific events 00142 *************************************************************************/ 00143 void Slider::addSliderEvents(void) 00144 { 00145 addEvent(EventValueChanged); 00146 addEvent(EventThumbTrackStarted); 00147 addEvent(EventThumbTrackEnded); 00148 } 00149 00150 00151 /************************************************************************* 00152 Handler triggered when the slider value changes 00153 *************************************************************************/ 00154 void Slider::onValueChanged(WindowEventArgs& e) 00155 { 00156 fireEvent(EventValueChanged, e, EventNamespace); 00157 } 00158 00159 00160 /************************************************************************* 00161 Handler triggered when the user begins to drag the slider thumb. 00162 *************************************************************************/ 00163 void Slider::onThumbTrackStarted(WindowEventArgs& e) 00164 { 00165 fireEvent(EventThumbTrackStarted, e, EventNamespace); 00166 } 00167 00168 00169 /************************************************************************* 00170 Handler triggered when the slider thumb is released 00171 *************************************************************************/ 00172 void Slider::onThumbTrackEnded(WindowEventArgs& e) 00173 { 00174 fireEvent(EventThumbTrackEnded, e, EventNamespace); 00175 } 00176 00177 00178 /************************************************************************* 00179 Handler for when a mouse button is pressed 00180 *************************************************************************/ 00181 void Slider::onMouseButtonDown(MouseEventArgs& e) 00182 { 00183 // base class processing 00184 Window::onMouseButtonDown(e); 00185 00186 if (e.button == LeftButton) 00187 { 00188 float adj = getAdjustDirectionFromPoint(e.position); 00189 00190 // adjust slider position in whichever direction as required. 00191 if (adj != 0) 00192 { 00193 setCurrentValue(d_value + (adj * d_step)); 00194 } 00195 00196 e.handled = true; 00197 } 00198 00199 } 00200 00201 00202 /************************************************************************* 00203 Handler for when the size of the slider widget changes. 00204 *************************************************************************/ 00205 void Slider::onSized(WindowEventArgs& e) 00206 { 00207 // base class processing 00208 Window::onSized(e); 00209 00210 layoutComponentWidgets(); 00211 00212 e.handled = true; 00213 } 00214 00215 00216 /************************************************************************* 00217 Handler for scroll wheel changes 00218 *************************************************************************/ 00219 void Slider::onMouseWheel(MouseEventArgs& e) 00220 { 00221 // base class processing 00222 Window::onMouseWheel(e); 00223 00224 // scroll by e.wheelChange * stepSize 00225 setCurrentValue(d_value + d_step * -e.wheelChange); 00226 00227 // ensure the message does not go to our parent. 00228 e.handled = true; 00229 } 00230 00231 00232 /************************************************************************* 00233 handler function for when thumb moves. 00234 *************************************************************************/ 00235 bool Slider::handleThumbMoved(const EventArgs& e) 00236 { 00237 setCurrentValue(getValueFromThumb()); 00238 00239 return true; 00240 } 00241 00242 00243 /************************************************************************* 00244 handler function for when thumb tracking begins 00245 *************************************************************************/ 00246 bool Slider::handleThumbTrackStarted(const EventArgs& e) 00247 { 00248 // simply trigger our own version of this event 00249 WindowEventArgs args(this); 00250 onThumbTrackStarted(args); 00251 00252 return true; 00253 } 00254 00255 00256 /************************************************************************* 00257 handler function for when thumb tracking begins 00258 *************************************************************************/ 00259 bool Slider::handleThumbTrackEnded(const EventArgs& e) 00260 { 00261 // simply trigger our own version of this event 00262 WindowEventArgs args(this); 00263 onThumbTrackEnded(args); 00264 00265 return true; 00266 } 00267 00268 00269 /************************************************************************* 00270 Add properties for the slider 00271 *************************************************************************/ 00272 void Slider::addSliderProperties(void) 00273 { 00274 addProperty(&d_currentValueProperty); 00275 addProperty(&d_clickStepSizeProperty); 00276 addProperty(&d_maximumValueProperty); 00277 } 00278 00279 00280 } // End of CEGUI namespace section