Animation.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  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) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  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 "../Common/TestsInterface.h"
  29. #include "../Common/TestsShell.h"
  30. #include "../Common/TypesToString.h"
  31. #include <RmlUi/Core/Context.h>
  32. #include <RmlUi/Core/Element.h>
  33. #include <RmlUi/Core/ElementDocument.h>
  34. #include <doctest.h>
  35. #include <float.h>
  36. using namespace Rml;
  37. static const String document_decorator_rml = R"(
  38. <rml>
  39. <head>
  40. <title>Test</title>
  41. <link type="text/rcss" href="/assets/rml.rcss"/>
  42. <style>
  43. body {
  44. left: 0;
  45. top: 0;
  46. right: 0;
  47. bottom: 0;
  48. }
  49. @decorator from_rule : horizontal-gradient { %s }
  50. @decorator to_rule: horizontal-gradient{ %s }
  51. @keyframes mix {
  52. from { %s: %s; }
  53. to { %s: %s; }
  54. }
  55. div {
  56. background: #333;
  57. height: 64px;
  58. width: 64px;
  59. animation: mix 0.1s;
  60. }
  61. </style>
  62. </head>
  63. <body>
  64. <div/>
  65. </body>
  66. </rml>
  67. )";
  68. TEST_CASE("animation.decorator")
  69. {
  70. struct Test {
  71. String from_rule;
  72. String to_rule;
  73. String from;
  74. String to;
  75. String expected_25p; // expected interpolated value at 25% progression
  76. };
  77. Vector<Test> tests{
  78. // Only standard declaration
  79. {
  80. "",
  81. "",
  82. "horizontal-gradient(transparent transparent)",
  83. "horizontal-gradient(white white)",
  84. "horizontal-gradient(horizontal #7f7f7f3f #7f7f7f3f)",
  85. },
  86. {
  87. "",
  88. "",
  89. "horizontal-gradient(transparent transparent) border-box",
  90. "horizontal-gradient(white white) border-box",
  91. "horizontal-gradient(horizontal #7f7f7f3f #7f7f7f3f) border-box",
  92. },
  93. {
  94. "",
  95. "",
  96. "none",
  97. "horizontal-gradient(transparent transparent)",
  98. "horizontal-gradient(horizontal #dcdcdcbf #dcdcdcbf)",
  99. },
  100. {
  101. "",
  102. "",
  103. "none",
  104. "horizontal-gradient(transparent transparent), horizontal-gradient(transparent transparent)",
  105. "horizontal-gradient(horizontal #dcdcdcbf #dcdcdcbf), horizontal-gradient(horizontal #dcdcdcbf #dcdcdcbf)",
  106. },
  107. {
  108. "",
  109. "",
  110. "horizontal-gradient(transparent transparent), horizontal-gradient(transparent transparent)",
  111. "none",
  112. "horizontal-gradient(horizontal #7f7f7f3f #7f7f7f3f), horizontal-gradient(horizontal #7f7f7f3f #7f7f7f3f)",
  113. },
  114. /// Only rule declaration
  115. {
  116. "start-color: transparent; stop-color: transparent;",
  117. "start-color: white; stop-color: white;",
  118. "from_rule",
  119. "to_rule",
  120. "horizontal-gradient(horizontal #7f7f7f3f #7f7f7f3f)",
  121. },
  122. {
  123. "",
  124. "start-color: transparent; stop-color: transparent;",
  125. "from_rule",
  126. "to_rule",
  127. "horizontal-gradient(horizontal #dcdcdcbf #dcdcdcbf)",
  128. },
  129. {
  130. "start-color: transparent; stop-color: transparent;",
  131. "",
  132. "from_rule",
  133. "to_rule",
  134. "horizontal-gradient(horizontal #7f7f7f3f #7f7f7f3f)",
  135. },
  136. /// Mix rule and standard declaration
  137. {
  138. "start-color: transparent; stop-color: transparent;",
  139. "",
  140. "from_rule",
  141. "horizontal-gradient(white white)",
  142. "horizontal-gradient(horizontal #7f7f7f3f #7f7f7f3f)",
  143. },
  144. {
  145. "",
  146. "start-color: transparent; stop-color: transparent;",
  147. "none",
  148. "to_rule",
  149. "horizontal-gradient(horizontal #dcdcdcbf #dcdcdcbf)",
  150. },
  151. {
  152. "start-color: transparent; stop-color: transparent;",
  153. "",
  154. "from_rule",
  155. "none",
  156. "horizontal-gradient(horizontal #7f7f7f3f #7f7f7f3f)",
  157. },
  158. {
  159. "",
  160. "",
  161. "from_rule, to_rule",
  162. "horizontal-gradient(transparent transparent), horizontal-gradient(transparent transparent)",
  163. "horizontal-gradient(horizontal #dcdcdcbf #dcdcdcbf), horizontal-gradient(horizontal #dcdcdcbf #dcdcdcbf)",
  164. },
  165. {
  166. "",
  167. "",
  168. "horizontal-gradient(transparent transparent), horizontal-gradient(transparent transparent)",
  169. "from_rule, to_rule",
  170. "horizontal-gradient(horizontal #7f7f7f3f #7f7f7f3f), horizontal-gradient(horizontal #7f7f7f3f #7f7f7f3f)",
  171. },
  172. // Standard declaration of linear gradients (consider string conversion a best-effort for now)
  173. {
  174. "",
  175. "",
  176. "linear-gradient(transparent, transparent)",
  177. "linear-gradient(white, white)",
  178. "linear-gradient(180deg unspecified unspecified unspecified #7d7d7d3f, #7d7d7d3f)",
  179. },
  180. {
  181. "",
  182. "",
  183. "linear-gradient(0deg, transparent, transparent)",
  184. "linear-gradient(180deg, white, white)",
  185. "linear-gradient(45deg unspecified unspecified unspecified #7d7d7d3f, #7d7d7d3f)",
  186. },
  187. {
  188. "",
  189. "",
  190. "linear-gradient(105deg, #000000 0%, #ff0000 100%)",
  191. "linear-gradient(105deg, #ffffff 20%, #00ff00 60%)",
  192. "linear-gradient(105deg unspecified unspecified unspecified #7f7f7f 5%, #dc7f00 90%)",
  193. },
  194. {
  195. "",
  196. "",
  197. "linear-gradient(105deg, #000000 0%, #ff0000 50%, #ff00ff 100%)",
  198. "linear-gradient(105deg, #ffffff 0%, #ffffff 10%, #ffffff 100%)",
  199. "linear-gradient(105deg unspecified unspecified unspecified #7f7f7f 0%, #ff7f7f 40%, #ff7fff 100%)",
  200. },
  201. {
  202. "",
  203. "",
  204. "linear-gradient(to right, transparent, transparent)",
  205. "linear-gradient(270deg, transparent, transparent)",
  206. // We don't really handle mixing direction keywords and angles here, the output will not be what one might expect.
  207. "linear-gradient(202.5deg to right unspecified #00000000, #00000000)",
  208. },
  209. {
  210. "",
  211. "",
  212. "linear-gradient(to right, transparent, transparent)",
  213. "linear-gradient(to left, transparent, transparent)",
  214. // This will effectively evaluate to "to right" with angle being ignored, resulting in discrete interpolation. Not ideal.
  215. "linear-gradient(180deg to right unspecified #00000000, #00000000)",
  216. },
  217. {
  218. "",
  219. "",
  220. "linear-gradient(#000000 0%, #ffffff 100%)",
  221. "repeating-linear-gradient(#000000 0%, #ffffff 100%)",
  222. "linear-gradient(#000000 0%, #ffffff 100%)",
  223. },
  224. };
  225. TestsSystemInterface* system_interface = TestsShell::GetTestsSystemInterface();
  226. Context* context = TestsShell::GetContext();
  227. for (const String property_str : {"decorator", "mask-image"})
  228. {
  229. for (const Test& test : tests)
  230. {
  231. const double t_final = 0.1;
  232. system_interface->SetTime(0.0);
  233. String document_rml = Rml::CreateString(document_decorator_rml.c_str(), test.from_rule.c_str(), test.to_rule.c_str(),
  234. property_str.c_str(), test.from.c_str(), property_str.c_str(), test.to.c_str());
  235. ElementDocument* document = context->LoadDocumentFromMemory(document_rml, "assets/");
  236. Element* element = document->GetChild(0);
  237. document->Show();
  238. TestsShell::RenderLoop();
  239. system_interface->SetTime(0.25 * t_final);
  240. TestsShell::RenderLoop();
  241. CAPTURE(property_str);
  242. CAPTURE(test.from);
  243. CAPTURE(test.to);
  244. CHECK(element->GetProperty<String>(property_str) == test.expected_25p);
  245. document->Close();
  246. }
  247. }
  248. system_interface->SetTime(0.0);
  249. TestsShell::ShutdownShell();
  250. }
  251. static const String document_filter_rml = R"(
  252. <rml>
  253. <head>
  254. <title>Test</title>
  255. <link type="text/rcss" href="/assets/rml.rcss"/>
  256. <style>
  257. body {
  258. left: 0;
  259. top: 0;
  260. right: 0;
  261. bottom: 0;
  262. }
  263. @keyframes mix {
  264. from { %s: %s; }
  265. to { %s: %s; }
  266. }
  267. div {
  268. background: #333;
  269. height: 64px;
  270. width: 64px;
  271. decorator: image(high_scores_alien_1.tga);
  272. animation: mix 0.1s;
  273. }
  274. </style>
  275. </head>
  276. <body>
  277. <div/>
  278. </body>
  279. </rml>
  280. )";
  281. TEST_CASE("animation.filter")
  282. {
  283. struct Test {
  284. String from;
  285. String to;
  286. String expected_25p; // expected interpolated value at 25% progression
  287. };
  288. Vector<Test> tests{
  289. {
  290. "blur( 0px)",
  291. "blur(40px)",
  292. "blur(10px)",
  293. },
  294. {
  295. "blur(10px)",
  296. "blur(25dp)", // assumes dp-ratio == 2
  297. "blur(20px)",
  298. },
  299. {
  300. "blur(40px)",
  301. "none",
  302. "blur(30px)",
  303. },
  304. {
  305. "none",
  306. "blur(40px)",
  307. "blur(10px)",
  308. },
  309. {
  310. "drop-shadow(#000 30px 20px 0px)",
  311. "drop-shadow(#f00 30px 20px 4px)", // colors interpolated in linear space
  312. "drop-shadow(#7f0000 30px 20px 1px)",
  313. },
  314. {
  315. "opacity(0) brightness(2)",
  316. "none",
  317. "opacity(0.25) brightness(1.75)",
  318. },
  319. {
  320. "opacity(0) brightness(0)",
  321. "opacity(0.5)",
  322. "opacity(0.125) brightness(0.25)",
  323. },
  324. {
  325. "opacity(0.5)",
  326. "opacity(0) brightness(0)",
  327. "opacity(0.375) brightness(0.75)",
  328. },
  329. {
  330. "opacity(0) brightness(0)",
  331. "brightness(1) opacity(0.5)", // discrete interpolation due to non-matching types
  332. "opacity(0) brightness(0)",
  333. },
  334. {
  335. "none", // Test initial values of various filters.
  336. "brightness(2.00) contrast(2.00) grayscale(1.00) hue-rotate(4rad) invert(1.00) opacity(0.00) sepia(1.00) saturate(2.00)",
  337. "brightness(1.25) contrast(1.25) grayscale(0.25) hue-rotate(1rad) invert(0.25) opacity(0.75) sepia(0.25) saturate(1.25)",
  338. },
  339. };
  340. TestsSystemInterface* system_interface = TestsShell::GetTestsSystemInterface();
  341. Context* context = TestsShell::GetContext();
  342. context->SetDensityIndependentPixelRatio(2.0f);
  343. for (const char* property_str : {"filter", "backdrop-filter"})
  344. {
  345. for (const Test& test : tests)
  346. {
  347. const double t_final = 0.1;
  348. system_interface->SetTime(0.0);
  349. String document_rml = Rml::CreateString(document_filter_rml.c_str(), property_str, test.from.c_str(), property_str, test.to.c_str());
  350. ElementDocument* document = context->LoadDocumentFromMemory(document_rml, "assets/");
  351. Element* element = document->GetChild(0);
  352. document->Show();
  353. system_interface->SetTime(0.25 * t_final);
  354. TestsShell::RenderLoop();
  355. CHECK_MESSAGE(element->GetProperty<String>(property_str) == test.expected_25p, property_str, " from: ", test.from, ", to: ", test.to);
  356. document->Close();
  357. }
  358. }
  359. system_interface->SetTime(0.0);
  360. TestsShell::ShutdownShell();
  361. }
  362. static const String document_multiple_values_rml = R"(
  363. <rml>
  364. <head>
  365. <title>Test</title>
  366. <link type="text/rcss" href="/assets/rml.rcss"/>
  367. <style>
  368. body {
  369. left: 0;
  370. top: 0;
  371. right: 0;
  372. bottom: 0;
  373. }
  374. @keyframes fade {
  375. from { opacity: 0; }
  376. to { opacity: 1; }
  377. }
  378. @keyframes colorize {
  379. from { background-color: #f00; }
  380. 25% { background-color: #0f0; }
  381. to { background-color: #00f; }
  382. }
  383. div {
  384. width: 300dp;
  385. height: 300dp;
  386. background-color: #000;
  387. animation: 2s fade, 4s 1s colorize;
  388. }
  389. </style>
  390. </head>
  391. <body>
  392. <div/>
  393. </body>
  394. </rml>
  395. )";
  396. TEST_CASE("animation.multiple_values")
  397. {
  398. TestsSystemInterface* system_interface = TestsShell::GetTestsSystemInterface();
  399. Context* context = TestsShell::GetContext();
  400. const double dt = 0.1;
  401. const double t_fade_start = 0;
  402. const double t_colorize_start = 1;
  403. const double t_fade_final = 2;
  404. const double t_colorize_final = 5;
  405. struct TestCase {
  406. double time;
  407. float opacity_min;
  408. Colourb background_color;
  409. };
  410. const std::vector<TestCase> tests = {
  411. {t_fade_start, 0.f, {0, 0, 0}},
  412. {t_fade_start + dt, 0.01f, {0, 0, 0}},
  413. {t_colorize_start - dt, 0.4f, {0, 0, 0}},
  414. {t_colorize_start, 0.49f, {0, 0, 0}},
  415. {t_colorize_start + dt, 0.5f, {0xf1, 0x50, 0}},
  416. {t_fade_final - dt, 0.9f, {0x50, 0xf1, 0}},
  417. {t_fade_final, 0.99f, {0, 0xfe, 0x00}},
  418. {t_fade_final + dt, 1.0f, {0, 0xfa, 0x2e}},
  419. {t_colorize_final - dt, 1.0f, {0, 0x2e, 0xfa}},
  420. {t_colorize_final, 1.0f, {0, 0, 0xfe}},
  421. {t_colorize_final + dt, 1.0f, {0, 0, 0}},
  422. };
  423. {
  424. system_interface->SetTime(0.0);
  425. ElementDocument* document = context->LoadDocumentFromMemory(document_multiple_values_rml, "assets/");
  426. Element* element = document->GetChild(0);
  427. document->Show();
  428. double t = 0.f;
  429. for (const auto& test : tests)
  430. {
  431. // Simulate progression in small time steps since animations are constrained to 0.1s maximum step size.
  432. while (t < test.time)
  433. {
  434. t = Math::Min(t + dt, test.time);
  435. system_interface->SetTime(t);
  436. context->Update();
  437. }
  438. INFO("Time: ", test.time);
  439. CHECK(element->GetProperty<float>("opacity") >= test.opacity_min);
  440. CHECK(element->GetProperty<Colourb>("background-color") == test.background_color);
  441. }
  442. document->Close();
  443. }
  444. system_interface->SetTime(0.0);
  445. TestsShell::ShutdownShell();
  446. }
  447. static const String document_animation_multiple_values_rml = R"(
  448. <rml>
  449. <head>
  450. <title>Test</title>
  451. <link type="text/rcss" href="/assets/rml.rcss"/>
  452. <style>
  453. body {
  454. left: 0;
  455. top: 0;
  456. right: 0;
  457. bottom: 0;
  458. }
  459. @keyframes fadein {
  460. from {
  461. opacity: 0;
  462. visibility: hidden;
  463. }
  464. to {
  465. opacity: 1;
  466. visibility: visible;
  467. }
  468. }
  469. @keyframes fadeout {
  470. from {
  471. opacity: 1;
  472. visibility: visible;
  473. }
  474. to {
  475. opacity: 0;
  476. visibility: hidden;
  477. }
  478. }
  479. div {
  480. background-color: #c66;
  481. width: 300dp;
  482. height: 300dp;
  483. margin: auto;
  484. animation: 1s fadein, 1s 5s fadeout;
  485. }
  486. </style>
  487. </head>
  488. <body>
  489. <div id="div"/>
  490. </body>
  491. </rml>
  492. )";
  493. TEST_CASE("animation.multiple_overlapping")
  494. {
  495. TestsSystemInterface* system_interface = TestsShell::GetTestsSystemInterface();
  496. Context* context = TestsShell::GetContext();
  497. const float greater_than_zero = FLT_MIN;
  498. const double dt = 1.0 / 60.0;
  499. const double t0 = 0;
  500. const double t_delta = 0.1;
  501. const double t_fadein_final = 1;
  502. const double t_fadeout_start = 5;
  503. const double t_fadeout_final = 6;
  504. struct TestCase {
  505. double time;
  506. float opacity_min;
  507. bool is_visible;
  508. };
  509. const std::vector<TestCase> tests = {
  510. {t0, 0.f, true},
  511. {t0 + t_delta, greater_than_zero, true},
  512. {t_fadein_final - t_delta, 0.8f, true},
  513. {t_fadein_final, 1.f, true},
  514. {t_fadein_final + t_delta, 1.f, true},
  515. {t_fadeout_start - t_delta, 1.f, true},
  516. {t_fadeout_start, 1.f, true},
  517. {t_fadeout_start + t_delta, 0.8f, true},
  518. {t_fadeout_final - t_delta, greater_than_zero, true},
  519. {t_fadeout_final, 0.f, false},
  520. {t_fadeout_final + t_delta, 0.f, false},
  521. };
  522. // Currently we don't support animating the same property from multiple animations on the same element, see #608.
  523. // This test only checks that we submit warnings to the user. Once we support this feature, we can enable more checks in this test.
  524. {
  525. system_interface->SetNumExpectedWarnings(2);
  526. system_interface->SetTime(0.0);
  527. ElementDocument* document = context->LoadDocumentFromMemory(document_animation_multiple_values_rml, "assets/");
  528. Element* element = document->GetChild(0);
  529. document->Show();
  530. TestsShell::RenderLoop();
  531. double t = 0.f;
  532. for (const auto& test : tests)
  533. {
  534. while (t < test.time)
  535. {
  536. t = Math::Min(t + dt, test.time);
  537. system_interface->SetTime(t);
  538. context->Update();
  539. }
  540. INFO("Time: ", test.time);
  541. (void)(element->GetProperty<float>("opacity") == test.opacity_min);
  542. (void)(element->IsVisible() == test.is_visible);
  543. }
  544. document->Close();
  545. }
  546. system_interface->SetTime(0.0);
  547. TestsShell::ShutdownShell();
  548. }