DecoratorTiledInstancer.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. RegisterProperty(CreateOrGetPropertyId( CreateString(32, "%s-src", name.c_str())), "").AddParser("string");
  39. RegisterProperty(CreateOrGetPropertyId( CreateString(32, "%s-s-begin", name.c_str())), "0").AddParser("length");
  40. RegisterProperty(CreateOrGetPropertyId( CreateString(32, "%s-s-end", name.c_str())), "1").AddParser("length");
  41. RegisterProperty(CreateOrGetPropertyId( CreateString(32, "%s-t-begin", name.c_str())), "0").AddParser("length");
  42. RegisterProperty(CreateOrGetPropertyId( CreateString(32, "%s-t-end", name.c_str())), "1").AddParser("length");
  43. RegisterShorthand(CreateOrGetPropertyId(CreateString(32, "%s-s", name.c_str())),
  44. { CreateOrGetPropertyId(CreateString(32, "%s-s-begin", name.c_str())), CreateOrGetPropertyId(CreateString(32, "%s-s-end", name.c_str())) }
  45. );
  46. RegisterShorthand(CreateOrGetPropertyId(CreateString(32, "%s-t", name.c_str())),
  47. { CreateOrGetPropertyId(CreateString(32, "%s-t-begin", name.c_str())), CreateOrGetPropertyId(CreateString(32, "%s-t-end", name.c_str())) }
  48. );
  49. if (register_repeat_modes)
  50. {
  51. RegisterProperty(CreateOrGetPropertyId( CreateString(32, "%s-repeat", name.c_str())), "stretch")
  52. .AddParser("keyword", "stretch, clamp-stretch, clamp-truncate, repeat-stretch, repeat-truncate");
  53. RegisterShorthand(CreateOrGetPropertyId(name),
  54. {
  55. CreateOrGetPropertyId(CreateString(32, "%s-src", name.c_str())),
  56. CreateOrGetPropertyId(CreateString(32, "%s-repeat", name.c_str())),
  57. CreateOrGetPropertyId(CreateString(32, "%s-s-begin", name.c_str())),
  58. CreateOrGetPropertyId(CreateString(32, "%s-t-begin", name.c_str())),
  59. CreateOrGetPropertyId(CreateString(32, "%s-s-end", name.c_str())),
  60. CreateOrGetPropertyId(CreateString(32, "%s-t-end", name.c_str())),
  61. }
  62. );
  63. }
  64. else
  65. RegisterShorthand(CreateOrGetPropertyId(name),
  66. {
  67. CreateOrGetPropertyId(CreateString(32, "%s-src", name.c_str())),
  68. CreateOrGetPropertyId(CreateString(32, "%s-s-begin", name.c_str())),
  69. CreateOrGetPropertyId(CreateString(32, "%s-t-begin", name.c_str())),
  70. CreateOrGetPropertyId(CreateString(32, "%s-s-end", name.c_str())),
  71. CreateOrGetPropertyId(CreateString(32, "%s-t-end", name.c_str())),
  72. }
  73. );
  74. }
  75. // Retrieves all the properties for a tile from the property dictionary.
  76. void DecoratorTiledInstancer::GetTileProperties(DecoratorTiled::Tile& tile, String& texture_name, String& rcss_path, const PropertyDictionary& properties, const String& name)
  77. {
  78. LoadTexCoord(properties, GetPropertyId( CreateString(32, "%s-s-begin", name.c_str())), tile.texcoords[0].x, tile.texcoords_absolute[0][0]);
  79. LoadTexCoord(properties, GetPropertyId( CreateString(32, "%s-t-begin", name.c_str())), tile.texcoords[0].y, tile.texcoords_absolute[0][1]);
  80. LoadTexCoord(properties, GetPropertyId( CreateString(32, "%s-s-end", name.c_str())), tile.texcoords[1].x, tile.texcoords_absolute[1][0]);
  81. LoadTexCoord(properties, GetPropertyId( CreateString(32, "%s-t-end", name.c_str())), tile.texcoords[1].y, tile.texcoords_absolute[1][1]);
  82. const Property* repeat_property = GetIf(properties, CreateOrGetPropertyId( CreateString(32, "%s-repeat", name.c_str())));
  83. if (repeat_property != NULL)
  84. tile.repeat_mode = (DecoratorTiled::TileRepeatMode) repeat_property->value.Get< int >();
  85. const Property* texture_property = GetIf(properties, CreateOrGetPropertyId( CreateString(32, "%s-src", name.c_str())));
  86. texture_name = texture_property->Get< String >();
  87. rcss_path = texture_property->source;
  88. }
  89. // Loads a single texture coordinate value from the properties.
  90. void DecoratorTiledInstancer::LoadTexCoord(const PropertyDictionary& properties, PropertyId id, float& tex_coord, bool& tex_coord_absolute)
  91. {
  92. const Property* property = GetIf(properties, id);
  93. if (property == NULL)
  94. return;
  95. tex_coord = property->value.Get< float >();
  96. if (property->unit == Property::PX)
  97. tex_coord_absolute = true;
  98. else
  99. {
  100. tex_coord_absolute = false;
  101. if (property->unit == Property::PERCENT)
  102. tex_coord *= 0.01f;
  103. }
  104. }
  105. }
  106. }