Engine
Frameworkcreatedbymeusableforthecreationofsimplegames.CurrentlysupportsOpenGL(Verysimple)andVulkan.
InputManager.cpp
Go to the documentation of this file.
2 #include "Engine/engine.hpp"
4 #include <windows.h>
5 
6 class InputCallbacks : public gainput::InputListener
7 {
8 public:
9  InputCallbacks(gainput::InputManager& manager, int index) : manager(manager), index(index) { }
10 
11  bool OnDeviceButtonBool(gainput::DeviceId deviceId, gainput::DeviceButtonId deviceButton, bool oldValue, bool newValue) override
12  {
13  const gainput::InputDevice* device = manager.GetDevice(deviceId);
14  char buttonName[64] = "";
15  device->GetButtonName(deviceButton, buttonName, 64);
16  ImGuiIO& io = ImGui::GetIO();
17 
18  if (deviceId == Engine::Engine::GetEngine().lock()->GetInputManager().lock()->GetKeyboardId())
19  {
20  io.KeysDown[deviceButton] = newValue;
21 
22  if (deviceButton == gainput::KeyCtrlL || deviceButton == gainput::KeyCtrlR)
23  io.KeyCtrl = newValue;
24 
25  if (deviceButton == gainput::KeyAltL || deviceButton == gainput::KeyAltR)
26  io.KeyAlt = newValue;
27 
28  if (deviceButton == gainput::KeyShiftL || deviceButton == gainput::KeyShiftR)
29  io.KeyShift = newValue;
30 
31  if (deviceButton == gainput::KeySuperL || deviceButton == gainput::KeySuperR)
32  io.KeySuper = newValue;
33  }
34  else if (deviceId == Engine::Engine::GetEngine().lock()->GetInputManager().lock()->GetMouseId())
35  {
36  io.MouseDown[deviceButton] = newValue;
37 
38  if (deviceButton == gainput::MouseButton3)
39  {
40  if (newValue)
41  io.MouseWheel += Engine::Engine::GetEngine().lock()->GetInputManager().lock()->GetInputDefaults().scrollSpeed;
42  }
43  else if (deviceButton == gainput::MouseButton4)
44  {
45  if (newValue)
46  io.MouseWheel -= Engine::Engine::GetEngine().lock()->GetInputManager().lock()->GetInputDefaults().scrollSpeed;
47  }
48 
49  //printf_s("(%d) Device %d (%s%d) bool button (%d/%s) changed: %d -> %d\n", index, deviceId, device->GetTypeName(), device->GetIndex(), deviceButton, buttonName, oldValue, newValue);
50  }
51 
52  return false;
53  }
54 
55  bool OnDeviceButtonFloat(gainput::DeviceId deviceId, gainput::DeviceButtonId deviceButton, float oldValue, float newValue) override
56  {
57  if (deviceId == Engine::Engine::GetEngine().lock()->GetInputManager().lock()->GetMouseId())
58  {
59  ImGuiIO& io = ImGui::GetIO();
60  if (deviceButton == gainput::MouseAxisX)
61  {
62  io.MousePos.x = newValue * Engine::Engine::GetEngine().lock()->GetWindow().lock()->GetWidth();
63  }
64  else if (deviceButton == gainput::MouseAxisY)
65  {
66  io.MousePos.y = newValue * Engine::Engine::GetEngine().lock()->GetWindow().lock()->GetHeight();
67  }
68  }
69 
70  //printf_s("(%d) Device %d (%s%d) float button (%d/%s) changed: %f -> %f\n", index, deviceId, device->GetTypeName(), device->GetIndex(), deviceButton, buttonName, oldValue, newValue);
71  return true;
72  }
73 
74  int GetPriority() const override
75  {
76  return index;
77  }
78 
79 private:
80  gainput::InputManager& manager;
81  int index;
82 };
83 
84 namespace Engine
85 {
86  InputManager::InputManager() noexcept : inputManager()
87  {
88  inputManager.SetDisplaySize(Engine::GetEngine().lock()->GetWindow().lock()->GetWidth(), Engine::GetEngine().lock()->GetWindow().lock()->GetHeight());
89 
90  mouseId = inputManager.CreateDevice<gainput::InputDeviceMouse>();
91  keyboardId = inputManager.CreateDevice<gainput::InputDeviceKeyboard>();
92  gamepadId = inputManager.CreateDevice<gainput::InputDevicePad>();
93  inputManager.AddListener(new InputCallbacks(inputManager, 1));
94 
95  ImGuiIO& io = ImGui::GetIO();
96  io.KeyMap[ImGuiKey_Tab] = gainput::KeyTab; // Keyboard mapping. ImGui will use those indices to peek into the io.KeyDown[] array.
97  io.KeyMap[ImGuiKey_LeftArrow] = gainput::KeyLeft;
98  io.KeyMap[ImGuiKey_RightArrow] = gainput::KeyRight;
99  io.KeyMap[ImGuiKey_UpArrow] = gainput::KeyUp;
100  io.KeyMap[ImGuiKey_DownArrow] = gainput::KeyDown;
101  io.KeyMap[ImGuiKey_PageUp] = gainput::KeyPageUp;
102  io.KeyMap[ImGuiKey_PageDown] = gainput::KeyPageDown;
103  io.KeyMap[ImGuiKey_Home] = gainput::KeyHome;
104  io.KeyMap[ImGuiKey_End] = gainput::KeyEnd;
105  io.KeyMap[ImGuiKey_Delete] = gainput::KeyDelete;
106  io.KeyMap[ImGuiKey_Backspace] = gainput::KeyBackSpace;
107  io.KeyMap[ImGuiKey_Enter] = gainput::KeyReturn;
108  io.KeyMap[ImGuiKey_Escape] = gainput::KeyEscape;
109  io.KeyMap[ImGuiKey_A] = gainput::KeyA;
110  io.KeyMap[ImGuiKey_C] = gainput::KeyC;
111  io.KeyMap[ImGuiKey_V] = gainput::KeyV;
112  io.KeyMap[ImGuiKey_X] = gainput::KeyX;
113  io.KeyMap[ImGuiKey_Y] = gainput::KeyY;
114  io.KeyMap[ImGuiKey_Z] = gainput::KeyZ;
115  }
116 
117  gainput::InputManager& InputManager::GetInputManager()
118  {
119  return inputManager;
120  }
121 
122  eastl::vector<gainput::DeviceButtonId> InputManager::GetAllKeysDown(gainput::DeviceId deviceId)
123  {
124  eastl::vector<gainput::DeviceButtonId> specToReturn(512, false);
125 
126  for (int i = 0; i < gainput::KeyCount_; ++i)
127  {
128  bool value = inputManager.GetDevice(deviceId)->GetBool(i);
129  specToReturn[i] = value;
130  }
131 
132  return specToReturn;
133  }
134 
135  const gainput::DeviceId& InputManager::GetMouseId() const
136  {
137  return mouseId;
138  }
139 
140  const gainput::DeviceId& InputManager::GetKeyboardId() const
141  {
142  return keyboardId;
143  }
144 
145  const gainput::DeviceId& InputManager::GetGamepadId() const
146  {
147  return gamepadId;
148  }
149 
150  InputDefaults InputManager::GetInputDefaults() const
151  {
152  return inputDefaults;
153  }
154 
155  void InputManager::Update() noexcept
156  {
157  inputManager.Update();
158 
159  MSG message;
160  while (PeekMessage(&message, Engine::GetEngine().lock()->GetWindow().lock()->GetWindowHandle(), 0, 0, PM_REMOVE))
161  {
162  TranslateMessage(&message);
163  DispatchMessage(&message);
164 
165  inputManager.HandleMessage(message);
166 
167  if (message.message == WM_CHAR)
168  {
169  const WPARAM key = message.wParam;
170  if (key == 0x08 // backspace
171  || key == 0x0A // linefeed
172  || key == 0x1B // escape
173  || key == 0x09 // tab
174  || key == 0x0D // carriage return
175  || key > 255)
176  {
177  return;
178  }
179  ImGuiIO& io = ImGui::GetIO();
180  io.AddInputCharacter(ImWchar(char(key)));
181  }
182  }
183  }
184 } // namespace Engine
ImVec2 MousePos
Definition: imgui.h:836
IMGUI_API void AddInputCharacter(ImWchar c)
Definition: imgui.cpp:799
int KeyMap[ImGuiKey_COUNT]
Definition: imgui.h:791
unsigned short ImWchar
Definition: imgui.h:71
bool OnDeviceButtonFloat(gainput::DeviceId deviceId, gainput::DeviceButtonId deviceButton, float oldValue, float newValue) override
IMGUI_API ImGuiIO & GetIO()
Definition: imgui.cpp:2144
int GetPriority() const override
bool KeysDown[512]
Definition: imgui.h:844
bool KeyShift
Definition: imgui.h:841
InputCallbacks(gainput::InputManager &manager, int index)
Definition: InputManager.cpp:9
bool MouseDown[5]
Definition: imgui.h:837
bool KeyCtrl
Definition: imgui.h:840
bool OnDeviceButtonBool(gainput::DeviceId deviceId, gainput::DeviceButtonId deviceButton, bool oldValue, bool newValue) override
float y
Definition: imgui.h:98
bool KeySuper
Definition: imgui.h:843
Definition: imgui.h:777
float MouseWheel
Definition: imgui.h:838
bool KeyAlt
Definition: imgui.h:842
static eastl::weak_ptr< Engine > GetEngine() noexcept
This method allows you to get the instance of the Engine. This method will automatically initialize t...
Definition: engine.cpp:130
float x
Definition: imgui.h:98