MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
linelist.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 _uilinelist_cpp
41 #define _uilinelist_cpp
42 
43 #include "UI/linelist.h"
44 #include "UI/uienumerations.h"
45 #include "UI/uimanager.h"
46 #include "UI/screen.h"
47 #include "UI/simplerenderer.h"
48 
49 namespace Mezzanine
50 {
51  namespace UI
52  {
53  ///////////////////////////////////////////////////////////////////////////////
54  // LineListRenderer Methods
55 
57  {
58  protected:
59  LineList* Parent;
60  /// @copydoc SimpleRenderer::RedrawImpl(bool)
61  virtual void RedrawImpl(bool Force)
62  {
63  if(!this->Parent->IsVisible())
64  {
65  this->Dirty = false;
66  return;
67  }
68 
69  const LineList::PointVector& Positions = this->Parent->GetPoints();
70  const ColourValue& Colour = this->Parent->GetLineColour();
71  const Real& Thickness = this->Parent->GetLineThickness();
72  bool IsClosed = this->Parent->IsClosed();
73 
74  if(Positions.size() < 2)
75  return;
76 
77  VertexData Temp;
78  Real HalfThickness = Thickness * 0.5;
79  Vector2 PerpNorm, LastLeft, LastRight, ThisLeft, ThisRight, UV = this->Parent->GetScreen()->GetWhitePixel(PriAtlas);
80  size_t Index = 1;
81  for( ; Index < Positions.size() ; Index++ )
82  {
83  PerpNorm = (Positions[Index] - Positions[Index - 1]).Perpendicular().Normalize();
84  LastLeft = Positions[Index -1 ] - PerpNorm * HalfThickness;
85  LastRight = Positions[Index -1 ] + PerpNorm * HalfThickness;
86  ThisLeft = Positions[Index] - PerpNorm * HalfThickness;
87  ThisRight = Positions[Index] + PerpNorm * HalfThickness;
88 
89  // Triangle A
90  this->PushVertex(LastRight.X,LastRight.Y,UV,Colour,PriAtlas); // Left/Bottom
91  this->PushVertex(ThisLeft.X,ThisLeft.Y,UV,Colour,PriAtlas); // Right/Top
92  this->PushVertex(LastLeft.X,LastLeft.Y,UV,Colour,PriAtlas); // Left/Top
93  // Triangle B
94  this->PushVertex(LastRight.X,LastRight.Y,UV,Colour,PriAtlas); // Left/Bottom
95  this->PushVertex(ThisRight.X,ThisRight.Y,UV,Colour,PriAtlas); // Right/Bottom
96  this->PushVertex(ThisLeft.X,ThisLeft.Y,UV,Colour,PriAtlas); // Right/Top
97  }
98 
99  if(IsClosed)
100  {
101  Index = Positions.size() - 1;
102  PerpNorm = (Positions[0] - Positions[Index]).Perpendicular().Normalize();
103  LastLeft = Positions[Index] - PerpNorm * HalfThickness;
104  LastRight = Positions[Index] + PerpNorm * HalfThickness;
105  ThisLeft = Positions[0] - PerpNorm * HalfThickness;
106  ThisRight = Positions[0] + PerpNorm * HalfThickness;
107 
108  // Triangle A
109  this->PushVertex(LastRight.X,LastRight.Y,UV,Colour,PriAtlas); // Left/Bottom
110  this->PushVertex(ThisLeft.X,ThisLeft.Y,UV,Colour,PriAtlas); // Right/Top
111  this->PushVertex(LastLeft.X,LastLeft.Y,UV,Colour,PriAtlas); // Left/Top
112  // Triangle B
113  this->PushVertex(LastRight.X,LastRight.Y,UV,Colour,PriAtlas); // Left/Bottom
114  this->PushVertex(ThisRight.X,ThisRight.Y,UV,Colour,PriAtlas); // Right/Bottom
115  this->PushVertex(ThisLeft.X,ThisLeft.Y,UV,Colour,PriAtlas); // Right/Top
116  }
117  }
118  public:
119  /// @brief Class constructor.
120  /// @param LL The parent LineList this renderer is rendering.
121  LineListRenderer(LineList* LL) : Parent(LL) {}
122  /// @brief Class destructor.
124 
125  /// @copydoc UI::SimpleRenderer::_MarkDirty()
126  virtual void _MarkDirty()
127  {
128  if(this->Dirty)
129  return;
130  this->Parent->_MarkDirty();
131  this->Dirty = true;
132  }
133  };//LineListRenderer
134 
135  ///////////////////////////////////////////////////////////////////////////////
136  // LineList Methods
137 
138  LineList::LineList(const String& RendName, Screen* PScreen) :
139  Renderable(RendName,PScreen),
140  Renderer(NULL),
141  Thickness(1.0),
142  Closed(false),
143  Priority(UI::RP_Medium)
144  {
145  this->Colour = ColourValue::Black();
146  this->Renderer = new LineListRenderer(this);
147  }
148 
150  {
151  delete this->Renderer;
152  }
153 
154  LineList& LineList::Begin(const Whole& LineThickness, const ColourValue& LineColour)
155  {
156  this->Positions.clear();
157  this->Thickness = LineThickness;
158  this->Colour = LineColour;
159  //_MarkDirty();
160  return *this;
161  }
162 
163  LineList& LineList::AddPoint(const Real& X, const Real& Y)
164  {
165  this->AddPoint(Vector2(X,Y));
166  return *this;
167  }
168 
170  {
171  this->Positions.push_back(Position * this->GetScreen()->GetViewportDimensions());
172  return *this;
173  }
174 
176  {
177  this->AddPoint(Vector2(X,Y));
178  return *this;
179  }
180 
182  {
183  this->Positions.push_back(Position);
184  return *this;
185  }
186 
187  void LineList::End(bool Closed)
188  {
189  this->Closed = Closed;
190  //_MarkDirty();
191  }
192 
193  ///////////////////////////////////////////////////////////////////////////////
194  // Utility Methods
195 
197  {
198  return Renderable::RT_LineList;
199  }
200 
202  {
203  return this->Positions;
204  }
205 
206  bool LineList::IsClosed() const
207  {
208  return this->Closed;
209  }
210 
212  {
213  return this->Colour;
214  }
215 
217  {
218  return this->Thickness;
219  }
220 
221  ///////////////////////////////////////////////////////////////////////////////
222  // Visibility Methods
223 
224  void LineList::SetVisible(bool visible)
225  {
226  if( this->Visible == visible )
227  return;
228  this->Visible = visible;
229  //_MarkDirty();
230  }
231 
232  bool LineList::GetVisible() const
233  {
234  return this->Visible;
235  }
236 
237  bool LineList::IsVisible() const
238  {
239  return (this->Visible && ParentScreen->IsVisible());
240  }
241 
243  {
244  this->SetVisible(true);
245  }
246 
248  {
249  this->SetVisible(false);
250  }
251 
252  ///////////////////////////////////////////////////////////////////////////////
253  // Utility Methods
254 
256  {
257  /*Vector2 OldMid = ViewportUpdateTool::GetOldSize() * 0.5;
258  Vector2 NewMid = ViewportUpdateTool::GetNewSize() * 0.5;
259 
260  for( Whole Index = 0 ; Index < Positions.size() ; Index++ )
261  {
262  Positions[Index] = (Positions[Index] - OldMid) + NewMid;
263  }//*/
264  //_MarkDirty();
265  }
266 
267  ///////////////////////////////////////////////////////////////////////////////
268  // Internal Methods
269 
271  {
272  this->Dirty = true;
273  }
274 
276  {
277  if( this->Renderer->_IsDirty() )
278  this->Renderer->_Redraw(false);
279  switch(Priority)
280  {
281  case UI::RP_Low:
282  {
283  this->Renderer->_AppendVertices(RenderData.LowVertices);
284  break;
285  }
286  case UI::RP_Medium:
287  {
288  this->Renderer->_AppendVertices(RenderData.MediumVertices);
289  break;
290  }
291  case UI::RP_High:
292  {
293  this->Renderer->_AppendVertices(RenderData.HighVertices);
294  break;
295  }
296  }
297  }
298  }//UI
299 }//Mezzanine
300 
301 #endif