DecoratorNinePatch.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #include "DecoratorNinePatch.h"
  2. #include "../../Include/RmlUi/Core/ComputedValues.h"
  3. #include "../../Include/RmlUi/Core/Element.h"
  4. #include "../../Include/RmlUi/Core/ElementUtilities.h"
  5. #include "../../Include/RmlUi/Core/Geometry.h"
  6. #include "../../Include/RmlUi/Core/PropertyDefinition.h"
  7. namespace Rml {
  8. DecoratorNinePatch::DecoratorNinePatch() {}
  9. DecoratorNinePatch::~DecoratorNinePatch() {}
  10. bool DecoratorNinePatch::Initialise(const Rectanglef& _rect_outer, const Rectanglef& _rect_inner, const Array<NumericValue, 4>* _edges,
  11. Texture _texture, float _display_scale)
  12. {
  13. rect_outer = _rect_outer;
  14. rect_inner = _rect_inner;
  15. display_scale = _display_scale;
  16. if (_edges)
  17. edges = MakeUnique<Array<NumericValue, 4>>(*_edges);
  18. int texture_index = AddTexture(_texture);
  19. return (texture_index >= 0);
  20. }
  21. DecoratorDataHandle DecoratorNinePatch::GenerateElementData(Element* element, BoxArea paint_area) const
  22. {
  23. const auto& computed = element->GetComputedValues();
  24. Texture texture = GetTexture();
  25. const Vector2f texture_dimensions(texture.GetDimensions());
  26. const RenderBox render_box = element->GetRenderBox(paint_area);
  27. const Vector2f surface_offset = render_box.GetFillOffset();
  28. const Vector2f surface_dimensions = render_box.GetFillSize();
  29. const ColourbPremultiplied quad_colour = computed.image_color().ToPremultiplied(computed.opacity());
  30. /* In the following, we operate on the four diagonal vertices in the grid, as they define the whole grid. */
  31. // Absolute texture coordinates 'px'
  32. Vector2f tex_pos[4] = {
  33. rect_outer.TopLeft(),
  34. rect_inner.TopLeft(),
  35. rect_inner.BottomRight(),
  36. rect_outer.BottomRight(),
  37. };
  38. // Normalized texture coordinates [0, 1]
  39. Vector2f tex_coords[4];
  40. for (int i = 0; i < 4; i++)
  41. tex_coords[i] = tex_pos[i] / texture_dimensions;
  42. // Natural size is determined from the raw pixel size multiplied by the dp-ratio and the sprite's
  43. // display scale (determined by eg. the inverse of spritesheet's 'src-scale').
  44. const float scale_raw_to_natural_dimensions = ElementUtilities::GetDensityIndependentPixelRatio(element) * display_scale;
  45. // Surface position in pixels [0, surface_dimensions]
  46. // Need to keep the corner patches at their natural size, but stretch the inner patches.
  47. Vector2f surface_pos[4];
  48. surface_pos[0] = {0, 0};
  49. surface_pos[1] = (tex_pos[1] - tex_pos[0]) * scale_raw_to_natural_dimensions;
  50. surface_pos[2] = surface_dimensions - (tex_pos[3] - tex_pos[2]) * scale_raw_to_natural_dimensions;
  51. surface_pos[3] = surface_dimensions;
  52. // Change the size of the edges if specified.
  53. if (edges)
  54. {
  55. float lengths[4]; // top, right, bottom, left
  56. lengths[0] = element->ResolveNumericValue((*edges)[0], (surface_pos[1].y - surface_pos[0].y));
  57. lengths[1] = element->ResolveNumericValue((*edges)[1], (surface_pos[3].x - surface_pos[2].x));
  58. lengths[2] = element->ResolveNumericValue((*edges)[2], (surface_pos[3].y - surface_pos[2].y));
  59. lengths[3] = element->ResolveNumericValue((*edges)[3], (surface_pos[1].x - surface_pos[0].x));
  60. surface_pos[1].y = lengths[0];
  61. surface_pos[2].x = surface_dimensions.x - lengths[1];
  62. surface_pos[2].y = surface_dimensions.y - lengths[2];
  63. surface_pos[1].x = lengths[3];
  64. }
  65. // 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.
  66. const Vector2f surface_center_size = surface_pos[2] - surface_pos[1];
  67. for (int i = 0; i < 2; i++)
  68. {
  69. if (surface_center_size[i] < 0.0f)
  70. {
  71. float top_left_size = tex_pos[1][i] - tex_pos[0][i];
  72. float bottom_right_size = tex_pos[3][i] - tex_pos[2][i];
  73. surface_pos[1][i] = top_left_size / (top_left_size + bottom_right_size) * surface_dimensions[i];
  74. surface_pos[2][i] = surface_pos[1][i];
  75. }
  76. }
  77. // Now offset all positions, relative to the border box.
  78. for (Vector2f& surface_pos_entry : surface_pos)
  79. surface_pos_entry += surface_offset;
  80. // Round the inner corners
  81. surface_pos[1] = surface_pos[1].Round();
  82. surface_pos[2] = surface_pos[2].Round();
  83. /* Now we have all the coordinates we need. Expand the diagonal vertices to the 16 individual vertices. */
  84. Mesh mesh;
  85. Vector<Vertex>& vertices = mesh.vertices;
  86. Vector<int>& indices = mesh.indices;
  87. vertices.resize(4 * 4);
  88. for (int y = 0; y < 4; y++)
  89. {
  90. for (int x = 0; x < 4; x++)
  91. {
  92. Vertex& vertex = vertices[y * 4 + x];
  93. vertex.colour = quad_colour;
  94. vertex.position = {surface_pos[x].x, surface_pos[y].y};
  95. vertex.tex_coord = {tex_coords[x].x, tex_coords[y].y};
  96. }
  97. }
  98. // Nine rectangles, two triangles per rectangle, three indices per triangle.
  99. indices.resize(9 * 2 * 3);
  100. // Fill in the indices one rectangle at a time.
  101. const int top_left_indices[9] = {0, 1, 2, 4, 5, 6, 8, 9, 10};
  102. for (int rectangle = 0; rectangle < 9; rectangle++)
  103. {
  104. int i = rectangle * 6;
  105. int top_left_index = top_left_indices[rectangle];
  106. indices[i] = top_left_index;
  107. indices[i + 1] = top_left_index + 4;
  108. indices[i + 2] = top_left_index + 1;
  109. indices[i + 3] = top_left_index + 1;
  110. indices[i + 4] = top_left_index + 4;
  111. indices[i + 5] = top_left_index + 5;
  112. }
  113. Geometry* data = new Geometry(element->GetRenderManager()->MakeGeometry(std::move(mesh)));
  114. return reinterpret_cast<DecoratorDataHandle>(data);
  115. }
  116. void DecoratorNinePatch::ReleaseElementData(DecoratorDataHandle element_data) const
  117. {
  118. delete reinterpret_cast<Geometry*>(element_data);
  119. }
  120. void DecoratorNinePatch::RenderElement(Element* element, DecoratorDataHandle element_data) const
  121. {
  122. Geometry* data = reinterpret_cast<Geometry*>(element_data);
  123. data->Render(element->GetAbsoluteOffset(BoxArea::Border), GetTexture());
  124. }
  125. DecoratorNinePatchInstancer::DecoratorNinePatchInstancer()
  126. {
  127. sprite_outer_id = RegisterProperty("outer", "").AddParser("string").GetId();
  128. sprite_inner_id = RegisterProperty("inner", "").AddParser("string").GetId();
  129. edge_ids[0] = RegisterProperty("edge-top", "0px").AddParser("number_length_percent").GetId();
  130. edge_ids[1] = RegisterProperty("edge-right", "0px").AddParser("number_length_percent").GetId();
  131. edge_ids[2] = RegisterProperty("edge-bottom", "0px").AddParser("number_length_percent").GetId();
  132. edge_ids[3] = RegisterProperty("edge-left", "0px").AddParser("number_length_percent").GetId();
  133. RegisterShorthand("edge", "edge-top, edge-right, edge-bottom, edge-left", ShorthandType::Box);
  134. RMLUI_ASSERT(sprite_outer_id != PropertyId::Invalid && sprite_inner_id != PropertyId::Invalid);
  135. RegisterShorthand("decorator", "outer, inner, edge?", ShorthandType::RecursiveCommaSeparated);
  136. }
  137. DecoratorNinePatchInstancer::~DecoratorNinePatchInstancer() {}
  138. SharedPtr<Decorator> DecoratorNinePatchInstancer::InstanceDecorator(const String& /*name*/, const PropertyDictionary& properties,
  139. const DecoratorInstancerInterface& instancer_interface)
  140. {
  141. bool edges_set = false;
  142. Array<NumericValue, 4> edges;
  143. for (int i = 0; i < 4; i++)
  144. {
  145. edges[i] = properties.GetProperty(edge_ids[i])->GetNumericValue();
  146. if (edges[i].number != 0.0f)
  147. {
  148. edges_set = true;
  149. }
  150. }
  151. const Sprite* sprite_outer = nullptr;
  152. const Sprite* sprite_inner = nullptr;
  153. {
  154. const String sprite_name = properties.GetProperty(sprite_outer_id)->Get<String>();
  155. sprite_outer = instancer_interface.GetSprite(sprite_name);
  156. if (!sprite_outer)
  157. {
  158. Log::Message(Log::LT_WARNING, "Could not find sprite named '%s' in ninepatch decorator.", sprite_name.c_str());
  159. return nullptr;
  160. }
  161. }
  162. {
  163. const String sprite_name = properties.GetProperty(sprite_inner_id)->Get<String>();
  164. sprite_inner = instancer_interface.GetSprite(sprite_name);
  165. if (!sprite_inner)
  166. {
  167. Log::Message(Log::LT_WARNING, "Could not find sprite named '%s' in ninepatch decorator.", sprite_name.c_str());
  168. return nullptr;
  169. }
  170. }
  171. if (sprite_outer->sprite_sheet != sprite_inner->sprite_sheet)
  172. {
  173. Log::Message(Log::LT_WARNING, "The outer and inner sprites in a ninepatch decorator must be from the same sprite sheet.");
  174. return nullptr;
  175. }
  176. auto decorator = MakeShared<DecoratorNinePatch>();
  177. if (!decorator->Initialise(sprite_outer->rectangle, sprite_inner->rectangle, (edges_set ? &edges : nullptr),
  178. sprite_outer->sprite_sheet->texture_source.GetTexture(instancer_interface.GetRenderManager()), sprite_outer->sprite_sheet->display_scale))
  179. {
  180. return nullptr;
  181. }
  182. return decorator;
  183. }
  184. } // namespace Rml