added mouse wheel support
This commit is contained in:
@@ -4,10 +4,6 @@ bool tim_is_event_key(TimEventType type, TimKey key) {
|
||||
return tim->event.type == type && tim->event.key == key;
|
||||
}
|
||||
|
||||
bool tim_is_key_press(TimKey key) {
|
||||
return tim_is_event_key(TimEvent_Key, key);
|
||||
}
|
||||
|
||||
bool tim_is_mouse_over(TimRect r) {
|
||||
i32 x = tim->event.x;
|
||||
i32 y = tim->event.y;
|
||||
|
||||
38
src/unix.c
38
src/unix.c
@@ -1,3 +1,12 @@
|
||||
// Enable POSIX 2004 definitions.
|
||||
// Required to use clock_gettime, localtime_r, gmtime_r in ISO C.
|
||||
#ifndef _XOPEN_SOURCE
|
||||
#define _XOPEN_SOURCE 600
|
||||
#endif
|
||||
// Enable cfmakeraw()
|
||||
#ifndef _DEFAULT_SOURCE
|
||||
#define _DEFAULT_SOURCE
|
||||
#endif
|
||||
#include "tim.h"
|
||||
|
||||
#ifdef TIM_UNIX
|
||||
@@ -62,7 +71,7 @@ void tim_reset_terminal(void) {
|
||||
}
|
||||
|
||||
// parse input stored in e->s
|
||||
bool parse_input(event* restrict e, i32 n) {
|
||||
bool parse_input(TimEvent* restrict e, i32 n) {
|
||||
char* s = e->s;
|
||||
|
||||
if (n == 1 || s[0] != 27) {
|
||||
@@ -76,14 +85,23 @@ bool parse_input(event* restrict e, i32 n) {
|
||||
// sgr mouse sequence
|
||||
e->type = TimEvent_Mouse;
|
||||
i32 btn = strtol(s + 3, &s, 10);
|
||||
e->x = strtol(s + 1, &s, 10) - 1;
|
||||
e->y = strtol(s + 1, &s, 10) - 1;
|
||||
if (btn == 0 && s[0] == 'M') {
|
||||
// left button pressed
|
||||
e->key = TimKey_MouseButtonLeft;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
e->x = strtol(s + 1, &s, 10);
|
||||
e->y = strtol(s + 1, &s, 10);
|
||||
// coordinates start from 1
|
||||
if(e->x > 0) e->x--;
|
||||
if(e->y > 0) e->y--;
|
||||
// invalid sequence end
|
||||
if (s[0] != 'M')
|
||||
return false;
|
||||
|
||||
switch(btn){
|
||||
case 0: e->key = TimKey_MouseButtonLeft; break;
|
||||
case 64: e->key = TimKey_MouseScrollUp; break;
|
||||
case 65: e->key = TimKey_MouseScrollDown; break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
struct {char s[4]; i32 k;} key_table[] = {
|
||||
@@ -120,7 +138,7 @@ bool parse_input(event* restrict e, i32 n) {
|
||||
}
|
||||
|
||||
void tim_read_event(i32 timeout_ms) {
|
||||
event* e = &tim->event;
|
||||
TimEvent* e = &tim->event;
|
||||
|
||||
struct pollfd pfd[2] = {
|
||||
{.fd = tim->signal_pipe[0], .events = POLLIN},
|
||||
|
||||
@@ -140,9 +140,16 @@ void tim_read_event(i32 timeout_ms) {
|
||||
}
|
||||
|
||||
case MOUSE_EVENT: {
|
||||
bool wheel = rec.Event.MouseEvent.dwEventFlags & MOUSE_WHEELED;
|
||||
if(wheel){
|
||||
i16 scroll_value = HIWORD(rec.Event.MouseEvent.dwButtonState);
|
||||
e->type = TimEvent_Mouse;
|
||||
e->key = scroll_value > 0 ? TimKey_MouseScrollUp : TimKey_MouseScrollDown;
|
||||
return;
|
||||
}
|
||||
|
||||
bool move = rec.Event.MouseEvent.dwEventFlags & ~DOUBLE_CLICK;
|
||||
bool left = rec.Event.MouseEvent.dwButtonState &
|
||||
FROM_LEFT_1ST_BUTTON_PRESSED;
|
||||
bool left = rec.Event.MouseEvent.dwButtonState & FROM_LEFT_1ST_BUTTON_PRESSED;
|
||||
if (move || !left) {
|
||||
// ignore move events and buttons other than left
|
||||
continue;
|
||||
@@ -150,8 +157,8 @@ void tim_read_event(i32 timeout_ms) {
|
||||
tim_update_screen_size(); // workaround, see WINDOW_BUFFER_SIZE_EVENT
|
||||
e->type = TimEvent_Mouse;
|
||||
e->key = TimKey_MouseButtonLeft;
|
||||
e->x = rec.Event.MouseEvent.dwMousePosition.X - tim->window.Left;
|
||||
e->y = rec.Event.MouseEvent.dwMousePosition.Y - tim->window.Top;
|
||||
e->x = rec.Event.MouseEvent.dwMousePosition.X - tim->window.Left;
|
||||
e->y = rec.Event.MouseEvent.dwMousePosition.Y - tim->window.Top;
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user