RmlUi_Platform_SFML.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. #if SFML_VERSION_MAJOR >= 3
  37. SystemInterface_SFML::Cursors::Cursors() :
  38. cursor_default(sf::Cursor::Type::Arrow), cursor_move(sf::Cursor::Type::SizeAll), cursor_pointer(sf::Cursor::Type::Hand),
  39. cursor_resize(sf::Cursor::createFromSystem(sf::Cursor::Type::SizeTopLeftBottomRight).value_or(sf::Cursor(sf::Cursor::Type::SizeAll))),
  40. cursor_cross(sf::Cursor::Type::Cross), cursor_text(sf::Cursor::Type::Text), cursor_unavailable(sf::Cursor::Type::NotAllowed)
  41. {}
  42. SystemInterface_SFML::SystemInterface_SFML()
  43. {
  44. try
  45. {
  46. cursors = std::make_unique<Cursors>();
  47. } catch (const sf::Exception& /*exception*/)
  48. {
  49. cursors.reset();
  50. }
  51. }
  52. #else
  53. SystemInterface_SFML::Cursors::Cursors() {}
  54. SystemInterface_SFML::SystemInterface_SFML()
  55. {
  56. cursors = std::make_unique<Cursors>();
  57. bool cursors_valid = true;
  58. cursors_valid &= cursors->cursor_default.loadFromSystem(sf::Cursor::Arrow);
  59. cursors_valid &= cursors->cursor_move.loadFromSystem(sf::Cursor::SizeAll);
  60. cursors_valid &= cursors->cursor_pointer.loadFromSystem(sf::Cursor::Hand);
  61. cursors_valid &=
  62. cursors->cursor_resize.loadFromSystem(sf::Cursor::SizeTopLeftBottomRight) || cursors->cursor_resize.loadFromSystem(sf::Cursor::SizeAll);
  63. cursors_valid &= cursors->cursor_cross.loadFromSystem(sf::Cursor::Cross);
  64. cursors_valid &= cursors->cursor_text.loadFromSystem(sf::Cursor::Text);
  65. cursors_valid &= cursors->cursor_unavailable.loadFromSystem(sf::Cursor::NotAllowed);
  66. if (!cursors_valid)
  67. cursors.reset();
  68. }
  69. #endif
  70. void SystemInterface_SFML::SetWindow(sf::RenderWindow* in_window)
  71. {
  72. window = in_window;
  73. }
  74. double SystemInterface_SFML::GetElapsedTime()
  75. {
  76. return static_cast<double>(timer.getElapsedTime().asMicroseconds()) / 1'000'000.0;
  77. }
  78. void SystemInterface_SFML::SetMouseCursor(const Rml::String& cursor_name)
  79. {
  80. if (cursors && window)
  81. {
  82. sf::Cursor* cursor = nullptr;
  83. if (cursor_name.empty() || cursor_name == "arrow")
  84. cursor = &cursors->cursor_default;
  85. else if (cursor_name == "move")
  86. cursor = &cursors->cursor_move;
  87. else if (cursor_name == "pointer")
  88. cursor = &cursors->cursor_pointer;
  89. else if (cursor_name == "resize")
  90. cursor = &cursors->cursor_resize;
  91. else if (cursor_name == "cross")
  92. cursor = &cursors->cursor_cross;
  93. else if (cursor_name == "text")
  94. cursor = &cursors->cursor_text;
  95. else if (cursor_name == "unavailable")
  96. cursor = &cursors->cursor_unavailable;
  97. else if (Rml::StringUtilities::StartsWith(cursor_name, "rmlui-scroll"))
  98. cursor = &cursors->cursor_move;
  99. if (cursor)
  100. window->setMouseCursor(*cursor);
  101. }
  102. }
  103. void SystemInterface_SFML::SetClipboardText(const Rml::String& text_utf8)
  104. {
  105. sf::Clipboard::setString(text_utf8);
  106. }
  107. void SystemInterface_SFML::GetClipboardText(Rml::String& text)
  108. {
  109. text = sf::Clipboard::getString();
  110. }
  111. bool RmlSFML::InputHandler(Rml::Context* context, const sf::Event& ev)
  112. {
  113. bool result = true;
  114. #if SFML_VERSION_MAJOR >= 3
  115. if (auto mouse_moved = ev.getIf<sf::Event::MouseMoved>())
  116. {
  117. result = context->ProcessMouseMove(mouse_moved->position.x, mouse_moved->position.y, RmlSFML::GetKeyModifierState());
  118. }
  119. else if (auto mouse_pressed = ev.getIf<sf::Event::MouseButtonPressed>())
  120. {
  121. result = context->ProcessMouseButtonDown(int(mouse_pressed->button), RmlSFML::GetKeyModifierState());
  122. }
  123. else if (auto mouse_released = ev.getIf<sf::Event::MouseButtonReleased>())
  124. {
  125. result = context->ProcessMouseButtonUp(int(mouse_released->button), RmlSFML::GetKeyModifierState());
  126. }
  127. else if (auto wheel_scrolled = ev.getIf<sf::Event::MouseWheelScrolled>())
  128. {
  129. const Rml::Vector2f delta = {
  130. wheel_scrolled->wheel == sf::Mouse::Wheel::Horizontal ? -wheel_scrolled->delta : 0.f,
  131. wheel_scrolled->wheel == sf::Mouse::Wheel::Vertical ? -wheel_scrolled->delta : 0.f,
  132. };
  133. result = context->ProcessMouseWheel(delta, RmlSFML::GetKeyModifierState());
  134. }
  135. else if (ev.is<sf::Event::MouseLeft>())
  136. {
  137. result = context->ProcessMouseLeave();
  138. }
  139. else if (auto text_entered = ev.getIf<sf::Event::TextEntered>())
  140. {
  141. Rml::Character character = Rml::Character(text_entered->unicode);
  142. if (character == Rml::Character('\r'))
  143. character = Rml::Character('\n');
  144. if (text_entered->unicode >= 32 || character == Rml::Character('\n'))
  145. result = context->ProcessTextInput(character);
  146. }
  147. else if (auto key_pressed = ev.getIf<sf::Event::KeyPressed>())
  148. {
  149. result = context->ProcessKeyDown(RmlSFML::ConvertKey(key_pressed->code), RmlSFML::GetKeyModifierState());
  150. }
  151. else if (auto key_released = ev.getIf<sf::Event::KeyReleased>())
  152. {
  153. result = context->ProcessKeyUp(RmlSFML::ConvertKey(key_released->code), RmlSFML::GetKeyModifierState());
  154. }
  155. #else
  156. switch (ev.type)
  157. {
  158. case sf::Event::MouseMoved: result = context->ProcessMouseMove(ev.mouseMove.x, ev.mouseMove.y, RmlSFML::GetKeyModifierState()); break;
  159. case sf::Event::MouseButtonPressed: result = context->ProcessMouseButtonDown(ev.mouseButton.button, RmlSFML::GetKeyModifierState()); break;
  160. case sf::Event::MouseButtonReleased: result = context->ProcessMouseButtonUp(ev.mouseButton.button, RmlSFML::GetKeyModifierState()); break;
  161. case sf::Event::MouseWheelMoved: result = context->ProcessMouseWheel(float(-ev.mouseWheel.delta), RmlSFML::GetKeyModifierState()); break;
  162. case sf::Event::MouseLeft: result = context->ProcessMouseLeave(); break;
  163. case sf::Event::TextEntered:
  164. {
  165. Rml::Character character = Rml::Character(ev.text.unicode);
  166. if (character == Rml::Character('\r'))
  167. character = Rml::Character('\n');
  168. if (ev.text.unicode >= 32 || character == Rml::Character('\n'))
  169. result = context->ProcessTextInput(character);
  170. }
  171. break;
  172. case sf::Event::KeyPressed: result = context->ProcessKeyDown(RmlSFML::ConvertKey(ev.key.code), RmlSFML::GetKeyModifierState()); break;
  173. case sf::Event::KeyReleased: result = context->ProcessKeyUp(RmlSFML::ConvertKey(ev.key.code), RmlSFML::GetKeyModifierState()); break;
  174. default: break;
  175. }
  176. #endif
  177. return result;
  178. }
  179. Rml::Input::KeyIdentifier RmlSFML::ConvertKey(sf::Keyboard::Key sfml_key)
  180. {
  181. #if SFML_VERSION_MAJOR >= 3
  182. constexpr auto sfBackspace = sf::Keyboard::Key::Backspace;
  183. constexpr auto sfEnter = sf::Keyboard::Key::Enter;
  184. #else
  185. constexpr auto sfBackspace = sf::Keyboard::Key::BackSpace;
  186. constexpr auto sfEnter = sf::Keyboard::Key::Return;
  187. #endif
  188. // clang-format off
  189. switch (sfml_key)
  190. {
  191. case sf::Keyboard::Key::A: return Rml::Input::KI_A;
  192. case sf::Keyboard::Key::B: return Rml::Input::KI_B;
  193. case sf::Keyboard::Key::C: return Rml::Input::KI_C;
  194. case sf::Keyboard::Key::D: return Rml::Input::KI_D;
  195. case sf::Keyboard::Key::E: return Rml::Input::KI_E;
  196. case sf::Keyboard::Key::F: return Rml::Input::KI_F;
  197. case sf::Keyboard::Key::G: return Rml::Input::KI_G;
  198. case sf::Keyboard::Key::H: return Rml::Input::KI_H;
  199. case sf::Keyboard::Key::I: return Rml::Input::KI_I;
  200. case sf::Keyboard::Key::J: return Rml::Input::KI_J;
  201. case sf::Keyboard::Key::K: return Rml::Input::KI_K;
  202. case sf::Keyboard::Key::L: return Rml::Input::KI_L;
  203. case sf::Keyboard::Key::M: return Rml::Input::KI_M;
  204. case sf::Keyboard::Key::N: return Rml::Input::KI_N;
  205. case sf::Keyboard::Key::O: return Rml::Input::KI_O;
  206. case sf::Keyboard::Key::P: return Rml::Input::KI_P;
  207. case sf::Keyboard::Key::Q: return Rml::Input::KI_Q;
  208. case sf::Keyboard::Key::R: return Rml::Input::KI_R;
  209. case sf::Keyboard::Key::S: return Rml::Input::KI_S;
  210. case sf::Keyboard::Key::T: return Rml::Input::KI_T;
  211. case sf::Keyboard::Key::U: return Rml::Input::KI_U;
  212. case sf::Keyboard::Key::V: return Rml::Input::KI_V;
  213. case sf::Keyboard::Key::W: return Rml::Input::KI_W;
  214. case sf::Keyboard::Key::X: return Rml::Input::KI_X;
  215. case sf::Keyboard::Key::Y: return Rml::Input::KI_Y;
  216. case sf::Keyboard::Key::Z: return Rml::Input::KI_Z;
  217. case sf::Keyboard::Key::Num0: return Rml::Input::KI_0;
  218. case sf::Keyboard::Key::Num1: return Rml::Input::KI_1;
  219. case sf::Keyboard::Key::Num2: return Rml::Input::KI_2;
  220. case sf::Keyboard::Key::Num3: return Rml::Input::KI_3;
  221. case sf::Keyboard::Key::Num4: return Rml::Input::KI_4;
  222. case sf::Keyboard::Key::Num5: return Rml::Input::KI_5;
  223. case sf::Keyboard::Key::Num6: return Rml::Input::KI_6;
  224. case sf::Keyboard::Key::Num7: return Rml::Input::KI_7;
  225. case sf::Keyboard::Key::Num8: return Rml::Input::KI_8;
  226. case sf::Keyboard::Key::Num9: return Rml::Input::KI_9;
  227. case sf::Keyboard::Key::Numpad0: return Rml::Input::KI_NUMPAD0;
  228. case sf::Keyboard::Key::Numpad1: return Rml::Input::KI_NUMPAD1;
  229. case sf::Keyboard::Key::Numpad2: return Rml::Input::KI_NUMPAD2;
  230. case sf::Keyboard::Key::Numpad3: return Rml::Input::KI_NUMPAD3;
  231. case sf::Keyboard::Key::Numpad4: return Rml::Input::KI_NUMPAD4;
  232. case sf::Keyboard::Key::Numpad5: return Rml::Input::KI_NUMPAD5;
  233. case sf::Keyboard::Key::Numpad6: return Rml::Input::KI_NUMPAD6;
  234. case sf::Keyboard::Key::Numpad7: return Rml::Input::KI_NUMPAD7;
  235. case sf::Keyboard::Key::Numpad8: return Rml::Input::KI_NUMPAD8;
  236. case sf::Keyboard::Key::Numpad9: return Rml::Input::KI_NUMPAD9;
  237. case sf::Keyboard::Key::Left: return Rml::Input::KI_LEFT;
  238. case sf::Keyboard::Key::Right: return Rml::Input::KI_RIGHT;
  239. case sf::Keyboard::Key::Up: return Rml::Input::KI_UP;
  240. case sf::Keyboard::Key::Down: return Rml::Input::KI_DOWN;
  241. case sf::Keyboard::Key::Add: return Rml::Input::KI_ADD;
  242. case sfBackspace: return Rml::Input::KI_BACK;
  243. case sf::Keyboard::Key::Delete: return Rml::Input::KI_DELETE;
  244. case sf::Keyboard::Key::Divide: return Rml::Input::KI_DIVIDE;
  245. case sf::Keyboard::Key::End: return Rml::Input::KI_END;
  246. case sf::Keyboard::Key::Escape: return Rml::Input::KI_ESCAPE;
  247. case sf::Keyboard::Key::F1: return Rml::Input::KI_F1;
  248. case sf::Keyboard::Key::F2: return Rml::Input::KI_F2;
  249. case sf::Keyboard::Key::F3: return Rml::Input::KI_F3;
  250. case sf::Keyboard::Key::F4: return Rml::Input::KI_F4;
  251. case sf::Keyboard::Key::F5: return Rml::Input::KI_F5;
  252. case sf::Keyboard::Key::F6: return Rml::Input::KI_F6;
  253. case sf::Keyboard::Key::F7: return Rml::Input::KI_F7;
  254. case sf::Keyboard::Key::F8: return Rml::Input::KI_F8;
  255. case sf::Keyboard::Key::F9: return Rml::Input::KI_F9;
  256. case sf::Keyboard::Key::F10: return Rml::Input::KI_F10;
  257. case sf::Keyboard::Key::F11: return Rml::Input::KI_F11;
  258. case sf::Keyboard::Key::F12: return Rml::Input::KI_F12;
  259. case sf::Keyboard::Key::F13: return Rml::Input::KI_F13;
  260. case sf::Keyboard::Key::F14: return Rml::Input::KI_F14;
  261. case sf::Keyboard::Key::F15: return Rml::Input::KI_F15;
  262. case sf::Keyboard::Key::Home: return Rml::Input::KI_HOME;
  263. case sf::Keyboard::Key::Insert: return Rml::Input::KI_INSERT;
  264. case sf::Keyboard::Key::LControl: return Rml::Input::KI_LCONTROL;
  265. case sf::Keyboard::Key::LShift: return Rml::Input::KI_LSHIFT;
  266. case sf::Keyboard::Key::Multiply: return Rml::Input::KI_MULTIPLY;
  267. case sf::Keyboard::Key::Pause: return Rml::Input::KI_PAUSE;
  268. case sf::Keyboard::Key::RControl: return Rml::Input::KI_RCONTROL;
  269. case sfEnter: return Rml::Input::KI_RETURN;
  270. case sf::Keyboard::Key::RShift: return Rml::Input::KI_RSHIFT;
  271. case sf::Keyboard::Key::Space: return Rml::Input::KI_SPACE;
  272. case sf::Keyboard::Key::Subtract: return Rml::Input::KI_SUBTRACT;
  273. case sf::Keyboard::Key::Tab: return Rml::Input::KI_TAB;
  274. default: break;
  275. }
  276. // clang-format on
  277. return Rml::Input::KI_UNKNOWN;
  278. }
  279. int RmlSFML::GetKeyModifierState()
  280. {
  281. int modifiers = 0;
  282. if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::LShift) || sf::Keyboard::isKeyPressed(sf::Keyboard::Key::RShift))
  283. modifiers |= Rml::Input::KM_SHIFT;
  284. if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::LControl) || sf::Keyboard::isKeyPressed(sf::Keyboard::Key::RControl))
  285. modifiers |= Rml::Input::KM_CTRL;
  286. if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::LAlt) || sf::Keyboard::isKeyPressed(sf::Keyboard::Key::RAlt))
  287. modifiers |= Rml::Input::KM_ALT;
  288. return modifiers;
  289. }