2019-12-11 12:05:53 -07:00
|
|
|
# Demo script that implements a simple drawing program
|
|
|
|
import upiwin
|
|
|
|
|
|
|
|
# Save off some color values.
|
|
|
|
WHITE = upiwin.rgb(255, 255, 255)
|
|
|
|
LTGRAY = upiwin.rgb(204, 204, 204)
|
|
|
|
YELLOW = upiwin.rgb(255, 255, 0)
|
|
|
|
|
|
|
|
hdc = upiwin.DevCtxt(type='screen')
|
|
|
|
|
|
|
|
# divide the screen into "drawing" and "command" areas
|
2019-12-11 12:12:33 -07:00
|
|
|
screen_rect = hdc.get_clip_rect()
|
|
|
|
drawing_rect = (screen_rect[0], screen_rect[1], screen_rect[2] - 60, screen_rect[3])
|
|
|
|
command_rect = (drawing_rect[2], screen_rect[1], screen_rect[2], screen_rect[3])
|
2019-12-11 12:05:53 -07:00
|
|
|
|
|
|
|
# further divide up the "command" area
|
2019-12-11 12:12:33 -07:00
|
|
|
cmd1_rect = (command_rect[0], command_rect[1], command_rect[2], 60)
|
|
|
|
cmd2_rect = (command_rect[0], 60, command_rect[2], 120)
|
|
|
|
cmd3_rect = (command_rect[0], 120, command_rect[2], 180)
|
2019-12-11 12:14:55 -07:00
|
|
|
cmd4_rect = (command_rect[0], 180, command_rect[2], command_rect[3])
|
2019-12-11 12:05:53 -07:00
|
|
|
|
|
|
|
def point_in_rect(rect, x, y):
|
|
|
|
return (x >= rect[0]) and (x < rect[2]) and (y >= rect[1]) and (y < rect[3])
|
|
|
|
|
|
|
|
|
2019-12-11 12:26:05 -07:00
|
|
|
# --- 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)
|
2019-12-11 12:05:53 -07:00
|
|
|
|
|
|
|
# --- Initialize and start message loop ---
|
|
|
|
|
|
|
|
# Draw the basic layout.
|
2019-12-11 12:13:35 -07:00
|
|
|
hdc.text_color = LTGRAY
|
2019-12-11 12:05:53 -07:00
|
|
|
hdc.rectangle(cmd1_rect[0], cmd1_rect[1], cmd1_rect[2], cmd1_rect[3])
|
|
|
|
hdc.rectangle(cmd2_rect[0], cmd2_rect[1], cmd2_rect[2], cmd2_rect[3])
|
|
|
|
hdc.rectangle(cmd3_rect[0], cmd3_rect[1], cmd3_rect[2], cmd3_rect[3])
|
|
|
|
hdc.rectangle(cmd4_rect[0], cmd4_rect[1], cmd4_rect[2], cmd4_rect[3])
|
|
|
|
|
|
|
|
# Main message loop
|
|
|
|
msg = {}
|
|
|
|
while upiwin.get_message(msg):
|
|
|
|
if msg['message'] == upiwin.WM_TOUCHCLICK:
|
2019-12-11 12:26:05 -07:00
|
|
|
on_touchclick(msg['attrs'][0], msg['attrs'][1])
|
2019-12-11 12:05:53 -07:00
|
|
|
elif msg['message'] == upiwin.WM_HWBUTTONCLICK:
|
2019-12-11 12:26:05 -07:00
|
|
|
on_button_click(msg['attrs'][0])
|