BorderImage.cpp 7.0 KB

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