Remove references to old control types, at least for now.

This commit is contained in:
H. Utku Maden 2023-07-28 22:37:13 +03:00
parent 6b8b3f2f0d
commit 2bcac4a83e
Signed by: themixedupstuff
GPG Key ID: 25A001B636F17843
9 changed files with 82 additions and 594 deletions

@ -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);
}
}
}

@ -1,140 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
using Quik.CommandMachine;
namespace Quik.Controls
{
public class Container : Control, IList<Control>
{
private List<Control> _children = new List<Control>();
public IEnumerator<Control> 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);
}
}
}
}

@ -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<CommandQueue> Paint;
public event EventHandler<RootChangedEventArgs> RootChanging;
public event EventHandler<ParentChangedEventArgs> ParentChanging;
public event EventHandler<FocusChangedEventArgs> FocusLost;
public event EventHandler<FocusChangedEventArgs> FocusAcquired;
// Mouse events.
public event EventHandler<MouseButtonEventArgs> Clicked;
public event EventHandler<MouseButtonEventArgs> MouseDown;
public event EventHandler<MouseButtonEventArgs> MouseUp;
public event EventHandler<MouseMoveEventArgs> MouseEnter;
public event EventHandler<MouseMoveEventArgs> MouseMove;
// public event EventHandler<MouseMoveEventArgs> MouseHover;
public event EventHandler<MouseMoveEventArgs> 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;
}
}
}
}

@ -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;
}
}
}

@ -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);
// }
// }
}
}

@ -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));
}
}
}
}

66
Quik/Controls/UIBase.cs Normal file

@ -0,0 +1,66 @@
using System;
using Quik.CommandMachine;
namespace Quik.Controls
{
/// <summary>
/// Bases for all UI elements.
/// </summary>
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);
}
}
}

8
Quik/Controls/View.cs Normal file

@ -0,0 +1,8 @@
using System;
namespace Quik.Controls
{
public class View : UIBase
{
}
}

@ -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;
// }
}
}