DecoratorTiledInstancer.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "precompiled.h"
  28. #include "DecoratorTiledInstancer.h"
  29. #include "../../Include/Rocket/Core/PropertyDefinition.h"
  30. namespace Rocket {
  31. namespace Core {
  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_repeat_modes)
  38. {
  39. TilePropertyIds ids = {};
  40. ids.src = RegisterProperty(CreateString(32, "%s-src", name.c_str()), "").AddParser("string").GetId();
  41. ids.x = RegisterProperty(CreateString(32, "%s-x", name.c_str()), "0px").AddParser("number_length_percent").GetId();
  42. ids.y = RegisterProperty(CreateString(32, "%s-y", name.c_str()), "0px").AddParser("number_length_percent").GetId();
  43. ids.width = RegisterProperty(CreateString(32, "%s-width", name.c_str()), "0px").AddParser("number_length_percent").GetId();
  44. ids.height = RegisterProperty(CreateString(32, "%s-height", name.c_str()), "0px").AddParser("number_length_percent").GetId();
  45. RegisterShorthand(CreateString(32, "%s-pos", name.c_str()), CreateString(64, "%s-x, %s-y", name.c_str(), name.c_str()), ShorthandType::FallThrough);
  46. RegisterShorthand(CreateString(32, "%s-size", name.c_str()), CreateString(64, "%s-width, %s-height", name.c_str(), name.c_str()), ShorthandType::FallThrough);
  47. if (register_repeat_modes)
  48. {
  49. ids.repeat = RegisterProperty(CreateString(32, "%s-repeat", name.c_str()), "stretch")
  50. .AddParser("keyword", "stretch, clamp-stretch, clamp-truncate, repeat-stretch, repeat-truncate")
  51. .GetId();
  52. RegisterShorthand(name, CreateString(256, "%s-src, %s-repeat, %s-x, %s-y, %s-width, %s-height", name.c_str(), name.c_str(), name.c_str(), name.c_str(), name.c_str(), name.c_str()), ShorthandType::FallThrough);
  53. }
  54. else
  55. {
  56. ids.repeat = PropertyId::Invalid;
  57. RegisterShorthand(name, CreateString(256, "%s-src, %s-x, %s-y, %s-width, %s-height", name.c_str(), name.c_str(), name.c_str(), name.c_str(), name.c_str()), ShorthandType::FallThrough);
  58. }
  59. tile_property_ids.push_back(ids);
  60. }
  61. // Loads a single texture coordinate value from the properties.
  62. static void LoadTexCoord(const Property& property, float& tex_coord, bool& tex_coord_absolute)
  63. {
  64. tex_coord = property.value.Get< float >();
  65. if (property.unit == Property::PX)
  66. {
  67. tex_coord_absolute = true;
  68. }
  69. else
  70. {
  71. tex_coord_absolute = false;
  72. if (property.unit == Property::PERCENT)
  73. tex_coord *= 0.01f;
  74. }
  75. }
  76. // Retrieves all the properties for a tile from the property dictionary.
  77. bool DecoratorTiledInstancer::GetTileProperties(DecoratorTiled::Tile* tiles, Texture* textures, size_t num_tiles_and_textures, const PropertyDictionary& properties, const DecoratorInstancerInterface& interface) const
  78. {
  79. ROCKET_ASSERT(num_tiles_and_textures == tile_property_ids.size());
  80. String previous_texture_name;
  81. Texture previous_texture;
  82. for(size_t i = 0; i < num_tiles_and_textures; i++)
  83. {
  84. const TilePropertyIds& ids = tile_property_ids[i];
  85. const Property* src_property = properties.GetProperty(ids.src);
  86. const String texture_name = src_property->Get< String >();
  87. if (texture_name == "window-tl")
  88. {
  89. int i = 0;
  90. }
  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. // A tile is always either a sprite or a texture with position and dimensions specified.
  98. bool src_is_sprite = false;
  99. // We are required to set default values before instancing the tile, thus, all properties should always be dereferencable.
  100. // If the debugger captures a zero-dereference, check that all properties for every tile is set and default values are set just before instancing.
  101. const Property& width_property = *properties.GetProperty(ids.width);
  102. float width = width_property.Get<float>();
  103. if (width == 0.0f)
  104. {
  105. // A sprite always has its own dimensions, thus, we let zero width/height define that it is a sprite.
  106. src_is_sprite = true;
  107. }
  108. DecoratorTiled::Tile& tile = tiles[i];
  109. Texture& texture = textures[i];
  110. if (src_is_sprite)
  111. {
  112. if (const Sprite * sprite = interface.GetSprite(texture_name))
  113. {
  114. tile.position.x = sprite->rectangle.x;
  115. tile.position.y = sprite->rectangle.y;
  116. tile.size.x = sprite->rectangle.width;
  117. tile.size.y = sprite->rectangle.height;
  118. tile.position_absolute[0] = tile.position_absolute[1] = true;
  119. tile.size_absolute[0] = tile.size_absolute[1] = true;
  120. texture = sprite->sprite_sheet->texture;
  121. }
  122. else
  123. {
  124. Log::Message(Log::LT_WARNING, "The sprite '%s' given in decorator could not be found, declared at %s:%d", texture_name.c_str(), src_property->source.c_str(), src_property->source_line_number);
  125. return false;
  126. }
  127. }
  128. else
  129. {
  130. LoadTexCoord(*properties.GetProperty(ids.x), tile.position.x, tile.position_absolute[0]);
  131. LoadTexCoord(*properties.GetProperty(ids.y), tile.position.y, tile.position_absolute[1]);
  132. LoadTexCoord(width_property, tile.size.x, tile.size_absolute[0]);
  133. LoadTexCoord(*properties.GetProperty(ids.height), tile.size.y, tile.size_absolute[1]);
  134. // Since the common use case is to specify the same texture for all tiles, we
  135. // check the previous texture first before fetching from the global database.
  136. if (texture_name == previous_texture_name)
  137. {
  138. texture = previous_texture;
  139. }
  140. else if (texture.Load(texture_name, src_property->source))
  141. {
  142. previous_texture_name = texture_name;
  143. previous_texture = texture;
  144. }
  145. else
  146. {
  147. Log::Message(Log::LT_WARNING, "Could not load texture '%s' given in decorator at %s:%d", texture_name.c_str(), src_property->source.c_str(), src_property->source_line_number);
  148. return false;
  149. }
  150. }
  151. if(ids.repeat != PropertyId::Invalid)
  152. {
  153. const Property& repeat_property = *properties.GetProperty(ids.repeat);
  154. tile.repeat_mode = (DecoratorTiled::TileRepeatMode)repeat_property.value.Get< int >();
  155. }
  156. }
  157. return true;
  158. }
  159. }
  160. }