Browse Source

Remove z value, use layer order to sort Drawable2D.

aster2013 11 years ago
parent
commit
bea3ec1c76

+ 11 - 6
Source/Engine/LuaScript/pkgs/Urho2D/Drawable2D.pkg

@@ -7,18 +7,23 @@ const float PIXEL_SIZE;
 class Drawable2D : public Drawable
 {
 public:
+    void SetLayer(int layer);
+    void SetOrderInLayer(int orderInLayer);
     void SetSprite(Sprite2D* sprite);
-    void SetMaterial(Material* material);
     void SetBlendMode(BlendMode mode);
-    void SetZValue(float zValue);
+    void SetMaterial(Material* material);
 
+    int GetLayer() const;
+    int GetOrderInLayer() const;
     Sprite2D* GetSprite() const;
-    Material* GetMaterial() const;
+    Texture2D* GetTexture() const;
     BlendMode GetBlendMode() const;
-    float GetZValue() const;
+    Material* GetMaterial() const;
 
+    tolua_property__get_set int layer;
+    tolua_property__get_set int orderInLayer;
     tolua_property__get_set Sprite2D* sprite;
-    tolua_property__get_set Material* material;
+    tolua_readonly tolua_property__get_set Texture2D* texture;
     tolua_property__get_set BlendMode blendMode;
-    tolua_property__get_set float zValue;
+    tolua_property__get_set Material* material;
 };

+ 6 - 4
Source/Engine/Script/Urho2DAPI.cpp

@@ -84,14 +84,16 @@ template <class T> void RegisterDrawable2D(asIScriptEngine* engine, const char*
 {
     RegisterDrawable<T>(engine, className);
     RegisterSubclass<Drawable2D, T>(engine, "Drawable2D", className);
+    engine->RegisterObjectMethod(className, "void set_layer(int)", asMETHOD(T, SetLayer), asCALL_THISCALL);
+    engine->RegisterObjectMethod(className, "int get_layer() const", asMETHOD(T, GetLayer), asCALL_THISCALL);
+    engine->RegisterObjectMethod(className, "void set_orderInLayer(int)", asMETHOD(T, SetOrderInLayer), asCALL_THISCALL);
+    engine->RegisterObjectMethod(className, "int get_orderInLayer() const", asMETHOD(T, GetOrderInLayer), asCALL_THISCALL);
     engine->RegisterObjectMethod(className, "void set_sprite(Sprite2D@+)", asMETHOD(T, SetSprite), asCALL_THISCALL);
     engine->RegisterObjectMethod(className, "Sprite2D@+ get_sprite() const", asMETHOD(T, GetSprite), asCALL_THISCALL);
-    engine->RegisterObjectMethod(className, "void set_material(Material@+)", asMETHOD(T, SetMaterial), asCALL_THISCALL);
-    engine->RegisterObjectMethod(className, "Material@+ get_material() const", asMETHOD(T, GetMaterial), asCALL_THISCALL);
     engine->RegisterObjectMethod(className, "void set_blendMode(BlendMode)", asMETHOD(T, SetBlendMode), asCALL_THISCALL);
     engine->RegisterObjectMethod(className, "BlendMode get_blendMode() const", asMETHOD(T, GetBlendMode), asCALL_THISCALL);
-    engine->RegisterObjectMethod(className, "void set_zValue(float)", asMETHOD(T, SetZValue), asCALL_THISCALL);
-    engine->RegisterObjectMethod(className, "float get_zValue() const", asMETHOD(T, GetZValue), asCALL_THISCALL);
+    engine->RegisterObjectMethod(className, "void set_material(Material@+)", asMETHOD(T, SetMaterial), asCALL_THISCALL);
+    engine->RegisterObjectMethod(className, "Material@+ get_material() const", asMETHOD(T, GetMaterial), asCALL_THISCALL);
 }
 
 static void RegisterDrawable2D(asIScriptEngine* engine)

+ 37 - 23
Source/Engine/Urho2D/Drawable2D.cpp

@@ -47,7 +47,8 @@ extern const char* blendModeNames[];
 
 Drawable2D::Drawable2D(Context* context) :
     Drawable(context, DRAWABLE_GEOMETRY),
-    zValue_(0.0f),
+    layer_(0),
+    orderInLayer_(0),
     blendMode_(BLEND_ALPHA),
     vertexBuffer_(new VertexBuffer(context_)),
     verticesDirty_(true),
@@ -66,10 +67,11 @@ Drawable2D::~Drawable2D()
 
 void Drawable2D::RegisterObject(Context* context)
 {
-    ACCESSOR_ATTRIBUTE(Drawable2D, VAR_FLOAT, "Z Value", GetZValue, SetZValue, float, 0.0f, AM_DEFAULT);
+    ACCESSOR_ATTRIBUTE(Drawable2D, VAR_INT, "Layer", GetLayer, SetLayer, int, 0, AM_DEFAULT);
+    ACCESSOR_ATTRIBUTE(Drawable2D, VAR_INT, "Order in Layer", GetOrderInLayer, SetOrderInLayer, int, 0, AM_DEFAULT);
     ACCESSOR_ATTRIBUTE(Drawable2D, VAR_RESOURCEREF, "Sprite", GetSpriteAttr, SetSpriteAttr, ResourceRef, ResourceRef(Sprite2D::GetTypeStatic()), AM_DEFAULT);
-    ACCESSOR_ATTRIBUTE(Drawable2D, VAR_RESOURCEREF, "Material", GetMaterialAttr, SetMaterialAttr, ResourceRef, ResourceRef(Material::GetTypeStatic()), AM_DEFAULT);
     ENUM_ACCESSOR_ATTRIBUTE(Drawable2D, "Blend Mode", GetBlendMode, SetBlendModeAttr, BlendMode, blendModeNames, BLEND_ALPHA, AM_DEFAULT);
+    ACCESSOR_ATTRIBUTE(Drawable2D, VAR_RESOURCEREF, "Material", GetMaterialAttr, SetMaterialAttr, ResourceRef, ResourceRef(Material::GetTypeStatic()), AM_DEFAULT);
     COPY_BASE_ATTRIBUTES(Drawable2D, Drawable);
 }
 
@@ -138,24 +140,35 @@ UpdateGeometryType Drawable2D::GetUpdateGeometryType()
         return UPDATE_NONE;
 }
 
-void Drawable2D::SetSprite(Sprite2D* sprite)
+
+
+void Drawable2D::SetLayer(int layer)
 {
-    if (sprite == sprite_)
+    if (layer == layer_)
         return;
 
-    sprite_ = sprite;
-    MarkDirty();
-    UpdateMaterial();
+    layer_ = layer;
+
     MarkNetworkUpdate();
 }
 
-void Drawable2D::SetMaterial(Material* material)
+void Drawable2D::SetOrderInLayer(int orderInLayer)
 {
-    if (material == material_)
+    if (orderInLayer == orderInLayer_)
         return;
 
-    material_ = material;
+    orderInLayer_ = orderInLayer;
+
+    MarkNetworkUpdate();
+}
 
+void Drawable2D::SetSprite(Sprite2D* sprite)
+{
+    if (sprite == sprite_)
+        return;
+
+    sprite_ = sprite;
+    MarkDirty();
     UpdateMaterial();
     MarkNetworkUpdate();
 }
@@ -170,13 +183,14 @@ void Drawable2D::SetBlendMode(BlendMode mode)
     MarkNetworkUpdate();
 }
 
-void Drawable2D::SetZValue(float zValue)
+void Drawable2D::SetMaterial(Material* material)
 {
-    if (zValue == zValue_)
+    if (material == material_)
         return;
 
-    zValue_ = zValue;
-    MarkDirty();
+    material_ = material;
+
+    UpdateMaterial();
     MarkNetworkUpdate();
 }
 
@@ -238,6 +252,14 @@ ResourceRef Drawable2D::GetSpriteAttr() const
     return ResourceRef(spriteSheet->GetType(), spriteSheet->GetName() + "@" + sprite_->GetName());
 }
 
+void Drawable2D::SetBlendModeAttr(BlendMode mode)
+{
+    // Delay applying material update
+    materialUpdatePending_ = true;
+
+    SetBlendMode(mode);
+}
+
 void Drawable2D::SetMaterialAttr(ResourceRef value)
 {
     // Delay applying material update
@@ -252,14 +274,6 @@ ResourceRef Drawable2D::GetMaterialAttr() const
     return GetResourceRef(material_, Material::GetTypeStatic());
 }
 
-void Drawable2D::SetBlendModeAttr(BlendMode mode)
-{
-    // Delay applying material update
-    materialUpdatePending_ = true;
-
-    SetBlendMode(mode);
-}
-
 void Drawable2D::OnWorldBoundingBoxUpdate()
 {
     if (verticesDirty_)

+ 23 - 12
Source/Engine/Urho2D/Drawable2D.h

@@ -55,25 +55,29 @@ public:
     /// Return whether a geometry update is necessary, and if it can happen in a worker thread.
     virtual UpdateGeometryType GetUpdateGeometryType();
 
+    /// Set layer.
+    void SetLayer(int layer);
+    /// Set order in layer.
+    void SetOrderInLayer(int orderInLayer);
     /// Set sprite.
     void SetSprite(Sprite2D* sprite);
-    /// Set material.
-    void SetMaterial(Material* material);
     /// Set blend mode.
     void SetBlendMode(BlendMode mode);
-    /// Set Z value.
-    void SetZValue(float zValue);
+    /// Set material.
+    void SetMaterial(Material* material);
 
+    /// Return layer.
+    int GetLayer() const { return layer_; }
+    /// Return order in layer.
+    int GetOrderInLayer() const { return orderInLayer_; }
     /// Return sprite.
     Sprite2D* GetSprite() const { return sprite_; }
     /// Return texture.
     Texture2D* GetTexture() const { return sprite_ ? sprite_->GetTexture() : 0; }
-    /// Return material.
-    Material* GetMaterial() const;
     /// Return blend mode.
     BlendMode GetBlendMode() const { return blendMode_; }
-    /// Return Z value.
-    float GetZValue() const { return zValue_; }
+    /// Return material.
+    Material* GetMaterial() const;
 
     /// Return all vertices.
     const Vector<Vertex2D>& GetVertices() const { return vertices_; }
@@ -84,12 +88,12 @@ public:
     void SetSpriteAttr(ResourceRef value);
     /// Return sprite attribute.
     ResourceRef GetSpriteAttr() const;
+    /// Set blend mode attribute.
+    void SetBlendModeAttr(BlendMode mode);
     /// Set material attribute.
     void SetMaterialAttr(ResourceRef value);
     /// Return material attribute.
     ResourceRef GetMaterialAttr() const;
-    /// Set blend mode attribute.
-    void SetBlendModeAttr(BlendMode mode);
 
 protected:
     /// Recalculate the world-space bounding box.
@@ -99,8 +103,10 @@ protected:
     /// Update the material's properties (blend mode and texture).
     void UpdateMaterial();
 
-    /// Z value.
-    float zValue_;
+    /// Layer.
+    int layer_;
+    /// Order in layer.
+    int orderInLayer_;
     /// Sprite.
     SharedPtr<Sprite2D> sprite_;
     /// Material. If null, use a default material. If non-null, use a clone of this for updating the diffuse texture.
@@ -122,4 +128,9 @@ protected:
     bool materialUpdatePending_;
 };
 
+inline bool CompareDrawable2Ds(Drawable2D* lhs, Drawable2D* rhs)
+{
+    return lhs->GetLayer() < rhs->GetLayer() || lhs->GetOrderInLayer() < rhs->GetOrderInLayer() || (int)lhs < (int)rhs;
+}
+
 }

+ 4 - 4
Source/Engine/Urho2D/ParticleEmitter2D.cpp

@@ -232,10 +232,10 @@ void ParticleEmitter2D::UpdateVertices()
         float add = (c + s) * p.size_ * 0.5f;
         float sub = (c - s) * p.size_ * 0.5f;
 
-        vertex0.position_ = Vector3(p.position_.x_ - sub, p.position_.y_ - add, zValue_);
-        vertex1.position_ = Vector3(p.position_.x_ - add, p.position_.y_ + sub, zValue_);
-        vertex2.position_ = Vector3(p.position_.x_ + sub, p.position_.y_ + add, zValue_);
-        vertex3.position_ = Vector3(p.position_.x_ + add, p.position_.y_ - sub, zValue_);
+        vertex0.position_ = Vector3(p.position_.x_ - sub, p.position_.y_ - add, 0.0f);
+        vertex1.position_ = Vector3(p.position_.x_ - add, p.position_.y_ + sub, 0.0f);
+        vertex2.position_ = Vector3(p.position_.x_ + sub, p.position_.y_ + add, 0.0f);
+        vertex3.position_ = Vector3(p.position_.x_ + add, p.position_.y_ - sub, 0.0f);
         
         vertex0.color_ = vertex1.color_ = vertex2.color_  = vertex3.color_ = p.color_.ToUInt();
 

+ 4 - 4
Source/Engine/Urho2D/StaticSprite2D.cpp

@@ -128,10 +128,10 @@ void StaticSprite2D::UpdateVertices()
     float rightX = width * (1.0f - hotSpotX);
     float bottomY = -height * hotSpotY;
     float topY = height * (1.0f - hotSpotY);
-    vertex0.position_ = Vector3(leftX, bottomY, zValue_);
-    vertex1.position_ = Vector3(leftX, topY, zValue_);
-    vertex2.position_ = Vector3(rightX, topY, zValue_);
-    vertex3.position_ = Vector3(rightX, bottomY, zValue_);
+    vertex0.position_ = Vector3(leftX, bottomY, 0.0f);
+    vertex1.position_ = Vector3(leftX, topY, 0.0f);
+    vertex2.position_ = Vector3(rightX, topY, 0.0f);
+    vertex3.position_ = Vector3(rightX, bottomY, 0.0f);
 
     float invTexW = 1.0f / (float)texture->GetWidth();
     float invTexH = 1.0f / (float)texture->GetHeight();