main.cpp 15 KB

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