Spinning Topp Logo BlackTopp Studios
inc
scrollbar.cpp
1 // © Copyright 2010 - 2016 BlackTopp Studios Inc.
2 /* This file is part of The Mezzanine Engine.
3 
4  The Mezzanine Engine is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  The Mezzanine Engine is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* The original authors have included a copy of the license specified above in the
18  'Docs' folder. See 'gpl.txt'
19 */
20 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21  Build professional software and charge for their product.
22 
23  However there are some practical restrictions, so if your project involves
24  any of the following you should contact us and we will try to work something
25  out:
26  - DRM or Copy Protection of any kind(except Copyrights)
27  - Software Patents You Do Not Wish to Freely License
28  - Any Kind of Linking to Non-GPL licensed Works
29  - Are Currently In Violation of Another Copyright Holder's GPL License
30  - If You want to change our code and not add a few hundred MB of stuff to
31  your distribution
32 
33  These and other limitations could cause serious legal problems if you ignore
34  them, so it is best to simply contact us or the Free Software Foundation, if
35  you have any questions.
36 
37  Joseph Toppi - toppij@gmail.com
38  John Blackwood - makoenergy02@gmail.com
39 */
40 #ifndef _uiscrollbar_cpp
41 #define _uiscrollbar_cpp
42 
43 #include "UI/scrollbar.h"
44 #include "UI/button.h"
45 #include "UI/pagedcontainer.h"
46 #include "UI/screen.h"
47 #include "UI/uimanager.h"
48 
49 #include "Input/metacode.h"
50 
51 #include <algorithm>
52 
53 namespace Mezzanine
54 {
55  namespace UI
56  {
57  ///////////////////////////////////////////////////////////////////////////////
58  // Scrollbar Methods
59 
60  const String Scrollbar::TypeName = "Scrollbar";
61  const String Scrollbar::EventScrollValueChanged = "ScrollValueChanged";
62 
64  PageProvider(Parent),
65  Scroller(NULL),
66  ScrollBack(NULL),
67  UpLeftButton(NULL),
68  DownRightButton(NULL),
69  ChildLock(NULL),
70  IncrementDistance(0.01),
71  ScrollerSize(1.0),
72  AutoHideScroll(false)
73  { }
74 
75  Scrollbar::Scrollbar(const String& RendName, Screen* Parent) :
76  PageProvider(RendName,Parent),
77  Scroller(NULL),
78  ScrollBack(NULL),
79  UpLeftButton(NULL),
80  DownRightButton(NULL),
81  ChildLock(NULL),
82  IncrementDistance(0.01),
83  ScrollerSize(1.0),
84  AutoHideScroll(false)
85  { this->AddEvent(Scrollbar::EventScrollValueChanged); }
86 
87  Scrollbar::Scrollbar(const String& RendName, const UnifiedRect& RendRect, Screen* Parent) :
88  PageProvider(RendName,RendRect,Parent),
89  Scroller(NULL),
90  ScrollBack(NULL),
91  UpLeftButton(NULL),
92  DownRightButton(NULL),
93  ChildLock(NULL),
94  IncrementDistance(0.01),
95  ScrollerSize(1.0),
96  AutoHideScroll(false)
97  { this->AddEvent(Scrollbar::EventScrollValueChanged); }
98 
100  { }
101 
103  {
104  if( this->ChildLock != NULL ) {
105  if( Code.IsDeviceButton() && Code.GetMetaValueAsButtonState() == Input::BUTTON_DOWN && !( this->ChildLock->IsOnStandby() ) ) {
106  if( this->ChildLock == this->ScrollBack ) {
108  }else if( this->ChildLock == this->UpLeftButton ) {
109  return this->_ButtonScroll(this->UpLeftButton);
110  }else if( this->ChildLock == this->DownRightButton ) {
111  return this->_ButtonScroll(this->DownRightButton);
112  }
113  }else if( Code.GetCode() == Input::MOUSEHORIZONTAL ) {
114  Vector2 Delta( static_cast<Real>(Code.GetMetaValue()),0 );
115  return this->_MouseScroll(Delta);
116  }else if( Code.GetCode() == Input::MOUSEVERTICAL ) {
117  Vector2 Delta( 0,static_cast<Real>(Code.GetMetaValue()) );
118  return this->_MouseScroll(Delta);
119  }
120  }else if( Code.GetCode() == Input::MOUSEWHEELVERTICAL || Code.GetCode() == Input::MOUSEWHEELHORIZONTAL ) {
121  return this->_MouseWheelScroll(Code.GetMetaValue());
122  }
123  return false;
124  }
125 
127  {
129  //this->Scroller->Subscribe(Button::EventStandby,this);
132  //this->ScrollBack->Subscribe(Button::EventStandby,this);
134  if( this->UpLeftButton != NULL )
135  {
137  //this->UpLeftButton->Subscribe(Button::EventStandby,this);
139  }
140  if( this->DownRightButton != NULL )
141  {
143  //this->DownRightButton->Subscribe(Button::EventStandby,this);
145  }
146  }
147 
149  {
150  return ( this->GetUpperScrollLimit() - this->GetLowerScrollLimit() );
151  }
152 
153  ///////////////////////////////////////////////////////////////////////////////
154  // Utility Methods
155 
157  { this->IncrementDistance = IncDist; }
158 
160  { return this->IncrementDistance; }
161 
163  { this->AutoHideScroll = AutoHide; }
164 
166  { return this->AutoHideScroll; }
167 
168  ///////////////////////////////////////////////////////////////////////////////
169  // Fetch Methods
170 
172  { return this->Scroller; }
173 
175  { return this->UpLeftButton; }
176 
178  { return this->DownRightButton; }
179 
181  { return this->ScrollBack; }
182 
183  ///////////////////////////////////////////////////////////////////////////////
184  // Serialization
185 
187  {
188  this->Widget::ProtoSerializeProperties(SelfRoot);
189  XML::Node PropertiesNode = SelfRoot.AppendChild( Scrollbar::GetSerializableName() + "Properties" );
190 
191  if( PropertiesNode.AppendAttribute("Version").SetValue("1") &&
192  PropertiesNode.AppendAttribute("IncrementDistance").SetValue( this->GetIncrementDistance() ) &&
193  PropertiesNode.AppendAttribute("ScrollerSize").SetValue( this->GetScrollerSize() ) &&
194  PropertiesNode.AppendAttribute("AutoHideScroll").SetValue( this->AutoHideScroll ) )
195  {
196  return;
197  }else{
198  SerializeError("Create XML Attribute Values",Scrollbar::GetSerializableName() + "Properties",true);
199  }
200  }
201 
203  {
204  this->Widget::ProtoDeSerializeProperties(SelfRoot);
205 
206  XML::Attribute CurrAttrib;
207  XML::Node PropertiesNode = SelfRoot.GetChild( Scrollbar::GetSerializableName() + "Properties" );
208 
209  if( !PropertiesNode.Empty() ) {
210  if(PropertiesNode.GetAttribute("Version").AsInt() == 1) {
211  CurrAttrib = PropertiesNode.GetAttribute("IncrementDistance");
212  if( !CurrAttrib.Empty() )
213  this->SetIncrementDistance( CurrAttrib.AsReal() );
214 
215  CurrAttrib = PropertiesNode.GetAttribute("ScrollerSize");
216  if( !CurrAttrib.Empty() )
217  this->SetScrollerSize( CurrAttrib.AsReal() );
218 
219  CurrAttrib = PropertiesNode.GetAttribute("AutoHideScroll");
220  if( !CurrAttrib.Empty() )
221  this->SetAutoHide( CurrAttrib.AsBool() );
222  }else{
223  MEZZ_EXCEPTION(ExceptionBase::INVALID_VERSION_EXCEPTION,"Incompatible XML Version for " + (Scrollbar::GetSerializableName() + "Properties") + ": Not Version 1.");
224  }
225  }else{
226  MEZZ_EXCEPTION(ExceptionBase::II_IDENTITY_NOT_FOUND_EXCEPTION,Scrollbar::GetSerializableName() + "Properties" + " was not found in the provided XML node, which was expected.");
227  }
228  }
229 
231  {
232  return Scrollbar::TypeName;
233  }
234 
235  ///////////////////////////////////////////////////////////////////////////////
236  // Internal Event Methods
237 
238  void Scrollbar::_OnScrollValueChanged(const Real OldValue, const Real NewValue)
239  {
240  if( this->Container != NULL ) {
242  }
243 
244  ScrollbarValueChangedArgumentsPtr Args( new ScrollbarValueChangedArguments(Scrollbar::EventScrollValueChanged,this->Name,OldValue,NewValue) );
245  this->FireEvent(Args);
246  }
247 
248  ///////////////////////////////////////////////////////////////////////////////
249  // Internal Methods
250 
252  {
253  WidgetEventArgumentsPtr WidArgs = CountedPtrCast<WidgetEventArguments>(Args);
254  Widget* EventWidget = this->ParentScreen->GetWidget(WidArgs->WidgetName);
255  if( EventWidget == NULL )
256  return;
257 
258  if( EventWidget == this->Scroller )
259  {
260  if( WidArgs->EventName == Button::EventActivated && ChildLock == NULL )
261  {
262  // Obtain the lock
263  this->ChildLock = this->Scroller;
264  }
265  else if( WidArgs->EventName == Button::EventDeactivated )
266  {
267  // Release the lock
268  this->ChildLock = NULL;
269  }
270  }
271  else if( EventWidget == this->ScrollBack )
272  {
273  if( WidArgs->EventName == Button::EventActivated && ChildLock == NULL )
274  {
275  // Obtain the lock
276  this->ChildLock = this->ScrollBack;
278  }
279  else if( WidArgs->EventName == Button::EventDeactivated )
280  {
281  // Release the lock
282  this->ChildLock = NULL;
283  }
284  }
285  else if( EventWidget == this->UpLeftButton )
286  {
287  if( WidArgs->EventName == Button::EventActivated && ChildLock == NULL )
288  {
289  // Obtain the lock
290  this->ChildLock = this->UpLeftButton;
291  this->_ButtonScroll(this->UpLeftButton);
292  }
293  else if( WidArgs->EventName == Button::EventDeactivated )
294  {
295  // Release the lock
296  this->ChildLock = NULL;
297  }
298  }
299  else if( EventWidget == this->DownRightButton )
300  {
301  if( WidArgs->EventName == Button::EventActivated && ChildLock == NULL )
302  {
303  // Obtain the lock
304  this->ChildLock = this->DownRightButton;
305  this->_ButtonScroll(this->DownRightButton);
306  }
307  else if( WidArgs->EventName == Button::EventDeactivated )
308  {
309  // Release the lock
310  this->ChildLock = NULL;
311  }
312  }
313  }
314  }//UI
315 }//Mezzanine
316 
317 #endif
Attribute AppendAttribute(const Char8 *Name)
Creates an Attribute and puts it at the end of this Nodes attributes.
virtual Boole _MouseWheelScroll(const Integer Direction)=0
Performs the operations for when the scroller is manipulated by the mouse wheel.
A light-weight handle for manipulating attributes in DOM tree.
Definition: attribute.h:74
Button * ScrollBack
The background around that can be clicked on and represents the valid bounds for the scroller...
Definition: scrollbar.h:139
Button * Scroller
The manipulatable widget that represents the current position on the scrollbar/slider.
Definition: scrollbar.h:136
virtual Boole _ButtonScroll(Button *TheButton)=0
Performs the operations for when one of the buttons is pressed to manipulate the scroller.
virtual void _OnScrollValueChanged(const Real OldValue, const Real NewValue)
Self logic to be executed when this scrollbar changes it's scrollvalue.
Definition: scrollbar.cpp:238
virtual Real GetIncrementDistance() const
Gets the relative distance the scroller will move on a button press.
Definition: scrollbar.cpp:159
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
bool AsBool(bool def=false) const
Attempts to convert the value of the attribute to a float and returns the results.
static const String EventActivated
Event name for when this activatable widget is activated.
Definition: button.h:93
virtual void SubscribeToChildEvents()
Subscribes to all the events of this scrollbars children we care about. Used only on construction...
Definition: scrollbar.cpp:126
static const String EventDeactivated
Event name for when this activatable widget is deactivated.
Definition: button.h:97
virtual void SetAutoHide(Boole AutoHide)
Sets whether or not this scrollbar will hide when it's set page container has less than one page to d...
Definition: scrollbar.cpp:162
virtual Boole GetAutoHide() const
Gets whether or not this scrollbar will hide when it's set page container has less than one page to d...
Definition: scrollbar.cpp:165
virtual ~Scrollbar()
Standard class destructor.
Definition: scrollbar.cpp:99
virtual Button * GetDownRightButton() const
Gets the DownRight button within this widget, if it was initialized.
Definition: scrollbar.cpp:177
static String GetSerializableName()
Get the name of the the XML tag the Renderable class will leave behind as its instances are serialize...
Definition: scrollbar.cpp:230
Thrown when the requested identity could not be found.
Definition: exception.h:94
virtual Real GetScrollRange() const
Gets the range on which the scroller can be placed.
Definition: scrollbar.cpp:148
#define MEZZ_EXCEPTION(num, desc)
An easy way to throw exceptions with rich information.
Definition: exception.h:3048
A simple reference counting pointer.
Definition: countedptr.h:70
virtual void ProtoSerializeProperties(XML::Node &SelfRoot) const
Convert the properties of this class to an XML::Node ready for serialization.
Definition: widget.cpp:278
EventSubscriberSlot * Subscribe(const String &EventName, EventSubscriber *Sub)
Adds a subscriber to this event.
Thrown when a version is accessed/parsed/required and it cannot work correctly or is missing...
Definition: exception.h:112
virtual Button * GetScroller() const
Gets the Scroller button within this widget.
Definition: scrollbar.cpp:171
virtual Button * GetScrollBack() const
Gets the Scroller background within this widget.
Definition: scrollbar.cpp:180
virtual Boole _ScrollBackScroll(const Vector2 &HitPosition)=0
Performs the operations for when the scrollback is clicked on to manipulate the scroller.
bool Empty() const
Is this storing anything at all?
This is the EventArguments class for when the scrollvalue of a scrollbar is updated.
Definition: scrollbar.h:56
static const String TypeName
String containing the type name for this class: "Scrollbar".
Definition: scrollbar.h:130
virtual Button * GetUpLeftButton() const
Gets the UpLeft button within this widget, if it was initialized.
Definition: scrollbar.cpp:174
PagedContainer * Container
A pointer to the PagedContainer this scrollbar is providing page data for.
Definition: pageprovider.h:60
Button * DownRightButton
The Down/Right button that when clicked will move the scroller in that respective direction...
Definition: scrollbar.h:147
virtual void SetIncrementDistance(const Real &IncDist)
Sets the relative distance the scroller will move when the up/left or down/right buttons are pressed...
Definition: scrollbar.cpp:156
Int32 GetMetaValue() const
This Returns the MetaValue.
Definition: metacode.cpp:250
float Real
A Datatype used to represent a real floating point number.
Definition: datatypes.h:141
bool SetValue(const Char8 *rhs)
Set the value of this.
Scrollbar(Screen *Parent)
Blank constructor.
Definition: scrollbar.cpp:63
This class represents a 2D rect which can express the size and position of a renderable on screen...
Definition: unifieddim.h:661
static const String EventScrollValueChanged
Event name for when this scrollbar has his scrollvalue updated.
Definition: scrollbar.h:132
virtual void ProtoDeSerializeProperties(const XML::Node &SelfRoot)
Take the data stored in an XML Node and overwrite the properties of this object with it...
Definition: scrollbar.cpp:202
A light-weight handle for manipulating nodes in DOM tree.
Definition: node.h:89
int AsInt(int def=0) const
Attempts to convert the value of the attribute to an int and returns the results. ...
This is used to represent a point on a 2 dimentional area, such as a screen.
Definition: vector2.h:63
bool Empty() const
Is this storing anything at all?
This is the base class for all widgets.
Definition: widget.h:126
Real AsReal(Real def=0) const
Attempts to convert the value of the attribute to a Real and returns the results. ...
Event * AddEvent(const String &EventName)
Creates a new event this Publisher can fire.
String Name
The unique name of this Renderable.
Definition: renderable.h:81
void FireEvent(EventArgumentsPtr Args)
Fires an event.
Input::InputCode GetCode() const
This Returns the Inputcode.
Definition: metacode.cpp:244
This Determines the kind of user input.
Definition: metacode.h:93
This is the base class for interpretting widget values to page positions.
Definition: pageprovider.h:55
Real IncrementDistance
The distance the scroller is to be moved when the up, left, down, or right buttons are clicked...
Definition: scrollbar.h:153
virtual Widget * GetWidget(const String &Name)
Gets a widget in this screen by name.
Definition: screen.cpp:573
Boole AutoHideScroll
Stores whether or not this scrollbar should hide when there is 1 page or less of list items...
Definition: scrollbar.h:159
Button * UpLeftButton
The Up/Left button that when clicked will move the scroller in that respective direction.
Definition: scrollbar.h:143
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
virtual Real GetUpperScrollLimit() const =0
Gets the pixel position of the upper limit the scroller can be placed on.
Boole IsDeviceButton() const
Does this MetaCode Represent a state of any button on an input device.
Definition: metacode.cpp:331
virtual const Vector2 & GetMouseHitPosition() const
Gets the mouse position from the last call to "FindHoveredQuad(const Vector2&).
Definition: screen.cpp:434
This class is a helper class, specifically for use as a button.
Definition: button.h:66
virtual void ProtoDeSerializeProperties(const XML::Node &SelfRoot)
Take the data stored in an XML Node and overwrite the properties of this object with it...
Definition: widget.cpp:337
Button * ChildLock
This is a pointer to the specific child that is locked and being manipulated.
Definition: scrollbar.h:150
virtual Real GetLowerScrollLimit() const =0
Gets the pixel position of the lower limit the scroller can be placed on.
virtual void SetScrollerSize(const Real &Size)=0
Sets the length(or height) of the scroller based on the relative size of it's background.
void SerializeError(const String &FailedTo, const String &ClassName, Boole SOrD)
Simply does some string concatenation, then throws an Exception.
Node AppendChild(NodeType Type=NodeElement)
Creates a Node and makes it a child of this one.
Input::ButtonState GetMetaValueAsButtonState() const
Get the MetaValue as a Input::ButtonState.
Definition: metacode.cpp:269
This class is a helper class for creating UI's. It is responsible for storing and keeping track of al...
Definition: screen.h:142
Boole IsOnStandby() const
Gets whether or not this button is currently on standby.
Definition: button.cpp:191
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
virtual void _NotifyEvent(EventArgumentsPtr Args)
Notifies this subscriber of an event being fired.
Definition: scrollbar.cpp:251
virtual void ProtoSerializeProperties(XML::Node &SelfRoot) const
Convert the properties of this class to an XML::Node ready for serialization.
Definition: scrollbar.cpp:186
Attribute GetAttribute(const Char8 *Name) const
Attempt to get an Attribute on this Node with a given name.
virtual Real GetScrollerSize() const =0
Gets the size of the scroller relative to the ScrollBack.
virtual Boole HandleInputImpl(const Input::MetaCode &Code)
Consumes input for this widget's use.
Definition: scrollbar.cpp:102
virtual Boole _MouseScroll(const Vector2 &MouseDelta)=0
Performs the operations for when the scroller is directly manipulated by the mouse.
virtual void UpdateVisibleChildren()
Forces an update of the visible children in this container.
Node GetChild(const Char8 *Name) const
Attempt to get a child Node with a given name.
Screen * ParentScreen
A pointer to the Screen that created this Renderable.
Definition: renderable.h:72