DecoratorTiledInstancer.cpp 7.0 KB

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