RmlUi_Platform_SFML.cpp 9.9 KB

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