BorderImage.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "BorderImage.h"
  25. #include "ResourceCache.h"
  26. #include "Texture2D.h"
  27. #include "DebugNew.h"
  28. BorderImage::BorderImage(const std::string& name) :
  29. UIElement(name),
  30. mImageRect(0, 0, 0, 0),
  31. mBorder(0, 0, 0, 0)
  32. {
  33. }
  34. BorderImage::~BorderImage()
  35. {
  36. }
  37. XMLElement BorderImage::loadParameters(XMLFile* file, const std::string& elementName, ResourceCache* cache)
  38. {
  39. XMLElement paramElem = UIElement::loadParameters(file, elementName, cache);
  40. if (paramElem.hasChildElement("texture"))
  41. setTexture(cache->getResource<Texture2D>(paramElem.getChildElement("texture").getString("name")));
  42. if (paramElem.hasChildElement("imagerect"))
  43. setImageRect(paramElem.getChildElement("imagerect").getIntRect("value"));
  44. if (paramElem.hasChildElement("border"))
  45. setBorder(paramElem.getChildElement("border").getIntRect("value"));
  46. return paramElem;
  47. }
  48. void BorderImage::getBatches(std::vector<UIBatch>& batches, std::vector<UIQuad>& quads, const IntRect& currentScissor)
  49. {
  50. UIBatch batch;
  51. batch.begin(&quads);
  52. batch.mBlendMode = getDerivedOpacity() == 1.0f ? BLEND_REPLACE : BLEND_ALPHA;
  53. batch.mScissor = currentScissor;
  54. batch.mTexture = mTexture;
  55. // Calculate size of the inner rect, and texture dimensions of the inner rect
  56. const IntVector2& size = getSize();
  57. IntVector2 innerSize(
  58. max(size.mX - mBorder.mLeft - mBorder.mRight, 0),
  59. max(size.mY - mBorder.mTop - mBorder.mBottom, 0));
  60. IntVector2 innerTextureSize(
  61. max(mImageRect.mRight - mImageRect.mLeft - mBorder.mLeft - mBorder.mRight, 0),
  62. max(mImageRect.mBottom - mImageRect.mTop - mBorder.mTop - mBorder.mBottom, 0));
  63. // Top
  64. if (mBorder.mTop)
  65. {
  66. if (mBorder.mLeft)
  67. batch.addQuad(*this, 0, 0, mBorder.mLeft, mBorder.mTop, mImageRect.mLeft, mImageRect.mTop);
  68. if (innerSize.mX)
  69. batch.addQuad(*this, mBorder.mLeft, 0, innerSize.mX, mBorder.mTop,
  70. mImageRect.mLeft + mBorder.mLeft, mImageRect.mTop, innerTextureSize.mX, mBorder.mTop);
  71. if (mBorder.mRight)
  72. batch.addQuad(*this, mBorder.mLeft + innerSize.mX, 0, mBorder.mRight, mBorder.mTop,
  73. mImageRect.mLeft + mBorder.mLeft + innerTextureSize.mX, mImageRect.mTop);
  74. }
  75. // Middle
  76. if (innerSize.mY)
  77. {
  78. if (mBorder.mLeft)
  79. batch.addQuad(*this, 0, mBorder.mTop, mBorder.mLeft, innerSize.mY,
  80. mImageRect.mLeft, mImageRect.mTop + mBorder.mTop, mBorder.mLeft, innerTextureSize.mY);
  81. if (innerSize.mX)
  82. batch.addQuad(*this, mBorder.mLeft, mBorder.mTop, innerSize.mX, innerSize.mY,
  83. mImageRect.mLeft + mBorder.mLeft, mImageRect.mTop + mBorder.mTop, innerTextureSize.mX, innerTextureSize.mY);
  84. if (mBorder.mRight)
  85. batch.addQuad(*this, mBorder.mLeft + innerSize.mX, mBorder.mTop, mBorder.mRight,
  86. innerSize.mY, mImageRect.mLeft + mBorder.mLeft + innerTextureSize.mX, mImageRect.mTop + mBorder.mTop,
  87. mBorder.mRight, innerTextureSize.mY);
  88. }
  89. // Bottom
  90. if (mBorder.mBottom)
  91. {
  92. if (mBorder.mLeft)
  93. batch.addQuad(*this, 0, mBorder.mTop + innerSize.mY, mBorder.mLeft, mBorder.mBottom,
  94. mImageRect.mLeft, mImageRect.mTop + mBorder.mTop + innerTextureSize.mY);
  95. if (innerSize.mX)
  96. batch.addQuad(*this, mBorder.mLeft, mBorder.mTop + innerSize.mY, innerSize.mX,
  97. mBorder.mBottom, mImageRect.mLeft + mBorder.mLeft, mImageRect.mTop + mBorder.mTop + innerTextureSize.mY,
  98. innerTextureSize.mX, mBorder.mBottom);
  99. if (mBorder.mRight)
  100. batch.addQuad(*this, mBorder.mLeft + innerSize.mX, mBorder.mTop + innerSize.mY,
  101. mBorder.mRight, mBorder.mBottom, mImageRect.mLeft + mBorder.mLeft + innerTextureSize.mX,
  102. mImageRect.mTop + mBorder.mTop + innerTextureSize.mY);
  103. }
  104. UIBatch::addOrMerge(batch, batches);
  105. }
  106. void BorderImage::setTexture(Texture* texture)
  107. {
  108. mTexture = texture;
  109. if (mImageRect == IntRect::sZero)
  110. setFullImageRect();
  111. }
  112. void BorderImage::setImageRect(const IntRect& rect)
  113. {
  114. mImageRect = rect;
  115. }
  116. void BorderImage::setImageRect(int left, int top, int right, int bottom)
  117. {
  118. mImageRect.mLeft = left;
  119. mImageRect.mTop = top;
  120. mImageRect.mRight = right;
  121. mImageRect.mBottom = bottom;
  122. }
  123. void BorderImage::setFullImageRect()
  124. {
  125. if (mTexture)
  126. {
  127. mImageRect.mLeft = 0;
  128. mImageRect.mTop = 0;
  129. mImageRect.mRight = mTexture->getWidth();
  130. mImageRect.mBottom = mTexture->getHeight();
  131. }
  132. }
  133. void BorderImage::setBorder(const IntRect& rect)
  134. {
  135. mBorder.mLeft = max(rect.mLeft, 0);
  136. mBorder.mTop = max(rect.mTop, 0);
  137. mBorder.mRight = max(rect.mRight, 0);
  138. mBorder.mBottom = max(rect.mBottom, 0);
  139. }
  140. void BorderImage::setBorder(int left, int top, int right, int bottom)
  141. {
  142. mBorder.mLeft = max(left, 0);
  143. mBorder.mTop = max(top, 0);
  144. mBorder.mRight = max(right, 0);
  145. mBorder.mBottom = max(bottom, 0);
  146. }