BorderImage.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  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 "Context.h"
  26. #include "ResourceCache.h"
  27. #include "Texture2D.h"
  28. #include "DebugNew.h"
  29. namespace Urho3D
  30. {
  31. OBJECTTYPESTATIC(BorderImage);
  32. BorderImage::BorderImage(Context* context) :
  33. UIElement(context),
  34. imageRect_(IntRect::ZERO),
  35. border_(IntRect::ZERO),
  36. hoverOffset_(IntVector2::ZERO),
  37. tiled_(false)
  38. {
  39. }
  40. BorderImage::~BorderImage()
  41. {
  42. }
  43. void BorderImage::RegisterObject(Context* context)
  44. {
  45. context->RegisterFactory<BorderImage>();
  46. ACCESSOR_ATTRIBUTE(BorderImage, VAR_RESOURCEREF, "Texture", GetTextureAttr, SetTextureAttr, ResourceRef, ResourceRef(Texture2D::GetTypeStatic()), AM_FILE);
  47. REF_ACCESSOR_ATTRIBUTE(BorderImage, VAR_INTRECT, "Image Rect", GetImageRect, SetImageRect, IntRect, IntRect::ZERO, AM_FILE);
  48. REF_ACCESSOR_ATTRIBUTE(BorderImage, VAR_INTRECT, "Border", GetBorder, SetBorder, IntRect, IntRect::ZERO, AM_FILE);
  49. REF_ACCESSOR_ATTRIBUTE(BorderImage, VAR_INTVECTOR2, "Hover Image Offset", GetHoverOffset, SetHoverOffset, IntVector2, IntVector2::ZERO, AM_FILE);
  50. ACCESSOR_ATTRIBUTE(BorderImage, VAR_BOOL, "Tiled", IsTiled, SetTiled, bool, true, AM_FILE);
  51. COPY_BASE_ATTRIBUTES(BorderImage, UIElement);
  52. }
  53. void BorderImage::SetTextureAttr(ResourceRef value)
  54. {
  55. ResourceCache* cache = GetSubsystem<ResourceCache>();
  56. SetTexture(cache->GetResource<Texture2D>(value.id_));
  57. }
  58. ResourceRef BorderImage::GetTextureAttr() const
  59. {
  60. return GetResourceRef(texture_, Texture2D::GetTypeStatic());
  61. }
  62. void BorderImage::GetBatches(PODVector<UIBatch>& batches, PODVector<UIQuad>& quads, const IntRect& currentScissor)
  63. {
  64. if (hovering_ || selected_)
  65. GetBatches(batches, quads, currentScissor, hoverOffset_);
  66. else
  67. GetBatches(batches, quads, currentScissor, IntVector2::ZERO);
  68. }
  69. void BorderImage::SetTexture(Texture* texture)
  70. {
  71. texture_ = texture;
  72. if (imageRect_ == IntRect::ZERO)
  73. SetFullImageRect();
  74. }
  75. void BorderImage::SetImageRect(const IntRect& rect)
  76. {
  77. if (rect != IntRect::ZERO)
  78. imageRect_ = rect;
  79. }
  80. void BorderImage::SetFullImageRect()
  81. {
  82. if (texture_)
  83. SetImageRect(IntRect(0, 0, texture_->GetWidth(), texture_->GetHeight()));
  84. }
  85. void BorderImage::SetBorder(const IntRect& rect)
  86. {
  87. border_.left_ = Max(rect.left_, 0);
  88. border_.top_ = Max(rect.top_, 0);
  89. border_.right_ = Max(rect.right_, 0);
  90. border_.bottom_ = Max(rect.bottom_, 0);
  91. }
  92. void BorderImage::SetHoverOffset(const IntVector2& offset)
  93. {
  94. hoverOffset_ = offset;
  95. }
  96. void BorderImage::SetHoverOffset(int x, int y)
  97. {
  98. hoverOffset_ = IntVector2(x, y);
  99. }
  100. void BorderImage::SetTiled(bool enable)
  101. {
  102. tiled_ = enable;
  103. }
  104. void BorderImage::GetBatches(PODVector<UIBatch>& batches, PODVector<UIQuad>& quads, const IntRect& currentScissor, const IntVector2& offset)
  105. {
  106. bool allOpaque = true;
  107. if (GetDerivedOpacity() < 1.0f || color_[C_TOPLEFT].a_ < 1.0f || color_[C_TOPRIGHT].a_ < 1.0f ||
  108. color_[C_BOTTOMLEFT].a_ < 1.0f || color_[C_BOTTOMRIGHT].a_ < 1.0f)
  109. allOpaque = false;
  110. UIBatch batch;
  111. batch.Begin(&quads);
  112. batch.blendMode_ = allOpaque ? BLEND_REPLACE : BLEND_ALPHA;
  113. batch.scissor_ = currentScissor;
  114. batch.texture_ = texture_;
  115. // Calculate size of the inner rect, and texture dimensions of the inner rect
  116. const IntVector2& size = GetSize();
  117. IntVector2 innerSize(
  118. Max(size.x_ - border_.left_ - border_.right_, 0),
  119. Max(size.y_ - border_.top_ - border_.bottom_, 0));
  120. IntVector2 innerTextureSize(
  121. Max(imageRect_.right_ - imageRect_.left_ - border_.left_ - border_.right_, 0),
  122. Max(imageRect_.bottom_ - imageRect_.top_ - border_.top_ - border_.bottom_, 0));
  123. IntVector2 topLeft(imageRect_.left_, imageRect_.top_);
  124. topLeft += offset;
  125. // Top
  126. if (border_.top_)
  127. {
  128. if (border_.left_)
  129. batch.AddQuad(*this, 0, 0, border_.left_, border_.top_, topLeft.x_, topLeft.y_);
  130. if (innerSize.x_)
  131. batch.AddQuad(*this, border_.left_, 0, innerSize.x_, border_.top_,
  132. topLeft.x_ + border_.left_, topLeft.y_, innerTextureSize.x_, border_.top_, tiled_);
  133. if (border_.right_)
  134. batch.AddQuad(*this, border_.left_ + innerSize.x_, 0, border_.right_, border_.top_,
  135. topLeft.x_ + border_.left_ + innerTextureSize.x_, topLeft.y_);
  136. }
  137. // Middle
  138. if (innerSize.y_)
  139. {
  140. if (border_.left_)
  141. batch.AddQuad(*this, 0, border_.top_, border_.left_, innerSize.y_,
  142. topLeft.x_, topLeft.y_ + border_.top_, border_.left_, innerTextureSize.y_, tiled_);
  143. if (innerSize.x_)
  144. batch.AddQuad(*this, border_.left_, border_.top_, innerSize.x_, innerSize.y_,
  145. topLeft.x_ + border_.left_, topLeft.y_ + border_.top_, innerTextureSize.x_, innerTextureSize.y_, tiled_);
  146. if (border_.right_)
  147. batch.AddQuad(*this, border_.left_ + innerSize.x_, border_.top_, border_.right_,
  148. innerSize.y_, topLeft.x_ + border_.left_ + innerTextureSize.x_, topLeft.y_ + border_.top_,
  149. border_.right_, innerTextureSize.y_, tiled_);
  150. }
  151. // Bottom
  152. if (border_.bottom_)
  153. {
  154. if (border_.left_)
  155. batch.AddQuad(*this, 0, border_.top_ + innerSize.y_, border_.left_, border_.bottom_,
  156. topLeft.x_, topLeft.y_ + border_.top_ + innerTextureSize.y_);
  157. if (innerSize.x_)
  158. batch.AddQuad(*this, border_.left_, border_.top_ + innerSize.y_, innerSize.x_,
  159. border_.bottom_, topLeft.x_ + border_.left_, topLeft.y_ + border_.top_ + innerTextureSize.y_,
  160. innerTextureSize.x_, border_.bottom_, tiled_);
  161. if (border_.right_)
  162. batch.AddQuad(*this, border_.left_ + innerSize.x_, border_.top_ + innerSize.y_,
  163. border_.right_, border_.bottom_, topLeft.x_ + border_.left_ + innerTextureSize.x_,
  164. topLeft.y_ + border_.top_ + innerTextureSize.y_);
  165. }
  166. UIBatch::AddOrMerge(batch, batches);
  167. // Reset hovering for next frame
  168. hovering_ = false;
  169. }
  170. }