BorderImage.cpp 9.1 KB

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