From 46213020ddc9d263ce280bfec76093074bf4b925 Mon Sep 17 00:00:00 2001 From: Amy Bowersox Date: Wed, 11 Dec 2019 12:26:05 -0700 Subject: [PATCH] separate out the message handlers for clarity --- scripts/demo1.py | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/scripts/demo1.py b/scripts/demo1.py index 5c83439..fbdf056 100755 --- a/scripts/demo1.py +++ b/scripts/demo1.py @@ -23,7 +23,22 @@ def point_in_rect(rect, x, y): return (x >= rect[0]) and (x < rect[2]) and (y >= rect[1]) and (y < rect[3]) +# --- Message handlers --- +def on_touchclick(x, y): + print("Click at {0},{1}".format(x, y)) + if point_in_rect(cmd1_rect, x, y): + print("Click command 1") + elif point_in_rect(cmd2_rect, x, y): + print("Click command 2") + elif point_in_rect(cmd3_rect, x, y): + print("Click command 3") + elif point_in_rect(cmd4_rect, x, y): + print("Click command 4") + +def on_button_click(button): + if button == 4: # Button 4 = Exit app + upiwin.post_quit_message(0) # --- Initialize and start message loop --- @@ -38,15 +53,6 @@ hdc.rectangle(cmd4_rect[0], cmd4_rect[1], cmd4_rect[2], cmd4_rect[3]) msg = {} while upiwin.get_message(msg): if msg['message'] == upiwin.WM_TOUCHCLICK: - print("Click at {0},{1}".format(msg['attrs'][0], msg['attrs'][1]) - if point_in_rect(cmd1_rect, msg['attrs'][0], msg['attrs'][1])): - print("Click command 1") - elif point_in_rect(cmd2_rect, msg['attrs'][0], msg['attrs'][1])): - print("Click command 2") - elif point_in_rect(cmd3_rect, msg['attrs'][0], msg['attrs'][1])): - print("Click command 3") - elif point_in_rect(cmd4_rect, msg['attrs'][0], msg['attrs'][1])): - print("Click command 4") + on_touchclick(msg['attrs'][0], msg['attrs'][1]) elif msg['message'] == upiwin.WM_HWBUTTONCLICK: - if msg['attrs'][0] == 4: # Button 4 = Exit app - upiwin.post_quit_message(0) + on_button_click(msg['attrs'][0])