BorderImage.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 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 "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. {
  38. }
  39. BorderImage::~BorderImage()
  40. {
  41. }
  42. void BorderImage::RegisterObject(Context* context)
  43. {
  44. context->RegisterFactory<BorderImage>();
  45. }
  46. void BorderImage::SetStyle(const XMLElement& element)
  47. {
  48. UIElement::SetStyle(element);
  49. if (element.HasChild("texture"))
  50. SetTexture(GetSubsystem<ResourceCache>()->GetResource<Texture2D>(element.GetChild("texture").GetAttribute("name")));
  51. if (element.HasChild("imagerect"))
  52. {
  53. XMLElement imageElem = element.GetChild("imagerect");
  54. if (imageElem.HasAttribute("value"))
  55. SetImageRect(imageElem.GetIntRect("value"));
  56. }
  57. if (element.HasChild("border"))
  58. SetBorder(element.GetChild("border").GetIntRect("value"));
  59. if (element.HasChild("hoveroffset"))
  60. SetHoverOffset(element.GetChild("hoveroffset").GetIntVector2("value"));
  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::GetBatches(PODVector<UIBatch>& batches, PODVector<UIQuad>& quads, const IntRect& currentScissor, const IntVector2& offset)
  101. {
  102. bool allOpaque = true;
  103. if (GetDerivedOpacity() < 1.0f || color_[C_TOPLEFT].a_ < 1.0f || color_[C_TOPRIGHT].a_ < 1.0f ||
  104. color_[C_BOTTOMLEFT].a_ < 1.0f || color_[C_BOTTOMRIGHT].a_ < 1.0f)
  105. allOpaque = false;
  106. UIBatch batch;
  107. batch.Begin(&quads);
  108. batch.blendMode_ = allOpaque ? BLEND_REPLACE : BLEND_ALPHA;
  109. batch.scissor_ = currentScissor;
  110. batch.texture_ = texture_;
  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.x_ - border_.left_ - border_.right_, 0),
  115. Max(size.y_ - border_.top_ - border_.bottom_, 0));
  116. IntVector2 innerTextureSize(
  117. Max(imageRect_.right_ - imageRect_.left_ - border_.left_ - border_.right_, 0),
  118. Max(imageRect_.bottom_ - imageRect_.top_ - border_.top_ - border_.bottom_, 0));
  119. IntVector2 topLeft(imageRect_.left_, imageRect_.top_);
  120. topLeft += offset;
  121. // Top
  122. if (border_.top_)
  123. {
  124. if (border_.left_)
  125. batch.AddQuad(*this, 0, 0, border_.left_, border_.top_, topLeft.x_, topLeft.y_);
  126. if (innerSize.x_)
  127. batch.AddQuad(*this, border_.left_, 0, innerSize.x_, border_.top_,
  128. topLeft.x_ + border_.left_, topLeft.y_, innerTextureSize.x_, border_.top_);
  129. if (border_.right_)
  130. batch.AddQuad(*this, border_.left_ + innerSize.x_, 0, border_.right_, border_.top_,
  131. topLeft.x_ + border_.left_ + innerTextureSize.x_, topLeft.y_);
  132. }
  133. // Middle
  134. if (innerSize.y_)
  135. {
  136. if (border_.left_)
  137. batch.AddQuad(*this, 0, border_.top_, border_.left_, innerSize.y_,
  138. topLeft.x_, topLeft.y_ + border_.top_, border_.left_, innerTextureSize.y_);
  139. if (innerSize.x_)
  140. batch.AddQuad(*this, border_.left_, border_.top_, innerSize.x_, innerSize.y_,
  141. topLeft.x_ + border_.left_, topLeft.y_ + border_.top_, innerTextureSize.x_, innerTextureSize.y_);
  142. if (border_.right_)
  143. batch.AddQuad(*this, border_.left_ + innerSize.x_, border_.top_, border_.right_,
  144. innerSize.y_, topLeft.x_ + border_.left_ + innerTextureSize.x_, topLeft.y_ + border_.top_,
  145. border_.right_, innerTextureSize.y_);
  146. }
  147. // Bottom
  148. if (border_.bottom_)
  149. {
  150. if (border_.left_)
  151. batch.AddQuad(*this, 0, border_.top_ + innerSize.y_, border_.left_, border_.bottom_,
  152. topLeft.x_, topLeft.y_ + border_.top_ + innerTextureSize.y_);
  153. if (innerSize.x_)
  154. batch.AddQuad(*this, border_.left_, border_.top_ + innerSize.y_, innerSize.x_,
  155. border_.bottom_, topLeft.x_ + border_.left_, topLeft.y_ + border_.top_ + innerTextureSize.y_,
  156. innerTextureSize.x_, border_.bottom_);
  157. if (border_.right_)
  158. batch.AddQuad(*this, border_.left_ + innerSize.x_, border_.top_ + innerSize.y_,
  159. border_.right_, border_.bottom_, topLeft.x_ + border_.left_ + innerTextureSize.x_,
  160. topLeft.y_ + border_.top_ + innerTextureSize.y_);
  161. }
  162. UIBatch::AddOrMerge(batch, batches);
  163. // Reset hovering for next frame
  164. hovering_ = false;
  165. }
  166. }