BorderImage.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. //
  2. // Copyright (c) 2008-2015 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 "../../Core/Context.h"
  23. #include "../../Graphics/Texture2D.h"
  24. #include "../../Resource/ResourceCache.h"
  25. #include "BorderImage.h"
  26. #include "../../DebugNew.h"
  27. namespace Atomic
  28. {
  29. extern const char* blendModeNames[];
  30. namespace SystemUI
  31. {
  32. extern const char* UI_CATEGORY;
  33. BorderImage::BorderImage(Context* context) :
  34. UIElement(context),
  35. imageRect_(IntRect::ZERO),
  36. border_(IntRect::ZERO),
  37. imageBorder_(IntRect::ZERO),
  38. hoverOffset_(IntVector2::ZERO),
  39. blendMode_(BLEND_REPLACE),
  40. tiled_(false)
  41. {
  42. }
  43. BorderImage::~BorderImage()
  44. {
  45. }
  46. void BorderImage::RegisterObject(Context* context)
  47. {
  48. context->RegisterFactory<BorderImage>(UI_CATEGORY);
  49. ATOMIC_COPY_BASE_ATTRIBUTES(UIElement);
  50. ATOMIC_MIXED_ACCESSOR_ATTRIBUTE("Texture", GetTextureAttr, SetTextureAttr, ResourceRef, ResourceRef(Texture2D::GetTypeStatic()),
  51. AM_FILE);
  52. ATOMIC_ACCESSOR_ATTRIBUTE("Image Rect", GetImageRect, SetImageRect, IntRect, IntRect::ZERO, AM_FILE);
  53. ATOMIC_ACCESSOR_ATTRIBUTE("Border", GetBorder, SetBorder, IntRect, IntRect::ZERO, AM_FILE);
  54. ATOMIC_ACCESSOR_ATTRIBUTE("Image Border", GetImageBorder, SetImageBorder, IntRect, IntRect::ZERO, AM_FILE);
  55. ATOMIC_ACCESSOR_ATTRIBUTE("Hover Image Offset", GetHoverOffset, SetHoverOffset, IntVector2, IntVector2::ZERO, AM_FILE);
  56. ATOMIC_ACCESSOR_ATTRIBUTE("Tiled", IsTiled, SetTiled, bool, false, AM_FILE);
  57. ATOMIC_ENUM_ACCESSOR_ATTRIBUTE("Blend Mode", GetBlendMode, SetBlendMode, BlendMode, blendModeNames, 0, AM_FILE);
  58. }
  59. void BorderImage::GetBatches(PODVector<SystemUIBatch>& batches, PODVector<float>& vertexData, const IntRect& currentScissor)
  60. {
  61. GetBatches(batches, vertexData, currentScissor, hovering_ || selected_ || HasFocus() ? hoverOffset_ : IntVector2::ZERO);
  62. }
  63. void BorderImage::SetTexture(Texture* texture)
  64. {
  65. texture_ = texture;
  66. if (imageRect_ == IntRect::ZERO)
  67. SetFullImageRect();
  68. }
  69. void BorderImage::SetImageRect(const IntRect& rect)
  70. {
  71. if (rect != IntRect::ZERO)
  72. imageRect_ = rect;
  73. }
  74. void BorderImage::SetFullImageRect()
  75. {
  76. if (texture_)
  77. SetImageRect(IntRect(0, 0, texture_->GetWidth(), texture_->GetHeight()));
  78. }
  79. void BorderImage::SetBorder(const IntRect& rect)
  80. {
  81. border_.left_ = Max(rect.left_, 0);
  82. border_.top_ = Max(rect.top_, 0);
  83. border_.right_ = Max(rect.right_, 0);
  84. border_.bottom_ = Max(rect.bottom_, 0);
  85. }
  86. void BorderImage::SetImageBorder(const IntRect& rect)
  87. {
  88. imageBorder_.left_ = Max(rect.left_, 0);
  89. imageBorder_.top_ = Max(rect.top_, 0);
  90. imageBorder_.right_ = Max(rect.right_, 0);
  91. imageBorder_.bottom_ = Max(rect.bottom_, 0);
  92. }
  93. void BorderImage::SetHoverOffset(const IntVector2& offset)
  94. {
  95. hoverOffset_ = offset;
  96. }
  97. void BorderImage::SetHoverOffset(int x, int y)
  98. {
  99. hoverOffset_ = IntVector2(x, y);
  100. }
  101. void BorderImage::SetBlendMode(BlendMode mode)
  102. {
  103. blendMode_ = mode;
  104. }
  105. void BorderImage::SetTiled(bool enable)
  106. {
  107. tiled_ = enable;
  108. }
  109. void BorderImage::GetBatches(PODVector<SystemUIBatch>& batches, PODVector<float>& vertexData, const IntRect& currentScissor,
  110. const IntVector2& offset)
  111. {
  112. bool allOpaque = true;
  113. if (GetDerivedOpacity() < 1.0f || color_[C_TOPLEFT].a_ < 1.0f || color_[C_TOPRIGHT].a_ < 1.0f ||
  114. color_[C_BOTTOMLEFT].a_ < 1.0f || color_[C_BOTTOMRIGHT].a_ < 1.0f)
  115. allOpaque = false;
  116. SystemUIBatch
  117. batch(this, blendMode_ == BLEND_REPLACE && !allOpaque ? BLEND_ALPHA : blendMode_, currentScissor, texture_, &vertexData);
  118. // Calculate size of the inner rect, and texture dimensions of the inner rect
  119. const IntRect& uvBorder = (imageBorder_ == IntRect::ZERO) ? border_ : imageBorder_;
  120. int x = GetIndentWidth();
  121. IntVector2 size = GetSize();
  122. size.x_ -= x;
  123. IntVector2 innerSize(
  124. Max(size.x_ - border_.left_ - border_.right_, 0),
  125. Max(size.y_ - border_.top_ - border_.bottom_, 0));
  126. IntVector2 innerUvSize(
  127. Max(imageRect_.right_ - imageRect_.left_ - uvBorder.left_ - uvBorder.right_, 0),
  128. Max(imageRect_.bottom_ - imageRect_.top_ - uvBorder.top_ - uvBorder.bottom_, 0));
  129. IntVector2 uvTopLeft(imageRect_.left_, imageRect_.top_);
  130. uvTopLeft += offset;
  131. // Top
  132. if (border_.top_)
  133. {
  134. if (border_.left_)
  135. batch.AddQuad(x, 0, border_.left_, border_.top_, uvTopLeft.x_, uvTopLeft.y_, uvBorder.left_, uvBorder.top_);
  136. if (innerSize.x_)
  137. batch.AddQuad(x + border_.left_, 0, innerSize.x_, border_.top_, uvTopLeft.x_ + uvBorder.left_, uvTopLeft.y_,
  138. innerUvSize.x_, uvBorder.top_, tiled_);
  139. if (border_.right_)
  140. batch.AddQuad(x + border_.left_ + innerSize.x_, 0, border_.right_, border_.top_,
  141. uvTopLeft.x_ + uvBorder.left_ + innerUvSize.x_, uvTopLeft.y_, uvBorder.right_, uvBorder.top_);
  142. }
  143. // Middle
  144. if (innerSize.y_)
  145. {
  146. if (border_.left_)
  147. batch.AddQuad(x, border_.top_, border_.left_, innerSize.y_, uvTopLeft.x_, uvTopLeft.y_ + uvBorder.top_,
  148. uvBorder.left_, innerUvSize.y_, tiled_);
  149. if (innerSize.x_)
  150. batch.AddQuad(x + border_.left_, border_.top_, innerSize.x_, innerSize.y_, uvTopLeft.x_ + uvBorder.left_,
  151. uvTopLeft.y_ + uvBorder.top_, innerUvSize.x_, innerUvSize.y_, tiled_);
  152. if (border_.right_)
  153. batch.AddQuad(x + border_.left_ + innerSize.x_, border_.top_, border_.right_, innerSize.y_,
  154. uvTopLeft.x_ + uvBorder.left_ + innerUvSize.x_, uvTopLeft.y_ + uvBorder.top_, uvBorder.right_, innerUvSize.y_,
  155. tiled_);
  156. }
  157. // Bottom
  158. if (border_.bottom_)
  159. {
  160. if (border_.left_)
  161. batch.AddQuad(x, border_.top_ + innerSize.y_, border_.left_, border_.bottom_, uvTopLeft.x_,
  162. uvTopLeft.y_ + uvBorder.top_ + innerUvSize.y_, uvBorder.left_, uvBorder.bottom_);
  163. if (innerSize.x_)
  164. batch.AddQuad(x + border_.left_, border_.top_ + innerSize.y_, innerSize.x_, border_.bottom_,
  165. uvTopLeft.x_ + uvBorder.left_, uvTopLeft.y_ + uvBorder.top_ + innerUvSize.y_, innerUvSize.x_, uvBorder.bottom_,
  166. tiled_);
  167. if (border_.right_)
  168. batch.AddQuad(x + border_.left_ + innerSize.x_, border_.top_ + innerSize.y_, border_.right_, border_.bottom_,
  169. uvTopLeft.x_ + uvBorder.left_ + innerUvSize.x_, uvTopLeft.y_ + uvBorder.top_ + innerUvSize.y_, uvBorder.right_,
  170. uvBorder.bottom_);
  171. }
  172. SystemUIBatch::AddOrMerge(batch, batches);
  173. // Reset hovering for next frame
  174. hovering_ = false;
  175. }
  176. void BorderImage::SetTextureAttr(const ResourceRef& value)
  177. {
  178. ResourceCache* cache = GetSubsystem<ResourceCache>();
  179. SetTexture(cache->GetResource<Texture2D>(value.name_));
  180. }
  181. ResourceRef BorderImage::GetTextureAttr() const
  182. {
  183. return GetResourceRef(texture_, Texture2D::GetTypeStatic());
  184. }
  185. }
  186. }