DecoratorNinePatch.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 "DecoratorNinePatch.h"
  29. #include "../../Include/RmlUi/Core/Element.h"
  30. #include "../../Include/RmlUi/Core/Geometry.h"
  31. #include "../../Include/RmlUi/Core/ElementUtilities.h"
  32. namespace Rml {
  33. namespace Core {
  34. DecoratorNinePatch::DecoratorNinePatch()
  35. {
  36. }
  37. DecoratorNinePatch::~DecoratorNinePatch()
  38. {
  39. }
  40. bool DecoratorNinePatch::Initialise(const Rectangle& _rect_outer, const Rectangle& _rect_inner, const std::array<Property, 4>* _edges, const Texture& _texture)
  41. {
  42. rect_outer = _rect_outer;
  43. rect_inner = _rect_inner;
  44. if (_edges)
  45. edges = std::make_unique< std::array<Property, 4> >(*_edges);
  46. int texture_index = AddTexture(_texture);
  47. return (texture_index >= 0);
  48. }
  49. DecoratorDataHandle DecoratorNinePatch::GenerateElementData(Element* element) const
  50. {
  51. RenderInterface* render_interface = element->GetRenderInterface();
  52. const auto& computed = element->GetComputedValues();
  53. Geometry* data = new Geometry(element);
  54. const Texture* texture = GetTexture();
  55. data->SetTexture(texture);
  56. const Vector2i texture_dimensions_i = texture->GetDimensions(render_interface);
  57. const Vector2f texture_dimensions = { (float)texture_dimensions_i.x, (float)texture_dimensions_i.y };
  58. const Vector2f surface_dimensions = element->GetBox().GetSize(Box::PADDING);
  59. const float opacity = computed.opacity;
  60. Colourb quad_colour = computed.image_color;
  61. quad_colour.alpha = (byte)(opacity * (float)quad_colour.alpha);
  62. /* In the following, we operate on the four diagonal vertices in the grid, as they define the whole grid. */
  63. // Absolute texture coordinates 'px'
  64. Vector2f tex_pos[4];
  65. tex_pos[0] = { rect_outer.x, rect_outer.y };
  66. tex_pos[1] = { rect_inner.x, rect_inner.y };
  67. tex_pos[2] = { rect_inner.x + rect_inner.width, rect_inner.y + rect_inner.height };
  68. tex_pos[3] = { rect_outer.x + rect_outer.width, rect_outer.y + rect_outer.height };
  69. // Normalized texture coordinates [0, 1]
  70. Vector2f tex_coords[4];
  71. for (int i = 0; i < 4; i++)
  72. tex_coords[i] = tex_pos[i] / texture_dimensions;
  73. // Surface position [0, surface_dimensions]
  74. // Need to keep the corner patches at their native pixel size, but stretch the inner patches.
  75. Vector2f surface_pos[4];
  76. surface_pos[0] = { 0, 0 };
  77. surface_pos[1] = tex_pos[1] - tex_pos[0];
  78. surface_pos[2] = surface_dimensions - (tex_pos[3] - tex_pos[2]);
  79. surface_pos[3] = surface_dimensions;
  80. // Change the size of the edges if specified.
  81. if (edges)
  82. {
  83. const float dp_ratio = ElementUtilities::GetDensityIndependentPixelRatio(element);
  84. float lengths[4]; // top, right, bottom, left
  85. lengths[0] = element->ResolveNumericProperty(&(*edges)[0], dp_ratio * (surface_pos[1].y - surface_pos[0].y));
  86. lengths[1] = element->ResolveNumericProperty(&(*edges)[1], dp_ratio * (surface_pos[3].x - surface_pos[2].x));
  87. lengths[2] = element->ResolveNumericProperty(&(*edges)[2], dp_ratio * (surface_pos[3].y - surface_pos[2].y));
  88. lengths[3] = element->ResolveNumericProperty(&(*edges)[3], dp_ratio * (surface_pos[1].x - surface_pos[0].x));
  89. surface_pos[1].y = lengths[0];
  90. surface_pos[2].x = surface_dimensions.x - lengths[1];
  91. surface_pos[2].y = surface_dimensions.y - lengths[2];
  92. surface_pos[1].x = lengths[3];
  93. }
  94. // In case the surface dimensions are less than the size of the corners, we need to scale down the corner rectangles, one dimension at a time.
  95. const Vector2f surface_center_size = surface_pos[2] - surface_pos[1];
  96. for (int i = 0; i < 2; i++)
  97. {
  98. if (surface_center_size[i] < 0.0f)
  99. {
  100. float top_left_size = tex_pos[1][i] - tex_pos[0][i];
  101. float bottom_right_size = tex_pos[3][i] - tex_pos[2][i];
  102. surface_pos[1][i] = top_left_size / (top_left_size + bottom_right_size) * surface_dimensions[i];
  103. surface_pos[2][i] = surface_pos[1][i];
  104. }
  105. }
  106. /* Now we have all the coordinates we need. Expand the diagonal vertices to the 16 individual vertices. */
  107. std::vector<Vertex>& vertices = data->GetVertices();
  108. std::vector<int>& indices = data->GetIndices();
  109. vertices.resize(4 * 4);
  110. for (int y = 0; y < 4; y++)
  111. {
  112. for (int x = 0; x < 4; x++)
  113. {
  114. Vertex& vertex = vertices[y * 4 + x];
  115. vertex.colour = quad_colour;
  116. vertex.position = { surface_pos[x].x, surface_pos[y].y };
  117. vertex.tex_coord = { tex_coords[x].x, tex_coords[y].y };
  118. }
  119. }
  120. // Nine rectangles, two triangles per rectangle, three indices per triangle.
  121. indices.resize(9 * 2 * 3);
  122. // Fill in the indices one rectangle at a time.
  123. const int top_left_indices[9] = { 0, 1, 2, 4, 5, 6, 8, 9, 10 };
  124. for (int rectangle = 0; rectangle < 9; rectangle++)
  125. {
  126. int i = rectangle * 6;
  127. int top_left_index = top_left_indices[rectangle];
  128. indices[i] = top_left_index;
  129. indices[i + 1] = top_left_index + 4;
  130. indices[i + 2] = top_left_index + 1;
  131. indices[i + 3] = top_left_index + 1;
  132. indices[i + 4] = top_left_index + 4;
  133. indices[i + 5] = top_left_index + 5;
  134. }
  135. return reinterpret_cast<DecoratorDataHandle>(data);
  136. }
  137. void DecoratorNinePatch::ReleaseElementData(DecoratorDataHandle element_data) const
  138. {
  139. delete reinterpret_cast< Geometry* >(element_data);
  140. }
  141. void DecoratorNinePatch::RenderElement(Element* element, DecoratorDataHandle element_data) const
  142. {
  143. Geometry* data = reinterpret_cast< Geometry* >(element_data);
  144. data->Render(element->GetAbsoluteOffset(Box::PADDING).Round());
  145. }
  146. DecoratorNinePatchInstancer::DecoratorNinePatchInstancer()
  147. {
  148. sprite_outer_id = RegisterProperty("outer", "").AddParser("string").GetId();
  149. sprite_inner_id = RegisterProperty("inner", "").AddParser("string").GetId();
  150. edge_ids[0] = RegisterProperty("edge-top", "0px").AddParser("number_length_percent").GetId();
  151. edge_ids[1] = RegisterProperty("edge-right", "0px").AddParser("number_length_percent").GetId();
  152. edge_ids[2] = RegisterProperty("edge-bottom", "0px").AddParser("number_length_percent").GetId();
  153. edge_ids[3] = RegisterProperty("edge-left", "0px").AddParser("number_length_percent").GetId();
  154. RegisterShorthand("edge", "edge-top, edge-right, edge-bottom, edge-left", ShorthandType::Box);
  155. RMLUI_ASSERT(sprite_outer_id != PropertyId::Invalid && sprite_inner_id != PropertyId::Invalid);
  156. RegisterShorthand("decorator", "outer, inner, edge?", ShorthandType::RecursiveCommaSeparated);
  157. }
  158. DecoratorNinePatchInstancer::~DecoratorNinePatchInstancer()
  159. {
  160. }
  161. SharedPtr<Decorator> DecoratorNinePatchInstancer::InstanceDecorator(const String& RMLUI_UNUSED_PARAMETER(name), const PropertyDictionary& properties, const DecoratorInstancerInterface& interface)
  162. {
  163. RMLUI_UNUSED(name);
  164. bool edges_set = false;
  165. std::array<Property,4> edges;
  166. for (int i = 0; i < 4; i++)
  167. {
  168. edges[i] = *properties.GetProperty(edge_ids[i]);
  169. if (edges[i].value.Get(0.0f) != 0.0f)
  170. {
  171. edges_set = true;
  172. }
  173. }
  174. const Sprite* sprite_outer = nullptr;
  175. const Sprite* sprite_inner = nullptr;
  176. {
  177. const String sprite_name = properties.GetProperty(sprite_outer_id)->Get< String >();
  178. sprite_outer = interface.GetSprite(sprite_name);
  179. if (!sprite_outer)
  180. {
  181. Log::Message(Log::LT_WARNING, "Could not find sprite named '%s' in ninepatch decorator.", sprite_name.c_str());
  182. return nullptr;
  183. }
  184. }
  185. {
  186. const String sprite_name = properties.GetProperty(sprite_inner_id)->Get< String >();
  187. sprite_inner = interface.GetSprite(sprite_name);
  188. if (!sprite_inner)
  189. {
  190. Log::Message(Log::LT_WARNING, "Could not find sprite named '%s' in ninepatch decorator.", sprite_name.c_str());
  191. return nullptr;
  192. }
  193. }
  194. if (sprite_outer->sprite_sheet != sprite_inner->sprite_sheet)
  195. {
  196. Log::Message(Log::LT_WARNING, "The outer and inner sprites in a ninepatch decorator must be from the same sprite sheet.");
  197. return nullptr;
  198. }
  199. auto decorator = std::make_shared<DecoratorNinePatch>();
  200. if (!decorator->Initialise(sprite_outer->rectangle, sprite_inner->rectangle, (edges_set ? &edges : nullptr), sprite_outer->sprite_sheet->texture))
  201. return nullptr;
  202. return decorator;
  203. }
  204. }
  205. }