BorderImage.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. OBJECTTYPESTATIC(BorderImage);
  30. BorderImage::BorderImage(Context* context) :
  31. UIElement(context),
  32. imageRect_(IntRect::ZERO),
  33. border_(IntRect::ZERO),
  34. hoverOffset_(IntVector2::ZERO)
  35. {
  36. }
  37. BorderImage::~BorderImage()
  38. {
  39. }
  40. void BorderImage::RegisterObject(Context* context)
  41. {
  42. context->RegisterFactory<BorderImage>();
  43. }
  44. void BorderImage::SetStyle(const XMLElement& element)
  45. {
  46. UIElement::SetStyle(element);
  47. if (element.HasChild("texture"))
  48. SetTexture(GetSubsystem<ResourceCache>()->GetResource<Texture2D>(element.GetChild("texture").GetAttribute("name")));
  49. if (element.HasChild("imagerect"))
  50. {
  51. XMLElement imageElem = element.GetChild("imagerect");
  52. if (imageElem.HasAttribute("value"))
  53. SetImageRect(imageElem.GetIntRect("value"));
  54. }
  55. if (element.HasChild("border"))
  56. SetBorder(element.GetChild("border").GetIntRect("value"));
  57. if (element.HasChild("hoveroffset"))
  58. SetHoverOffset(element.GetChild("hoveroffset").GetIntVector2("value"));
  59. }
  60. void BorderImage::GetBatches(PODVector<UIBatch>& batches, PODVector<UIQuad>& quads, const IntRect& currentScissor)
  61. {
  62. if (hovering_ || selected_)
  63. GetBatches(batches, quads, currentScissor, hoverOffset_);
  64. else
  65. GetBatches(batches, quads, currentScissor, IntVector2::ZERO);
  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::SetHoverOffset(const IntVector2& offset)
  91. {
  92. hoverOffset_ = offset;
  93. }
  94. void BorderImage::SetHoverOffset(int x, int y)
  95. {
  96. hoverOffset_ = IntVector2(x, y);
  97. }
  98. void BorderImage::GetBatches(PODVector<UIBatch>& batches, PODVector<UIQuad>& quads, const IntRect& currentScissor, const IntVector2& offset)
  99. {
  100. bool allOpaque = true;
  101. if (GetDerivedOpacity() < 1.0f || color_[C_TOPLEFT].a_ < 1.0f || color_[C_TOPRIGHT].a_ < 1.0f ||
  102. color_[C_BOTTOMLEFT].a_ < 1.0f || color_[C_BOTTOMRIGHT].a_ < 1.0f)
  103. allOpaque = false;
  104. UIBatch batch;
  105. batch.Begin(&quads);
  106. batch.blendMode_ = allOpaque ? BLEND_REPLACE : BLEND_ALPHA;
  107. batch.scissor_ = currentScissor;
  108. batch.texture_ = texture_;
  109. // Calculate size of the inner rect, and texture dimensions of the inner rect
  110. const IntVector2& size = GetSize();
  111. IntVector2 innerSize(
  112. Max(size.x_ - border_.left_ - border_.right_, 0),
  113. Max(size.y_ - border_.top_ - border_.bottom_, 0));
  114. IntVector2 innerTextureSize(
  115. Max(imageRect_.right_ - imageRect_.left_ - border_.left_ - border_.right_, 0),
  116. Max(imageRect_.bottom_ - imageRect_.top_ - border_.top_ - border_.bottom_, 0));
  117. IntVector2 topLeft(imageRect_.left_, imageRect_.top_);
  118. topLeft += offset;
  119. // Top
  120. if (border_.top_)
  121. {
  122. if (border_.left_)
  123. batch.AddQuad(*this, 0, 0, border_.left_, border_.top_, topLeft.x_, topLeft.y_);
  124. if (innerSize.x_)
  125. batch.AddQuad(*this, border_.left_, 0, innerSize.x_, border_.top_,
  126. topLeft.x_ + border_.left_, topLeft.y_, innerTextureSize.x_, border_.top_);
  127. if (border_.right_)
  128. batch.AddQuad(*this, border_.left_ + innerSize.x_, 0, border_.right_, border_.top_,
  129. topLeft.x_ + border_.left_ + innerTextureSize.x_, topLeft.y_);
  130. }
  131. // Middle
  132. if (innerSize.y_)
  133. {
  134. if (border_.left_)
  135. batch.AddQuad(*this, 0, border_.top_, border_.left_, innerSize.y_,
  136. topLeft.x_, topLeft.y_ + border_.top_, border_.left_, innerTextureSize.y_);
  137. if (innerSize.x_)
  138. batch.AddQuad(*this, border_.left_, border_.top_, innerSize.x_, innerSize.y_,
  139. topLeft.x_ + border_.left_, topLeft.y_ + border_.top_, innerTextureSize.x_, innerTextureSize.y_);
  140. if (border_.right_)
  141. batch.AddQuad(*this, border_.left_ + innerSize.x_, border_.top_, border_.right_,
  142. innerSize.y_, topLeft.x_ + border_.left_ + innerTextureSize.x_, topLeft.y_ + border_.top_,
  143. border_.right_, innerTextureSize.y_);
  144. }
  145. // Bottom
  146. if (border_.bottom_)
  147. {
  148. if (border_.left_)
  149. batch.AddQuad(*this, 0, border_.top_ + innerSize.y_, border_.left_, border_.bottom_,
  150. topLeft.x_, topLeft.y_ + border_.top_ + innerTextureSize.y_);
  151. if (innerSize.x_)
  152. batch.AddQuad(*this, border_.left_, border_.top_ + innerSize.y_, innerSize.x_,
  153. border_.bottom_, topLeft.x_ + border_.left_, topLeft.y_ + border_.top_ + innerTextureSize.y_,
  154. innerTextureSize.x_, border_.bottom_);
  155. if (border_.right_)
  156. batch.AddQuad(*this, border_.left_ + innerSize.x_, border_.top_ + innerSize.y_,
  157. border_.right_, border_.bottom_, topLeft.x_ + border_.left_ + innerTextureSize.x_,
  158. topLeft.y_ + border_.top_ + innerTextureSize.y_);
  159. }
  160. UIBatch::AddOrMerge(batch, batches);
  161. // Reset hovering for next frame
  162. hovering_ = false;
  163. }