DecoratorTiledInstancer.cpp 4.5 KB

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