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 | _toggleculling = 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 'c': |
---|
80 | _toggleculling = true; |
---|
81 | handled = true; |
---|
82 | break; |
---|
83 | |
---|
84 | |
---|
85 | } |
---|
86 | |
---|
87 | } |
---|
88 | return handled; |
---|
89 | } |
---|
90 | |
---|
91 | |
---|
92 | void KeyboardEventHandler::setTime( float time) |
---|
93 | { |
---|
94 | if( !isPaused() && time - _prevtime > 1.0/_tps ) |
---|
95 | { |
---|
96 | _prevtime = time; |
---|
97 | _timestep = _timestep + _direction; |
---|
98 | if( _timestep == _ntimesteps ) |
---|
99 | _timestep = 0; |
---|
100 | else if( _timestep < 0 ) |
---|
101 | _timestep = _ntimesteps-1; |
---|
102 | _timestepchanged = true; |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|
106 | |
---|
107 | bool KeyboardEventHandler::timestepChanged() |
---|
108 | { |
---|
109 | if( _timestepchanged ) |
---|
110 | { |
---|
111 | _timestepchanged = false; |
---|
112 | return true; |
---|
113 | } |
---|
114 | return false; |
---|
115 | } |
---|
116 | |
---|
117 | |
---|
118 | bool KeyboardEventHandler::toggleWireframe() |
---|
119 | { |
---|
120 | if( _togglewireframe ) |
---|
121 | { |
---|
122 | _togglewireframe = false; |
---|
123 | return true; |
---|
124 | } |
---|
125 | return false; |
---|
126 | } |
---|
127 | |
---|
128 | |
---|
129 | bool KeyboardEventHandler::toggleCulling() |
---|
130 | { |
---|
131 | if( _toggleculling ) |
---|
132 | { |
---|
133 | _toggleculling = false; |
---|
134 | return true; |
---|
135 | } |
---|
136 | return false; |
---|
137 | } |
---|
138 | |
---|
139 | |
---|