DecoratorTiledVertical.cpp 6.0 KB

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