DecoratorTiledInstancer.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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)
  60. {
  61. ROCKET_ASSERT(tile_index < tile_property_ids.size());
  62. const TilePropertyIds& ids = tile_property_ids[tile_index];
  63. LoadTexCoord(properties, ids.s_begin, tile.texcoords[0].x, tile.texcoords_absolute[0][0]);
  64. LoadTexCoord(properties, ids.t_begin, tile.texcoords[0].y, tile.texcoords_absolute[0][1]);
  65. LoadTexCoord(properties, ids.s_end, tile.texcoords[1].x, tile.texcoords_absolute[1][0]);
  66. LoadTexCoord(properties, ids.t_end, tile.texcoords[1].y, tile.texcoords_absolute[1][1]);
  67. const Property* repeat_property = properties.GetProperty(ids.repeat);
  68. if (repeat_property != NULL)
  69. tile.repeat_mode = (DecoratorTiled::TileRepeatMode) repeat_property->value.Get< int >();
  70. const Property* texture_property = properties.GetProperty(ids.src);
  71. texture_name = texture_property->Get< String >();
  72. rcss_path = texture_property->source;
  73. }
  74. // Loads a single texture coordinate value from the properties.
  75. void DecoratorTiledInstancer::LoadTexCoord(const PropertyDictionary& properties, PropertyId id, float& tex_coord, bool& tex_coord_absolute)
  76. {
  77. const Property* property = properties.GetProperty(id);
  78. if (property == NULL)
  79. return;
  80. tex_coord = property->value.Get< float >();
  81. if (property->unit == Property::PX)
  82. tex_coord_absolute = true;
  83. else
  84. {
  85. tex_coord_absolute = false;
  86. if (property->unit == Property::PERCENT)
  87. tex_coord *= 0.01f;
  88. }
  89. }
  90. }
  91. }