DecoratorTiledHorizontal.cpp 5.7 KB

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