Skip to content

Commit 1585be0

Browse files
committed
Add WindowUserPointer for glfw callbacks
1 parent 5465f85 commit 1585be0

File tree

9 files changed

+97
-11
lines changed

9 files changed

+97
-11
lines changed

include/NovelRT/UI/DearImGui/GlfwVulkan/UI.DearImGui.GlfwVulkan.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ namespace NovelRT::UI::DearImGui::GlfwVulkan
1717
#include "../../../Graphics/Graphics.h"
1818
#include "../../../Graphics/Vulkan/Graphics.Vulkan.h"
1919
#include "../../../Windowing/Glfw/Windowing.Glfw.h"
20+
#include "../../../Input/Glfw/Input.Glfw.h"
2021
#include "../../../PluginManagement/PluginManagement.h"
2122
#include "../UI.DearImGui.h"
2223
#include <imgui_impl_glfw.h>

include/NovelRT/Windowing/Glfw/GlfwWindowingDevice.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ namespace NovelRT::Windowing::Glfw
1515
private:
1616
std::unique_ptr<GLFWwindow, decltype(&glfwDestroyWindow)> _window;
1717
std::string _currentTitle;
18+
Windowing::WindowUserPointer _pointers;
1819

1920
public:
2021
GlfwWindowingDevice() noexcept;
@@ -53,6 +54,11 @@ namespace NovelRT::Windowing::Glfw
5354
glfwSetWindowTitle(_window.get(), _currentTitle.c_str());
5455
}
5556

57+
[[nodiscard]] inline Windowing::WindowUserPointer GetUserPointerMap() const noexcept
58+
{
59+
return _pointers;
60+
}
61+
5662
void ProcessAllMessages() final;
5763
~GlfwWindowingDevice() final;
5864
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright © Matt Jones and Contributors. Licensed under the MIT Licence (MIT). See LICENCE.md in the repository root
2+
// for more information.
3+
4+
#ifndef NOVELRT_WINDOWING_WINDOWUSERPOINTER_H
5+
#define NOVELRT_WINDOWING_WINDOWUSERPOINTER_H
6+
7+
namespace NovelRT::Windowing
8+
{
9+
class WindowUserPointer
10+
{
11+
private:
12+
std::map<std::string, void*> _userPointerMap;
13+
14+
public:
15+
WindowUserPointer() noexcept;
16+
void AppendUserPointer(std::string key, void* value);
17+
void RemoveUserPointer(std::string key);
18+
void* GetUserPointer(std::string key);
19+
20+
};
21+
}
22+
23+
#endif // NOVELRT_WINDOWING_WINDOWUSERPOINTER_H

include/NovelRT/Windowing/Windowing.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace NovelRT::Windowing
1111
{
1212
class IWindowingDevice;
13+
class WindowUserPointer;
1314
}
1415

1516
// Windowing dependencies
@@ -19,5 +20,6 @@ namespace NovelRT::Windowing
1920

2021
// Windowing types
2122
#include "IWindowingDevice.h"
23+
#include "WindowUserPointer.h"
2224

2325
#endif // NOVELRT_WINDOWING_H

src/NovelRT/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ set(CORE_SOURCES
8383

8484
Utilities/Misc.cpp
8585

86+
Windowing/WindowUserPointer.cpp
87+
8688
Windowing/Glfw/GlfwWindowingDevice.cpp
8789
Windowing/Glfw/GlfwWindowingPluginProvider.cpp
8890
)

src/NovelRT/Input/Glfw/GlfwInputDevice.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ namespace NovelRT::Input::Glfw
3232
_mappedActions = std::vector<InputAction>();
3333
_window = reinterpret_cast<GLFWwindow*>(window);
3434

35+
auto pointerMap = reinterpret_cast<NovelRT::Windowing::WindowUserPointer*>(glfwGetWindowUserPointer(_window));
36+
pointerMap->AppendUserPointer("input", this);
37+
3538
// Map GLFW keys to NovelKeys
3639
_availableKeys.emplace("LeftMouseButton", NovelKey("LeftMouseButton", GLFW_MOUSE_BUTTON_LEFT));
3740
_availableKeys.emplace("RightMouseButton", NovelKey("RightMouseButton", GLFW_MOUSE_BUTTON_RIGHT));
@@ -167,6 +170,7 @@ namespace NovelRT::Input::Glfw
167170
int width = 0;
168171
int height = 0;
169172
glfwGetWindowSize(_window, &width, &height);
173+
170174
_logger.logInfo("GLFW input system initialised: window at {} x {}", width, height);
171175
}
172176

src/NovelRT/UI/DearImGui/GlfwVulkan/GlfwVulkanUIProvider.cpp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,26 @@ namespace NovelRT::UI::DearImGui::GlfwVulkan
5858
(NovelRT::Utilities::Misc::getExecutableDirPath() / "Resources" / "Fonts" / "Raleway-Regular.ttf").string();
5959
io.Fonts->AddFontFromFileTTF(defaultFont.c_str(), 36.0f);
6060
io.ConfigFlags |= ImGuiConfigFlags_NoMouse;
61-
62-
// if(_editorMode)
63-
// {
64-
// _logger.logDebugLine("Enabling viewport / editor mode for UI support.");
65-
// io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable;
66-
// }
61+
GLFWwindow* glfwWindow = reinterpret_cast<GLFWwindow*>(windowingDevice->GetHandle());
6762

6863
// GLFW Specific Init
69-
ImGui_ImplGlfw_InitForVulkan(reinterpret_cast<GLFWwindow*>(windowingDevice->GetHandle()), true);
64+
ImGui_ImplGlfw_InitForVulkan(glfwWindow, false);
65+
66+
//Setup callbacks
67+
//Cursor Position Callback
68+
glfwSetCursorPosCallback(glfwWindow, [](GLFWwindow* window, double x, double y)
69+
{
70+
if(glfwGetInputMode(window, GLFW_CURSOR) == GLFW_CURSOR_DISABLED)
71+
{
72+
return;
73+
}
74+
75+
ImGuiIO& io = ImGui::GetIO();
76+
io.AddMousePosEvent(static_cast<float>(x), static_cast<float>(y));
77+
});
78+
79+
//Setup input
80+
auto inputProvider = reinterpret_cast<NovelRT::Input::Glfw::GlfwInputDevice*>(reinterpret_cast<Windowing::WindowUserPointer*>(glfwWindow)->GetUserPointer("input"));
7081

7182
// Vulkan Specific Init
7283
_initInfo.Instance = vulkanProvider->GetVulkanInstance();
@@ -144,7 +155,7 @@ namespace NovelRT::UI::DearImGui::GlfwVulkan
144155
{
145156
x->Render(this->shared_from_this(), _windowSize);
146157
}
147-
158+
148159
for (auto&& button : _buttons)
149160
{
150161
button->Render(shared_from_this(), _windowSize);

src/NovelRT/Windowing/Glfw/GlfwWindowingDevice.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace NovelRT::Windowing::Glfw
77
{
8-
GlfwWindowingDevice::GlfwWindowingDevice() noexcept : _window(nullptr, nullptr)
8+
GlfwWindowingDevice::GlfwWindowingDevice() noexcept : _window(nullptr, nullptr), _pointers()
99
{
1010
}
1111

@@ -54,10 +54,13 @@ namespace NovelRT::Windowing::Glfw
5454
#endif
5555

5656
glfwSetWindowAttrib(window, GLFW_RESIZABLE, windowMode == NovelRT::Windowing::WindowMode::Windowed);
57-
glfwSetWindowUserPointer(window, this);
57+
58+
_pointers.AppendUserPointer("windowing", this);
59+
glfwSetWindowUserPointer(window, &_pointers);
5860

5961
glfwSetWindowSizeCallback(window, [](auto window, auto width, auto height) {
60-
auto thisDevice = reinterpret_cast<GlfwWindowingDevice*>(glfwGetWindowUserPointer(window));
62+
auto pointerMap = reinterpret_cast<Windowing::WindowUserPointer*>(glfwGetWindowUserPointer(window));
63+
auto thisDevice = reinterpret_cast<GlfwWindowingDevice*>(pointerMap->GetUserPointer("windowing"));
6164
auto countFfs = thisDevice->SizeChanged.getHandlerCount();
6265
unused(countFfs);
6366
thisDevice->SizeChanged(Maths::GeoVector2F(static_cast<float>(width), static_cast<float>(height)));
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright © Matt Jones and Contributors. Licensed under the MIT Licence (MIT). See LICENCE.md in the repository root
2+
// for more information.
3+
4+
#include "NovelRT/Windowing/Windowing.h"
5+
6+
namespace NovelRT::Windowing
7+
{
8+
WindowUserPointer::WindowUserPointer() noexcept
9+
: _userPointerMap(std::map<std::string, void*>())
10+
{
11+
}
12+
13+
void WindowUserPointer::AppendUserPointer(std::string key, void* value)
14+
{
15+
if(value == nullptr)
16+
{
17+
18+
}
19+
20+
unused(value);
21+
unused(key);
22+
}
23+
24+
void WindowUserPointer::RemoveUserPointer(std::string key)
25+
{
26+
unused(key);
27+
}
28+
29+
void* WindowUserPointer::GetUserPointer(std::string key)
30+
{
31+
unused(key);
32+
return nullptr;
33+
}
34+
}

0 commit comments

Comments
 (0)