Spinning Topp Logo BlackTopp Studios
inc
nineboxstrategy.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 _uinineboxstrategy_cpp
41 #define _uinineboxstrategy_cpp
42 
43 #include "UI/nineboxstrategy.h"
44 #include "UI/screen.h"
45 #include "UI/widget.h"
46 
47 namespace Mezzanine
48 {
49  namespace UI
50  {
51  ///////////////////////////////////////////////////////////////////////////////
52  // NineBoxStrategy Functors
53 
54  ///////////////////////////////////////////////////////////////////////////////
55  /// @class NineBoxCheck
56  /// @headerfile nineboxstrategy.cpp
57  /// @brief Simple functor for finding which renderable the mouse is hovered over.
58  /// @details This is intended for reverse checks.
59  ///////////////////////////////////////
61  {
62  protected:
63  /// @brief A pointer to the Vector of 9 partitions on the screen to use for spacial checks.
65  public:
66  /// @brief Functor constructor.
67  /// @param StratPartitions A pointer to the Vector of 9 partitions on the screen to use for spacial checks.
69  Partitions(StratPartitions)
70  { }
71  /// @brief Class destructor.
73  { }
74 
75  /// @brief Interface needed for processing child widgets of screens and widgets.
76  /// @param Wid A pointer to the Widget that will be processed.
77  /// @return Returns true if a valid result is found.
79  {
80  for( NineBoxStrategy::PartitionVec::iterator It = Partitions->begin() ; It != Partitions->end() ; ++It )
81  {
82  if( (*It)->PartitionRect.CheckOverlap(Wid->GetRect()) )
83  (*It)->Widgets.push_back(Wid);
84  }
85  return false;
86  }
87  };//NineBoxCheck
88 
89  ///////////////////////////////////////////////////////////////////////////////
90  // NineBoxStrategy Methods
91 
93  {
94  this->Partitions.push_back(new PartitionData(NBP_TopLeft));
95  this->Partitions.push_back(new PartitionData(NBP_TopCenter));
96  this->Partitions.push_back(new PartitionData(NBP_TopRight));
97  this->Partitions.push_back(new PartitionData(NBP_LeftCenter));
98  this->Partitions.push_back(new PartitionData(NBP_Center));
99  this->Partitions.push_back(new PartitionData(NBP_RightCenter));
100  this->Partitions.push_back(new PartitionData(NBP_BottomLeft));
101  this->Partitions.push_back(new PartitionData(NBP_BottomCenter));
102  this->Partitions.push_back(new PartitionData(NBP_BottomRight));
103  }
104 
106  {
107  for( PartitionVec::iterator It = this->Partitions.begin() ; It != this->Partitions.end() ; ++It )
108  {
109  delete (*It);
110  }
111  this->Partitions.clear();
112  }
113 
115  {
116  if( this->ScreenDirty )
117  {
118  Vector2 ParentSize = this->ParentScreen->GetActualSize();
119  for( PartitionVec::iterator It = this->Partitions.begin() ; It != this->Partitions.end() ; ++It )
120  {
121  (*It)->PartitionRect = CalculatePartitionRect((*It)->PartitionID,ParentSize);
122  (*It)->Widgets.clear();
123  }
124 
125  NineBoxCheck Check( &(this->Partitions) );
126  this->ParentScreen->_ProcessAllChildren(&Check);
127 
128  this->ScreenDirty = false;
129  }
130  }
131 
133  {
134  Vector2 ThirdSize(ScreenSize.X / 3,ScreenSize.Y / 3);
135  switch(PartID)
136  {
137  case NBP_TopLeft: return Rect(Vector2(0,0),ThirdSize); break;
138  case NBP_TopCenter: return Rect(Vector2(ThirdSize.X * 1,0),ThirdSize); break;
139  case NBP_TopRight: return Rect(Vector2(ThirdSize.X * 2,0),ThirdSize); break;
140  case NBP_LeftCenter: return Rect(Vector2(0,ThirdSize.Y * 1),ThirdSize); break;
141  case NBP_Center: return Rect(Vector2(ThirdSize.X * 1,ThirdSize.Y * 1),ThirdSize); break;
142  case NBP_RightCenter: return Rect(Vector2(ThirdSize.X * 2,ThirdSize.Y * 1),ThirdSize); break;
143  case NBP_BottomLeft: return Rect(Vector2(0,ThirdSize.Y * 2),ThirdSize); break;
144  case NBP_BottomCenter: return Rect(Vector2(ThirdSize.X * 1,ThirdSize.Y * 2),ThirdSize); break;
145  case NBP_BottomRight: return Rect(Vector2(ThirdSize.X * 2,ThirdSize.Y * 2),ThirdSize); break;
146  default:
147  { MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_EXCEPTION,"Invalid Partition ID used to calculate a Partition Rect."); break; }
148  }
149  }
150 
152  {
153  for( PartitionVec::iterator It = this->Partitions.begin() ; It != this->Partitions.end() ; ++It )
154  {
155  if( (*It)->PartitionRect.IsInside(MousePos) )
156  return (*It);
157  }
158  return NULL;
159  }
160 
162  {
163  this->UpdateCache();
164  PartitionData* HoveredPartition = this->GetHoveredPartition(MousePos);
165  if( HoveredPartition == NULL )
166  return NULL;
167 
168  for( PartitionData::WidgetContainer::reverse_iterator WidIt = HoveredPartition->Widgets.rbegin() ; WidIt != HoveredPartition->Widgets.rend() ; ++WidIt )
169  {
170  // Can it be seen?
171  if( (*WidIt)->GetVisible() && (*WidIt)->GetNumVisibleRenderLayers() ) {
172  // Is it inside?
173  if( (*WidIt)->IsInside(MousePos) ) {
174  // Has mouse picking been disabled?
175  if( !(*WidIt)->GetMousePassthrough() ) {
176  return (*WidIt);
177  }
178  }
179  }
180  }
181  return NULL;
182  }
183  }//UI
184 }//Mezzanine
185 
186 #endif
NineBoxCheck(NineBoxStrategy::PartitionVec *StratPartitions)
Functor constructor.
virtual Rect GetRect() const
Gets this QuadRenderables' Rect.
Rect CalculatePartitionRect(const NineBoxPartition &PartID, const Vector2 &ScreenSize)
Convenience method for calculating the actual size and position of a single partition.
Simple functor for finding which renderable the mouse is hovered over.
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
#define MEZZ_EXCEPTION(num, desc)
An easy way to throw exceptions with rich information.
Definition: exception.h:3048
Boole operator()(Widget *Wid)
Interface needed for processing child widgets of screens and widgets.
This class represents a box shaped area on the screen.
Definition: rect.h:55
Boole _ProcessAllChildren(Callback *CB)
Processes all children of this screen by their zorder.
Definition: screen.h:665
Widget * FindHoveredWidget(const Vector2 &MousePos)
Finds the hovered quad for the parent screen.
NineBoxStrategy()
Class constructor.
Real Y
Coordinate on the Y vector.
Definition: vector2.h:69
Real X
Coordinate on the X vector.
Definition: vector2.h:67
This is used to represent a point on a 2 dimentional area, such as a screen.
Definition: vector2.h:63
This is the base class for all widgets.
Definition: widget.h:126
This is a helper class for storing metadata for partitions.
~NineBoxCheck()
Class destructor.
virtual Vector2 GetActualSize() const
Gets the pixel size of this widget.
NineBoxStrategy::PartitionVec * Partitions
A pointer to the Vector of 9 partitions on the screen to use for spacial checks.
NineBoxPartition
Enum describing the different 9-Box partitions of a screen.
PartitionData * GetHoveredPartition(const Vector2 &MousePos)
Gets the partition that the provided position is in.
Thrown when parameters are checked at runtime and found invalid.
Definition: exception.h:108
bool ScreenDirty
Stores whether or not the parent screen has been updated since the last time this strategys cache was...
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
virtual ~NineBoxStrategy()
Class destructor.
std::vector< PartitionData * > PartitionVec
Basic container type for PartitionData storage by this class.
Screen * ParentScreen
A pointer to the screen this strategy is being used by.
WidgetContainer Widgets
A container storing all the quads that overlap with this screen partition.
void UpdateCache()
Updates the partition cache with the newest quad positions.
PartitionVec Partitions
A container storing all the quads that overlap with this screen partition.