DecoratorTiledImage.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include "DecoratorTiledImage.h"
  2. #include "../../Include/RmlUi/Core/Element.h"
  3. #include "../../Include/RmlUi/Core/Geometry.h"
  4. #include "../../Include/RmlUi/Core/MeshUtilities.h"
  5. #include "../../Include/RmlUi/Core/RenderManager.h"
  6. namespace Rml {
  7. DecoratorTiledImage::DecoratorTiledImage() {}
  8. DecoratorTiledImage::~DecoratorTiledImage() {}
  9. bool DecoratorTiledImage::Initialise(const Tile& _tile, Texture _texture)
  10. {
  11. tile = _tile;
  12. tile.texture_index = AddTexture(_texture);
  13. return (tile.texture_index >= 0);
  14. }
  15. DecoratorDataHandle DecoratorTiledImage::GenerateElementData(Element* element, BoxArea paint_area) const
  16. {
  17. // Calculate the tile's dimensions for this element.
  18. tile.CalculateDimensions(GetTexture());
  19. const ComputedValues& computed = element->GetComputedValues();
  20. const RenderBox render_box = element->GetRenderBox(paint_area);
  21. const Vector2f offset = render_box.GetFillOffset();
  22. const Vector2f size = render_box.GetFillSize();
  23. // Generate the geometry for the tile.
  24. Mesh mesh;
  25. tile.GenerateGeometry(mesh, computed, offset, size, tile.GetNaturalDimensions(element));
  26. Geometry* data = new Geometry(element->GetRenderManager()->MakeGeometry(std::move(mesh)));
  27. return reinterpret_cast<DecoratorDataHandle>(data);
  28. }
  29. void DecoratorTiledImage::ReleaseElementData(DecoratorDataHandle element_data) const
  30. {
  31. delete reinterpret_cast<Geometry*>(element_data);
  32. }
  33. void DecoratorTiledImage::RenderElement(Element* element, DecoratorDataHandle element_data) const
  34. {
  35. Geometry* data = reinterpret_cast<Geometry*>(element_data);
  36. data->Render(element->GetAbsoluteOffset(BoxArea::Border), GetTexture());
  37. }
  38. DecoratorTiledImageInstancer::DecoratorTiledImageInstancer() : DecoratorTiledInstancer(1)
  39. {
  40. RegisterTileProperty("image", true);
  41. RegisterShorthand("decorator", "image", ShorthandType::RecursiveRepeat);
  42. }
  43. DecoratorTiledImageInstancer::~DecoratorTiledImageInstancer() {}
  44. SharedPtr<Decorator> DecoratorTiledImageInstancer::InstanceDecorator(const String& /*name*/, const PropertyDictionary& properties,
  45. const DecoratorInstancerInterface& instancer_interface)
  46. {
  47. DecoratorTiled::Tile tile;
  48. Texture texture;
  49. if (!GetTileProperties(&tile, &texture, 1, properties, instancer_interface))
  50. return nullptr;
  51. auto decorator = MakeShared<DecoratorTiledImage>();
  52. if (!decorator->Initialise(tile, texture))
  53. return nullptr;
  54. return decorator;
  55. }
  56. } // namespace Rml