MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
verticalscrollbar.cpp
1 //© Copyright 2010 - 2013 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 _uiverticalscrollbar_cpp
41 #define _uiverticalscrollbar_cpp
42 
43 #include "UI/verticalscrollbar.h"
44 #include "UI/button.h"
45 #include "UI/screen.h"
46 #include "UI/pagedcontainer.h"
47 #include "UI/uimanager.h"
48 
49 #include "UI/verticallayoutstrategy.h"
50 
51 #include "Input/metacode.h"
52 #include "stringtool.h"
53 #include "mathtool.h"
54 
55 #include <algorithm>
56 
57 namespace Mezzanine
58 {
59  namespace UI
60  {
61  ///////////////////////////////////////////////////////////////////////////////
62  // VerticalScrollbar Methods
63  ///////////////////////////////////////
64 
65  const String VerticalScrollbar::TypeName = "VerticalScrollbar";
66 
68  Scrollbar(Parent)
69  { this->LayoutStrat = new VerticalLayoutStrategy(); }
70 
71  VerticalScrollbar::VerticalScrollbar(const String& RendName, const UI::ScrollbarStyle& Style, Screen* Parent) :
72  Scrollbar(RendName,Parent)
73  { this->ConstructVerticalScrollbar(Style); }
74 
75  VerticalScrollbar::VerticalScrollbar(const String& RendName, const UnifiedRect& RendRect, const UI::ScrollbarStyle& Style, Screen* Parent) :
76  Scrollbar(RendName,RendRect,Parent)
77  { this->ConstructVerticalScrollbar(Style); }
78 
80  Scrollbar(Parent)
81  { this->LayoutStrat = new VerticalLayoutStrategy(); this->ProtoDeSerialize(XMLNode); }
82 
84  {
85  delete this->LayoutStrat;
86  this->LayoutStrat = NULL;
87 
88  if( this->Container != NULL ) {
89  this->Container->UnbindProvider(this);
90  }
91 
92  this->ParentScreen->DestroyWidget(this->Scroller);
96  }
97 
99  {
100  this->LayoutStrat = new VerticalLayoutStrategy();
101  // Create the rects we'll use
102  UnifiedRect ChildRect(0,0,1,1,0,0,0,0);
103  if( UI::SB_NoButtons == ScrollStyle )
104  {
105  // Create our objects
106  this->ScrollBack = ParentScreen->CreateButton(this->Name+".ScrollBack",ChildRect);
109  this->AddChild(this->ScrollBack,0);
110 
111  this->Scroller = ParentScreen->CreateButton(this->Name+".Scroller",ChildRect);
112  this->Scroller->SetManualTransformUpdates(true);
113  this->AddChild(this->Scroller,1);
114  }else{
115  //Get the position for all items involved and configure their offsets
116  if(UI::SB_Separate == ScrollStyle)
117  {
118  // Create our objects
119  this->UpLeftButton = ParentScreen->CreateButton(this->Name+".UpButton",ChildRect);
123  this->AddChild(this->UpLeftButton,0);
124 
125  this->ScrollBack = ParentScreen->CreateButton(this->Name+".ScrollBack",ChildRect);
128  this->AddChild(this->ScrollBack,1);
129 
130  this->DownRightButton = ParentScreen->CreateButton(this->Name+".DownButton",ChildRect);
134  this->AddChild(this->DownRightButton,2);
135 
136  this->Scroller = ParentScreen->CreateButton(this->Name+".Scroller",ChildRect);
137  this->Scroller->SetManualTransformUpdates(true);
138  this->AddChild(this->Scroller,3);
139  }
140  else if(UI::SB_TogetherDownRight == ScrollStyle)
141  {
142  // Create our objects
143  this->ScrollBack = ParentScreen->CreateButton(this->Name+".ScrollBack",ChildRect);
146  this->AddChild(this->ScrollBack,0);
147 
148  this->UpLeftButton = ParentScreen->CreateButton(this->Name+".UpButton",ChildRect);
152  this->AddChild(this->UpLeftButton,1);
153 
154  this->DownRightButton = ParentScreen->CreateButton(this->Name+".DownButton",ChildRect);
158  this->AddChild(this->DownRightButton,2);
159 
160  this->Scroller = ParentScreen->CreateButton(this->Name+".Scroller",ChildRect);
161  this->Scroller->SetManualTransformUpdates(true);
162  this->AddChild(this->Scroller,3);
163  }
164  else if(UI::SB_TogetherUpLeft == ScrollStyle)
165  {
166  // Create our objects
167  this->UpLeftButton = ParentScreen->CreateButton(this->Name+".UpButton",ChildRect);
171  this->AddChild(this->UpLeftButton,0);
172 
173  this->DownRightButton = ParentScreen->CreateButton(this->Name+".DownButton",ChildRect);
177  this->AddChild(this->DownRightButton,1);
178 
179  this->ScrollBack = ParentScreen->CreateButton(this->Name+".ScrollBack",ChildRect);
182  this->AddChild(this->ScrollBack,2);
183 
184  this->Scroller = ParentScreen->CreateButton(this->Name+".Scroller",ChildRect);
185  this->Scroller->SetManualTransformUpdates(true);
186  this->AddChild(this->Scroller,3);
187  }
188  }
189 
190  Rect TempDims;
191  if( this->ActDims != TempDims ) {
192  TempDims = this->ActDims;
193  this->UpdateDimensions(TempDims,TempDims);
194  }
195 
196  this->SubscribeToChildEvents();
197  }
198 
200  {
201  return this->ScrollBack->GetActualPosition().Y + this->ScrollBack->GetActualSize().Y;
202  }
203 
205  {
206  return this->ScrollBack->GetActualPosition().Y;
207  }
208 
209  bool VerticalScrollbar::MouseScroll(const Vector2& MouseDelta)
210  {
211  if( this->ChildLock == this->Scroller && MouseDelta.Y != 0 )
212  {
213  const Real OldScrollerValue = this->GetScrollerValue();
214  const Rect OldScrollerRect = this->Scroller->GetRect();
215  Rect NewScrollerRect;
216  NewScrollerRect.Size = OldScrollerRect.Size;
217 
218  NewScrollerRect.Position.X = OldScrollerRect.Position.X;
219  NewScrollerRect.Position.Y = OldScrollerRect.Position.Y + MouseDelta.Y;
220 
221  NewScrollerRect.Position.Y = std::min(NewScrollerRect.Position.Y,this->GetUpperScrollLimit() - OldScrollerRect.Size.Y);
222  NewScrollerRect.Position.Y = std::max(NewScrollerRect.Position.Y,this->GetLowerScrollLimit());
223 
224  this->Scroller->UpdateDimensions(OldScrollerRect,NewScrollerRect);
225 
226  const Real NewScrollerValue = this->GetScrollerValue();
227  this->_OnScrollValueChanged(OldScrollerValue,NewScrollerValue);
228 
229  return true;
230  }
231  return false;
232  }
233 
235  {
236  if( this->ChildLock == this->ScrollBack )
237  {
238  const Real OldScrollerValue = this->GetScrollerValue();
239  const Rect OldScrollerRect = this->Scroller->GetRect();
240  Rect NewScrollerRect;
241  NewScrollerRect.Size = OldScrollerRect.Size;
242 
243  // Early escape in case of error, but in general this shouldn't happen.
244  if( HitPosition.X == -1.0 || HitPosition.Y == -1.0 )
245  return false;
246 
247  if( HitPosition.Y < OldScrollerRect.Position.Y ) { // Check for above
248  NewScrollerRect.Position.X = OldScrollerRect.Position.X;
249  NewScrollerRect.Position.Y = ( OldScrollerRect.Position.Y - OldScrollerRect.Size.Y >= this->GetLowerScrollLimit() ?
250  this->GetLowerScrollLimit() : OldScrollerRect.Position.Y - OldScrollerRect.Size.Y );
251  }else if( HitPosition.Y > OldScrollerRect.Position.Y + OldScrollerRect.Size.Y ) { // Check for below
252  NewScrollerRect.Position.X = OldScrollerRect.Position.X;
253  NewScrollerRect.Position.Y = ( OldScrollerRect.Position.Y + ( OldScrollerRect.Size.Y * 2 ) >= this->GetUpperScrollLimit() ?
254  this->GetUpperScrollLimit() - OldScrollerRect.Size.Y : OldScrollerRect.Position.Y + OldScrollerRect.Size.Y );
255  }
256 
257  this->Scroller->UpdateDimensions(OldScrollerRect,NewScrollerRect);
258 
259  const Real NewScrollerValue = this->GetScrollerValue();
260  this->_OnScrollValueChanged(OldScrollerValue,NewScrollerValue);
261 
262  return true;
263  }
264  return false;
265  }
266 
268  {
269  if( this->ChildLock == this->UpLeftButton ) {
270  const Real OldScrollerValue = this->GetScrollerValue();
271  const Rect OldScrollerRect = this->Scroller->GetRect();
272  Rect NewScrollerRect;
273  NewScrollerRect.Size = OldScrollerRect.Size;
274 
275  Real ScrollDist = -(this->ScrollBack->GetActualSize().Y * this->IncrementDistance);
276  NewScrollerRect.Position.X = OldScrollerRect.Position.X;
277  NewScrollerRect.Position.Y = OldScrollerRect.Position.Y + ScrollDist;
278 
279  NewScrollerRect.Position.Y = std::min(NewScrollerRect.Position.Y,this->GetUpperScrollLimit() - OldScrollerRect.Size.Y);
280  NewScrollerRect.Position.Y = std::max(NewScrollerRect.Position.Y,this->GetLowerScrollLimit());
281 
282  this->Scroller->UpdateDimensions(OldScrollerRect,NewScrollerRect);
283 
284  const Real NewScrollerValue = this->GetScrollerValue();
285  this->_OnScrollValueChanged(OldScrollerValue,NewScrollerValue);
286 
287  return true;
288  }else if( this->ChildLock == this->DownRightButton ) {
289  const Real OldScrollerValue = this->GetScrollerValue();
290  const Rect OldScrollerRect = this->Scroller->GetRect();
291  Rect NewScrollerRect;
292  NewScrollerRect.Size = OldScrollerRect.Size;
293 
294  Real ScrollDist = this->ScrollBack->GetActualSize().Y * this->IncrementDistance;
295  NewScrollerRect.Position.X = OldScrollerRect.Position.X;
296  NewScrollerRect.Position.Y = OldScrollerRect.Position.Y + ScrollDist;
297 
298  NewScrollerRect.Position.Y = std::min(NewScrollerRect.Position.Y,this->GetUpperScrollLimit() - OldScrollerRect.Size.Y);
299  NewScrollerRect.Position.Y = std::max(NewScrollerRect.Position.Y,this->GetLowerScrollLimit());
300 
301  this->Scroller->UpdateDimensions(OldScrollerRect,NewScrollerRect);
302 
303  const Real NewScrollerValue = this->GetScrollerValue();
304  this->_OnScrollValueChanged(OldScrollerValue,NewScrollerValue);
305 
306  return true;
307  }
308  return false;
309  }
310 
311  ///////////////////////////////////////////////////////////////////////////////
312  // Utility Methods
313 
315  {
317  }
318 
320  {
321  if(Value > 1 || Value < 0)
322  return;
323 
324  const Rect ScrollBackRect = this->ScrollBack->GetRect();
325  const Rect OldScrollerRect = this->Scroller->GetRect();
326  Rect NewScrollerRect;
327 
328  NewScrollerRect.Size.X = OldScrollerRect.Size.X;
329  NewScrollerRect.Size.Y = OldScrollerRect.Size.Y;
330  NewScrollerRect.Position.X = ScrollBackRect.Position.X;
331  NewScrollerRect.Position.Y = ScrollBackRect.Position.Y + ( ( this->GetScrollRange() - NewScrollerRect.Size.Y ) * Value );
332 
333  this->Scroller->UpdateDimensions(OldScrollerRect,NewScrollerRect);
334  }
335 
337  {
338  Real RelPos = this->Scroller->GetActualPosition().Y - this->GetLowerScrollLimit();
339  Real RelLimit = this->GetScrollRange() - this->Scroller->GetActualSize().Y;
340  return (RelPos / RelLimit);
341  }
342 
344  {
345  if(Size > 1 || Size < 0)
346  return;
347 
348  const Rect ScrollBackRect = this->ScrollBack->GetRect();
349  const Rect OldScrollerRect = this->Scroller->GetRect();
350  Rect NewScrollerRect;
351 
352  Real ScrollValue = this->GetScrollerValue();
353  NewScrollerRect.Size.X = ScrollBackRect.Size.X;
354  NewScrollerRect.Size.Y = ScrollBackRect.Size.Y * Size;
355  NewScrollerRect.Position.X = ScrollBackRect.Position.X;
356  NewScrollerRect.Position.Y = ScrollBackRect.Position.Y + ( ( this->GetScrollRange() - NewScrollerRect.Size.Y ) * ScrollValue );
357 
358  this->Scroller->UpdateDimensions(OldScrollerRect,NewScrollerRect);
359  }
360 
362  {
363  return ( this->Scroller->GetActualSize().Y / this->ScrollBack->GetActualSize().Y );
364  }
365 
366  void VerticalScrollbar::UpdateDimensions(const Rect& OldSelfRect, const Rect& NewSelfRect)
367  {
368  // Update the personal data first
369  this->ActDims = NewSelfRect;
370 
371  // Get the data regarding the scroller we'll need after everything else is updated
372  Real ScrollValue = this->GetScrollerValue();
373  Real ScrollSize = this->GetScrollerSize();
374  const Rect OldScrollerRect = this->Scroller->GetRect();
375 
376  // Update the children
377  this->LayoutStrat->Layout(OldSelfRect,NewSelfRect,this->ChildWidgets);
378 
379  // Next prepare the new rect for the scroller
380  const Rect ScrollBackRect = this->ScrollBack->GetRect();
381  Rect NewScrollerRect;
382  NewScrollerRect.Size.X = ScrollBackRect.Size.X;
383  NewScrollerRect.Size.Y = ScrollBackRect.Size.Y * ScrollSize;
384  NewScrollerRect.Position.X = ScrollBackRect.Position.X;
385  NewScrollerRect.Position.Y = ScrollBackRect.Position.Y + ( ( this->GetScrollRange() - NewScrollerRect.Size.Y ) * ScrollValue );
386 
387  // Finally update the scroller
388  this->Scroller->UpdateDimensions(OldScrollerRect,NewScrollerRect);
389 
390  // We done got icky
391  this->_MarkAllLayersDirty();
392  }
393 
394  ///////////////////////////////////////////////////////////////////////////////
395  // PageProvider Methods
396 
398  { return 1; }
399 
401  {
403  return ( Ret > 0 ? Ret : 1 );
404  }
405 
407  { return 1; }
408 
410  { return ( (this->GetMaxYPages() - 1) * this->GetScrollerValue() ) + 1; }
411 
412  ///////////////////////////////////////////////////////////////////////////////
413  // Serialization
414 
416  {
418  }
419 
421  {
423 
424  // Assign the ScrollBack
425  this->ScrollBack = static_cast<Button*>( this->GetChild(this->Name+".ScrollBack") );
426  if( this->ScrollBack == NULL ) {
427  MEZZ_EXCEPTION(Exception::INVALID_STATE_EXCEPTION,"ScrollBack button not found after VerticalScrollbar deserialization.");
428  }
429 
430  // Assign the Scroller
431  this->Scroller = static_cast<Button*>( this->GetChild(this->Name+".Scroller") );
432  if( this->Scroller == NULL ) {
433  MEZZ_EXCEPTION(Exception::INVALID_STATE_EXCEPTION,"Scroller button not found after VerticalScrollbar deserialization.");
434  }
435 
436  if( this->GetNumChildren() > 2 )
437  {
438  // Assign the LeftButton
439  this->UpLeftButton = static_cast<Button*>( this->GetChild(this->Name+".UpButton") );
440  if( this->UpLeftButton == NULL ) {
441  MEZZ_EXCEPTION(Exception::INVALID_STATE_EXCEPTION,"UpButton button not found after VerticalScrollbar deserialization.");
442  }
443 
444  // Assign the RightButton
445  this->DownRightButton = static_cast<Button*>( this->GetChild(this->Name+".DownButton") );
446  if( this->DownRightButton == NULL ) {
447  MEZZ_EXCEPTION(Exception::INVALID_STATE_EXCEPTION,"DownButton button not found after VerticalScrollbar deserialization.");
448  }
449  }
450 
451  this->SubscribeToChildEvents();
452  }
453 
455  { return VerticalScrollbar::TypeName; }
456 
457  ///////////////////////////////////////////////////////////////////////////////
458  // Internal Methods
459 
461  {
462  if( this->Container != NULL ) {
463  Real YView = this->Container->GetActualSize().Y;
464  Real YWork = this->Container->GetActualWorkAreaSize().Y;
465  if( YWork > 0 ) {
466  this->SetScrollerSize( YView / YWork );
467  }else{
468  this->SetScrollerSize( 1.0 );
469  }
470  }
471  }
472 
473  ///////////////////////////////////////////////////////////////////////////////
474  // VerticalScrollbarFactory Methods
475 
477  { return VerticalScrollbar::TypeName; }
478 
480  { return new VerticalScrollbar(RendName,Style,Parent); }
481 
483  { return new VerticalScrollbar(RendName,RendRect,Style,Parent); }
484 
486  { return new VerticalScrollbar(XMLNode,Parent); }
487 
489  { return new VerticalScrollbar(Parent); }
490 
492  {
493  UI::ScrollbarStyle Style = UI::SB_NoButtons;
494 
495  NameValuePairMap::const_iterator ParamIt;
496  ParamIt = Params.find("ScrollbarStyle");
497  if( ParamIt != Params.end() )
498  Style = static_cast<UI::ScrollbarStyle>( StringTools::ConvertToUInt32( (*ParamIt).second ) );
499 
500  return this->CreateVerticalScrollbar(RendName,Style,Parent);
501  }
502 
503  Widget* VerticalScrollbarFactory::CreateWidget(const String& RendName, const UnifiedRect& RendRect, const NameValuePairMap& Params, Screen* Parent)
504  {
505  UI::ScrollbarStyle Style = UI::SB_NoButtons;
506 
507  NameValuePairMap::const_iterator ParamIt;
508  ParamIt = Params.find("ScrollbarStyle");
509  if( ParamIt != Params.end() )
510  Style = static_cast<UI::ScrollbarStyle>( StringTools::ConvertToUInt32( (*ParamIt).second ) );
511 
512  return this->CreateVerticalScrollbar(RendName,RendRect,Style,Parent);
513  }
514 
516  { return this->CreateVerticalScrollbar(XMLNode,Parent); }
517 
519  { delete static_cast<VerticalScrollbar*>( ToBeDestroyed ); }
520  }//UI
521 }//Mezzanine
522 
523 #endif