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 | _togglemanipulatormode = false; |
---|
17 | } |
---|
18 | |
---|
19 | |
---|
20 | bool KeyboardEventHandler::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter&) |
---|
21 | { |
---|
22 | bool handled = false; |
---|
23 | |
---|
24 | if( ea.getEventType() == osgGA::GUIEventAdapter::KEYDOWN ) |
---|
25 | { |
---|
26 | switch( ea.getKey() ) |
---|
27 | { |
---|
28 | case osgGA::GUIEventAdapter::KEY_Space: |
---|
29 | _paused = _paused ? false : true; |
---|
30 | handled = true; |
---|
31 | break; |
---|
32 | |
---|
33 | case osgGA::GUIEventAdapter::KEY_Up: |
---|
34 | if( !_paused ) _tps *= 1.5; |
---|
35 | handled = true; |
---|
36 | break; |
---|
37 | |
---|
38 | case osgGA::GUIEventAdapter::KEY_Down: |
---|
39 | if( !_paused ) _tps /= 1.5; |
---|
40 | handled = true; |
---|
41 | break; |
---|
42 | |
---|
43 | case 'r': |
---|
44 | _paused = DEF_PAUSED_START; |
---|
45 | _tps = _tpsorig; |
---|
46 | _timestep = 0; |
---|
47 | _timestepchanged = true; |
---|
48 | handled = true; |
---|
49 | break; |
---|
50 | |
---|
51 | case osgGA::GUIEventAdapter::KEY_Right: |
---|
52 | if( _paused ) |
---|
53 | { |
---|
54 | _timestep = (_timestep+1) % _ntimesteps; |
---|
55 | _timestepchanged = true; |
---|
56 | } |
---|
57 | else |
---|
58 | _direction = +1; |
---|
59 | handled = true; |
---|
60 | break; |
---|
61 | |
---|
62 | case osgGA::GUIEventAdapter::KEY_Left: |
---|
63 | if( _paused ) |
---|
64 | { |
---|
65 | _timestep = _timestep-1; |
---|
66 | if( _timestep < 0 ) _timestep = _ntimesteps-1; |
---|
67 | _timestepchanged = true; |
---|
68 | } |
---|
69 | else |
---|
70 | _direction = -1; |
---|
71 | handled = true; |
---|
72 | break; |
---|
73 | |
---|
74 | case 'w': |
---|
75 | _togglewireframe = true; |
---|
76 | handled = true; |
---|
77 | break; |
---|
78 | |
---|
79 | case 'm': |
---|
80 | _togglemanipulatormode = true; |
---|
81 | handled = true; |
---|
82 | break; |
---|
83 | |
---|
84 | } |
---|
85 | |
---|
86 | } |
---|
87 | return handled; |
---|
88 | } |
---|
89 | |
---|
90 | |
---|
91 | void KeyboardEventHandler::setTime( float time) |
---|
92 | { |
---|
93 | if( !isPaused() && time - _prevtime > 1.0/_tps ) |
---|
94 | { |
---|
95 | _prevtime = time; |
---|
96 | _timestep = _timestep + _direction; |
---|
97 | if( _timestep == _ntimesteps ) |
---|
98 | _timestep = 0; |
---|
99 | else if( _timestep < 0 ) |
---|
100 | _timestep = _ntimesteps-1; |
---|
101 | _timestepchanged = true; |
---|
102 | } |
---|
103 | } |
---|
104 | |
---|
105 | |
---|
106 | bool KeyboardEventHandler::timestepChanged() |
---|
107 | { |
---|
108 | if( _timestepchanged ) |
---|
109 | { |
---|
110 | _timestepchanged = false; |
---|
111 | return true; |
---|
112 | } |
---|
113 | return false; |
---|
114 | } |
---|
115 | |
---|
116 | |
---|
117 | bool KeyboardEventHandler::toggleWireframe() |
---|
118 | { |
---|
119 | if( _togglewireframe ) |
---|
120 | { |
---|
121 | _togglewireframe = false; |
---|
122 | return true; |
---|
123 | } |
---|
124 | return false; |
---|
125 | } |
---|
126 | |
---|
127 | |
---|
128 | bool KeyboardEventHandler::toggleManipulatorMode() |
---|
129 | { |
---|
130 | if( _togglemanipulatormode ) |
---|
131 | { |
---|
132 | _togglemanipulatormode = false; |
---|
133 | return true; |
---|
134 | } |
---|
135 | return false; |
---|
136 | } |
---|
137 | |
---|
138 | |
---|