From d4b2829de83ca5f0efd3d1ee2683ee400e63ffe2 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sun, 29 Apr 2012 06:08:19 +0000 Subject: Scroll API. --- pugl/pugl.h | 7 +++++++ pugl/pugl_internal.h | 7 +++++++ pugl/pugl_x11.c | 17 ++++++++++++++++- pugl_test.c | 14 ++++++++++++-- 4 files changed, 42 insertions(+), 3 deletions(-) diff --git a/pugl/pugl.h b/pugl/pugl.h index a764620..f173525 100644 --- a/pugl/pugl.h +++ b/pugl/pugl.h @@ -55,6 +55,7 @@ typedef void (*PuglMouseFunc)(PuglWindow* handle, int button, bool down, int x, int y); typedef void (*PuglReshapeFunc)(PuglWindow* handle, int width, int height); +typedef void (*PuglScrollFunc)(PuglWindow* handle, int dx, int dy); /** Create a new GL window. @@ -119,6 +120,12 @@ puglSetMotionFunc(PuglWindow* window, PuglMotionFunc motionFunc); void puglSetMouseFunc(PuglWindow* window, PuglMouseFunc mouseFunc); +/** + Set the function to call on scroll events. +*/ +void +puglSetScrollFunc(PuglWindow* window, PuglScrollFunc scrollFunc); + /** Set the function to call when the window size changes. */ diff --git a/pugl/pugl_internal.h b/pugl/pugl_internal.h index 8a7b217..3685522 100644 --- a/pugl/pugl_internal.h +++ b/pugl/pugl_internal.h @@ -35,6 +35,7 @@ struct PuglWindowImpl { PuglMotionFunc motionFunc; PuglMouseFunc mouseFunc; PuglReshapeFunc reshapeFunc; + PuglScrollFunc scrollFunc; PuglPlatformData* impl; @@ -90,3 +91,9 @@ puglSetReshapeFunc(PuglWindow* window, PuglReshapeFunc reshapeFunc) { window->reshapeFunc = reshapeFunc; } + +void +puglSetScrollFunc(PuglWindow* window, PuglScrollFunc scrollFunc) +{ + window->scrollFunc = scrollFunc; +} diff --git a/pugl/pugl_x11.c b/pugl/pugl_x11.c index d8685b2..735cdb1 100644 --- a/pugl/pugl_x11.c +++ b/pugl/pugl_x11.c @@ -238,8 +238,23 @@ puglProcessEvents(PuglWindow* win) } break; case ButtonPress: + if (event.xbutton.button >= 4 && event.xbutton.button <= 7) { + if (win->scrollFunc) { + int dx = 0, dy = 0; + switch (event.xbutton.button) { + case 4: dy = 1; break; + case 5: dy = -1; break; + case 6: dx = -1; break; + case 7: dx = 1; break; + } + win->scrollFunc(win, dx, dy); + } + break; + } + // nobreak case ButtonRelease: - if (win->mouseFunc) { + if (win->mouseFunc && + (event.xbutton.button < 4 || event.xbutton.button > 7)) { win->mouseFunc(win, event.xbutton.button, event.type == ButtonPress, event.xbutton.x, event.xbutton.y); diff --git a/pugl_test.c b/pugl_test.c index 03afac0..57b9e5e 100644 --- a/pugl_test.c +++ b/pugl_test.c @@ -34,6 +34,7 @@ static int quit = 0; static float xAngle = 0.0f; static float yAngle = 0.0f; +static float dist = 10.0f; #define KEY_ESCAPE 27 @@ -43,7 +44,7 @@ onDisplay(PuglWindow* win) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); - glTranslatef(0.0f, 0.0f, -10.0f); + glTranslatef(0.0f, 0.0f, dist * -1); glRotatef(xAngle, 0.0f, 1.0f, 0.0f); glRotatef(yAngle, 1.0f, 0.0f, 0.0f); @@ -102,12 +103,20 @@ onMotion(PuglWindow* win, int x, int y) } static void -onMouse(PuglWindow* handle, int button, bool press, int x, int y) +onMouse(PuglWindow* win, int button, bool press, int x, int y) { fprintf(stderr, "Mouse %d %s at %d,%d\n", button, press ? "down" : "up", x, y); } +static void +onScroll(PuglWindow* win, int dx, int dy) +{ + fprintf(stderr, "Scroll %d %d\n", dx, dy); + dist += dy / 4.0f; + puglPostRedisplay(win); +} + static void onClose(PuglWindow* win) { @@ -122,6 +131,7 @@ main(int argc, char** argv) puglSetKeyboardFunc(win, onKeyboard); puglSetMotionFunc(win, onMotion); puglSetMouseFunc(win, onMouse); + puglSetScrollFunc(win, onScroll); puglSetDisplayFunc(win, onDisplay); puglSetCloseFunc(win, onClose); -- cgit v1.2.3