Spinning Topp Logo BlackTopp Studios
inc
viewport.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 _graphicsviewport_cpp
41 #define _graphicsviewport_cpp
42 
43 #include "Graphics/viewport.h"
44 #include "Graphics/cameraproxy.h"
45 #include "Graphics/gamewindow.h"
46 
47 #include "serialization.h"
48 #include "exception.h"
49 
50 #include <Ogre.h>
51 
52 namespace Mezzanine
53 {
54  namespace Graphics
55  {
56  Viewport::Viewport(CameraProxy* ViewportCamera, const Integer& ZOrder, GameWindow* ParentWindow) :
57  Parent(ParentWindow),
58  ViewportCam(ViewportCamera)
59  {
60  Ogre::Camera* ViewCam = ViewportCamera ? ViewportCamera->_GetGraphicsObject() : NULL;
61  this->OgreViewport = this->Parent->_GetOgreWindowPointer()->addViewport(ViewCam,ZOrder);
62  }
63 
64  Viewport::Viewport(const XML::Node& XMLNode, GameWindow* ParentWindow) :
65  Parent(ParentWindow),
66  ViewportCam(NULL)
67  { this->ProtoDeSerialize(XMLNode); }
68 
70  { this->Parent->_GetOgreWindowPointer()->removeViewport(this->OgreViewport->getZOrder()); }
71 
72  ///////////////////////////////////////////////////////////////////////////////
73  // Camera and parent Management
74 
75  void Viewport::SetCamera(CameraProxy* ViewportCamera)
76  {
77  Ogre::Camera* ViewCam = ViewportCamera ? ViewportCamera->_GetGraphicsObject() : NULL;
78  if( this->ViewportCam && this->ViewportCam != ViewportCamera )
79  this->ViewportCam->CameraVP = NULL;
80 
81  this->OgreViewport->setCamera(ViewCam);
82  this->ViewportCam = ViewportCamera;
83  if( this->ViewportCam )
84  this->ViewportCam->CameraVP = this;
85  }
86 
88  { return this->ViewportCam; }
89 
91  { return this->Parent; }
92 
93  ///////////////////////////////////////////////////////////////////////////////
94  // Utility
95 
97  { return this->OgreViewport->getZOrder(); }
98 
100  {
101  #if OGRE_NO_VIEWPORT_ORIENTATIONMODE != 0
102  return Mezzanine::OM_Degree_0;
103  #else
104  return static_cast<Mezzanine::OrientationMode>( OgreViewport->getOrientationMode() );
105  #endif
106  }
107 
108  ///////////////////////////////////////////////////////////////////////////////
109  // Viewport Metrics Management
110 
111  void Viewport::SetDimensions(const Vector2& Position, const Vector2& Size)
112  { this->SetDimensions(Position.X,Position.Y,Size.X,Size.Y); }
113 
114  void Viewport::SetDimensions(const Real& Left, const Real& Top, const Real& Width, const Real& Height)
115  { this->OgreViewport->setDimensions(Left,Top,Width,Height); }
116 
118  { return Vector2(this->GetLeft(),this->GetTop()); }
119 
121  { return Vector2(this->GetWidth(),this->GetHeight()); }
122 
124  { return this->OgreViewport->getLeft(); }
125 
127  { return this->OgreViewport->getTop(); }
128 
130  { return this->OgreViewport->getWidth(); }
131 
133  { return this->OgreViewport->getHeight(); }
134 
136  { return Vector2(this->GetActualLeft(),this->GetActualTop()); }
137 
139  { return Vector2(this->GetActualWidth(),this->GetActualHeight()); }
140 
142  { return this->OgreViewport->getActualLeft(); }
143 
145  { return this->OgreViewport->getActualTop(); }
146 
148  { return this->OgreViewport->getActualWidth(); }
149 
151  { return this->OgreViewport->getActualHeight(); }
152 
153  ///////////////////////////////////////////////////////////////////////////////
154  // Serialization
155 
156  void Viewport::ProtoSerialize(XML::Node& ParentNode) const
157  {
158  XML::Node ViewNode = ParentNode.AppendChild( Viewport::GetSerializableName() );
159 
160  if( ViewNode.AppendAttribute("Version").SetValue("1") &&
161  ViewNode.AppendAttribute("ZOrder").SetValue( this->GetZOrder() ) )
162  //ViewNode.AppendAttribute("CameraName").SetValue( this->GetCamera()->GetName() ) )
163  {
164  XML::Node PositionNode = ViewNode.AppendChild("Position");
165  this->GetPosition().ProtoSerialize(PositionNode);
166 
167  XML::Node SizeNode = ViewNode.AppendChild("Size");
168  this->GetSize().ProtoSerialize(SizeNode);
169 
170  return;
171  }else{
172  SerializeError("Create XML Attribute Values",Viewport::GetSerializableName(),true);
173  }
174  }
175 
176  void Viewport::ProtoDeSerialize(const XML::Node& SelfRoot)
177  {
178  // We need to perform some cleanup first, because Ogre doesn't let us change the viewport ZOrder.
179  // Start with the camera
180  CameraProxy* CurrCam = this->ViewportCam;
181  this->ViewportCam = NULL;
182  // Now the viewport itself
183  if( this->OgreViewport ) {
184  this->Parent->_GetOgreWindowPointer()->removeViewport( this->GetZOrder() );
185  this->OgreViewport = NULL;
186  }
187 
188  // Now do some deserializing
189  XML::Attribute CurrAttrib;
190  if( SelfRoot.Name() == Viewport::GetSerializableName() ) {
191  if(SelfRoot.GetAttribute("Version").AsInt() == 1) {
192  Integer ViewZOrder = 0;
193  Vector2 TempPos, TempSize;
194 
195  CurrAttrib = SelfRoot.GetAttribute("ZOrder");
196  if( !CurrAttrib.Empty() )
197  ViewZOrder = CurrAttrib.AsInteger();
198 
199  /*CurrAttrib = SelfRoot.GetAttribute("CameraName");
200  if( !CurrAttrib.Empty() )
201  this->SetCamera( CurrAttrib.AsString() );// */
202 
203  XML::Node PositionNode = SelfRoot.GetChild("Position").GetFirstChild();
204  if( !PositionNode.Empty() ) {
205  TempPos.ProtoDeSerialize(PositionNode);
206  }
207 
208  XML::Node SizeNode = SelfRoot.GetChild("Size").GetFirstChild();
209  if( !SizeNode.Empty() ) {
210  TempSize.ProtoDeSerialize(SizeNode);
211  }
212 
213  this->OgreViewport = this->Parent->_GetOgreWindowPointer()->addViewport(NULL,ViewZOrder);
214  this->SetDimensions(TempPos,TempSize);
215  this->SetCamera(CurrCam);
216  }else{
217  MEZZ_EXCEPTION(ExceptionBase::INVALID_VERSION_EXCEPTION,"Incompatible XML Version for " + Viewport::GetSerializableName() + ": Not Version 1.");
218  }
219  }else{
220  MEZZ_EXCEPTION(ExceptionBase::II_IDENTITY_NOT_FOUND_EXCEPTION,Viewport::GetSerializableName() + " was not found in the provided XML node, which was expected.");
221  }
222  }
223 
225  { return "Viewport"; }
226 
227  ///////////////////////////////////////////////////////////////////////////////
228  // Internal Methods
229 
230  Ogre::Viewport* Viewport::_GetOgreViewport() const
231  { return this->OgreViewport; }
232  }//Graphics
233 }//Mezzanine
234 
235 #endif
void SetCamera(CameraProxy *ViewportCamera)
Sets which CameraProxy is bound to this viewport.
Definition: viewport.cpp:75
Attribute AppendAttribute(const Char8 *Name)
Creates an Attribute and puts it at the end of this Nodes attributes.
Whole GetActualTop() const
Gets the top position of the viewport in pixels.
Definition: viewport.cpp:144
A light-weight handle for manipulating attributes in DOM tree.
Definition: attribute.h:74
Vector2 GetPosition() const
Gets the position of this viewport in it's parent GameWindow in relative units.
Definition: viewport.cpp:117
~Viewport()
Class destructor.
Definition: viewport.cpp:69
Real GetTop() const
Gets the relative top position of the viewport.
Definition: viewport.cpp:126
Thrown when the requested identity could not be found.
Definition: exception.h:94
Integer GetZOrder() const
Gets the Zorder assigned to this viewport.
Definition: viewport.cpp:96
Node GetFirstChild() const
Get the first child Node of this Node.
#define MEZZ_EXCEPTION(num, desc)
An easy way to throw exceptions with rich information.
Definition: exception.h:3048
void ProtoSerialize(XML::Node &ParentNode) const
Convert this class to an XML::Node ready for serialization.
Definition: viewport.cpp:156
int Integer
A datatype used to represent any integer close to.
Definition: datatypes.h:154
Thrown when a version is accessed/parsed/required and it cannot work correctly or is missing...
Definition: exception.h:112
Ogre::RenderWindow * _GetOgreWindowPointer()
This will get a pointer to the Ogre RenderWindow.
Definition: gamewindow.cpp:580
bool Empty() const
Is this storing anything at all?
Whole GetActualWidth() const
Gets the width of the viewport in pixels.
Definition: viewport.cpp:147
This implements the exception hiearchy for Mezzanine.
Viewport(CameraProxy *ViewportCamera, const Integer &ZOrder, GameWindow *ParentWindow)
Class constructor.
Definition: viewport.cpp:56
GameWindow * Parent
A pointer to the window that created this viewport.
Definition: viewport.h:73
float Real
A Datatype used to represent a real floating point number.
Definition: datatypes.h:141
The interface for serialization.
bool SetValue(const Char8 *rhs)
Set the value of this.
void ProtoDeSerialize(const XML::Node &SelfRoot)
Take the data stored in an XML Node and overwrite this object with it.
Definition: viewport.cpp:176
Vector2 GetActualPosition() const
Gets the position of this viewport in it's parent GameWindow in pixels.
Definition: viewport.cpp:135
Real Y
Coordinate on the Y vector.
Definition: vector2.h:69
This file contains the declaration for the World proxy wrapping camera functionality.
Real GetLeft() const
Gets the relative left position of the viewport.
Definition: viewport.cpp:123
A light-weight handle for manipulating nodes in DOM tree.
Definition: node.h:89
GameWindow * GetParentWindow() const
Gets the game window this viewport belongs to.
Definition: viewport.cpp:90
Real X
Coordinate on the X vector.
Definition: vector2.h:67
int AsInt(int def=0) const
Attempts to convert the value of the attribute to an int and returns the results. ...
This is used to represent a point on a 2 dimentional area, such as a screen.
Definition: vector2.h:63
bool Empty() const
Is this storing anything at all?
Integer AsInteger(Integer def=0) const
Attempts to convert the value of the attribute to a Integer and returns the results.
Whole GetActualHeight() const
Gets the height of the viewport in pixels.
Definition: viewport.cpp:150
CameraProxy * ViewportCam
A pointer to the camera being used to render this viewport, or NULL if one isn't set.
Definition: viewport.h:76
Vector2 GetSize() const
Gets the size of this viewport relative to it's parent GameWindow.
Definition: viewport.cpp:120
This is the proxy class for placing and manipulating a camera in the scene.
Definition: cameraproxy.h:65
Viewport * CameraVP
This is a pointer to the Viewport this camera is attached to, if any.
Definition: cameraproxy.h:78
CameraProxy * GetCamera() const
Gets the CameraProxy associated with this viewport.
Definition: viewport.cpp:87
Ogre::Viewport * OgreViewport
A pointer to the internal viewport providing this classes functionality.
Definition: viewport.h:70
Whole GetActualLeft() const
Gets the left position of the viewport in pixels.
Definition: viewport.cpp:141
void ProtoSerialize(XML::Node &CurrentRoot) const
Convert this class to an XML::Node ready for serialization.
Definition: vector2.cpp:314
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
static String GetSerializableName()
Get the name of the the XML tag the Renderable class will leave behind as its instances are serialize...
Definition: viewport.cpp:224
OrientationMode
Simple enum for communicating the orientation the UI and Camera have relative to the world it is rend...
Definition: enumerations.h:66
unsigned long Whole
Whole is an unsigned integer, it will be at least 32bits in size.
Definition: datatypes.h:151
Real GetHeight() const
Gets the relative height of the viewport.
Definition: viewport.cpp:132
Real GetWidth() const
Gets the relative width of the viewport.
Definition: viewport.cpp:129
virtual Ogre::Camera * _GetGraphicsObject() const
Accessor for the internal camera.
const Char8 * Name() const
ptrdiff_tGet the name of this Node.
void SerializeError(const String &FailedTo, const String &ClassName, Boole SOrD)
Simply does some string concatenation, then throws an Exception.
Node AppendChild(NodeType Type=NodeElement)
Creates a Node and makes it a child of this one.
Ogre::Viewport * _GetOgreViewport() const
Gets the internal Ogre Viewport.
Definition: viewport.cpp:230
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
void ProtoDeSerialize(const XML::Node &OneNode)
Take the data stored in an XML and overwrite this instance of this object with it.
Definition: vector2.cpp:335
Attribute GetAttribute(const Char8 *Name) const
Attempt to get an Attribute on this Node with a given name.
Vector2 GetActualSize() const
Gets the size of this viewport in pixels.
Definition: viewport.cpp:138
This class is for creating and managing game windows.
Definition: gamewindow.h:63
Mezzanine::OrientationMode GetOrientationMode() const
Gets the current Orientation of the viewport.
Definition: viewport.cpp:99
Node GetChild(const Char8 *Name) const
Attempt to get a child Node with a given name.
void SetDimensions(const Vector2 &Position, const Vector2 &Size)
Sets the position and size of this viewport within the game window.
Definition: viewport.cpp:111