DecoratorTiledInstancer.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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()
  33. {
  34. }
  35. // Adds the property declarations for a tile.
  36. void DecoratorTiledInstancer::RegisterTileProperty(const String& name, bool register_repeat_modes)
  37. {
  38. TilePropertyIds tile = {};
  39. tile.src = RegisterProperty(CreateString(32, "%s-src", name.c_str()), "").AddParser("string").GetId();
  40. tile.s_begin = RegisterProperty(CreateString(32, "%s-s-begin", name.c_str()), "0").AddParser("length").GetId();
  41. tile.s_end = RegisterProperty(CreateString(32, "%s-s-end", name.c_str()), "1").AddParser("length").GetId();
  42. tile.t_begin = RegisterProperty(CreateString(32, "%s-t-begin", name.c_str()), "0").AddParser("length").GetId();
  43. tile.t_end = RegisterProperty(CreateString(32, "%s-t-end", name.c_str()), "1").AddParser("length").GetId();
  44. RegisterShorthand(CreateString(32, "%s-s", name.c_str()), CreateString(64, "%s-s-begin, %s-s-end", name.c_str(), name.c_str()), ShorthandType::FallThrough);
  45. RegisterShorthand(CreateString(32, "%s-t", name.c_str()), CreateString(64, "%s-t-begin, %s-t-end", name.c_str(), name.c_str()), ShorthandType::FallThrough);
  46. if (register_repeat_modes)
  47. {
  48. tile.repeat = RegisterProperty(CreateString(32, "%s-repeat", name.c_str()), "stretch")
  49. .AddParser("keyword", "stretch, clamp-stretch, clamp-truncate, repeat-stretch, repeat-truncate")
  50. .GetId();
  51. RegisterShorthand(name, CreateString(256, "%s-src, %s-repeat, %s-s-begin, %s-t-begin, %s-s-end, %s-t-end", name.c_str(), name.c_str(), name.c_str(), name.c_str(), name.c_str(), name.c_str()), ShorthandType::FallThrough);
  52. }
  53. else
  54. RegisterShorthand(name, CreateString(256, "%s-src, %s-s-begin, %s-t-begin, %s-s-end, %s-t-end", name.c_str(), name.c_str(), name.c_str(), name.c_str(), name.c_str()), ShorthandType::FallThrough);
  55. // @performance: Reserve number of tiles on construction
  56. tile_property_ids.push_back(tile);
  57. }
  58. // Retrieves all the properties for a tile from the property dictionary.
  59. void DecoratorTiledInstancer::GetTileProperties(size_t tile_index, DecoratorTiled::Tile& tile, String& texture_name, String& rcss_path, const PropertyDictionary& properties, const StyleSheet& style_sheet)
  60. {
  61. ROCKET_ASSERT(tile_index < tile_property_ids.size());
  62. const TilePropertyIds& ids = tile_property_ids[tile_index];
  63. const Property* texture_property = properties.GetProperty(ids.src);
  64. texture_name = texture_property->Get< String >();
  65. rcss_path = texture_property->source;
  66. if (texture_name == "window-c")
  67. {
  68. int i = 0;
  69. }
  70. // Declaring the name 'auto' is the same as an empty string. This gives an easy way to skip certain
  71. // tiles in a shorthand since we can't always declare an empty string.
  72. static const String none_texture_name = "auto";
  73. if (texture_name == none_texture_name)
  74. texture_name.clear();
  75. // @performance / @todo: We want some way to determine sprite or image instead of always do the lookup up as a sprite name.
  76. // @performance: We already have the texture loaded in the spritesheet, very unnecessary to return as name and then convert to texture again.
  77. if (const Sprite * sprite = style_sheet.GetSprite(texture_name))
  78. {
  79. texture_name = sprite->sprite_sheet->image_source;
  80. rcss_path = sprite->sprite_sheet->definition_source;
  81. tile.texcoords[0].x = sprite->rectangle.x;
  82. tile.texcoords[0].y = sprite->rectangle.y;
  83. tile.texcoords[1].x = sprite->rectangle.x + sprite->rectangle.width;
  84. tile.texcoords[1].y = sprite->rectangle.y + sprite->rectangle.height;
  85. tile.texcoords_absolute[0][0] = true;
  86. tile.texcoords_absolute[0][1] = true;
  87. tile.texcoords_absolute[1][0] = true;
  88. tile.texcoords_absolute[1][1] = true;
  89. }
  90. else
  91. {
  92. LoadTexCoord(properties, ids.s_begin, tile.texcoords[0].x, tile.texcoords_absolute[0][0]);
  93. LoadTexCoord(properties, ids.t_begin, tile.texcoords[0].y, tile.texcoords_absolute[0][1]);
  94. LoadTexCoord(properties, ids.s_end, tile.texcoords[1].x, tile.texcoords_absolute[1][0]);
  95. LoadTexCoord(properties, ids.t_end, tile.texcoords[1].y, tile.texcoords_absolute[1][1]);
  96. }
  97. if(ids.repeat != PropertyId::Invalid)
  98. {
  99. const Property* repeat_property = properties.GetProperty(ids.repeat);
  100. ROCKET_ASSERT(repeat_property);
  101. tile.repeat_mode = (DecoratorTiled::TileRepeatMode) repeat_property->value.Get< int >();
  102. }
  103. }
  104. // Loads a single texture coordinate value from the properties.
  105. void DecoratorTiledInstancer::LoadTexCoord(const PropertyDictionary& properties, PropertyId id, float& tex_coord, bool& tex_coord_absolute)
  106. {
  107. const Property* property = properties.GetProperty(id);
  108. // May fail if we forgot to set default values before instancing the tile
  109. ROCKET_ASSERT(property);
  110. tex_coord = property->value.Get< float >();
  111. if (property->unit == Property::PX)
  112. {
  113. tex_coord_absolute = true;
  114. }
  115. else
  116. {
  117. tex_coord_absolute = false;
  118. if (property->unit == Property::PERCENT)
  119. tex_coord *= 0.01f;
  120. }
  121. }
  122. }
  123. }