RmlUi_Platform_SFML.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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_SFML.h"
  29. #include <RmlUi/Core/Context.h>
  30. #include <RmlUi/Core/Input.h>
  31. #include <RmlUi/Core/StringUtilities.h>
  32. #include <RmlUi/Core/SystemInterface.h>
  33. #include <SFML/Graphics.hpp>
  34. #include <SFML/System.hpp>
  35. #include <SFML/Window.hpp>
  36. SystemInterface_SFML::SystemInterface_SFML()
  37. {
  38. cursors_valid = true;
  39. cursors_valid &= cursor_default.loadFromSystem(sf::Cursor::Arrow);
  40. cursors_valid &= cursor_move.loadFromSystem(sf::Cursor::SizeAll);
  41. cursors_valid &= cursor_pointer.loadFromSystem(sf::Cursor::Hand);
  42. cursors_valid &= cursor_resize.loadFromSystem(sf::Cursor::SizeTopLeftBottomRight) || cursor_resize.loadFromSystem(sf::Cursor::SizeAll);
  43. cursors_valid &= cursor_cross.loadFromSystem(sf::Cursor::Cross);
  44. cursors_valid &= cursor_text.loadFromSystem(sf::Cursor::Text);
  45. cursors_valid &= cursor_unavailable.loadFromSystem(sf::Cursor::NotAllowed);
  46. }
  47. void SystemInterface_SFML::SetWindow(sf::RenderWindow* in_window)
  48. {
  49. window = in_window;
  50. }
  51. double SystemInterface_SFML::GetElapsedTime()
  52. {
  53. return static_cast<double>(timer.getElapsedTime().asMicroseconds()) / 1'000'000.0;
  54. }
  55. void SystemInterface_SFML::SetMouseCursor(const Rml::String& cursor_name)
  56. {
  57. if (cursors_valid && window)
  58. {
  59. sf::Cursor* cursor = nullptr;
  60. if (cursor_name.empty() || cursor_name == "arrow")
  61. cursor = &cursor_default;
  62. else if (cursor_name == "move")
  63. cursor = &cursor_move;
  64. else if (cursor_name == "pointer")
  65. cursor = &cursor_pointer;
  66. else if (cursor_name == "resize")
  67. cursor = &cursor_resize;
  68. else if (cursor_name == "cross")
  69. cursor = &cursor_cross;
  70. else if (cursor_name == "text")
  71. cursor = &cursor_text;
  72. else if (cursor_name == "unavailable")
  73. cursor = &cursor_unavailable;
  74. else if (Rml::StringUtilities::StartsWith(cursor_name, "rmlui-scroll"))
  75. cursor = &cursor_move;
  76. if (cursor)
  77. window->setMouseCursor(*cursor);
  78. }
  79. }
  80. void SystemInterface_SFML::SetClipboardText(const Rml::String& text_utf8)
  81. {
  82. sf::Clipboard::setString(text_utf8);
  83. }
  84. void SystemInterface_SFML::GetClipboardText(Rml::String& text)
  85. {
  86. text = sf::Clipboard::getString();
  87. }
  88. bool RmlSFML::InputHandler(Rml::Context* context, sf::Event& ev)
  89. {
  90. bool result = true;
  91. switch (ev.type)
  92. {
  93. case sf::Event::MouseMoved: result = context->ProcessMouseMove(ev.mouseMove.x, ev.mouseMove.y, RmlSFML::GetKeyModifierState()); break;
  94. case sf::Event::MouseButtonPressed: result = context->ProcessMouseButtonDown(ev.mouseButton.button, RmlSFML::GetKeyModifierState()); break;
  95. case sf::Event::MouseButtonReleased: result = context->ProcessMouseButtonUp(ev.mouseButton.button, RmlSFML::GetKeyModifierState()); break;
  96. case sf::Event::MouseWheelMoved: result = context->ProcessMouseWheel(float(-ev.mouseWheel.delta), RmlSFML::GetKeyModifierState()); break;
  97. case sf::Event::MouseLeft: result = context->ProcessMouseLeave(); break;
  98. case sf::Event::TextEntered:
  99. {
  100. Rml::Character character = Rml::Character(ev.text.unicode);
  101. if (character == Rml::Character('\r'))
  102. character = Rml::Character('\n');
  103. if (ev.text.unicode >= 32 || character == Rml::Character('\n'))
  104. result = context->ProcessTextInput(character);
  105. }
  106. break;
  107. case sf::Event::KeyPressed: result = context->ProcessKeyDown(RmlSFML::ConvertKey(ev.key.code), RmlSFML::GetKeyModifierState()); break;
  108. case sf::Event::KeyReleased: result = context->ProcessKeyUp(RmlSFML::ConvertKey(ev.key.code), RmlSFML::GetKeyModifierState()); break;
  109. default: break;
  110. }
  111. return result;
  112. }
  113. Rml::Input::KeyIdentifier RmlSFML::ConvertKey(sf::Keyboard::Key sfml_key)
  114. {
  115. // clang-format off
  116. switch (sfml_key)
  117. {
  118. case sf::Keyboard::A: return Rml::Input::KI_A;
  119. case sf::Keyboard::B: return Rml::Input::KI_B;
  120. case sf::Keyboard::C: return Rml::Input::KI_C;
  121. case sf::Keyboard::D: return Rml::Input::KI_D;
  122. case sf::Keyboard::E: return Rml::Input::KI_E;
  123. case sf::Keyboard::F: return Rml::Input::KI_F;
  124. case sf::Keyboard::G: return Rml::Input::KI_G;
  125. case sf::Keyboard::H: return Rml::Input::KI_H;
  126. case sf::Keyboard::I: return Rml::Input::KI_I;
  127. case sf::Keyboard::J: return Rml::Input::KI_J;
  128. case sf::Keyboard::K: return Rml::Input::KI_K;
  129. case sf::Keyboard::L: return Rml::Input::KI_L;
  130. case sf::Keyboard::M: return Rml::Input::KI_M;
  131. case sf::Keyboard::N: return Rml::Input::KI_N;
  132. case sf::Keyboard::O: return Rml::Input::KI_O;
  133. case sf::Keyboard::P: return Rml::Input::KI_P;
  134. case sf::Keyboard::Q: return Rml::Input::KI_Q;
  135. case sf::Keyboard::R: return Rml::Input::KI_R;
  136. case sf::Keyboard::S: return Rml::Input::KI_S;
  137. case sf::Keyboard::T: return Rml::Input::KI_T;
  138. case sf::Keyboard::U: return Rml::Input::KI_U;
  139. case sf::Keyboard::V: return Rml::Input::KI_V;
  140. case sf::Keyboard::W: return Rml::Input::KI_W;
  141. case sf::Keyboard::X: return Rml::Input::KI_X;
  142. case sf::Keyboard::Y: return Rml::Input::KI_Y;
  143. case sf::Keyboard::Z: return Rml::Input::KI_Z;
  144. case sf::Keyboard::Num0: return Rml::Input::KI_0;
  145. case sf::Keyboard::Num1: return Rml::Input::KI_1;
  146. case sf::Keyboard::Num2: return Rml::Input::KI_2;
  147. case sf::Keyboard::Num3: return Rml::Input::KI_3;
  148. case sf::Keyboard::Num4: return Rml::Input::KI_4;
  149. case sf::Keyboard::Num5: return Rml::Input::KI_5;
  150. case sf::Keyboard::Num6: return Rml::Input::KI_6;
  151. case sf::Keyboard::Num7: return Rml::Input::KI_7;
  152. case sf::Keyboard::Num8: return Rml::Input::KI_8;
  153. case sf::Keyboard::Num9: return Rml::Input::KI_9;
  154. case sf::Keyboard::Numpad0: return Rml::Input::KI_NUMPAD0;
  155. case sf::Keyboard::Numpad1: return Rml::Input::KI_NUMPAD1;
  156. case sf::Keyboard::Numpad2: return Rml::Input::KI_NUMPAD2;
  157. case sf::Keyboard::Numpad3: return Rml::Input::KI_NUMPAD3;
  158. case sf::Keyboard::Numpad4: return Rml::Input::KI_NUMPAD4;
  159. case sf::Keyboard::Numpad5: return Rml::Input::KI_NUMPAD5;
  160. case sf::Keyboard::Numpad6: return Rml::Input::KI_NUMPAD6;
  161. case sf::Keyboard::Numpad7: return Rml::Input::KI_NUMPAD7;
  162. case sf::Keyboard::Numpad8: return Rml::Input::KI_NUMPAD8;
  163. case sf::Keyboard::Numpad9: return Rml::Input::KI_NUMPAD9;
  164. case sf::Keyboard::Left: return Rml::Input::KI_LEFT;
  165. case sf::Keyboard::Right: return Rml::Input::KI_RIGHT;
  166. case sf::Keyboard::Up: return Rml::Input::KI_UP;
  167. case sf::Keyboard::Down: return Rml::Input::KI_DOWN;
  168. case sf::Keyboard::Add: return Rml::Input::KI_ADD;
  169. case sf::Keyboard::BackSpace: return Rml::Input::KI_BACK;
  170. case sf::Keyboard::Delete: return Rml::Input::KI_DELETE;
  171. case sf::Keyboard::Divide: return Rml::Input::KI_DIVIDE;
  172. case sf::Keyboard::End: return Rml::Input::KI_END;
  173. case sf::Keyboard::Escape: return Rml::Input::KI_ESCAPE;
  174. case sf::Keyboard::F1: return Rml::Input::KI_F1;
  175. case sf::Keyboard::F2: return Rml::Input::KI_F2;
  176. case sf::Keyboard::F3: return Rml::Input::KI_F3;
  177. case sf::Keyboard::F4: return Rml::Input::KI_F4;
  178. case sf::Keyboard::F5: return Rml::Input::KI_F5;
  179. case sf::Keyboard::F6: return Rml::Input::KI_F6;
  180. case sf::Keyboard::F7: return Rml::Input::KI_F7;
  181. case sf::Keyboard::F8: return Rml::Input::KI_F8;
  182. case sf::Keyboard::F9: return Rml::Input::KI_F9;
  183. case sf::Keyboard::F10: return Rml::Input::KI_F10;
  184. case sf::Keyboard::F11: return Rml::Input::KI_F11;
  185. case sf::Keyboard::F12: return Rml::Input::KI_F12;
  186. case sf::Keyboard::F13: return Rml::Input::KI_F13;
  187. case sf::Keyboard::F14: return Rml::Input::KI_F14;
  188. case sf::Keyboard::F15: return Rml::Input::KI_F15;
  189. case sf::Keyboard::Home: return Rml::Input::KI_HOME;
  190. case sf::Keyboard::Insert: return Rml::Input::KI_INSERT;
  191. case sf::Keyboard::LControl: return Rml::Input::KI_LCONTROL;
  192. case sf::Keyboard::LShift: return Rml::Input::KI_LSHIFT;
  193. case sf::Keyboard::Multiply: return Rml::Input::KI_MULTIPLY;
  194. case sf::Keyboard::Pause: return Rml::Input::KI_PAUSE;
  195. case sf::Keyboard::RControl: return Rml::Input::KI_RCONTROL;
  196. case sf::Keyboard::Return: return Rml::Input::KI_RETURN;
  197. case sf::Keyboard::RShift: return Rml::Input::KI_RSHIFT;
  198. case sf::Keyboard::Space: return Rml::Input::KI_SPACE;
  199. case sf::Keyboard::Subtract: return Rml::Input::KI_SUBTRACT;
  200. case sf::Keyboard::Tab: return Rml::Input::KI_TAB;
  201. default: break;
  202. }
  203. // clang-format on
  204. return Rml::Input::KI_UNKNOWN;
  205. }
  206. int RmlSFML::GetKeyModifierState()
  207. {
  208. int modifiers = 0;
  209. if (sf::Keyboard::isKeyPressed(sf::Keyboard::LShift) || sf::Keyboard::isKeyPressed(sf::Keyboard::RShift))
  210. modifiers |= Rml::Input::KM_SHIFT;
  211. if (sf::Keyboard::isKeyPressed(sf::Keyboard::LControl) || sf::Keyboard::isKeyPressed(sf::Keyboard::RControl))
  212. modifiers |= Rml::Input::KM_CTRL;
  213. if (sf::Keyboard::isKeyPressed(sf::Keyboard::LAlt) || sf::Keyboard::isKeyPressed(sf::Keyboard::RAlt))
  214. modifiers |= Rml::Input::KM_ALT;
  215. return modifiers;
  216. }