DecoratorTiledInstancer.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 "DecoratorTiledInstancer.h"
  29. #include "../../Include/RmlUi/Core/PropertyDefinition.h"
  30. #include "../../Include/RmlUi/Core/Spritesheet.h"
  31. namespace Rml {
  32. DecoratorTiledInstancer::DecoratorTiledInstancer(size_t num_tiles)
  33. {
  34. tile_property_ids.reserve(num_tiles);
  35. }
  36. void DecoratorTiledInstancer::RegisterTileProperty(const String& name, bool register_fit_modes)
  37. {
  38. TilePropertyIds ids = {};
  39. ids.src = RegisterProperty(CreateString(32, "%s-src", name.c_str()), "").AddParser("string").GetId();
  40. String additional_modes;
  41. if (register_fit_modes)
  42. {
  43. String fit_name = CreateString(32, "%s-fit", name.c_str());
  44. ids.fit = RegisterProperty(fit_name, "fill").AddParser("keyword", "fill, contain, cover, scale-none, scale-down").GetId();
  45. String align_x_name = CreateString(32, "%s-align-x", name.c_str());
  46. ids.align_x = RegisterProperty(align_x_name, "center").AddParser("keyword", "left, center, right").AddParser("length_percent").GetId();
  47. String align_y_name = CreateString(32, "%s-align-y", name.c_str());
  48. ids.align_y = RegisterProperty(align_y_name, "center").AddParser("keyword", "top, center, bottom").AddParser("length_percent").GetId();
  49. additional_modes += ", " + fit_name + ", " + align_x_name + ", " + align_y_name;
  50. }
  51. ids.orientation = RegisterProperty(CreateString(32, "%s-orientation", name.c_str()), "none")
  52. .AddParser("keyword", "none, flip-horizontal, flip-vertical, rotate-180")
  53. .GetId();
  54. RegisterShorthand(name,
  55. CreateString(256, ("%s-src, %s-orientation" + additional_modes).c_str(), name.c_str(), name.c_str(), name.c_str(), name.c_str(), name.c_str(),
  56. name.c_str()),
  57. ShorthandType::FallThrough);
  58. tile_property_ids.push_back(ids);
  59. }
  60. bool DecoratorTiledInstancer::GetTileProperties(DecoratorTiled::Tile* tiles, Texture* textures, size_t num_tiles_and_textures,
  61. const PropertyDictionary& properties, const DecoratorInstancerInterface& instancer_interface) const
  62. {
  63. RMLUI_ASSERT(num_tiles_and_textures == tile_property_ids.size());
  64. String previous_texture_name;
  65. Texture previous_texture;
  66. for (size_t i = 0; i < num_tiles_and_textures; i++)
  67. {
  68. const TilePropertyIds& ids = tile_property_ids[i];
  69. const Property* src_property = properties.GetProperty(ids.src);
  70. const String texture_name = src_property->Get<String>();
  71. // Skip the tile if it has no source name.
  72. // Declaring the name 'auto' is the same as an empty string. This gives an easy way to skip certain
  73. // tiles in a shorthand since we can't always declare an empty string.
  74. if (texture_name.empty() || texture_name == "auto")
  75. continue;
  76. // We are required to set default values before instancing the tile, thus, all properties should always be
  77. // dereferencable. If the debugger captures a zero-dereference, check that all properties for every tile is set
  78. // and default values are set just before instancing.
  79. DecoratorTiled::Tile& tile = tiles[i];
  80. Texture& texture = textures[i];
  81. // A tile is always either a sprite or an image.
  82. if (const Sprite* sprite = instancer_interface.GetSprite(texture_name))
  83. {
  84. tile.position = sprite->rectangle.Position();
  85. tile.size = sprite->rectangle.Size();
  86. tile.display_scale = sprite->sprite_sheet->display_scale;
  87. texture = sprite->sprite_sheet->texture;
  88. }
  89. else
  90. {
  91. // No sprite found, so assume that the name is an image source. Since the common use case is to specify the
  92. // same texture for all tiles, check the previous texture first before fetching from the global database.
  93. if (texture_name == previous_texture_name)
  94. {
  95. texture = previous_texture;
  96. }
  97. else
  98. {
  99. texture = instancer_interface.GetTexture(texture_name);
  100. if (!texture)
  101. return false;
  102. previous_texture_name = texture_name;
  103. previous_texture = texture;
  104. }
  105. }
  106. if (ids.fit != PropertyId::Invalid)
  107. {
  108. RMLUI_ASSERT(ids.align_x != PropertyId::Invalid && ids.align_y != PropertyId::Invalid);
  109. const Property& fit_property = *properties.GetProperty(ids.fit);
  110. tile.fit_mode = (DecoratorTiled::TileFitMode)fit_property.value.Get<int>();
  111. const Property* align_properties[2] = {properties.GetProperty(ids.align_x), properties.GetProperty(ids.align_y)};
  112. for (int dimension = 0; dimension < 2; dimension++)
  113. {
  114. using Style::LengthPercentage;
  115. LengthPercentage& align = tile.align[dimension];
  116. const Property& property = *align_properties[dimension];
  117. if (property.unit == Unit::KEYWORD)
  118. {
  119. enum { TOP_LEFT, CENTER, BOTTOM_RIGHT };
  120. switch (property.Get<int>())
  121. {
  122. case TOP_LEFT: align = LengthPercentage(LengthPercentage::Percentage, 0.0f); break;
  123. case CENTER: align = LengthPercentage(LengthPercentage::Percentage, 50.0f); break;
  124. case BOTTOM_RIGHT: align = LengthPercentage(LengthPercentage::Percentage, 100.0f); break;
  125. }
  126. }
  127. else if (property.unit == Unit::PERCENT)
  128. {
  129. align = LengthPercentage(LengthPercentage::Percentage, property.Get<float>());
  130. }
  131. else if (property.unit == Unit::PX)
  132. {
  133. align = LengthPercentage(LengthPercentage::Length, property.Get<float>());
  134. }
  135. else
  136. {
  137. Log::Message(Log::LT_WARNING, "Decorator alignment value is '%s' which uses an unsupported unit (use px, %%, or keyword)",
  138. property.ToString().c_str());
  139. }
  140. }
  141. }
  142. if (ids.orientation != PropertyId::Invalid)
  143. {
  144. const Property& orientation_property = *properties.GetProperty(ids.orientation);
  145. tile.orientation = (DecoratorTiled::TileOrientation)orientation_property.value.Get<int>();
  146. }
  147. }
  148. return true;
  149. }
  150. } // namespace Rml