upiwin/scripts/demo1.py

170 lines
5.0 KiB
Python
Raw Normal View History

# 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)
RED = upiwin.rgb(255, 0, 0)
GREEN = upiwin.rgb(0, 255, 0)
BLUE = upiwin.rgb(0, 0, 255)
CYAN = upiwin.rgb(0, 255, 255)
MAGENTA = upiwin.rgb(255, 0, 255)
YELLOW = upiwin.rgb(255, 255, 0)
# Get the stock bitmaps.
bmp_freehand = upiwin.Bitmap(stock='freehand')
bmp_line = upiwin.Bitmap(stock='line')
bmp_rect = upiwin.Bitmap(stock='rect')
bmp_fillrect = upiwin.Bitmap(stock='fillrect')
hdc = upiwin.DevCtxt(type='screen')
hdc_bits = upiwin.DevCtxt(type='memory')
# divide the screen into "drawing" and "command" areas
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])
# further divide up the "command" area
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])
def point_in_rect(rect, x, y):
return (x >= rect[0]) and (x < rect[2]) and (y >= rect[1]) and (y < rect[3])
# --- Color selections ---
color_list = [WHITE, RED, GREEN, BLUE, CYAN, MAGENTA, YELLOW]
current_color = 0
def draw_current_color():
hdc.text_color = color_list[current_color]
hdc.solid_rectangle(cmd2_rect[0] + 6, cmd2_rect[1] + 6, cmd2_rect[0] + 54, cmd2_rect[1] + 54)
def select_next_color():
2019-12-11 14:45:59 -07:00
global current_color
current_color += 1
if current_color == len(color_list):
current_color = 0
draw_current_color()
2019-12-11 12:46:26 -07:00
# --- Graphic feedback --
origin_x = 0
origin_y = 0
current_x = 0
current_y = 0
2019-12-11 14:52:32 -07:00
def freehand_draw(x, y, down, up):
global current_x, current_y
hdc.text_color = color_list[current_color]
hdc.move_to(current_x, current_y)
hdc.line_to(x, y)
current_x = x
current_y = y
def rubberband_rectangle(x, y, down, up):
global current_x, current_y
2019-12-11 14:52:32 -07:00
hdc.text_color = LTGRAY
2019-12-11 12:46:26 -07:00
hdc.rop2 = upiwin.R2_XORPEN
if not down:
hdc.rectangle(min(origin_x, current_x), min(origin_y, current_y), max(origin_x, current_x), max(origin_y, current_y))
current_x = x
current_y = y
2019-12-11 12:46:26 -07:00
if not up:
hdc.rectangle(min(origin_x, current_x), min(origin_y, current_y), max(origin_x, current_x), max(origin_y, current_y))
2019-12-11 12:59:45 -07:00
def rubberband_line(x, y, down, up):
global current_x, current_y
2019-12-11 14:52:32 -07:00
hdc.text_color = LTGRAY
2019-12-11 12:59:45 -07:00
hdc.rop2 = upiwin.R2_XORPEN
if not down:
hdc.move_to(origin_x, origin_y)
2019-12-11 13:00:38 -07:00
hdc.line_to(current_x, current_y)
2019-12-11 12:59:45 -07:00
current_x = x
current_y = y
if not up:
hdc.move_to(origin_x, origin_y)
2019-12-11 13:00:38 -07:00
hdc.line_to(current_x, current_y)
2019-12-11 12:46:26 -07:00
# --- Tool definitions ---
tool_list = [
2019-12-11 14:52:32 -07:00
{ 'icon': bmp_freehand, 'feedback': freehand_draw },
{ 'icon': bmp_line, 'feedback': rubberband_line },
{ 'icon': bmp_rect, 'feedback': rubberband_rectangle },
{ 'icon': bmp_fillrect, 'feedback': rubberband_rectangle }
]
2019-12-11 12:46:26 -07:00
current_tool = 0
2019-12-11 12:46:26 -07:00
def draw_current_tool():
hdc_bits.select_object(tool_list[current_tool]['icon'])
hdc.bitblt(cmd1_rect[0] + 6, cmd1_rect[1] + 6, cmd1_rect[0] + 54, cmd1_rect[1] + 54, hdc_bits, 0, 0, 0)
def select_next_tool():
2019-12-11 14:45:59 -07:00
global current_tool
current_tool += 1
if current_tool == len(tool_list):
current_tool = 0
draw_current_tool()
2019-12-11 12:46:26 -07:00
# --- Message handlers ---
2019-12-11 12:46:26 -07:00
def on_touchdown(x, y):
global origin_x, origin_y
2019-12-11 12:46:26 -07:00
if point_in_rect(drawing_rect, x, y):
origin_x = x
origin_y = y
2019-12-11 14:52:32 -07:00
tool_list[current_tool]['feedback'](x, y, True, False)
2019-12-11 12:46:26 -07:00
def on_touchmove(x, y):
if point_in_rect(drawing_rect, x, y):
2019-12-11 14:52:32 -07:00
tool_list[current_tool]['feedback'](x, y, False, False)
2019-12-11 12:46:26 -07:00
def on_touchup(x, y):
if point_in_rect(drawing_rect, x, y):
2019-12-11 14:52:32 -07:00
tool_list[current_tool]['feedback'](x, y, False, True)
2019-12-11 12:46:26 -07:00
def on_touchclick(x, y):
if point_in_rect(cmd1_rect, x, y):
select_next_tool()
elif point_in_rect(cmd2_rect, x, y):
select_next_color()
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 ---
# Draw the basic layout.
hdc.text_color = LTGRAY
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])
draw_current_tool()
draw_current_color()
# Main message loop
msg = {}
while upiwin.get_message(msg):
2019-12-11 12:46:26 -07:00
if msg['message'] == upiwin.WM_TOUCHDOWN:
on_touchdown(msg['attrs'][0], msg['attrs'][1])
if msg['message'] == upiwin.WM_TOUCHMOVE:
on_touchmove(msg['attrs'][0], msg['attrs'][1])
if msg['message'] == upiwin.WM_TOUCHUP:
on_touchup(msg['attrs'][0], msg['attrs'][1])
if msg['message'] == upiwin.WM_TOUCHCLICK:
on_touchclick(msg['attrs'][0], msg['attrs'][1])
elif msg['message'] == upiwin.WM_HWBUTTONCLICK:
on_button_click(msg['attrs'][0])