DecoratorTiled.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. #include "DecoratorTiled.h"
  2. #include "../../Include/RmlUi/Core/Element.h"
  3. #include "../../Include/RmlUi/Core/ElementUtilities.h"
  4. #include "../../Include/RmlUi/Core/Geometry.h"
  5. #include "../../Include/RmlUi/Core/Math.h"
  6. #include "../../Include/RmlUi/Core/MeshUtilities.h"
  7. #include "../../Include/RmlUi/Core/PropertyDefinition.h"
  8. #include "../../Include/RmlUi/Core/Spritesheet.h"
  9. #include <algorithm>
  10. namespace Rml {
  11. DecoratorTiled::DecoratorTiled() {}
  12. DecoratorTiled::~DecoratorTiled() {}
  13. static const Vector2f oriented_texcoords[4][2] = {
  14. {Vector2f(0, 0), Vector2f(1, 1)}, // ORIENTATION_NONE
  15. {Vector2f(1, 0), Vector2f(0, 1)}, // FLIP_HORIZONTAL
  16. {Vector2f(0, 1), Vector2f(1, 0)}, // FLIP_VERTICAL
  17. {Vector2f(1, 1), Vector2f(0, 0)} // ROTATE_180
  18. };
  19. DecoratorTiled::Tile::Tile() : display_scale(1), position(0, 0), size(0, 0)
  20. {
  21. texture_index = -1;
  22. fit_mode = FILL;
  23. orientation = ORIENTATION_NONE;
  24. }
  25. void DecoratorTiled::Tile::CalculateDimensions(Texture texture) const
  26. {
  27. if (!tile_data_calculated)
  28. {
  29. tile_data_calculated = true;
  30. tile_data = {};
  31. const Vector2f texture_dimensions(texture.GetDimensions());
  32. if (texture_dimensions.x == 0 || texture_dimensions.y == 0)
  33. {
  34. tile_data.size = Vector2f(0, 0);
  35. tile_data.texcoords[0] = Vector2f(0, 0);
  36. tile_data.texcoords[1] = Vector2f(0, 0);
  37. }
  38. else
  39. {
  40. // Need to scale the coordinates to normalized units and 'size' to absolute size (pixels)
  41. if (size.x == 0 && size.y == 0 && position.x == 0 && position.y == 0)
  42. tile_data.size = texture_dimensions;
  43. else
  44. tile_data.size = size;
  45. const Vector2f size_relative = tile_data.size / texture_dimensions;
  46. tile_data.size = Vector2f(Math::Absolute(tile_data.size.x), Math::Absolute(tile_data.size.y));
  47. tile_data.texcoords[0] = position / texture_dimensions;
  48. tile_data.texcoords[1] = size_relative + tile_data.texcoords[0];
  49. }
  50. }
  51. }
  52. Vector2f DecoratorTiled::Tile::GetNaturalDimensions(Element* element) const
  53. {
  54. if (!tile_data_calculated)
  55. return Vector2f(0, 0);
  56. const float scale_raw_to_natural_dimensions = ElementUtilities::GetDensityIndependentPixelRatio(element) * display_scale;
  57. const Vector2f raw_dimensions = tile_data.size;
  58. return raw_dimensions * scale_raw_to_natural_dimensions;
  59. }
  60. void DecoratorTiled::Tile::GenerateGeometry(Mesh& mesh, const ComputedValues& computed, const Vector2f surface_origin,
  61. const Vector2f surface_dimensions, const Vector2f tile_dimensions) const
  62. {
  63. if (surface_dimensions.x <= 0 || surface_dimensions.y <= 0)
  64. return;
  65. const ColourbPremultiplied quad_colour = computed.image_color().ToPremultiplied(computed.opacity());
  66. if (!tile_data_calculated)
  67. return;
  68. // Generate the oriented texture coordinates for the tiles.
  69. Vector2f scaled_texcoords[2];
  70. for (int i = 0; i < 2; i++)
  71. {
  72. scaled_texcoords[i] = tile_data.texcoords[0] + oriented_texcoords[orientation][i] * (tile_data.texcoords[1] - tile_data.texcoords[0]);
  73. }
  74. Vector2f final_tile_dimensions;
  75. bool offset_and_clip_tile = false;
  76. Vector2f repeat_factor = Vector2f(1);
  77. switch (fit_mode)
  78. {
  79. case FILL:
  80. {
  81. final_tile_dimensions = surface_dimensions;
  82. }
  83. break;
  84. case CONTAIN:
  85. {
  86. Vector2f scale_factor = surface_dimensions / tile_dimensions;
  87. float min_factor = std::min(scale_factor.x, scale_factor.y);
  88. final_tile_dimensions = tile_dimensions * min_factor;
  89. offset_and_clip_tile = true;
  90. }
  91. break;
  92. case COVER:
  93. {
  94. Vector2f scale_factor = surface_dimensions / tile_dimensions;
  95. float max_factor = std::max(scale_factor.x, scale_factor.y);
  96. final_tile_dimensions = tile_dimensions * max_factor;
  97. offset_and_clip_tile = true;
  98. }
  99. break;
  100. case SCALE_NONE:
  101. {
  102. final_tile_dimensions = tile_dimensions;
  103. offset_and_clip_tile = true;
  104. }
  105. break;
  106. case SCALE_DOWN:
  107. {
  108. Vector2f scale_factor = surface_dimensions / tile_dimensions;
  109. float min_factor = std::min(scale_factor.x, scale_factor.y);
  110. if (min_factor < 1.0f)
  111. final_tile_dimensions = tile_dimensions * min_factor;
  112. else
  113. final_tile_dimensions = tile_dimensions;
  114. offset_and_clip_tile = true;
  115. }
  116. break;
  117. case REPEAT:
  118. final_tile_dimensions = surface_dimensions;
  119. repeat_factor = surface_dimensions / tile_dimensions;
  120. break;
  121. case REPEAT_X:
  122. final_tile_dimensions = Vector2f(surface_dimensions.x, tile_dimensions.y);
  123. repeat_factor.x = surface_dimensions.x / tile_dimensions.x;
  124. offset_and_clip_tile = true;
  125. break;
  126. case REPEAT_Y:
  127. final_tile_dimensions = Vector2f(tile_dimensions.x, surface_dimensions.y);
  128. repeat_factor.y = surface_dimensions.y / tile_dimensions.y;
  129. offset_and_clip_tile = true;
  130. break;
  131. }
  132. Vector2f tile_offset(0, 0);
  133. if (offset_and_clip_tile)
  134. {
  135. // Offset tile along each dimension.
  136. for (int i = 0; i < 2; i++)
  137. {
  138. switch (align[i].type)
  139. {
  140. case Style::LengthPercentage::Length: tile_offset[i] = align[i].value; break;
  141. case Style::LengthPercentage::Percentage:
  142. tile_offset[i] = (surface_dimensions[i] - final_tile_dimensions[i]) * align[i].value * 0.01f;
  143. break;
  144. }
  145. }
  146. tile_offset = tile_offset.Round();
  147. // Clip tile. See if our tile extends outside the boundary at either side, along each dimension.
  148. for (int i = 0; i < 2; i++)
  149. {
  150. // Left/right acts as top/bottom during the second iteration.
  151. float overshoot_left = std::max(-tile_offset[i], 0.0f);
  152. float overshoot_right = std::max(tile_offset[i] + final_tile_dimensions[i] - surface_dimensions[i], 0.0f);
  153. if (overshoot_left > 0.f || overshoot_right > 0.f)
  154. {
  155. float& left = scaled_texcoords[0][i];
  156. float& right = scaled_texcoords[1][i];
  157. float width = right - left;
  158. left += overshoot_left / final_tile_dimensions[i] * width;
  159. right -= overshoot_right / final_tile_dimensions[i] * width;
  160. final_tile_dimensions[i] -= overshoot_left + overshoot_right;
  161. tile_offset[i] += overshoot_left;
  162. }
  163. }
  164. }
  165. scaled_texcoords[0] *= repeat_factor;
  166. scaled_texcoords[1] *= repeat_factor;
  167. // Generate the vertices for the tiled surface.
  168. Vector2f tile_position = (surface_origin + tile_offset);
  169. Math::SnapToPixelGrid(tile_position, final_tile_dimensions);
  170. MeshUtilities::GenerateQuad(mesh, tile_position, final_tile_dimensions, quad_colour, scaled_texcoords[0], scaled_texcoords[1]);
  171. }
  172. void DecoratorTiled::ScaleTileDimensions(Vector2f& tile_dimensions, float axis_value, Axis axis_enum) const
  173. {
  174. int axis = static_cast<int>(axis_enum);
  175. if (tile_dimensions[axis] != axis_value)
  176. {
  177. tile_dimensions[1 - axis] = tile_dimensions[1 - axis] * (axis_value / tile_dimensions[axis]);
  178. tile_dimensions[axis] = axis_value;
  179. }
  180. }
  181. DecoratorTiledInstancer::DecoratorTiledInstancer(size_t num_tiles)
  182. {
  183. tile_property_ids.reserve(num_tiles);
  184. }
  185. void DecoratorTiledInstancer::RegisterTileProperty(const String& name, bool register_fit_modes)
  186. {
  187. TilePropertyIds ids = {};
  188. ids.src = RegisterProperty(CreateString("%s-src", name.c_str()), "").AddParser("string").GetId();
  189. String additional_modes;
  190. if (register_fit_modes)
  191. {
  192. String fit_name = CreateString("%s-fit", name.c_str());
  193. ids.fit = RegisterProperty(fit_name, "fill")
  194. .AddParser("keyword", "fill, contain, cover, scale-none, scale-down, repeat, repeat-x, repeat-y")
  195. .GetId();
  196. String align_x_name = CreateString("%s-align-x", name.c_str());
  197. ids.align_x = RegisterProperty(align_x_name, "center").AddParser("keyword", "left, center, right").AddParser("length_percent").GetId();
  198. String align_y_name = CreateString("%s-align-y", name.c_str());
  199. ids.align_y = RegisterProperty(align_y_name, "center").AddParser("keyword", "top, center, bottom").AddParser("length_percent").GetId();
  200. additional_modes += ", " + fit_name + ", " + align_x_name + ", " + align_y_name;
  201. }
  202. ids.orientation = RegisterProperty(CreateString("%s-orientation", name.c_str()), "none")
  203. .AddParser("keyword", "none, flip-horizontal, flip-vertical, rotate-180")
  204. .GetId();
  205. RegisterShorthand(name,
  206. CreateString(("%s-src, %s-orientation" + additional_modes).c_str(), name.c_str(), name.c_str(), name.c_str(), name.c_str(), name.c_str(),
  207. name.c_str()),
  208. ShorthandType::FallThrough);
  209. tile_property_ids.push_back(ids);
  210. }
  211. bool DecoratorTiledInstancer::GetTileProperties(DecoratorTiled::Tile* tiles, Texture* textures, size_t num_tiles_and_textures,
  212. const PropertyDictionary& properties, const DecoratorInstancerInterface& instancer_interface) const
  213. {
  214. RMLUI_ASSERT(num_tiles_and_textures == tile_property_ids.size());
  215. String previous_texture_name;
  216. Texture previous_texture;
  217. for (size_t i = 0; i < num_tiles_and_textures; i++)
  218. {
  219. const TilePropertyIds& ids = tile_property_ids[i];
  220. const Property* src_property = properties.GetProperty(ids.src);
  221. const String texture_name = src_property->Get<String>();
  222. // Skip the tile if it has no source name.
  223. // Declaring the name 'auto' is the same as an empty string. This gives an easy way to skip certain
  224. // tiles in a shorthand since we can't always declare an empty string.
  225. if (texture_name.empty() || texture_name == "auto")
  226. continue;
  227. // We are required to set default values before instancing the tile, thus, all properties should always be
  228. // dereferencable. If the debugger captures a zero-dereference, check that all properties for every tile is set
  229. // and default values are set just before instancing.
  230. DecoratorTiled::Tile& tile = tiles[i];
  231. Texture& texture = textures[i];
  232. const Sprite* sprite = instancer_interface.GetSprite(texture_name);
  233. // A tile is always either a sprite or an image.
  234. if (sprite)
  235. {
  236. tile.position = sprite->rectangle.Position();
  237. tile.size = sprite->rectangle.Size();
  238. tile.display_scale = sprite->sprite_sheet->display_scale;
  239. texture = sprite->sprite_sheet->texture_source.GetTexture(instancer_interface.GetRenderManager());
  240. }
  241. else
  242. {
  243. // No sprite found, so assume that the name is an image source. Since the common use case is to specify the
  244. // same texture for all tiles, check the previous texture first before fetching from the global database.
  245. if (texture_name == previous_texture_name)
  246. {
  247. texture = previous_texture;
  248. }
  249. else
  250. {
  251. texture = instancer_interface.GetTexture(texture_name);
  252. if (!texture)
  253. return false;
  254. previous_texture_name = texture_name;
  255. previous_texture = texture;
  256. }
  257. }
  258. if (ids.fit != PropertyId::Invalid)
  259. {
  260. RMLUI_ASSERT(ids.align_x != PropertyId::Invalid && ids.align_y != PropertyId::Invalid);
  261. const Property& fit_property = *properties.GetProperty(ids.fit);
  262. tile.fit_mode = (DecoratorTiled::TileFitMode)fit_property.value.Get<int>();
  263. if (sprite &&
  264. (tile.fit_mode == DecoratorTiled::TileFitMode::REPEAT || tile.fit_mode == DecoratorTiled::TileFitMode::REPEAT_X ||
  265. tile.fit_mode == DecoratorTiled::TileFitMode::REPEAT_Y))
  266. {
  267. Log::Message(Log::LT_WARNING, "Decorator 'fit' value is '%s', which is incompatible with sprites", fit_property.ToString().c_str());
  268. return false;
  269. }
  270. const Property* align_properties[2] = {properties.GetProperty(ids.align_x), properties.GetProperty(ids.align_y)};
  271. for (int dimension = 0; dimension < 2; dimension++)
  272. {
  273. using Style::LengthPercentage;
  274. LengthPercentage& align = tile.align[dimension];
  275. const Property& property = *align_properties[dimension];
  276. if (property.unit == Unit::KEYWORD)
  277. {
  278. enum { TOP_LEFT, CENTER, BOTTOM_RIGHT };
  279. switch (property.Get<int>())
  280. {
  281. case TOP_LEFT: align = LengthPercentage(LengthPercentage::Percentage, 0.0f); break;
  282. case CENTER: align = LengthPercentage(LengthPercentage::Percentage, 50.0f); break;
  283. case BOTTOM_RIGHT: align = LengthPercentage(LengthPercentage::Percentage, 100.0f); break;
  284. }
  285. }
  286. else if (property.unit == Unit::PERCENT)
  287. {
  288. align = LengthPercentage(LengthPercentage::Percentage, property.Get<float>());
  289. }
  290. else if (property.unit == Unit::PX)
  291. {
  292. align = LengthPercentage(LengthPercentage::Length, property.Get<float>());
  293. }
  294. else
  295. {
  296. Log::Message(Log::LT_WARNING, "Decorator alignment value is '%s' which uses an unsupported unit (use px, %%, or keyword)",
  297. property.ToString().c_str());
  298. }
  299. }
  300. }
  301. if (ids.orientation != PropertyId::Invalid)
  302. {
  303. const Property& orientation_property = *properties.GetProperty(ids.orientation);
  304. tile.orientation = (DecoratorTiled::TileOrientation)orientation_property.value.Get<int>();
  305. }
  306. }
  307. return true;
  308. }
  309. } // namespace Rml