main.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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-2023 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/Debugger.h>
  30. #include <RmlUi_Backend.h>
  31. #include <Shell.h>
  32. #include <numeric>
  33. namespace {
  34. namespace BasicExample {
  35. Rml::DataModelHandle model_handle;
  36. struct MyData {
  37. Rml::String title = "Simple data binding example";
  38. Rml::String animal = "dog";
  39. bool show_text = true;
  40. } my_data;
  41. bool Initialize(Rml::Context* context)
  42. {
  43. Rml::DataModelConstructor constructor = context->CreateDataModel("basics");
  44. if (!constructor)
  45. return false;
  46. constructor.Bind("title", &my_data.title);
  47. constructor.Bind("animal", &my_data.animal);
  48. constructor.Bind("show_text", &my_data.show_text);
  49. model_handle = constructor.GetModelHandle();
  50. return true;
  51. }
  52. } // namespace BasicExample
  53. namespace EventsExample {
  54. Rml::DataModelHandle model_handle;
  55. struct MyData {
  56. Rml::String hello_world = "Hello World!";
  57. Rml::String mouse_detector = "Mouse-move <em>Detector</em>.";
  58. int rating = 99;
  59. Rml::Vector<float> list = {1, 2, 3, 4, 5};
  60. Rml::Vector<Rml::Vector2f> positions;
  61. void AddMousePos(Rml::DataModelHandle model, Rml::Event& ev, const Rml::VariantList& /*arguments*/)
  62. {
  63. positions.emplace_back(ev.GetParameter("mouse_x", 0.f), ev.GetParameter("mouse_y", 0.f));
  64. model.DirtyVariable("positions");
  65. }
  66. } my_data;
  67. void ClearPositions(Rml::DataModelHandle model, Rml::Event& /*ev*/, const Rml::VariantList& /*arguments*/)
  68. {
  69. my_data.positions.clear();
  70. model.DirtyVariable("positions");
  71. }
  72. void HasGoodRating(Rml::Variant& variant)
  73. {
  74. variant = int(my_data.rating > 50);
  75. }
  76. bool Initialize(Rml::Context* context)
  77. {
  78. using namespace Rml;
  79. DataModelConstructor constructor = context->CreateDataModel("events");
  80. if (!constructor)
  81. return false;
  82. // Register all the types first
  83. constructor.RegisterArray<Rml::Vector<float>>();
  84. if (auto vec2_handle = constructor.RegisterStruct<Vector2f>())
  85. {
  86. vec2_handle.RegisterMember("x", &Vector2f::x);
  87. vec2_handle.RegisterMember("y", &Vector2f::y);
  88. }
  89. constructor.RegisterArray<Rml::Vector<Vector2f>>();
  90. // Bind the variables to the data model
  91. constructor.Bind("hello_world", &my_data.hello_world);
  92. constructor.Bind("mouse_detector", &my_data.mouse_detector);
  93. constructor.Bind("rating", &my_data.rating);
  94. constructor.BindFunc("good_rating", &HasGoodRating);
  95. constructor.BindFunc("great_rating", [](Variant& variant) { variant = int(my_data.rating > 80); });
  96. constructor.Bind("list", &my_data.list);
  97. constructor.Bind("positions", &my_data.positions);
  98. constructor.BindEventCallback("clear_positions", &ClearPositions);
  99. constructor.BindEventCallback("add_mouse_pos", &MyData::AddMousePos, &my_data);
  100. model_handle = constructor.GetModelHandle();
  101. return true;
  102. }
  103. void Update()
  104. {
  105. if (model_handle.IsVariableDirty("rating"))
  106. {
  107. model_handle.DirtyVariable("good_rating");
  108. model_handle.DirtyVariable("great_rating");
  109. size_t new_size = my_data.rating / 10 + 1;
  110. if (new_size != my_data.list.size())
  111. {
  112. my_data.list.resize(new_size);
  113. std::iota(my_data.list.begin(), my_data.list.end(), float(new_size));
  114. model_handle.DirtyVariable("list");
  115. }
  116. }
  117. }
  118. } // namespace EventsExample
  119. namespace InvadersExample {
  120. Rml::DataModelHandle model_handle;
  121. static constexpr int num_invaders = 12;
  122. static constexpr double incoming_invaders_rate = 50; // Per minute
  123. struct Invader {
  124. Rml::String name;
  125. Rml::String sprite;
  126. Rml::Colourb color{255, 255, 255};
  127. float max_health = 0;
  128. float charge_rate = 0;
  129. float health = 0;
  130. float charge = 0;
  131. };
  132. struct InvadersData {
  133. float health = 0;
  134. float charge = 0;
  135. int score = 0;
  136. double elapsed_time = 0;
  137. double next_invader_spawn_time = 0;
  138. int num_games_played = 0;
  139. Rml::Array<Invader, num_invaders> invaders;
  140. // Start a new game.
  141. void StartGame(Rml::DataModelHandle model, Rml::Event& /*ev*/, const Rml::VariantList& /*arguments*/)
  142. {
  143. health = 100;
  144. charge = 30;
  145. score = 0;
  146. elapsed_time = 0;
  147. next_invader_spawn_time = 0;
  148. num_games_played += 1;
  149. for (Invader& invader : invaders)
  150. invader.health = 0;
  151. model.DirtyVariable("health");
  152. model.DirtyVariable("charge");
  153. model.DirtyVariable("score");
  154. model.DirtyVariable("elapsed_time");
  155. model.DirtyVariable("num_games_played");
  156. model.DirtyVariable("invaders");
  157. }
  158. // Fire on the invader of the given index (first argument).
  159. void Fire(Rml::DataModelHandle model, Rml::Event& /*ev*/, const Rml::VariantList& arguments)
  160. {
  161. if (arguments.size() != 1)
  162. return;
  163. const std::size_t index = arguments[0].Get<std::size_t>();
  164. if (index >= invaders.size())
  165. return;
  166. Invader& invader = invaders[index];
  167. if (health <= 0 || invader.health <= 0)
  168. return;
  169. const float new_health = Rml::Math::Max(invader.health - charge * Rml::Math::SquareRoot(charge), 0.0f);
  170. charge = 30.f;
  171. score += int(invader.health - new_health) + 1000 * (new_health == 0);
  172. invader.health = new_health;
  173. model.DirtyVariable("invaders");
  174. model.DirtyVariable("charge");
  175. model.DirtyVariable("score");
  176. }
  177. } data;
  178. bool Initialize(Rml::Context* context)
  179. {
  180. Rml::DataModelConstructor constructor = context->CreateDataModel("invaders");
  181. if (!constructor)
  182. return false;
  183. // Register a custom getter for the Colourb type.
  184. constructor.RegisterScalar<Rml::Colourb>(
  185. [](const Rml::Colourb& color, Rml::Variant& variant) { variant = Rml::ToString(color); });
  186. // Register a transform function for formatting time
  187. constructor.RegisterTransformFunc("format_time", [](const Rml::VariantList& arguments) -> Rml::Variant {
  188. if (arguments.empty())
  189. return {};
  190. const double t = arguments[0].Get<double>();
  191. const int minutes = int(t) / 60;
  192. const double seconds = t - 60.0 * double(minutes);
  193. return Rml::Variant(Rml::CreateString(10, "%02d:%05.2f", minutes, seconds));
  194. });
  195. // Structs are registered by adding all their members through the returned handle.
  196. if (auto invader_handle = constructor.RegisterStruct<Invader>())
  197. {
  198. invader_handle.RegisterMember("name", &Invader::name);
  199. invader_handle.RegisterMember("sprite", &Invader::sprite);
  200. invader_handle.RegisterMember("color", &Invader::color);
  201. invader_handle.RegisterMember("max_health", &Invader::max_health);
  202. invader_handle.RegisterMember("charge_rate", &Invader::charge_rate);
  203. invader_handle.RegisterMember("health", &Invader::health);
  204. invader_handle.RegisterMember("charge", &Invader::charge);
  205. }
  206. // We can even have an Array of Structs, infinitely nested if we so desire.
  207. // Make sure the underlying type (here Invader) is registered before the array.
  208. constructor.RegisterArray<decltype(data.invaders)>();
  209. // Now we can bind the variables to the model.
  210. constructor.Bind("invaders", &data.invaders);
  211. constructor.Bind("health", &data.health);
  212. constructor.Bind("charge", &data.charge);
  213. constructor.Bind("score", &data.score);
  214. constructor.Bind("elapsed_time", &data.elapsed_time);
  215. constructor.Bind("num_games_played", &data.num_games_played);
  216. // This function will be called when the user clicks on the (re)start game button.
  217. constructor.BindEventCallback("start_game", &InvadersData::StartGame, &data);
  218. // This function will be called when the user clicks on any of the invaders.
  219. constructor.BindEventCallback("fire", &InvadersData::Fire, &data);
  220. model_handle = constructor.GetModelHandle();
  221. return true;
  222. }
  223. void Update(const double dt)
  224. {
  225. using namespace Rml;
  226. if (data.health == 0)
  227. return;
  228. data.elapsed_time += dt;
  229. model_handle.DirtyVariable("elapsed_time");
  230. // Steadily increase the player charge.
  231. data.charge = Math::Min(data.charge + float(40.0 * dt), 100.f);
  232. model_handle.DirtyVariable("charge");
  233. // Add new invaders at the scheduled time.
  234. if (data.elapsed_time >= data.next_invader_spawn_time)
  235. {
  236. constexpr int num_items = 4;
  237. static Array<String, num_items> names = {"Angry invader", "Harmless invader", "Deceitful invader", "Cute invader"};
  238. static Array<String, num_items> sprites = {"icon-invader", "icon-flag", "icon-game", "icon-waves"};
  239. static Array<Colourb, num_items> colors = {{{255, 40, 30}, {20, 40, 255}, {255, 255, 30}, {230, 230, 230}}};
  240. Invader new_invader;
  241. new_invader.name = names[Math::RandomInteger(num_items)];
  242. new_invader.sprite = sprites[Math::RandomInteger(num_items)];
  243. new_invader.color = colors[Math::RandomInteger(num_items)];
  244. new_invader.max_health = 300.f + float(30.0 * data.elapsed_time) + Math::RandomReal(300.f);
  245. new_invader.charge_rate = 10.f + Math::RandomReal(50.f);
  246. new_invader.health = new_invader.max_health;
  247. // Find an available slot to spawn the new invader in.
  248. const int i_begin = Math::RandomInteger(num_invaders);
  249. for (int i = 0; i < num_invaders; i++)
  250. {
  251. Invader& invader = data.invaders[(i + i_begin) % num_invaders];
  252. if (invader.health <= 0)
  253. {
  254. invader = std::move(new_invader);
  255. model_handle.DirtyVariable("invaders");
  256. break;
  257. }
  258. }
  259. // Add new invaders at steadily decreasing time intervals.
  260. data.next_invader_spawn_time = data.elapsed_time + 60.0 / (incoming_invaders_rate + 0.1 * data.elapsed_time);
  261. }
  262. // Iterate through the invaders and fire at the player.
  263. for (Invader& invader : data.invaders)
  264. {
  265. if (invader.health > 0)
  266. {
  267. invader.charge = invader.charge + invader.charge_rate * float(dt);
  268. if (invader.charge >= 100)
  269. {
  270. data.health = Math::Max(data.health - float(10.0 * dt), 0.0f);
  271. model_handle.DirtyVariable("health");
  272. }
  273. if (invader.charge >= 120)
  274. invader.charge = 0;
  275. model_handle.DirtyVariable("invaders");
  276. }
  277. }
  278. }
  279. } // namespace InvadersExample
  280. namespace FormsExample {
  281. Rml::DataModelHandle model_handle;
  282. struct MyData {
  283. int rating = 50;
  284. bool pizza = true;
  285. bool pasta = false;
  286. bool lasagne = false;
  287. Rml::String animal = "dog";
  288. Rml::Vector<Rml::String> subjects = {"Choose your subject", "Feature request", "Bug report", "Praise", "Criticism"};
  289. int selected_subject = 0;
  290. Rml::String new_subject = "New subject";
  291. } my_data;
  292. bool Initialize(Rml::Context* context)
  293. {
  294. Rml::DataModelConstructor constructor = context->CreateDataModel("forms");
  295. if (!constructor)
  296. return false;
  297. constructor.RegisterArray<Rml::Vector<Rml::String>>();
  298. constructor.Bind("rating", &my_data.rating);
  299. constructor.Bind("pizza", &my_data.pizza);
  300. constructor.Bind("pasta", &my_data.pasta);
  301. constructor.Bind("lasagne", &my_data.lasagne);
  302. constructor.Bind("animal", &my_data.animal);
  303. constructor.Bind("subjects", &my_data.subjects);
  304. constructor.Bind("selected_subject", &my_data.selected_subject);
  305. constructor.Bind("new_subject", &my_data.new_subject);
  306. constructor.BindEventCallback("add_subject", [](Rml::DataModelHandle model, Rml::Event& /*ev*/, const Rml::VariantList& arguments) {
  307. Rml::String name = (arguments.size() == 1 ? arguments[0].Get<Rml::String>() : "");
  308. if (!name.empty())
  309. {
  310. my_data.subjects.push_back(std::move(name));
  311. model.DirtyVariable("subjects");
  312. }
  313. });
  314. constructor.BindEventCallback("erase_subject", [](Rml::DataModelHandle model, Rml::Event& /*ev*/, const Rml::VariantList& arguments) {
  315. const int i = (arguments.size() == 1 ? arguments[0].Get<int>(-1) : -1);
  316. if (i >= 0 && i < (int)my_data.subjects.size())
  317. {
  318. my_data.subjects.erase(my_data.subjects.begin() + i);
  319. my_data.selected_subject = 0;
  320. model.DirtyVariable("subjects");
  321. model.DirtyVariable("selected_subject");
  322. }
  323. });
  324. model_handle = constructor.GetModelHandle();
  325. return true;
  326. }
  327. } // namespace FormsExample
  328. } // namespace
  329. class DemoWindow : public Rml::EventListener {
  330. public:
  331. DemoWindow(const Rml::String& title, Rml::Context* context)
  332. {
  333. document = context->LoadDocument("basic/data_binding/data/data_binding.rml");
  334. if (document)
  335. {
  336. document->GetElementById("title")->SetInnerRML(title);
  337. document->Show();
  338. }
  339. }
  340. void Shutdown()
  341. {
  342. if (document)
  343. {
  344. document->Close();
  345. document = nullptr;
  346. }
  347. }
  348. void ProcessEvent(Rml::Event& event) override
  349. {
  350. switch (event.GetId())
  351. {
  352. case Rml::EventId::Keydown:
  353. {
  354. Rml::Input::KeyIdentifier key_identifier = (Rml::Input::KeyIdentifier)event.GetParameter<int>("key_identifier", 0);
  355. if (key_identifier == Rml::Input::KI_ESCAPE)
  356. Backend::RequestExit();
  357. }
  358. break;
  359. default: break;
  360. }
  361. }
  362. Rml::ElementDocument* GetDocument() { return document; }
  363. private:
  364. Rml::ElementDocument* document = nullptr;
  365. };
  366. #if defined RMLUI_PLATFORM_WIN32
  367. #include <RmlUi_Include_Windows.h>
  368. int APIENTRY WinMain(HINSTANCE /*instance_handle*/, HINSTANCE /*previous_instance_handle*/, char* /*command_line*/, int /*command_show*/)
  369. #else
  370. int main(int /*argc*/, char** /*argv*/)
  371. #endif
  372. {
  373. const int width = 1600;
  374. const int height = 900;
  375. // Initializes the shell which provides common functionality used by the included samples.
  376. if (!Shell::Initialize())
  377. return -1;
  378. // Constructs the system and render interfaces, creates a window, and attaches the renderer.
  379. if (!Backend::Initialize("Data Binding Sample", width, height, true))
  380. {
  381. Shell::Shutdown();
  382. return -1;
  383. }
  384. // Install the custom interfaces constructed by the backend before initializing RmlUi.
  385. Rml::SetSystemInterface(Backend::GetSystemInterface());
  386. Rml::SetRenderInterface(Backend::GetRenderInterface());
  387. // RmlUi initialisation.
  388. Rml::Initialise();
  389. // Create the main RmlUi context.
  390. Rml::Context* context = Rml::CreateContext("main", Rml::Vector2i(width, height));
  391. if (!context //
  392. || !BasicExample::Initialize(context) //
  393. || !EventsExample::Initialize(context) //
  394. || !InvadersExample::Initialize(context) //
  395. || !FormsExample::Initialize(context) //
  396. )
  397. {
  398. Rml::Shutdown();
  399. Backend::Shutdown();
  400. Shell::Shutdown();
  401. return -1;
  402. }
  403. Rml::Debugger::Initialise(context);
  404. Shell::LoadFonts();
  405. auto demo_window = Rml::MakeUnique<DemoWindow>("Data binding", context);
  406. demo_window->GetDocument()->AddEventListener(Rml::EventId::Keydown, demo_window.get());
  407. demo_window->GetDocument()->AddEventListener(Rml::EventId::Keyup, demo_window.get());
  408. double t_prev = 0;
  409. bool running = true;
  410. while (running)
  411. {
  412. running = Backend::ProcessEvents(context, &Shell::ProcessKeyDownShortcuts);
  413. const double t = Rml::GetSystemInterface()->GetElapsedTime();
  414. const double dt = Rml::Math::Min(t - t_prev, 0.1);
  415. t_prev = t;
  416. EventsExample::Update();
  417. InvadersExample::Update(dt);
  418. context->Update();
  419. Backend::BeginFrame();
  420. context->Render();
  421. Backend::PresentFrame();
  422. }
  423. demo_window->Shutdown();
  424. // Shutdown RmlUi.
  425. Rml::Shutdown();
  426. Backend::Shutdown();
  427. Shell::Shutdown();
  428. demo_window.reset();
  429. return 0;
  430. }