DecoratorTiledInstancer.cpp 7.2 KB

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