From b73da33daf17202eff12749cdd956891657c62f8 Mon Sep 17 00:00:00 2001 From: skejeton Date: Wed, 10 Jul 2024 12:29:56 -0300 Subject: [PATCH] Fix sticky keys on `input::clear` --- samples/input/keyboard/main.um | 26 +++++++++++++++++++++++++- src/input.c | 4 +++- src/staembed.c | 10 +++++----- src/tophat.h | 2 ++ umka/ui.um | 6 +++--- 5 files changed, 38 insertions(+), 10 deletions(-) diff --git a/samples/input/keyboard/main.um b/samples/input/keyboard/main.um index a4917d1..6b1d47b 100644 --- a/samples/input/keyboard/main.um +++ b/samples/input/keyboard/main.um @@ -14,8 +14,11 @@ pressCount := 0 repeatCount := 0 releaseCount := 0 +mousePressCount := 0 +mouseReleaseCount := 0 + fn init*() { - window::setup("Keyboard Input Sample", 640, 480) + window::setup("Keyboard Input Sample", 640, 640) window::onFrame.register({ p := th::Vf2{ 5, 5 } @@ -52,6 +55,14 @@ fn init*() { p.y += 44 + if input::isPressed(.mouse1) { + canvas::drawText("LMB IS PRESSED", p, th::green, 4) + } else { + canvas::drawText("LMB IS NOT PRESSED", p, th::red, 4) + } + + p.y += 44 + if input::isJustPressed(.shift) { fakePressCount++ } @@ -76,6 +87,14 @@ fn init*() { releaseCount++ } + if input::isJustPressed(.mouse1) { + mousePressCount++ + } + + if input::isJustReleased(.mouse1) { + mouseReleaseCount++ + } + canvas::drawText(sprintf("SHIFT PRESS COUNT: %4d", fakePressCount), p, th::black, 4) p.y += 44 canvas::drawText(sprintf("SHIFT REPEAT COUNT: %4d", fakeRepeatCount), p, th::black, 4) @@ -89,5 +108,10 @@ fn init*() { p.y += 44 canvas::drawText(sprintf("SPACE RELEASE COUNT: %4d", releaseCount), p, th::black, 4) p.y += 44 + + canvas::drawText(sprintf("LMB PRESS COUNT: %4d", mousePressCount), p, th::black, 4) + p.y += 44 + canvas::drawText(sprintf("LMB RELEASE COUNT: %4d", mouseReleaseCount), p, th::black, 4) + p.y += 44 }) } diff --git a/src/input.c b/src/input.c index 5735f8f..fcda166 100644 --- a/src/input.c +++ b/src/input.c @@ -15,14 +15,16 @@ void th_input_key(int keycode, int bDown) { if (!bDown) { - thg->just_released[keycode] = thg->pressed[keycode]; + thg->just_released[keycode] = thg->internal_pressed_state[keycode]; thg->pressed[keycode] = 0; + thg->internal_pressed_state[keycode] = 0; thg->just_pressed[keycode] = 0; return; } if (!thg->pressed[keycode]) { thg->pressed[keycode] = 1; + thg->internal_pressed_state[keycode] = 1; thg->just_pressed[keycode] = 1; return; } diff --git a/src/staembed.c b/src/staembed.c index 6762755..f76f167 100644 --- a/src/staembed.c +++ b/src/staembed.c @@ -3231,8 +3231,8 @@ const char *th_em_modulesrc[] = { "// Runs the evaluation phase on `layout`.\n" "fn (this: ^Gui) eval*(layout: LayoutFn) {\n" "//~~\n" -"\tif input::isJustPressed(input::Key.mouse1) { this.m1Pressed = true }\n" -"\tif input::isJustReleased(input::Key.mouse1) { this.m1Pressed = false }\n" +"\tif input::isJustPressed(input::Key.mouse1) { this.m1Pressed = true; printf(\"Press\\n\") }\n" +"\tif input::isJustReleased(input::Key.mouse1) { this.m1Pressed = false; printf(\"Release\\n\") }\n" "\tif input::isJustPressed(input::Key.mouse2) { this.m2Pressed = true }\n" "\tif input::isJustReleased(input::Key.mouse2) { this.m2Pressed = false }\n" "\tif input::isJustPressed(input::Key.mouse3) { this.m3Pressed = true }\n" @@ -3243,9 +3243,9 @@ const char *th_em_modulesrc[] = { "\tlayout(this)\n" "\n" "\tif this.hover(this.container[0].getDims()) {\n" -"\t\t\tinput::clear(input::Key.mouse1)\n" -"\t\t\tinput::clear(input::Key.mouse2)\n" -"\t\t\tinput::clear(input::Key.mouse3)\n" +"\t\tinput::clear(input::Key.mouse1)\n" +"\t\tinput::clear(input::Key.mouse2)\n" +"\t\tinput::clear(input::Key.mouse3)\n" "\t}\n" "}\n" "\n" diff --git a/src/tophat.h b/src/tophat.h index d61f7a3..92ff0bf 100644 --- a/src/tophat.h +++ b/src/tophat.h @@ -285,6 +285,8 @@ typedef struct void *umka; uu pressed[512]; + // since `input::clear` clears `pressed`, there will be no way to detect releases. + uu internal_pressed_state[512]; uu just_pressed[512]; uu just_released[512]; uu press_repeat[512]; diff --git a/umka/ui.um b/umka/ui.um index 346075f..21250f0 100644 --- a/umka/ui.um +++ b/umka/ui.um @@ -214,9 +214,9 @@ fn (this: ^Gui) eval*(layout: LayoutFn) { layout(this) if this.hover(this.container[0].getDims()) { - input::clear(input::Key.mouse1) - input::clear(input::Key.mouse2) - input::clear(input::Key.mouse3) + input::clear(input::Key.mouse1) + input::clear(input::Key.mouse2) + input::clear(input::Key.mouse3) } }