main.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2018 Michael Ragazzon
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include <Rocket/Core.h>
  28. #include <Rocket/Controls.h>
  29. #include <Rocket/Debugger.h>
  30. #include <Input.h>
  31. #include <Shell.h>
  32. #include <Rocket/Core/TransformPrimitive.h>
  33. #include <cmath>
  34. #include <sstream>
  35. // Animations TODO:
  36. // - Update transform animations / resolve keys again when parent box size changes.
  37. // - [offtopic] Improve performance of transform parser (hashtable)
  38. // - [offtopic] Use double for absolute time, get and cache time for each render/update loop
  39. class DemoWindow
  40. {
  41. public:
  42. DemoWindow(const Rocket::Core::String &title, const Rocket::Core::Vector2f &position, Rocket::Core::Context *context)
  43. {
  44. using namespace Rocket::Core;
  45. document = context->LoadDocument("basic/animation/data/animation.rml");
  46. if (document != NULL)
  47. {
  48. {
  49. //document->GetElementById("title")->SetInnerRML(title);
  50. document->SetProperty("left", Property(position.x, Property::PX));
  51. document->SetProperty("top", Property(position.y, Property::PX));
  52. //document->Animate("opacity", Property(1.0f, Property::NUMBER), 1.5f, Tween{Tween::Quadratic, Tween::Out}, 1, true, 0.0f);
  53. }
  54. // Button fun
  55. {
  56. auto el = document->GetElementById("start_game");
  57. auto p1 = Transform::MakeProperty({ Transforms::Rotate2D{10.f}, Transforms::TranslateX{100.f} });
  58. auto p2 = Transform::MakeProperty({ Transforms::Scale2D{3.f} });
  59. el->Animate("transform", p1, 1.8f, Tween{ Tween::Elastic, Tween::InOut }, -1, true);
  60. el->AddAnimationKey("transform", p2, 1.3f, Tween{ Tween::Elastic, Tween::InOut });
  61. }
  62. {
  63. auto el = document->GetElementById("high_scores");
  64. el->Animate("margin-left", Property(0.f, Property::PX), 0.3f, Tween{ Tween::Sine, Tween::In }, 10, true, 1.f);
  65. el->AddAnimationKey("margin-left", Property(100.f, Property::PX), 3.0f, Tween{ Tween::Circular, Tween::Out });
  66. }
  67. {
  68. auto el = document->GetElementById("options");
  69. el->Animate("image-color", Property(Colourb(128, 255, 255, 255), Property::COLOUR), 0.3f, Tween{}, -1, false);
  70. el->AddAnimationKey("image-color", Property(Colourb(128, 128, 255, 255), Property::COLOUR), 0.3f);
  71. el->AddAnimationKey("image-color", Property(Colourb(0, 128, 128, 255), Property::COLOUR), 0.3f);
  72. el->AddAnimationKey("image-color", Property(Colourb(64, 128, 255, 0), Property::COLOUR), 0.9f);
  73. el->AddAnimationKey("image-color", Property(Colourb(255, 255, 255, 255), Property::COLOUR), 0.3f);
  74. }
  75. {
  76. auto el = document->GetElementById("help");
  77. el->Animate("margin-left", Property(100.f, Property::PX), 1.0f, Tween{ Tween::Quadratic, Tween::InOut }, -1, true);
  78. }
  79. {
  80. auto el = document->GetElementById("exit");
  81. PropertyDictionary pd;
  82. StyleSheetSpecification::ParsePropertyDeclaration(pd, "transform", "translate(200px, 200px) rotate(1215deg)");
  83. el->Animate("transform", *pd.GetProperty("transform"), 3.f, Tween{ Tween::Bounce, Tween::Out }, -1);
  84. }
  85. // Transform tests
  86. {
  87. auto el = document->GetElementById("generic");
  88. auto p = Transform::MakeProperty({ Transforms::TranslateY{50, Property::PX}, Transforms::Rotate3D{0.8f, 0, 1, 110, Property::DEG}});
  89. el->Animate("transform", p, 1.3f, Tween{Tween::Quadratic, Tween::InOut}, -1, true);
  90. }
  91. {
  92. auto el = document->GetElementById("combine");
  93. auto p = Transform::MakeProperty({ Transforms::Translate2D{50, 50, Property::PX}, Transforms::Rotate2D(1215) });
  94. el->Animate("transform", p, 8.0f, Tween{}, -1, true);
  95. }
  96. {
  97. auto el = document->GetElementById("decomposition");
  98. auto p = Transform::MakeProperty({ Transforms::Translate2D{50, 50, Property::PX}, Transforms::Rotate2D(1215) });
  99. el->Animate("transform", p, 8.0f, Tween{}, -1, true);
  100. }
  101. // Mixed units tests
  102. {
  103. auto el = document->GetElementById("abs_rel");
  104. el->Animate("margin-left", Property(50.f, Property::PERCENT), 1.5f, Tween{}, -1, true);
  105. }
  106. {
  107. auto el = document->GetElementById("abs_rel_transform");
  108. auto p = Transform::MakeProperty({ Transforms::TranslateX{0, Property::PX} });
  109. el->Animate("transform", p, 1.5f, Tween{}, -1, true);
  110. }
  111. {
  112. auto el = document->GetElementById("animation_event");
  113. el->Animate("top", Property(Math::RandomReal(250.f), Property::PX), 1.5f, Tween{ Tween::Cubic, Tween::InOut });
  114. el->Animate("left", Property(Math::RandomReal(250.f), Property::PX), 1.5f, Tween{ Tween::Cubic, Tween::InOut });
  115. }
  116. document->Show();
  117. }
  118. }
  119. void performance_test()
  120. {
  121. /*
  122. FPS values
  123. Original: 18.5
  124. Without property counter: 22.0
  125. With std::string: 23.0 [603fd40]
  126. robin_hood unordered_flat_map: 24.0 [709852f]
  127. Avoid dirtying em's: 27.5
  128. Restructuring update loop: 34.5 [f9892a9]
  129. Element constructor, remove geometry database, remove update() from Context::render: 38.0 [1aab59e]
  130. Replace Dictionary with unordered_flat_map: 40.0
  131. */
  132. if (!document)
  133. return;
  134. Rocket::Core::String rml;
  135. for (int i = 0; i < 50; i++)
  136. {
  137. int index = rand() % 1000;
  138. int route = rand() % 50;
  139. int max = (rand() % 40) + 10;
  140. int value = rand() % max;
  141. Rocket::Core::String rml_row = Rocket::Core::CreateString(1000, R"(
  142. <div class="row">
  143. <div class="col col1"><button class="expand" index="%d">+</button>&nbsp;<a>Route %d</a></div>
  144. <div class="col col23"><input type="range" class="assign_range" min="0" max="%d" value="%d"/></div>
  145. <div class="col col4">Assigned</div>
  146. <div class="inrow unmark_collapse">
  147. <div class="col col123 assign_text">Assign to route</div>
  148. <div class="col col4">
  149. <button class="vehicle_depot_assign_confirm" quantity="0">Confirm</button>
  150. </div>
  151. </div>
  152. </div>)",
  153. index,
  154. route,
  155. max,
  156. value
  157. );
  158. rml += rml_row;
  159. }
  160. if (auto el = document->GetElementById("performance"))
  161. el->SetInnerRML(rml);
  162. }
  163. ~DemoWindow()
  164. {
  165. if (document)
  166. {
  167. document->RemoveReference();
  168. document->Close();
  169. }
  170. }
  171. Rocket::Core::ElementDocument * GetDocument() {
  172. return document;
  173. }
  174. private:
  175. Rocket::Core::ElementDocument *document;
  176. };
  177. Rocket::Core::Context* context = NULL;
  178. ShellRenderInterfaceExtensions *shell_renderer;
  179. DemoWindow* window = NULL;
  180. bool pause_loop = false;
  181. bool single_loop = false;
  182. int nudge = 0;
  183. void GameLoop()
  184. {
  185. if(!pause_loop || single_loop)
  186. {
  187. context->Update();
  188. shell_renderer->PrepareRenderBuffer();
  189. context->Render();
  190. shell_renderer->PresentRenderBuffer();
  191. single_loop = false;
  192. }
  193. static double t_prev = 0.0f;
  194. double t = Shell::GetElapsedTime();
  195. float dt = float(t - t_prev);
  196. static int count_frames = 0;
  197. count_frames += 1;
  198. //t_prev = t;
  199. //if(dt > 1.0f)
  200. if(nudge)
  201. {
  202. t_prev = t;
  203. static float ff = 0.0f;
  204. ff += float(nudge)*0.3f;
  205. auto el = window->GetDocument()->GetElementById("exit");
  206. auto f = el->GetProperty<float>("margin-left");
  207. el->SetProperty("margin-left", Rocket::Core::Property(ff, Rocket::Core::Property::PX));
  208. float f_left = el->GetAbsoluteLeft();
  209. Rocket::Core::Log::Message(Rocket::Core::Log::LT_INFO, "margin-left: '%f' abs: %f.", ff, f_left);
  210. nudge = 0;
  211. }
  212. if (window && dt > 0.2f)
  213. {
  214. t_prev = t;
  215. auto el = window->GetDocument()->GetElementById("fps");
  216. float fps = float(count_frames) / dt;
  217. count_frames = 0;
  218. el->SetInnerRML(Rocket::Core::CreateString( 20, "FPS: %f", fps ));
  219. }
  220. window->performance_test();
  221. //static int f_prev = 0.0f;
  222. //int df = f - f_prev;
  223. //f_prev = f;
  224. //if(df != 0)
  225. // Rocket::Core::Log::Message(Rocket::Core::Log::LT_INFO, "Animation f = %d, df = %d", f, df);
  226. }
  227. class Event : public Rocket::Core::EventListener
  228. {
  229. public:
  230. Event(const Rocket::Core::String& value) : value(value) {}
  231. void ProcessEvent(Rocket::Core::Event& event) override
  232. {
  233. using namespace Rocket::Core;
  234. if(value == "exit")
  235. Shell::RequestExit();
  236. if (event == "keydown")
  237. {
  238. bool key_down = event == "keydown";
  239. Rocket::Core::Input::KeyIdentifier key_identifier = (Rocket::Core::Input::KeyIdentifier) event.GetParameter< int >("key_identifier", 0);
  240. if (key_identifier == Rocket::Core::Input::KI_SPACE)
  241. {
  242. pause_loop = !pause_loop;
  243. }
  244. else if (key_identifier == Rocket::Core::Input::KI_RETURN)
  245. {
  246. pause_loop = true;
  247. single_loop = true;
  248. }
  249. else if (key_identifier == Rocket::Core::Input::KI_OEM_PLUS)
  250. {
  251. nudge = 1;
  252. }
  253. else if (key_identifier == Rocket::Core::Input::KI_OEM_MINUS)
  254. {
  255. nudge = -1;
  256. }
  257. else if (key_identifier == Rocket::Core::Input::KI_ESCAPE)
  258. {
  259. Shell::RequestExit();
  260. }
  261. else if (key_identifier == Rocket::Core::Input::KI_F8)
  262. {
  263. Rocket::Debugger::SetVisible(!Rocket::Debugger::IsVisible());
  264. }
  265. else if (key_identifier == Rocket::Core::Input::KI_LEFT)
  266. {
  267. auto el = context->GetRootElement()->GetElementById("keyevent_response");
  268. if (el) el->Animate("left", Property{ -200.f, Property::PX }, 0.5, Tween{ Tween::Cubic });
  269. }
  270. else if (key_identifier == Rocket::Core::Input::KI_RIGHT)
  271. {
  272. auto el = context->GetRootElement()->GetElementById("keyevent_response");
  273. if (el) el->Animate("left", Property{ 200.f, Property::PX }, 0.5, Tween{ Tween::Cubic });
  274. }
  275. else if (key_identifier == Rocket::Core::Input::KI_UP)
  276. {
  277. auto el = context->GetRootElement()->GetElementById("keyevent_response");
  278. auto offset_right = Property{ 200.f, Property::PX };
  279. if (el) el->Animate("left", Property{ 0.f, Property::PX }, 0.5, Tween{ Tween::Cubic }, 1, true, 0, &offset_right);
  280. }
  281. else if (key_identifier == Rocket::Core::Input::KI_DOWN)
  282. {
  283. auto el = context->GetRootElement()->GetElementById("keyevent_response");
  284. if (el) el->Animate("left", Property{ 0.f, Property::PX }, 0.5, Tween{ Tween::Cubic });
  285. }
  286. }
  287. if (event == "click")
  288. {
  289. auto el = event.GetTargetElement();
  290. if (el->GetId() == "transition_class")
  291. {
  292. el->SetClass("move_me", !el->IsClassSet("move_me"));
  293. }
  294. }
  295. if (event == "animationend")
  296. {
  297. auto el = event.GetTargetElement();
  298. if (el->GetId() == "animation_event")
  299. {
  300. el->Animate("top", Property(Math::RandomReal(200.f), Property::PX), 1.2f, Tween{ Tween::Cubic, Tween::InOut });
  301. el->Animate("left", Property(Math::RandomReal(100.f), Property::PERCENT), 0.8f, Tween{ Tween::Cubic, Tween::InOut });
  302. }
  303. }
  304. }
  305. void OnDetach(Rocket::Core::Element* element) override { delete this; }
  306. private:
  307. Rocket::Core::String value;
  308. };
  309. class EventInstancer : public Rocket::Core::EventListenerInstancer
  310. {
  311. public:
  312. /// Instances a new event handle for Invaders.
  313. Rocket::Core::EventListener* InstanceEventListener(const Rocket::Core::String& value, Rocket::Core::Element* element) override
  314. {
  315. return new Event(value);
  316. }
  317. /// Destroys the instancer.
  318. void Release() override { delete this; }
  319. };
  320. #if defined ROCKET_PLATFORM_WIN32
  321. #include <windows.h>
  322. int APIENTRY WinMain(HINSTANCE ROCKET_UNUSED_PARAMETER(instance_handle), HINSTANCE ROCKET_UNUSED_PARAMETER(previous_instance_handle), char* ROCKET_UNUSED_PARAMETER(command_line), int ROCKET_UNUSED_PARAMETER(command_show))
  323. #else
  324. int main(int ROCKET_UNUSED_PARAMETER(argc), char** ROCKET_UNUSED_PARAMETER(argv))
  325. #endif
  326. {
  327. #ifdef ROCKET_PLATFORM_WIN32
  328. ROCKET_UNUSED(instance_handle);
  329. ROCKET_UNUSED(previous_instance_handle);
  330. ROCKET_UNUSED(command_line);
  331. ROCKET_UNUSED(command_show);
  332. #else
  333. ROCKET_UNUSED(argc);
  334. ROCKET_UNUSED(argv);
  335. #endif
  336. const int width = 1800;
  337. const int height = 1000;
  338. ShellRenderInterfaceOpenGL opengl_renderer;
  339. shell_renderer = &opengl_renderer;
  340. // Generic OS initialisation, creates a window and attaches OpenGL.
  341. if (!Shell::Initialise("../../Samples/") ||
  342. !Shell::OpenWindow("Animation Sample", shell_renderer, width, height, true))
  343. {
  344. Shell::Shutdown();
  345. return -1;
  346. }
  347. // Rocket initialisation.
  348. Rocket::Core::SetRenderInterface(&opengl_renderer);
  349. opengl_renderer.SetViewport(width, height);
  350. ShellSystemInterface system_interface;
  351. Rocket::Core::SetSystemInterface(&system_interface);
  352. Rocket::Core::Initialise();
  353. // Create the main Rocket context and set it on the shell's input layer.
  354. context = Rocket::Core::CreateContext("main", Rocket::Core::Vector2i(width, height));
  355. if (context == NULL)
  356. {
  357. Rocket::Core::Shutdown();
  358. Shell::Shutdown();
  359. return -1;
  360. }
  361. Rocket::Controls::Initialise();
  362. Rocket::Debugger::Initialise(context);
  363. Input::SetContext(context);
  364. shell_renderer->SetContext(context);
  365. EventInstancer* event_instancer = new EventInstancer();
  366. Rocket::Core::Factory::RegisterEventListenerInstancer(event_instancer);
  367. event_instancer->RemoveReference();
  368. Shell::LoadFonts("assets/");
  369. window = new DemoWindow("Animation sample", Rocket::Core::Vector2f(81, 100), context);
  370. window->GetDocument()->AddEventListener("keydown", new Event("hello"));
  371. window->GetDocument()->AddEventListener("keyup", new Event("hello"));
  372. window->GetDocument()->AddEventListener("animationend", new Event("hello"));
  373. Shell::EventLoop(GameLoop);
  374. delete window;
  375. // Shutdown Rocket.
  376. context->RemoveReference();
  377. Rocket::Core::Shutdown();
  378. Shell::CloseWindow();
  379. Shell::Shutdown();
  380. return 0;
  381. }