main.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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) 2018 Michael R. P. Ragazzon
  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/Core.h>
  29. #include <RmlUi/Controls.h>
  30. #include <RmlUi/Debugger.h>
  31. #include <Input.h>
  32. #include <Shell.h>
  33. #include <ShellRenderInterfaceOpenGL.h>
  34. #include <RmlUi/Core/TransformPrimitive.h>
  35. #include <sstream>
  36. class DemoWindow : public Rml::Core::EventListener
  37. {
  38. public:
  39. DemoWindow(const Rml::Core::String &title, const Rml::Core::Vector2f &position, Rml::Core::Context *context)
  40. {
  41. using namespace Rml::Core;
  42. document = context->LoadDocument("basic/demo/data/demo.rml");
  43. if (document != nullptr)
  44. {
  45. {
  46. document->GetElementById("title")->SetInnerRML(title);
  47. document->SetProperty(PropertyId::Left, Property(position.x, Property::PX));
  48. document->SetProperty(PropertyId::Top, Property(position.y, Property::PX));
  49. }
  50. document->Show();
  51. }
  52. }
  53. void Shutdown() {
  54. if (document)
  55. {
  56. document->Close();
  57. document = nullptr;
  58. }
  59. }
  60. void ProcessEvent(Rml::Core::Event& event) override
  61. {
  62. using namespace Rml::Core;
  63. switch (event.GetId())
  64. {
  65. case EventId::Keydown:
  66. {
  67. Rml::Core::Input::KeyIdentifier key_identifier = (Rml::Core::Input::KeyIdentifier) event.GetParameter< int >("key_identifier", 0);
  68. bool ctrl_key = event.GetParameter< bool >("ctrl_key", false);
  69. if (key_identifier == Rml::Core::Input::KI_ESCAPE)
  70. {
  71. Shell::RequestExit();
  72. }
  73. else if (key_identifier == Rml::Core::Input::KI_F8)
  74. {
  75. Rml::Debugger::SetVisible(!Rml::Debugger::IsVisible());
  76. }
  77. }
  78. break;
  79. default:
  80. break;
  81. }
  82. }
  83. Rml::Core::ElementDocument * GetDocument() {
  84. return document;
  85. }
  86. private:
  87. Rml::Core::ElementDocument *document;
  88. };
  89. Rml::Core::Context* context = nullptr;
  90. ShellRenderInterfaceExtensions *shell_renderer;
  91. void GameLoop()
  92. {
  93. context->Update();
  94. shell_renderer->PrepareRenderBuffer();
  95. context->Render();
  96. shell_renderer->PresentRenderBuffer();
  97. }
  98. class DemoEventListener : public Rml::Core::EventListener
  99. {
  100. public:
  101. DemoEventListener(const Rml::Core::String& value, Rml::Core::Element* element) : value(value), element(element) {}
  102. void ProcessEvent(Rml::Core::Event& event) override
  103. {
  104. using namespace Rml::Core;
  105. if (value == "exit")
  106. {
  107. // Test replacing the current element.
  108. // Need to be careful with regard to lifetime issues. The event's current element will be destroyed, so we cannot
  109. // use it after SetInnerRml(). The library should handle this case safely internally when propagating the event further.
  110. auto parent = element->GetParentNode();
  111. parent->SetInnerRML("<button onclick='confirm_exit' onblur='cancel_exit' onmouseout='cancel_exit'>Are you sure?</button>");
  112. if (auto child = parent->GetChild(0))
  113. child->Focus();
  114. }
  115. else if (value == "confirm_exit")
  116. {
  117. Shell::RequestExit();
  118. }
  119. else if (value == "cancel_exit")
  120. {
  121. if(auto parent = element->GetParentNode())
  122. parent->SetInnerRML("<button id='exit' onclick='exit'>Exit</button>");
  123. }
  124. }
  125. void OnDetach(Rml::Core::Element* element) override { delete this; }
  126. private:
  127. Rml::Core::String value;
  128. Rml::Core::Element* element;
  129. };
  130. class DemoEventListenerInstancer : public Rml::Core::EventListenerInstancer
  131. {
  132. public:
  133. Rml::Core::EventListener* InstanceEventListener(const Rml::Core::String& value, Rml::Core::Element* element) override
  134. {
  135. return new DemoEventListener(value, element);
  136. }
  137. };
  138. #if defined RMLUI_PLATFORM_WIN32
  139. #include <windows.h>
  140. int APIENTRY WinMain(HINSTANCE RMLUI_UNUSED_PARAMETER(instance_handle), HINSTANCE RMLUI_UNUSED_PARAMETER(previous_instance_handle), char* RMLUI_UNUSED_PARAMETER(command_line), int RMLUI_UNUSED_PARAMETER(command_show))
  141. #else
  142. int main(int RMLUI_UNUSED_PARAMETER(argc), char** RMLUI_UNUSED_PARAMETER(argv))
  143. #endif
  144. {
  145. #ifdef RMLUI_PLATFORM_WIN32
  146. RMLUI_UNUSED(instance_handle);
  147. RMLUI_UNUSED(previous_instance_handle);
  148. RMLUI_UNUSED(command_line);
  149. RMLUI_UNUSED(command_show);
  150. #else
  151. RMLUI_UNUSED(argc);
  152. RMLUI_UNUSED(argv);
  153. #endif
  154. const int width = 1600;
  155. const int height = 950;
  156. ShellRenderInterfaceOpenGL opengl_renderer;
  157. shell_renderer = &opengl_renderer;
  158. // Generic OS initialisation, creates a window and attaches OpenGL.
  159. if (!Shell::Initialise() ||
  160. !Shell::OpenWindow("Demo Sample", shell_renderer, width, height, true))
  161. {
  162. Shell::Shutdown();
  163. return -1;
  164. }
  165. // RmlUi initialisation.
  166. Rml::Core::SetRenderInterface(&opengl_renderer);
  167. opengl_renderer.SetViewport(width, height);
  168. ShellSystemInterface system_interface;
  169. Rml::Core::SetSystemInterface(&system_interface);
  170. Rml::Core::Initialise();
  171. // Create the main RmlUi context and set it on the shell's input layer.
  172. context = Rml::Core::CreateContext("main", Rml::Core::Vector2i(width, height));
  173. if (context == nullptr)
  174. {
  175. Rml::Core::Shutdown();
  176. Shell::Shutdown();
  177. return -1;
  178. }
  179. Rml::Controls::Initialise();
  180. Rml::Debugger::Initialise(context);
  181. Input::SetContext(context);
  182. shell_renderer->SetContext(context);
  183. context->SetDensityIndependentPixelRatio(1.0f);
  184. DemoEventListenerInstancer event_listener_instancer;
  185. Rml::Core::Factory::RegisterEventListenerInstancer(&event_listener_instancer);
  186. Shell::LoadFonts("assets/");
  187. auto window = std::make_unique<DemoWindow>("Demo sample", Rml::Core::Vector2f(150, 80), context);
  188. window->GetDocument()->AddEventListener(Rml::Core::EventId::Keydown, window.get());
  189. window->GetDocument()->AddEventListener(Rml::Core::EventId::Keyup, window.get());
  190. window->GetDocument()->AddEventListener(Rml::Core::EventId::Animationend, window.get());
  191. Shell::EventLoop(GameLoop);
  192. window->Shutdown();
  193. // Shutdown RmlUi.
  194. Rml::Core::Shutdown();
  195. Shell::CloseWindow();
  196. Shell::Shutdown();
  197. window.reset();
  198. return 0;
  199. }