Decorator.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 "../../Include/Rocket/Core/Decorator.h"
  29. #include "TextureDatabase.h"
  30. #include "TextureResource.h"
  31. #include "../../Include/Rocket/Core/DecoratorInstancer.h"
  32. #include "../../Include/Rocket/Core/PropertyDefinition.h"
  33. namespace Rocket {
  34. namespace Core {
  35. Decorator::Decorator()
  36. {
  37. instancer = NULL;
  38. z_index = 0;
  39. specificity = -1;
  40. }
  41. Decorator::~Decorator()
  42. {
  43. }
  44. // Sets the z-index of the decorator.
  45. void Decorator::SetZIndex(float _z_index)
  46. {
  47. z_index = _z_index;
  48. }
  49. // Returns the decorator's z-index.
  50. float Decorator::GetZIndex() const
  51. {
  52. return z_index;
  53. }
  54. // Sets the specificity of the decorator.
  55. void Decorator::SetSpecificity(int _specificity)
  56. {
  57. specificity = _specificity;
  58. }
  59. // Returns the specificity of the decorator.
  60. int Decorator::GetSpecificity() const
  61. {
  62. return specificity;
  63. }
  64. // Releases the decorator through its instancer.
  65. void Decorator::OnReferenceDeactivate()
  66. {
  67. if (instancer != NULL)
  68. instancer->ReleaseDecorator(this);
  69. }
  70. // Attempts to load a texture into the list of textures in use by the decorator.
  71. int Decorator::LoadTexture(const String& texture_name, const String& rcss_path)
  72. {
  73. for (size_t i = 0; i < textures.size(); i++)
  74. {
  75. if (texture_name == textures[i].GetSource())
  76. return (int) i;
  77. }
  78. Texture texture;
  79. if (!texture.Load(texture_name, rcss_path))
  80. return -1;
  81. textures.push_back(texture);
  82. return (int) textures.size() - 1;
  83. }
  84. // Returns one of the decorator's previously loaded textures.
  85. const Texture* Decorator::GetTexture(int index) const
  86. {
  87. if (index < 0 || index >= (int) textures.size())
  88. return NULL;
  89. return &(textures[index]);
  90. }
  91. // Returns the floating-point value of a numerical property from a dictionary of properties.
  92. float Decorator::ResolveProperty(const PropertyDictionary& properties, const String& name, float base_value) const
  93. {
  94. const Property* property = properties.GetProperty(name);
  95. if (property == NULL)
  96. {
  97. ROCKET_ERROR;
  98. return 0;
  99. }
  100. // Need to include em!
  101. if (property->unit & Property::RELATIVE_UNIT)
  102. return base_value * property->value.Get< float >() * 0.01f;
  103. if (property->unit & Property::NUMBER || property->unit & Property::PX)
  104. return property->value.Get< float >();
  105. // Values based on pixels-per-inch.
  106. if (property->unit & Property::PPI_UNIT)
  107. {
  108. float inch = property->value.Get< float >() * GetRenderInterface()->GetPixelsPerInch();
  109. if (property->unit & Property::INCH) // inch
  110. return inch;
  111. if (property->unit & Property::CM) // centimeter
  112. return inch / 2.54f;
  113. if (property->unit & Property::MM) // millimeter
  114. return inch / 25.4f;
  115. if (property->unit & Property::PT) // point
  116. return inch / 72.0f;
  117. if (property->unit & Property::PC) // pica
  118. return inch / 6.0f;
  119. }
  120. ROCKET_ERROR;
  121. return 0;
  122. }
  123. }
  124. }