DecoratorDefender.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 "DecoratorDefender.h"
  29. #include <RmlUi/Core/Core.h>
  30. #include <RmlUi/Core/Element.h>
  31. #include <RmlUi/Core/Geometry.h>
  32. #include <RmlUi/Core/Math.h>
  33. #include <RmlUi/Core/MeshUtilities.h>
  34. #include <RmlUi/Core/PropertyDefinition.h>
  35. #include <RmlUi/Core/RenderManager.h>
  36. #include <RmlUi/Core/Texture.h>
  37. #include <RmlUi/Core/Types.h>
  38. struct DecoratorDefenderElementData {
  39. Rml::Texture texture;
  40. Rml::Geometry geometry;
  41. };
  42. DecoratorDefender::~DecoratorDefender() {}
  43. bool DecoratorDefender::Initialise(const Rml::Texture& texture)
  44. {
  45. image_index = AddTexture(texture);
  46. if (image_index == -1)
  47. {
  48. return false;
  49. }
  50. return true;
  51. }
  52. Rml::DecoratorDataHandle DecoratorDefender::GenerateElementData(Rml::Element* element, Rml::BoxArea /*paint_area*/) const
  53. {
  54. Rml::RenderManager* render_manager = element->GetRenderManager();
  55. if (!render_manager)
  56. return Rml::Decorator::INVALID_DECORATORDATAHANDLE;
  57. Rml::Vector2f position = element->GetAbsoluteOffset(Rml::BoxArea::Padding);
  58. Rml::Vector2f size = element->GetBox().GetSize(Rml::BoxArea::Padding);
  59. Rml::Math::SnapToPixelGrid(position, size);
  60. Rml::ColourbPremultiplied color = element->GetProperty<Rml::Colourb>("image-color").ToPremultiplied();
  61. Rml::Mesh mesh;
  62. Rml::MeshUtilities::GenerateQuad(mesh, Rml::Vector2f(0.f), size, color);
  63. DecoratorDefenderElementData* element_data = new DecoratorDefenderElementData{
  64. GetTexture(image_index),
  65. render_manager->MakeGeometry(std::move(mesh)),
  66. };
  67. if (!element_data->texture || !element_data->geometry)
  68. return Rml::Decorator::INVALID_DECORATORDATAHANDLE;
  69. return reinterpret_cast<Rml::DecoratorDataHandle>(element_data);
  70. }
  71. void DecoratorDefender::ReleaseElementData(Rml::DecoratorDataHandle element_data_handle) const
  72. {
  73. delete reinterpret_cast<DecoratorDefenderElementData*>(element_data_handle);
  74. }
  75. void DecoratorDefender::RenderElement(Rml::Element* element, Rml::DecoratorDataHandle element_data_handle) const
  76. {
  77. Rml::Vector2f position = element->GetAbsoluteOffset(Rml::BoxArea::Padding).Round();
  78. DecoratorDefenderElementData* element_data = reinterpret_cast<DecoratorDefenderElementData*>(element_data_handle);
  79. element_data->geometry.Render(position, element_data->texture);
  80. }
  81. DecoratorInstancerDefender::DecoratorInstancerDefender()
  82. {
  83. id_image_src = RegisterProperty("image-src", "").AddParser("string").GetId();
  84. RegisterShorthand("decorator", "image-src", Rml::ShorthandType::FallThrough);
  85. }
  86. DecoratorInstancerDefender::~DecoratorInstancerDefender() {}
  87. Rml::SharedPtr<Rml::Decorator> DecoratorInstancerDefender::InstanceDecorator(const Rml::String& /*name*/, const Rml::PropertyDictionary& properties,
  88. const Rml::DecoratorInstancerInterface& instancer_interface)
  89. {
  90. const Rml::Property* image_source_property = properties.GetProperty(id_image_src);
  91. Rml::String image_source = image_source_property->Get<Rml::String>();
  92. Rml::Texture texture = instancer_interface.GetTexture(image_source);
  93. auto decorator = Rml::MakeShared<DecoratorDefender>();
  94. if (decorator->Initialise(texture))
  95. return decorator;
  96. return nullptr;
  97. }