From 2a5a6dbe4e1268c042a415037ef65b1bb3085522 Mon Sep 17 00:00:00 2001 From: Amy Bowersox Date: Wed, 11 Dec 2019 15:18:33 -0700 Subject: [PATCH] now we're actually saving the objects and drawing them properly --- scripts/demo1.py | 73 ++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 64 insertions(+), 9 deletions(-) diff --git a/scripts/demo1.py b/scripts/demo1.py index 31b781d..955c17d 100755 --- a/scripts/demo1.py +++ b/scripts/demo1.py @@ -41,6 +41,7 @@ current_color = 0 def draw_current_color(): hdc.text_color = color_list[current_color] + hdc.rop2 = upiwin.R2_COPYPEN hdc.solid_rectangle(cmd2_rect[0] + 6, cmd2_rect[1] + 6, cmd2_rect[0] + 54, cmd2_rect[1] + 54) def select_next_color(): @@ -50,24 +51,58 @@ def select_next_color(): current_color = 0 draw_current_color() +# --- Object drawing --- + +def draw_freehand(color, data): + hdc.text_color = color + hdc.rop2 = upiwin.R2_COPYPEN + hdc.move_to(data[0][0], data[0][1]) + for pt in data[1:len(data) - 1]: + hdc.line_to(pt[0], pt[1]) + +def draw_line(color, data): + hdc.text_color = color + hdc.rop2 = upiwin.R2_COPYPEN + hdc.move_to(data[0][0], data[0][1]) + hdc.line_to(data[1][0], data[1][1]) + +def draw_rectangle(color, data): + hdc.text_color = color + hdc.rop2 = upiwin.R2_COPYPEN + hdc.rectangle(data[0], data[1], data[2], data[3]) + +def draw_filled_rectangle(color, data): + hdc.text_color = color + hdc.rop2 = upiwin.R2_COPYPEN + hdc.solid_rectangle(data[0], data[1], data[2], data[3]) + +def draw_object(obj): + obj['draw'](obj['color'], obj['data']) + +# --- Master object list --- + +objects_drawn = [] + # --- Graphic feedback -- origin_x = 0 origin_y = 0 current_x = 0 current_y = 0 +freehand_points = [] def freehand_draw(x, y, down, up): - global current_x, current_y + global current_x, current_y, freehand_points if down: - current_x = origin_x - current_y = origin_y + freehand_points = [] else: hdc.text_color = color_list[current_color] + hdc.rop2 = upiwin.R2_COPYPEN hdc.move_to(current_x, current_y) hdc.line_to(x, y) - current_x = x - current_y = y + current_x = x + current_y = y + freehand_points += [(x, y)] def rubberband_rectangle(x, y, down, up): global current_x, current_y @@ -95,11 +130,25 @@ def rubberband_line(x, y, down, up): # --- Tool definitions --- +def save_freehand(): + return { 'draw': draw_freehand, 'color': color_list[current_color], 'data': freehand_points } + +def save_line(): + return { 'draw': draw_line, 'color': color_list[current_color], 'data': [(origin_x, origin_y), (current_x, current_y)] } + +def save_rectangle(): + return { 'draw': draw_rectangle, 'color': color_list[current_color], + 'data': (min(origin_x, current_x), min(origin_y, current_y), max(origin_x, current_x), max(origin_y, current_y)) } + +def save_filled_rectangle(): + return { 'draw': draw_filled_rectangle, 'color': color_list[current_color], + 'data': (min(origin_x, current_x), min(origin_y, current_y), max(origin_x, current_x), max(origin_y, current_y)) } + tool_list = [ - { 'icon': bmp_freehand, 'feedback': freehand_draw }, - { 'icon': bmp_line, 'feedback': rubberband_line }, - { 'icon': bmp_rect, 'feedback': rubberband_rectangle }, - { 'icon': bmp_fillrect, 'feedback': rubberband_rectangle } + { 'icon': bmp_freehand, 'feedback': freehand_draw, 'save': save_freehand, 'dodraw': False }, + { 'icon': bmp_line, 'feedback': rubberband_line, 'save': save_line, 'dodraw': True }, + { 'icon': bmp_rect, 'feedback': rubberband_rectangle, 'save': save_rectangle, 'dodraw': True }, + { 'icon': bmp_fillrect, 'feedback': rubberband_rectangle, 'save': save_filled_rectangle, 'dodraw': True } ] current_tool = 0 @@ -129,8 +178,13 @@ def on_touchmove(x, y): tool_list[current_tool]['feedback'](x, y, False, False) def on_touchup(x, y): + global objects_drawn if point_in_rect(drawing_rect, x, y): tool_list[current_tool]['feedback'](x, y, False, True) + object = tool_list[current_tool]['save']() + if tool_list[current_tool]['dodraw']: + draw_object(object) + objects_drawn += [object] def on_touchclick(x, y): if point_in_rect(cmd1_rect, x, y): @@ -150,6 +204,7 @@ def on_button_click(button): # Draw the basic layout. hdc.text_color = LTGRAY +hdc.rop2 = upiwin.R2_COPYPEN 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])