ElementProgressBar.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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 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 "../../Include/RmlUi/Controls/ElementProgressBar.h"
  29. #include "../../Include/RmlUi/Core/Math.h"
  30. #include "../../Include/RmlUi/Core/GeometryUtilities.h"
  31. #include "../../Include/RmlUi/Core/PropertyIdSet.h"
  32. #include "../../Include/RmlUi/Core/Factory.h"
  33. #include "../../Include/RmlUi/Core/ElementDocument.h"
  34. #include "../../Include/RmlUi/Core/StyleSheet.h"
  35. #include "../../Include/RmlUi/Core/ElementUtilities.h"
  36. #include "../../Include/RmlUi/Core/URL.h"
  37. namespace Rml {
  38. namespace Controls {
  39. ElementProgressBar::ElementProgressBar(const Core::String& tag) : Element(tag), direction(DefaultDirection), start_edge(DefaultStartEdge), value(0), fill(nullptr), rect_set(false), geometry(this)
  40. {
  41. geometry_dirty = false;
  42. texture_dirty = true;
  43. // Add the fill element as a non-DOM element.
  44. Core::ElementPtr fill_element = Core::Factory::InstanceElement(this, "*", "fill", Core::XMLAttributes());
  45. RMLUI_ASSERT(fill_element);
  46. fill = AppendChild(std::move(fill_element), false);
  47. }
  48. ElementProgressBar::~ElementProgressBar()
  49. {
  50. }
  51. float ElementProgressBar::GetValue() const
  52. {
  53. return value;
  54. }
  55. void ElementProgressBar::SetValue(float in_value)
  56. {
  57. SetAttribute("value", in_value);
  58. }
  59. void ElementProgressBar::OnRender()
  60. {
  61. // Some properties may change geometry without dirtying the layout, eg. opacity.
  62. if (geometry_dirty)
  63. GenerateGeometry();
  64. // Render the geometry at the fill element's content region.
  65. geometry.Render(fill->GetAbsoluteOffset().Round());
  66. }
  67. void ElementProgressBar::OnAttributeChange(const Rml::Core::ElementAttributes& changed_attributes)
  68. {
  69. Rml::Core::Element::OnAttributeChange(changed_attributes);
  70. if (changed_attributes.find("value") != changed_attributes.end())
  71. {
  72. value = Core::Math::Clamp( GetAttribute< float >("value", 0.0f), 0.0f, 1.0f);
  73. geometry_dirty = true;
  74. }
  75. if (changed_attributes.find("direction") != changed_attributes.end())
  76. {
  77. using DirectionNameList = std::array<Core::String, size_t(Direction::Count)>;
  78. static const DirectionNameList names = { "top", "right", "bottom", "left", "clockwise", "counter-clockwise" };
  79. direction = DefaultDirection;
  80. Core::String name = Core::StringUtilities::ToLower( GetAttribute< Core::String >("direction", "") );
  81. auto it = std::find(names.begin(), names.end(), name);
  82. size_t index = size_t(it - names.begin());
  83. if (index < size_t(Direction::Count))
  84. direction = Direction(index);
  85. geometry_dirty = true;
  86. }
  87. if (changed_attributes.find("start-edge") != changed_attributes.end())
  88. {
  89. using StartEdgeNameList = std::array<Core::String, size_t(StartEdge::Count)>;
  90. static const StartEdgeNameList names = { "top", "right", "bottom", "left" };
  91. start_edge = DefaultStartEdge;
  92. Core::String name = Core::StringUtilities::ToLower(GetAttribute< Core::String >("start-edge", ""));
  93. auto it = std::find(names.begin(), names.end(), name);
  94. size_t index = size_t(it - names.begin());
  95. if (index < size_t(StartEdge::Count))
  96. start_edge = StartEdge(index);
  97. geometry_dirty = true;
  98. }
  99. }
  100. void ElementProgressBar::OnPropertyChange(const Core::PropertyIdSet& changed_properties)
  101. {
  102. Element::OnPropertyChange(changed_properties);
  103. if (changed_properties.Contains(Core::PropertyId::ImageColor) ||
  104. changed_properties.Contains(Core::PropertyId::Opacity)) {
  105. geometry_dirty = true;
  106. }
  107. if (changed_properties.Contains(Core::PropertyId::FillImage)) {
  108. texture_dirty = true;
  109. }
  110. }
  111. void ElementProgressBar::OnResize()
  112. {
  113. using Core::Box;
  114. using Core::Vector2f;
  115. const Vector2f element_size = GetBox().GetSize();
  116. // Build and set the 'fill' element's box. Here we are mainly interested in all the edge sizes set by the user.
  117. // The content size of the box is here scaled to fit inside the progress bar. Then, during 'CreateGeometry()',
  118. // the 'fill' element's content size is further shrunk according to 'value' along the proper direction.
  119. Box fill_box;
  120. Core::ElementUtilities::BuildBox(fill_box, element_size, fill);
  121. const Vector2f margin_top_left(
  122. fill_box.GetEdge(Box::MARGIN, Box::LEFT),
  123. fill_box.GetEdge(Box::MARGIN, Box::TOP)
  124. );
  125. const Vector2f edge_size = fill_box.GetSize(Box::MARGIN) - fill_box.GetSize(Box::CONTENT);
  126. fill_offset = GetBox().GetPosition() + margin_top_left;
  127. fill_size = element_size - edge_size;
  128. fill_box.SetContent(fill_size);
  129. fill->SetBox(fill_box);
  130. geometry_dirty = true;
  131. }
  132. void ElementProgressBar::GenerateGeometry()
  133. {
  134. using Core::Vector2f;
  135. Vector2f render_size = fill_size;
  136. {
  137. // Size and offset the fill element depending on the progressbar value.
  138. Vector2f offset = fill_offset;
  139. switch (direction) {
  140. case Direction::Top:
  141. render_size.y = fill_size.y * value;
  142. offset.y = fill_offset.y + fill_size.y - render_size.y;
  143. break;
  144. case Direction::Right:
  145. render_size.x = fill_size.x * value;
  146. break;
  147. case Direction::Bottom:
  148. render_size.y = fill_size.y * value;
  149. break;
  150. case Direction::Left:
  151. render_size.x = fill_size.x * value;
  152. offset.x = fill_offset.x + fill_size.x - render_size.x;
  153. break;
  154. case Direction::Clockwise:
  155. case Direction::CounterClockwise:
  156. // Circular progress bars cannot use a box to shape the fill element, instead we need to manually create the geometry from the image texture.
  157. // Thus, we leave the size and offset untouched as a canvas for the manual geometry.
  158. break;
  159. RMLUI_UNUSED_SWITCH_ENUM(Direction::Count);
  160. }
  161. Core::Box fill_box = fill->GetBox();
  162. fill_box.SetContent(render_size);
  163. fill->SetBox(fill_box);
  164. fill->SetOffset(offset, this);
  165. }
  166. if (texture_dirty)
  167. LoadTexture();
  168. geometry.Release(true);
  169. geometry_dirty = false;
  170. // If we don't have a fill texture, then there is no need to generate manual geometry, and we are done here.
  171. // Instead, users can style the fill element eg. by decorators.
  172. if (!texture)
  173. return;
  174. // Otherwise, the 'fill-image' property is set, let's generate its geometry.
  175. auto& vertices = geometry.GetVertices();
  176. auto& indices = geometry.GetIndices();
  177. Vector2f texcoords[2];
  178. if (rect_set)
  179. {
  180. Vector2f texture_dimensions((float)texture.GetDimensions(GetRenderInterface()).x, (float)texture.GetDimensions(GetRenderInterface()).y);
  181. if (texture_dimensions.x == 0)
  182. texture_dimensions.x = 1;
  183. if (texture_dimensions.y == 0)
  184. texture_dimensions.y = 1;
  185. texcoords[0].x = rect.x / texture_dimensions.x;
  186. texcoords[0].y = rect.y / texture_dimensions.y;
  187. texcoords[1].x = (rect.x + rect.width) / texture_dimensions.x;
  188. texcoords[1].y = (rect.y + rect.height) / texture_dimensions.y;
  189. }
  190. else
  191. {
  192. texcoords[0] = Vector2f(0, 0);
  193. texcoords[1] = Vector2f(1, 1);
  194. }
  195. Core::Colourb quad_colour;
  196. {
  197. const Core::ComputedValues& computed = GetComputedValues();
  198. const float opacity = computed.opacity;
  199. quad_colour = computed.image_color;
  200. quad_colour.alpha = (Core::byte)(opacity * (float)quad_colour.alpha);
  201. }
  202. switch (direction)
  203. {
  204. // For the top, right, bottom, left directions the fill element already describes where we should draw the fill,
  205. // we only need to generate the final texture coordinates here.
  206. case Direction::Top: texcoords[0].y = texcoords[0].y + (1.0f - value) * (texcoords[1].y - texcoords[0].y); break;
  207. case Direction::Right: texcoords[1].x = texcoords[0].x + value * (texcoords[1].x - texcoords[0].x); break;
  208. case Direction::Bottom: texcoords[1].y = texcoords[0].y + value * (texcoords[1].y - texcoords[0].y); break;
  209. case Direction::Left: texcoords[0].x = texcoords[0].x + (1.0f - value) * (texcoords[1].x - texcoords[0].x); break;
  210. case Direction::Clockwise:
  211. case Direction::CounterClockwise:
  212. {
  213. // The circular directions require custom geometry as a box is insufficient.
  214. // We divide the "circle" into eight parts, here called octants, such that each part can be represented by a triangle.
  215. // 'num_octants' tells us how many of these are completely or partially filled.
  216. const int num_octants = Core::Math::Clamp(Core::Math::RoundUpToInteger(8.f * value), 0, 8);
  217. const int num_vertices = 2 + num_octants;
  218. const int num_triangles = num_octants;
  219. const bool cw = (direction == Direction::Clockwise);
  220. if (num_octants == 0)
  221. break;
  222. vertices.resize(num_vertices);
  223. indices.resize(3 * num_triangles);
  224. RMLUI_ASSERT(int(start_edge) >= int(StartEdge::Top) && int(start_edge) <= int(StartEdge::Left));
  225. // The octant our "circle" expands from.
  226. const int start_octant = 2 * int(start_edge);
  227. // Positions along the unit square (clockwise, index 0 on top)
  228. const float x[8] = { 0, 1, 1, 1, 0, -1, -1, -1 };
  229. const float y[8] = { -1, -1, 0, 1, 1, 1, 0, -1 };
  230. // Set the position of the octant vertices to be rendered.
  231. for (int i = 0; i <= num_octants; i++)
  232. {
  233. int j = (cw ? i : 8 - i);
  234. j = ((j + start_octant) % 8);
  235. vertices[i].position = Vector2f(x[j], y[j]);
  236. }
  237. // Find the position of the vertex representing the partially filled triangle.
  238. if (value < 1.f)
  239. {
  240. using namespace Core::Math;
  241. const float angle_offset = float(start_octant) / 8.f * 2.f * RMLUI_PI;
  242. const float angle = angle_offset + (cw ? 1.f : -1.f) * value * 2.f * RMLUI_PI;
  243. Vector2f pos(Sin(angle), -Cos(angle));
  244. // Project it from the circle towards the surrounding unit square.
  245. pos = pos / Max(AbsoluteValue(pos.x), AbsoluteValue(pos.y));
  246. vertices[num_octants].position = pos;
  247. }
  248. const int i_center = num_vertices - 1;
  249. vertices[i_center].position = Vector2f(0, 0);
  250. for (int i = 0; i < num_triangles; i++)
  251. {
  252. indices[i * 3 + 0] = i_center;
  253. indices[i * 3 + 2] = i;
  254. indices[i * 3 + 1] = i + 1;
  255. }
  256. for (int i = 0; i < num_vertices; i++)
  257. {
  258. // Transform position from [-1, 1] to [0, 1] and then to [0, size]
  259. const Vector2f pos = (Vector2f(1, 1) + vertices[i].position) * 0.5f;
  260. vertices[i].position = pos * render_size;
  261. vertices[i].tex_coord = texcoords[0] + pos * (texcoords[1] - texcoords[0]);
  262. vertices[i].colour = quad_colour;
  263. }
  264. }
  265. break;
  266. RMLUI_UNUSED_SWITCH_ENUM(Direction::Count);
  267. }
  268. const bool is_circular = (direction == Direction::Clockwise || direction == Direction::CounterClockwise);
  269. if(!is_circular)
  270. {
  271. vertices.resize(4);
  272. indices.resize(6);
  273. Rml::Core::GeometryUtilities::GenerateQuad(&vertices[0], &indices[0], Vector2f(0), render_size, quad_colour, texcoords[0], texcoords[1]);
  274. }
  275. }
  276. bool ElementProgressBar::LoadTexture()
  277. {
  278. texture_dirty = false;
  279. geometry_dirty = true;
  280. rect_set = false;
  281. Core::String name;
  282. if (auto property = fill->GetLocalProperty(Core::PropertyId::FillImage))
  283. name = property->Get<Core::String>();
  284. Core::ElementDocument* document = GetOwnerDocument();
  285. bool texture_set = false;
  286. if(!name.empty() && document)
  287. {
  288. // Check for a sprite first, this takes precedence.
  289. if (auto& style_sheet = document->GetStyleSheet())
  290. {
  291. if (const Core::Sprite* sprite = style_sheet->GetSprite(name))
  292. {
  293. rect = sprite->rectangle;
  294. rect_set = true;
  295. texture = sprite->sprite_sheet->texture;
  296. texture_set = true;
  297. }
  298. }
  299. // Otherwise, treat it as a path
  300. if (!texture_set)
  301. {
  302. Core::URL source_url;
  303. source_url.SetURL(document->GetSourceURL());
  304. texture.Set(name, source_url.GetPath());
  305. texture_set = true;
  306. }
  307. }
  308. if (!texture_set)
  309. {
  310. texture = {};
  311. rect = {};
  312. }
  313. // Set the texture onto our geometry object.
  314. geometry.SetTexture(&texture);
  315. return true;
  316. }
  317. }
  318. }