RmlUi_Backend_GLFW_GL2.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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_Backend.h"
  29. #include "RmlUi_Platform_GLFW.h"
  30. #include "RmlUi_Renderer_GL2.h"
  31. #include <RmlUi/Core/Context.h>
  32. #include <RmlUi/Core/Input.h>
  33. #include <RmlUi/Core/Profiling.h>
  34. #include <GLFW/glfw3.h>
  35. static void SetupCallbacks(GLFWwindow* window);
  36. static void LogErrorFromGLFW(int error, const char* description)
  37. {
  38. Rml::Log::Message(Rml::Log::LT_ERROR, "GLFW error (0x%x): %s", error, description);
  39. }
  40. /**
  41. Global data used by this backend.
  42. Lifetime governed by the calls to Backend::Initialize() and Backend::Shutdown().
  43. */
  44. struct BackendData {
  45. SystemInterface_GLFW system_interface;
  46. RenderInterface_GL2 render_interface;
  47. GLFWwindow* window = nullptr;
  48. int glfw_active_modifiers = 0;
  49. bool context_dimensions_dirty = true;
  50. // Arguments set during event processing and nulled otherwise.
  51. Rml::Context* context = nullptr;
  52. KeyDownCallback key_down_callback = nullptr;
  53. };
  54. static Rml::UniquePtr<BackendData> data;
  55. bool Backend::Initialize(const char* name, int width, int height, bool allow_resize)
  56. {
  57. RMLUI_ASSERT(!data);
  58. glfwSetErrorCallback(LogErrorFromGLFW);
  59. if (!glfwInit())
  60. return false;
  61. // Set window hints for OpenGL 2 context creation.
  62. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
  63. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
  64. glfwWindowHint(GLFW_DOUBLEBUFFER, GLFW_TRUE);
  65. // Request stencil buffer of at least 8-bit size to supporting clipping on transformed elements.
  66. glfwWindowHint(GLFW_STENCIL_BITS, 8);
  67. // Enable MSAA for better-looking visuals, especially when transforms are applied.
  68. glfwWindowHint(GLFW_SAMPLES, 2);
  69. // Apply window properties and create it.
  70. glfwWindowHint(GLFW_RESIZABLE, allow_resize ? GLFW_TRUE : GLFW_FALSE);
  71. glfwWindowHint(GLFW_SCALE_TO_MONITOR, GLFW_TRUE);
  72. GLFWwindow* window = glfwCreateWindow(width, height, name, nullptr, nullptr);
  73. if (!window)
  74. return false;
  75. glfwMakeContextCurrent(window);
  76. glfwSwapInterval(1);
  77. data = Rml::MakeUnique<BackendData>();
  78. data->window = window;
  79. data->system_interface.SetWindow(window);
  80. // The window size may have been scaled by DPI settings, get the actual pixel size.
  81. glfwGetFramebufferSize(window, &width, &height);
  82. data->render_interface.SetViewport(width, height);
  83. // Receive num lock and caps lock modifiers for proper handling of numpad inputs in text fields.
  84. glfwSetInputMode(window, GLFW_LOCK_KEY_MODS, GLFW_TRUE);
  85. // Setup the input and window event callback functions.
  86. SetupCallbacks(window);
  87. return true;
  88. }
  89. void Backend::Shutdown()
  90. {
  91. RMLUI_ASSERT(data);
  92. glfwDestroyWindow(data->window);
  93. data.reset();
  94. glfwTerminate();
  95. }
  96. Rml::SystemInterface* Backend::GetSystemInterface()
  97. {
  98. RMLUI_ASSERT(data);
  99. return &data->system_interface;
  100. }
  101. Rml::RenderInterface* Backend::GetRenderInterface()
  102. {
  103. RMLUI_ASSERT(data);
  104. return &data->render_interface;
  105. }
  106. bool Backend::ProcessEvents(Rml::Context* context, KeyDownCallback key_down_callback, bool power_save)
  107. {
  108. RMLUI_ASSERT(data && context);
  109. // The initial window size may have been affected by system DPI settings, apply the actual pixel size and dp-ratio to the context.
  110. if (data->context_dimensions_dirty)
  111. {
  112. data->context_dimensions_dirty = false;
  113. Rml::Vector2i window_size;
  114. float dp_ratio = 1.f;
  115. glfwGetFramebufferSize(data->window, &window_size.x, &window_size.y);
  116. glfwGetWindowContentScale(data->window, &dp_ratio, nullptr);
  117. context->SetDimensions(window_size);
  118. context->SetDensityIndependentPixelRatio(dp_ratio);
  119. }
  120. data->context = context;
  121. data->key_down_callback = key_down_callback;
  122. if (power_save)
  123. glfwWaitEventsTimeout(Rml::Math::Min(context->GetNextUpdateDelay(), 10.0));
  124. else
  125. glfwPollEvents();
  126. data->context = nullptr;
  127. data->key_down_callback = nullptr;
  128. const bool result = !glfwWindowShouldClose(data->window);
  129. glfwSetWindowShouldClose(data->window, GLFW_FALSE);
  130. return result;
  131. }
  132. void Backend::RequestExit()
  133. {
  134. RMLUI_ASSERT(data);
  135. glfwSetWindowShouldClose(data->window, GLFW_TRUE);
  136. }
  137. void Backend::BeginFrame()
  138. {
  139. RMLUI_ASSERT(data);
  140. data->render_interface.BeginFrame();
  141. data->render_interface.Clear();
  142. }
  143. void Backend::PresentFrame()
  144. {
  145. RMLUI_ASSERT(data);
  146. data->render_interface.EndFrame();
  147. glfwSwapBuffers(data->window);
  148. // Optional, used to mark frames during performance profiling.
  149. RMLUI_FrameMark;
  150. }
  151. static void SetupCallbacks(GLFWwindow* window)
  152. {
  153. RMLUI_ASSERT(data);
  154. // Key input
  155. glfwSetKeyCallback(window, [](GLFWwindow* /*window*/, int glfw_key, int /*scancode*/, int glfw_action, int glfw_mods) {
  156. if (!data->context)
  157. return;
  158. // Store the active modifiers for later because GLFW doesn't provide them in the callbacks to the mouse input events.
  159. data->glfw_active_modifiers = glfw_mods;
  160. // Override the default key event callback to add global shortcuts for the samples.
  161. Rml::Context* context = data->context;
  162. KeyDownCallback key_down_callback = data->key_down_callback;
  163. switch (glfw_action)
  164. {
  165. case GLFW_PRESS:
  166. case GLFW_REPEAT:
  167. {
  168. const Rml::Input::KeyIdentifier key = RmlGLFW::ConvertKey(glfw_key);
  169. const int key_modifier = RmlGLFW::ConvertKeyModifiers(glfw_mods);
  170. float dp_ratio = 1.f;
  171. glfwGetWindowContentScale(data->window, &dp_ratio, nullptr);
  172. // See if we have any global shortcuts that take priority over the context.
  173. if (key_down_callback && !key_down_callback(context, key, key_modifier, dp_ratio, true))
  174. break;
  175. // Otherwise, hand the event over to the context by calling the input handler as normal.
  176. if (!RmlGLFW::ProcessKeyCallback(context, glfw_key, glfw_action, glfw_mods))
  177. break;
  178. // The key was not consumed by the context either, try keyboard shortcuts of lower priority.
  179. if (key_down_callback && !key_down_callback(context, key, key_modifier, dp_ratio, false))
  180. break;
  181. }
  182. break;
  183. case GLFW_RELEASE: RmlGLFW::ProcessKeyCallback(context, glfw_key, glfw_action, glfw_mods); break;
  184. }
  185. });
  186. glfwSetCharCallback(window, [](GLFWwindow* /*window*/, unsigned int codepoint) { RmlGLFW::ProcessCharCallback(data->context, codepoint); });
  187. glfwSetCursorEnterCallback(window, [](GLFWwindow* /*window*/, int entered) { RmlGLFW::ProcessCursorEnterCallback(data->context, entered); });
  188. // Mouse input
  189. glfwSetCursorPosCallback(window, [](GLFWwindow* window, double xpos, double ypos) {
  190. RmlGLFW::ProcessCursorPosCallback(data->context, window, xpos, ypos, data->glfw_active_modifiers);
  191. });
  192. glfwSetMouseButtonCallback(window, [](GLFWwindow* /*window*/, int button, int action, int mods) {
  193. data->glfw_active_modifiers = mods;
  194. RmlGLFW::ProcessMouseButtonCallback(data->context, button, action, mods);
  195. });
  196. glfwSetScrollCallback(window, [](GLFWwindow* /*window*/, double /*xoffset*/, double yoffset) {
  197. RmlGLFW::ProcessScrollCallback(data->context, yoffset, data->glfw_active_modifiers);
  198. });
  199. // Window events
  200. glfwSetFramebufferSizeCallback(window, [](GLFWwindow* /*window*/, int width, int height) {
  201. data->render_interface.SetViewport(width, height);
  202. RmlGLFW::ProcessFramebufferSizeCallback(data->context, width, height);
  203. });
  204. glfwSetWindowContentScaleCallback(window,
  205. [](GLFWwindow* /*window*/, float xscale, float /*yscale*/) { RmlGLFW::ProcessContentScaleCallback(data->context, xscale); });
  206. }