Selaa lähdekoodia

Added rendering scale to UI

ninjastone 10 vuotta sitten
vanhempi
sitoutus
4daf7fd3f0
2 muutettua tiedostoa jossa 46 lisäystä ja 4 poistoa
  1. 32 4
      Source/Urho3D/UI/UI.cpp
  2. 14 0
      Source/Urho3D/UI/UI.h

+ 32 - 4
Source/Urho3D/UI/UI.cpp

@@ -108,7 +108,8 @@ UI::UI(Context* context) :
     uiRendered_(false),
     uiRendered_(false),
     nonModalBatchSize_(0),
     nonModalBatchSize_(0),
     dragElementsCount_(0),
     dragElementsCount_(0),
-    dragConfirmedCount_(0)
+    dragConfirmedCount_(0),
+    uiScale_(1.0f)
 {
 {
     rootElement_->SetTraversalMode(TM_DEPTH_FIRST);
     rootElement_->SetTraversalMode(TM_DEPTH_FIRST);
     rootModalElement_->SetTraversalMode(TM_DEPTH_FIRST);
     rootModalElement_->SetTraversalMode(TM_DEPTH_FIRST);
@@ -756,9 +757,9 @@ void UI::Render(bool resetRenderTargets, VertexBuffer* buffer, const PODVector<U
     Vector2 offset(-1.0f, 1.0f);
     Vector2 offset(-1.0f, 1.0f);
 
 
     Matrix4 projection(Matrix4::IDENTITY);
     Matrix4 projection(Matrix4::IDENTITY);
-    projection.m00_ = scale.x_;
+    projection.m00_ = scale.x_ * uiScale_;
     projection.m03_ = offset.x_;
     projection.m03_ = offset.x_;
-    projection.m11_ = scale.y_;
+    projection.m11_ = scale.y_ * uiScale_;
     projection.m13_ = offset.y_;
     projection.m13_ = offset.y_;
     projection.m22_ = 1.0f;
     projection.m22_ = 1.0f;
     projection.m23_ = 0.0f;
     projection.m23_ = 0.0f;
@@ -819,8 +820,14 @@ void UI::Render(bool resetRenderTargets, VertexBuffer* buffer, const PODVector<U
         if (graphics_->NeedParameterUpdate(SP_MATERIAL, this))
         if (graphics_->NeedParameterUpdate(SP_MATERIAL, this))
             graphics_->SetShaderParameter(PSP_MATDIFFCOLOR, Color(1.0f, 1.0f, 1.0f, 1.0f));
             graphics_->SetShaderParameter(PSP_MATDIFFCOLOR, Color(1.0f, 1.0f, 1.0f, 1.0f));
 
 
+        IntRect scissor = batch.scissor_;
+        scissor.left_ = (int)(scissor.left_ * uiScale_);
+        scissor.top_ = (int)(scissor.top_ * uiScale_);
+        scissor.right_ = (int)(scissor.right_ * uiScale_);
+        scissor.bottom_ = (int)(scissor.bottom_ * uiScale_);
+
         graphics_->SetBlendMode(batch.blendMode_);
         graphics_->SetBlendMode(batch.blendMode_);
-        graphics_->SetScissorTest(true, batch.scissor_);
+        graphics_->SetScissorTest(true, scissor);
         graphics_->SetTexture(0, batch.texture_);
         graphics_->SetTexture(0, batch.texture_);
         graphics_->Draw(TRIANGLE_LIST, batch.vertexStart_ / UI_VERTEX_SIZE,
         graphics_->Draw(TRIANGLE_LIST, batch.vertexStart_ / UI_VERTEX_SIZE,
             (batch.vertexEnd_ - batch.vertexStart_) / UI_VERTEX_SIZE);
             (batch.vertexEnd_ - batch.vertexStart_) / UI_VERTEX_SIZE);
@@ -979,6 +986,9 @@ void UI::GetCursorPositionAndVisible(IntVector2& pos, bool& visible)
         if (!visible && cursor_)
         if (!visible && cursor_)
             pos = cursor_->GetPosition();
             pos = cursor_->GetPosition();
     }
     }
+
+    pos.x_ = (int)(pos.x_ / uiScale_);
+    pos.y_ = (int)(pos.y_ / uiScale_);
 }
 }
 
 
 void UI::SetCursorShape(CursorShape shape)
 void UI::SetCursorShape(CursorShape shape)
@@ -1767,6 +1777,24 @@ IntVector2 UI::SumTouchPositions(UI::DragData* dragData, const IntVector2& oldSe
     return sendPos;
     return sendPos;
 }
 }
 
 
+void UI::SetWidth(float size)
+{
+    Graphics* g = GetSubsystem<Graphics>();
+    if (g)
+    {
+        uiScale_ = g->GetWidth() / size;
+    }
+}
+
+void UI::SetHeight(float size)
+{
+    Graphics* g = GetSubsystem<Graphics>();
+    if (g)
+    {
+        uiScale_ = g->GetHeight() / size;
+    }
+}
+
 void RegisterUILibrary(Context* context)
 void RegisterUILibrary(Context* context)
 {
 {
     Font::RegisterObject(context);
     Font::RegisterObject(context);

+ 14 - 0
Source/Urho3D/UI/UI.h

@@ -181,6 +181,18 @@ public:
         IntVector2 dragBeginSumPos;
         IntVector2 dragBeginSumPos;
     };
     };
 
 
+    /// Return current UI scale.
+    float GetScale() const { return uiScale_; }
+
+    /// Set current UI scale.
+    void SetScale(float scale) { uiScale_ = scale; }
+
+    /// Set UI width.
+    void SetWidth(float size);
+
+    /// Set UI height.
+    void SetHeight(float size);
+
 private:
 private:
     /// Initialize when screen mode initially set.
     /// Initialize when screen mode initially set.
     void Initialize();
     void Initialize();
@@ -331,6 +343,8 @@ private:
     HashMap<WeakPtr<UIElement>, int> touchDragElements_;
     HashMap<WeakPtr<UIElement>, int> touchDragElements_;
     /// Confirmed drag elements cache.
     /// Confirmed drag elements cache.
     Vector<UIElement*> dragElementsConfirmed_;
     Vector<UIElement*> dragElementsConfirmed_;
+    /// Current scale of UI
+    float uiScale_;
 };
 };
 
 
 /// Register UI library objects.
 /// Register UI library objects.