DecoratorTiledVertical.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 "DecoratorTiledVertical.h"
  29. #include "../../Include/Rocket/Core/Element.h"
  30. #include "../../Include/Rocket/Core/Geometry.h"
  31. #include "../../Include/Rocket/Core/GeometryUtilities.h"
  32. #include "../../Include/Rocket/Core/Texture.h"
  33. namespace Rocket {
  34. namespace Core {
  35. struct DecoratorTiledVerticalData
  36. {
  37. DecoratorTiledVerticalData(Element* host_element)
  38. {
  39. for (int i = 0; i < 3; ++i)
  40. geometry[i] = new Geometry(host_element);
  41. }
  42. ~DecoratorTiledVerticalData()
  43. {
  44. for (int i = 0; i < 3; ++i)
  45. delete geometry[i];
  46. }
  47. Geometry* geometry[3];
  48. };
  49. DecoratorTiledVertical::DecoratorTiledVertical()
  50. {
  51. }
  52. DecoratorTiledVertical::~DecoratorTiledVertical()
  53. {
  54. }
  55. // Initialises the tiles for the decorator.
  56. bool DecoratorTiledVertical::Initialise(const Tile* _tiles, const String* _texture_names, const String* _rcss_paths)
  57. {
  58. // Load the textures.
  59. for (int i = 0; i < 3; i++)
  60. {
  61. if (!_texture_names[i].Empty())
  62. {
  63. tiles[i] = _tiles[i];
  64. tiles[i].texture_index = LoadTexture(_texture_names[i], _rcss_paths[i]);
  65. if (tiles[i].texture_index < 0)
  66. return false;
  67. }
  68. else
  69. tiles[i].texture_index = -1;
  70. }
  71. // If only one side of the decorator has been configured, then mirror the texture for the other side.
  72. if (tiles[TOP].texture_index == -1 && tiles[BOTTOM].texture_index > -1)
  73. {
  74. tiles[TOP] = tiles[BOTTOM];
  75. tiles[TOP].orientation = FLIP_HORIZONTAL;
  76. }
  77. else if (tiles[BOTTOM].texture_index == -1 && tiles[TOP].texture_index > -1)
  78. {
  79. tiles[BOTTOM] = tiles[TOP];
  80. tiles[BOTTOM].orientation = FLIP_HORIZONTAL;
  81. }
  82. else if (tiles[TOP].texture_index == -1 && tiles[BOTTOM].texture_index == -1)
  83. return false;
  84. if (tiles[CENTRE].texture_index == -1)
  85. return false;
  86. return true;
  87. }
  88. // Called on a decorator to generate any required per-element data for a newly decorated element.
  89. DecoratorDataHandle DecoratorTiledVertical::GenerateElementData(Element* element)
  90. {
  91. // Initialise the tile for this element.
  92. for (int i = 0; i < 3; i++)
  93. tiles[i].CalculateDimensions(element, *GetTexture(tiles[i].texture_index));
  94. DecoratorTiledVerticalData* data = new DecoratorTiledVerticalData(element);
  95. Vector2f padded_size = element->GetBox().GetSize(Box::PADDING);
  96. Vector2f top_dimensions = tiles[TOP].GetDimensions(element);
  97. Vector2f bottom_dimensions = tiles[BOTTOM].GetDimensions(element);
  98. Vector2f centre_dimensions = tiles[CENTRE].GetDimensions(element);
  99. // Scale the tile sizes by the width scale.
  100. ScaleTileDimensions(top_dimensions, padded_size.x, 0);
  101. ScaleTileDimensions(bottom_dimensions, padded_size.x, 0);
  102. ScaleTileDimensions(centre_dimensions, padded_size.x, 0);
  103. // Shrink the y-sizes on the left and right tiles if necessary.
  104. if (padded_size.y < top_dimensions.y + bottom_dimensions.y)
  105. {
  106. float minimum_height = top_dimensions.y + bottom_dimensions.y;
  107. top_dimensions.y = padded_size.y * (top_dimensions.y / minimum_height);
  108. bottom_dimensions.y = padded_size.y * (bottom_dimensions.y / minimum_height);
  109. }
  110. // Generate the geometry for the left tile.
  111. tiles[TOP].GenerateGeometry(data->geometry[tiles[TOP].texture_index]->GetVertices(), data->geometry[tiles[TOP].texture_index]->GetIndices(), element, Vector2f(0, 0), top_dimensions, top_dimensions);
  112. // Generate the geometry for the centre tiles.
  113. tiles[CENTRE].GenerateGeometry(data->geometry[tiles[CENTRE].texture_index]->GetVertices(), data->geometry[tiles[CENTRE].texture_index]->GetIndices(), element, Vector2f(0, top_dimensions.y), Vector2f(centre_dimensions.x, padded_size.y - (top_dimensions.y + bottom_dimensions.y)), centre_dimensions);
  114. // Generate the geometry for the right tile.
  115. tiles[BOTTOM].GenerateGeometry(data->geometry[tiles[BOTTOM].texture_index]->GetVertices(), data->geometry[tiles[BOTTOM].texture_index]->GetIndices(), element, Vector2f(0, padded_size.y - bottom_dimensions.y), bottom_dimensions, bottom_dimensions);
  116. // Set the textures on the geometry.
  117. const Texture* texture = NULL;
  118. int texture_index = 0;
  119. while ((texture = GetTexture(texture_index)) != NULL)
  120. data->geometry[texture_index++]->SetTexture(texture);
  121. return reinterpret_cast<DecoratorDataHandle>(data);
  122. }
  123. // Called to release element data generated by this decorator.
  124. void DecoratorTiledVertical::ReleaseElementData(DecoratorDataHandle element_data)
  125. {
  126. delete reinterpret_cast< DecoratorTiledVerticalData* >(element_data);
  127. }
  128. // Called to render the decorator on an element.
  129. void DecoratorTiledVertical::RenderElement(Element* element, DecoratorDataHandle element_data)
  130. {
  131. Vector2f translation = element->GetAbsoluteOffset(Box::PADDING);
  132. DecoratorTiledVerticalData* data = reinterpret_cast< DecoratorTiledVerticalData* >(element_data);
  133. for (int i = 0; i < 3; i++)
  134. data->geometry[i]->Render(translation);
  135. }
  136. }
  137. }