From d0c878fe0e496083a1e892795c23a1ac939714a9 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Wed, 27 Aug 2014 23:32:26 +0000 Subject: Event-based dispatch. --- pugl_cairo_test.c | 49 ++++++++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 21 deletions(-) (limited to 'pugl_cairo_test.c') diff --git a/pugl_cairo_test.c b/pugl_cairo_test.c index 6ff5bd5..bd97668 100644 --- a/pugl_cairo_test.c +++ b/pugl_cairo_test.c @@ -63,7 +63,7 @@ roundedBox(cairo_t* cr, double x, double y, double w, double h) } static void -drawButton(cairo_t* cr, const Button* but) +buttonDraw(cairo_t* cr, const Button* but) { // Draw base if (but->pressed) { @@ -90,39 +90,47 @@ drawButton(cairo_t* cr, const Button* but) cairo_show_text(cr, but->label); } +static bool +buttonTouches(const Button* but, double x, double y) +{ + return (x >= toggle_button.x && x <= toggle_button.x + toggle_button.w && + y >= toggle_button.y && y <= toggle_button.y + toggle_button.h); +} + static void onDisplay(PuglView* view) { cairo_t* cr = puglGetContext(view); - drawButton(cr, &toggle_button); + buttonDraw(cr, &toggle_button); } static void -onKeyboard(PuglView* view, bool press, uint32_t key) +onClose(PuglView* view) { - if (key == 'q' || key == 'Q' || key == PUGL_CHAR_ESCAPE) { - quit = 1; - } + quit = 1; } static void -onMouse(PuglView* view, int button, bool press, int x, int y) +onEvent(PuglView* view, const PuglEvent* event) { - if (press && - x >= toggle_button.x && x <= toggle_button.x + toggle_button.w && - y >= toggle_button.y && y <= toggle_button.y + toggle_button.h) { - toggle_button.pressed = !toggle_button.pressed; - puglPostRedisplay(view); + switch (event->type) { + case PUGL_KEY_PRESS: + if (event->key.character == 'q' || + event->key.character == 'Q' || + event->key.character == PUGL_CHAR_ESCAPE) { + quit = 1; + } + break; + case PUGL_BUTTON_PRESS: + if (buttonTouches(&toggle_button, event->button.x, event->button.y)) { + toggle_button.pressed = !toggle_button.pressed; + puglPostRedisplay(view); + } + default: break; } } -static void -onClose(PuglView* view) -{ - quit = 1; -} - int main(int argc, char** argv) { @@ -148,10 +156,9 @@ main(int argc, char** argv) puglInitWindowSize(view, 512, 512); puglInitResizable(view, resizable); puglInitContextType(view, PUGL_CAIRO); - + puglIgnoreKeyRepeat(view, ignoreKeyRepeat); - puglSetKeyboardFunc(view, onKeyboard); - puglSetMouseFunc(view, onMouse); + puglSetEventFunc(view, onEvent); puglSetDisplayFunc(view, onDisplay); puglSetCloseFunc(view, onClose); -- cgit v1.2.3