Skip to content

Commit

Permalink
Fix sticky keys on input::clear
Browse files Browse the repository at this point in the history
  • Loading branch information
skejeton committed Jul 10, 2024
1 parent cc7f763 commit b73da33
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 10 deletions.
26 changes: 25 additions & 1 deletion samples/input/keyboard/main.um
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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++
}
Expand All @@ -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)
Expand All @@ -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
})
}
4 changes: 3 additions & 1 deletion src/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
10 changes: 5 additions & 5 deletions src/staembed.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions src/tophat.h
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
6 changes: 3 additions & 3 deletions umka/ui.um
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down

0 comments on commit b73da33

Please sign in to comment.