main.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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
  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/animation/data/animation.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. //document->Animate("opacity", Property(0.0f, Property::NUMBER), 2.0f, Tween{ Tween::Quadratic, Tween::InOut }, -1, true, 1.0f);
  50. }
  51. // Button fun
  52. {
  53. auto el = document->GetElementById("start_game");
  54. auto p1 = Transform::MakeProperty({ Transforms::Rotate2D{10.f}, Transforms::TranslateX{100.f} });
  55. auto p2 = Transform::MakeProperty({ Transforms::Scale2D{3.f} });
  56. el->Animate("transform", p1, 1.8f, Tween{ Tween::Elastic, Tween::InOut }, -1, true);
  57. el->AddAnimationKey("transform", p2, 1.3f, Tween{ Tween::Elastic, Tween::InOut });
  58. }
  59. {
  60. auto el = document->GetElementById("high_scores");
  61. el->Animate("margin-left", Property(0.f, Property::PX), 0.3f, Tween{ Tween::Sine, Tween::In }, 10, true, 1.f);
  62. el->AddAnimationKey("margin-left", Property(100.f, Property::PX), 3.0f, Tween{ Tween::Circular, Tween::Out });
  63. }
  64. {
  65. auto el = document->GetElementById("options");
  66. el->Animate("image-color", Property(Colourb(128, 255, 255, 255), Property::COLOUR), 0.3f, Tween{}, -1, false);
  67. el->AddAnimationKey("image-color", Property(Colourb(128, 128, 255, 255), Property::COLOUR), 0.3f);
  68. el->AddAnimationKey("image-color", Property(Colourb(0, 128, 128, 255), Property::COLOUR), 0.3f);
  69. el->AddAnimationKey("image-color", Property(Colourb(64, 128, 255, 0), Property::COLOUR), 0.9f);
  70. el->AddAnimationKey("image-color", Property(Colourb(255, 255, 255, 255), Property::COLOUR), 0.3f);
  71. }
  72. {
  73. auto el = document->GetElementById("help");
  74. el->Animate("margin-left", Property(100.f, Property::PX), 1.0f, Tween{ Tween::Quadratic, Tween::InOut }, -1, true);
  75. }
  76. {
  77. auto el = document->GetElementById("exit");
  78. PropertyDictionary pd;
  79. StyleSheetSpecification::ParsePropertyDeclaration(pd, "transform", "translate(200px, 200px) rotate(1215deg)");
  80. el->Animate("transform", *pd.GetProperty(PropertyId::Transform), 3.f, Tween{ Tween::Bounce, Tween::Out }, -1);
  81. }
  82. // Transform tests
  83. {
  84. auto el = document->GetElementById("generic");
  85. auto p = Transform::MakeProperty({ Transforms::TranslateY{50, Property::PX}, Transforms::Rotate3D{0.8f, 0, 1, 110, Property::DEG}});
  86. el->Animate("transform", p, 1.3f, Tween{Tween::Quadratic, Tween::InOut}, -1, true);
  87. }
  88. {
  89. auto el = document->GetElementById("combine");
  90. auto p = Transform::MakeProperty({ Transforms::Translate2D{50, 50, Property::PX}, Transforms::Rotate2D(1215) });
  91. el->Animate("transform", p, 8.0f, Tween{}, -1, true);
  92. }
  93. {
  94. auto el = document->GetElementById("decomposition");
  95. auto p = Transform::MakeProperty({ Transforms::Translate2D{50, 50, Property::PX}, Transforms::Rotate2D(1215) });
  96. el->Animate("transform", p, 8.0f, Tween{}, -1, true);
  97. }
  98. // Mixed units tests
  99. {
  100. auto el = document->GetElementById("abs_rel");
  101. el->Animate("margin-left", Property(50.f, Property::PERCENT), 1.5f, Tween{}, -1, true);
  102. }
  103. {
  104. auto el = document->GetElementById("abs_rel_transform");
  105. auto p = Transform::MakeProperty({ Transforms::TranslateX{0, Property::PX} });
  106. el->Animate("transform", p, 1.5f, Tween{}, -1, true);
  107. }
  108. {
  109. auto el = document->GetElementById("animation_event");
  110. el->Animate("top", Property(Math::RandomReal(250.f), Property::PX), 1.5f, Tween{ Tween::Cubic, Tween::InOut });
  111. el->Animate("left", Property(Math::RandomReal(250.f), Property::PX), 1.5f, Tween{ Tween::Cubic, Tween::InOut });
  112. }
  113. document->Show();
  114. }
  115. }
  116. ~DemoWindow()
  117. {
  118. if (document)
  119. {
  120. document->Close();
  121. }
  122. }
  123. Rml::Core::ElementDocument * GetDocument() {
  124. return document;
  125. }
  126. private:
  127. Rml::Core::ElementDocument *document;
  128. };
  129. Rml::Core::Context* context = nullptr;
  130. ShellRenderInterfaceExtensions *shell_renderer;
  131. DemoWindow* window = nullptr;
  132. bool run_loop = true;
  133. bool single_loop = false;
  134. int nudge = 0;
  135. void GameLoop()
  136. {
  137. if(run_loop || single_loop)
  138. {
  139. context->Update();
  140. shell_renderer->PrepareRenderBuffer();
  141. context->Render();
  142. shell_renderer->PresentRenderBuffer();
  143. single_loop = false;
  144. }
  145. static double t_prev = 0.0f;
  146. double t = Shell::GetElapsedTime();
  147. float dt = float(t - t_prev);
  148. static int count_frames = 0;
  149. count_frames += 1;
  150. if(nudge)
  151. {
  152. t_prev = t;
  153. static float ff = 0.0f;
  154. ff += float(nudge)*0.3f;
  155. auto el = window->GetDocument()->GetElementById("exit");
  156. auto f = el->GetProperty<float>("margin-left");
  157. el->SetProperty(Rml::Core::PropertyId::MarginLeft, Rml::Core::Property(ff, Rml::Core::Property::PX));
  158. float f_left = el->GetAbsoluteLeft();
  159. Rml::Core::Log::Message(Rml::Core::Log::LT_INFO, "margin-left: '%f' abs: %f.", ff, f_left);
  160. nudge = 0;
  161. }
  162. if (window && dt > 0.2f)
  163. {
  164. t_prev = t;
  165. auto el = window->GetDocument()->GetElementById("fps");
  166. float fps = float(count_frames) / dt;
  167. count_frames = 0;
  168. el->SetInnerRML(Rml::Core::CreateString( 20, "FPS: %f", fps ));
  169. }
  170. }
  171. class Event : public Rml::Core::EventListener
  172. {
  173. public:
  174. Event(const Rml::Core::String& value) : value(value) {}
  175. void ProcessEvent(Rml::Core::Event& event) override
  176. {
  177. using namespace Rml::Core;
  178. if(value == "exit")
  179. Shell::RequestExit();
  180. switch (event.GetId())
  181. {
  182. case EventId::Keydown:
  183. {
  184. Rml::Core::Input::KeyIdentifier key_identifier = (Rml::Core::Input::KeyIdentifier) event.GetParameter< int >("key_identifier", 0);
  185. if (key_identifier == Rml::Core::Input::KI_SPACE)
  186. {
  187. run_loop = !run_loop;
  188. }
  189. else if (key_identifier == Rml::Core::Input::KI_RETURN)
  190. {
  191. run_loop = false;
  192. single_loop = true;
  193. }
  194. else if (key_identifier == Rml::Core::Input::KI_OEM_PLUS)
  195. {
  196. nudge = 1;
  197. }
  198. else if (key_identifier == Rml::Core::Input::KI_OEM_MINUS)
  199. {
  200. nudge = -1;
  201. }
  202. else if (key_identifier == Rml::Core::Input::KI_ESCAPE)
  203. {
  204. Shell::RequestExit();
  205. }
  206. else if (key_identifier == Rml::Core::Input::KI_F8)
  207. {
  208. Rml::Debugger::SetVisible(!Rml::Debugger::IsVisible());
  209. }
  210. else if (key_identifier == Rml::Core::Input::KI_LEFT)
  211. {
  212. auto el = context->GetRootElement()->GetElementById("keyevent_response");
  213. if (el) el->Animate("left", Property{ -200.f, Property::PX }, 0.5, Tween{ Tween::Cubic });
  214. }
  215. else if (key_identifier == Rml::Core::Input::KI_RIGHT)
  216. {
  217. auto el = context->GetRootElement()->GetElementById("keyevent_response");
  218. if (el) el->Animate("left", Property{ 200.f, Property::PX }, 0.5, Tween{ Tween::Cubic });
  219. }
  220. else if (key_identifier == Rml::Core::Input::KI_UP)
  221. {
  222. auto el = context->GetRootElement()->GetElementById("keyevent_response");
  223. auto offset_right = Property{ 200.f, Property::PX };
  224. if (el) el->Animate("left", Property{ 0.f, Property::PX }, 0.5, Tween{ Tween::Cubic }, 1, true, 0, &offset_right);
  225. }
  226. else if (key_identifier == Rml::Core::Input::KI_DOWN)
  227. {
  228. auto el = context->GetRootElement()->GetElementById("keyevent_response");
  229. if (el) el->Animate("left", Property{ 0.f, Property::PX }, 0.5, Tween{ Tween::Cubic });
  230. }
  231. }
  232. break;
  233. case EventId::Click:
  234. {
  235. auto el = event.GetTargetElement();
  236. if (el->GetId() == "transition_class")
  237. {
  238. el->SetClass("move_me", !el->IsClassSet("move_me"));
  239. }
  240. }
  241. break;
  242. case EventId::Animationend:
  243. {
  244. auto el = event.GetTargetElement();
  245. if (el->GetId() == "animation_event")
  246. {
  247. el->Animate("top", Property(Math::RandomReal(200.f), Property::PX), 1.2f, Tween{ Tween::Cubic, Tween::InOut });
  248. el->Animate("left", Property(Math::RandomReal(100.f), Property::PERCENT), 0.8f, Tween{ Tween::Cubic, Tween::InOut });
  249. }
  250. }
  251. break;
  252. default:
  253. break;
  254. }
  255. }
  256. void OnDetach(Rml::Core::Element* element) override { delete this; }
  257. private:
  258. Rml::Core::String value;
  259. };
  260. class EventInstancer : public Rml::Core::EventListenerInstancer
  261. {
  262. public:
  263. /// Instances a new event handle for Invaders.
  264. Rml::Core::EventListener* InstanceEventListener(const Rml::Core::String& value, Rml::Core::Element* element) override
  265. {
  266. return new Event(value);
  267. }
  268. };
  269. #if defined RMLUI_PLATFORM_WIN32
  270. #include <windows.h>
  271. 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))
  272. #else
  273. int main(int RMLUI_UNUSED_PARAMETER(argc), char** RMLUI_UNUSED_PARAMETER(argv))
  274. #endif
  275. {
  276. #ifdef RMLUI_PLATFORM_WIN32
  277. RMLUI_UNUSED(instance_handle);
  278. RMLUI_UNUSED(previous_instance_handle);
  279. RMLUI_UNUSED(command_line);
  280. RMLUI_UNUSED(command_show);
  281. #else
  282. RMLUI_UNUSED(argc);
  283. RMLUI_UNUSED(argv);
  284. #endif
  285. const int width = 1800;
  286. const int height = 1000;
  287. ShellRenderInterfaceOpenGL opengl_renderer;
  288. shell_renderer = &opengl_renderer;
  289. // Generic OS initialisation, creates a window and attaches OpenGL.
  290. if (!Shell::Initialise() ||
  291. !Shell::OpenWindow("Animation Sample", shell_renderer, width, height, true))
  292. {
  293. Shell::Shutdown();
  294. return -1;
  295. }
  296. // RmlUi initialisation.
  297. Rml::Core::SetRenderInterface(&opengl_renderer);
  298. opengl_renderer.SetViewport(width, height);
  299. ShellSystemInterface system_interface;
  300. Rml::Core::SetSystemInterface(&system_interface);
  301. Rml::Core::Initialise();
  302. // Create the main RmlUi context and set it on the shell's input layer.
  303. context = Rml::Core::CreateContext("main", Rml::Core::Vector2i(width, height));
  304. if (context == nullptr)
  305. {
  306. Rml::Core::Shutdown();
  307. Shell::Shutdown();
  308. return -1;
  309. }
  310. Rml::Controls::Initialise();
  311. Rml::Debugger::Initialise(context);
  312. Input::SetContext(context);
  313. shell_renderer->SetContext(context);
  314. EventInstancer event_listener_instancer;
  315. Rml::Core::Factory::RegisterEventListenerInstancer(&event_listener_instancer);
  316. Shell::LoadFonts("assets/");
  317. window = new DemoWindow("Animation sample", Rml::Core::Vector2f(81, 100), context);
  318. window->GetDocument()->AddEventListener(Rml::Core::EventId::Keydown, new Event("hello"));
  319. window->GetDocument()->AddEventListener(Rml::Core::EventId::Keyup, new Event("hello"));
  320. window->GetDocument()->AddEventListener(Rml::Core::EventId::Animationend, new Event("hello"));
  321. Shell::EventLoop(GameLoop);
  322. delete window;
  323. // Shutdown RmlUi.
  324. Rml::Core::Shutdown();
  325. Shell::CloseWindow();
  326. Shell::Shutdown();
  327. return 0;
  328. }