#205 Window movement 4.x-dev

Sat, 28 Nov 2020 07:37:42 +0200

author
cemkalyoncu
date
Sat, 28 Nov 2020 07:37:42 +0200
branch
4.x-dev
changeset 1502
e1bd0a8d7cf7
parent 1501
5d263312e69a
child 1503
38acd13739cc

#205 Window movement

Source/Gorgon/Widgets/Generator.cpp file | annotate | diff | comparison | revisions
Source/Gorgon/Widgets/Panel.cpp file | annotate | diff | comparison | revisions
Source/Gorgon/Widgets/Panel.h file | annotate | diff | comparison | revisions
Source/Gorgon/Widgets/Window.cpp file | annotate | diff | comparison | revisions
Source/Gorgon/Widgets/Window.h file | annotate | diff | comparison | revisions
--- a/Source/Gorgon/Widgets/Generator.cpp	Fri Nov 27 20:56:41 2020 +0200
+++ b/Source/Gorgon/Widgets/Generator.cpp	Sat Nov 28 07:37:42 2020 +0200
@@ -1885,6 +1885,7 @@
             bg.Background.SetAnimation(image);
             bg.SetPadding(Border.Width*2, Border.Width*2, Border.Width*2, Border.Width*2);
             bg.SetOrientation(Graphics::Orientation::Vertical);
+            bg.SetTag(UI::ComponentTemplate::DragTag);            
         };
         
         addborder(UI::ComponentCondition::Always, PassiveWindowBorder());
@@ -1896,6 +1897,7 @@
         text.SetMargin(0, 0, 0, Border.Width);
         text.SetDataEffect(UI::ComponentTemplate::Title);
         text.SetSizing(UI::ComponentTemplate::Fixed);
+        text.SetTag(UI::ComponentTemplate::DragTag);
         
         return tmp;
     }
--- a/Source/Gorgon/Widgets/Panel.cpp	Fri Nov 27 20:56:41 2020 +0200
+++ b/Source/Gorgon/Widgets/Panel.cpp	Sat Nov 28 07:37:42 2020 +0200
@@ -12,38 +12,8 @@
         },
     }) 
     {
-        stack.SetOtherMouseEvent([this](UI::ComponentTemplate::Tag tag, Input::Mouse::EventType type, Geometry::Point, float amount) {
-            //if only horizontal scroll is enabled, use regular scroll to scroll that direction
-            if(!vscroll && hscroll && type == Input::Mouse::EventType::Scroll_Vert) {
-                type = Input::Mouse::EventType::Scroll_Hor;
-            }
-            else if(hscroll && type == Input::Mouse::EventType::Scroll_Vert && (Input::Keyboard::CurrentModifier & Input::Keyboard::Modifier::Shift)) {
-                type = Input::Mouse::EventType::Scroll_Hor;
-            }
-            
-            if(type == Input::Mouse::EventType::Scroll_Vert && vscroll) {
-                if(amount<0 && ScrollOffset().Y >= MaxScrollOffset().Y)
-                    return false;
-                
-                if(amount>0 && ScrollOffset().Y <= 0)
-                    return false;
-                
-                ScrollBy(-(int)amount*scrolldist.Y);
-                return true;
-            }
-            
-            if(type == Input::Mouse::EventType::Scroll_Hor && hscroll) {
-                if(amount<0 && ScrollOffset().X >= MaxScrollOffset().X)
-                    return false;
-                
-                if(amount>0 && ScrollOffset().X <= 0)
-                    return false;
-                
-                ScrollBy(-(int)amount*scrolldist.X, 0);
-                return true;
-            }
-            
-            return false;
+        stack.SetOtherMouseEvent([this](UI::ComponentTemplate::Tag, Input::Mouse::EventType type, Geometry::Point location, float amount) {
+            return MouseScroll(type, location, amount);
         });
         
         
@@ -532,5 +502,40 @@
             return stack.GetTemplate().GetUnitWidth();
         }
     }
+
+    bool Panel::MouseScroll(Input::Mouse::EventType type, Geometry::Point, float amount) {
+        //if only horizontal scroll is enabled, use regular scroll to scroll that direction
+        if(!vscroll && hscroll && type == Input::Mouse::EventType::Scroll_Vert) {
+            type = Input::Mouse::EventType::Scroll_Hor;
+        }
+        else if(hscroll && type == Input::Mouse::EventType::Scroll_Vert && (Input::Keyboard::CurrentModifier & Input::Keyboard::Modifier::Shift)) {
+            type = Input::Mouse::EventType::Scroll_Hor;
+        }
+        
+        if(type == Input::Mouse::EventType::Scroll_Vert && vscroll) {
+            if(amount<0 && ScrollOffset().Y >= MaxScrollOffset().Y)
+                return false;
+            
+            if(amount>0 && ScrollOffset().Y <= 0)
+                return false;
+            
+            ScrollBy(-(int)amount*scrolldist.Y);
+            return true;
+        }
+        
+        if(type == Input::Mouse::EventType::Scroll_Hor && hscroll) {
+            if(amount<0 && ScrollOffset().X >= MaxScrollOffset().X)
+                return false;
+            
+            if(amount>0 && ScrollOffset().X <= 0)
+                return false;
+            
+            ScrollBy(-(int)amount*scrolldist.X, 0);
+            return true;
+        }
+        
+        return false;
+    }
+
 } }
 
--- a/Source/Gorgon/Widgets/Panel.h	Fri Nov 27 20:56:41 2020 +0200
+++ b/Source/Gorgon/Widgets/Panel.h	Sat Nov 28 07:37:42 2020 +0200
@@ -206,6 +206,11 @@
             issizesset = false;
         }
         
+        /// Report mouse scroll. This function will be called automatically
+        /// for regular mouse events. This function will return false if the
+        /// given mouse event is not consumed.
+        bool MouseScroll(Input::Mouse::EventType type, Geometry::Point location, float amount);
+        
     protected:
         virtual bool allowfocus() const override;
         
--- a/Source/Gorgon/Widgets/Window.cpp	Fri Nov 27 20:56:41 2020 +0200
+++ b/Source/Gorgon/Widgets/Window.cpp	Sat Nov 28 07:37:42 2020 +0200
@@ -9,6 +9,11 @@
         title(title)
     {
         stack.SetData(UI::ComponentTemplate::Title, title);
+        
+        stack.SetMouseDownEvent([this](auto tag, auto location, auto button) { mouse_down(tag, location, button); });
+        stack.SetMouseUpEvent([this](auto tag, auto location, auto button) { mouse_up(tag, location, button); });
+        stack.SetMouseMoveEvent([this](auto tag, auto location) { mouse_move(tag, location); });
+        
         updatescrollvisibility();
     }
     
@@ -36,13 +41,13 @@
     void Window::updatescrollvisibility() {
         auto val = stack.GetTargetValue();
         
-        if(val[3] == 1 || val[3] == 0 || !hscroll) {
+        if(val[2] == 1 || val[2] == 0 || !hscroll) {
             stack.RemoveCondition(UI::ComponentCondition::HScroll);
         }
         else {
             stack.AddCondition(UI::ComponentCondition::HScroll);
         }
-        if(val[4] == 1 || val[4] == 0 || !vscroll) {
+        if(val[3] == 1 || val[3] == 0 || !vscroll) {
             stack.RemoveCondition(UI::ComponentCondition::VScroll);
         }
         else {
@@ -50,4 +55,46 @@
         }
     }
     
+    void Window::mouse_down(UI::ComponentTemplate::Tag tag, Geometry::Point location, Input::Mouse::Button button) {
+        if(tag == UI::ComponentTemplate::NoTag) {
+            if(stack.IndexOfTag(UI::ComponentTemplate::DragTag) == -1)
+                tag = UI::ComponentTemplate::DragTag;
+            else {
+                int ind = stack.ComponentAt(location);
+                
+                if(ind != -1)
+                    tag = stack.GetTemplate(ind).GetTag();
+            }
+        }
+        
+        if(button == Input::Mouse::Button::Left && allowmove && tag == UI::ComponentTemplate::DragTag) {
+            moving = true;
+            dragoffset = location;
+        }
+        
+        Focus();
+    }
+    
+    void Window::mouse_up(UI::ComponentTemplate::Tag, Geometry::Point, Input::Mouse::Button button) {
+        if(button == Input::Mouse::Button::Left) {
+            moving = false;
+        }
+    }
+    
+    void Window::mouse_move(UI::ComponentTemplate::Tag, Geometry::Point location) {
+        if(moving && location != dragoffset) {
+            Move(GetLocation() + location-dragoffset);
+        }
+    }
+    
+
+    void Window::AllowMovement(bool allow) {
+        if(allowmove == allow) 
+            return;
+        
+        allowmove = allow;
+        if(moving)
+            mouse_up(UI::ComponentTemplate::NoTag, {0, 0}, Input::Mouse::Button::Left);
+    }
+
 } }
--- a/Source/Gorgon/Widgets/Window.h	Fri Nov 27 20:56:41 2020 +0200
+++ b/Source/Gorgon/Widgets/Window.h	Sat Nov 28 07:37:42 2020 +0200
@@ -34,17 +34,41 @@
             return title;
         }
         
+        /// Locks the movement of the window.
+        void LockMovement() {
+            AllowMovement(false);
+        }
+        
+        /// Allows movement of the window.
+        void AllowMovement(bool allow = true);
+        
+        /// Returns whether the window can be moved.
+        bool CanBeMoved() const {
+            return allowmove;
+        }
+        
         virtual void EnableScroll(bool vertical, bool horizontal) override;
         
         TextualProperty<Window, std::string, &Window::GetTitle, &Window::SetTitle> Title;
 
     protected:
         virtual void updatescroll() override;
+        
         virtual void updatecontent() override;
+        
         void updatescrollvisibility();
         
+        void mouse_down(UI::ComponentTemplate::Tag tag, Geometry::Point location, Input::Mouse::Button button);
+        
+        void mouse_up(UI::ComponentTemplate::Tag tag, Geometry::Point location, Input::Mouse::Button button);
+        
+        void mouse_move(UI::ComponentTemplate::Tag tag, Geometry::Point location);
+        
     private:
         std::string title;
+        bool allowmove = true;
+        bool moving = false;
+        Geometry::Point dragoffset;
     };
     
 } }

mercurial