Browse Source

Added tiled BorderImage patch from primitivewaste.
Added foreachv macro from Jason Kinzer.

Lasse Öörni 13 years ago
parent
commit
4cafb5c0d7
7 changed files with 71 additions and 6 deletions
  1. 1 0
      Docs/Urho3D.dox
  2. 8 0
      Engine/Container/ForEach.h
  3. 13 6
      Engine/UI/BorderImage.cpp
  4. 6 0
      Engine/UI/BorderImage.h
  5. 40 0
      Engine/UI/UIBatch.cpp
  6. 2 0
      Engine/UI/UIBatch.h
  7. 1 0
      Readme.txt

+ 1 - 0
Docs/Urho3D.dox

@@ -83,6 +83,7 @@ Contributions and bugfixes from:
 - Jason Kinzer
 - Jason Kinzer
 - Miika Santala
 - Miika Santala
 - Magic.Lixin
 - Magic.Lixin
+- primitivewaste
 - skaiware
 - skaiware
 
 
 DXT / ETC1 / PVRTC decompression code based on the Squish library and the Oolong %Engine.
 DXT / ETC1 / PVRTC decompression code based on the Squish library and the Oolong %Engine.

+ 8 - 0
Engine/Container/ForEach.h

@@ -100,3 +100,11 @@ Urho3D::RandomAccessConstIterator<T> End(const Urho3D::PODVector<T> *v) {
     for (auto it = _foreach_begin.value; it != _foreach_end.value; ++it) \
     for (auto it = _foreach_begin.value; it != _foreach_end.value; ++it) \
     if (bool _foreach_flag = false) { } else \
     if (bool _foreach_flag = false) { } else \
     for (VAL = *it; !_foreach_flag; _foreach_flag = true)
     for (VAL = *it; !_foreach_flag; _foreach_flag = true)
+
+#define foreachv(ITER, VAL, VALS) \
+    if (const auto& _foreach_begin = Urho3D::make_false_wrapper(Urho3D::Begin(VALS))) { } else \
+    if (const auto& _foreach_end = Urho3D::make_false_wrapper(Urho3D::End(VALS))) { } else \
+    if (int ITER = 0) { } else \
+    for (auto it = _foreach_begin.value; it != _foreach_end.value; ++it) \
+    if (bool _foreach_flag = false) { } else \
+    for (VAL = *it; !_foreach_flag; ITER++, _foreach_flag = true)

+ 13 - 6
Engine/UI/BorderImage.cpp

@@ -38,7 +38,8 @@ BorderImage::BorderImage(Context* context) :
     UIElement(context),
     UIElement(context),
     imageRect_(IntRect::ZERO),
     imageRect_(IntRect::ZERO),
     border_(IntRect::ZERO),
     border_(IntRect::ZERO),
-    hoverOffset_(IntVector2::ZERO)
+    hoverOffset_(IntVector2::ZERO),
+    tiled_(false)
 {
 {
 }
 }
 
 
@@ -54,6 +55,7 @@ void BorderImage::RegisterObject(Context* context)
     REF_ACCESSOR_ATTRIBUTE(BorderImage, VAR_INTRECT, "Image Rect", GetImageRect, SetImageRect, IntRect, IntRect::ZERO, AM_FILE);
     REF_ACCESSOR_ATTRIBUTE(BorderImage, VAR_INTRECT, "Image Rect", GetImageRect, SetImageRect, IntRect, IntRect::ZERO, AM_FILE);
     REF_ACCESSOR_ATTRIBUTE(BorderImage, VAR_INTRECT, "Border", GetBorder, SetBorder, IntRect, IntRect::ZERO, AM_FILE);
     REF_ACCESSOR_ATTRIBUTE(BorderImage, VAR_INTRECT, "Border", GetBorder, SetBorder, IntRect, IntRect::ZERO, AM_FILE);
     REF_ACCESSOR_ATTRIBUTE(BorderImage, VAR_INTVECTOR2, "Hover Image Offset", GetHoverOffset, SetHoverOffset, IntVector2, IntVector2::ZERO, AM_FILE);
     REF_ACCESSOR_ATTRIBUTE(BorderImage, VAR_INTVECTOR2, "Hover Image Offset", GetHoverOffset, SetHoverOffset, IntVector2, IntVector2::ZERO, AM_FILE);
+    ACCESSOR_ATTRIBUTE(BorderImage, VAR_BOOL, "Tiled", IsTiled, SetTiled, bool, true, AM_FILE);
     COPY_BASE_ATTRIBUTES(BorderImage, UIElement);
     COPY_BASE_ATTRIBUTES(BorderImage, UIElement);
 }
 }
 
 
@@ -113,6 +115,11 @@ void BorderImage::SetHoverOffset(int x, int y)
     hoverOffset_ = IntVector2(x, y);
     hoverOffset_ = IntVector2(x, y);
 }
 }
 
 
+void BorderImage::SetTiled(bool enable)
+{
+    tiled_ = enable;
+}
+
 void BorderImage::GetBatches(PODVector<UIBatch>& batches, PODVector<UIQuad>& quads, const IntRect& currentScissor, const IntVector2& offset)
 void BorderImage::GetBatches(PODVector<UIBatch>& batches, PODVector<UIQuad>& quads, const IntRect& currentScissor, const IntVector2& offset)
 {
 {
     bool allOpaque = true;
     bool allOpaque = true;
@@ -145,7 +152,7 @@ void BorderImage::GetBatches(PODVector<UIBatch>& batches, PODVector<UIQuad>& qua
             batch.AddQuad(*this, 0, 0, border_.left_, border_.top_, topLeft.x_, topLeft.y_);
             batch.AddQuad(*this, 0, 0, border_.left_, border_.top_, topLeft.x_, topLeft.y_);
         if (innerSize.x_)
         if (innerSize.x_)
             batch.AddQuad(*this, border_.left_, 0, innerSize.x_, border_.top_,
             batch.AddQuad(*this, border_.left_, 0, innerSize.x_, border_.top_,
-            topLeft.x_ + border_.left_, topLeft.y_, innerTextureSize.x_, border_.top_);
+            topLeft.x_ + border_.left_, topLeft.y_, innerTextureSize.x_, border_.top_, tiled_);
         if (border_.right_)
         if (border_.right_)
             batch.AddQuad(*this, border_.left_ + innerSize.x_, 0, border_.right_, border_.top_,
             batch.AddQuad(*this, border_.left_ + innerSize.x_, 0, border_.right_, border_.top_,
             topLeft.x_ + border_.left_ + innerTextureSize.x_, topLeft.y_);
             topLeft.x_ + border_.left_ + innerTextureSize.x_, topLeft.y_);
@@ -155,14 +162,14 @@ void BorderImage::GetBatches(PODVector<UIBatch>& batches, PODVector<UIQuad>& qua
     {
     {
         if (border_.left_)
         if (border_.left_)
             batch.AddQuad(*this, 0, border_.top_, border_.left_, innerSize.y_,
             batch.AddQuad(*this, 0, border_.top_, border_.left_, innerSize.y_,
-            topLeft.x_, topLeft.y_ + border_.top_, border_.left_, innerTextureSize.y_);
+            topLeft.x_, topLeft.y_ + border_.top_, border_.left_, innerTextureSize.y_, tiled_);
         if (innerSize.x_)
         if (innerSize.x_)
             batch.AddQuad(*this, border_.left_, border_.top_, innerSize.x_, innerSize.y_,
             batch.AddQuad(*this, border_.left_, border_.top_, innerSize.x_, innerSize.y_,
-            topLeft.x_ + border_.left_, topLeft.y_ + border_.top_, innerTextureSize.x_, innerTextureSize.y_);
+            topLeft.x_ + border_.left_, topLeft.y_ + border_.top_, innerTextureSize.x_, innerTextureSize.y_, tiled_);
         if (border_.right_)
         if (border_.right_)
             batch.AddQuad(*this, border_.left_ + innerSize.x_, border_.top_, border_.right_,
             batch.AddQuad(*this, border_.left_ + innerSize.x_, border_.top_, border_.right_,
             innerSize.y_, topLeft.x_ + border_.left_ + innerTextureSize.x_, topLeft.y_ + border_.top_,
             innerSize.y_, topLeft.x_ + border_.left_ + innerTextureSize.x_, topLeft.y_ + border_.top_,
-            border_.right_, innerTextureSize.y_);
+            border_.right_, innerTextureSize.y_, tiled_);
     }
     }
     // Bottom
     // Bottom
     if (border_.bottom_)
     if (border_.bottom_)
@@ -173,7 +180,7 @@ void BorderImage::GetBatches(PODVector<UIBatch>& batches, PODVector<UIQuad>& qua
         if (innerSize.x_)
         if (innerSize.x_)
             batch.AddQuad(*this, border_.left_, border_.top_ + innerSize.y_, innerSize.x_,
             batch.AddQuad(*this, border_.left_, border_.top_ + innerSize.y_, innerSize.x_,
             border_.bottom_, topLeft.x_ + border_.left_, topLeft.y_ + border_.top_ + innerTextureSize.y_,
             border_.bottom_, topLeft.x_ + border_.left_, topLeft.y_ + border_.top_ + innerTextureSize.y_,
-            innerTextureSize.x_, border_.bottom_);
+            innerTextureSize.x_, border_.bottom_, tiled_);
         if (border_.right_)
         if (border_.right_)
             batch.AddQuad(*this, border_.left_ + innerSize.x_, border_.top_ + innerSize.y_,
             batch.AddQuad(*this, border_.left_ + innerSize.x_, border_.top_ + innerSize.y_,
             border_.right_, border_.bottom_, topLeft.x_ + border_.left_ + innerTextureSize.x_, 
             border_.right_, border_.bottom_, topLeft.x_ + border_.left_ + innerTextureSize.x_, 

+ 6 - 0
Engine/UI/BorderImage.h

@@ -59,6 +59,10 @@ public:
     void SetHoverOffset(const IntVector2& offset);
     void SetHoverOffset(const IntVector2& offset);
     /// Set offset to image rectangle used on hover.
     /// Set offset to image rectangle used on hover.
     void SetHoverOffset(int x, int y);
     void SetHoverOffset(int x, int y);
+    /// Set tiled.
+    void SetTiled(bool enable);
+    /// Return whether is tiled.
+    bool IsTiled() const { return tiled_; }
     
     
     /// Return texture.
     /// Return texture.
     Texture* GetTexture() const { return texture_; }
     Texture* GetTexture() const { return texture_; }
@@ -86,6 +90,8 @@ protected:
     IntRect border_;
     IntRect border_;
     /// Offset to image rectangle on hover.
     /// Offset to image rectangle on hover.
     IntVector2 hoverOffset_;
     IntVector2 hoverOffset_;
+    /// Tiled flag.
+    bool tiled_;
 };
 };
 
 
 }
 }

+ 40 - 0
Engine/UI/UIBatch.cpp

@@ -123,6 +123,46 @@ void UIBatch::AddQuad(UIElement& element, int x, int y, int width, int height, i
     quadCount_++;
     quadCount_++;
 }
 }
 
 
+void UIBatch::AddQuad(UIElement& element, int x, int y, int width, int height, int texOffsetX, int texOffsetY, int texWidth,
+    int texHeight, bool tiled)
+{
+    if (!quads_)
+        return;
+
+    unsigned uintColor = element.GetUIntColor();
+    // If alpha is 0, nothing will be rendered, so do not add the quad
+    if (!(uintColor & 0xff000000))
+        return;
+
+    if (!tiled)
+    {
+        AddQuad(element, x, y, width, height, texOffsetX, texOffsetY, texWidth, texHeight);
+        return;
+    }
+
+    int tileX = 0;
+    int tileY = 0;
+    int tileW = 0;
+    int tileH = 0;
+
+    while(tileY < height)
+    {
+        tileX = 0;
+        tileH = Min(height - tileY, texHeight);
+
+        while(tileX < width)
+        {
+            tileW = Min(width - tileX, texWidth);
+
+            AddQuad(element, x + tileX, y + tileY, tileW, tileH, texOffsetX, texOffsetY, tileW, tileH);
+
+            tileX += tileW;
+        }
+
+        tileY += tileH;
+    }
+}
+
 void UIBatch::AddQuad(UIElement& element, int x, int y, int width, int height, int texOffsetX, int texOffsetY, int texWidth,
 void UIBatch::AddQuad(UIElement& element, int x, int y, int width, int height, int texOffsetX, int texOffsetY, int texWidth,
     int texHeight, const Color& color)
     int texHeight, const Color& color)
 {
 {

+ 2 - 0
Engine/UI/UIBatch.h

@@ -84,6 +84,8 @@ public:
     void AddQuad(UIElement& element, int x, int y, int width, int height, int texOffsetX, int texOffsetY);
     void AddQuad(UIElement& element, int x, int y, int width, int height, int texOffsetX, int texOffsetY);
     /// Add a quad with scaled texture.
     /// Add a quad with scaled texture.
     void AddQuad(UIElement& element, int x, int y, int width, int height, int texOffsetX, int texOffsetY, int texWidth, int texHeight);
     void AddQuad(UIElement& element, int x, int y, int width, int height, int texOffsetX, int texOffsetY, int texWidth, int texHeight);
+    /// Add a quad with tiled texture.
+    void AddQuad(UIElement& element, int x, int y, int width, int height, int texOffsetX, int texOffsetY, int texWidth, int texHeight, bool tiled);
     /// Add a quad with custom color.
     /// Add a quad with custom color.
     void AddQuad(UIElement& element, int x, int y, int width, int height, int texOffsetX, int texOffsetY, int texWidth, int texHeight, const Color& color);
     void AddQuad(UIElement& element, int x, int y, int width, int height, int texOffsetX, int texOffsetY, int texWidth, int texHeight, const Color& color);
     /// Merge with another batch.
     /// Merge with another batch.

+ 1 - 0
Readme.txt

@@ -56,6 +56,7 @@ Contributions and bugfixes from:
 - Jason Kinzer
 - Jason Kinzer
 - Miika Santala
 - Miika Santala
 - Magic.Lixin
 - Magic.Lixin
+- primitivewaste
 - skaiware
 - skaiware
 
 
 DXT / ETC1 / PVRTC decompression code based on the Squish library and the Oolong
 DXT / ETC1 / PVRTC decompression code based on the Squish library and the Oolong