Decorator.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 "../../Include/Rocket/Core/Decorator.h"
  29. #include "TextureDatabase.h"
  30. #include "TextureResource.h"
  31. #include "../../Include/Rocket/Core/PropertyDefinition.h"
  32. namespace Rocket {
  33. namespace Core {
  34. Decorator::Decorator()
  35. {
  36. }
  37. Decorator::~Decorator()
  38. {
  39. }
  40. // Attempts to load a texture into the list of textures in use by the decorator.
  41. int Decorator::LoadTexture(const String& texture_name, const String& rcss_path)
  42. {
  43. if (texture_name == first_texture.GetSource())
  44. return 0;
  45. for (size_t i = 0; i < additional_textures.size(); i++)
  46. {
  47. if (texture_name == additional_textures[i].GetSource())
  48. return (int)i + 1;
  49. }
  50. Texture texture;
  51. if (!texture.Load(texture_name, rcss_path))
  52. return -1;
  53. additional_textures.push_back(texture);
  54. return (int)additional_textures.size();
  55. }
  56. int Decorator::AddTexture(const Texture& texture)
  57. {
  58. if (!texture)
  59. return -1;
  60. if (!first_texture)
  61. first_texture = texture;
  62. if (first_texture == texture)
  63. return 0;
  64. auto it = std::find(additional_textures.begin(), additional_textures.end(), texture);
  65. if (it != additional_textures.end())
  66. return (int)(it - additional_textures.begin()) + 1;
  67. additional_textures.push_back(texture);
  68. return (int)additional_textures.size();
  69. }
  70. // Returns one of the decorator's previously loaded textures.
  71. const Texture* Decorator::GetTexture(int index) const
  72. {
  73. if (index == 0)
  74. return &first_texture;
  75. index -= 1;
  76. if (index < 0 || index >= (int)additional_textures.size())
  77. return nullptr;
  78. return &(additional_textures[index]);
  79. }
  80. }
  81. }