AnnDefaultEventListener.cpp
Go to the documentation of this file.
1 // This is an open source non-commercial project. Dear PVS-Studio, please check it.
2 // PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
3 
4 #include "AnnLogger.hpp"
5 #include "AnnGetter.hpp"
7 
8 using namespace Annwvyn;
9 
12  turnMode{ WHEEL },
15  straffleft{ KeyCode::a },
16  straffright{ KeyCode::d },
17  jump{ KeyCode::space },
18  run{ KeyCode::lshift },
19  recenter{ KeyCode::f12 },
20  deadzone{ 1.0f / 5.0f },
21  wheelStickSensitivity{ 6.0f / 8.0f },
22  maxWheelAngle{ 45 },
23  minWheelAngle{ 0.5f },
24  stickCurrentAngleDegree{ 0 },
25  computedWheelValue{ 0 },
26  lastAngle{ 0 },
27  OpenVRController{ AnnGetStringUtility()->hash("OpenVR Hand Controller") },
28  OculusTouchController{ AnnGetStringUtility()->hash("Oculus Touch") }
29 {
30  //Use 1st analog stick for displacement
31  axes[ax_walk] = 0;
32  axes[ax_straff] = 1;
33  //Use second analog stick for horizontal rotation
34  axes[ax_rotate] = 3;
35 
36  buttons[b_run] = 2;
37  buttons[b_jump] = 0;
38  buttons[b_console] = 7;
39  buttons[b_debug] = 6;
40 }
41 
43  KeyCode::code bw,
44  KeyCode::code sl,
45  KeyCode::code sr,
46  KeyCode::code jmp,
47  KeyCode::code rn)
48 {
49  forward = fw;
50  backward = bw;
51  straffleft = sl;
52  straffright = sr;
53  jump = jmp;
54  run = rn;
55 }
56 
57 //Each key press an release are a separated event. in contain a state "pressed" or "released" and the Annwvyn keycode corresponding to the key.
58 //They are derivate from the OIS keycode list, see AnnKeyCode.h for the complete list.
59 //All keys are regarded as their equivalent on the American QWERTY layout, independently of the operating system behavior.
60 //For compatibility purposes, it's possible that the engine has done a system call to set the current keyboard layout to the American QWERTY layout.
61 //It will normally switch the keyboard back to it's original configuration once the AnnEngine object is destroyed
63 {
64  //If the corresponding key is pressed, set the direction to true.
65  if(!e.shouldIgnore())
66  {
67  if(AnnGetVRRenderer()->shouldPauseFlag()) return;
68 
69  //Z and Q are hacks for supporting french AZERTY keyboard easilly
70  if(e.getKey() == forward || e.getKey() == KeyCode::z)
71  player->walking[walkDirection::forward] = e.isPressed();
72  if(e.getKey() == backward)
73  player->walking[walkDirection::backward] = e.isPressed();
74  if(e.getKey() == straffleft || e.getKey() == KeyCode::q)
75  player->walking[left] = e.isPressed();
76  if(e.getKey() == straffright)
77  player->walking[right] = e.isPressed();
78  if(e.getKey() == run)
79  player->run = e.isPressed();
80 
81  //Jumping is a function call because it's an action and not a "state" the player has.
82  if(e.isPressed() && e.getKey() == recenter)
83  AnnGetVRRenderer()->recenter();
84 
85  if(e.isPressed() && e.getKey() == KeyCode::escape)
87  }
88  if(e.isPressed()) switch(e.getKey())
89  {
90  case KeyCode::grave:
91  AnnGetOnScreenConsole()->toggle();
92  break;
93  case KeyCode::tab:
94  AnnGetVRRenderer()->cycleDebugHud();
95  break;
96  case KeyCode::f1:
98  break;
99  case KeyCode::f2:
101  break;
102  case KeyCode::f3:
104  break;
105  case KeyCode::f5:
106  AnnGetPhysicsEngine()->toggleDebugPhysics();
107  default: break;
108  }
109 }
110 
111 //The mouse event contain all information about the mouse. Mouse movement are integer and are represented by 3 axis
112 // X : horizontal movement to the right in pixels
113 // Y : Vertical movement, to the front in pixels
114 // Z : Scroll wheel movement, scroll up is positive, in "line" increments
116 {
117  player->applyMouseRelativeRotation(e.getAxis(MouseAxisID(X)).getRelValue());
118 }
119 
120 //The stick event contain all the data for a specific joystick. In includes buttons current states, press and release events, stick relative and absolute values
122 {
123  if(AnnGetVRRenderer()->shouldPauseFlag()) return;
124  if(!e.isXboxController()) return;
125  if(e.getAxisCount() >= 4) //If we have 2 analog stick (or equivalent) available
126  {
127  player->analogWalk = trim(e.getAxis(axes[ax_walk]).getAbsValue(), deadzone);
128  player->analogStraff = trim(e.getAxis(axes[ax_straff]).getAbsValue(), deadzone);
129  player->analogRotate = trim(e.getAxis(axes[ax_rotate]).getAbsValue(), deadzone);
130  }
131  if(e.isPressed(buttons[b_run]))
132  player->run = true;
133  if(e.isReleased(buttons[b_run]))
134  player->run = false;
135 
136  if(e.isPressed(buttons[b_console]))
137  AnnGetOnScreenConsole()->toggle();
138  if(e.isPressed(buttons[b_debug]))
139  AnnGetVRRenderer()->cycleDebugHud();
140 }
141 
143 {
144  if(degree < 0)
145  degree += 360.0f;
146 }
147 
149 {
150  if(AnnGetVRRenderer()->shouldPauseFlag()) return;
151  auto rightStickThreashold{ 0.0225 };
152  if(e._getController()->getType() == OculusTouchController) rightStickThreashold = 0.8;
153 
154  auto controller = e._getController();
155  AnnVect2 analog{ controller->getAxis(0).getValue(), controller->getAxis(1).getValue() };
156  switch(controller->getSide())
157  {
158  default: break;
160  {
161  player->analogStraff = controller->getAxis(0).getValue();
162  player->analogWalk = -controller->getAxis(1).getValue();
163 
164  if(controller->hasBeenPressed(2))
165  {
166  if(controller->getType() == OculusTouchController)
167  AnnGetOnScreenConsole()->toggle();
168  }
169  break;
170  }
172  {
173  if(controller->hasBeenPressed(3))
174  AnnGetVRRenderer()->recenter();
175  switch(turnMode)
176  {
177  default:
178  case WHEEL:
179  //If we take the stick values as coordinate in the trigonometric plan, this will give the angle
180  stickCurrentAngleDegree = AnnRadian(std::atan2(analog.y, analog.x)).valueDegrees();
181  //Change range from [-180; +180] to [0; 360]
183 
184  //Detect the relative angle between 2 frames
186  //If value is too high it's either that you completed a full turn or there's a glitch in the input data, ignore.
187  if(stickCurrentAngleDegree == 0 || std::abs(computedWheelValue) > maxWheelAngle || std::abs(computedWheelValue) < minWheelAngle || analog.squaredLength() < rightStickThreashold)
188  computedWheelValue = 0;
189 
192  break;
193 
194  case NORMAL:
195  player->analogRotate = controller->getAxis(0).getValue();
196  }
197  break;
198  }
199  }
200 }
KeyCode::code backward
S by default.
MouseAxisID
Name and number of axes.
Definition: AnnEvents.hpp:70
AnnEngine * AnnGetEngine()
Get the current instance of AnnEngine.
Definition: AnnGetter.cpp:10
KeyCode::code recenter
F12 by default.
void MouseEvent(AnnMouseEvent e) override
Get events from the mouse.
AnnPlayerBody * player
Pointer to the player. Set by the constructor, provide easy access to the AnnPlayerBody.
void KeyEvent(AnnKeyEvent e) override
Get events from keyboards.
KeyCode::code straffleft
A by default.
bool run
Running state.
Namespace containing the totality of Annwvyn components.
Definition: AnnGetter.cpp:8
AnnOgreVRRendererPtr AnnGetVRRenderer()
Get the VR renderer.
Definition: AnnGetter.cpp:20
A joystick event.
Definition: AnnEvents.hpp:240
T atan2(T... args)
void requestQuit()
Permit.
Definition: AnnEngine.cpp:584
static void reclampDegreeToPositiveRange(float &degree)
KeyCode::code run
shift by default
Create a ostream to the Ogre logger.
AnnStringUtilityPtr AnnGetStringUtility()
Get the string utility object.
Definition: AnnGetter.cpp:24
An angle in Radian.
Definition: AnnAngle.hpp:22
void HandControllerEvent(AnnHandControllerEvent e) override
Get events from an hand controller.
A hand controller event.
Definition: AnnEvents.hpp:300
static float trim(float value, float deadzone)
Utility function for applying a dead-zone on a joystick axis.
bool walking[4]
Waling state. Forward Backward Left Right.
KeyCode::code straffright
D by default.
AnnPhysicsEnginePtr AnnGetPhysicsEngine()
Get the physics engine.
Definition: AnnGetter.cpp:13
A keyboard event.
Definition: AnnEvents.hpp:37
AnnConsolePtr AnnGetOnScreenConsole()
Get the on screen console.
Definition: AnnGetter.cpp:23
void applyMouseRelativeRotation(int relValue)
void setKeys(KeyCode::code fw, KeyCode::code bw, KeyCode::code sl, KeyCode::code sr, KeyCode::code jmp, KeyCode::code rn)
Set all the key-codes for the controls.
Ogre::Vector2 AnnVect2
Definition: AnnTypes.h:28
Base class for all event listener.
A mouse event information object.
Definition: AnnEvents.hpp:124
float deadzone
value used for trimming low joysticks value
void ControllerEvent(AnnControllerEvent e) override
Get events from the joystick.
AnnDefaultEventListener()
Construct the default listener.
KeyCode::code jump
Space by default.