Add Panel, ProgressBar, RadioButton, and Slider UI controls and test windows

This commit is contained in:
Stone_Red
2026-06-16 12:29:04 +02:00
parent 8ecca53881
commit 99c98e74a2
6 changed files with 438 additions and 18 deletions
+24
View File
@@ -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,
}
};
}
}
+103
View File
@@ -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;
}
}
}
+142
View File
@@ -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;
}
}
+10 -3
View File
@@ -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, Control> controls = [];
private Control? focusedControl = null;
private Control? capturedControl = null;
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);
Control? target = HitTestControl(local);
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)
{
return;