Browse Source

Merge remote-tracking branch 'ninjastone/uiscale'

Lasse Öörni 10 years ago
parent
commit
a0386ea75c

+ 3 - 0
Source/Urho3D/AngelScript/UIAPI.cpp

@@ -735,6 +735,9 @@ static void RegisterUI(asIScriptEngine* engine)
     engine->RegisterObjectMethod("UI", "bool get_useMutableGlyphs() const", asMETHOD(UI, GetUseMutableGlyphs), asCALL_THISCALL);
     engine->RegisterObjectMethod("UI", "bool get_useMutableGlyphs() const", asMETHOD(UI, GetUseMutableGlyphs), asCALL_THISCALL);
     engine->RegisterObjectMethod("UI", "void set_forceAutoHint(bool)", asMETHOD(UI, SetForceAutoHint), asCALL_THISCALL);
     engine->RegisterObjectMethod("UI", "void set_forceAutoHint(bool)", asMETHOD(UI, SetForceAutoHint), asCALL_THISCALL);
     engine->RegisterObjectMethod("UI", "bool get_forceAutoHint() const", asMETHOD(UI, GetForceAutoHint), asCALL_THISCALL);
     engine->RegisterObjectMethod("UI", "bool get_forceAutoHint() const", asMETHOD(UI, GetForceAutoHint), asCALL_THISCALL);
+    engine->RegisterObjectMethod("UI", "void SetWidth(float value)", asMETHOD(UI, SetWidth), asCALL_THISCALL);
+    engine->RegisterObjectMethod("UI", "void SetHeight(float value)", asMETHOD(UI, SetHeight), asCALL_THISCALL);
+    engine->RegisterObjectMethod("UI", "void SetScale(float value)", asMETHOD(UI, SetScale), asCALL_THISCALL);
     engine->RegisterGlobalFunction("UI@+ get_ui()", asFUNCTION(GetUI), asCALL_CDECL);
     engine->RegisterGlobalFunction("UI@+ get_ui()", asFUNCTION(GetUI), asCALL_CDECL);
 }
 }
 
 

+ 1 - 1
Source/Urho3D/Input/Input.cpp

@@ -2039,7 +2039,7 @@ void Input::HandleScreenJoystickTouch(StringHash eventType, VariantMap& eventDat
 
 
     // Only interested in events from screen joystick(s)
     // Only interested in events from screen joystick(s)
     TouchState& state = touches_[eventData[P_TOUCHID].GetInt()];
     TouchState& state = touches_[eventData[P_TOUCHID].GetInt()];
-    IntVector2 position(state.position_.x_, state.position_.y_);
+    IntVector2 position(int(state.position_.x_ / GetSubsystem<UI>()->GetScale()), int(state.position_.y_ / GetSubsystem<UI>()->GetScale()));
     UIElement* element = eventType == E_TOUCHBEGIN ? GetSubsystem<UI>()->GetElementAt(position) : state.touchedElement_;
     UIElement* element = eventType == E_TOUCHBEGIN ? GetSubsystem<UI>()->GetElementAt(position) : state.touchedElement_;
     if (!element)
     if (!element)
         return;
         return;

+ 50 - 8
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);
@@ -362,7 +363,10 @@ void UI::Update(float timeStep)
     for (unsigned i = 0; i < numTouches; ++i)
     for (unsigned i = 0; i < numTouches; ++i)
     {
     {
         TouchState* touch = input->GetTouch(i);
         TouchState* touch = input->GetTouch(i);
-        ProcessHover(touch->position_, TOUCHID_MASK(touch->touchID_), 0, 0);
+        IntVector2 touchPos = touch->position_;
+        touchPos.x_ = (int)(touchPos.x_ / uiScale_);
+        touchPos.y_ = (int)(touchPos.y_ / uiScale_);
+        ProcessHover(touchPos, TOUCHID_MASK(touch->touchID_), 0, 0);
     }
     }
 
 
     // End hovers that expired without refreshing
     // End hovers that expired without refreshing
@@ -404,7 +408,7 @@ void UI::RenderUpdate()
     batches_.Clear();
     batches_.Clear();
     vertexData_.Clear();
     vertexData_.Clear();
     const IntVector2& rootSize = rootElement_->GetSize();
     const IntVector2& rootSize = rootElement_->GetSize();
-    IntRect currentScissor = IntRect(0, 0, rootSize.x_, rootSize.y_);
+    IntRect currentScissor = IntRect(0, 0, (int)(rootSize.x_ / uiScale_), (int)(rootSize.y_ / uiScale_));
     if (rootElement_->IsVisible())
     if (rootElement_->IsVisible())
         GetBatches(rootElement_, currentScissor);
         GetBatches(rootElement_, currentScissor);
 
 
@@ -756,9 +760,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 +823,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 +989,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)
@@ -1506,6 +1519,8 @@ void UI::HandleTouchBegin(StringHash eventType, VariantMap& eventData)
     using namespace TouchBegin;
     using namespace TouchBegin;
 
 
     IntVector2 pos(eventData[P_X].GetInt(), eventData[P_Y].GetInt());
     IntVector2 pos(eventData[P_X].GetInt(), eventData[P_Y].GetInt());
+    pos.x_ = int(pos.x_ / uiScale_);
+    pos.y_ = int(pos.y_ / uiScale_);
     usingTouchInput_ = true;
     usingTouchInput_ = true;
 
 
     int touchId = TOUCHID_MASK(eventData[P_TOUCHID].GetInt());
     int touchId = TOUCHID_MASK(eventData[P_TOUCHID].GetInt());
@@ -1525,6 +1540,8 @@ void UI::HandleTouchEnd(StringHash eventType, VariantMap& eventData)
     using namespace TouchEnd;
     using namespace TouchEnd;
 
 
     IntVector2 pos(eventData[P_X].GetInt(), eventData[P_Y].GetInt());
     IntVector2 pos(eventData[P_X].GetInt(), eventData[P_Y].GetInt());
+    pos.x_ = int(pos.x_ / uiScale_);
+    pos.y_ = int(pos.y_ / uiScale_);
 
 
     // Get the touch index
     // Get the touch index
     int touchId = TOUCHID_MASK(eventData[P_TOUCHID].GetInt());
     int touchId = TOUCHID_MASK(eventData[P_TOUCHID].GetInt());
@@ -1554,6 +1571,10 @@ void UI::HandleTouchMove(StringHash eventType, VariantMap& eventData)
 
 
     IntVector2 pos(eventData[P_X].GetInt(), eventData[P_Y].GetInt());
     IntVector2 pos(eventData[P_X].GetInt(), eventData[P_Y].GetInt());
     IntVector2 deltaPos(eventData[P_DX].GetInt(), eventData[P_DY].GetInt());
     IntVector2 deltaPos(eventData[P_DX].GetInt(), eventData[P_DY].GetInt());
+    pos.x_ = int(pos.x_ / uiScale_);
+    pos.y_ = int(pos.y_ / uiScale_);
+    deltaPos.x_ = int(deltaPos.x_ / uiScale_);
+    deltaPos.y_ = int(deltaPos.y_ / uiScale_);
     usingTouchInput_ = true;
     usingTouchInput_ = true;
 
 
     int touchId = TOUCHID_MASK(eventData[P_TOUCHID].GetInt());
     int touchId = TOUCHID_MASK(eventData[P_TOUCHID].GetInt());
@@ -1676,6 +1697,9 @@ void UI::HandleDropFile(StringHash eventType, VariantMap& eventData)
     if (input->IsMouseVisible())
     if (input->IsMouseVisible())
     {
     {
         IntVector2 screenPos = input->GetMousePosition();
         IntVector2 screenPos = input->GetMousePosition();
+        screenPos.x_ = int(screenPos.x_ / uiScale_);
+        screenPos.y_ = int(screenPos.y_ / uiScale_);
+
         UIElement* element = GetElementAt(screenPos);
         UIElement* element = GetElementAt(screenPos);
 
 
         using namespace UIDropFile;
         using namespace UIDropFile;
@@ -1759,8 +1783,8 @@ IntVector2 UI::SumTouchPositions(UI::DragData* dragData, const IntVector2& oldSe
                 if (!ts)
                 if (!ts)
                     break;
                     break;
                 IntVector2 pos = ts->position_;
                 IntVector2 pos = ts->position_;
-                dragData->sumPos.x_ += pos.x_;
-                dragData->sumPos.y_ += pos.y_;
+                dragData->sumPos.x_ += (int)(pos.x_ / uiScale_);
+                dragData->sumPos.y_ += (int)(pos.y_ / uiScale_);
             }
             }
         }
         }
         sendPos.x_ = dragData->sumPos.x_ / dragData->numDragButtons;
         sendPos.x_ = dragData->sumPos.x_ / dragData->numDragButtons;
@@ -1769,6 +1793,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.