main.cpp 12 KB

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