DecoratorTiledInstancer.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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, repeat, repeat-x, repeat-y").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. const Sprite* sprite = instancer_interface.GetSprite(texture_name);
  82. // A tile is always either a sprite or an image.
  83. if (sprite)
  84. {
  85. tile.position = sprite->rectangle.Position();
  86. tile.size = sprite->rectangle.Size();
  87. tile.display_scale = sprite->sprite_sheet->display_scale;
  88. texture = sprite->sprite_sheet->texture;
  89. }
  90. else
  91. {
  92. // No sprite found, so assume that the name is an image source. Since the common use case is to specify the
  93. // same texture for all tiles, check the previous texture first before fetching from the global database.
  94. if (texture_name == previous_texture_name)
  95. {
  96. texture = previous_texture;
  97. }
  98. else
  99. {
  100. texture = instancer_interface.GetTexture(texture_name);
  101. if (!texture)
  102. return false;
  103. previous_texture_name = texture_name;
  104. previous_texture = texture;
  105. }
  106. }
  107. if (ids.fit != PropertyId::Invalid)
  108. {
  109. RMLUI_ASSERT(ids.align_x != PropertyId::Invalid && ids.align_y != PropertyId::Invalid);
  110. const Property& fit_property = *properties.GetProperty(ids.fit);
  111. tile.fit_mode = (DecoratorTiled::TileFitMode)fit_property.value.Get<int>();
  112. if (sprite && (tile.fit_mode == DecoratorTiled::TileFitMode::REPEAT ||
  113. tile.fit_mode == DecoratorTiled::TileFitMode::REPEAT_X ||
  114. tile.fit_mode == DecoratorTiled::TileFitMode::REPEAT_Y)) {
  115. Log::Message(Log::LT_WARNING, "Decorator fit value is '%s', which is incompatible with a spritesheet",
  116. fit_property.ToString().c_str());
  117. return false;
  118. }
  119. const Property* align_properties[2] = {properties.GetProperty(ids.align_x), properties.GetProperty(ids.align_y)};
  120. for (int dimension = 0; dimension < 2; dimension++)
  121. {
  122. using Style::LengthPercentage;
  123. LengthPercentage& align = tile.align[dimension];
  124. const Property& property = *align_properties[dimension];
  125. if (property.unit == Unit::KEYWORD)
  126. {
  127. enum { TOP_LEFT, CENTER, BOTTOM_RIGHT };
  128. switch (property.Get<int>())
  129. {
  130. case TOP_LEFT: align = LengthPercentage(LengthPercentage::Percentage, 0.0f); break;
  131. case CENTER: align = LengthPercentage(LengthPercentage::Percentage, 50.0f); break;
  132. case BOTTOM_RIGHT: align = LengthPercentage(LengthPercentage::Percentage, 100.0f); break;
  133. }
  134. }
  135. else if (property.unit == Unit::PERCENT)
  136. {
  137. align = LengthPercentage(LengthPercentage::Percentage, property.Get<float>());
  138. }
  139. else if (property.unit == Unit::PX)
  140. {
  141. align = LengthPercentage(LengthPercentage::Length, property.Get<float>());
  142. }
  143. else
  144. {
  145. Log::Message(Log::LT_WARNING, "Decorator alignment value is '%s' which uses an unsupported unit (use px, %%, or keyword)",
  146. property.ToString().c_str());
  147. }
  148. }
  149. }
  150. if (ids.orientation != PropertyId::Invalid)
  151. {
  152. const Property& orientation_property = *properties.GetProperty(ids.orientation);
  153. tile.orientation = (DecoratorTiled::TileOrientation)orientation_property.value.Get<int>();
  154. }
  155. }
  156. return true;
  157. }
  158. } // namespace Rml