BorderImage.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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(IntRect::sZero),
  31. mBorder(IntRect::sZero),
  32. mHoverOffset(IntVector2::sZero)
  33. {
  34. }
  35. BorderImage::~BorderImage()
  36. {
  37. }
  38. void BorderImage::setStyle(const XMLElement& element, ResourceCache* cache)
  39. {
  40. if (!cache)
  41. return;
  42. UIElement::setStyle(element, cache);
  43. if (element.hasChildElement("texture"))
  44. setTexture(cache->getResource<Texture2D>(element.getChildElement("texture").getString("name")));
  45. if (element.hasChildElement("imagerect"))
  46. {
  47. XMLElement imageElem = element.getChildElement("imagerect");
  48. if (imageElem.hasAttribute("value"))
  49. setImageRect(imageElem.getIntRect("value"));
  50. }
  51. if (element.hasChildElement("border"))
  52. setBorder(element.getChildElement("border").getIntRect("value"));
  53. if (element.hasChildElement("hoveroffset"))
  54. setHoverOffset(element.getChildElement("hoveroffset").getIntVector2("value"));
  55. }
  56. void BorderImage::getBatches(std::vector<UIBatch>& batches, std::vector<UIQuad>& quads, const IntRect& currentScissor)
  57. {
  58. if ((mHovering) || (mSelected))
  59. getBatches(batches, quads, currentScissor, mHoverOffset);
  60. else
  61. getBatches(batches, quads, currentScissor, IntVector2::sZero);
  62. }
  63. void BorderImage::setTexture(Texture* texture)
  64. {
  65. mTexture = texture;
  66. if (mImageRect == IntRect::sZero)
  67. setFullImageRect();
  68. }
  69. void BorderImage::setImageRect(const IntRect& rect)
  70. {
  71. if (rect != IntRect::sZero)
  72. mImageRect = rect;
  73. }
  74. void BorderImage::setImageRect(int left, int top, int right, int bottom)
  75. {
  76. setImageRect(IntRect(left, top, right, bottom));
  77. }
  78. void BorderImage::setFullImageRect()
  79. {
  80. if (mTexture)
  81. setImageRect(IntRect(0, 0, mTexture->getWidth(), mTexture->getHeight()));
  82. }
  83. void BorderImage::setBorder(const IntRect& rect)
  84. {
  85. mBorder.mLeft = max(rect.mLeft, 0);
  86. mBorder.mTop = max(rect.mTop, 0);
  87. mBorder.mRight = max(rect.mRight, 0);
  88. mBorder.mBottom = max(rect.mBottom, 0);
  89. }
  90. void BorderImage::setBorder(int left, int top, int right, int bottom)
  91. {
  92. setBorder(IntRect(left, top, right, bottom));
  93. }
  94. void BorderImage::setHoverOffset(const IntVector2& offset)
  95. {
  96. mHoverOffset = offset;
  97. }
  98. void BorderImage::setHoverOffset(int x, int y)
  99. {
  100. mHoverOffset = IntVector2(x, y);
  101. }
  102. void BorderImage::getBatches(std::vector<UIBatch>& batches, std::vector<UIQuad>& quads, const IntRect& currentScissor, const IntVector2& offset)
  103. {
  104. bool allOpaque = true;
  105. if ((getDerivedOpacity() < 1.0f) || (mColor[C_TOPLEFT].mA < 1.0f) || (mColor[C_TOPRIGHT].mA < 1.0f) ||
  106. (mColor[C_BOTTOMLEFT].mA < 1.0f) || (mColor[C_BOTTOMRIGHT].mA < 1.0f))
  107. allOpaque = false;
  108. UIBatch batch;
  109. batch.begin(&quads);
  110. batch.mBlendMode = allOpaque ? BLEND_REPLACE : BLEND_ALPHA;
  111. batch.mScissor = currentScissor;
  112. batch.mTexture = mTexture;
  113. // Calculate size of the inner rect, and texture dimensions of the inner rect
  114. const IntVector2& size = getSize();
  115. IntVector2 innerSize(
  116. max(size.mX - mBorder.mLeft - mBorder.mRight, 0),
  117. max(size.mY - mBorder.mTop - mBorder.mBottom, 0));
  118. IntVector2 innerTextureSize(
  119. max(mImageRect.mRight - mImageRect.mLeft - mBorder.mLeft - mBorder.mRight, 0),
  120. max(mImageRect.mBottom - mImageRect.mTop - mBorder.mTop - mBorder.mBottom, 0));
  121. IntVector2 topLeft(mImageRect.mLeft, mImageRect.mTop);
  122. topLeft += offset;
  123. // Top
  124. if (mBorder.mTop)
  125. {
  126. if (mBorder.mLeft)
  127. batch.addQuad(*this, 0, 0, mBorder.mLeft, mBorder.mTop, topLeft.mX, topLeft.mY);
  128. if (innerSize.mX)
  129. batch.addQuad(*this, mBorder.mLeft, 0, innerSize.mX, mBorder.mTop,
  130. topLeft.mX + mBorder.mLeft, topLeft.mY, innerTextureSize.mX, mBorder.mTop);
  131. if (mBorder.mRight)
  132. batch.addQuad(*this, mBorder.mLeft + innerSize.mX, 0, mBorder.mRight, mBorder.mTop,
  133. topLeft.mX + mBorder.mLeft + innerTextureSize.mX, topLeft.mY);
  134. }
  135. // Middle
  136. if (innerSize.mY)
  137. {
  138. if (mBorder.mLeft)
  139. batch.addQuad(*this, 0, mBorder.mTop, mBorder.mLeft, innerSize.mY,
  140. topLeft.mX, topLeft.mY + mBorder.mTop, mBorder.mLeft, innerTextureSize.mY);
  141. if (innerSize.mX)
  142. batch.addQuad(*this, mBorder.mLeft, mBorder.mTop, innerSize.mX, innerSize.mY,
  143. topLeft.mX + mBorder.mLeft, topLeft.mY + mBorder.mTop, innerTextureSize.mX, innerTextureSize.mY);
  144. if (mBorder.mRight)
  145. batch.addQuad(*this, mBorder.mLeft + innerSize.mX, mBorder.mTop, mBorder.mRight,
  146. innerSize.mY, topLeft.mX + mBorder.mLeft + innerTextureSize.mX, topLeft.mY + mBorder.mTop,
  147. mBorder.mRight, innerTextureSize.mY);
  148. }
  149. // Bottom
  150. if (mBorder.mBottom)
  151. {
  152. if (mBorder.mLeft)
  153. batch.addQuad(*this, 0, mBorder.mTop + innerSize.mY, mBorder.mLeft, mBorder.mBottom,
  154. topLeft.mX, topLeft.mY + mBorder.mTop + innerTextureSize.mY);
  155. if (innerSize.mX)
  156. batch.addQuad(*this, mBorder.mLeft, mBorder.mTop + innerSize.mY, innerSize.mX,
  157. mBorder.mBottom, topLeft.mX + mBorder.mLeft, topLeft.mY + mBorder.mTop + innerTextureSize.mY,
  158. innerTextureSize.mX, mBorder.mBottom);
  159. if (mBorder.mRight)
  160. batch.addQuad(*this, mBorder.mLeft + innerSize.mX, mBorder.mTop + innerSize.mY,
  161. mBorder.mRight, mBorder.mBottom, topLeft.mX + mBorder.mLeft + innerTextureSize.mX,
  162. topLeft.mY + mBorder.mTop + innerTextureSize.mY);
  163. }
  164. UIBatch::addOrMerge(batch, batches);
  165. // Reset hovering for next frame
  166. mHovering = false;
  167. }