Forráskód Böngészése

Merge remote-tracking branch 'gogoprog/feature-draw_and_texture_rects'

Lasse Öörni 9 éve
szülő
commit
c3fc83c7f3

+ 8 - 0
Source/Urho3D/AngelScript/Urho2DAPI.cpp

@@ -127,6 +127,14 @@ template <class T> void RegisterStaticSprite2D(asIScriptEngine* engine, const ch
     engine->RegisterObjectMethod(className, "const Vector2& get_hotSpot() const", asMETHOD(T, GetHotSpot), asCALL_THISCALL);
     engine->RegisterObjectMethod(className, "void set_customMaterial(Material@+)", asMETHOD(T, SetCustomMaterial), asCALL_THISCALL);
     engine->RegisterObjectMethod(className, "Material@+ get_customMaterial() const", asMETHOD(T, GetCustomMaterial), asCALL_THISCALL);
+    engine->RegisterObjectMethod(className, "void set_useDrawRect(bool)", asMETHOD(T, SetUseDrawRect), asCALL_THISCALL);
+    engine->RegisterObjectMethod(className, "bool get_useDrawRect() const", asMETHOD(T, GetUseDrawRect), asCALL_THISCALL);
+    engine->RegisterObjectMethod(className, "void set_useTextureRect(bool)", asMETHOD(T, SetUseTextureRect), asCALL_THISCALL);
+    engine->RegisterObjectMethod(className, "bool get_useTextureRect() const", asMETHOD(T, GetUseTextureRect), asCALL_THISCALL);
+    engine->RegisterObjectMethod(className, "void set_drawRect(const Rect&)", asMETHOD(T, SetDrawRect), asCALL_THISCALL);
+    engine->RegisterObjectMethod(className, "const Rect& get_drawRect() const", asMETHOD(T, GetDrawRect), asCALL_THISCALL);
+    engine->RegisterObjectMethod(className, "void set_textureRect(const Rect&)", asMETHOD(T, SetTextureRect), asCALL_THISCALL);
+    engine->RegisterObjectMethod(className, "const Rect& get_textureRect() const", asMETHOD(T, GetTextureRect), asCALL_THISCALL);
 }
 
 static void RegisterStaticSprite2D(asIScriptEngine* engine)

+ 24 - 0
Source/Urho3D/Core/Variant.cpp

@@ -57,6 +57,7 @@ static const char* typeNames[] =
     "VariantVector",
     "VariantMap",
     "IntRect",
+    "Rect",
     "IntVector2",
     "Ptr",
     "Matrix3",
@@ -187,6 +188,9 @@ bool Variant::operator ==(const Variant& rhs) const
     case VAR_INTRECT:
         return *(reinterpret_cast<const IntRect*>(&value_)) == *(reinterpret_cast<const IntRect*>(&rhs.value_));
 
+    case VAR_RECT:
+        return *(reinterpret_cast<const Rect*>(&value_)) == *(reinterpret_cast<const Rect*>(&rhs.value_));
+
     case VAR_INTVECTOR2:
         return *(reinterpret_cast<const IntVector2*>(&value_)) == *(reinterpret_cast<const IntVector2*>(&rhs.value_));
 
@@ -324,6 +328,10 @@ void Variant::FromString(VariantType type, const char* value)
         *this = ToIntRect(value);
         break;
 
+    case VAR_RECT:
+        *this = ToRect(value);
+        break;
+
     case VAR_INTVECTOR2:
         *this = ToIntVector2(value);
         break;
@@ -423,6 +431,9 @@ String Variant::ToString() const
     case VAR_INTRECT:
         return (reinterpret_cast<const IntRect*>(&value_))->ToString();
 
+    case VAR_RECT:
+        return (reinterpret_cast<const Rect*>(&value_))->ToString();
+
     case VAR_INTVECTOR2:
         return (reinterpret_cast<const IntVector2*>(&value_))->ToString();
 
@@ -510,6 +521,9 @@ bool Variant::IsZero() const
     case VAR_INTRECT:
         return *reinterpret_cast<const IntRect*>(&value_) == IntRect::ZERO;
 
+    case VAR_RECT:
+        return *reinterpret_cast<const Rect*>(&value_) == Rect::ZERO;
+
     case VAR_INTVECTOR2:
         return *reinterpret_cast<const IntVector2*>(&value_) == IntVector2::ZERO;
 
@@ -706,6 +720,11 @@ template <> const IntRect& Variant::Get<const IntRect&>() const
     return GetIntRect();
 }
 
+template <> const Rect& Variant::Get<const Rect&>() const
+{
+    return GetRect();
+}
+
 template <> const IntVector2& Variant::Get<const IntVector2&>() const
 {
     return GetIntVector2();
@@ -801,6 +820,11 @@ template <> IntRect Variant::Get<IntRect>() const
     return GetIntRect();
 }
 
+template <> Rect Variant::Get<Rect>() const
+{
+    return GetRect();
+}
+
 template <> IntVector2 Variant::Get<IntVector2>() const
 {
     return GetIntVector2();

+ 34 - 0
Source/Urho3D/Core/Variant.h

@@ -53,6 +53,7 @@ enum VariantType
     VAR_VARIANTVECTOR,
     VAR_VARIANTMAP,
     VAR_INTRECT,
+    VAR_RECT,
     VAR_INTVECTOR2,
     VAR_PTR,
     VAR_MATRIX3,
@@ -359,6 +360,13 @@ public:
         *this = value;
     }
 
+    /// Construct from a rect.
+    Variant(const Rect& value) :
+        type_(VAR_NONE)
+    {
+        *this = value;
+    }
+
     /// Construct from an IntVector2.
     Variant(const IntVector2& value) :
         type_(VAR_NONE)
@@ -615,6 +623,14 @@ public:
         return *this;
     }
 
+    /// Assign from a rect.
+    Variant& operator =(const Rect& rhs)
+    {
+        SetType(VAR_RECT);
+        *(reinterpret_cast<Rect*>(&value_)) = rhs;
+        return *this;
+    }
+
     /// Assign from an IntVector2.
     Variant& operator =(const IntVector2& rhs)
     {
@@ -761,6 +777,12 @@ public:
         return type_ == VAR_INTRECT ? *(reinterpret_cast<const IntRect*>(&value_)) == rhs : false;
     }
 
+    /// Test for equality with a rect. To return true, both the type and value must match.
+    bool operator ==(const Rect& rhs) const
+    {
+        return type_ == VAR_RECT ? *(reinterpret_cast<const Rect*>(&value_)) == rhs : false;
+    }
+
     /// Test for equality with an IntVector2. To return true, both the type and value must match.
     bool operator ==(const IntVector2& rhs) const
     {
@@ -859,6 +881,9 @@ public:
     /// Test for inequality with an integer rect.
     bool operator !=(const IntRect& rhs) const { return !(*this == rhs); }
 
+    /// Test for inequality with a rect.
+    bool operator !=(const Rect& rhs) const { return !(*this == rhs); }
+
     /// Test for inequality with an IntVector2.
     bool operator !=(const IntVector2& rhs) const { return !(*this == rhs); }
 
@@ -1020,6 +1045,9 @@ public:
     /// Return an integer rect or empty on type mismatch.
     const IntRect& GetIntRect() const { return type_ == VAR_INTRECT ? *reinterpret_cast<const IntRect*>(&value_) : IntRect::ZERO; }
 
+    /// Return an integer rect or empty on type mismatch.
+    const Rect& GetRect() const { return type_ == VAR_RECT ? *reinterpret_cast<const Rect*>(&value_) : Rect::ZERO; }
+
     /// Return an IntVector2 or empty on type mismatch.
     const IntVector2& GetIntVector2() const
     {
@@ -1155,6 +1183,8 @@ template <> inline VariantType GetVariantType<VariantMap>() { return VAR_VARIANT
 
 template <> inline VariantType GetVariantType<IntRect>() { return VAR_INTRECT; }
 
+template <> inline VariantType GetVariantType<Rect>() { return VAR_RECT; }
+
 template <> inline VariantType GetVariantType<IntVector2>() { return VAR_INTVECTOR2; }
 
 template <> inline VariantType GetVariantType<Matrix3>() { return VAR_MATRIX3; }
@@ -1190,6 +1220,8 @@ template <> URHO3D_API const String& Variant::Get<const String&>() const;
 
 template <> URHO3D_API const IntRect& Variant::Get<const IntRect&>() const;
 
+template <> URHO3D_API const Rect& Variant::Get<const Rect&>() const;
+
 template <> URHO3D_API const IntVector2& Variant::Get<const IntVector2&>() const;
 
 template <> URHO3D_API const PODVector<unsigned char>& Variant::Get<const PODVector<unsigned char>&>() const;
@@ -1228,6 +1260,8 @@ template <> URHO3D_API String Variant::Get<String>() const;
 
 template <> URHO3D_API IntRect Variant::Get<IntRect>() const;
 
+template <> URHO3D_API Rect Variant::Get<Rect>() const;
+
 template <> URHO3D_API IntVector2 Variant::Get<IntVector2>() const;
 
 template <> URHO3D_API PODVector<unsigned char> Variant::Get<PODVector<unsigned char> >() const;

+ 6 - 2
Source/Urho3D/LuaScript/pkgs/Urho2D/StaticSprite2D.pkg

@@ -1,7 +1,7 @@
 $#include "Urho2D/StaticSprite2D.h"
 
 class StaticSprite2D : public Drawable2D
-{    
+{
 public:
     void SetSprite(Sprite2D* sprite);
     void SetBlendMode(BlendMode mode);
@@ -23,7 +23,7 @@ public:
     bool GetUseHotSpot() const;
     const Vector2& GetHotSpot() const;
     Material* GetCustomMaterial() const;
-    
+
     tolua_property__get_set Sprite2D* sprite;
     tolua_property__get_set BlendMode blendMode;
     tolua_property__get_set bool flipX;
@@ -33,4 +33,8 @@ public:
     tolua_property__get_set bool useHotSpot;
     tolua_property__get_set Vector2 hotSpot;
     tolua_property__get_set Material* customMaterial;
+    tolua_property__get_set Rect drawRect;
+    tolua_property__get_set bool useDrawRect;
+    tolua_property__get_set Rect textureRect;
+    tolua_property__get_set bool useTextureRect;
 };

+ 80 - 21
Source/Urho3D/Urho2D/StaticSprite2D.cpp

@@ -46,7 +46,9 @@ StaticSprite2D::StaticSprite2D(Context* context) :
     flipY_(false),
     color_(Color::WHITE),
     useHotSpot_(false),
-    hotSpot_(0.5f, 0.5f)
+    hotSpot_(0.5f, 0.5f),
+    useDrawRect_(false),
+    useTextureRect_(false)
 {
     sourceBatches_.Resize(1);
     sourceBatches_[0].owner_ = this;
@@ -70,6 +72,10 @@ void StaticSprite2D::RegisterObject(Context* context)
     URHO3D_ACCESSOR_ATTRIBUTE("Color", GetColor, SetColor, Color, Color::WHITE, AM_DEFAULT);
     URHO3D_MIXED_ACCESSOR_ATTRIBUTE("Custom material", GetCustomMaterialAttr, SetCustomMaterialAttr, ResourceRef,
         ResourceRef(Material::GetTypeStatic()), AM_DEFAULT);
+    URHO3D_ACCESSOR_ATTRIBUTE("Draw Rectangle", GetDrawRect, SetDrawRect, Rect, Rect(), AM_DEFAULT);
+    URHO3D_ACCESSOR_ATTRIBUTE("Use Draw Rectangle", GetUseDrawRect, SetUseDrawRect, bool, false, AM_DEFAULT);
+    URHO3D_ACCESSOR_ATTRIBUTE("Texture Rectangle", GetTextureRect, SetTextureRect, Rect, Rect(), AM_DEFAULT);
+    URHO3D_ACCESSOR_ATTRIBUTE("Use Texture Rectangle", GetUseTextureRect, SetUseTextureRect, bool, false, AM_DEFAULT);
 }
 
 void StaticSprite2D::SetSprite(Sprite2D* sprite)
@@ -82,6 +88,28 @@ void StaticSprite2D::SetSprite(Sprite2D* sprite)
 
     sourceBatchesDirty_ = true;
     MarkNetworkUpdate();
+
+    UpdateDrawRect();
+}
+
+void StaticSprite2D::SetDrawRect(const Rect& rect)
+{
+    drawRect_ = rect;
+
+    if(useDrawRect_)
+    {
+        sourceBatchesDirty_ = true;
+    }
+}
+
+void StaticSprite2D::SetTextureRect(const Rect& rect)
+{
+    textureRect_ = rect;
+
+    if(useTextureRect_)
+    {
+        sourceBatchesDirty_ = true;
+    }
 }
 
 void StaticSprite2D::SetBlendMode(BlendMode blendMode)
@@ -145,6 +173,28 @@ void StaticSprite2D::SetUseHotSpot(bool useHotSpot)
     useHotSpot_ = useHotSpot;
     sourceBatchesDirty_ = true;
     MarkNetworkUpdate();
+    UpdateDrawRect();
+}
+
+void StaticSprite2D::SetUseDrawRect(bool useDrawRect)
+{
+    if (useDrawRect == useDrawRect_)
+        return;
+
+    useDrawRect_ = useDrawRect;
+    sourceBatchesDirty_ = true;
+    MarkNetworkUpdate();
+    UpdateDrawRect();
+}
+
+void StaticSprite2D::SetUseTextureRect(bool useTextureRect)
+{
+    if (useTextureRect == useTextureRect_)
+        return;
+
+    useTextureRect_ = useTextureRect;
+    sourceBatchesDirty_ = true;
+    MarkNetworkUpdate();
 }
 
 void StaticSprite2D::SetHotSpot(const Vector2& hotspot)
@@ -159,6 +209,8 @@ void StaticSprite2D::SetHotSpot(const Vector2& hotspot)
         sourceBatchesDirty_ = true;
         MarkNetworkUpdate();
     }
+
+    UpdateDrawRect();
 }
 
 void StaticSprite2D::SetCustomMaterial(Material* customMaterial)
@@ -235,22 +287,12 @@ void StaticSprite2D::UpdateSourceBatches()
     if (!sprite_)
         return;
 
-    Rect drawRect;
-    if (useHotSpot_)
+    if(!useTextureRect_)
     {
-        if (!sprite_->GetDrawRectangle(drawRect, hotSpot_, flipX_, flipY_))
-            return;
-    }
-    else
-    {
-        if (!sprite_->GetDrawRectangle(drawRect, flipX_, flipY_))
+        if (!sprite_->GetTextureRectangle(textureRect_, flipX_, flipY_))
             return;
     }
 
-    Rect textureRect;
-    if (!sprite_->GetTextureRectangle(textureRect, flipX_, flipY_))
-        return;
-
     /*
     V1---------V2
     |         / |
@@ -267,15 +309,15 @@ void StaticSprite2D::UpdateSourceBatches()
 
     // Convert to world space
     const Matrix3x4& worldTransform = node_->GetWorldTransform();
-    vertex0.position_ = worldTransform * Vector3(drawRect.min_.x_, drawRect.min_.y_, 0.0f);
-    vertex1.position_ = worldTransform * Vector3(drawRect.min_.x_, drawRect.max_.y_, 0.0f);
-    vertex2.position_ = worldTransform * Vector3(drawRect.max_.x_, drawRect.max_.y_, 0.0f);
-    vertex3.position_ = worldTransform * Vector3(drawRect.max_.x_, drawRect.min_.y_, 0.0f);
+    vertex0.position_ = worldTransform * Vector3(drawRect_.min_.x_, drawRect_.min_.y_, 0.0f);
+    vertex1.position_ = worldTransform * Vector3(drawRect_.min_.x_, drawRect_.max_.y_, 0.0f);
+    vertex2.position_ = worldTransform * Vector3(drawRect_.max_.x_, drawRect_.max_.y_, 0.0f);
+    vertex3.position_ = worldTransform * Vector3(drawRect_.max_.x_, drawRect_.min_.y_, 0.0f);
 
-    vertex0.uv_ = textureRect.min_;
-    vertex1.uv_ = Vector2(textureRect.min_.x_, textureRect.max_.y_);
-    vertex2.uv_ = textureRect.max_;
-    vertex3.uv_ = Vector2(textureRect.max_.x_, textureRect.min_.y_);
+    vertex0.uv_ = textureRect_.min_;
+    vertex1.uv_ = Vector2(textureRect_.min_.x_, textureRect_.max_.y_);
+    vertex2.uv_ = textureRect_.max_;
+    vertex3.uv_ = Vector2(textureRect_.max_.x_, textureRect_.min_.y_);
 
     vertex0.color_ = vertex1.color_ = vertex2.color_ = vertex3.color_ = color_.ToUInt();
 
@@ -300,4 +342,21 @@ void StaticSprite2D::UpdateMaterial()
     }
 }
 
+void StaticSprite2D::UpdateDrawRect()
+{
+    if (!useDrawRect_)
+    {
+        if (useHotSpot_)
+        {
+            if (!sprite_->GetDrawRectangle(drawRect_, hotSpot_, flipX_, flipY_))
+                return;
+        }
+        else
+        {
+            if (!sprite_->GetDrawRectangle(drawRect_, flipX_, flipY_))
+                return;
+        }
+    }
+}
+
 }

+ 30 - 0
Source/Urho3D/Urho2D/StaticSprite2D.h

@@ -44,6 +44,10 @@ public:
 
     /// Set sprite.
     void SetSprite(Sprite2D* sprite);
+    /// Set draw rectangle.
+    void SetDrawRect(const Rect &rect);
+    /// Set texture rectangle.
+    void SetTextureRect(const Rect &rect);
     /// Set blend mode.
     void SetBlendMode(BlendMode blendMode);
     /// Set flip.
@@ -58,6 +62,10 @@ public:
     void SetAlpha(float alpha);
     /// Set use hot spot.
     void SetUseHotSpot(bool useHotSpot);
+    /// Set use draw rectangle.
+    void SetUseDrawRect(bool useDrawRect);
+    /// Set use texture rectangle.
+    void SetUseTextureRect(bool useTextureRect);
     /// Set hot spot.
     void SetHotSpot(const Vector2& hotspot);
     /// Set custom material.
@@ -66,6 +74,12 @@ public:
     /// Return sprite.
     Sprite2D* GetSprite() const;
 
+    /// Return draw rect.
+    const Rect& GetDrawRect() const { return drawRect_; }
+
+    /// Return texture rect.
+    const Rect& GetTextureRect() const { return textureRect_; }
+
     /// Return blend mode.
     BlendMode GetBlendMode() const { return blendMode_; }
 
@@ -84,6 +98,12 @@ public:
     /// Return use hot spot.
     bool GetUseHotSpot() const { return useHotSpot_; }
 
+    /// Return use draw rect.
+    bool GetUseDrawRect() const { return useDrawRect_; }
+
+    /// Return use draw rect.
+    bool GetUseTextureRect() const { return useTextureRect_; }
+
     /// Return hot spot.
     const Vector2& GetHotSpot() const { return hotSpot_; }
 
@@ -108,6 +128,8 @@ protected:
     virtual void UpdateSourceBatches();
     /// Update material.
     void UpdateMaterial();
+    /// Update drawRect.
+    void UpdateDrawRect();
 
     /// Sprite.
     SharedPtr<Sprite2D> sprite_;
@@ -125,6 +147,14 @@ protected:
     Vector2 hotSpot_;
     /// Custom material.
     SharedPtr<Material> customMaterial_;
+    /// Draw rectangle.
+    Rect drawRect_;
+    /// Use drawRect.
+    bool useDrawRect_;
+    /// Texture rectangle.
+    Rect textureRect_;
+    /// Use textureRect.
+    bool useTextureRect_;
 };
 
 }