Skip to content

Commit c7264ef

Browse files
committed
Fix input handling
1 parent 79b881a commit c7264ef

File tree

12 files changed

+190
-381
lines changed

12 files changed

+190
-381
lines changed

src/Cpp/1-getting-started/1-3-6-Camera/1-3-6-Camera.vcxproj

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,6 @@ xcopy /Y $(ProjectDir)Assets\Textures\*.* $(OutDir)Assets\Textures\</Command>
119119
<ClCompile Include="..\..\..\..\lib\imgui\include\imgui\imgui_widgets.cpp" />
120120
<ClCompile Include="Application.cpp" />
121121
<ClCompile Include="DeviceContext.cpp" />
122-
<ClCompile Include="Input\Input.cpp" />
123-
<ClCompile Include="Input\Keyboard.cpp" />
124-
<ClCompile Include="Input\Mouse.cpp" />
125122
<ClCompile Include="Main.cpp" />
126123
<ClCompile Include="CameraApplication.cpp" />
127124
<ClCompile Include="ModelFactory.cpp" />
@@ -134,9 +131,6 @@ xcopy /Y $(ProjectDir)Assets\Textures\*.* $(OutDir)Assets\Textures\</Command>
134131
<ClInclude Include="Definitions.hpp" />
135132
<ClInclude Include="DeviceContext.hpp" />
136133
<ClInclude Include="CameraApplication.hpp" />
137-
<ClInclude Include="Input\Input.hpp" />
138-
<ClInclude Include="Input\Keyboard.hpp" />
139-
<ClInclude Include="Input\Mouse.hpp" />
140134
<ClInclude Include="ModelFactory.hpp" />
141135
<ClInclude Include="Pipeline.hpp" />
142136
<ClInclude Include="PipelineFactory.hpp" />

src/Cpp/1-getting-started/1-3-6-Camera/1-3-6-Camera.vcxproj.filters

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,6 @@
6060
<ClCompile Include="..\..\..\..\lib\imgui\include\imgui\backend\imgui_impl_glfw.cpp">
6161
<Filter>Source Files</Filter>
6262
</ClCompile>
63-
<ClCompile Include="Input\Input.cpp">
64-
<Filter>Source Files</Filter>
65-
</ClCompile>
66-
<ClCompile Include="Input\Keyboard.cpp">
67-
<Filter>Source Files</Filter>
68-
</ClCompile>
69-
<ClCompile Include="Input\Mouse.cpp">
70-
<Filter>Source Files</Filter>
71-
</ClCompile>
7263
</ItemGroup>
7364
<ItemGroup>
7465
<ClInclude Include="Application.hpp">
@@ -101,15 +92,6 @@
10192
<ClInclude Include="DeviceContext.hpp">
10293
<Filter>Header Files</Filter>
10394
</ClInclude>
104-
<ClInclude Include="Input\Input.hpp">
105-
<Filter>Header Files</Filter>
106-
</ClInclude>
107-
<ClInclude Include="Input\Keyboard.hpp">
108-
<Filter>Header Files</Filter>
109-
</ClInclude>
110-
<ClInclude Include="Input\Mouse.hpp">
111-
<Filter>Header Files</Filter>
112-
</ClInclude>
11395
</ItemGroup>
11496
<ItemGroup>
11597
<Image Include="Assets\Textures\T_Good_Froge.dds" />

src/Cpp/1-getting-started/1-3-6-Camera/Application.cpp

Lines changed: 128 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include "Application.hpp"
2-
#include "Input/Input.hpp"
2+
33
#include <GLFW/glfw3.h>
44

55
Application::Application(const std::string_view title)
@@ -38,13 +38,14 @@ bool Application::Initialize()
3838
return false;
3939
}
4040

41-
_input = std::make_unique<Input>(_window);
42-
4341
const int32_t windowLeft = videoMode->width / 2 - _width / 2;
4442
const int32_t windowTop = videoMode->height / 2 - _height / 2;
4543
glfwSetWindowPos(_window, windowLeft, windowTop);
4644
glfwSetWindowUserPointer(_window, this);
4745
glfwSetFramebufferSizeCallback(_window, HandleResize);
46+
glfwSetKeyCallback(_window, HandleKeyboard);
47+
glfwSetMouseButtonCallback(_window, HandleMouseButton);
48+
glfwSetCursorPosCallback(_window, HandleMouseMovement);
4849
return true;
4950
}
5051

@@ -56,8 +57,30 @@ void Application::OnResize(
5657
_height = height;
5758
}
5859

60+
void Application::OnKey(
61+
const int32_t key,
62+
const int32_t action)
63+
{
64+
switch (action)
65+
{
66+
case GLFW_PRESS:
67+
_keysPressed.insert(key);
68+
_keysDown.insert(key);
69+
break;
70+
case GLFW_RELEASE:
71+
_keysReleased.insert(key);
72+
_keysDown.erase(key);
73+
}
74+
}
75+
5976
void Application::Cleanup()
6077
{
78+
glfwSetCursorPosCallback(_window, nullptr);
79+
glfwSetMouseButtonCallback(_window, nullptr);
80+
glfwSetKeyCallback(_window, nullptr);
81+
glfwSetFramebufferSizeCallback(_window, nullptr);
82+
glfwSetWindowUserPointer(_window, nullptr);
83+
6184
glfwDestroyWindow(_window);
6285
glfwTerminate();
6386
}
@@ -82,9 +105,10 @@ void Application::Run()
82105

83106
while (!glfwWindowShouldClose(_window))
84107
{
85-
_input->Update(
108+
UpdateInput(
86109
static_cast<float>(_width) / 2.0f,
87110
static_cast<float>(_height) / 2.0f);
111+
88112
glfwPollEvents();
89113
Update();
90114
Render();
@@ -100,6 +124,88 @@ void Application::HandleResize(
100124
application.OnResize(width, height);
101125
}
102126

127+
void Application::HandleKeyboard(
128+
GLFWwindow* window,
129+
const int32_t key,
130+
const int32_t scanCode,
131+
const int32_t action,
132+
const int32_t modifier)
133+
{
134+
Application& application = *static_cast<Application*>(glfwGetWindowUserPointer(window));
135+
application.OnKey(key, action);
136+
}
137+
138+
void Application::HandleMouseButton(
139+
GLFWwindow* window,
140+
const int32_t button,
141+
const int32_t action,
142+
const int32_t modifiers)
143+
{
144+
Application& application = *static_cast<Application*>(glfwGetWindowUserPointer(window));
145+
application.OnMouseButton(button, action);
146+
}
147+
148+
void Application::HandleMouseMovement(
149+
GLFWwindow* window,
150+
const double x,
151+
const double y)
152+
{
153+
Application& application = *static_cast<Application*>(glfwGetWindowUserPointer(window));
154+
application.OnMouseMove(static_cast<float>(x), static_cast<float>(y));
155+
}
156+
157+
void Application::OnMouseButton(
158+
const int32_t button,
159+
const int32_t action)
160+
{
161+
// ReSharper disable once CppDefaultCaseNotHandledInSwitchStatement
162+
switch (action)
163+
{
164+
case GLFW_PRESS:
165+
_buttonsPressed.insert(button);
166+
_buttonsDown.insert(button);
167+
break;
168+
case GLFW_RELEASE:
169+
_buttonsUp.insert(button);
170+
_buttonsDown.erase(button);
171+
break;
172+
}
173+
}
174+
175+
void Application::OnMouseMove(
176+
const float x,
177+
const float y)
178+
{
179+
DeltaPosition = DirectX::XMFLOAT2(
180+
x - CursorPosition.x,
181+
y - CursorPosition.y);
182+
183+
CursorPosition = DirectX::XMFLOAT2(x, y);
184+
}
185+
186+
void Application::UpdateInput(
187+
float centerX,
188+
float centerY)
189+
{
190+
_keysDown.clear();
191+
_keysUp.clear();
192+
_keysPressed.clear();
193+
_keysReleased.clear();
194+
195+
const auto window = glfwGetCurrentContext();
196+
_buttonsDown.clear();
197+
_buttonsPressed.clear();
198+
_buttonsUp.clear();
199+
200+
DeltaPosition = DirectX::XMFLOAT2(0.0f, 0.0f);
201+
202+
if (_isCaptured)
203+
{
204+
CursorPosition = DirectX::XMFLOAT2(centerX, centerY);
205+
glfwSetCursorPos(window, centerX, centerY);
206+
}
207+
}
208+
103209
GLFWwindow* Application::GetWindow() const
104210
{
105211
return _window;
@@ -115,22 +221,32 @@ int32_t Application::GetWindowHeight() const
115221
return _height;
116222
}
117223

118-
bool Application::IsKeyDown(const std::int32_t key) const
224+
bool Application::IsKeyDown(const int32_t key) const
225+
{
226+
return _keysDown.count(key) != 0;
227+
}
228+
229+
bool Application::IsKeyPressed(const int32_t key) const
230+
{
231+
return _keysPressed.count(key) != 0;
232+
}
233+
234+
bool Application::IsKeyUp(const int32_t key) const
119235
{
120-
return _input->GetKeyboard().IsKeyDown(key);
236+
return _keysUp.count(key) != 0;
121237
}
122238

123-
bool Application::IsKeyPressed(const std::int32_t key) const
239+
bool Application::IsButtonDown(const int32_t button) const
124240
{
125-
return _input->GetKeyboard().IsKeyPressed(key);
241+
return _buttonsDown.count(button) != 0;
126242
}
127243

128-
bool Application::IsKeyUp(const std::int32_t key) const
244+
bool Application::IsButtonPressed(const int32_t button) const
129245
{
130-
return _input->GetKeyboard().IsKeyUp(key);
246+
return _buttonsPressed.count(button) != 0;
131247
}
132248

133-
bool Application::IsButtonPressed(const std::int32_t button) const
249+
bool Application::IsButtonUp(const int32_t button) const
134250
{
135-
return _input->GetMouse().IsButtonPressed(button);
251+
return _buttonsUp.count(button) != 0;
136252
}
Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
#pragma once
22

3+
#include <DirectXMath.h>
4+
35
#include <string_view>
46
#include <cstdint>
57
#include <memory>
8+
#include <set>
69

710
struct GLFWwindow;
8-
class Input;
911

1012
class Application
1113
{
@@ -15,13 +17,18 @@ class Application
1517

1618
void Run();
1719
protected:
18-
static void HandleResize(
19-
GLFWwindow* window,
20-
const int32_t width,
21-
const int32_t height);
2220
virtual void OnResize(
2321
const int32_t width,
2422
const int32_t height);
23+
void OnKey(
24+
const int32_t key,
25+
const int32_t action);
26+
void OnMouseButton(
27+
const int32_t button,
28+
const int32_t action);
29+
void OnMouseMove(
30+
const float x,
31+
const float y);
2532

2633
virtual bool Initialize();
2734
virtual bool Load() = 0;
@@ -34,16 +41,53 @@ class Application
3441
[[nodiscard]] int32_t GetWindowWidth() const;
3542
[[nodiscard]] int32_t GetWindowHeight() const;
3643

37-
[[nodiscard]] bool IsButtonPressed(const std::int32_t button) const;
38-
[[nodiscard]] bool IsKeyDown(const std::int32_t key) const;
39-
[[nodiscard]] bool IsKeyPressed(const std::int32_t key) const;
40-
[[nodiscard]] bool IsKeyUp(const std::int32_t key) const;
44+
[[nodiscard]] bool IsButtonPressed(const int32_t button) const;
45+
[[nodiscard]] bool IsButtonDown(const int32_t button) const;
46+
[[nodiscard]] bool IsButtonUp(const int32_t button) const;
47+
[[nodiscard]] bool IsKeyDown(const int32_t key) const;
48+
[[nodiscard]] bool IsKeyPressed(const int32_t key) const;
49+
[[nodiscard]] bool IsKeyUp(const int32_t key) const;
4150

42-
std::unique_ptr<Input> _input = nullptr;
51+
DirectX::XMFLOAT2 DeltaPosition = { 0.0f, 0.0f };
52+
DirectX::XMFLOAT2 CursorPosition = { 0.0f, 0.0f };
4353

4454
private:
55+
static void HandleResize(
56+
GLFWwindow* window,
57+
const int32_t width,
58+
const int32_t height);
59+
60+
static void HandleKeyboard(
61+
GLFWwindow* window,
62+
const int32_t key,
63+
const int32_t scanCode,
64+
const int32_t action,
65+
const int32_t modifier);
66+
static void HandleMouseButton(
67+
GLFWwindow* window,
68+
const int32_t button,
69+
const int32_t action,
70+
const int32_t modifiers);
71+
static void HandleMouseMovement(
72+
GLFWwindow* window,
73+
const double x,
74+
const double y);
75+
76+
void UpdateInput(float centerX, float centerY);
77+
4578
GLFWwindow* _window = nullptr;
4679
int32_t _width = 0;
4780
int32_t _height = 0;
4881
std::string_view _title;
82+
83+
std::set<int32_t> _keysDown;
84+
std::set<int32_t> _keysUp;
85+
std::set<int32_t> _keysPressed;
86+
std::set<int32_t> _keysReleased;
87+
88+
std::set<int32_t> _buttonsDown{};
89+
std::set<int32_t> _buttonsPressed{};
90+
std::set<int32_t> _buttonsUp{};
91+
bool _isCaptured = false;
92+
4993
};

src/Cpp/1-getting-started/1-3-6-Camera/CameraApplication.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ CameraApplication::~CameraApplication()
7575
#endif
7676
_device.Reset();
7777

78-
ImGui_ImplGlfw_Shutdown();
79-
ImGui::DestroyContext(_imGuiContext);
78+
//ImGui_ImplGlfw_Shutdown();
79+
//ImGui::DestroyContext(_imGuiContext);
8080

8181
Application::Cleanup();
8282
}
@@ -125,7 +125,7 @@ bool CameraApplication::Initialize()
125125
return false;
126126
}
127127

128-
InitializeImGui();
128+
//InitializeImGui();
129129

130130
constexpr char deviceName[] = "DEV_Main";
131131
_device->SetPrivateData(WKPDID_D3DDebugObjectName, sizeof(deviceName), deviceName);
@@ -339,8 +339,8 @@ void CameraApplication::OnResize(
339339

340340
CreateSwapchainResources();
341341

342-
ImGuiIO& io = ImGui::GetIO();
343-
io.DisplaySize = ImVec2(GetWindowWidth(), GetWindowHeight());
342+
//ImGuiIO& io = ImGui::GetIO();
343+
//io.DisplaySize = ImVec2(GetWindowWidth(), GetWindowHeight());
344344

345345
_projectionMatrix = DirectX::XMMatrixPerspectiveFovLH(
346346
DirectX::XMConvertToRadians(45.0f),
@@ -394,7 +394,7 @@ void CameraApplication::Render()
394394

395395
_deviceContext->DrawIndexed();
396396

397-
RenderUi();
397+
//RenderUi();
398398
_swapChain->Present(1, 0);
399399
}
400400

0 commit comments

Comments
 (0)