From d56f214a449b1d5e6c89835854d044ae4bc3da3f Mon Sep 17 00:00:00 2001 From: Bobby Lucero Date: Mon, 7 Jul 2025 15:45:07 -0400 Subject: [PATCH] MuInput class --- MicroUI.cs/MicroUI.cs | 128 +++++++++++++++++++++++++++++------------- 1 file changed, 89 insertions(+), 39 deletions(-) diff --git a/MicroUI.cs/MicroUI.cs b/MicroUI.cs/MicroUI.cs index 7c794d1..6ddf723 100644 --- a/MicroUI.cs/MicroUI.cs +++ b/MicroUI.cs/MicroUI.cs @@ -419,45 +419,6 @@ namespace MicroUI public char[] inputText = new char[32]; } - public static class MuPool - { - public static int Get(MuContext ctx, MuPoolItem[] items, uint id) - { - for (int i = 0; i < items.Length; i++) - { - if (items[i].Id == id) - { - return i; - } - } - return -1; - } - - public static int Init(MuContext ctx, MuPoolItem[] items, uint id) - { - int n = -1; - int f = ctx.Frame; - // Find the item with the oldest last_update - for (int i = 0; i < items.Length; i++) - { - if (items[i].LastUpdate < f) - { - f = items[i].LastUpdate; - n = i; - } - } - MuAssert.Expect(n > -1); - items[n].Id = id; - Update(ctx, items, n); - return n; - } - - public static void Update(MuContext ctx, MuPoolItem[] items, int idx) - { - items[idx].LastUpdate = ctx.Frame; - } - } - public static class MuAssert { public static bool TestMode { get; set; } = false; @@ -825,4 +786,93 @@ namespace MicroUI } + + public static class MuPool + { + public static int Get(MuContext ctx, MuPoolItem[] items, uint id) + { + for (int i = 0; i < items.Length; i++) + { + if (items[i].Id == id) + { + return i; + } + } + return -1; + } + + public static int Init(MuContext ctx, MuPoolItem[] items, uint id) + { + int n = -1; + int f = ctx.Frame; + // Find the item with the oldest last_update + for (int i = 0; i < items.Length; i++) + { + if (items[i].LastUpdate < f) + { + f = items[i].LastUpdate; + n = i; + } + } + MuAssert.Expect(n > -1); + items[n].Id = id; + Update(ctx, items, n); + return n; + } + + public static void Update(MuContext ctx, MuPoolItem[] items, int idx) + { + items[idx].LastUpdate = ctx.Frame; + } + } + + public static class MuInput + { + public static void MouseMove(MuContext ctx, int x, int y) + { + ctx.MousePos = new MuVec2(x, y); + } + + public static void MouseDown(MuContext ctx, int x, int y, int btn) + { + MouseMove(ctx, x, y); + ctx.MouseDown |= btn; + ctx.MousePressed |= btn; + } + + public static void MouseUp(MuContext ctx, int x, int y, int btn) + { + MouseMove(ctx, x, y); + ctx.MouseDown &= ~btn; + } + + public static void Scroll(MuContext ctx, int x, int y) + { + ctx.ScrollDelta = new MuVec2(ctx.ScrollDelta.X + x, ctx.ScrollDelta.Y + y); + } + + public static void KeyDown(MuContext ctx, int key) + { + ctx.KeyPressed |= key; + ctx.KeyDown |= key; + } + + public static void KeyUp(MuContext ctx, int key) + { + ctx.KeyDown &= ~key; + } + + public static void InputText(MuContext ctx, string text) + { + int len = ctx.inputText.Length; + int size = text.Length; + if (len + size > ctx.inputText.Length) + throw new Exception("Input text buffer overflow"); + // Copy text into the buffer (simple version) + for (int i = 0; i < size && i < ctx.inputText.Length; i++) + { + ctx.inputText[i] = text[i]; + } + } + } } \ No newline at end of file