MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
nineboxstrategy.cpp
1 //© Copyright 2010 - 2012 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.
78  Boolean operator()(Widget* Wid)
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  }
147  }
148 
150  {
151  for( PartitionVec::iterator It = this->Partitions.begin() ; It != this->Partitions.end() ; ++It )
152  {
153  if( (*It)->PartitionRect.IsInside(MousePos) )
154  return (*It);
155  }
156  return NULL;
157  }
158 
160  {
161  this->UpdateCache();
162  PartitionData* HoveredPartition = this->GetHoveredPartition(MousePos);
163  if( HoveredPartition == NULL )
164  return NULL;
165 
166  for( PartitionData::WidgetContainer::reverse_iterator WidIt = HoveredPartition->Widgets.rbegin() ; WidIt != HoveredPartition->Widgets.rend() ; ++WidIt )
167  {
168  // Can it be seen?
169  if( (*WidIt)->IsVisible() && (*WidIt)->GetNumVisibleRenderLayers() ) {
170  // Is it inside?
171  if( (*WidIt)->IsInside(MousePos) ) {
172  // Has mouse picking been disabled?
173  if( !(*WidIt)->GetMousePassthrough() ) {
174  return (*WidIt);
175  }
176  }
177  }
178  }
179  }
180  }//UI
181 }//Mezzanine
182 
183 #endif