Add initial implementation of mouse buttons

This commit is contained in:
Bad Diode 2022-03-05 20:01:18 +01:00
parent ce07c0539c
commit ea6b535a40
1 changed files with 29 additions and 5 deletions

View File

@ -38,9 +38,7 @@ time_elapsed(Time since){
typedef struct Mouse { typedef struct Mouse {
s32 x; s32 x;
s32 y; s32 y;
bool left; u8 buttons;
bool mid;
bool right;
bool update; bool update;
} Mouse; } Mouse;
@ -110,9 +108,26 @@ poll_mouse(void) {
in.mouse.y = CLAMP( in.mouse.y = CLAMP(
in.mouse.y + (s32)mouse_event.value, 0, (s32)screen_height); in.mouse.y + (s32)mouse_event.value, 0, (s32)screen_height);
} }
} else if (mouse_event.type == EV_KEY) {
switch (mouse_event.code) {
case BTN_LEFT: {
if (mouse_event.value == 1) {
in.mouse.buttons |= 0x01
} else {
in.mouse.buttons &= ~0x01
}
} break;
case BTN_RIGHT: {
if (mouse_event.value == 1) {
in.mouse.buttons |= 0x10
} else {
in.mouse.buttons &= ~0x10
}
} break;
default: break;
}
} }
in.mouse.update = true; in.mouse.update = true;
// TODO: Handle mouse keys
// DEBUG: // DEBUG:
// printf("MOUSE: (type: %d, code: %d, value: %d)\n", // printf("MOUSE: (type: %d, code: %d, value: %d)\n",
// mouse_event.type, mouse_event.code, mouse_event.value); // mouse_event.type, mouse_event.code, mouse_event.value);
@ -274,7 +289,16 @@ handle_keyboard(void) {
void void
handle_mouse(void) { handle_mouse(void) {
if (in.mouse.update) { if (in.mouse.update) {
// TODO: Handle mouse keys // Handle mouse keys.
devmouse->dat[6] = in.mouse.buttons;
if(in.mouse.buttons == 0x10 && (devmouse->dat[6] & 0x01)) {
devmouse->dat[7] = 0x01;
}
if(in.mouse.buttons == 0x01 && (devmouse->dat[6] & 0x10)) {
devmouse->dat[7] = 0x10;
}
// Handle mouse location.
mempoke16(devmouse->dat, 0x2, in.mouse.x); mempoke16(devmouse->dat, 0x2, in.mouse.x);
mempoke16(devmouse->dat, 0x4, in.mouse.y); mempoke16(devmouse->dat, 0x4, in.mouse.y);
uxn_eval(&u, mempeek16(devmouse->dat, 0)); uxn_eval(&u, mempeek16(devmouse->dat, 0));