* RadioButtons now allow columns 4.x-dev

Sun, 04 Oct 2020 11:36:11 +0300

author
cemkalyoncu
date
Sun, 04 Oct 2020 11:36:11 +0300
branch
4.x-dev
changeset 1456
f08cebbb3c3f
parent 1455
1cd168e97418
child 1457
d468ecfe32e3

* RadioButtons now allow columns

Source/Gorgon/UI/RadioControl.h file | annotate | diff | comparison | revisions
Source/Gorgon/Widgets/RadioButtons.h file | annotate | diff | comparison | revisions
Testing/Source/Manual/UI_Generate.cpp file | annotate | diff | comparison | revisions
--- a/Source/Gorgon/UI/RadioControl.h	Sun Oct 04 10:24:35 2020 +0300
+++ b/Source/Gorgon/UI/RadioControl.h	Sun Oct 04 11:36:11 2020 +0300
@@ -176,6 +176,7 @@
         void PlaceIn(C_ &container, Geometry::Point start, int spacing) {
             auto loc = start;
             
+            int col = 0;
             for(auto p : elements) {
                 auto w = dynamic_cast<WidgetBase *>(&p.second);
                 
@@ -186,26 +187,48 @@
                 
                 w->Move(loc);
                 
-                loc.Y += w->GetSize().Height + spacing;
+                col++;
+                if(col%columns == 0) {
+                    loc.X = start.X;
+                    loc.Y += w->GetSize().Height + spacing;
+                    col = 0;
+                }
+                else {
+                    loc.X += w->GetSize().Width + spacing;
+                }
             }
         }
         
+        /// For iteration
         auto begin() {
             return elements.begin();
         }
         
+        /// For iteration
         auto end() {
             return elements.begin();
         }
         
+        /// For iteration
         auto begin() const {
             return elements.begin();
         }
         
+        /// For iteration
         auto end() const {
             return elements.begin();
         }
         
+        /// Changes the number of columns when placing the widgets
+        void SetColumns(int value) {
+            columns = value;
+        }
+        
+        /// Returns the number of columns when placing the widgets
+        int GetColumns() const {
+            return columns;
+        }
+        
         Event<RadioControl, T_> ChangedEvent;
         
     protected:
@@ -238,6 +261,8 @@
         bool own = false;
         
         T_ current = T_{};
+        
+        int columns = 1;
     };
     
 } }
--- a/Source/Gorgon/Widgets/RadioButtons.h	Sun Oct 04 10:24:35 2020 +0300
+++ b/Source/Gorgon/Widgets/RadioButtons.h	Sun Oct 04 11:36:11 2020 +0300
@@ -22,25 +22,22 @@
     public:
         explicit RadioButtons(const UI::Template &temp) : temp(temp) { 
             spacing = temp.GetSpacing();
+            SetWidth(temp.GetWidth());
             this->own = true;
         }
         
-        explicit RadioButtons(Registry::TemplateType type = Registry::Radio_Regular) : temp(Registry::Active()[type]) {  
-            spacing = temp.GetSpacing();
-            this->own = true; 
+        explicit RadioButtons(Registry::TemplateType type = Registry::Radio_Regular) : RadioButtons(Registry::Active()[type]) {  
         }
-
         
         /// Radio buttons height is automatically adjusted. Only width will be used.
         virtual void Resize(const Geometry::Size &size) override {
             for(auto p : this->elements) {
-                p.second.SetWidth(size.Width);
+                p.second.SetWidth((GetWidth() - spacing * (GetColumns() - 1)) / GetColumns());
             }
             
             contents.SetWidth(size.Width);
         }
 
-
         virtual bool Activate() override {
             return false;
         }
@@ -51,17 +48,7 @@
 
             spacing = value;
             
-            int total = spacing * (this->elements.GetSize() - 1);
-            
-            for(auto p : this->elements) {
-                total += p.second.GetHeight();
-            }
-            
-            if(total < 0) total = 0;
-            
-            SetHeight(total);
-            
-            this->PlaceIn((UI::WidgetContainer&)*this, {0, 0}, spacing);
+            rearrange();
         }
 
         void Add(const T_ value) {
@@ -77,12 +64,11 @@
             auto &c = *new W_(temp, text);
             UI::RadioControl<T_, W_>::Add(value, c);
             
+            c.SetWidth((GetWidth() - spacing * (GetColumns() - 1)) / GetColumns());
+            
             if(value == this->Get())
                 c.Check();
             
-            if(GetWidth() < c.GetWidth())
-                SetWidth(c.GetWidth());
-            
             if(IsVisible())
                 this->PlaceIn((UI::WidgetContainer&)*this, {0, 0}, spacing);
                 
@@ -249,6 +235,15 @@
         bool KeyEvent(Input::Key key, float state) override {
             return UI::WidgetContainer::KeyEvent(key, state);
         }
+        
+        /// Changes the number of columns
+        void SetColumns(int value) {
+            UI::RadioControl<T_, W_>::SetColumns(value);
+            
+            rearrange();
+        }
+        
+        using UI::RadioControl<T_, W_>::GetColumns;
 
     protected:
         virtual void addto(Layer &layer) override { 
@@ -298,6 +293,23 @@
         }
         
         virtual bool removingwidget(WidgetBase &) override { return false; }
+        
+        void rearrange() {
+            int total = 0, col = 0;
+            for(auto p : this->elements) {
+                if(col % GetColumns() == 0)
+                    total += p.second.GetHeight() + spacing;
+                
+                p.second.SetWidth((GetWidth() - spacing * (GetColumns() - 1)) / GetColumns());
+                col++;
+            }
+            
+            if(total > 0) total -= spacing;
+            
+            contents.SetHeight(total);
+            
+            this->PlaceIn((UI::WidgetContainer&)*this, {0, 0}, spacing);
+        }
 
         Geometry::Point location = {0, 0};
         int spacing = 4;
--- a/Testing/Source/Manual/UI_Generate.cpp	Sun Oct 04 10:24:35 2020 +0300
+++ b/Testing/Source/Manual/UI_Generate.cpp	Sun Oct 04 11:36:11 2020 +0300
@@ -75,8 +75,10 @@
     Gorgon::Widgets::Label l2("Error", Gorgon::Widgets::Registry::Label_Error);
 
     Gorgon::Widgets::RadioButtons<int> radio(Gorgon::Widgets::Registry::Radio_Regular);
-    radio.Add(0,"Americano");
-    radio.Add(1,"Latte");
+    radio.Add(0,"One");
+    radio.Add(1,"Two");
+    
+    radio.SetColumns(2);
 
     Gorgon::Widgets::Inputbox<std::string> input;
 

mercurial