Fling Engine  0.00.1
Fling Engine is a game engine written in Vulkan
ImGuiInputBinding.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 
4 #if WITH_IMGUI
5 #include <imgui.h>
6 
7 #if FLING_WINDOWS
8 
9 #undef APIENTRY
10 #define GLFW_EXPOSE_NATIVE_WIN32
11 
12 #elif FLING_LINUX
13 
14 #define GLFW_EXPOSE_NATIVE_X11
15 
16 #endif
17 
18 #include <GLFW/glfw3native.h> // for glfwGetWin32Window
19 
20 namespace Fling
21 {
22  namespace InternalImGui
23  {
24  // Data
25  enum GlfwClientApi
26  {
27  GlfwClientApi_Unknown,
28  GlfwClientApi_OpenGL,
29  GlfwClientApi_Vulkan
30  };
31 
32  static GlfwClientApi g_ClientApi = GlfwClientApi_Unknown;
33  static bool g_MouseJustPressed[5] = { false, false, false, false, false };
34  static bool g_InstalledCallbacks = false;
35 
36  // Chain GLFW callbacks: our callbacks will call the user's previously installed callbacks, if any.
37  static GLFWmousebuttonfun g_PrevUserCallbackMousebutton = NULL;
38  static GLFWscrollfun g_PrevUserCallbackScroll = NULL;
39  static GLFWkeyfun g_PrevUserCallbackKey = NULL;
40  static GLFWcharfun g_PrevUserCallbackChar = NULL;
41 
42  static const char* ImGui_ImplGlfw_GetClipboardText(void* user_data)
43  {
44  return glfwGetClipboardString((GLFWwindow*)user_data);
45  }
46 
47  static void ImGui_ImplGlfw_SetClipboardText(void* user_data, const char* text)
48  {
49  glfwSetClipboardString((GLFWwindow*)user_data, text);
50  }
51 
52  void ImGui_ImplGlfw_MouseButtonCallback(GLFWwindow* window, int button, int action, int mods)
53  {
54  if (g_PrevUserCallbackMousebutton != NULL)
55  g_PrevUserCallbackMousebutton(window, button, action, mods);
56 
57  if (action == GLFW_PRESS && button >= 0 && button < IM_ARRAYSIZE(g_MouseJustPressed))
58  g_MouseJustPressed[button] = true;
59  }
60 
61  void ImGui_ImplGlfw_ScrollCallback(GLFWwindow* window, double xoffset, double yoffset)
62  {
63  if (g_PrevUserCallbackScroll != NULL)
64  g_PrevUserCallbackScroll(window, xoffset, yoffset);
65 
66  ImGuiIO& io = ImGui::GetIO();
67  io.MouseWheelH += (float)xoffset;
68  io.MouseWheel += (float)yoffset;
69  }
70 
71  void ImGui_ImplGlfw_CharCallback(GLFWwindow* window, unsigned int c)
72  {
73  if (g_PrevUserCallbackChar != NULL)
74  g_PrevUserCallbackChar(window, c);
75 
76  ImGuiIO& io = ImGui::GetIO();
77  io.AddInputCharacter(c);
78  }
79 
80  void ImGui_ImplGlfw_KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
81  {
82  if (g_PrevUserCallbackKey != NULL)
83  g_PrevUserCallbackKey(window, key, scancode, action, mods);
84 
85  ImGuiIO& io = ImGui::GetIO();
86  if (action == GLFW_PRESS)
87  io.KeysDown[key] = true;
88  if (action == GLFW_RELEASE)
89  io.KeysDown[key] = false;
90 
91  // Modifiers are not reliable across systems
92  io.KeyCtrl = io.KeysDown[GLFW_KEY_LEFT_CONTROL] || io.KeysDown[GLFW_KEY_RIGHT_CONTROL];
93  io.KeyShift = io.KeysDown[GLFW_KEY_LEFT_SHIFT] || io.KeysDown[GLFW_KEY_RIGHT_SHIFT];
94  io.KeyAlt = io.KeysDown[GLFW_KEY_LEFT_ALT] || io.KeysDown[GLFW_KEY_RIGHT_ALT];
95  io.KeySuper = io.KeysDown[GLFW_KEY_LEFT_SUPER] || io.KeysDown[GLFW_KEY_RIGHT_SUPER];
96  }
97 
98  void SetImGuiCallbacks()
99  {
100  if (!ImGui::GetCurrentContext())
101  {
102  ImGui::CreateContext();
103  }
104 
105  ImGuiIO& io = ImGui::GetIO();
106  io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional)
107  io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used)
108  io.BackendPlatformName = "imgui_impl_glfw";
109 
110  io.KeyMap[ImGuiKey_Tab] = FL_KEYCODE_TAB;
111  io.KeyMap[ImGuiKey_LeftArrow] = FL_KEYCODE_LEFT;
112  io.KeyMap[ImGuiKey_RightArrow] = FL_KEYCODE_RIGHT;
113  io.KeyMap[ImGuiKey_UpArrow] = FL_KEYCODE_UP;
114  io.KeyMap[ImGuiKey_DownArrow] = FL_KEYCODE_DOWN;
115  io.KeyMap[ImGuiKey_PageUp] = FL_KEYCODE_PAGE_UP;
116  io.KeyMap[ImGuiKey_PageDown] = FL_KEYCODE_PAGE_DOWN;
117  io.KeyMap[ImGuiKey_Home] = FL_KEYCODE_HOME;
118  io.KeyMap[ImGuiKey_End] = FL_KEYCODE_END;
119  io.KeyMap[ImGuiKey_Insert] = FL_KEYCODE_INSERT;
120  io.KeyMap[ImGuiKey_Delete] = FL_KEYCODE_DELETE;
121  io.KeyMap[ImGuiKey_Backspace] = FL_KEYCODE_BACKSPACE;
122  io.KeyMap[ImGuiKey_Space] = FL_KEYCODE_SPACE;
123  io.KeyMap[ImGuiKey_Enter] = FL_KEYCODE_ENTER;
124  io.KeyMap[ImGuiKey_Escape] = FL_KEYCODE_ESCAPE;
125  io.KeyMap[ImGuiKey_KeyPadEnter] = FL_KEYCODE_KP_ENTER;
126  io.KeyMap[ImGuiKey_A] = FL_KEYCODE_A;
127  io.KeyMap[ImGuiKey_C] = FL_KEYCODE_C;
128  io.KeyMap[ImGuiKey_V] = FL_KEYCODE_V;
129  io.KeyMap[ImGuiKey_X] = FL_KEYCODE_X;
130  io.KeyMap[ImGuiKey_Y] = FL_KEYCODE_Y;
131  io.KeyMap[ImGuiKey_Z] = FL_KEYCODE_Z;
132 
133  DesktopWindow* DesktopWin = static_cast<DesktopWindow*>(VulkanApp::Get().GetCurrentWindow());
134 
135  assert(DesktopWin);
136 
137  io.SetClipboardTextFn = ImGui_ImplGlfw_SetClipboardText;
138  io.GetClipboardTextFn = ImGui_ImplGlfw_GetClipboardText;
139  io.ClipboardUserData = DesktopWin;
140 
141 #if FLING_WINDOWS
142  io.ImeWindowHandle = (void*)glfwGetWin32Window(DesktopWin->GetGlfwWindow());
143 #elif FLING_LINUX
144  io.ImeWindowHandle = (void*)glfwGetX11Window(DesktopWin->GetGlfwWindow());
145 #endif
146  // Chain GLFW callbacks: our callbacks will call the user's previously installed callbacks, if any.
147  g_PrevUserCallbackMousebutton = nullptr;
148  g_PrevUserCallbackScroll = nullptr;
149  g_PrevUserCallbackKey = nullptr;
150  g_PrevUserCallbackChar = nullptr;
151  {
152  GLFWwindow* Window = DesktopWin->GetGlfwWindow();
153  g_InstalledCallbacks = true;
154  g_PrevUserCallbackMousebutton = glfwSetMouseButtonCallback(Window, ImGui_ImplGlfw_MouseButtonCallback);
155  g_PrevUserCallbackScroll = glfwSetScrollCallback(Window, ImGui_ImplGlfw_ScrollCallback);
156  g_PrevUserCallbackKey = glfwSetKeyCallback(Window, ImGui_ImplGlfw_KeyCallback);
157  g_PrevUserCallbackChar = glfwSetCharCallback(Window, ImGui_ImplGlfw_CharCallback);
158  }
159  g_ClientApi = GlfwClientApi_Vulkan;
160  }
161  } // namespace InternalImGui
162 } // namespace Fling
163 #endif
#define FL_KEYCODE_END
Definition: LinuxKeycodes.h:74
#define FL_KEYCODE_C
Definition: LinuxKeycodes.h:29
#define FL_KEYCODE_PAGE_DOWN
Definition: LinuxKeycodes.h:72
#define FL_KEYCODE_TAB
Definition: LinuxKeycodes.h:63
#define FL_KEYCODE_RIGHT
Definition: LinuxKeycodes.h:67
#define FL_KEYCODE_ESCAPE
Definition: LinuxKeycodes.h:61
#define FL_KEYCODE_BACKSPACE
Definition: LinuxKeycodes.h:64
#define FL_KEYCODE_UP
Definition: LinuxKeycodes.h:70
#define FL_KEYCODE_A
Definition: LinuxKeycodes.h:27
#define FL_KEYCODE_DOWN
Definition: LinuxKeycodes.h:69
#define FL_KEYCODE_SPACE
Definition: LinuxKeycodes.h:9
#define FL_KEYCODE_V
Definition: LinuxKeycodes.h:48
#define FL_KEYCODE_HOME
Definition: LinuxKeycodes.h:73
#define FL_KEYCODE_ENTER
Definition: LinuxKeycodes.h:62
#define FL_KEYCODE_DELETE
Definition: LinuxKeycodes.h:66
#define FL_KEYCODE_PAGE_UP
Definition: LinuxKeycodes.h:71
FlingWindow * GetCurrentWindow() const
Definition: VulkanApp.h:57
#define FL_KEYCODE_Y
Definition: LinuxKeycodes.h:51
#define FL_KEYCODE_Z
Definition: LinuxKeycodes.h:52
#define FL_KEYCODE_X
Definition: LinuxKeycodes.h:50
static VulkanApp & Get()
Definition: Singleton.hpp:36
#define FL_KEYCODE_INSERT
Definition: LinuxKeycodes.h:65
#define FL_KEYCODE_KP_ENTER
Definition: LinuxKeycodes.h:120
Definition: Engine.h:29
#define FL_KEYCODE_LEFT
Definition: LinuxKeycodes.h:68