MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
scrollbar.cpp
1 // © Copyright 2010 - 2014 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/screen.h"
46 #include "UI/uimanager.h"
47 #include "Input/metacode.h"
48 
49 #include <algorithm>
50 
51 namespace Mezzanine
52 {
53  namespace UI
54  {
55  ///////////////////////////////////////////////////////////////////////////////
56  // Scrollbar Methods
57 
58  const String Scrollbar::TypeName = "Scrollbar";
59  const String Scrollbar::EventScrollValueChanged = "ScrollValueChanged";
60 
62  PageProvider(Parent),
63  Scroller(NULL),
64  ScrollBack(NULL),
65  UpLeftButton(NULL),
66  DownRightButton(NULL),
67  ChildLock(NULL),
68  IncrementDistance(0.01)
69  { }
70 
71  Scrollbar::Scrollbar(const String& RendName, Screen* Parent) :
72  PageProvider(RendName,Parent),
73  Scroller(NULL),
74  ScrollBack(NULL),
75  UpLeftButton(NULL),
76  DownRightButton(NULL),
77  ChildLock(NULL),
78  IncrementDistance(0.01)
80 
81  Scrollbar::Scrollbar(const String& RendName, const UnifiedRect& RendRect, Screen* Parent) :
82  PageProvider(RendName,RendRect,Parent),
83  Scroller(NULL),
84  ScrollBack(NULL),
85  UpLeftButton(NULL),
86  DownRightButton(NULL),
87  ChildLock(NULL),
88  IncrementDistance(0.01)
90 
92  { }
93 
95  {
96  if( this->ChildLock != NULL )
97  {
98  if( Code.IsDeviceButton() && Code.GetMetaValueAsButtonState() == Input::BUTTON_DOWN && !( this->ChildLock->IsOnStandby() ) ) {
99  if( this->ChildLock == this->ScrollBack ) {
101  }else if( this->ChildLock == this->UpLeftButton ) {
102  return this->ButtonScroll(this->UpLeftButton);
103  }else if( this->ChildLock == this->DownRightButton ) {
104  return this->ButtonScroll(this->DownRightButton);
105  }
106  }else if( Code.GetCode() == Input::MOUSEHORIZONTAL ) {
107  Vector2 Delta( static_cast<Real>(Code.GetMetaValue()),0 );
108  return this->MouseScroll(Delta);
109  }else if( Code.GetCode() == Input::MOUSEVERTICAL ) {
110  Vector2 Delta( 0,static_cast<Real>(Code.GetMetaValue()) );
111  return this->MouseScroll(Delta);
112  }
113  }
114  return false;
115  }
116 
118  {
120  //this->Scroller->Subscribe(Button::EventStandby,this);
123  //this->ScrollBack->Subscribe(Button::EventStandby,this);
125  if( this->UpLeftButton != NULL )
126  {
128  //this->UpLeftButton->Subscribe(Button::EventStandby,this);
130  }
131  if( this->DownRightButton != NULL )
132  {
134  //this->DownRightButton->Subscribe(Button::EventStandby,this);
136  }
137  }
138 
140  {
141  return ( this->GetUpperScrollLimit() - this->GetLowerScrollLimit() );
142  }
143 
144  ///////////////////////////////////////////////////////////////////////////////
145  // Utility Methods
146 
148  {
149  this->IncrementDistance = IncDist;
150  }
151 
153  {
154  return this->IncrementDistance;
155  }
156 
157  ///////////////////////////////////////////////////////////////////////////////
158  // Fetch Methods
159 
161  { return this->Scroller; }
162 
164  { return this->UpLeftButton; }
165 
167  { return this->DownRightButton; }
168 
170  { return this->ScrollBack; }
171 
172  ///////////////////////////////////////////////////////////////////////////////
173  // Serialization
174 
176  {
177  this->Widget::ProtoSerializeProperties(SelfRoot);
178  XML::Node PropertiesNode = SelfRoot.AppendChild( Scrollbar::GetSerializableName() + "Properties" );
179 
180  if( PropertiesNode.AppendAttribute("Version").SetValue("1") &&
181  PropertiesNode.AppendAttribute("IncrementDistance").SetValue( this->IncrementDistance ) )
182  {
183  return;
184  }else{
185  SerializeError("Create XML Attribute Values",Scrollbar::GetSerializableName() + "Properties",true);
186  }
187  }
188 
190  {
191  this->Widget::ProtoDeSerializeProperties(SelfRoot);
192 
193  XML::Attribute CurrAttrib;
194  XML::Node PropertiesNode = SelfRoot.GetChild( Scrollbar::GetSerializableName() + "Properties" );
195 
196  if( !PropertiesNode.Empty() ) {
197  if(PropertiesNode.GetAttribute("Version").AsInt() == 1) {
198  CurrAttrib = PropertiesNode.GetAttribute("IncrementDistance");
199  if( !CurrAttrib.Empty() )
200  this->IncrementDistance = CurrAttrib.AsReal();
201  }else{
202  MEZZ_EXCEPTION(Exception::INVALID_VERSION_EXCEPTION,"Incompatible XML Version for " + (Scrollbar::GetSerializableName() + "Properties") + ": Not Version 1.");
203  }
204  }else{
205  MEZZ_EXCEPTION(Exception::II_IDENTITY_NOT_FOUND_EXCEPTION,Scrollbar::GetSerializableName() + "Properties" + " was not found in the provided XML node, which was expected.");
206  }
207  }
208 
210  {
211  return Scrollbar::TypeName;
212  }
213 
214  ///////////////////////////////////////////////////////////////////////////////
215  // Internal Event Methods
216 
217  void Scrollbar::_OnScrollValueChanged(const Real OldValue, const Real NewValue)
218  {
220  this->FireEvent(Args);
221  }
222 
223  ///////////////////////////////////////////////////////////////////////////////
224  // Internal Methods
225 
227  {
228  const WidgetEventArguments& WidArgs = static_cast<const WidgetEventArguments&>(Args);
229  Widget* EventWidget = this->ParentScreen->GetWidget(WidArgs.WidgetName);
230  if( EventWidget == NULL )
231  return;
232 
233  if( EventWidget == this->Scroller )
234  {
235  if( WidArgs.EventName == Button::EventActivated && ChildLock == NULL )
236  {
237  // Obtain the lock
238  this->ChildLock = this->Scroller;
239  }
240  else if( WidArgs.EventName == Button::EventDeactivated )
241  {
242  // Release the lock
243  this->ChildLock = NULL;
244  }
245  }
246  else if( EventWidget == this->ScrollBack )
247  {
248  if( WidArgs.EventName == Button::EventActivated && ChildLock == NULL )
249  {
250  // Obtain the lock
251  this->ChildLock = this->ScrollBack;
253  }
254  else if( WidArgs.EventName == Button::EventDeactivated )
255  {
256  // Release the lock
257  this->ChildLock = NULL;
258  }
259  }
260  else if( EventWidget == this->UpLeftButton )
261  {
262  if( WidArgs.EventName == Button::EventActivated && ChildLock == NULL )
263  {
264  // Obtain the lock
265  this->ChildLock = this->UpLeftButton;
266  this->ButtonScroll(this->UpLeftButton);
267  }
268  else if( WidArgs.EventName == Button::EventDeactivated )
269  {
270  // Release the lock
271  this->ChildLock = NULL;
272  }
273  }
274  else if( EventWidget == this->DownRightButton )
275  {
276  if( WidArgs.EventName == Button::EventActivated && ChildLock == NULL )
277  {
278  // Obtain the lock
279  this->ChildLock = this->DownRightButton;
280  this->ButtonScroll(this->DownRightButton);
281  }
282  else if( WidArgs.EventName == Button::EventDeactivated )
283  {
284  // Release the lock
285  this->ChildLock = NULL;
286  }
287  }
288  }
289  }//UI
290 }//Mezzanine
291 
292 #endif