DecoratorTiledInstancer.cpp 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "precompiled.h"
  29. #include "DecoratorTiledInstancer.h"
  30. #include "../../Include/RmlUi/Core/PropertyDefinition.h"
  31. namespace Rml {
  32. namespace Core {
  33. DecoratorTiledInstancer::~DecoratorTiledInstancer()
  34. {
  35. }
  36. // Adds the property declarations for a tile.
  37. void DecoratorTiledInstancer::RegisterTileProperty(const String& name, bool register_repeat_modes)
  38. {
  39. RegisterProperty(String(32, "%s-src", name.CString()), "").AddParser("string");
  40. RegisterProperty(String(32, "%s-s-begin", name.CString()), "0").AddParser("length");
  41. RegisterProperty(String(32, "%s-s-end", name.CString()), "1").AddParser("length");
  42. RegisterProperty(String(32, "%s-t-begin", name.CString()), "0").AddParser("length");
  43. RegisterProperty(String(32, "%s-t-end", name.CString()), "1").AddParser("length");
  44. RegisterShorthand(String(32, "%s-s", name.CString()), String(64, "%s-s-begin, %s-s-end", name.CString(), name.CString()));
  45. RegisterShorthand(String(32, "%s-t", name.CString()), String(64, "%s-t-begin, %s-t-end", name.CString(), name.CString()));
  46. if (register_repeat_modes)
  47. {
  48. RegisterProperty(String(32, "%s-repeat", name.CString()), "stretch")
  49. .AddParser("keyword", "stretch, clamp-stretch, clamp-truncate, repeat-stretch, repeat-truncate");
  50. 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()));
  51. }
  52. else
  53. 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()));
  54. }
  55. // Retrieves all the properties for a tile from the property dictionary.
  56. void DecoratorTiledInstancer::GetTileProperties(DecoratorTiled::Tile& tile, String& texture_name, String& rcss_path, const PropertyDictionary& properties, const String& name)
  57. {
  58. LoadTexCoord(properties, String(32, "%s-s-begin", name.CString()), tile.texcoords[0].x, tile.texcoords_absolute[0][0]);
  59. LoadTexCoord(properties, String(32, "%s-t-begin", name.CString()), tile.texcoords[0].y, tile.texcoords_absolute[0][1]);
  60. LoadTexCoord(properties, String(32, "%s-s-end", name.CString()), tile.texcoords[1].x, tile.texcoords_absolute[1][0]);
  61. LoadTexCoord(properties, String(32, "%s-t-end", name.CString()), tile.texcoords[1].y, tile.texcoords_absolute[1][1]);
  62. const Property* repeat_property = properties.GetProperty(String(32, "%s-repeat", name.CString()));
  63. if (repeat_property != NULL)
  64. tile.repeat_mode = (DecoratorTiled::TileRepeatMode) repeat_property->value.Get< int >();
  65. const Property* texture_property = properties.GetProperty(String(32, "%s-src", name.CString()));
  66. texture_name = texture_property->Get< String >();
  67. rcss_path = texture_property->source;
  68. }
  69. // Loads a single texture coordinate value from the properties.
  70. void DecoratorTiledInstancer::LoadTexCoord(const PropertyDictionary& properties, const String& name, float& tex_coord, bool& tex_coord_absolute)
  71. {
  72. const Property* property = properties.GetProperty(name);
  73. if (property == NULL)
  74. return;
  75. tex_coord = property->value.Get< float >();
  76. if (property->unit == Property::PX)
  77. tex_coord_absolute = true;
  78. else
  79. {
  80. tex_coord_absolute = false;
  81. if (property->unit == Property::PERCENT)
  82. tex_coord *= 0.01f;
  83. }
  84. }
  85. }
  86. }