DecoratorTiledInstancer.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 "precompiled.h"
  29. #include "DecoratorTiledInstancer.h"
  30. #include "../../Include/RmlUi/Core/PropertyDefinition.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_repeat_modes)
  39. {
  40. TilePropertyIds ids = {};
  41. ids.src = RegisterProperty(CreateString(32, "%s-src", name.c_str()), "").AddParser("string").GetId();
  42. ids.x = RegisterProperty(CreateString(32, "%s-x", name.c_str()), "0px").AddParser("number_length_percent").GetId();
  43. ids.y = RegisterProperty(CreateString(32, "%s-y", name.c_str()), "0px").AddParser("number_length_percent").GetId();
  44. ids.width = RegisterProperty(CreateString(32, "%s-width", name.c_str()), "1.0").AddParser("number_length_percent").GetId();
  45. ids.height = RegisterProperty(CreateString(32, "%s-height", name.c_str()), "1.0").AddParser("number_length_percent").GetId();
  46. RegisterShorthand(CreateString(32, "%s-pos", name.c_str()), CreateString(64, "%s-x, %s-y", name.c_str(), name.c_str()), ShorthandType::FallThrough);
  47. RegisterShorthand(CreateString(32, "%s-size", name.c_str()), CreateString(64, "%s-width, %s-height", name.c_str(), name.c_str()), ShorthandType::FallThrough);
  48. ids.orientation = RegisterProperty(CreateString(32, "%s-orientation", name.c_str()), "none")
  49. .AddParser("keyword", "none, rotate-90, rotate-180, rotate-270, flip-horizontal, flip-vertical")
  50. .GetId();
  51. if (register_repeat_modes)
  52. {
  53. ids.repeat = RegisterProperty(CreateString(32, "%s-repeat", name.c_str()), "stretch")
  54. .AddParser("keyword", "stretch, clamp-stretch, clamp-truncate, repeat-stretch, repeat-truncate")
  55. .GetId();
  56. RegisterShorthand(name, CreateString(256, "%s-src, %s-repeat, %s-x, %s-y, %s-width, %s-height, %s-orientation", name.c_str(), name.c_str(), name.c_str(), name.c_str(), name.c_str(), name.c_str(), name.c_str()), ShorthandType::FallThrough);
  57. }
  58. else
  59. {
  60. ids.repeat = PropertyId::Invalid;
  61. RegisterShorthand(name, CreateString(256, "%s-src, %s-x, %s-y, %s-width, %s-height, %s-orientation", name.c_str(), name.c_str(), name.c_str(), name.c_str(), name.c_str(), name.c_str()), ShorthandType::FallThrough);
  62. }
  63. tile_property_ids.push_back(ids);
  64. }
  65. // Loads a single texture coordinate value from the properties.
  66. static void LoadTexCoord(const Property& property, float& tex_coord, bool& tex_coord_absolute)
  67. {
  68. tex_coord = property.value.Get< float >();
  69. if (property.unit == Property::PX)
  70. {
  71. tex_coord_absolute = true;
  72. }
  73. else
  74. {
  75. tex_coord_absolute = false;
  76. if (property.unit == Property::PERCENT)
  77. tex_coord *= 0.01f;
  78. }
  79. }
  80. // Retrieves all the properties for a tile from the property dictionary.
  81. bool DecoratorTiledInstancer::GetTileProperties(DecoratorTiled::Tile* tiles, Texture* textures, size_t num_tiles_and_textures, const PropertyDictionary& properties, const DecoratorInstancerInterface& interface) const
  82. {
  83. RMLUI_ASSERT(num_tiles_and_textures == tile_property_ids.size());
  84. String previous_texture_name;
  85. Texture previous_texture;
  86. for(size_t i = 0; i < num_tiles_and_textures; i++)
  87. {
  88. const TilePropertyIds& ids = tile_property_ids[i];
  89. const Property* src_property = properties.GetProperty(ids.src);
  90. const String texture_name = src_property->Get< String >();
  91. // Skip the tile if it has no source name.
  92. // Declaring the name 'auto' is the same as an empty string. This gives an easy way to skip certain
  93. // tiles in a shorthand since we can't always declare an empty string.
  94. static const String auto_str = "auto";
  95. if (texture_name.empty() || texture_name == auto_str)
  96. continue;
  97. // We are required to set default values before instancing the tile, thus, all properties should always be dereferencable.
  98. // If the debugger captures a zero-dereference, check that all properties for every tile is set and default values are set just before instancing.
  99. DecoratorTiled::Tile& tile = tiles[i];
  100. Texture& texture = textures[i];
  101. // A tile is always either a sprite or a texture with position and dimensions specified.
  102. if (const Sprite * sprite = interface.GetSprite(texture_name))
  103. {
  104. tile.position.x = sprite->rectangle.x;
  105. tile.position.y = sprite->rectangle.y;
  106. tile.size.x = sprite->rectangle.width;
  107. tile.size.y = sprite->rectangle.height;
  108. tile.position_absolute[0] = tile.position_absolute[1] = true;
  109. tile.size_absolute[0] = tile.size_absolute[1] = true;
  110. texture = sprite->sprite_sheet->texture;
  111. }
  112. else
  113. {
  114. LoadTexCoord(*properties.GetProperty(ids.x), tile.position.x, tile.position_absolute[0]);
  115. LoadTexCoord(*properties.GetProperty(ids.y), tile.position.y, tile.position_absolute[1]);
  116. LoadTexCoord(*properties.GetProperty(ids.width), tile.size.x, tile.size_absolute[0]);
  117. LoadTexCoord(*properties.GetProperty(ids.height), tile.size.y, tile.size_absolute[1]);
  118. // Since the common use case is to specify the same texture for all tiles, we
  119. // check the previous texture first before fetching from the global database.
  120. if (texture_name == previous_texture_name)
  121. {
  122. texture = previous_texture;
  123. }
  124. else if (src_property->source && texture.Load(texture_name, src_property->source->path))
  125. {
  126. previous_texture_name = texture_name;
  127. previous_texture = texture;
  128. }
  129. else
  130. {
  131. auto& source = src_property->source;
  132. 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);
  133. return false;
  134. }
  135. }
  136. if(ids.repeat != PropertyId::Invalid)
  137. {
  138. const Property& repeat_property = *properties.GetProperty(ids.repeat);
  139. tile.repeat_mode = (DecoratorTiled::TileRepeatMode)repeat_property.value.Get< int >();
  140. }
  141. if (ids.orientation != PropertyId::Invalid)
  142. {
  143. const Property& repeat_property = *properties.GetProperty(ids.orientation);
  144. tile.orientation = (DecoratorTiled::TileOrientation)repeat_property.value.Get< int >();
  145. }
  146. }
  147. return true;
  148. }
  149. }
  150. }