aboutsummaryrefslogtreecommitdiff
path: root/pugl_cairo_test.c
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2014-08-27 23:32:26 +0000
committerDavid Robillard <d@drobilla.net>2014-08-27 23:32:26 +0000
commitd0c878fe0e496083a1e892795c23a1ac939714a9 (patch)
tree6a70e0fbd68726357a8b008ecd2bba1d3062e6b1 /pugl_cairo_test.c
parentb4dbb3616865cd236cda4f2e4ac806b3b4840b29 (diff)
downloadcanvas.lv2-d0c878fe0e496083a1e892795c23a1ac939714a9.tar.xz
Event-based dispatch.
Diffstat (limited to 'pugl_cairo_test.c')
-rw-r--r--pugl_cairo_test.c49
1 files changed, 28 insertions, 21 deletions
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);