MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
horizontalscrollbar.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 _uihorizontalscrollbar_cpp
41 #define _uihorizontalscrollbar_cpp
42 
43 #include "UI/horizontalscrollbar.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/horizontallayoutstrategy.h"
50 
51 #include "stringtool.h"
52 #include "mathtool.h"
53 #include "Input/metacode.h"
54 
55 #include <algorithm>
56 
57 namespace Mezzanine
58 {
59  namespace UI
60  {
61  ///////////////////////////////////////////////////////////////////////////////
62  // HorizontalScrollbar Methods
63  ///////////////////////////////////////
64 
65  const String HorizontalScrollbar::TypeName = "HorizontalScrollbar";
66 
68  Scrollbar(Parent)
69  { this->LayoutStrat = new HorizontalLayoutStrategy(); }
70 
72  Scrollbar(RendName,Parent)
73  { this->ConstructHorizontalScrollbar(Style); }
74 
75  HorizontalScrollbar::HorizontalScrollbar(const String& RendName, const UnifiedRect& RendRect, const UI::ScrollbarStyle& Style, Screen* Parent) :
76  Scrollbar(RendName,RendRect,Parent)
77  { this->ConstructHorizontalScrollbar(Style); }
78 
80  Scrollbar(Parent)
81  { this->LayoutStrat = new HorizontalLayoutStrategy(); 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  {
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+".LeftButton",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+".RightButton",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+".LeftButton",ChildRect);
152  this->AddChild(this->UpLeftButton,1);
153 
154  this->DownRightButton = ParentScreen->CreateButton(this->Name+".RightButton",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+".LeftButton",ChildRect);
171  this->AddChild(this->UpLeftButton,0);
172 
173  this->DownRightButton = ParentScreen->CreateButton(this->Name+".RightButton",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().X + this->ScrollBack->GetActualSize().X;
202  }
203 
205  {
206  return this->ScrollBack->GetActualPosition().X;
207  }
208 
210  {
211  if( this->ChildLock == this->Scroller && MouseDelta.X != 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 + MouseDelta.X;
219  NewScrollerRect.Position.Y = OldScrollerRect.Position.Y;
220 
221  NewScrollerRect.Position.X = std::min(NewScrollerRect.Position.X,this->GetUpperScrollLimit() - OldScrollerRect.Size.X);
222  NewScrollerRect.Position.X = std::max(NewScrollerRect.Position.X,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.X < OldScrollerRect.Position.X ) { // Check for above
248  NewScrollerRect.Position.X = ( OldScrollerRect.Position.X - OldScrollerRect.Size.X >= this->GetLowerScrollLimit() ?
249  this->GetLowerScrollLimit() : OldScrollerRect.Position.X - OldScrollerRect.Size.X );
250  NewScrollerRect.Position.Y = OldScrollerRect.Position.Y;
251  }else if( HitPosition.X > OldScrollerRect.Position.X + OldScrollerRect.Size.X ) { // Check for below
252  NewScrollerRect.Position.X = ( OldScrollerRect.Position.X + ( OldScrollerRect.Size.X * 2 ) >= this->GetUpperScrollLimit() ?
253  this->GetUpperScrollLimit() - OldScrollerRect.Size.X : OldScrollerRect.Position.X + OldScrollerRect.Size.X );
254  NewScrollerRect.Position.Y = OldScrollerRect.Position.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().X * this->IncrementDistance);
276  NewScrollerRect.Position.X = OldScrollerRect.Position.X + ScrollDist;
277  NewScrollerRect.Position.Y = OldScrollerRect.Position.Y;
278 
279  NewScrollerRect.Position.X = std::min(NewScrollerRect.Position.X,this->GetUpperScrollLimit() - OldScrollerRect.Size.X);
280  NewScrollerRect.Position.X = std::max(NewScrollerRect.Position.X,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().X * this->IncrementDistance;
295  NewScrollerRect.Position.X = OldScrollerRect.Position.X + ScrollDist;
296  NewScrollerRect.Position.Y = OldScrollerRect.Position.Y;
297 
298  NewScrollerRect.Position.X = std::min(NewScrollerRect.Position.X,this->GetUpperScrollLimit() - OldScrollerRect.Size.X);
299  NewScrollerRect.Position.X = std::max(NewScrollerRect.Position.X,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 + ( ( this->GetScrollRange() - NewScrollerRect.Size.X ) * Value );
331  NewScrollerRect.Position.Y = ScrollBackRect.Position.Y;
332 
333  this->Scroller->UpdateDimensions(OldScrollerRect,NewScrollerRect);
334  }
335 
337  {
338  Real RelPos = this->Scroller->GetActualPosition().X - this->GetLowerScrollLimit();
339  Real RelLimit = this->GetScrollRange() - this->Scroller->GetActualSize().X;
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 * Size;
354  NewScrollerRect.Size.Y = ScrollBackRect.Size.Y;
355  NewScrollerRect.Position.X = ScrollBackRect.Position.X + ( ( this->GetScrollRange() - NewScrollerRect.Size.X ) * ScrollValue );
356  NewScrollerRect.Position.Y = ScrollBackRect.Position.Y;
357 
358  this->Scroller->UpdateDimensions(OldScrollerRect,NewScrollerRect);
359  }
360 
362  {
363  return ( this->Scroller->GetActualSize().X / this->ScrollBack->GetActualSize().X );
364  }
365 
366  void HorizontalScrollbar::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 * ScrollSize;
383  NewScrollerRect.Size.Y = ScrollBackRect.Size.Y;
384  NewScrollerRect.Position.X = ScrollBackRect.Position.X + ( ( this->GetScrollRange() - NewScrollerRect.Size.Y ) * ScrollValue );
385  NewScrollerRect.Position.Y = ScrollBackRect.Position.Y;
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  {
400  return ( Ret > 0 ? Ret : 1 );
401  }
402 
404  { return 1; }
405 
407  { return ( (this->GetMaxXPages() - 1) * this->GetScrollerValue() ) + 1; }
408 
410  { return 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 HorizontalScrollbar 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 HorizontalScrollbar deserialization.");
434  }
435 
436  if( this->GetNumChildren() > 2 )
437  {
438  // Assign the LeftButton
439  this->UpLeftButton = static_cast<Button*>( this->GetChild(this->Name+".LeftButton") );
440  if( this->UpLeftButton == NULL ) {
441  MEZZ_EXCEPTION(Exception::INVALID_STATE_EXCEPTION,"LeftButton button not found after HorizontalScrollbar deserialization.");
442  }
443 
444  // Assign the RightButton
445  this->DownRightButton = static_cast<Button*>( this->GetChild(this->Name+".RightButton") );
446  if( this->DownRightButton == NULL ) {
447  MEZZ_EXCEPTION(Exception::INVALID_STATE_EXCEPTION,"RightButton button not found after HorizontalScrollbar deserialization.");
448  }
449  }
450 
451  this->SubscribeToChildEvents();
452  }
453 
456 
457  ///////////////////////////////////////////////////////////////////////////////
458  // Internal Methods
459 
461  {
462  if( this->Container != NULL ) {
463  Real XView = this->Container->GetActualSize().X;
464  Real XWork = this->Container->GetActualWorkAreaSize().X;
465  if( XWork > 0 ) {
466  this->SetScrollerSize( XView / XWork );
467  }else{
468  this->SetScrollerSize( 1.0 );
469  }
470  }
471  }
472 
473  ///////////////////////////////////////////////////////////////////////////////
474  // HorizontalScrollbarFactory Methods
475 
478 
480  { return new HorizontalScrollbar(RendName,Style,Parent); }
481 
483  { return new HorizontalScrollbar(RendName,RendRect,Style,Parent); }
484 
486  { return new HorizontalScrollbar(XMLNode,Parent); }
487 
489  { return new HorizontalScrollbar(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->CreateHorizontalScrollbar(RendName,Style,Parent);
501  }
502 
503  Widget* HorizontalScrollbarFactory::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->CreateHorizontalScrollbar(RendName,RendRect,Style,Parent);
513  }
514 
516  { return this->CreateHorizontalScrollbar(XMLNode,Parent); }
517 
519  { delete static_cast<HorizontalScrollbar*>( ToBeDestroyed ); }
520  }//UI
521 }//Mezzanine
522 
523 #endif