Просмотр исходного кода

rename unitSize2D to pixelsPerUnit.

aster2013 11 лет назад
Родитель
Сommit
c96d44fcb8

+ 2 - 2
Bin/Data/LuaScripts/24_Urho2DSprite.lua

@@ -44,8 +44,8 @@ function CreateScene()
     local camera = cameraNode:CreateComponent("Camera")
     camera.orthographic = true
 
-    local width = graphics.width / scene_.unitSize2D
-    local height = graphics.height / scene_.unitSize2D
+    local width = graphics.width / scene_.pixelsPerUnit
+    local height = graphics.height / scene_.pixelsPerUnit
     camera:SetOrthoSize(Vector2(width, height))
 
     local sprite = cache:GetResource("Sprite2D", "Urho2D/Aster.png")

+ 1 - 1
Bin/Data/LuaScripts/25_Urho2DParticle.lua

@@ -45,7 +45,7 @@ function CreateScene()
     cameraNode.position = Vector3(0.0, 0.0, -10.0)
     local camera = cameraNode:CreateComponent("Camera")
     camera.orthographic = true
-    camera:SetOrthoSize(Vector2(graphics.width, graphics.height) / scene_.unitSize2D)
+    camera:SetOrthoSize(Vector2(graphics.width, graphics.height) / scene_.pixelsPerUnit)
 
     local particleModel = cache:GetResource("ParticleModel2D", "Urho2D/LavaFlow.plist")
     if particleModel == nil then

+ 4 - 4
Bin/Data/Scripts/24_Urho2DSprite.as

@@ -47,8 +47,8 @@ void CreateScene()
     Camera@ camera = cameraNode.CreateComponent("Camera");
     camera.orthographic = true;
 
-    uint width = graphics.width / scene_.unitSize2D;
-    uint height = graphics.height / scene_.unitSize2D;
+    uint width = graphics.width / scene_.pixelsPerUnit;
+    uint height = graphics.height / scene_.pixelsPerUnit;
     camera.SetOrthoSize(Vector2(width, height));
 
     Sprite2D@ sprite = cache.GetResource("Sprite2D", "Urho2D/Aster.png");
@@ -166,8 +166,8 @@ void HandleUpdate(StringHash eventType, VariantMap& eventData)
     // Move the camera, scale movement with time step
     MoveCamera(timeStep);
 
-    float halfWidth = graphics.width * 0.5f / scene_.unitSize2D;
-    float halfHeight = graphics.height * 0.5f / scene_.unitSize2D;
+    float halfWidth = graphics.width * 0.5f / scene_.pixelsPerUnit;
+    float halfHeight = graphics.height * 0.5f / scene_.pixelsPerUnit;
 
     // Go through all sprites
     for (uint i = 0; i < spriteNodes.length; ++i)

+ 1 - 1
Bin/Data/Scripts/25_Urho2DParticle.as

@@ -49,7 +49,7 @@ void CreateScene()
 
     Camera@ camera = cameraNode.CreateComponent("Camera");
     camera.orthographic = true;
-    camera.SetOrthoSize(Vector2(graphics.width, graphics.height) / scene_.unitSize2D);
+    camera.SetOrthoSize(Vector2(graphics.width, graphics.height) / scene_.pixelsPerUnit);
 
     ParticleModel2D@ particleModel = cache.GetResource("ParticleModel2D", "Urho2D/LavaFlow.plist");
     if (particleModel is null)

+ 3 - 3
Source/Engine/LuaScript/pkgs/Scene/Scene.pkg

@@ -32,7 +32,7 @@ class Scene : public Node
     void SetUpdateEnabled(bool enable);
     void SetTimeScale(float scale);
     void SetElapsedTime(float time);
-    void SetUnitSize2D(float pixels);
+    void SetPixelsPerUnit(float pixelsPerUnit);
     void SetSmoothingConstant(float constant);
     void SetSnapThreshold(float threshold);
     
@@ -46,7 +46,7 @@ class Scene : public Node
     unsigned GetChecksum() const;
     float GetTimeScale() const;
     float GetElapsedTime() const;
-    float GetUnitSize2D() const;
+    float GetPixelsPerUnit() const;
     float GetSmoothingConstant() const;
     float GetSnapThreshold() const;
     const String GetVarName(ShortStringHash hash) const;
@@ -77,7 +77,7 @@ class Scene : public Node
     tolua_readonly tolua_property__get_set unsigned checksum;
     tolua_property__get_set float timeScale;
     tolua_property__get_set float elapsedTime;
-    tolua_property__get_set float unitSize2D;
+    tolua_property__get_set float pixelsPerUnit;
     tolua_property__get_set float smoothingConstant;
     tolua_property__get_set float snapThreshold;
     tolua_readonly tolua_property__is_set bool threadedUpdate;

+ 8 - 4
Source/Engine/Scene/Scene.cpp

@@ -61,7 +61,7 @@ Scene::Scene(Context* context) :
     checksum_(0),
     timeScale_(1.0f),
     elapsedTime_(0),
-    unitSize2D_(100.0f),
+    pixelsPerUnit_(100.0f),
     smoothingConstant_(DEFAULT_SMOOTHING_CONSTANT),
     snapThreshold_(DEFAULT_SNAP_THRESHOLD),
     updateEnabled_(true),
@@ -98,7 +98,7 @@ void Scene::RegisterObject(Context* context)
     ACCESSOR_ATTRIBUTE(Scene, VAR_FLOAT, "Smoothing Constant", GetSmoothingConstant, SetSmoothingConstant, float, DEFAULT_SMOOTHING_CONSTANT, AM_DEFAULT);
     ACCESSOR_ATTRIBUTE(Scene, VAR_FLOAT, "Snap Threshold", GetSnapThreshold, SetSnapThreshold, float, DEFAULT_SNAP_THRESHOLD, AM_DEFAULT);
     ACCESSOR_ATTRIBUTE(Scene, VAR_FLOAT, "Elapsed Time", GetElapsedTime, SetElapsedTime, float, 0.0f, AM_FILE);
-    ACCESSOR_ATTRIBUTE(Scene, VAR_FLOAT, "2D Unit Size", GetUnitSize2D, SetUnitSize2D, float, 1.0f, AM_DEFAULT);
+    ACCESSOR_ATTRIBUTE(Scene, VAR_FLOAT, "Pixel per Unit", GetPixelsPerUnit, SetPixelsPerUnit, float, 100.0f, AM_DEFAULT);
     ATTRIBUTE(Scene, VAR_INT, "Next Replicated Node ID", replicatedNodeID_, FIRST_REPLICATED_ID, AM_FILE | AM_NOEDIT);
     ATTRIBUTE(Scene, VAR_INT, "Next Replicated Component ID", replicatedComponentID_, FIRST_REPLICATED_ID, AM_FILE | AM_NOEDIT);
     ATTRIBUTE(Scene, VAR_INT, "Next Local Node ID", localNodeID_, FIRST_LOCAL_ID, AM_FILE | AM_NOEDIT);
@@ -435,9 +435,13 @@ void Scene::SetElapsedTime(float time)
     elapsedTime_ = time;
 }
 
-void Scene::SetUnitSize2D(float pixels)
+void Scene::SetPixelsPerUnit(float pixelsPerUnit)
 {
-    unitSize2D_ = pixels;
+    if (pixelsPerUnit_ == pixelsPerUnit)
+        return;
+
+    pixelsPerUnit_ = Max(pixelsPerUnit, 1.0f);
+
     MarkAllDrawable2DDirty(this);
     Node::MarkNetworkUpdate();
 }

+ 5 - 5
Source/Engine/Scene/Scene.h

@@ -103,8 +103,8 @@ public:
     void SetTimeScale(float scale);
     /// Set elapsed time in seconds. This can be used to prevent inaccuracy in the timer if the scene runs for a long time.
     void SetElapsedTime(float time);
-    /// Set 2D unit size in pixels. This represents the number of pixels would be used by Drawable2D class per coordinate unit.
-    void SetUnitSize2D(float pixels);
+    /// Set 2D pixels per unit. This represents the number of pixels would be used by Drawable2D class per coordinate unit.
+    void SetPixelsPerUnit(float pixelsPerUnit);
     /// Set network client motion smoothing constant.
     void SetSmoothingConstant(float constant);
     /// Set network client motion smoothing snap threshold.
@@ -138,8 +138,8 @@ public:
     float GetTimeScale() const { return timeScale_; }
     /// Return elapsed time in seconds.
     float GetElapsedTime() const { return elapsedTime_; }
-    /// Return 2D unit size in pixels.
-    float GetUnitSize2D() const { return unitSize2D_; }
+    /// Return 2D pixels per unit. This represents the number of pixels would be used by Drawable2D class per coordinate unit.
+    float GetPixelsPerUnit() const { return pixelsPerUnit_; }
     /// Return motion smoothing constant.
     float GetSmoothingConstant() const { return smoothingConstant_; }
     /// Return motion smoothing snap threshold.
@@ -243,7 +243,7 @@ private:
     /// Elapsed time accumulator.
     float elapsedTime_;
     /// 2D unit size representing the number of pixels per coordinate unit.
-    float unitSize2D_;
+    float pixelsPerUnit_;
     /// Motion smoothing constant.
     float smoothingConstant_;
     /// Motion smoothing snap threshold.

+ 2 - 2
Source/Engine/Script/SceneAPI.cpp

@@ -233,8 +233,8 @@ static void RegisterScene(asIScriptEngine* engine)
     engine->RegisterObjectMethod("Scene", "float get_timeScale() const", asMETHOD(Scene, GetTimeScale), asCALL_THISCALL);
     engine->RegisterObjectMethod("Scene", "void set_elapsedTime(float)", asMETHOD(Scene, SetElapsedTime), asCALL_THISCALL);
     engine->RegisterObjectMethod("Scene", "float get_elapsedTime() const", asMETHOD(Scene, GetElapsedTime), asCALL_THISCALL);
-    engine->RegisterObjectMethod("Scene", "void set_unitSize2D(float)", asMETHOD(Scene, SetUnitSize2D), asCALL_THISCALL);
-    engine->RegisterObjectMethod("Scene", "float get_unitSize2D() const", asMETHOD(Scene, GetUnitSize2D), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Scene", "void set_pixelsPerUnit(float)", asMETHOD(Scene, SetPixelsPerUnit), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Scene", "float get_pixelsPerUnit() const", asMETHOD(Scene, GetPixelsPerUnit), asCALL_THISCALL);
     engine->RegisterObjectMethod("Scene", "void set_smoothingConstant(float)", asMETHOD(Scene, SetSmoothingConstant), asCALL_THISCALL);
     engine->RegisterObjectMethod("Scene", "float get_smoothingConstant() const", asMETHOD(Scene, GetSmoothingConstant), asCALL_THISCALL);
     engine->RegisterObjectMethod("Scene", "void set_snapThreshold(float)", asMETHOD(Scene, SetSnapThreshold), asCALL_THISCALL);

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

@@ -87,7 +87,7 @@ void ParticleEmitter2D::Update(const FrameInfo& frame)
 
     float timeStep = frame.timeStep_;
     Vector3 worldPosition = GetNode()->GetWorldPosition();
-    float worldScale = GetNode()->GetWorldScale().x_ / GetScene()->GetUnitSize2D();
+    float worldScale = GetNode()->GetWorldScale().x_ / GetScene()->GetPixelsPerUnit();
 
     unsigned particleIndex = 0;
     while (particleIndex < numParticles_)

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

@@ -124,9 +124,9 @@ void StaticSprite2D::UpdateVertices()
     Vertex2D vertex2;
     Vertex2D vertex3;
 
-    float unitSize = scene->GetUnitSize2D();
-    float width = (float)rectangle_.Width() / unitSize;     // Compute width and height in pixels
-    float height = (float)rectangle_.Height() / unitSize;
+    float pixelsPerUnit = scene->GetPixelsPerUnit();
+    float width = (float)rectangle_.Width() / pixelsPerUnit;     // Compute width and height in pixels
+    float height = (float)rectangle_.Height() / pixelsPerUnit;
 
     const Vector2& hotSpot = sprite_->GetHotSpot();
     float hotSpotX = flipX_ ? (1.0f - hotSpot.x_) : hotSpot.x_;

+ 5 - 5
Source/Samples/24_Urho2DSprite/Urho2DSprite.cpp

@@ -84,8 +84,8 @@ void Urho2DSprite::CreateScene()
     camera->SetOrthographic(true);
 
     Graphics* graphics = GetSubsystem<Graphics>();
-    float width = (float)graphics->GetWidth() / scene_->GetUnitSize2D();
-    float height = (float)graphics->GetHeight() / scene_->GetUnitSize2D();
+    float width = (float)graphics->GetWidth() / scene_->GetPixelsPerUnit();
+    float height = (float)graphics->GetHeight() / scene_->GetPixelsPerUnit();
     camera->SetOrthoSize(Vector2(width, height));
 
     ResourceCache* cache = GetSubsystem<ResourceCache>();
@@ -111,7 +111,7 @@ void Urho2DSprite::CreateScene()
         staticSprite->SetSprite(sprite);
 
         // Set move speed
-        spriteNode->SetVar(VAR_MOVESPEED, Vector3(Random(-200.0f, 200.0f), Random(-200.0f, 200.0f), 0.0f));
+        spriteNode->SetVar(VAR_MOVESPEED, Vector3(Random(-2.0f, 2.0f), Random(-2.0f, 2.0f), 0.0f));
         // Set rotate speed
         spriteNode->SetVar(VAR_ROTATESPEED, Random(-90.0f, 90.0f));
 
@@ -212,8 +212,8 @@ void Urho2DSprite::HandleUpdate(StringHash eventType, VariantMap& eventData)
     MoveCamera(timeStep);
 
     Graphics* graphics = GetSubsystem<Graphics>();
-    float halfWidth = (float)graphics->GetWidth() * 0.5f;
-    float halfHeight = (float)graphics->GetHeight() * 0.5f;
+    float halfWidth = (float)graphics->GetWidth() * 0.5f / scene_->GetPixelsPerUnit();
+    float halfHeight = (float)graphics->GetHeight() * 0.5f / scene_->GetPixelsPerUnit();
 
     for (unsigned i = 0; i < spriteNodes_.Size(); ++i)
     {

+ 1 - 1
Source/Samples/25_Urho2DParticle/Urho2DParticle.cpp

@@ -82,7 +82,7 @@ void Urho2DParticle::CreateScene()
     camera->SetOrthographic(true);
 
     Graphics* graphics = GetSubsystem<Graphics>();
-    camera->SetOrthoSize(Vector2((float)graphics->GetWidth(), (float)graphics->GetHeight()) / scene_->GetUnitSize2D());
+    camera->SetOrthoSize(Vector2((float)graphics->GetWidth(), (float)graphics->GetHeight()) / scene_->GetPixelsPerUnit());
 
     ResourceCache* cache = GetSubsystem<ResourceCache>();
     ParticleModel2D* particleModel = cache->GetResource<ParticleModel2D>("Urho2D/LavaFlow.plist");