1 | |
---|
2 | #include <keyboardeventhandler.h> |
---|
3 | #include <iostream> |
---|
4 | |
---|
5 | |
---|
6 | KeyboardEventHandler::KeyboardEventHandler( int nTimesteps, float tps) |
---|
7 | { |
---|
8 | _paused = DEF_PAUSED_START; |
---|
9 | _direction = 1; |
---|
10 | _ntimesteps = nTimesteps; |
---|
11 | _tps = tps; |
---|
12 | _tpsorig = tps; |
---|
13 | _timestepchanged = false; |
---|
14 | _timestep = 0; |
---|
15 | _prevtime = 0; |
---|
16 | _togglewireframe = false; |
---|
17 | _toggleculling = false; |
---|
18 | _togglerecording = false; |
---|
19 | _toggleplayback = false; |
---|
20 | _togglesave = false; |
---|
21 | } |
---|
22 | |
---|
23 | |
---|
24 | bool KeyboardEventHandler::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter&) |
---|
25 | { |
---|
26 | bool handled = false; |
---|
27 | |
---|
28 | if( ea.getEventType() == osgGA::GUIEventAdapter::KEYDOWN ) |
---|
29 | { |
---|
30 | switch( ea.getKey() ) |
---|
31 | { |
---|
32 | case osgGA::GUIEventAdapter::KEY_Space: |
---|
33 | _paused = _paused ? false : true; |
---|
34 | handled = true; |
---|
35 | break; |
---|
36 | |
---|
37 | case osgGA::GUIEventAdapter::KEY_Up: |
---|
38 | if( !_paused ) _tps *= 1.5; |
---|
39 | handled = true; |
---|
40 | break; |
---|
41 | |
---|
42 | case osgGA::GUIEventAdapter::KEY_Down: |
---|
43 | if( !_paused ) _tps /= 1.5; |
---|
44 | handled = true; |
---|
45 | break; |
---|
46 | |
---|
47 | case 'r': |
---|
48 | _paused = DEF_PAUSED_START; |
---|
49 | _tps = _tpsorig; |
---|
50 | _timestep = 0; |
---|
51 | _timestepchanged = true; |
---|
52 | handled = true; |
---|
53 | break; |
---|
54 | |
---|
55 | case osgGA::GUIEventAdapter::KEY_Right: |
---|
56 | if( _paused ) |
---|
57 | { |
---|
58 | _timestep = (_timestep+1) % _ntimesteps; |
---|
59 | _timestepchanged = true; |
---|
60 | } |
---|
61 | else |
---|
62 | _direction = +1; |
---|
63 | handled = true; |
---|
64 | break; |
---|
65 | |
---|
66 | case osgGA::GUIEventAdapter::KEY_Left: |
---|
67 | if( _paused ) |
---|
68 | { |
---|
69 | _timestep = _timestep-1; |
---|
70 | if( _timestep < 0 ) _timestep = _ntimesteps-1; |
---|
71 | _timestepchanged = true; |
---|
72 | } |
---|
73 | else |
---|
74 | _direction = -1; |
---|
75 | handled = true; |
---|
76 | break; |
---|
77 | |
---|
78 | case 'w': |
---|
79 | _togglewireframe = true; |
---|
80 | handled = true; |
---|
81 | break; |
---|
82 | |
---|
83 | case 'c': |
---|
84 | _toggleculling = true; |
---|
85 | handled = true; |
---|
86 | break; |
---|
87 | |
---|
88 | |
---|
89 | case '1': |
---|
90 | _togglerecording = true; |
---|
91 | handled = true; |
---|
92 | break; |
---|
93 | |
---|
94 | case '2': |
---|
95 | _toggleplayback = true; |
---|
96 | handled = true; |
---|
97 | break; |
---|
98 | |
---|
99 | case '3': |
---|
100 | _togglesave = true; |
---|
101 | handled = true; |
---|
102 | break; |
---|
103 | |
---|
104 | } |
---|
105 | |
---|
106 | } |
---|
107 | return handled; |
---|
108 | } |
---|
109 | |
---|
110 | |
---|
111 | void KeyboardEventHandler::setTime( float time) |
---|
112 | { |
---|
113 | //std::cout << "setTime: " << time << std::endl; |
---|
114 | //std::cout << "setTime: " << _tps << std::endl; |
---|
115 | if( !isPaused() && time - _prevtime > 1.0/_tps ) |
---|
116 | { |
---|
117 | _prevtime = time; |
---|
118 | _timestep = _timestep + _direction; |
---|
119 | if( _timestep == _ntimesteps ) |
---|
120 | _timestep = 0; |
---|
121 | else if( _timestep < 0 ) |
---|
122 | _timestep = _ntimesteps-1; |
---|
123 | _timestepchanged = true; |
---|
124 | } |
---|
125 | } |
---|
126 | |
---|
127 | |
---|
128 | bool KeyboardEventHandler::timestepChanged() |
---|
129 | { |
---|
130 | if( _timestepchanged ) |
---|
131 | { |
---|
132 | _timestepchanged = false; |
---|
133 | return true; |
---|
134 | } |
---|
135 | return false; |
---|
136 | } |
---|
137 | |
---|
138 | |
---|
139 | bool KeyboardEventHandler::toggleWireframe() |
---|
140 | { |
---|
141 | if( _togglewireframe ) |
---|
142 | { |
---|
143 | _togglewireframe = false; |
---|
144 | return true; |
---|
145 | } |
---|
146 | return false; |
---|
147 | } |
---|
148 | |
---|
149 | |
---|
150 | bool KeyboardEventHandler::toggleCulling() |
---|
151 | { |
---|
152 | if( _toggleculling ) |
---|
153 | { |
---|
154 | _toggleculling = false; |
---|
155 | return true; |
---|
156 | } |
---|
157 | return false; |
---|
158 | } |
---|
159 | |
---|
160 | |
---|
161 | bool KeyboardEventHandler::toggleRecording() |
---|
162 | { |
---|
163 | if( _togglerecording ) |
---|
164 | { |
---|
165 | _togglerecording = false; |
---|
166 | return true; |
---|
167 | } |
---|
168 | return false; |
---|
169 | } |
---|
170 | |
---|
171 | |
---|
172 | bool KeyboardEventHandler::togglePlayback() |
---|
173 | { |
---|
174 | if( _toggleplayback ) |
---|
175 | { |
---|
176 | _toggleplayback = false; |
---|
177 | return true; |
---|
178 | } |
---|
179 | return false; |
---|
180 | } |
---|
181 | |
---|
182 | |
---|
183 | bool KeyboardEventHandler::toggleSave() |
---|
184 | { |
---|
185 | if( _togglesave ) |
---|
186 | { |
---|
187 | _togglesave = false; |
---|
188 | return true; |
---|
189 | } |
---|
190 | return false; |
---|
191 | } |
---|