main.cpp 12 KB

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