Menge
Modular Pedestrian Simulation Framework for Research and Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Context.h
Go to the documentation of this file.
1 /*
2 
3 License
4 
5 Menge
6 Copyright © and trademark ™ 2012-14 University of North Carolina at Chapel Hill.
7 All rights reserved.
8 
9 Permission to use, copy, modify, and distribute this software and its documentation
10 for educational, research, and non-profit purposes, without fee, and without a
11 written agreement is hereby granted, provided that the above copyright notice,
12 this paragraph, and the following four paragraphs appear in all copies.
13 
14 This software program and documentation are copyrighted by the University of North
15 Carolina at Chapel Hill. The software program and documentation are supplied "as is,"
16 without any accompanying services from the University of North Carolina at Chapel
17 Hill or the authors. The University of North Carolina at Chapel Hill and the
18 authors do not warrant that the operation of the program will be uninterrupted
19 or error-free. The end-user understands that the program was developed for research
20 purposes and is advised not to rely exclusively on the program for any reason.
21 
22 IN NO EVENT SHALL THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL OR THE AUTHORS
23 BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
24 DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
25 DOCUMENTATION, EVEN IF THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL OR THE
26 AUTHORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL AND THE AUTHORS SPECIFICALLY
29 DISCLAIM ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
30 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE AND ANY STATUTORY WARRANTY
31 OF NON-INFRINGEMENT. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND
32 THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL AND THE AUTHORS HAVE NO OBLIGATIONS
33 TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
34 
35 Any questions or comments should be sent to the authors {menge,geom}@cs.unc.edu
36 
37 */
38 
44 #ifndef __CONTEXT_H__
45 #define __CONTEXT_H__
46 
47 #include "CoreConfig.h"
48 //#include "SDL/SDL.h"
49 #include "GLCamera.h"
50 #include "TextWriter.h"
51 
52 // forward declaration
53 union SDL_Event;
54 
58 typedef SDL_Event SDL_Event;
59 
60 namespace Menge {
61 
66  namespace SceneGraph {
67  class Context;
68  class GLScene;
69 
70  // TODO: decouple this from SDL events
74  class MENGE_API ContextResult {
75  public:
83  ContextResult( bool handled, bool needsRedraw ): _handled(handled), _redraw(needsRedraw){}
84 
91  ContextResult & operator=( const ContextResult & res ) { _handled=res._handled; _redraw=res._redraw; return *this; }
92 
98  inline void setHandled( bool state ) { _handled = state; }
99 
105  inline bool isHandled() const { return _handled; }
106 
112  inline void setNeedsRedraw( bool state ) { _redraw = state; }
113 
119  inline bool needsRedraw() const { return _redraw; }
120 
127  inline void set( bool handled, bool redraw ) { _handled = handled; _redraw = redraw; }
128 
134  inline void combine( const ContextResult & res ) { _handled = _handled || res._handled; _redraw = _redraw || res._redraw; }
135 
136  friend class Context;
137  protected:
141  bool _handled;
142 
146  bool _redraw;
147  };
148 
156  class MENGE_API Context {
157  public:
162 
166  virtual ~Context(){}
167 
174  virtual void drawGL( int vWidth, int vHeight ){}
175 
188  virtual bool selectGL( const GLScene * scene, const GLCamera & camera, int vWidth, int vHeight, int * selectPoint ) { return false; }
189 
198  virtual ContextResult handleMouse( SDL_Event & e ) { return ContextResult( false, false ); }
199 
208  virtual ContextResult handleKeyboard( SDL_Event & e ) { return ContextResult( false, false ); }
209 
214  virtual void update() {}
215 
219  virtual void newGLContext() {}
220 
224  virtual void activate() {}
225 
229  virtual void deactivate() {}
230 
231  protected:
239  void uiSetup( int vWidth, int vHeight );
240 
245  void uiShutdown();
246 
255  virtual void drawUIGL( int vWidth, int vHeight, bool select=false ) {};
256 
263  virtual void draw3DGL( bool select=false ) {};
264 
269  void getOpenGLView();
270 
278  void writeText( const std::string & txt, const Vector2 & pos, bool currColor=false ) const;
279 
287  void writeText( const std::string & txt, const Vector3 & pos, bool currColor=false ) const;
288 
300  void writeTextRadially( const std::string & txt, const Vector2 & pos, const Vector2 & dir, bool currColor=false ) const;
301 
310  void writeAlignedText( const std::string & txt, const Vector2 & pos, TextWriter::Alignment align, bool currColor=false ) const;
311 
330  void writeToScreen( const std::string & txt, TextWriter::Alignment align, int fontSize, float hPad=0.f, float vPad=0.f, bool currColor=false, bool trans=true ) const;
331 
335  double _modViewMat[16];
336 
340  double _projMat[16];
341 
345  int _viewMat[4];
346  };
347 
352  class MENGE_API SelectContext : public Context {
353  public:
366  virtual bool selectGL( const GLScene * scene, const GLCamera & camera, int vWidth, int vHeight, int * selectPoint );
367  };
368 
369  } // namespace SceneGraph
370 } // namespace Menge
371 #endif // __CONTEXT_H__
virtual void update()
Allow the context to update any time-dependent state it might have to the given global time...
Definition: Context.h:214
Context()
Constructor.
Definition: Context.h:161
SDL_Event SDL_Event
Forward declaration of the SDL event type.
Definition: Context.h:53
The core namespace. All elements of Menge are contained in this namespace.
Definition: AgentGenerator.cpp:43
bool _handled
Reports if the event has been handled (and no one else needs to worry about it).
Definition: Context.h:141
Sets up the proper compiler directives for platform and dll export/import.
Alignment
Enumeration for controlling text alignment.
Definition: TextWriter.h:98
bool _redraw
Reports if the event requires a redraw on the scene.
Definition: Context.h:146
virtual void newGLContext()
Callback for when the OpenGL context is changed.
Definition: Context.h:219
The base context class for defining a how events are handled. It also is responsible drawing UI eleme...
Definition: Context.h:156
bool needsRedraw() const
Reports if the result believes the event handling requires a redraw.
Definition: Context.h:119
A context that performs object selection by directly clicking on selectable entities in the OpenGL co...
Definition: Context.h:352
void setNeedsRedraw(bool state)
Sets the "needs redraw" state to the given boolean state.
Definition: Context.h:112
The SceneGraph (SceneGraph) name space, containing all elements to use in a SceneGraph.
Functionality for writing text on the OpenGL context.
virtual void activate()
Called when the context is activated.
Definition: Context.h:224
bool isHandled() const
Reports if the result considers the event handled.
Definition: Context.h:105
virtual ~Context()
Virtual destructor.
Definition: Context.h:166
virtual void drawUIGL(int vWidth, int vHeight, bool select=false)
Draw UI elements into the context.
Definition: Context.h:255
void combine(const ContextResult &res)
Combines the provided context result with this one; flags are combined with a boolean OR...
Definition: Context.h:134
virtual bool selectGL(const GLScene *scene, const GLCamera &camera, int vWidth, int vHeight, int *selectPoint)
Performs selection based on a click on screen space. Uses the OpenGL selection mechanism.
Definition: Context.h:188
void setHandled(bool state)
Sets the "handled" state to the given boolean state.
Definition: Context.h:98
The class which contains the entire drawable scene.
Definition: GLScene.h:72
ContextResult & operator=(const ContextResult &res)
Simple assignment operator overloaded.
Definition: Context.h:91
void set(bool handled, bool redraw)
Sets the handled and needs redraw state simultaneously.
Definition: Context.h:127
Defines the result of the context's consideration of user input.
Definition: Context.h:74
virtual ContextResult handleKeyboard(SDL_Event &e)
Give the context the opportunity to respond to a keyboard event.
Definition: Context.h:208
The class for controlling the view camera: it's position, orientation field of view, projection matrix, etc.
Definition: GLCamera.h:57
virtual void draw3DGL(bool select=false)
Draw context elements into the 3D world.
Definition: Context.h:263
virtual void deactivate()
Called when the context is deactivated.
Definition: Context.h:229
virtual ContextResult handleMouse(SDL_Event &e)
Give the context the opportunity to respond to a mouse event.
Definition: Context.h:198
ContextResult(bool handled, bool needsRedraw)
Constructor.
Definition: Context.h:83
virtual void drawGL(int vWidth, int vHeight)
The draw function for the context.
Definition: Context.h:174
Class to handle camera transformations in OpenGL.