From 2bcac4a83eebd0811b1ad0bb6b572305e2d75c47 Mon Sep 17 00:00:00 2001 From: "H. Utku Maden" Date: Fri, 28 Jul 2023 22:37:13 +0300 Subject: [PATCH] Remove references to old control types, at least for now. --- Quik/Controls/Button.cs | 107 --------------------- Quik/Controls/Container.cs | 140 ---------------------------- Quik/Controls/Control.cs | 154 ------------------------------- Quik/Controls/HierarchyEvents.cs | 38 -------- Quik/Controls/Label.cs | 37 -------- Quik/Controls/RootControl.cs | 110 ---------------------- Quik/Controls/UIBase.cs | 66 +++++++++++++ Quik/Controls/View.cs | 8 ++ Quik/Mouse.cs | 16 ++-- 9 files changed, 82 insertions(+), 594 deletions(-) delete mode 100644 Quik/Controls/Button.cs delete mode 100644 Quik/Controls/Container.cs delete mode 100644 Quik/Controls/Control.cs delete mode 100644 Quik/Controls/HierarchyEvents.cs delete mode 100644 Quik/Controls/Label.cs delete mode 100644 Quik/Controls/RootControl.cs create mode 100644 Quik/Controls/UIBase.cs create mode 100644 Quik/Controls/View.cs diff --git a/Quik/Controls/Button.cs b/Quik/Controls/Button.cs deleted file mode 100644 index 8ff8909..0000000 --- a/Quik/Controls/Button.cs +++ /dev/null @@ -1,107 +0,0 @@ -using Quik.CommandMachine; -using Quik.Typography; - -namespace Quik.Controls -{ - public class Button : Control - { - public string Text { get; set; } = "Button"; - public float Padding { get; set; } = 4.0f; - - // public QuikFont Font { get; set; } - - public QuikStrokeStyle NormalStroke { get; set; } - public QuikFillStyle NormalFill { get; set; } - public QuikStrokeStyle HoverStroke { get; set; } - public QuikFillStyle HoverFill { get; set; } - public QuikStrokeStyle ActiveStroke { get; set; } - public QuikFillStyle ActiveFill { get; set; } - - private ButtonClass _class = ButtonClass.Normal; - - private enum ButtonClass - { - Normal, - Hover, - Active - } - - - protected override void OnMouseEnter(MouseMoveEventArgs args) - { - base.OnMouseEnter(args); - - if (_class == ButtonClass.Normal) - _class = ButtonClass.Hover; - } - - protected override void OnMouseLeave(MouseMoveEventArgs args) - { - base.OnMouseLeave(args); - - if (_class == ButtonClass.Hover) - _class = ButtonClass.Normal; - } - - protected override void OnMouseDown(MouseButtonEventArgs args) - { - base.OnMouseDown(args); - - if (_class == ButtonClass.Hover) - _class = ButtonClass.Active; - } - - protected override void OnMouseUp(MouseButtonEventArgs args) - { - base.OnMouseUp(args); - - if (_class == ButtonClass.Active) - { - _class = ButtonClass.Hover; - } - } - - protected override void OnPaint(CommandQueue draw) - { - QRectangle bounds = AbsoluteBounds; - - switch (_class) - { - default: - case ButtonClass.Normal: - draw.Rectangle(bounds); - // draw.Commands.Enqueue(new QuikCommandRectangle(bounds) { - // StrokeStyle = NormalStroke, - // FillStyle = NormalFill - // }); - break; - // case ButtonClass.Hover: - // draw.Commands.Enqueue(new QuikCommandRectangle(bounds) { - // StrokeStyle = HoverStroke, - // FillStyle = HoverFill - // }); - // break; - // case ButtonClass.Active: - // draw.Commands.Enqueue(new QuikCommandRectangle(bounds) { - // StrokeStyle = ActiveStroke, - // FillStyle = ActiveFill - // }); - // break; - } - - // Position the text so that it is centered. - // float ascender = Root.Context.DefaultFont.Ascender; - // float descender = -Root.Context.DefaultFont.Descender; - // QuikVec2 position = - // bounds.Min + - // new QuikVec2( - // Padding, - // ( - // (bounds.Size.Y - ascender - descender) / - // 2) - // + descender); - - // draw.PutText(Text, position); - } - } -} \ No newline at end of file diff --git a/Quik/Controls/Container.cs b/Quik/Controls/Container.cs deleted file mode 100644 index 9927a42..0000000 --- a/Quik/Controls/Container.cs +++ /dev/null @@ -1,140 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using Quik.CommandMachine; - -namespace Quik.Controls -{ - public class Container : Control, IList - { - private List _children = new List(); - public IEnumerator GetEnumerator() => _children.GetEnumerator(); - IEnumerator IEnumerable.GetEnumerator() - { - return GetEnumerator(); - } - - public virtual void Add(Control item) - { - _children.Add(item); - item.NotifyParentChanged(new ParentChangedEventArgs(this)); - } - - public virtual void Clear() - { - foreach (Control child in _children) - { - child.NotifyParentChanged(ParentChangedEventArgs.Disowned); - } - _children.Clear(); - } - - public bool Contains(Control item) => _children.Contains(item); - - public void CopyTo(Control[] array, int arrayIndex) => _children.CopyTo(array, arrayIndex); - - public virtual bool Remove(Control item) - { - if (_children.Remove(item)) - { - item.NotifyParentChanged(ParentChangedEventArgs.Disowned); - - return true; - } - else - { - return false; - } - } - - public int Count => _children.Count; - public bool IsReadOnly => false; - - public int IndexOf(Control item) =>_children.IndexOf(item); - - public virtual void Insert(int index, Control item) - { - _children.Insert(index, item); - NotifyParentChanged(new ParentChangedEventArgs(this)); - } - - public virtual void RemoveAt(int index) - { - _children.RemoveAt(index); - _children[index].NotifyParentChanged(ParentChangedEventArgs.Disowned); - } - - public virtual Control this[int index] - { - get => _children[index]; - set - { - _children[index].NotifyParentChanged(ParentChangedEventArgs.Disowned); - _children[index] = value; - value.NotifyParentChanged(new ParentChangedEventArgs(this)); - } - } - - public Control this[string name] => _children.Find(x => x.Name == name); - - internal override void NotifyUpdate() - { - base.NotifyUpdate(); - - foreach (Control child in _children) - { - child.NotifyUpdate(); - } - } - - internal override void NotifyPaint(CommandQueue draw) - { - base.NotifyPaint(draw); - - foreach (Control child in _children) - { - child.NotifyPaint(draw); - } - } - - internal override void NotifyRootChanged(RootChangedEventArgs args) - { - base.NotifyRootChanged(args); - - foreach (Control child in _children) - { - child.NotifyRootChanged(args); - } - } - - internal override void NotifyMouseMove(MouseMoveEventArgs args) - { - base.NotifyMouseMove(args); - - foreach (Control child in _children) - { - child.NotifyMouseMove(args); - } - } - - internal override void NotifyMouseDown(MouseButtonEventArgs args) - { - base.NotifyMouseDown(args); - - foreach (Control child in _children) - { - child.NotifyMouseDown(args); - } - } - - internal override void NotifyMouseUp(MouseButtonEventArgs args) - { - base.NotifyMouseUp(args); - - foreach (Control child in _children) - { - child.NotifyMouseUp(args); - } - } - } -} \ No newline at end of file diff --git a/Quik/Controls/Control.cs b/Quik/Controls/Control.cs deleted file mode 100644 index 9314294..0000000 --- a/Quik/Controls/Control.cs +++ /dev/null @@ -1,154 +0,0 @@ -using System; -using Quik.CommandMachine; - -namespace Quik.Controls -{ - public class Control - { - public string Name { get; set; } = null; - - public Control Parent { get; set; } = null; - - protected RootControl Root { get; set; } = null; - - public QRectangle Bounds { get; set; } - - public bool Focused { get => Root.FocusedControl == this; } - - public QRectangle AbsoluteBounds - { - get => Parent is null - ? Bounds - : new QRectangle(Parent.Bounds.Min + Bounds.Max, Parent.Bounds.Min + Bounds.Min); - set => Bounds = Parent is null - ? value - : new QRectangle(value.Min - Parent.Bounds.Min, value.Max - Parent.Bounds.Min); - } - - private MouseButton DownButtons; - - // Hierarchy events. - - public event EventHandler Update; - public event EventHandler Paint; - public event EventHandler RootChanging; - public event EventHandler ParentChanging; - public event EventHandler FocusLost; - public event EventHandler FocusAcquired; - - // Mouse events. - - public event EventHandler Clicked; - public event EventHandler MouseDown; - public event EventHandler MouseUp; - public event EventHandler MouseEnter; - public event EventHandler MouseMove; - // public event EventHandler MouseHover; - public event EventHandler MouseLeave; - - protected virtual void OnUpdate() => Update?.Invoke(this, EventArgs.Empty); - protected virtual void OnPaint(CommandQueue draw) => Paint?.Invoke(this, draw); - protected virtual void OnParentChanging(ParentChangedEventArgs args) => ParentChanging?.Invoke(this, args); - protected virtual void OnRootChanging(RootChangedEventArgs args) => RootChanging?.Invoke(this, args); - protected virtual void OnFocusLost(FocusChangedEventArgs args) => FocusLost?.Invoke(this, args); - protected virtual void OnFocusAcquired(FocusChangedEventArgs args) => FocusAcquired?.Invoke(this, args); - - protected virtual void OnClicked(MouseButtonEventArgs args) => Clicked?.Invoke(this, args); - protected virtual void OnMouseDown(MouseButtonEventArgs args) => MouseDown?.Invoke(this, args); - protected virtual void OnMouseUp(MouseButtonEventArgs args) => MouseUp?.Invoke(this, args); - protected virtual void OnMouseEnter(MouseMoveEventArgs args) => MouseEnter?.Invoke(this, args); - protected virtual void OnMouseMove(MouseMoveEventArgs args) => MouseMove?.Invoke(this, args); - // protected virtual void OnMouseHover(MouseMoveEventArgs args) => MouseHover?.Invoke(this, args); - protected virtual void OnMouseLeave(MouseMoveEventArgs args) => MouseLeave?.Invoke(this, args); - - public void Focus() - { - Root?.Focus(this); - } - - internal virtual void NotifyUpdate() - { - OnUpdate(); - } - - internal virtual void NotifyPaint(CommandQueue draw) - { - OnPaint(draw); - } - - internal void NotifyFocusChanged(FocusChangedEventArgs args) - { - if (args.Focused == this) - { - OnFocusAcquired(args); - } - else - { - OnFocusLost(args); - } - } - - internal virtual void NotifyParentChanged(ParentChangedEventArgs args) - { - OnParentChanging(args); - Parent = args.NewParent; - } - - internal virtual void NotifyRootChanged(RootChangedEventArgs args) - { - OnRootChanging(args); - Root = args.NewRoot; - } - - internal virtual void NotifyMouseMove(MouseMoveEventArgs args) - { - QRectangle bounds = AbsoluteBounds; - - if (bounds.Contains(args.AbsolutePosition)) - { - if (!bounds.Contains(args.AbsolutePosition - args.Motion)) - { - OnMouseEnter(args); - } - - OnMouseMove(args); - } - else if (bounds.Contains(args.AbsolutePosition - args.Motion)) - { - OnMouseLeave(args); - } - } - - internal virtual void NotifyMouseDown(MouseButtonEventArgs args) - { - if (AbsoluteBounds.Contains(args.AbsolutePosition)) - { - OnMouseDown(args); - DownButtons = args.Buttons; - } - } - - internal virtual void NotifyMouseUp(MouseButtonEventArgs args) - { - if (AbsoluteBounds.Contains(args.AbsolutePosition)) - { - MouseButton mask; - - OnMouseUp(args); - - if ((mask = DownButtons & args.Buttons) > 0) - { - MouseButtonEventArgs nargs = new MouseButtonEventArgs(args.AbsolutePosition, mask); - Focus(); - OnClicked(nargs); - } - - DownButtons &= ~mask; - } - else - { - DownButtons = 0; - } - } - } -} \ No newline at end of file diff --git a/Quik/Controls/HierarchyEvents.cs b/Quik/Controls/HierarchyEvents.cs deleted file mode 100644 index afa35cc..0000000 --- a/Quik/Controls/HierarchyEvents.cs +++ /dev/null @@ -1,38 +0,0 @@ -using System; - -namespace Quik.Controls -{ - public class ParentChangedEventArgs : EventArgs - { - public Control NewParent { get; } - - public ParentChangedEventArgs(Control newParent) - { - NewParent = newParent; - } - - public static ParentChangedEventArgs Disowned { get; } = new ParentChangedEventArgs(null); - } - - public class RootChangedEventArgs : EventArgs - { - public RootControl NewRoot { get; } - - public RootChangedEventArgs(RootControl newRoot) - { - NewRoot = newRoot; - } - - public static RootChangedEventArgs Disowned { get; } = new RootChangedEventArgs(null); - } - - public class FocusChangedEventArgs : EventArgs - { - public Control Focused { get; } - - public FocusChangedEventArgs(Control focused) - { - Focused = focused; - } - } -} \ No newline at end of file diff --git a/Quik/Controls/Label.cs b/Quik/Controls/Label.cs deleted file mode 100644 index 581d4ca..0000000 --- a/Quik/Controls/Label.cs +++ /dev/null @@ -1,37 +0,0 @@ -using Quik.CommandMachine; -using Quik.Typography; - -namespace Quik.Controls -{ - public class Label : Control - { - public bool MultiLine { get; set; } = false; - public string Text { get; set; } = ""; - - public float Padding { get; set; } = 4.0f; - - // public QuikFont Font { get; set; } - - // protected override void OnPaint(CommandQueue draw) - // { - // if (MultiLine) - // { - // QuikRectangle absolute = AbsoluteBounds; - // QuikVec2 paddingVector = new QuikVec2(Padding, Padding); - // QuikRectangle rectangle; - // rectangle.Min = absolute.Min + paddingVector; - // rectangle.Max = absolute.Min - paddingVector; - - // // FIXME: For now use a puttext command. - // draw.PutText(Text, rectangle.Min + new QuikVec2(0, rectangle.Size.Y / 3f)); - - // // draw.FlowText(Text, rectangle); - // } - // else - // { - // QuikVec2 position = AbsoluteBounds.Min + new QuikVec2(Padding, Padding + AbsoluteBounds.Size.Y / 3f); - // draw.PutText(Text, position); - // } - // } - } -} \ No newline at end of file diff --git a/Quik/Controls/RootControl.cs b/Quik/Controls/RootControl.cs deleted file mode 100644 index ee4b3d3..0000000 --- a/Quik/Controls/RootControl.cs +++ /dev/null @@ -1,110 +0,0 @@ - -using Quik.CommandMachine; - -namespace Quik.Controls -{ - public sealed class RootControl : Container - { - // public QuikContext Context { get; } - public Control FocusedControl { get; private set; } - - public RootControl(object context) - { - // Context = context; - } - - public void Focus(Control which) - { - FocusChangedEventArgs args = new FocusChangedEventArgs(which); - - FocusedControl?.NotifyFocusChanged(args); - (FocusedControl = which).NotifyFocusChanged(args); - } - - public new void NotifyUpdate() - { - base.NotifyUpdate(); - } - - public new void NotifyPaint(CommandQueue draw) - { - base.NotifyPaint(draw); - } - - private MouseState LastMouseState; - public void NotifyMouse(in MouseState state) - { - MouseMoveEventArgs move = new MouseMoveEventArgs( - state.AbsolutePosition, - LastMouseState.AbsolutePosition); - MouseButtonEventArgs up = new MouseButtonEventArgs( - state.AbsolutePosition, - LastMouseState.ButtonsDown & ~state.ButtonsDown); - MouseButtonEventArgs down = new MouseButtonEventArgs( - state.AbsolutePosition, - ~LastMouseState.ButtonsDown & state.ButtonsDown); - - if (move.Motion.Magnitude > 0) - NotifyMouseMove(move); - - if (up.Buttons != 0) - NotifyMouseUp(up); - - if (down.Buttons != 0) - NotifyMouseDown(down); - - LastMouseState = state; - } - - public override void Add(Control item) - { - base.Add(item); - item.NotifyRootChanged(new RootChangedEventArgs(this)); - } - - public override void Clear() - { - foreach (Control child in this) - { - child.NotifyRootChanged(RootChangedEventArgs.Disowned); - } - base.Clear(); - } - - public override void Insert(int index, Control item) - { - base.Insert(index, item); - item.NotifyRootChanged(new RootChangedEventArgs(this)); - } - - public override bool Remove(Control item) - { - if(base.Remove(item)) - { - item.NotifyRootChanged(RootChangedEventArgs.Disowned); - return true; - } - else - { - return false; - } - } - - public override void RemoveAt(int index) - { - this[index].NotifyRootChanged(RootChangedEventArgs.Disowned); - base.RemoveAt(index); - } - - public override Control this[int index] - { - get => base[index]; - set - { - base[index].NotifyRootChanged(RootChangedEventArgs.Disowned); - base[index] = value; - value.NotifyRootChanged(new RootChangedEventArgs(this)); - } - } - } -} \ No newline at end of file diff --git a/Quik/Controls/UIBase.cs b/Quik/Controls/UIBase.cs new file mode 100644 index 0000000..5068608 --- /dev/null +++ b/Quik/Controls/UIBase.cs @@ -0,0 +1,66 @@ + +using System; +using Quik.CommandMachine; + +namespace Quik.Controls +{ + /// + /// Bases for all UI elements. + /// + public abstract class UIBase + { + public UIBase Parent { get; protected set; } + public string Id { get; set; } + public QRectangle Bounds { get; set; } + + public QVec2 Position + { + get => Bounds.Min; + set => Bounds = new QRectangle(Bounds.Max - Bounds.Min + value, value); + } + + public QVec2 Size + { + get => Bounds.Max - Bounds.Min; + set => Bounds = new QRectangle(value + Bounds.Min, Bounds.Min); + } + + public QRectangle AbsoluteBounds + { + get + { + if (Parent == null) + { + return Bounds; + } + else + { + return new QRectangle(Bounds.Max + Parent.Position, Bounds.Min + Parent.Position); + } + } + } + + public void NotifyEvent(object sender, EventArgs args) + { + } + + protected virtual void PaintBegin(CommandQueue cmd) + { + cmd.PushViewport(); + cmd.StoreViewport(AbsoluteBounds); + cmd.PushZ(); + } + + protected virtual void PaintEnd(CommandQueue cmd) + { + cmd.PopZ(); + cmd.PopViewport(); + } + + public void Paint(CommandQueue cmd) + { + PaintBegin(cmd); + PaintEnd(cmd); + } + } +} \ No newline at end of file diff --git a/Quik/Controls/View.cs b/Quik/Controls/View.cs new file mode 100644 index 0000000..2955dfd --- /dev/null +++ b/Quik/Controls/View.cs @@ -0,0 +1,8 @@ +using System; + +namespace Quik.Controls +{ + public class View : UIBase + { + } +} \ No newline at end of file diff --git a/Quik/Mouse.cs b/Quik/Mouse.cs index c631e7e..b1f5871 100644 --- a/Quik/Mouse.cs +++ b/Quik/Mouse.cs @@ -42,10 +42,10 @@ namespace Quik return AbsolutePosition - origin; } - public QVec2 RelativePosition(Controls.Control control) - { - return AbsolutePosition - control.AbsoluteBounds.Min; - } + // public QVec2 RelativePosition(Controls.Control control) + // { + // return AbsolutePosition - control.AbsoluteBounds.Min; + // } } public class MouseMoveEventArgs : EventArgs @@ -66,9 +66,9 @@ namespace Quik return AbsolutePosition - origin; } - public QVec2 RelativePosition(Controls.Control control) - { - return AbsolutePosition - control.AbsoluteBounds.Min; - } + // public QVec2 RelativePosition(Controls.Control control) + // { + // return AbsolutePosition - control.AbsoluteBounds.Min; + // } } } \ No newline at end of file