Panda3D
 All Classes Functions Variables Enumerations
rocketRegion.cxx
1 // Filename: rocketRegion.cxx
2 // Created by: rdb (30Nov11)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "rocketRegion.h"
16 #include "graphicsOutput.h"
17 #include "orthographicLens.h"
18 #include "pStatTimer.h"
19 
20 #if defined(HAVE_ROCKET_DEBUGGER) && !defined(CPPPARSER)
21 #include <Rocket/Debugger.h>
22 #endif
23 
24 TypeHandle RocketRegion::_type_handle;
25 
26 ////////////////////////////////////////////////////////////////////
27 // Function: RocketRegion::Constructor
28 // Access: Protected
29 // Description: Make sure that context_name is unique.
30 ////////////////////////////////////////////////////////////////////
31 RocketRegion::
32 RocketRegion(GraphicsOutput *window, const LVecBase4 &dr_dimensions,
33  const string &context_name) :
34  DisplayRegion(window, dr_dimensions) {
35 
36  // A hack I don't like. libRocket's decorator system has
37  // a bug somewhere, and this seems to be a workaround.
38  if (Rocket::Core::GetRenderInterface() == NULL) {
39  Rocket::Core::SetRenderInterface(&_interface);
40  }
41 
42  int pl, pr, pb, pt;
43  get_pixels(pl, pr, pb, pt);
44  Rocket::Core::Vector2i dimensions (pr - pl, pt - pb);
45 
46  rocket_cat.debug()
47  << "Setting initial context dimensions to ("
48  << dimensions.x << ", " << dimensions.y << ")\n";
49 
50  _context = Rocket::Core::CreateContext(context_name.c_str(),
51  dimensions, &_interface);
52  nassertv(_context != NULL);
53 
54  _lens = new OrthographicLens;
55  _lens->set_film_size(dimensions.x, -dimensions.y);
56  _lens->set_film_offset(dimensions.x * 0.5, dimensions.y * 0.5);
57  _lens->set_near_far(-1, 1);
58 
59  PT(Camera) cam = new Camera(context_name, _lens);
60  set_camera(NodePath(cam));
61 }
62 
63 ////////////////////////////////////////////////////////////////////
64 // Function: RocketRegion::Destructor
65 // Access: Public, Virtual
66 // Description:
67 ////////////////////////////////////////////////////////////////////
69 ~RocketRegion() {
70  if (Rocket::Core::GetRenderInterface() == &_interface) {
71  Rocket::Core::SetRenderInterface(NULL);
72  }
73 
74  if (_context != NULL) {
75  if (_context->GetReferenceCount() > 1) {
76  _context->RemoveReference();
77  return;
78  }
79 
80  // We need to do this because libRocket may call into Python
81  // code to throw destruction events.
82 #ifdef HAVE_ROCKET_PYTHON
83  PyGILState_STATE gstate;
84  gstate = PyGILState_Ensure();
85 #endif
86 
87  _context->RemoveReference();
88 
89 #ifdef HAVE_ROCKET_PYTHON
90  PyGILState_Release(gstate);
91 #endif
92  }
93 }
94 
95 ////////////////////////////////////////////////////////////////////
96 // Function: RocketRegion::do_cull
97 // Access: Protected, Virtual
98 // Description: Performs a cull traversal for this region.
99 ////////////////////////////////////////////////////////////////////
100 void RocketRegion::
101 do_cull(CullHandler *cull_handler, SceneSetup *scene_setup,
102  GraphicsStateGuardian *gsg, Thread *current_thread) {
103 
104  PStatTimer timer(get_cull_region_pcollector(), current_thread);
105 
106  // We (unfortunately) need to do this because libRocket
107  // may call into Python code to throw events.
108 #ifdef HAVE_ROCKET_PYTHON
109  PyGILState_STATE gstate;
110  gstate = PyGILState_Ensure();
111 #endif
112 
113  int pl, pr, pb, pt;
114  get_pixels(pl, pr, pb, pt);
115  Rocket::Core::Vector2i dimensions (pr - pl, pt - pb);
116 
117  if (_context->GetDimensions() != dimensions) {
118  rocket_cat.debug() << "Setting context dimensions to ("
119  << dimensions.x << ", " << dimensions.y << ")\n";
120 
121  _context->SetDimensions(dimensions);
122 
123  _lens->set_film_size(dimensions.x, -dimensions.y);
124  _lens->set_film_offset(dimensions.x * 0.5, dimensions.y * 0.5);
125  }
126 
127  if (_input_handler != NULL) {
128  _input_handler->update_context(_context, pl, pb);
129  } else {
130  _context->Update();
131  }
132 
133  CullTraverser *trav = get_cull_traverser();
134  trav->set_cull_handler(cull_handler);
135  trav->set_scene(scene_setup, gsg, get_incomplete_render());
136  trav->set_view_frustum(NULL);
137 
138  _interface.render(_context, trav);
139 
140 #ifdef HAVE_ROCKET_PYTHON
141  PyGILState_Release(gstate);
142 #endif
143 
144  trav->end_traverse();
145 }
146 
147 ////////////////////////////////////////////////////////////////////
148 // Function: RocketRegion::init_debugger
149 // Access: Published
150 // Description: Initializes the libRocket debugger. This will
151 // return false if the debugger failed to initialize,
152 // or if support for the debugger has not been built
153 // in (for example in an optimize=4 build).
154 ////////////////////////////////////////////////////////////////////
155 bool RocketRegion::
157 #ifdef HAVE_ROCKET_DEBUGGER
158  return Rocket::Debugger::Initialise(_context);
159 #else
160  return false;
161 #endif
162 }
163 
164 ////////////////////////////////////////////////////////////////////
165 // Function: RocketRegion::set_debugger_visible
166 // Access: Published
167 // Description: Sets whether the debugger should be visible.
168 ////////////////////////////////////////////////////////////////////
169 void RocketRegion::
170 set_debugger_visible(bool visible) {
171 #ifdef HAVE_ROCKET_DEBUGGER
172  Rocket::Debugger::SetVisible(visible);
173 #endif
174 }
175 
176 ////////////////////////////////////////////////////////////////////
177 // Function: RocketRegion::is_debugger_visible
178 // Access: Published
179 // Description: Returns true if the debugger is visible.
180 ////////////////////////////////////////////////////////////////////
181 bool RocketRegion::
183 #ifdef HAVE_ROCKET_DEBUGGER
184  return Rocket::Debugger::IsVisible();
185 #else
186  return false;
187 #endif
188 }
virtual void end_traverse()
Should be called when the traverser has finished traversing its scene, this gives it a chance to do a...
void set_film_size(PN_stdfloat width)
Sets the horizontal size of the film without changing its shape.
Definition: lens.I:226
A lightweight class that can be used to automatically start and stop a PStatCollector around a sectio...
Definition: pStatTimer.h:34
Represents a region in a window or buffer where the libRocket UI will be rendered to...
Definition: rocketRegion.h:30
This defines the abstract interface for an object that receives Geoms identified by the CullTraverser...
Definition: cullHandler.h:31
void set_view_frustum(GeometricBoundingVolume *view_frustum)
Specifies the bounding volume that corresponds to the view frustum.
This is a base class for the various different classes that represent the result of a frame of render...
An orthographic lens.
This is the base class for all three-component vectors and points.
Definition: lvecBase4.h:111
A thread; that is, a lightweight process.
Definition: thread.h:51
Encapsulates all the communication with a particular instance of a given rendering backend...
void set_debugger_visible(bool visible)
Sets whether the debugger should be visible.
A rectangular subregion within a window for rendering into.
Definition: displayRegion.h:61
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
This object holds the camera position, etc., and other general setup information for rendering a part...
Definition: sceneSetup.h:35
A node that can be positioned around in the scene graph to represent a point of view for rendering a ...
Definition: camera.h:37
bool init_debugger()
Initializes the libRocket debugger.
NodePath is the fundamental system for disambiguating instances, and also provides a higher-level int...
Definition: nodePath.h:165
This object performs a depth-first traversal of the scene graph, with optional view-frustum culling...
Definition: cullTraverser.h:48
virtual void set_scene(SceneSetup *scene_setup, GraphicsStateGuardianBase *gsg, bool dr_incomplete_render)
Sets the SceneSetup object that indicates the initial camera position, etc.
bool is_debugger_visible() const
Returns true if the debugger is visible.
void set_cull_handler(CullHandler *cull_handler)
Specifies the object that will receive the culled Geoms.