Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Slider Class Reference

#include <slider.h>

Inheritance diagram for Slider:
Control EventResponder

Public Member Functions

void setMaxMin (int max, int min=0)
 
void setThumbLength (float len)
 
void setPageSize (int s)
 
int position (void)
 
void setPosition (int pos)
 
void setThumbColor (const GFXColor &color, const GFXColor &outline)
 
void setThumbColorBasedOnColor (const GFXColor &c)
 
virtual void draw (void)
 
virtual void setRect (const Rect &r)
 
virtual void setColor (const GFXColor &c)
 
virtual bool processMouseDown (const InputEvent &event)
 
virtual bool processMouseUp (const InputEvent &event)
 
virtual bool processMouseDrag (const InputEvent &event)
 
 Slider (void)
 
virtual ~Slider (void)
 
- Public Member Functions inherited from Control
virtual Rect rect (void)
 
virtual bool hitTest (const Point &p)
 
virtual bool hidden (void)
 
virtual void setHidden (bool h=true)
 
virtual const std::string & id (void)
 
virtual void setId (const std::string &newId)
 
virtual GFXColor color (void)
 
virtual GFXColor outlineColor (void)
 
virtual void setOutlineColor (const GFXColor &c)
 
virtual GFXColor textColor (void)
 
virtual void setTextColor (const GFXColor &c)
 
virtual Font font (void)
 
virtual void setFont (const Font &f)
 
virtual bool hasGroupChildren (void)
 
 Control (void)
 
virtual ~Control (void)
 
- Public Member Functions inherited from EventResponder
virtual bool processCommand (const EventCommandId &command, Control *control)
 
virtual bool processKeyDown (const InputEvent &event)
 
virtual bool processKeyUp (const InputEvent &event)
 
virtual void processUnfocus (const InputEvent &event)
 
virtual bool processMouseMove (const InputEvent &event)
 
virtual void sendCommand (const EventCommandId &command, Control *control)
 
virtual void setCommandTarget (EventResponder *responder)
 
virtual void setModal (bool modal)
 
 EventResponder (void)
 
virtual ~EventResponder (void)
 

Protected Types

enum  MouseState { MOUSE_NONE, MOUSE_PAGE_UP, MOUSE_PAGE_DOWN, MOUSE_THUMB_DRAG }
 

Protected Attributes

int m_minValue
 
int m_maxValue
 
float m_thumbLength
 
float m_originalThumbLength
 
int m_pageSize
 
GFXColor m_thumbColor
 
GFXColor m_thumbOutlineColor
 
int m_position
 
bool m_vertical
 
MouseState m_mouseState
 
float m_buttonDownMouse
 
int m_buttonDownPosition
 
Rect m_thumbRect
 
- Protected Attributes inherited from Control
Rect m_rect
 
std::string m_id
 
GFXColor m_color
 
GFXColor m_outlineColor
 
GFXColor m_textColor
 
Font m_font
 
bool m_hidden
 
- Protected Attributes inherited from EventResponder
bool m_modal
 
EventResponderm_commandTarget
 

Additional Inherited Members

- Protected Member Functions inherited from Control
virtual void drawBackground (void)
 

Detailed Description

Definition at line 31 of file slider.h.

Member Enumeration Documentation

enum Slider::MouseState
protected
Enumerator
MOUSE_NONE 
MOUSE_PAGE_UP 
MOUSE_PAGE_DOWN 
MOUSE_THUMB_DRAG 

Definition at line 80 of file slider.h.

81  {
82  MOUSE_NONE, //Nothing interesting going on.
83  MOUSE_PAGE_UP, //In the middle of a Page Up click.
84  MOUSE_PAGE_DOWN, //In the middle of a Page Down click.
85  MOUSE_THUMB_DRAG //In the middle of dragging the thumb.
86  };

Constructor & Destructor Documentation

Slider::Slider ( void  )

Definition at line 289 of file slider.cpp.

289  :
290  m_minValue( 0 )
291  , m_maxValue( 100 )
292  , m_thumbLength( .15 )
294  , m_pageSize( 10 )
295  , m_thumbColor( m_color )
298  , m_vertical( true )
300  , m_buttonDownMouse( 0.0 )
301  , m_buttonDownPosition( 0 )
302  , m_thumbRect()
303 {}
virtual Slider::~Slider ( void  )
inlinevirtual

Definition at line 74 of file slider.h.

74 {}

Member Function Documentation

void Slider::draw ( void  )
virtual

Implements Control.

Definition at line 156 of file slider.cpp.

References Control::drawBackground(), drawRect(), drawRectOutline(), float, Size::height, Rect::inset(), isClear(), m_maxValue, m_minValue, m_position, Control::m_rect, m_thumbColor, m_thumbLength, m_thumbOutlineColor, m_thumbRect, m_vertical, Rect::origin, Rect::size, Size::width, Point::x, and Point::y.

157 {
158  //Draw the background.
159  drawBackground();
160  //Draw the thumb.
161  if ( !( isClear( m_thumbColor ) && isClear( m_thumbOutlineColor ) ) ) {
162  float relativePosition = 0.0;
163  if (m_maxValue != m_minValue)
164  relativePosition = ( (float) (m_position-m_minValue) )/(m_maxValue-m_minValue);
165  Rect thumbRect = m_rect;
166  if (m_vertical) {
167  //Vertical thumb.
169  thumbRect.origin.y += (1.0-relativePosition)*(m_rect.size.height-thumbRect.size.height);
170  } else {
171  //Horizontal thumb.
172  thumbRect.size.width = m_thumbLength*m_rect.size.width;
173  thumbRect.origin.x += relativePosition*(m_rect.size.width-thumbRect.size.width);
174  }
175  m_thumbRect = thumbRect; //Want to save away the bigger version for mouse hits.
176  thumbRect.inset( Size( .01, .01 ) );
177  drawRect( thumbRect, m_thumbColor );
178  drawRectOutline( thumbRect, m_thumbOutlineColor, 1.0 );
179  }
180 }
int Slider::position ( void  )
inline

Definition at line 48 of file slider.h.

References m_position.

Referenced by Scroller::processCommand(), processMouseDown(), and processMouseUp().

49  {
50  return m_position;
51  }
bool Slider::processMouseDown ( const InputEvent event)
virtual

Reimplemented from EventResponder.

Definition at line 182 of file slider.cpp.

References InputEvent::code, VegaConfig::getVariable(), globalEventManager(), Control::hitTest(), LEFT_MOUSE_BUTTON, InputEvent::loc, m_buttonDownMouse, m_buttonDownPosition, m_mouseState, m_position, m_thumbLength, m_thumbRect, m_vertical, MOUSE_PAGE_DOWN, MOUSE_PAGE_UP, MOUSE_THUMB_DRAG, NO_THUMB_LENGTH, Rect::origin, XMLSupport::parse_int(), position(), EventResponder::processMouseDown(), EventManager::pushResponder(), Rect::right(), EventResponder::setModal(), setPosition(), Rect::top(), vs_config, WHEELDOWN_MOUSE_BUTTON, WHEELUP_MOUSE_BUTTON, Point::x, and Point::y.

183 {
184  static int zoominc = XMLSupport::parse_int( vs_config->getVariable( "general", "wheel_increment_lines", "3" ) );
185  if ( event.code == LEFT_MOUSE_BUTTON && m_thumbLength != NO_THUMB_LENGTH && hitTest( event.loc ) ) {
186  if (m_vertical) {
187  if (event.loc.y < m_thumbRect.origin.y) {
189  } else if ( event.loc.y > m_thumbRect.top() ) {
191  } else {
193  m_buttonDownMouse = event.loc.y;
195  }
196  } else {
197  if (event.loc.x < m_thumbRect.origin.x) {
199  } else if ( event.loc.x > m_thumbRect.right() ) {
201  } else {
203  m_buttonDownMouse = event.loc.x;
205  }
206  }
207  setModal( true ); //Make sure we don't miss anything.
208  //Make sure we see mouse events *first* until we get a mouse-up.
210  return true;
211  } else if (event.code == WHEELUP_MOUSE_BUTTON) {
212  if ( hitTest( event.loc ) )
213  setPosition( position()-zoominc );
214  } else if (event.code == WHEELDOWN_MOUSE_BUTTON) {
215  if ( hitTest( event.loc ) )
216  setPosition( position()+zoominc );
217  }
218  return Control::processMouseDown( event );
219 }
bool Slider::processMouseDrag ( const InputEvent event)
virtual

Reimplemented from EventResponder.

Definition at line 221 of file slider.cpp.

References Rect::copyAndInset(), float_to_int(), Size::height, Rect::inside(), InputEvent::loc, m_buttonDownMouse, m_buttonDownPosition, m_maxValue, m_minValue, m_mouseState, Control::m_rect, m_thumbLength, m_vertical, MOUSE_THUMB_DRAG, NO_THUMB_LENGTH, EventResponder::processMouseDrag(), setPosition(), Rect::size, Size::width, Point::x, and Point::y.

222 {
223  //The interface for mouse dragging is a little weird. There is no button information. All
224  //we know is that some button is down. This is enough, since we don't get into a specific
225  //mouse state in this control unless we know which mouse button was pressed...
227  if (m_thumbLength == NO_THUMB_LENGTH) return true;
228  const Rect cancelRect = m_rect.copyAndInset( THUMB_DRAG_CANCEL_MARGINS );
229  if ( !cancelRect.inside( event.loc ) ) {
230  //We are outside the cancel rect. Go back to original position.
232  return true;
233  } else {
234  //We are dragging the thumb -- get a new scroll position.
235  if (m_vertical) {
236  //Calculate the factor to convert a change in mouse coords to a change in slider position.
237  //This is derived from the ratio of the non-thumb length in the slider to the
238  //total range.
239  const float totalMouseLength = (1.0-m_thumbLength)*m_rect.size.height;
240  const int totalRange = m_maxValue-m_minValue;
241  const int positionChange = float_to_int( (m_buttonDownMouse-event.loc.y)*totalRange/totalMouseLength+0.5 );
242  setPosition( m_buttonDownPosition+positionChange );
243  return true;
244  } else {
245  const float totalMouseLength = (1.0-m_thumbLength)*m_rect.size.width;
246  const int totalRange = m_maxValue-m_minValue;
247  const int positionChange = float_to_int( (event.loc.x-m_buttonDownMouse)*totalRange/totalMouseLength+0.5 );
248  setPosition( m_buttonDownPosition+positionChange );
249  return true;
250  }
251  }
252  return Control::processMouseDrag( event );
253  }
254  return false;
255 }
bool Slider::processMouseUp ( const InputEvent event)
virtual

Reimplemented from EventResponder.

Definition at line 257 of file slider.cpp.

References InputEvent::code, globalEventManager(), Control::hitTest(), LEFT_MOUSE_BUTTON, InputEvent::loc, m_mouseState, m_pageSize, MOUSE_NONE, MOUSE_PAGE_DOWN, MOUSE_PAGE_UP, MOUSE_THUMB_DRAG, position(), EventResponder::processMouseUp(), EventManager::removeResponder(), EventResponder::setModal(), and setPosition().

258 {
259  if (m_mouseState != MOUSE_NONE && event.code == LEFT_MOUSE_BUTTON) {
260  //We are now done with the modal mouse loop.
261  switch (m_mouseState)
262  {
263  case MOUSE_PAGE_UP:
264  if ( hitTest( event.loc ) )
266  break;
267  case MOUSE_PAGE_DOWN:
268  if ( hitTest( event.loc ) )
270  break;
271  case MOUSE_THUMB_DRAG:
272  //Whatever position we have is the correct one.
273  //Do nothing.
274  break;
275  default:
276  assert( false ); //Forgot an enum case.
277  }
278  //Make sure we get off the event chain.
279  globalEventManager().removeResponder( this, true );
280  setModal( false );
282 
283  return Control::processMouseUp( event );
284  }
285  return false;
286 }
void Slider::setColor ( const GFXColor c)
virtual

Reimplemented from Control.

Definition at line 147 of file slider.cpp.

References Control::setColor(), and setThumbColorBasedOnColor().

Referenced by Scroller::createControls().

148 {
149  //Calculate a reasonable thumb color.
151 
152  Control::setColor( c );
153 }
void Slider::setMaxMin ( int  max,
int  min = 0 
)

Definition at line 65 of file slider.cpp.

References float_to_int(), m_maxValue, m_minValue, max(), min(), and setPageSize().

Referenced by Scroller::setRangeValues().

66 {
67  m_maxValue = max;
68  m_minValue = min;
69 
70  //Set default page size
71  const int pageSize = float_to_int( (max-min)/10.0+0.5 );
72  setPageSize( pageSize );
73 }
void Slider::setPageSize ( int  s)
inline

Definition at line 41 of file slider.h.

References m_pageSize.

Referenced by setMaxMin(), and Scroller::setRangeValues().

42  {
43  m_pageSize = s;
44  }
void Slider::setPosition ( int  pos)

Definition at line 51 of file slider.cpp.

References m_maxValue, m_minValue, m_position, and EventResponder::sendCommand().

Referenced by processMouseDown(), processMouseDrag(), processMouseUp(), and Scroller::setScrollPosition().

52 {
53  int newPosition = pos;
54  if (pos > m_maxValue)
55  newPosition = m_maxValue;
56  else if (pos < m_minValue)
57  newPosition = m_minValue;
58  if (m_position != newPosition) {
59  m_position = newPosition;
60  sendCommand( "Slider::PositionChanged", this );
61  }
62 }
void Slider::setRect ( const Rect r)
virtual

Reimplemented from Control.

Definition at line 94 of file slider.cpp.

References Size::height, m_originalThumbLength, Control::m_rect, m_vertical, Control::setRect(), setThumbLength(), Rect::size, and Size::width.

95 {
96  Control::setRect( r );
97 
98  //Calculate other things based on new rect.
101 }
void Slider::setThumbColor ( const GFXColor color,
const GFXColor outline 
)
inline

Definition at line 55 of file slider.h.

References Control::color(), m_thumbColor, and m_thumbOutlineColor.

Referenced by Scroller::setThumbColor(), and setThumbColorBasedOnColor().

56  {
58  m_thumbOutlineColor = outline;
59  }
void Slider::setThumbColorBasedOnColor ( const GFXColor c)

Definition at line 134 of file slider.cpp.

References darkenColor(), GUI_OPAQUE_WHITE(), isClear(), isColorLight(), lightenColor(), and setThumbColor().

Referenced by Scroller::createControls(), setColor(), and Scroller::setColor().

135 {
136  if ( !isClear( c ) ) {
137  if ( isColorLight( c ) )
138  //Light color. Make thumb darker.
140  else
141  //Dark Color.
143  }
144 }
void Slider::setThumbLength ( float  len)

Definition at line 76 of file slider.cpp.

References ABSOLUTE_MIN_THUMB_LENGTH, Size::height, m_originalThumbLength, Control::m_rect, m_thumbLength, m_vertical, MAX_THUMB_LENGTH, MIN_THUMB_LENGTH, NO_THUMB_LENGTH, Rect::size, and Size::width.

Referenced by Scroller::setRangeValues(), and setRect().

77 {
78  float realLen = m_originalThumbLength = len;
79  if (realLen > MAX_THUMB_LENGTH || realLen < 0.0)
80  realLen = NO_THUMB_LENGTH;
81  else if (realLen < MIN_THUMB_LENGTH)
82  realLen = MIN_THUMB_LENGTH;
83  //Make sure the thumb is at least an absolute size.
84  if (m_vertical) {
87  } else if (realLen*m_rect.size.width < ABSOLUTE_MIN_THUMB_LENGTH && m_rect.size.height > 0) {
89  }
90  m_thumbLength = realLen;
91 }

Member Data Documentation

float Slider::m_buttonDownMouse
protected

Definition at line 101 of file slider.h.

Referenced by processMouseDown(), and processMouseDrag().

int Slider::m_buttonDownPosition
protected

Definition at line 102 of file slider.h.

Referenced by processMouseDown(), and processMouseDrag().

int Slider::m_maxValue
protected

Definition at line 91 of file slider.h.

Referenced by draw(), processMouseDrag(), setMaxMin(), and setPosition().

int Slider::m_minValue
protected

Definition at line 90 of file slider.h.

Referenced by draw(), processMouseDrag(), setMaxMin(), and setPosition().

MouseState Slider::m_mouseState
protected

Definition at line 100 of file slider.h.

Referenced by processMouseDown(), processMouseDrag(), and processMouseUp().

float Slider::m_originalThumbLength
protected

Definition at line 93 of file slider.h.

Referenced by setRect(), and setThumbLength().

int Slider::m_pageSize
protected

Definition at line 94 of file slider.h.

Referenced by processMouseUp(), and setPageSize().

int Slider::m_position
protected

Definition at line 98 of file slider.h.

Referenced by draw(), position(), processMouseDown(), and setPosition().

GFXColor Slider::m_thumbColor
protected

Definition at line 95 of file slider.h.

Referenced by draw(), and setThumbColor().

float Slider::m_thumbLength
protected

Definition at line 92 of file slider.h.

Referenced by draw(), processMouseDown(), processMouseDrag(), and setThumbLength().

GFXColor Slider::m_thumbOutlineColor
protected

Definition at line 96 of file slider.h.

Referenced by draw(), and setThumbColor().

Rect Slider::m_thumbRect
protected

Definition at line 103 of file slider.h.

Referenced by draw(), and processMouseDown().

bool Slider::m_vertical
protected

Definition at line 99 of file slider.h.

Referenced by draw(), processMouseDown(), processMouseDrag(), setRect(), and setThumbLength().


The documentation for this class was generated from the following files: