Decorator.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. #ifndef ROCKETCOREDECORATOR_H
  28. #define ROCKETCOREDECORATOR_H
  29. #include "ReferenceCountable.h"
  30. #include <vector>
  31. #include "Header.h"
  32. #include "Texture.h"
  33. #include "Types.h"
  34. namespace Rocket {
  35. namespace Core {
  36. class DecoratorInstancer;
  37. class Element;
  38. class PropertyDictionary;
  39. class Property;
  40. struct Texture;
  41. class TextureResource;
  42. /**
  43. The abstract base class for any visual object that can be attached to any element.
  44. @author Peter Curry
  45. */
  46. class ROCKETCORE_API Decorator : public ReferenceCountable
  47. {
  48. public:
  49. Decorator();
  50. virtual ~Decorator();
  51. /// Called on a decorator to generate any required per-element data for a newly decorated element.
  52. /// @param[in] element The newly decorated element.
  53. /// @return A handle to a decorator-defined data handle, or NULL if none is needed for the element.
  54. virtual DecoratorDataHandle GenerateElementData(Element* element) = 0;
  55. /// Called to release element data generated by this decorator.
  56. /// @param[in] element_data The element data handle to release.
  57. virtual void ReleaseElementData(DecoratorDataHandle element_data) = 0;
  58. /// Sets the z-index of the decorator. A decorator with a higher z-index will be rendered after a decorator
  59. /// with a lower z-index. By default, all decorators have a z-index of 0.
  60. /// @param[in] z-index The new z-index of the decorator.
  61. void SetZIndex(float z_index);
  62. /// Returns the decorator's z-index.
  63. /// @return The z-index of the decorator.
  64. float GetZIndex() const;
  65. /// Sets the specificity of the decorator.
  66. /// @param[in] specificity The specificity of the decorator.
  67. void SetSpecificity(int specificity);
  68. /// Returns the specificity of the decorator. This is used when multiple pseudo-classes are active on an
  69. /// element, each with similarly-named decorators.
  70. /// @return The specificity of the decorator.
  71. int GetSpecificity() const;
  72. /// Called to render the decorator on an element.
  73. /// @param[in] element The element to render the decorator on.
  74. /// @param[in] element_data The handle to the data generated by the decorator for the element.
  75. virtual void RenderElement(Element* element, DecoratorDataHandle element_data) = 0;
  76. /// Value specifying an invalid or non-existent Decorator data handle.
  77. static const DecoratorDataHandle INVALID_DECORATORDATAHANDLE = 0;
  78. protected:
  79. /// Releases the decorator through its instancer.
  80. virtual void OnReferenceDeactivate();
  81. /// Attempts to load a texture into the list of textures in use by the decorator.
  82. /// @param[in] texture_name The name of the texture to load.
  83. /// @param[in] rcss_path The RCSS file the decorator definition was loaded from; this is used to resolve relative paths.
  84. /// @return The index of the texture if the load was successful, or -1 if the load failed.
  85. int LoadTexture(const String& texture_name, const String& rcss_path);
  86. /// Returns one of the decorator's previously loaded textures.
  87. /// @param[in] index The index of the desired texture.
  88. /// @return The texture at the appropriate index, or NULL if the index was invalid.
  89. const Texture* GetTexture(int index = 0) const;
  90. /// Returns the floating-point value of a numerical property from a dictionary of properties, resolving it
  91. /// against a base value if it is a relative property.
  92. /// @param[in] properties The user-supplied dictionary of properties.
  93. /// @param[in] name The name of the desired property. This must be a numerical property.
  94. /// @return The fully-resolved value of the property, or 0 if an error occured.
  95. float ResolveProperty(const PropertyDictionary& properties, const String& name, float base_value) const;
  96. private:
  97. DecoratorInstancer* instancer;
  98. // The z-index of this decorator, used to resolve render order when multiple decorators are rendered
  99. // simultaneously on the same element.
  100. float z_index;
  101. // The maximum specificity of the properties used to define the decorator.
  102. int specificity;
  103. // Stores a list of textures in use by this decorator.
  104. std::vector< Texture > textures;
  105. friend class Factory;
  106. };
  107. }
  108. }
  109. #endif