mirror of
https://github.com/Stone-Red-Code/RemSox.git
synced 2026-09-09 07:46:32 +02:00
Add Panel, ProgressBar, RadioButton, and Slider UI controls and test windows
This commit is contained in:
+89
-15
@@ -3,6 +3,8 @@ using RemSox.UI.GUI.Rendering;
|
|||||||
using RemSox.UI.GUI.UIEelements.Controls;
|
using RemSox.UI.GUI.UIEelements.Controls;
|
||||||
using RemSox.UI.GUI.Windows;
|
using RemSox.UI.GUI.Windows;
|
||||||
|
|
||||||
|
using System.Drawing;
|
||||||
|
|
||||||
namespace RemSox.Processes;
|
namespace RemSox.Processes;
|
||||||
|
|
||||||
internal class DesktopProcess() : Process("Desktop Manager")
|
internal class DesktopProcess() : Process("Desktop Manager")
|
||||||
@@ -17,31 +19,30 @@ internal class DesktopProcess() : Process("Desktop Manager")
|
|||||||
isGraphicsInitialized = true;
|
isGraphicsInitialized = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Force existing windows to redraw onto the new canvas renderer
|
|
||||||
WindowManager.InvalidateAll();
|
WindowManager.InvalidateAll();
|
||||||
|
|
||||||
// Create a test window for new UI controls
|
// Test window 1: interactive controls
|
||||||
Window testWindow = WindowManager.CreateWindow(this, "UI Controls Test", new System.Drawing.Size(200, 180));
|
Window testWindow = WindowManager.CreateWindow(this, "UI Controls", new Size(280, 260));
|
||||||
testWindow.AutoFlush = true;
|
testWindow.AutoFlush = true;
|
||||||
|
|
||||||
Button button = testWindow.CreateUIElement<UI.GUI.UIEelements.Controls.Button>(b =>
|
Button button = testWindow.CreateUIElement<Button>(b =>
|
||||||
{
|
{
|
||||||
b.Position = new System.Drawing.Point(20, 30);
|
b.Position = new Point(20, 30);
|
||||||
b.Size = new System.Drawing.Size(200, 30);
|
b.Size = new Size(240, 25);
|
||||||
b.Text = "Click Me";
|
b.Text = "Click Me";
|
||||||
b.BackgroundColor = System.Drawing.Color.LightBlue;
|
b.BackgroundColor = Color.LightBlue;
|
||||||
});
|
});
|
||||||
|
|
||||||
button.OnClick += (s, e) =>
|
button.OnClick += (s, e) =>
|
||||||
{
|
{
|
||||||
button.Text = "Clicked!";
|
button.Text = "Clicked!";
|
||||||
button.BackgroundColor = System.Drawing.Color.LightGreen;
|
button.BackgroundColor = Color.LightGreen;
|
||||||
};
|
};
|
||||||
|
|
||||||
CheckBox check = testWindow.CreateUIElement<UI.GUI.UIEelements.Controls.CheckBox>(c =>
|
CheckBox check = testWindow.CreateUIElement<CheckBox>(c =>
|
||||||
{
|
{
|
||||||
c.Position = new System.Drawing.Point(20, 80);
|
c.Position = new Point(20, 65);
|
||||||
c.Size = new System.Drawing.Size(200, 30);
|
c.Size = new Size(240, 20);
|
||||||
c.Text = "Check Me";
|
c.Text = "Check Me";
|
||||||
c.IsChecked = true;
|
c.IsChecked = true;
|
||||||
});
|
});
|
||||||
@@ -51,12 +52,85 @@ internal class DesktopProcess() : Process("Desktop Manager")
|
|||||||
check.Text = check.IsChecked ? "Checked!" : "Unchecked!";
|
check.Text = check.IsChecked ? "Checked!" : "Unchecked!";
|
||||||
};
|
};
|
||||||
|
|
||||||
_ = testWindow.CreateUIElement<UI.GUI.UIEelements.Shapes.Line>(l =>
|
RadioButton radio = testWindow.CreateUIElement<RadioButton>(r =>
|
||||||
{
|
{
|
||||||
l.Position = new System.Drawing.Point(20, 130);
|
r.Position = new Point(20, 95);
|
||||||
l.EndPosition = new System.Drawing.Point(180, 130);
|
r.Size = new Size(240, 20);
|
||||||
l.Color = System.Drawing.Color.Red;
|
r.Text = "Radio Option";
|
||||||
|
r.IsChecked = true;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Slider slider = testWindow.CreateUIElement<Slider>(s =>
|
||||||
|
{
|
||||||
|
s.Position = new Point(20, 130);
|
||||||
|
s.Size = new Size(240, 24);
|
||||||
|
s.BackgroundColor = Color.SteelBlue;
|
||||||
|
s.Value = 60;
|
||||||
|
});
|
||||||
|
|
||||||
|
ProgressBar progress = testWindow.CreateUIElement<ProgressBar>(p =>
|
||||||
|
{
|
||||||
|
p.Position = new Point(20, 170);
|
||||||
|
p.Size = new Size(240, 20);
|
||||||
|
p.BackgroundColor = Color.DimGray;
|
||||||
|
p.FillColor = Color.LimeGreen;
|
||||||
|
p.Value = 60;
|
||||||
|
});
|
||||||
|
|
||||||
|
slider.OnValueChanged += (_, _) =>
|
||||||
|
{
|
||||||
|
progress.Value = slider.Value;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Test window 2: shapes and panel
|
||||||
|
Window shapesWin = WindowManager.CreateWindow(this, "Shapes & Panel", new Size(200, 220));
|
||||||
|
shapesWin.AutoFlush = true;
|
||||||
|
|
||||||
|
_ = shapesWin.CreateUIElement<Panel>(p =>
|
||||||
|
{
|
||||||
|
p.Position = new Point(10, 25);
|
||||||
|
p.Size = new Size(180, 80);
|
||||||
|
p.BackgroundColor = Color.FromArgb(48, 48, 48);
|
||||||
|
});
|
||||||
|
|
||||||
|
_ = shapesWin.CreateUIElement<UI.GUI.UIEelements.Shapes.Circle>(c =>
|
||||||
|
{
|
||||||
|
c.Position = new Point(20, 35);
|
||||||
|
c.Radius = 10;
|
||||||
|
c.Color = Color.Coral;
|
||||||
|
});
|
||||||
|
|
||||||
|
_ = shapesWin.CreateUIElement<UI.GUI.UIEelements.Shapes.Rectangle>(r =>
|
||||||
|
{
|
||||||
|
r.Position = new Point(60, 35);
|
||||||
|
r.Size = new Size(50, 30);
|
||||||
|
r.Color = Color.CornflowerBlue;
|
||||||
|
r.IsFilled = true;
|
||||||
|
});
|
||||||
|
|
||||||
|
_ = shapesWin.CreateUIElement<UI.GUI.UIEelements.Shapes.Text>(t =>
|
||||||
|
{
|
||||||
|
t.Position = new Point(10, 120);
|
||||||
|
t.Content = "Hello from the desktop!";
|
||||||
|
t.Color = Color.White;
|
||||||
|
t.FontSize = 14;
|
||||||
|
});
|
||||||
|
|
||||||
|
_ = shapesWin.CreateUIElement<UI.GUI.UIEelements.Shapes.Line>(l =>
|
||||||
|
{
|
||||||
|
l.Position = new Point(10, 150);
|
||||||
|
l.EndPosition = new Point(180, 180);
|
||||||
|
l.Color = Color.Orange;
|
||||||
|
});
|
||||||
|
|
||||||
|
CheckBox shapesCheck = shapesWin.CreateUIElement<CheckBox>(c =>
|
||||||
|
{
|
||||||
|
c.Position = new Point(10, 185);
|
||||||
|
c.Size = new Size(180, 20);
|
||||||
|
c.Text = "Toggle";
|
||||||
|
});
|
||||||
|
|
||||||
|
shapesCheck.OnCheckedChanged += (_, _) => { };
|
||||||
}
|
}
|
||||||
|
|
||||||
internal override void Tick()
|
internal override void Tick()
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
using RemSox.UI.GUI.Rendering;
|
||||||
|
|
||||||
|
using System.Drawing;
|
||||||
|
|
||||||
|
namespace RemSox.UI.GUI.UIEelements.Controls;
|
||||||
|
|
||||||
|
public class Panel() : Control("Panel")
|
||||||
|
{
|
||||||
|
public override IEnumerable<RenderCommand> ToPrimitives(int windowId)
|
||||||
|
{
|
||||||
|
yield return new RenderCommand
|
||||||
|
{
|
||||||
|
WindowId = windowId,
|
||||||
|
ElementId = PrimitiveId(0),
|
||||||
|
Type = RenderCommandType.DrawFilledRect,
|
||||||
|
Position = Position,
|
||||||
|
Properties = new Dictionary<string, object?>
|
||||||
|
{
|
||||||
|
["Color"] = BackgroundColor,
|
||||||
|
["Size"] = Size,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,70 @@
|
|||||||
|
using RemSox.UI.GUI.Rendering;
|
||||||
|
|
||||||
|
using System.Drawing;
|
||||||
|
|
||||||
|
namespace RemSox.UI.GUI.UIEelements.Controls;
|
||||||
|
|
||||||
|
public class ProgressBar() : Control("ProgressBar")
|
||||||
|
{
|
||||||
|
public int Value
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
set => SetProperty(nameof(Value), ref field, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Color FillColor
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
set => SetProperty(nameof(FillColor), ref field, value);
|
||||||
|
} = Color.Green;
|
||||||
|
|
||||||
|
public override IEnumerable<RenderCommand> ToPrimitives(int windowId)
|
||||||
|
{
|
||||||
|
// Background
|
||||||
|
yield return new RenderCommand
|
||||||
|
{
|
||||||
|
WindowId = windowId,
|
||||||
|
ElementId = PrimitiveId(0),
|
||||||
|
Type = RenderCommandType.DrawFilledRect,
|
||||||
|
Position = Position,
|
||||||
|
Properties = new Dictionary<string, object?>
|
||||||
|
{
|
||||||
|
["Color"] = BackgroundColor,
|
||||||
|
["Size"] = Size,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Fill
|
||||||
|
int fillWidth = Size.Width * Math.Clamp(Value, 0, 100) / 100;
|
||||||
|
|
||||||
|
if (fillWidth > 0)
|
||||||
|
{
|
||||||
|
yield return new RenderCommand
|
||||||
|
{
|
||||||
|
WindowId = windowId,
|
||||||
|
ElementId = PrimitiveId(1),
|
||||||
|
Type = RenderCommandType.DrawFilledRect,
|
||||||
|
Position = Position,
|
||||||
|
Properties = new Dictionary<string, object?>
|
||||||
|
{
|
||||||
|
["Color"] = FillColor,
|
||||||
|
["Size"] = new Size(fillWidth, Size.Height),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Border
|
||||||
|
yield return new RenderCommand
|
||||||
|
{
|
||||||
|
WindowId = windowId,
|
||||||
|
ElementId = PrimitiveId(2),
|
||||||
|
Type = RenderCommandType.DrawRectBorder,
|
||||||
|
Position = Position,
|
||||||
|
Properties = new Dictionary<string, object?>
|
||||||
|
{
|
||||||
|
["Color"] = Color.DarkGray,
|
||||||
|
["Size"] = Size,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,103 @@
|
|||||||
|
using RemSox.UI.GUI.Rendering;
|
||||||
|
|
||||||
|
using System.Drawing;
|
||||||
|
|
||||||
|
namespace RemSox.UI.GUI.UIEelements.Controls;
|
||||||
|
|
||||||
|
public class RadioButton() : Control("RadioButton")
|
||||||
|
{
|
||||||
|
public event EventHandler? OnCheckedChanged;
|
||||||
|
|
||||||
|
public bool IsChecked
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
set
|
||||||
|
{
|
||||||
|
SetProperty(nameof(IsChecked), ref field, value);
|
||||||
|
OnCheckedChanged?.Invoke(this, EventArgs.Empty);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public string Text
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
set => SetProperty(nameof(Text), ref field, value);
|
||||||
|
} = string.Empty;
|
||||||
|
|
||||||
|
public Color TextColor
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
set => SetProperty(nameof(TextColor), ref field, value);
|
||||||
|
} = Color.White;
|
||||||
|
|
||||||
|
public override IEnumerable<RenderCommand> ToPrimitives(int windowId)
|
||||||
|
{
|
||||||
|
int diameter = Size.Height;
|
||||||
|
int radius = diameter / 2;
|
||||||
|
Point center = new(Position.X + radius, Position.Y + radius);
|
||||||
|
|
||||||
|
// Outer circle
|
||||||
|
yield return new RenderCommand
|
||||||
|
{
|
||||||
|
WindowId = windowId,
|
||||||
|
ElementId = PrimitiveId(0),
|
||||||
|
Type = RenderCommandType.DrawCircle,
|
||||||
|
Position = new Point(center.X - radius, center.Y - radius),
|
||||||
|
Properties = new Dictionary<string, object?>
|
||||||
|
{
|
||||||
|
["Color"] = Color.DarkGray,
|
||||||
|
["Radius"] = radius,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Inner fill when checked
|
||||||
|
if (IsChecked)
|
||||||
|
{
|
||||||
|
int innerRadius = radius - 3;
|
||||||
|
if (innerRadius > 0)
|
||||||
|
{
|
||||||
|
yield return new RenderCommand
|
||||||
|
{
|
||||||
|
WindowId = windowId,
|
||||||
|
ElementId = PrimitiveId(1),
|
||||||
|
Type = RenderCommandType.DrawFilledCircle,
|
||||||
|
Position = new Point(center.X - innerRadius, center.Y - innerRadius),
|
||||||
|
Properties = new Dictionary<string, object?>
|
||||||
|
{
|
||||||
|
["Color"] = Color.Black,
|
||||||
|
["Radius"] = innerRadius,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Label text
|
||||||
|
if (!string.IsNullOrEmpty(Text))
|
||||||
|
{
|
||||||
|
int textX = Position.X + diameter + 5;
|
||||||
|
int textY = Position.Y + (Size.Height / 2) - 8;
|
||||||
|
|
||||||
|
yield return new RenderCommand
|
||||||
|
{
|
||||||
|
WindowId = windowId,
|
||||||
|
ElementId = PrimitiveId(2),
|
||||||
|
Type = RenderCommandType.DrawText,
|
||||||
|
Position = new Point(textX, textY),
|
||||||
|
Properties = new Dictionary<string, object?>
|
||||||
|
{
|
||||||
|
["Color"] = TextColor,
|
||||||
|
["Content"] = Text,
|
||||||
|
["FontSize"] = Size.Height,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void HandleMouseEvent(MouseEvent mouseEvent)
|
||||||
|
{
|
||||||
|
if (mouseEvent.Type == MouseEventType.ButtonDown && mouseEvent.Button == MouseButton.Left)
|
||||||
|
{
|
||||||
|
IsChecked = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,142 @@
|
|||||||
|
using RemSox.UI.GUI.Rendering;
|
||||||
|
|
||||||
|
using System.Drawing;
|
||||||
|
|
||||||
|
namespace RemSox.UI.GUI.UIEelements.Controls;
|
||||||
|
|
||||||
|
public class Slider() : Control("Slider")
|
||||||
|
{
|
||||||
|
public event EventHandler? OnValueChanged;
|
||||||
|
|
||||||
|
public int MinValue
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
set => SetProperty(nameof(MinValue), ref field, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int MaxValue
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
set => SetProperty(nameof(MaxValue), ref field, value);
|
||||||
|
} = 100;
|
||||||
|
|
||||||
|
public int Value
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
set
|
||||||
|
{
|
||||||
|
int clamped = Math.Clamp(value, MinValue, MaxValue);
|
||||||
|
SetProperty(nameof(Value), ref field, clamped);
|
||||||
|
if (Value == clamped)
|
||||||
|
{
|
||||||
|
OnValueChanged?.Invoke(this, EventArgs.Empty);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Color ThumbColor
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
set => SetProperty(nameof(ThumbColor), ref field, value);
|
||||||
|
} = Color.LightGray;
|
||||||
|
|
||||||
|
public override IEnumerable<RenderCommand> ToPrimitives(int windowId)
|
||||||
|
{
|
||||||
|
int trackHeight = 4;
|
||||||
|
int trackY = Position.Y + (Size.Height / 2) - (trackHeight / 2);
|
||||||
|
|
||||||
|
// Track background
|
||||||
|
yield return new RenderCommand
|
||||||
|
{
|
||||||
|
WindowId = windowId,
|
||||||
|
ElementId = PrimitiveId(0),
|
||||||
|
Type = RenderCommandType.DrawFilledRect,
|
||||||
|
Position = new Point(Position.X, trackY),
|
||||||
|
Properties = new Dictionary<string, object?>
|
||||||
|
{
|
||||||
|
["Color"] = Color.DimGray,
|
||||||
|
["Size"] = new Size(Size.Width, trackHeight),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Filled portion
|
||||||
|
int range = MaxValue - MinValue;
|
||||||
|
int fillWidth = range > 0 ? Size.Width * (Value - MinValue) / range : 0;
|
||||||
|
|
||||||
|
if (fillWidth > 0)
|
||||||
|
{
|
||||||
|
yield return new RenderCommand
|
||||||
|
{
|
||||||
|
WindowId = windowId,
|
||||||
|
ElementId = PrimitiveId(1),
|
||||||
|
Type = RenderCommandType.DrawFilledRect,
|
||||||
|
Position = new Point(Position.X, trackY),
|
||||||
|
Properties = new Dictionary<string, object?>
|
||||||
|
{
|
||||||
|
["Color"] = BackgroundColor,
|
||||||
|
["Size"] = new Size(fillWidth, trackHeight),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Thumb (centered at the fill edge)
|
||||||
|
int thumbSize = Size.Height;
|
||||||
|
int thumbX = fillWidth - (thumbSize / 2);
|
||||||
|
int thumbY = Position.Y;
|
||||||
|
|
||||||
|
yield return new RenderCommand
|
||||||
|
{
|
||||||
|
WindowId = windowId,
|
||||||
|
ElementId = PrimitiveId(2),
|
||||||
|
Type = RenderCommandType.DrawFilledCircle,
|
||||||
|
Position = new Point(Position.X + thumbX, thumbY),
|
||||||
|
Properties = new Dictionary<string, object?>
|
||||||
|
{
|
||||||
|
["Color"] = ThumbColor,
|
||||||
|
["Radius"] = thumbSize / 2,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Thumb border
|
||||||
|
yield return new RenderCommand
|
||||||
|
{
|
||||||
|
WindowId = windowId,
|
||||||
|
ElementId = PrimitiveId(3),
|
||||||
|
Type = RenderCommandType.DrawCircle,
|
||||||
|
Position = new Point(Position.X + thumbX, thumbY),
|
||||||
|
Properties = new Dictionary<string, object?>
|
||||||
|
{
|
||||||
|
["Color"] = Color.DarkGray,
|
||||||
|
["Radius"] = thumbSize / 2,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool isDragging;
|
||||||
|
|
||||||
|
public override void HandleMouseEvent(MouseEvent mouseEvent)
|
||||||
|
{
|
||||||
|
if (mouseEvent.Type == MouseEventType.ButtonDown && mouseEvent.Button == MouseButton.Left)
|
||||||
|
{
|
||||||
|
isDragging = true;
|
||||||
|
SetValueFromX(mouseEvent.X);
|
||||||
|
}
|
||||||
|
else if (mouseEvent.Type == MouseEventType.Move && isDragging)
|
||||||
|
{
|
||||||
|
SetValueFromX(mouseEvent.X);
|
||||||
|
}
|
||||||
|
else if (mouseEvent.Type == MouseEventType.ButtonUp && mouseEvent.Button == MouseButton.Left)
|
||||||
|
{
|
||||||
|
isDragging = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void SetValueFromX(int windowX)
|
||||||
|
{
|
||||||
|
int localX = windowX - Position.X;
|
||||||
|
int range = MaxValue - MinValue;
|
||||||
|
Value = range > 0
|
||||||
|
? MinValue + (localX * range / Size.Width)
|
||||||
|
: MinValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -64,6 +64,7 @@ public sealed class Window(string title, int processId, int id, IRenderSource re
|
|||||||
private readonly Dictionary<int, UIElement> uiElements = [];
|
private readonly Dictionary<int, UIElement> uiElements = [];
|
||||||
private readonly Dictionary<int, Control> controls = [];
|
private readonly Dictionary<int, Control> controls = [];
|
||||||
private Control? focusedControl = null;
|
private Control? focusedControl = null;
|
||||||
|
private Control? capturedControl = null;
|
||||||
|
|
||||||
private int nextUIElementId = 1;
|
private int nextUIElementId = 1;
|
||||||
|
|
||||||
@@ -354,13 +355,19 @@ public sealed class Window(string title, int processId, int id, IRenderSource re
|
|||||||
|
|
||||||
Point local = new(mouseEvent.X - Position.X, mouseEvent.Y - Position.Y);
|
Point local = new(mouseEvent.X - Position.X, mouseEvent.Y - Position.Y);
|
||||||
|
|
||||||
Control? target = HitTestControl(local);
|
|
||||||
|
|
||||||
if (mouseEvent.Type == MouseEventType.ButtonDown && mouseEvent.Button == MouseButton.Left)
|
if (mouseEvent.Type == MouseEventType.ButtonDown && mouseEvent.Button == MouseButton.Left)
|
||||||
{
|
{
|
||||||
focusedControl = target;
|
capturedControl = HitTestControl(local);
|
||||||
|
focusedControl = capturedControl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (mouseEvent.Type == MouseEventType.ButtonUp && mouseEvent.Button == MouseButton.Left)
|
||||||
|
{
|
||||||
|
capturedControl = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
Control? target = capturedControl ?? HitTestControl(local);
|
||||||
|
|
||||||
if (target is null)
|
if (target is null)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
|
|||||||
Reference in New Issue
Block a user