summaryrefslogtreecommitdiff
path: root/kon
diff options
context:
space:
mode:
authorKonAki <konaki@anokonaki.org>2026-10-09 17:38:43 +0800
committerKonAki <konaki@anokonaki.org>2026-10-09 17:38:43 +0800
commit3e4acddeb4df4d21ad89b8926aa2eafc634daa25 (patch)
treecf86972b240f2ea8752bd726cbed328dcd7de265 /kon
parentaf7dafe1d79e39ba2d51a6e3f10990d9e8df3735 (diff)
Add mouse wheel events and wheel zoom in modelview
Diffstat (limited to 'kon')
-rw-r--r--kon/kondebug.c6
-rw-r--r--kon/konwinlib.h49
2 files changed, 54 insertions, 1 deletions
diff --git a/kon/kondebug.c b/kon/kondebug.c
index a905adc..a68ebcf 100644
--- a/kon/kondebug.c
+++ b/kon/kondebug.c
@@ -541,6 +541,12 @@ int kon_debugFeedEvent(kon_debug_t *d, const kon_event_t *e)
if (e->codepoint >= 32 && e->codepoint <= 126) kon_debugInsert_(d, (char)e->codepoint);
return 1;
}
+ if (e->type == KON_EVENT_MOUSE_WHEEL) {
+ d->scroll += e->wheel * 3;
+ if (d->scroll < 0) d->scroll = 0;
+ if (d->scroll > d->logCount - 1) d->scroll = d->logCount > 0 ? d->logCount - 1 : 0;
+ return 1;
+ }
if (e->type == KON_EVENT_KEY_UP) return 1;
if (e->type != KON_EVENT_KEY_DOWN) return 0;
diff --git a/kon/konwinlib.h b/kon/konwinlib.h
index 6e2ad5a..0a1f365 100644
--- a/kon/konwinlib.h
+++ b/kon/konwinlib.h
@@ -62,7 +62,8 @@ typedef enum kon_eventType {
KON_EVENT_MOUSE_DOWN,
KON_EVENT_MOUSE_UP,
KON_EVENT_MOUSE_MOVE,
- KON_EVENT_TEXT
+ KON_EVENT_TEXT,
+ KON_EVENT_MOUSE_WHEEL
} kon_eventType_t;
typedef enum kon_mouseButton {
@@ -110,6 +111,7 @@ typedef struct kon_event {
unsigned int codepoint; /* unicode codepoint, only set for KON_EVENT_TEXT */
int mouseX, mouseY;
kon_mouseButton_t mouseButton;
+ int wheel; /* only set for KON_EVENT_MOUSE_WHEEL, 1 for each notch away from you and -1 towards you */
} kon_event_t;
kon_window_t *kon_createWindow(const char *title, int x, int y, int width, int height, kon_windowFlags_t flags);
@@ -380,6 +382,21 @@ static EM_BOOL kon_mouseCallback_(int eventType, const EmscriptenMouseEvent *e,
return EM_TRUE;
}
+static EM_BOOL kon_wheelCallback_(int eventType, const EmscriptenWheelEvent *e, void *userData) {
+ kon_window_t *window = (kon_window_t *)userData;
+ kon_event_t ev = {0};
+
+ (void)eventType;
+ if (e->deltaY == 0.0) return EM_FALSE;
+ ev.type = KON_EVENT_MOUSE_WHEEL;
+ ev.wheel = e->deltaY < 0.0 ? 1 : -1;
+ ev.mouseX = (int)e->mouse.targetX;
+ ev.mouseY = (int)e->mouse.targetY;
+ kon_pushEvent_(&window->queue, &ev);
+
+ return EM_TRUE;
+}
+
kon_window_t *kon_createWindow(const char *title, int x, int y, int width, int height, kon_windowFlags_t flags) {
(void)x; (void)y; (void)flags;
@@ -406,6 +423,7 @@ kon_window_t *kon_createWindow(const char *title, int x, int y, int width, int h
emscripten_set_mousedown_callback(window->canvasID, window, EM_FALSE, kon_mouseCallback_);
emscripten_set_mouseup_callback(window->canvasID, window, EM_FALSE, kon_mouseCallback_);
emscripten_set_mousemove_callback(window->canvasID, window, EM_FALSE, kon_mouseCallback_);
+ emscripten_set_wheel_callback(window->canvasID, window, EM_FALSE, kon_wheelCallback_);
return window;
}
@@ -419,6 +437,7 @@ void kon_destroyWindow(kon_window_t *window) {
emscripten_set_mousedown_callback(window->canvasID, NULL, EM_FALSE, NULL);
emscripten_set_mouseup_callback(window->canvasID, NULL, EM_FALSE, NULL);
emscripten_set_mousemove_callback(window->canvasID, NULL, EM_FALSE, NULL);
+ emscripten_set_wheel_callback(window->canvasID, NULL, EM_FALSE, NULL);
free(window);
}
@@ -948,6 +967,17 @@ static void kon_pumpX11_(kon_window_t *window) {
case Button1: ev.mouseButton = KON_MOUSE_LEFT; break;
case Button2: ev.mouseButton = KON_MOUSE_MIDDLE; break;
case Button3: ev.mouseButton = KON_MOUSE_RIGHT; break;
+ case Button4:
+ case Button5:
+ /* the wheel shows up as button 4 and 5 being pressed, the release is not interesting */
+ if (xev.type == ButtonPress) {
+ ev.type = KON_EVENT_MOUSE_WHEEL;
+ ev.wheel = xev.xbutton.button == Button4 ? 1 : -1;
+ ev.mouseX = xev.xbutton.x;
+ ev.mouseY = xev.xbutton.y;
+ kon_pushEvent_(&window->queue, &ev);
+ }
+ continue;
default: continue;
}
ev.type = (xev.type == ButtonPress) ? KON_EVENT_MOUSE_DOWN : KON_EVENT_MOUSE_UP;
@@ -1635,6 +1665,23 @@ LRESULT CALLBACK kon_wndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
kon_pushEvent_(&window->queue, &ev);
}
break;
+ case WM_MOUSEWHEEL:
+ if (window) {
+ kon_event_t ev = {0};
+ POINT point;
+ int delta = (int)(short)HIWORD(wParam);
+
+ if (delta == 0) break;
+ point.x = (int)(short)LOWORD(lParam);
+ point.y = (int)(short)HIWORD(lParam);
+ ScreenToClient(hwnd, &point);
+ ev.type = KON_EVENT_MOUSE_WHEEL;
+ ev.wheel = delta > 0 ? 1 : -1;
+ ev.mouseX = (int)point.x;
+ ev.mouseY = (int)point.y;
+ kon_pushEvent_(&window->queue, &ev);
+ }
+ break;
case WM_MOUSEMOVE:
if (window) {
kon_event_t ev = {0};