1 | |
---|
2 | #include <keyboardeventhandler.h> |
---|
3 | |
---|
4 | |
---|
5 | KeyboardEventHandler::KeyboardEventHandler( int nTimesteps, float tps) |
---|
6 | { |
---|
7 | _paused = DEF_PAUSED_START; |
---|
8 | _direction = 1; |
---|
9 | _ntimesteps = nTimesteps; |
---|
10 | _tps = tps; |
---|
11 | _tpsorig = tps; |
---|
12 | _timestepchanged = false; |
---|
13 | _timestep = 0; |
---|
14 | _prevtime = 0; |
---|
15 | _togglewireframe = false; |
---|
16 | } |
---|
17 | |
---|
18 | |
---|
19 | bool KeyboardEventHandler::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter&) |
---|
20 | { |
---|
21 | bool handled = false; |
---|
22 | |
---|
23 | if( ea.getEventType() == osgGA::GUIEventAdapter::KEYDOWN ) |
---|
24 | { |
---|
25 | switch( ea.getKey() ) |
---|
26 | { |
---|
27 | case osgGA::GUIEventAdapter::KEY_Space: |
---|
28 | _paused = _paused ? false : true; |
---|
29 | handled = true; |
---|
30 | break; |
---|
31 | |
---|
32 | case osgGA::GUIEventAdapter::KEY_Up: |
---|
33 | if( !_paused ) _tps *= 1.5; |
---|
34 | handled = true; |
---|
35 | break; |
---|
36 | |
---|
37 | case osgGA::GUIEventAdapter::KEY_Down: |
---|
38 | if( !_paused ) _tps /= 1.5; |
---|
39 | handled = true; |
---|
40 | break; |
---|
41 | |
---|
42 | case 'r': |
---|
43 | _paused = DEF_PAUSED_START; |
---|
44 | _tps = _tpsorig; |
---|
45 | _timestep = 0; |
---|
46 | _timestepchanged = true; |
---|
47 | handled = true; |
---|
48 | break; |
---|
49 | |
---|
50 | case osgGA::GUIEventAdapter::KEY_Right: |
---|
51 | if( _paused ) |
---|
52 | { |
---|
53 | _timestep = (_timestep+1) % _ntimesteps; |
---|
54 | _timestepchanged = true; |
---|
55 | } |
---|
56 | else |
---|
57 | _direction = +1; |
---|
58 | handled = true; |
---|
59 | break; |
---|
60 | |
---|
61 | case osgGA::GUIEventAdapter::KEY_Left: |
---|
62 | if( _paused ) |
---|
63 | { |
---|
64 | _timestep = _timestep-1; |
---|
65 | if( _timestep < 0 ) _timestep = _ntimesteps-1; |
---|
66 | _timestepchanged = true; |
---|
67 | } |
---|
68 | else |
---|
69 | _direction = -1; |
---|
70 | handled = true; |
---|
71 | break; |
---|
72 | |
---|
73 | case 'w': |
---|
74 | _togglewireframe = true; |
---|
75 | handled = true; |
---|
76 | break; |
---|
77 | |
---|
78 | } |
---|
79 | |
---|
80 | } |
---|
81 | return handled; |
---|
82 | } |
---|
83 | |
---|
84 | |
---|
85 | void KeyboardEventHandler::setTime( float time) |
---|
86 | { |
---|
87 | if( !isPaused() && time - _prevtime > 1.0/_tps ) |
---|
88 | { |
---|
89 | _prevtime = time; |
---|
90 | _timestep = _timestep + _direction; |
---|
91 | if( _timestep == _ntimesteps ) |
---|
92 | _timestep = 0; |
---|
93 | else if( _timestep < 0 ) |
---|
94 | _timestep = _ntimesteps-1; |
---|
95 | _timestepchanged = true; |
---|
96 | } |
---|
97 | } |
---|
98 | |
---|
99 | |
---|
100 | bool KeyboardEventHandler::timestepChanged() |
---|
101 | { |
---|
102 | if( _timestepchanged ) |
---|
103 | { |
---|
104 | _timestepchanged = false; |
---|
105 | return true; |
---|
106 | } |
---|
107 | return false; |
---|
108 | } |
---|
109 | |
---|
110 | |
---|
111 | bool KeyboardEventHandler::toggleWireframe() |
---|
112 | { |
---|
113 | if( _togglewireframe ) |
---|
114 | { |
---|
115 | _togglewireframe = false; |
---|
116 | return true; |
---|
117 | } |
---|
118 | return false; |
---|
119 | } |
---|
120 | |
---|
121 | |
---|