#337 CalculatedSize/Location 4.x-dev

Thu, 28 Oct 2021 06:53:24 +0300

author
cemkalyoncu
date
Thu, 28 Oct 2021 06:53:24 +0300
branch
4.x-dev
changeset 1746
37a08db9a582
parent 1745
c417bc41d46c
child 1747
2f9ad833617f

#337 CalculatedSize/Location

Source/Gorgon/UI/LayerAdapter.cpp file | annotate | diff | comparison | revisions
Source/Gorgon/UI/Widget.cpp file | annotate | diff | comparison | revisions
Source/Gorgon/UI/Widget.h file | annotate | diff | comparison | revisions
Source/Gorgon/UI/Window.cpp file | annotate | diff | comparison | revisions
--- a/Source/Gorgon/UI/LayerAdapter.cpp	Thu Oct 28 06:21:19 2021 +0300
+++ b/Source/Gorgon/UI/LayerAdapter.cpp	Thu Oct 28 06:53:24 2021 +0300
@@ -53,7 +53,7 @@
             s = base->GetCalculatedSize();
         }
 
-        base->Resize(Convert(size, s, GetUnitSize(), GetSpacing()));
+        base->Resize(Convert(size, s, GetUnitSize(), GetSpacing(), Widgets::Registry::Active().GetEmSize()));
 
         return true;
     }
--- a/Source/Gorgon/UI/Widget.cpp	Thu Oct 28 06:21:19 2021 +0300
+++ b/Source/Gorgon/UI/Widget.cpp	Thu Oct 28 06:53:24 2021 +0300
@@ -150,7 +150,7 @@
             sz = GetCurrentSize();
         }
 
-        auto l= Convert(
+        auto l= UI::Convert(
             location, sz,
             unitsize, spacing,
             Widgets::Registry::Active().GetEmSize()
@@ -161,7 +161,7 @@
             llocation = l;
         }
 
-        auto s = Convert(
+        auto s = UI::Convert(
             size, sz,
             unitsize, spacing,
             Widgets::Registry::Active().GetEmSize()
@@ -199,6 +199,165 @@
         calculatebounds();
     }
 
+    int Widget::Convert(const UnitDimension &val, bool vertical, bool size) const {
+        int unitsize = 0, spacing = 0;
+        Geometry::Size sz;
+        if(HasParent()) {
+            unitsize = GetParent().GetUnitSize();
+            spacing = GetParent().GetSpacing();
+            sz = GetParent().GetInteriorSize() + Geometry::Size(spacing, spacing);
+        }
+        else {
+            unitsize = Widgets::Registry::Active().GetUnitSize();
+            spacing  = Widgets::Registry::Active().GetSpacing();
+            sz = GetCurrentSize();
+        }
+
+        return val(vertical ? sz.Height : sz.Width, unitsize, spacing, size, Widgets::Registry::Active().GetEmSize());
+    }
+
+    Geometry::Size Widget::Convert(const UnitSize &val) const {
+        int unitsize = 0, spacing = 0;
+        Geometry::Size sz;
+        if(HasParent()) {
+            unitsize = GetParent().GetUnitSize();
+            spacing = GetParent().GetSpacing();
+            sz = GetParent().GetInteriorSize() + Geometry::Size(spacing, spacing);
+        }
+        else {
+            unitsize = Widgets::Registry::Active().GetUnitSize();
+            spacing  = Widgets::Registry::Active().GetSpacing();
+            sz = GetCurrentSize();
+        }
+
+        return UI::Convert(val, sz, unitsize, spacing, Widgets::Registry::Active().GetEmSize());
+    }
+
+    Geometry::Point Widget::Convert(const UnitPoint &val) const {
+        int unitsize = 0, spacing = 0;
+        Geometry::Size sz;
+        if(HasParent()) {
+            unitsize = GetParent().GetUnitSize();
+            spacing = GetParent().GetSpacing();
+            sz = GetParent().GetInteriorSize() + Geometry::Size(spacing, spacing);
+        }
+        else {
+            unitsize = Widgets::Registry::Active().GetUnitSize();
+            spacing  = Widgets::Registry::Active().GetSpacing();
+            sz = GetCurrentSize();
+        }
+
+        return UI::Convert(val, sz, unitsize, spacing, Widgets::Registry::Active().GetEmSize());
+    }
+
+    UnitDimension Widget::Convert(Dimension::Unit target, const UnitDimension &val, bool vertical, bool size) const {
+        int unitsize = 0, spacing = 0;
+        Geometry::Size sz;
+        if(HasParent()) {
+            unitsize = GetParent().GetUnitSize();
+            spacing = GetParent().GetSpacing();
+            sz = GetParent().GetInteriorSize() + Geometry::Size(spacing, spacing);
+        }
+        else {
+            unitsize = Widgets::Registry::Active().GetUnitSize();
+            spacing  = Widgets::Registry::Active().GetSpacing();
+            sz = GetCurrentSize();
+        }
+
+        int em = Widgets::Registry::Active().GetEmSize();
+
+        int px = val(vertical ? sz.Height : sz.Width, unitsize, spacing, size, em);
+
+        switch(target) {
+        case Dimension::Pixel:
+        default:
+            return Pixels(px);
+        case Dimension::MilliPixel:
+            return {(float)px, Dimension::MilliPixel};
+        case Dimension::Percent:
+        case Dimension::BasisPoint:
+            return {(float)px/(vertical ? sz.Height : sz.Width), target};
+        case Dimension::UnitSize:
+        case Dimension::MilliUnitSize:
+            if(size)
+                return {(float)(px+spacing)/(unitsize+spacing), target};
+            else
+                return {(float)px/(unitsize+spacing), target};
+        case Dimension::EM:
+            return {(float)px/em, Dimension::EM};
+        }
+    }
+
+    UnitSize Widget::Convert(Dimension::Unit target, const UnitSize &val) const {
+        int unitsize = 0, spacing = 0;
+        Geometry::Size sz;
+        if(HasParent()) {
+            unitsize = GetParent().GetUnitSize();
+            spacing = GetParent().GetSpacing();
+            sz = GetParent().GetInteriorSize() + Geometry::Size(spacing, spacing);
+        }
+        else {
+            unitsize = Widgets::Registry::Active().GetUnitSize();
+            spacing  = Widgets::Registry::Active().GetSpacing();
+            sz = GetCurrentSize();
+        }
+
+        int em = Widgets::Registry::Active().GetEmSize();
+
+        auto px = UI::Convert(val, sz, unitsize, spacing, em);
+
+        switch(target) {
+        case Dimension::Pixel:
+        default:
+            return Pixels(px);
+        case Dimension::MilliPixel:
+            return {{(float)px.Width, target}, {(float)px.Height, target}};;
+        case Dimension::Percent:
+        case Dimension::BasisPoint:
+            return {{(float)px.Width/sz.Width, target}, {(float)px.Height/sz.Height, target}};;
+        case Dimension::UnitSize:
+        case Dimension::MilliUnitSize:
+            return {{(float)(px.Width+spacing)/(unitsize+spacing), target}, {(float)(px.Height+spacing)/(unitsize+spacing), target}};
+        case Dimension::EM:
+            return {{(float)px.Width/em, Dimension::EM}, {(float)px.Height/em, Dimension::EM}};;
+        }
+    }
+
+    UnitPoint Widget::Convert(Dimension::Unit target, const UnitPoint &val) const {
+        int unitsize = 0, spacing = 0;
+        Geometry::Size sz;
+        if(HasParent()) {
+            unitsize = GetParent().GetUnitSize();
+            spacing = GetParent().GetSpacing();
+            sz = GetParent().GetInteriorSize() + Geometry::Size(spacing, spacing);
+        }
+        else {
+            unitsize = Widgets::Registry::Active().GetUnitSize();
+            spacing  = Widgets::Registry::Active().GetSpacing();
+            sz = GetCurrentSize();
+        }
+
+        int em = Widgets::Registry::Active().GetEmSize();
+
+
+        auto px = UI::Convert(val, sz, unitsize, spacing, em);
+
+        switch(target) {
+        case Dimension::Pixel:
+        default:
+            return Pixels(px);
+        case Dimension::MilliPixel:
+            return {{(float)px.X, target}, {(float)px.Y, target}};;
+        case Dimension::Percent:
+        case Dimension::BasisPoint:
+            return {{(float)px.X/sz.Width, target}, {(float)px.Y/sz.Height, target}};;
+        case Dimension::UnitSize:
+        case Dimension::MilliUnitSize:
+            return {{(float)px.X/(unitsize+spacing), target}, {(float)px.Y/(unitsize+spacing), target}};
+        case Dimension::EM:
+            return {{(float)px.X/em, Dimension::EM}, {(float)px.Y/em, Dimension::EM}};;
+        }
+    }
 
     void Widget::parentboundschanged() {
         calculatebounds();
--- a/Source/Gorgon/UI/Widget.h	Thu Oct 28 06:21:19 2021 +0300
+++ b/Source/Gorgon/UI/Widget.h	Thu Oct 28 06:53:24 2021 +0300
@@ -49,6 +49,11 @@
         /// be different than location that is set.
         virtual Geometry::Point GetCurrentLocation() const = 0;
 
+        /// Returns the current location converted to requested units
+        UnitPoint GetCalculatedLocation(Dimension::Unit units = Dimension::MilliUnitSize) const {
+            return Convert(units, GetCurrentLocation());
+        }
+
         /// Changes the size of the widget.
         void Resize(UnitDimension w, UnitDimension h) { Resize({w, h}); };
 
@@ -64,6 +69,10 @@
         /// different than the size that is set.
         virtual Geometry::Size GetCurrentSize() const = 0;
         
+        /// Returns the current size converted to requested units
+        UnitSize GetCalculatedSize(Dimension::Unit units = Dimension::MilliUnitSize) const {
+            return Convert(units, GetCurrentSize());
+        }
         /// Returns the bounds of the widget in pixels.
         Geometry::Bounds GetBounds() const { return {GetCurrentLocation(), GetCurrentSize()}; }
         
@@ -200,6 +209,24 @@
             return tooltip;
         }
 
+        /// Converts the given size in units to pixels
+        int Convert(const UnitDimension &val, bool vertical, bool size = false) const;
+
+        /// Converts the given size in units to pixels
+        Geometry::Size Convert(const UnitSize &size) const;
+
+        /// Converts the given location in units to pixels
+        Geometry::Point Convert(const UnitPoint &location) const;
+
+        /// Converts the given size in units to requested units.
+        UnitDimension Convert(Dimension::Unit target, const UnitDimension &val, bool vertical, bool size = false) const;
+
+        /// Converts the given size in units to requested units.
+        UnitSize Convert(Dimension::Unit target, const UnitSize &size) const;
+
+        /// Converts the given location in units to requested units.
+        UnitPoint Convert(Dimension::Unit target, const UnitPoint &location) const;
+
         /// This event will be fired when the widget receives or looses focus.
         Event<Widget> FocusEvent = Event<Widget>{*this};
 
--- a/Source/Gorgon/UI/Window.cpp	Thu Oct 28 06:21:19 2021 +0300
+++ b/Source/Gorgon/UI/Window.cpp	Thu Oct 28 06:53:24 2021 +0300
@@ -309,7 +309,7 @@
         if(mon)
             s = mon->GetSize();
 
-        auto sz = Convert(size, s, GetUnitSize(), GetSpacing());
+        auto sz = Convert(size, s, GetUnitSize(), GetSpacing(), Widgets::Registry::Active().GetEmSize());
 
         Gorgon::Window::Resize(sz);
 

mercurial