Equalizer  1.6.1
eVolve/config.cpp
1 
2 /* Copyright (c) 2006-2013, Stefan Eilemann <eile@equalizergraphics.com>
3  * 2007-2011, Maxim Makhinya <maxmah@gmail.com>
4  * 2012, Daniel Nachbaur <danielnachbaur@gmail.com>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * - Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  * - Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  * - Neither the name of Eyescale Software GmbH nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "config.h"
32 
33 
34 namespace eVolve
35 {
36 
37 Config::Config( eq::ServerPtr parent )
38  : eq::Config( parent )
39  , _spinX( 5 )
40  , _spinY( 5 )
41  , _currentCanvas( 0 )
42  , _messageTime( 0 ){}
43 
45 
47 {
48  // init distributed objects
49  LBCHECK( registerObject( &_frameData ));
50 
51  _frameData.setOrtho( _initData.getOrtho( ));
52  _initData.setFrameDataID( _frameData.getID( ));
53 
54  _frameData.setAutoObsolete( getLatency( ));
55 
56  LBCHECK( registerObject( &_initData ));
57 
58  // init config
59  if( !eq::Config::init( _initData.getID( )))
60  {
61  _deregisterData();
62  return false;
63  }
64 
65  const eq::Canvases& canvases = getCanvases();
66  if( canvases.empty( ))
67  _currentCanvas = 0;
68  else
69  _currentCanvas = canvases.front();
70 
71  _setMessage( "Welcome to eVolve\nPress F1 for help" );
72 
73  return true;
74 }
75 
76 bool Config::mapData( const eq::uint128_t& initDataID )
77 {
78  if( !_initData.isAttached() )
79  {
80  if( !mapObject( &_initData, initDataID ))
81  return false;
82  unmapObject( &_initData ); // data was retrieved, unmap immediately
83  }
84  else // appNode, _initData is registered already
85  {
86  LBASSERT( _initData.getID() == initDataID );
87  }
88  return true;
89 }
90 
92 {
93  const bool ret = eq::Config::exit();
94  _deregisterData();
95 
96  return ret;
97 }
98 
99 void Config::_deregisterData()
100 {
101  deregisterObject( &_initData );
102  deregisterObject( &_frameData );
103 
104  _initData.setFrameDataID( co::UUID( ));
105 }
106 
107 
109 {
110  // update database
111  _frameData.spinCamera( -0.001f * _spinX, -0.001f * _spinY );
112  const lunchbox::uint128_t& version = _frameData.commit();
113 
114  _resetMessage();
115 
116  return eq::Config::startFrame( version );
117 }
118 
119 void Config::_resetMessage()
120 {
121  // reset message after two seconds
122  if( _messageTime > 0 && getTime() - _messageTime > 2000 )
123  {
124  _messageTime = 0;
125  _frameData.setMessage( "" );
126  }
127 }
128 
130 {
131  switch( event->data.type )
132  {
134  if( _handleKeyEvent( event->data.keyPress ))
135  return true;
136  break;
137 
139  {
140  const lunchbox::UUID& viewID = event->data.context.view.identifier;
141  _frameData.setCurrentViewID( viewID );
142  if( viewID == 0 )
143  {
144  _currentCanvas = 0;
145  return true;
146  }
147 
148  const eq::View* view = find< eq::View >( viewID );
149  const eq::Layout* layout = view->getLayout();
150  const eq::Canvases& canvases = getCanvases();
151  for( eq::Canvases::const_iterator i = canvases.begin();
152  i != canvases.end(); ++i )
153  {
154  eq::Canvas* canvas = *i;
155  const eq::Layout* canvasLayout = canvas->getActiveLayout();
156 
157  if( canvasLayout == layout )
158  {
159  _currentCanvas = canvas;
160  return true;
161  }
162  }
163  return true;
164  }
165 
167  if( event->data.pointerButtonRelease.buttons == eq::PTR_BUTTON_NONE
168  && event->data.pointerButtonRelease.button == eq::PTR_BUTTON1 )
169  {
170  _spinY = event->data.pointerButtonRelease.dx;
171  _spinX = event->data.pointerButtonRelease.dy;
172  }
173  return true;
174 
175  case eq::Event::CHANNEL_POINTER_MOTION:
176  if( event->data.pointerMotion.buttons == eq::PTR_BUTTON1 )
177  {
178  _spinX = 0;
179  _spinY = 0;
180 
181  _frameData.spinCamera( -0.005f * event->data.pointerMotion.dy,
182  -0.005f * event->data.pointerMotion.dx);
183  return true;
184  }
185  if( event->data.pointerMotion.buttons == eq::PTR_BUTTON2 ||
186  event->data.pointerMotion.buttons == ( eq::PTR_BUTTON1 |
187  eq::PTR_BUTTON3 ))
188  {
189  _frameData.moveCamera( .0, .0,
190  .005f*event->data.pointerMotion.dy );
191  return true;
192  }
193  if( event->data.pointerMotion.buttons == eq::PTR_BUTTON3 )
194  {
195  _frameData.moveCamera( .0005f * event->data.pointerMotion.dx,
196  -.0005f * event->data.pointerMotion.dy,
197  .0f );
198  return true;
199  }
200  break;
201 
202  default:
203  break;
204  }
205  return eq::Config::handleEvent( event );
206 }
207 
208 bool Config::_handleKeyEvent( const eq::KeyEvent& event )
209 {
210  switch( event.key )
211  {
212  case 'b':
213  case 'B':
214  _frameData.toggleBackground();
215  return true;
216 
217  case 'd':
218  case 'D':
219  _frameData.toggleColorMode();
220  return true;
221 
222  case eq::KC_F1:
223  case 'h':
224  case 'H':
225  _frameData.toggleHelp();
226  return true;
227 
228  case 'r':
229  case 'R':
230  case ' ':
231  _spinX = 0;
232  _spinY = 0;
233  _frameData.reset();
234  return true;
235 
236  case 'n':
237  case 'N':
238  _frameData.toggleNormalsQuality();
239  return true;
240 
241  case 'o':
242  case 'O':
243  _frameData.toggleOrtho();
244  return true;
245 
246  case 's':
247  case 'S':
248  _frameData.toggleStatistics();
249  return true;
250 
251  case 'l':
252  _switchLayout( 1 );
253  return true;
254  case 'L':
255  _switchLayout( -1 );
256  return true;
257 
258  case 'q':
259  _frameData.adjustQuality( -.1f );
260  return true;
261 
262  case 'Q':
263  _frameData.adjustQuality( .1f );
264  return true;
265 
266  case 'c':
267  case 'C':
268  {
269  const eq::Canvases& canvases = getCanvases();
270  if( canvases.empty( ))
271  return true;
272 
273  _frameData.setCurrentViewID( eq::uint128_t( 0 ));
274 
275  if( !_currentCanvas )
276  {
277  _currentCanvas = canvases.front();
278  return true;
279  }
280 
281  eq::Canvases::const_iterator i = std::find( canvases.begin(),
282  canvases.end(),
283  _currentCanvas );
284  LBASSERT( i != canvases.end( ));
285 
286  ++i;
287  if( i == canvases.end( ))
288  _currentCanvas = canvases.front();
289  else
290  _currentCanvas = *i;
291  return true;
292  }
293 
294  case 'v':
295  case 'V':
296  {
297  const eq::Canvases& canvases = getCanvases();
298  if( !_currentCanvas && !canvases.empty( ))
299  _currentCanvas = canvases.front();
300 
301  if( !_currentCanvas )
302  return true;
303 
304  const eq::Layout* layout = _currentCanvas->getActiveLayout();
305  if( !layout )
306  return true;
307 
308  const eq::View* current =
309  find< eq::View >( _frameData.getCurrentViewID( ));
310 
311  const eq::Views& views = layout->getViews();
312  LBASSERT( !views.empty( ))
313 
314  if( !current )
315  {
316  _frameData.setCurrentViewID( views.front()->getID( ));
317  return true;
318  }
319 
320  eq::Views::const_iterator i = std::find( views.begin(),
321  views.end(),
322  current );
323  LBASSERT( i != views.end( ));
324 
325  ++i;
326  if( i == views.end( ))
327  _frameData.setCurrentViewID( eq::uint128_t( 0 ));
328  else
329  _frameData.setCurrentViewID( (*i)->getID( ));
330  return true;
331  }
332 
333  default:
334  break;
335  }
336  return false;
337 }
338 
339 void Config::_setMessage( const std::string& message )
340 {
341  _frameData.setMessage( message );
342  _messageTime = getTime();
343 }
344 
345 void Config::_switchLayout( int32_t increment )
346 {
347  if( !_currentCanvas )
348  return;
349 
350  _frameData.setCurrentViewID( eq::uint128_t( 0 ));
351 
352  size_t index = _currentCanvas->getActiveLayoutIndex() + increment;
353  const eq::Layouts& layouts = _currentCanvas->getLayouts();
354  LBASSERT( !layouts.empty( ));
355 
356  index = ( index % layouts.size( ));
357  _currentCanvas->useLayout( uint32_t( index ));
358 
359  const eq::Layout* layout = _currentCanvas->getActiveLayout();
360  std::ostringstream stream;
361  stream << "Layout ";
362  if( layout )
363  {
364  const std::string& name = layout->getName();
365  if( name.empty( ))
366  stream << index;
367  else
368  stream << name;
369  }
370  else
371  stream << "NONE";
372 
373  stream << " active";
374  _setMessage( stream.str( ));
375 }
376 
377 }
virtual uint32_t startFrame()
int32_t dx
X position change since last event.
Definition: event.h:119
virtual ~Config()
Destruct a config.
virtual bool init(const uint128_t &initID)
Initialize this configuration.
A View is a 2D area of a Layout.
int64_t getTime() const
Get the current time in milliseconds.
int32_t dy
Y position change since last event.
Definition: event.h:120
A layout groups one or more View, logically belonging together.
Definition: client/layout.h:44
const Layouts & getLayouts() const
Definition: fabric/canvas.h:69
A canvas represents a logical 2D projection surface.
Definition: client/canvas.h:51
virtual void unmapObject(co::Object *object)
Unmap a mapped object.
std::vector< Layout * > Layouts
A vector of pointers to eq::Layout.
const Views & getViews() const
Get the list of views.
Definition: fabric/layout.h:47
lunchbox::RefPtr< Server > ServerPtr
A reference-counted pointer to an eq::Server.
virtual void deregisterObject(co::Object *object)
Deregister a distributed object.
virtual bool handleEvent(const eq::ConfigEvent *event)
Key press data in keyPress.
Definition: event.h:223
virtual bool handleEvent(const ConfigEvent *event)
Handle one (old) config event.
virtual bool mapObject(co::Object *object, const UUID &id, const uint128_t &version=co::VERSION_OLDEST)
Map a distributed object.
virtual EQFABRIC_INL bool useLayout(const uint32_t index)
Activate the given layout on this canvas.
virtual uint32_t startFrame(const uint128_t &frameID)
Request a new frame of rendering.
std::vector< View * > Views
A vector of pointers to eq::View.
uint32_t buttons
current state of all buttons
Definition: event.h:121
std::vector< Canvas * > Canvases
A vector of pointers to eq::Canvas.
Channel pointer button press data in pointerButtonPress.
Definition: event.h:204
uint32_t type
The event type.
Definition: event.h:246
KeyEvent keyPress
Key press event data.
Definition: event.h:271
virtual bool registerObject(co::Object *object)
Register a distributed object.
virtual bool init()
uint32_t getActiveLayoutIndex() const
Definition: fabric/canvas.h:54
EQFABRIC_INL const L * getActiveLayout() const
uint32_t button
fired button
Definition: event.h:122
Channel pointer button release data in pointerButtonRelease.
Definition: event.h:206
PointerEvent pointerMotion
Pointer motion data.
Definition: event.h:265
PointerEvent pointerButtonRelease
Mouse button release data.
Definition: event.h:267
virtual bool exit()
Exit this configuration.
const std::string & getName() const
Event for a key press or release.
Definition: event.h:128
uint32_t key
KeyCode for special keys, ascii code otherwise.
Definition: event.h:130
virtual bool exit()