From aad731da310bdeaada93b76b3202b4172aa89cc6 Mon Sep 17 00:00:00 2001 From: Ry Date: Wed, 18 Jan 2023 17:55:12 -0800 Subject: [PATCH] foxpaint: Add a very basic color picker window using button widgets --- applications/foxpaint/main.asm | 232 ++++++++++++++++++++++++++++----- 1 file changed, 198 insertions(+), 34 deletions(-) diff --git a/applications/foxpaint/main.asm b/applications/foxpaint/main.asm index b70b094..9995ce4 100644 --- a/applications/foxpaint/main.asm +++ b/applications/foxpaint/main.asm @@ -1,8 +1,8 @@ -; mouse painting demo +; simple paint application - mov r0, window_struct - mov r1, window_title - mov r2, 640 + mov r0, canvas_window_struct + mov r1, canvas_window_title + mov r2, 512 mov r3, 448 mov r4, 0 mov r5, 16 @@ -10,13 +10,50 @@ mov r7, 0 call new_window + mov r0, tools_window_struct + mov r1, tools_window_title + mov r2, 128 + mov r3, 128 + mov r4, 512 + mov r5, 16 + mov r6, menu_items_root + mov r7, color_button_black_widget + call new_window + + mov r0, 0xFFFFFFFF + mov r1, tools_window_struct + call fill_window + + mov r0, tools_window_struct + call get_window_overlay_number + mov r5, r0 + mov r0, 16 + mov r1, 32 + mov r2, 96 + mov r3, 96 + mov r4, 0xFF888888 + call draw_filled_rectangle_to_overlay + + mov r0, color_section_text + mov r1, 32 + mov r2, 48 + mov r3, [color] + not r3 + or r3, 0xFF000000 + mov r4, [color] + call draw_str_to_overlay + + mov r0, tools_window_struct + call draw_widgets_to_window + event_loop: - mov r0, window_struct + ; canvas events + mov r0, canvas_window_struct call get_next_window_event ; was the mouse clicked? cmp r0, EVENT_TYPE_MOUSE_CLICK - ifz call mouse_click_event + ifz call canvas_mouse_click_event ; was the mouse released? cmp r0, EVENT_TYPE_MOUSE_RELEASE @@ -39,18 +76,52 @@ event_loop: cmp r0, EVENT_TYPE_MENU_ACK ifz call menu_ack_event + + ; tools events + mov r0, tools_window_struct + call get_next_window_event + + ; was the mouse clicked? + cmp r0, EVENT_TYPE_MOUSE_CLICK + ifz call tools_mouse_click_event + + ; was the mouse released? + cmp r0, EVENT_TYPE_MOUSE_RELEASE + ifz mov.8 [is_drawing], 0 + + ; did the user click the menu bar? + cmp r0, EVENT_TYPE_MENU_BAR_CLICK + ifz mov r0, menu_items_root + ifz call menu_bar_click_event + + ; is the user in a menu? + cmp r0, EVENT_TYPE_MENU_UPDATE + ifz call menu_update_event + + ; did the user click a menu item? + cmp r0, EVENT_TYPE_MENU_CLICK + ifz call menu_click_event + + ; should the menu be closed? + cmp r0, EVENT_TYPE_MENU_ACK + ifz call menu_ack_event + + ; did the user click a button? + cmp r0, EVENT_TYPE_BUTTON_CLICK + ifz call tools_button_click_event + cmp.8 [is_drawing], 0 ifnz call draw_pixel call yield_task jmp event_loop -mouse_click_event: +canvas_mouse_click_event: push r0 ; first, check if we are attempting to drag or close the window cmp r2, 16 - iflteq jmp drag_or_close_window + iflteq jmp drag_or_close_canvas_window ; if not, enable the drawing flag mov.8 [is_drawing], 1 @@ -58,6 +129,20 @@ mouse_click_event: pop r0 ret +tools_mouse_click_event: + push r0 + + ; first, check if we are attempting to drag or close the window + cmp r2, 16 + iflteq jmp drag_or_close_tools_window + + ; then, handle widget clicks + mov r0, tools_window_struct + call handle_widget_click + + pop r0 + ret + menu_click_event: ; r2 contains the clicked root menu ; r3 contains the clicked menu item @@ -70,10 +155,6 @@ menu_click_event: cmp r2, 1 ifz call brush_menu_click_event - ; color - cmp r2, 2 - ifz call color_menu_click_event - ret menu_ack_event: @@ -121,34 +202,48 @@ brush_menu_click_event: ret -color_menu_click_event: - ; r2 contains the clicked root menu - ; r3 contains the clicked menu item +tools_button_click_event: + ; r1 contains the ID of the clicked button + + ; colors ; black - cmp r3, 0 + cmp r1, 0 ifz mov [color], 0xFF000000 ; white - cmp r3, 1 + cmp r1, 1 ifz mov [color], 0xFFFFFFFF ; red - cmp r3, 2 + cmp r1, 2 ifz mov [color], 0xFF0000FF ; green - cmp r3, 3 + cmp r1, 3 ifz mov [color], 0xFF00FF00 ; blue - cmp r3, 4 + cmp r1, 4 ifz mov [color], 0xFFFF0000 + ; redraw the "color" text in the clicked color + mov r0, tools_window_struct + call get_window_overlay_number + mov r5, r0 + mov r0, color_section_text + mov r1, 32 + mov r2, 48 + mov r3, [color] + not r3 + or r3, 0xFF000000 + mov r4, [color] + call draw_str_to_overlay + ret draw_pixel: - mov r0, window_struct + mov r0, canvas_window_struct call get_window_overlay_number mov r2, r0 mov r5, r0 @@ -163,44 +258,113 @@ draw_pixel: clear_canvas_black: mov r0, 0xFF000000 - mov r1, window_struct + mov r1, canvas_window_struct call fill_window ret clear_canvas_white: mov r0, 0xFFFFFFFF - mov r1, window_struct + mov r1, canvas_window_struct call fill_window ret -drag_or_close_window: +drag_or_close_canvas_window: cmp r1, 8 - iflteq jmp close_window - mov r0, window_struct + iflteq jmp close_canvas_window + mov r0, canvas_window_struct call start_dragging_window pop r0 ret -close_window: - mov r0, window_struct +close_canvas_window: + mov r0, canvas_window_struct + call destroy_window + mov r0, tools_window_struct call destroy_window call end_current_task -window_title: data.str "FoxPaint" data.8 0 -window_struct: data.fill 0, 36 +drag_or_close_tools_window: + cmp r1, 8 + iflteq pop r0 + iflteq ret + mov r0, tools_window_struct + call start_dragging_window + pop r0 + ret + +canvas_window_title: data.str "FoxPaint canvas" data.8 0 +canvas_window_struct: data.fill 0, 36 + +tools_window_title: data.str "FoxPaint tools" data.8 0 +tools_window_struct: data.fill 0, 36 + +color_section_text: data.str "Color " data.8 0 +color_button_black_widget: + data.32 color_button_white_widget ; next_ptr + data.32 0 ; id + data.32 WIDGET_TYPE_BUTTON ; type + data.32 color_button_text ; text_ptr + data.32 0xFFFFFFFF ; foreground_color + data.32 0xFF000000 ; background_color + data.16 16 ; width + data.16 0 ; reserved + data.16 32 ; x_pos + data.16 64 ; y_pos +color_button_white_widget: + data.32 color_button_red_widget ; next_ptr + data.32 1 ; id + data.32 WIDGET_TYPE_BUTTON ; type + data.32 color_button_text ; text_ptr + data.32 0xFFFFFFFF ; foreground_color + data.32 0xFFFFFFFF ; background_color + data.16 16 ; width + data.16 0 ; reserved + data.16 48 ; x_pos + data.16 64 ; y_pos +color_button_red_widget: + data.32 color_button_green_widget ; next_ptr + data.32 2 ; id + data.32 WIDGET_TYPE_BUTTON ; type + data.32 color_button_text ; text_ptr + data.32 0xFFFFFFFF ; foreground_color + data.32 0xFF0000FF ; background_color + data.16 16 ; width + data.16 0 ; reserved + data.16 64 ; x_pos + data.16 64 ; y_pos +color_button_green_widget: + data.32 color_button_blue_widget ; next_ptr + data.32 3 ; id + data.32 WIDGET_TYPE_BUTTON ; type + data.32 color_button_text ; text_ptr + data.32 0xFFFFFFFF ; foreground_color + data.32 0xFF00FF00 ; background_color + data.16 16 ; width + data.16 0 ; reserved + data.16 32 ; x_pos + data.16 80 ; y_pos +color_button_blue_widget: + data.32 0 ; next_ptr + data.32 4 ; id + data.32 WIDGET_TYPE_BUTTON ; type + data.32 color_button_text ; text_ptr + data.32 0xFFFFFFFF ; foreground_color + data.32 0xFFFF0000 ; background_color + data.16 16 ; width + data.16 0 ; reserved + data.16 48 ; x_pos + data.16 80 ; y_pos +color_button_text: data.str " " data.8 0 menu_items_root: - data.8 3 ; number of menus + data.8 2 ; number of menus data.32 menu_items_canvas_list data.32 menu_items_canvas_name ; pointer to menu list, pointer to menu name data.32 menu_items_brush_list data.32 menu_items_brush_name ; pointer to menu list, pointer to menu name - data.32 menu_items_color_list data.32 menu_items_color_name ; pointer to menu list, pointer to menu name menu_items_canvas_name: data.8 6 data.str "Canvas" data.8 0x00 ; text length, text, null-terminator menu_items_brush_name: data.8 5 data.str "Brush" data.8 0x00 ; text length, text, null-terminator -menu_items_color_name: - data.8 5 data.str "Color" data.8 0x00 ; text length, text, null-terminator menu_items_canvas_list: data.8 2 ; number of items data.8 16 ; menu width (usually longest item + 2)