RmlUi_Platform_GLFW.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019-2023 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "RmlUi_Platform_GLFW.h"
  29. #include <RmlUi/Core/Context.h>
  30. #include <RmlUi/Core/Input.h>
  31. #include <RmlUi/Core/Math.h>
  32. #include <RmlUi/Core/StringUtilities.h>
  33. #include <GLFW/glfw3.h>
  34. #define GLFW_HAS_EXTRA_CURSORS (GLFW_VERSION_MAJOR >= 3 && GLFW_VERSION_MINOR >= 4)
  35. SystemInterface_GLFW::SystemInterface_GLFW()
  36. {
  37. cursor_pointer = glfwCreateStandardCursor(GLFW_HAND_CURSOR);
  38. cursor_cross = glfwCreateStandardCursor(GLFW_CROSSHAIR_CURSOR);
  39. cursor_text = glfwCreateStandardCursor(GLFW_IBEAM_CURSOR);
  40. #if GLFW_HAS_EXTRA_CURSORS
  41. cursor_move = glfwCreateStandardCursor(GLFW_RESIZE_ALL_CURSOR);
  42. cursor_resize = glfwCreateStandardCursor(GLFW_RESIZE_NWSE_CURSOR);
  43. cursor_unavailable = glfwCreateStandardCursor(GLFW_NOT_ALLOWED_CURSOR);
  44. #else
  45. cursor_move = cursor_pointer;
  46. cursor_resize = cursor_pointer;
  47. cursor_unavailable = nullptr;
  48. #endif
  49. }
  50. SystemInterface_GLFW::~SystemInterface_GLFW()
  51. {
  52. glfwDestroyCursor(cursor_pointer);
  53. glfwDestroyCursor(cursor_cross);
  54. glfwDestroyCursor(cursor_text);
  55. #if GLFW_HAS_EXTRA_CURSORS
  56. glfwDestroyCursor(cursor_move);
  57. glfwDestroyCursor(cursor_resize);
  58. glfwDestroyCursor(cursor_unavailable);
  59. #endif
  60. }
  61. void SystemInterface_GLFW::SetWindow(GLFWwindow* in_window)
  62. {
  63. window = in_window;
  64. }
  65. double SystemInterface_GLFW::GetElapsedTime()
  66. {
  67. return glfwGetTime();
  68. }
  69. void SystemInterface_GLFW::SetMouseCursor(const Rml::String& cursor_name)
  70. {
  71. GLFWcursor* cursor = nullptr;
  72. if (cursor_name.empty() || cursor_name == "arrow")
  73. cursor = nullptr;
  74. else if (cursor_name == "move")
  75. cursor = cursor_move;
  76. else if (cursor_name == "pointer")
  77. cursor = cursor_pointer;
  78. else if (cursor_name == "resize")
  79. cursor = cursor_resize;
  80. else if (cursor_name == "cross")
  81. cursor = cursor_cross;
  82. else if (cursor_name == "text")
  83. cursor = cursor_text;
  84. else if (cursor_name == "unavailable")
  85. cursor = cursor_unavailable;
  86. else if (Rml::StringUtilities::StartsWith(cursor_name, "rmlui-scroll"))
  87. cursor = cursor_move;
  88. if (window)
  89. glfwSetCursor(window, cursor);
  90. }
  91. void SystemInterface_GLFW::SetClipboardText(const Rml::String& text_utf8)
  92. {
  93. if (window)
  94. glfwSetClipboardString(window, text_utf8.c_str());
  95. }
  96. void SystemInterface_GLFW::GetClipboardText(Rml::String& text)
  97. {
  98. if (window)
  99. text = Rml::String(glfwGetClipboardString(window));
  100. }
  101. bool RmlGLFW::ProcessKeyCallback(Rml::Context* context, int key, int action, int mods)
  102. {
  103. if (!context)
  104. return true;
  105. bool result = true;
  106. switch (action)
  107. {
  108. case GLFW_PRESS:
  109. case GLFW_REPEAT:
  110. result = context->ProcessKeyDown(RmlGLFW::ConvertKey(key), RmlGLFW::ConvertKeyModifiers(mods));
  111. if (key == GLFW_KEY_ENTER || key == GLFW_KEY_KP_ENTER)
  112. result &= context->ProcessTextInput('\n');
  113. break;
  114. case GLFW_RELEASE: result = context->ProcessKeyUp(RmlGLFW::ConvertKey(key), RmlGLFW::ConvertKeyModifiers(mods)); break;
  115. }
  116. return result;
  117. }
  118. bool RmlGLFW::ProcessCharCallback(Rml::Context* context, unsigned int codepoint)
  119. {
  120. if (!context)
  121. return true;
  122. bool result = context->ProcessTextInput((Rml::Character)codepoint);
  123. return result;
  124. }
  125. bool RmlGLFW::ProcessCursorEnterCallback(Rml::Context* context, int entered)
  126. {
  127. if (!context)
  128. return true;
  129. bool result = true;
  130. if (!entered)
  131. result = context->ProcessMouseLeave();
  132. return result;
  133. }
  134. bool RmlGLFW::ProcessCursorPosCallback(Rml::Context* context, GLFWwindow* window, double xpos, double ypos, int mods)
  135. {
  136. if (!context)
  137. return true;
  138. using Rml::Vector2i;
  139. using Vector2d = Rml::Vector2<double>;
  140. Vector2i window_size, framebuffer_size;
  141. glfwGetWindowSize(window, &window_size.x, &window_size.y);
  142. glfwGetFramebufferSize(window, &framebuffer_size.x, &framebuffer_size.y);
  143. // Convert from mouse position in GLFW screen coordinates to framebuffer coordinates (pixels) used by RmlUi.
  144. const Vector2d mouse_pos = Vector2d(xpos, ypos) * (Vector2d(framebuffer_size) / Vector2d(window_size));
  145. const Vector2i mouse_pos_round = {int(Rml::Math::Round(mouse_pos.x)), int(Rml::Math::Round(mouse_pos.y))};
  146. bool result = context->ProcessMouseMove(mouse_pos_round.x, mouse_pos_round.y, RmlGLFW::ConvertKeyModifiers(mods));
  147. return result;
  148. }
  149. bool RmlGLFW::ProcessMouseButtonCallback(Rml::Context* context, int button, int action, int mods)
  150. {
  151. if (!context)
  152. return true;
  153. bool result = true;
  154. switch (action)
  155. {
  156. case GLFW_PRESS: result = context->ProcessMouseButtonDown(button, RmlGLFW::ConvertKeyModifiers(mods)); break;
  157. case GLFW_RELEASE: result = context->ProcessMouseButtonUp(button, RmlGLFW::ConvertKeyModifiers(mods)); break;
  158. }
  159. return result;
  160. }
  161. bool RmlGLFW::ProcessScrollCallback(Rml::Context* context, double yoffset, int mods)
  162. {
  163. if (!context)
  164. return true;
  165. bool result = context->ProcessMouseWheel(-float(yoffset), RmlGLFW::ConvertKeyModifiers(mods));
  166. return result;
  167. }
  168. void RmlGLFW::ProcessFramebufferSizeCallback(Rml::Context* context, int width, int height)
  169. {
  170. if (context)
  171. context->SetDimensions(Rml::Vector2i(width, height));
  172. }
  173. void RmlGLFW::ProcessContentScaleCallback(Rml::Context* context, float xscale)
  174. {
  175. if (context)
  176. context->SetDensityIndependentPixelRatio(xscale);
  177. }
  178. int RmlGLFW::ConvertKeyModifiers(int glfw_mods)
  179. {
  180. int key_modifier_state = 0;
  181. if (GLFW_MOD_SHIFT & glfw_mods)
  182. key_modifier_state |= Rml::Input::KM_SHIFT;
  183. if (GLFW_MOD_CONTROL & glfw_mods)
  184. key_modifier_state |= Rml::Input::KM_CTRL;
  185. if (GLFW_MOD_ALT & glfw_mods)
  186. key_modifier_state |= Rml::Input::KM_ALT;
  187. if (GLFW_MOD_CAPS_LOCK & glfw_mods)
  188. key_modifier_state |= Rml::Input::KM_SCROLLLOCK;
  189. if (GLFW_MOD_NUM_LOCK & glfw_mods)
  190. key_modifier_state |= Rml::Input::KM_NUMLOCK;
  191. return key_modifier_state;
  192. }
  193. Rml::Input::KeyIdentifier RmlGLFW::ConvertKey(int glfw_key)
  194. {
  195. // clang-format off
  196. switch (glfw_key)
  197. {
  198. case GLFW_KEY_A: return Rml::Input::KI_A;
  199. case GLFW_KEY_B: return Rml::Input::KI_B;
  200. case GLFW_KEY_C: return Rml::Input::KI_C;
  201. case GLFW_KEY_D: return Rml::Input::KI_D;
  202. case GLFW_KEY_E: return Rml::Input::KI_E;
  203. case GLFW_KEY_F: return Rml::Input::KI_F;
  204. case GLFW_KEY_G: return Rml::Input::KI_G;
  205. case GLFW_KEY_H: return Rml::Input::KI_H;
  206. case GLFW_KEY_I: return Rml::Input::KI_I;
  207. case GLFW_KEY_J: return Rml::Input::KI_J;
  208. case GLFW_KEY_K: return Rml::Input::KI_K;
  209. case GLFW_KEY_L: return Rml::Input::KI_L;
  210. case GLFW_KEY_M: return Rml::Input::KI_M;
  211. case GLFW_KEY_N: return Rml::Input::KI_N;
  212. case GLFW_KEY_O: return Rml::Input::KI_O;
  213. case GLFW_KEY_P: return Rml::Input::KI_P;
  214. case GLFW_KEY_Q: return Rml::Input::KI_Q;
  215. case GLFW_KEY_R: return Rml::Input::KI_R;
  216. case GLFW_KEY_S: return Rml::Input::KI_S;
  217. case GLFW_KEY_T: return Rml::Input::KI_T;
  218. case GLFW_KEY_U: return Rml::Input::KI_U;
  219. case GLFW_KEY_V: return Rml::Input::KI_V;
  220. case GLFW_KEY_W: return Rml::Input::KI_W;
  221. case GLFW_KEY_X: return Rml::Input::KI_X;
  222. case GLFW_KEY_Y: return Rml::Input::KI_Y;
  223. case GLFW_KEY_Z: return Rml::Input::KI_Z;
  224. case GLFW_KEY_0: return Rml::Input::KI_0;
  225. case GLFW_KEY_1: return Rml::Input::KI_1;
  226. case GLFW_KEY_2: return Rml::Input::KI_2;
  227. case GLFW_KEY_3: return Rml::Input::KI_3;
  228. case GLFW_KEY_4: return Rml::Input::KI_4;
  229. case GLFW_KEY_5: return Rml::Input::KI_5;
  230. case GLFW_KEY_6: return Rml::Input::KI_6;
  231. case GLFW_KEY_7: return Rml::Input::KI_7;
  232. case GLFW_KEY_8: return Rml::Input::KI_8;
  233. case GLFW_KEY_9: return Rml::Input::KI_9;
  234. case GLFW_KEY_BACKSPACE: return Rml::Input::KI_BACK;
  235. case GLFW_KEY_TAB: return Rml::Input::KI_TAB;
  236. case GLFW_KEY_ENTER: return Rml::Input::KI_RETURN;
  237. case GLFW_KEY_PAUSE: return Rml::Input::KI_PAUSE;
  238. case GLFW_KEY_CAPS_LOCK: return Rml::Input::KI_CAPITAL;
  239. case GLFW_KEY_ESCAPE: return Rml::Input::KI_ESCAPE;
  240. case GLFW_KEY_SPACE: return Rml::Input::KI_SPACE;
  241. case GLFW_KEY_PAGE_UP: return Rml::Input::KI_PRIOR;
  242. case GLFW_KEY_PAGE_DOWN: return Rml::Input::KI_NEXT;
  243. case GLFW_KEY_END: return Rml::Input::KI_END;
  244. case GLFW_KEY_HOME: return Rml::Input::KI_HOME;
  245. case GLFW_KEY_LEFT: return Rml::Input::KI_LEFT;
  246. case GLFW_KEY_UP: return Rml::Input::KI_UP;
  247. case GLFW_KEY_RIGHT: return Rml::Input::KI_RIGHT;
  248. case GLFW_KEY_DOWN: return Rml::Input::KI_DOWN;
  249. case GLFW_KEY_PRINT_SCREEN: return Rml::Input::KI_SNAPSHOT;
  250. case GLFW_KEY_INSERT: return Rml::Input::KI_INSERT;
  251. case GLFW_KEY_DELETE: return Rml::Input::KI_DELETE;
  252. case GLFW_KEY_LEFT_SUPER: return Rml::Input::KI_LWIN;
  253. case GLFW_KEY_RIGHT_SUPER: return Rml::Input::KI_RWIN;
  254. case GLFW_KEY_KP_0: return Rml::Input::KI_NUMPAD0;
  255. case GLFW_KEY_KP_1: return Rml::Input::KI_NUMPAD1;
  256. case GLFW_KEY_KP_2: return Rml::Input::KI_NUMPAD2;
  257. case GLFW_KEY_KP_3: return Rml::Input::KI_NUMPAD3;
  258. case GLFW_KEY_KP_4: return Rml::Input::KI_NUMPAD4;
  259. case GLFW_KEY_KP_5: return Rml::Input::KI_NUMPAD5;
  260. case GLFW_KEY_KP_6: return Rml::Input::KI_NUMPAD6;
  261. case GLFW_KEY_KP_7: return Rml::Input::KI_NUMPAD7;
  262. case GLFW_KEY_KP_8: return Rml::Input::KI_NUMPAD8;
  263. case GLFW_KEY_KP_9: return Rml::Input::KI_NUMPAD9;
  264. case GLFW_KEY_KP_ENTER: return Rml::Input::KI_NUMPADENTER;
  265. case GLFW_KEY_KP_MULTIPLY: return Rml::Input::KI_MULTIPLY;
  266. case GLFW_KEY_KP_ADD: return Rml::Input::KI_ADD;
  267. case GLFW_KEY_KP_SUBTRACT: return Rml::Input::KI_SUBTRACT;
  268. case GLFW_KEY_KP_DECIMAL: return Rml::Input::KI_DECIMAL;
  269. case GLFW_KEY_KP_DIVIDE: return Rml::Input::KI_DIVIDE;
  270. case GLFW_KEY_F1: return Rml::Input::KI_F1;
  271. case GLFW_KEY_F2: return Rml::Input::KI_F2;
  272. case GLFW_KEY_F3: return Rml::Input::KI_F3;
  273. case GLFW_KEY_F4: return Rml::Input::KI_F4;
  274. case GLFW_KEY_F5: return Rml::Input::KI_F5;
  275. case GLFW_KEY_F6: return Rml::Input::KI_F6;
  276. case GLFW_KEY_F7: return Rml::Input::KI_F7;
  277. case GLFW_KEY_F8: return Rml::Input::KI_F8;
  278. case GLFW_KEY_F9: return Rml::Input::KI_F9;
  279. case GLFW_KEY_F10: return Rml::Input::KI_F10;
  280. case GLFW_KEY_F11: return Rml::Input::KI_F11;
  281. case GLFW_KEY_F12: return Rml::Input::KI_F12;
  282. case GLFW_KEY_F13: return Rml::Input::KI_F13;
  283. case GLFW_KEY_F14: return Rml::Input::KI_F14;
  284. case GLFW_KEY_F15: return Rml::Input::KI_F15;
  285. case GLFW_KEY_F16: return Rml::Input::KI_F16;
  286. case GLFW_KEY_F17: return Rml::Input::KI_F17;
  287. case GLFW_KEY_F18: return Rml::Input::KI_F18;
  288. case GLFW_KEY_F19: return Rml::Input::KI_F19;
  289. case GLFW_KEY_F20: return Rml::Input::KI_F20;
  290. case GLFW_KEY_F21: return Rml::Input::KI_F21;
  291. case GLFW_KEY_F22: return Rml::Input::KI_F22;
  292. case GLFW_KEY_F23: return Rml::Input::KI_F23;
  293. case GLFW_KEY_F24: return Rml::Input::KI_F24;
  294. case GLFW_KEY_NUM_LOCK: return Rml::Input::KI_NUMLOCK;
  295. case GLFW_KEY_SCROLL_LOCK: return Rml::Input::KI_SCROLL;
  296. case GLFW_KEY_LEFT_SHIFT: return Rml::Input::KI_LSHIFT;
  297. case GLFW_KEY_LEFT_CONTROL: return Rml::Input::KI_LCONTROL;
  298. case GLFW_KEY_RIGHT_SHIFT: return Rml::Input::KI_RSHIFT;
  299. case GLFW_KEY_RIGHT_CONTROL: return Rml::Input::KI_RCONTROL;
  300. case GLFW_KEY_MENU: return Rml::Input::KI_LMENU;
  301. case GLFW_KEY_KP_EQUAL: return Rml::Input::KI_OEM_NEC_EQUAL;
  302. default: break;
  303. }
  304. // clang-format on
  305. return Rml::Input::KI_UNKNOWN;
  306. }