BorderImage.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. //
  2. // Copyright (c) 2008-2013 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "BorderImage.h"
  24. #include "Context.h"
  25. #include "ResourceCache.h"
  26. #include "Texture2D.h"
  27. #include "DebugNew.h"
  28. namespace Urho3D
  29. {
  30. static const char* blendModes[] =
  31. {
  32. "Replace",
  33. "Add",
  34. "Multiply",
  35. "Alpha",
  36. "AddAlpha",
  37. "PreMultiply",
  38. "InvDestAlpha",
  39. 0
  40. };
  41. OBJECTTYPESTATIC(BorderImage);
  42. BorderImage::BorderImage(Context* context) :
  43. UIElement(context),
  44. imageRect_(IntRect::ZERO),
  45. border_(IntRect::ZERO),
  46. hoverOffset_(IntVector2::ZERO),
  47. tiled_(false),
  48. blendMode_(0)
  49. {
  50. }
  51. BorderImage::~BorderImage()
  52. {
  53. }
  54. void BorderImage::RegisterObject(Context* context)
  55. {
  56. context->RegisterFactory<BorderImage>();
  57. ACCESSOR_ATTRIBUTE(BorderImage, VAR_RESOURCEREF, "Texture", GetTextureAttr, SetTextureAttr, ResourceRef, ResourceRef(Texture2D::GetTypeStatic()), AM_FILE);
  58. REF_ACCESSOR_ATTRIBUTE(BorderImage, VAR_INTRECT, "Image Rect", GetImageRect, SetImageRect, IntRect, IntRect::ZERO, AM_FILE);
  59. REF_ACCESSOR_ATTRIBUTE(BorderImage, VAR_INTRECT, "Border", GetBorder, SetBorder, IntRect, IntRect::ZERO, AM_FILE);
  60. REF_ACCESSOR_ATTRIBUTE(BorderImage, VAR_INTVECTOR2, "Hover Image Offset", GetHoverOffset, SetHoverOffset, IntVector2, IntVector2::ZERO, AM_FILE);
  61. ACCESSOR_ATTRIBUTE(BorderImage, VAR_BOOL, "Tiled", IsTiled, SetTiled, bool, true, AM_FILE);
  62. ENUM_ACCESSOR_ATTRIBUTE(BorderImage, "Blend Mode", GetBlendMode, SetBlendMode, unsigned, blendModes, 0, AM_FILE);
  63. COPY_BASE_ATTRIBUTES(BorderImage, UIElement);
  64. }
  65. void BorderImage::SetTextureAttr(ResourceRef value)
  66. {
  67. ResourceCache* cache = GetSubsystem<ResourceCache>();
  68. SetTexture(cache->GetResource<Texture2D>(value.id_));
  69. }
  70. ResourceRef BorderImage::GetTextureAttr() const
  71. {
  72. return GetResourceRef(texture_, Texture2D::GetTypeStatic());
  73. }
  74. void BorderImage::GetBatches(PODVector<UIBatch>& batches, PODVector<UIQuad>& quads, const IntRect& currentScissor)
  75. {
  76. GetBatches(batches, quads, currentScissor, hovering_ || selected_ ? hoverOffset_ : IntVector2::ZERO);
  77. }
  78. void BorderImage::SetTexture(Texture* texture)
  79. {
  80. texture_ = texture;
  81. if (imageRect_ == IntRect::ZERO)
  82. SetFullImageRect();
  83. }
  84. void BorderImage::SetImageRect(const IntRect& rect)
  85. {
  86. if (rect != IntRect::ZERO)
  87. imageRect_ = rect;
  88. }
  89. void BorderImage::SetFullImageRect()
  90. {
  91. if (texture_)
  92. SetImageRect(IntRect(0, 0, texture_->GetWidth(), texture_->GetHeight()));
  93. }
  94. void BorderImage::SetBorder(const IntRect& rect)
  95. {
  96. border_.left_ = Max(rect.left_, 0);
  97. border_.top_ = Max(rect.top_, 0);
  98. border_.right_ = Max(rect.right_, 0);
  99. border_.bottom_ = Max(rect.bottom_, 0);
  100. }
  101. void BorderImage::SetHoverOffset(const IntVector2& offset)
  102. {
  103. hoverOffset_ = offset;
  104. }
  105. void BorderImage::SetHoverOffset(int x, int y)
  106. {
  107. hoverOffset_ = IntVector2(x, y);
  108. }
  109. void BorderImage::SetTiled(bool enable)
  110. {
  111. tiled_ = enable;
  112. }
  113. void BorderImage::SetBlendMode(unsigned mode)
  114. {
  115. blendMode_ = mode;
  116. }
  117. void BorderImage::GetBatches(PODVector<UIBatch>& batches, PODVector<UIQuad>& quads, const IntRect& currentScissor, const IntVector2& offset)
  118. {
  119. bool allOpaque = true;
  120. if (GetDerivedOpacity() < 1.0f || color_[C_TOPLEFT].a_ < 1.0f || color_[C_TOPRIGHT].a_ < 1.0f ||
  121. color_[C_BOTTOMLEFT].a_ < 1.0f || color_[C_BOTTOMRIGHT].a_ < 1.0f)
  122. allOpaque = false;
  123. UIBatch batch(blendMode_ == BLEND_REPLACE && !allOpaque ? BLEND_ALPHA : static_cast<BlendMode>(blendMode_), currentScissor, texture_, &quads);
  124. // Calculate size of the inner rect, and texture dimensions of the inner rect
  125. const IntVector2& size = GetSize();
  126. IntVector2 innerSize(
  127. Max(size.x_ - border_.left_ - border_.right_, 0),
  128. Max(size.y_ - border_.top_ - border_.bottom_, 0));
  129. IntVector2 innerTextureSize(
  130. Max(imageRect_.right_ - imageRect_.left_ - border_.left_ - border_.right_, 0),
  131. Max(imageRect_.bottom_ - imageRect_.top_ - border_.top_ - border_.bottom_, 0));
  132. IntVector2 topLeft(imageRect_.left_, imageRect_.top_);
  133. topLeft += offset;
  134. // Top
  135. if (border_.top_)
  136. {
  137. if (border_.left_)
  138. batch.AddQuad(*this, 0, 0, border_.left_, border_.top_, topLeft.x_, topLeft.y_);
  139. if (innerSize.x_)
  140. batch.AddQuad(*this, border_.left_, 0, innerSize.x_, border_.top_,
  141. topLeft.x_ + border_.left_, topLeft.y_, innerTextureSize.x_, border_.top_, tiled_);
  142. if (border_.right_)
  143. batch.AddQuad(*this, border_.left_ + innerSize.x_, 0, border_.right_, border_.top_,
  144. topLeft.x_ + border_.left_ + innerTextureSize.x_, topLeft.y_);
  145. }
  146. // Middle
  147. if (innerSize.y_)
  148. {
  149. if (border_.left_)
  150. batch.AddQuad(*this, 0, border_.top_, border_.left_, innerSize.y_,
  151. topLeft.x_, topLeft.y_ + border_.top_, border_.left_, innerTextureSize.y_, tiled_);
  152. if (innerSize.x_)
  153. batch.AddQuad(*this, border_.left_, border_.top_, innerSize.x_, innerSize.y_,
  154. topLeft.x_ + border_.left_, topLeft.y_ + border_.top_, innerTextureSize.x_, innerTextureSize.y_, tiled_);
  155. if (border_.right_)
  156. batch.AddQuad(*this, border_.left_ + innerSize.x_, border_.top_, border_.right_,
  157. innerSize.y_, topLeft.x_ + border_.left_ + innerTextureSize.x_, topLeft.y_ + border_.top_,
  158. border_.right_, innerTextureSize.y_, tiled_);
  159. }
  160. // Bottom
  161. if (border_.bottom_)
  162. {
  163. if (border_.left_)
  164. batch.AddQuad(*this, 0, border_.top_ + innerSize.y_, border_.left_, border_.bottom_,
  165. topLeft.x_, topLeft.y_ + border_.top_ + innerTextureSize.y_);
  166. if (innerSize.x_)
  167. batch.AddQuad(*this, border_.left_, border_.top_ + innerSize.y_, innerSize.x_,
  168. border_.bottom_, topLeft.x_ + border_.left_, topLeft.y_ + border_.top_ + innerTextureSize.y_,
  169. innerTextureSize.x_, border_.bottom_, tiled_);
  170. if (border_.right_)
  171. batch.AddQuad(*this, border_.left_ + innerSize.x_, border_.top_ + innerSize.y_,
  172. border_.right_, border_.bottom_, topLeft.x_ + border_.left_ + innerTextureSize.x_,
  173. topLeft.y_ + border_.top_ + innerTextureSize.y_);
  174. }
  175. UIBatch::AddOrMerge(batch, batches);
  176. // Reset hovering for next frame
  177. hovering_ = false;
  178. }
  179. }