From decd79e6d3cf8a0293c3dcafdf74a496fbd2c9df Mon Sep 17 00:00:00 2001 From: Stone_Red <56473591+Stone-Red-Code@users.noreply.github.com> Date: Sat, 13 Jun 2026 15:06:29 +0200 Subject: [PATCH] Add button click and checkbox toggle handlers for UI controls --- Processes/DesktopProcess.cs | 21 ++++++++++++++++----- UI/GUI/UIEelements/Controls/Button.cs | 7 +++++-- UI/GUI/UIEelements/Controls/CheckBox.cs | 8 ++++++++ UI/GUI/Windows/Window.cs | 4 ++-- UI/GUI/Windows/WindowManager.cs | 10 +++++----- 5 files changed, 36 insertions(+), 14 deletions(-) diff --git a/Processes/DesktopProcess.cs b/Processes/DesktopProcess.cs index c49148b..38bc20f 100644 --- a/Processes/DesktopProcess.cs +++ b/Processes/DesktopProcess.cs @@ -1,5 +1,6 @@ using RemSox.Processing; using RemSox.UI.GUI.Rendering; +using RemSox.UI.GUI.UIEelements.Controls; using RemSox.UI.GUI.Windows; namespace RemSox.Processes; @@ -21,8 +22,9 @@ internal class DesktopProcess() : Process("Desktop Manager") // Create a test window for new UI controls Window testWindow = WindowManager.CreateWindow(this, "UI Controls Test", new System.Drawing.Size(200, 180)); + testWindow.AutoFlush = true; - _ = testWindow.CreateUIElement(b => + Button button = testWindow.CreateUIElement(b => { b.Position = new System.Drawing.Point(20, 30); b.Size = new System.Drawing.Size(100, 30); @@ -30,22 +32,31 @@ internal class DesktopProcess() : Process("Desktop Manager") b.BackgroundColor = System.Drawing.Color.LightBlue; }); - _ = testWindow.CreateUIElement(c => + button.OnClick += (s, e) => + { + button.Text = "Clicked!"; + button.BackgroundColor = System.Drawing.Color.LightGreen; + }; + + CheckBox check = testWindow.CreateUIElement(c => { c.Position = new System.Drawing.Point(20, 80); + c.Size = new System.Drawing.Size(100, 30); c.Text = "Check Me"; c.IsChecked = true; }); + check.OnCheckedChanged += (s, e) => + { + check.Text = check.IsChecked ? "Checked!" : "Unchecked!"; + }; + _ = testWindow.CreateUIElement(l => { l.Position = new System.Drawing.Point(20, 130); l.EndPosition = new System.Drawing.Point(180, 130); l.Color = System.Drawing.Color.Red; }); - - testWindow.AutoFlush = true; - testWindow.Flush(); } internal override void Tick() diff --git a/UI/GUI/UIEelements/Controls/Button.cs b/UI/GUI/UIEelements/Controls/Button.cs index b2f1245..3721722 100644 --- a/UI/GUI/UIEelements/Controls/Button.cs +++ b/UI/GUI/UIEelements/Controls/Button.cs @@ -18,8 +18,11 @@ public class Button() : Control("Button") set => SetProperty(nameof(TextColor), ref field, value); } = Color.Black; - public void Click() + public override void HandleMouseEvent(MouseEvent mouseEvent) { - OnClick?.Invoke(this, EventArgs.Empty); + if (mouseEvent.Type == MouseEventType.ButtonDown && mouseEvent.Button == MouseButton.Left) + { + OnClick?.Invoke(this, EventArgs.Empty); + } } } \ No newline at end of file diff --git a/UI/GUI/UIEelements/Controls/CheckBox.cs b/UI/GUI/UIEelements/Controls/CheckBox.cs index d38fe5b..76ef247 100644 --- a/UI/GUI/UIEelements/Controls/CheckBox.cs +++ b/UI/GUI/UIEelements/Controls/CheckBox.cs @@ -27,4 +27,12 @@ public class CheckBox() : Control("CheckBox") get; set => SetProperty(nameof(TextColor), ref field, value); } = Color.White; + + public override void HandleMouseEvent(MouseEvent mouseEvent) + { + if (mouseEvent.Type == MouseEventType.ButtonDown && mouseEvent.Button == MouseButton.Left) + { + IsChecked = !IsChecked; + } + } } \ No newline at end of file diff --git a/UI/GUI/Windows/Window.cs b/UI/GUI/Windows/Window.cs index d6c05f6..4efe297 100644 --- a/UI/GUI/Windows/Window.cs +++ b/UI/GUI/Windows/Window.cs @@ -107,9 +107,9 @@ public sealed class Window(string title, int processId, int id, IRenderSource re uiElements.Add(uiElementId, uiElement); } - lock (controlsLock) + if (uiElement is Control control) { - if (uiElement is Control control) + lock (controlsLock) { controls.Add(uiElementId, control); } diff --git a/UI/GUI/Windows/WindowManager.cs b/UI/GUI/Windows/WindowManager.cs index 3bbba9f..0df79e1 100644 --- a/UI/GUI/Windows/WindowManager.cs +++ b/UI/GUI/Windows/WindowManager.cs @@ -57,20 +57,20 @@ public static class WindowManager activeInteractWindow = null; } - wasLeftButtonDown = leftButtonDown; - wasRightButtonDown = rightButtonDown; - wasMiddleButtonDown = middleButtonDown; - while (KeyboardManager.TryReadKey(out KeyEvent? keyEvent) && keyEvent is not null) { focusedWindow?.HandleKeyEvent(keyEvent); } - if (focusedWindow is not null && activeInteractWindow is null) + if (focusedWindow is not null) { DispatchMouseEvents(focusedWindow, pointerPosition); } + wasLeftButtonDown = leftButtonDown; + wasRightButtonDown = rightButtonDown; + wasMiddleButtonDown = middleButtonDown; + CanvasRenderSource.CompositeAndDisplay(canvas, pointerPosition); lastPointerPosition = pointerPosition;