From d455f917b833f921d421099418c31ba355de90ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Boh=C3=A1=C4=8Dek?= Date: Fri, 18 Jun 2021 11:40:42 +0200 Subject: [PATCH] feat: add outline for cursor --- image-viewer/include/cursor.h | 5 +- image-viewer/src/cursor.c | 87 ++++++++++++++++++++++++++--------- 2 files changed, 67 insertions(+), 25 deletions(-) diff --git a/image-viewer/include/cursor.h b/image-viewer/include/cursor.h index 3d89626..c4a114f 100644 --- a/image-viewer/include/cursor.h +++ b/image-viewer/include/cursor.h @@ -7,7 +7,7 @@ #include #include -#define CURSOR_SIZE 9 +#define CURSOR_WIDTH 11 typedef struct { uint16_t x; @@ -16,10 +16,11 @@ typedef struct { bool shown; time_t shown_at; - display_pixel_t previous_display_data[CURSOR_SIZE * 2 - 1]; + display_pixel_t previous_display_data[CURSOR_WIDTH * CURSOR_WIDTH]; } cursor_t; extern const display_pixel_t CURSOR_COLOR; +extern const display_pixel_t CURSOR_OUTLINE_COLOR; cursor_t cursor_create(); void cursor_center(cursor_t *cursor, image_region_t region); diff --git a/image-viewer/src/cursor.c b/image-viewer/src/cursor.c index 0bdcbbb..334deff 100644 --- a/image-viewer/src/cursor.c +++ b/image-viewer/src/cursor.c @@ -4,7 +4,26 @@ #include "image.h" #include "coords.h" -const display_pixel_t CURSOR_COLOR = {.fields = {.r = (uint8_t)DISPLAY_MAX_RED, .g = 0, .b = 0}}; +const uint8_t CURSOR[] = { + 0x0, 0x0, 0x0, 0x0, 0x2, 0x2, 0x2, 0x0, 0x0, 0x0, 0x0, // end of 1st line + 0x0, 0x0, 0x0, 0x0, 0x2, 0x1, 0x2, 0x0, 0x0, 0x0, 0x0, // end of 2nd line + 0x0, 0x0, 0x0, 0x0, 0x2, 0x1, 0x2, 0x0, 0x0, 0x0, 0x0, // end of 3rd line + 0x0, 0x0, 0x0, 0x0, 0x2, 0x1, 0x2, 0x0, 0x0, 0x0, 0x0, // end of 4th line + 0x2, 0x2, 0x2, 0x2, 0x2, 0x1, 0x2, 0x2, 0x2, 0x2, 0x2, // end of 5th line + 0x2, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x2, // end of 6th line + 0x2, 0x2, 0x2, 0x2, 0x2, 0x1, 0x2, 0x2, 0x2, 0x2, 0x2, // end of 7th line + 0x0, 0x0, 0x0, 0x0, 0x2, 0x1, 0x2, 0x0, 0x0, 0x0, 0x0, // end of 8th line + 0x0, 0x0, 0x0, 0x0, 0x2, 0x1, 0x2, 0x0, 0x0, 0x0, 0x0, // end of 9th line + 0x0, 0x0, 0x0, 0x0, 0x2, 0x1, 0x2, 0x0, 0x0, 0x0, 0x0, // end of 10th line + 0x0, 0x0, 0x0, 0x0, 0x2, 0x2, 0x2, 0x0, 0x0, 0x0, 0x0, // end of 11th line + 0x0, 0x0 +}; + +const display_pixel_t CURSOR_OUTLINE_COLOR = {.fields = {.r = (uint8_t)DISPLAY_MAX_RED, + .g = DISPLAY_MAX_GREEN, + .b = DISPLAY_MAX_BLUE}}; +const display_pixel_t CURSOR_COLOR = { + .fields = {.r = 0, .g = 0, .b = 0}}; cursor_t cursor_create() { cursor_t cursor = { @@ -55,18 +74,30 @@ void cursor_show(cursor_t *cursor, image_t *image, image_zoom_t zoom, uint16_t base_x = screen_coords.x; uint16_t base_y = screen_coords.y; - uint16_t first_x = base_x - CURSOR_SIZE / 2; - uint16_t first_y = base_y - CURSOR_SIZE / 2; - - for (int i = 0; i < CURSOR_SIZE; i++) { - uint16_t x = first_x + i; - uint16_t y = first_y + i; - - cursor->previous_display_data[i] = display_get_pixel(display, base_x, y); - cursor->previous_display_data[i + CURSOR_SIZE] = display_get_pixel(display, x, base_y); - - display_set_pixel(display, base_x, y, CURSOR_COLOR); - display_set_pixel(display, x, base_y, CURSOR_COLOR); + uint16_t first_x = base_x - CURSOR_WIDTH / 2; + uint16_t first_y = base_y - CURSOR_WIDTH / 2; + + for (uint16_t iy = 0; iy < CURSOR_WIDTH; iy++) { + for (uint16_t ix = 0; ix < CURSOR_WIDTH; ix++) { + uint16_t x = first_x + ix; + uint16_t y = first_y + iy; + + uint8_t colorn = CURSOR[iy * CURSOR_WIDTH + ix]; + display_pixel_t color; + switch (colorn) { + case 0x1: + color = CURSOR_COLOR; + break; + case 0x2: + color = CURSOR_OUTLINE_COLOR; + break; + default: + continue; + } + + cursor->previous_display_data[iy * CURSOR_WIDTH + ix] = display_get_pixel(display, x, y); + display_set_pixel(display, x, y, color); + } } } @@ -80,16 +111,26 @@ void cursor_hide(cursor_t *cursor, image_t *image, image_zoom_t zoom, image_get_screen_coords(image, zoom, coords_create(cursor->x, cursor->y)); uint16_t base_x = screen_coords.x; uint16_t base_y = screen_coords.y; - - uint16_t first_x = base_x - CURSOR_SIZE / 2; - uint16_t first_y = base_y - CURSOR_SIZE / 2; - - for (int i = 0; i < CURSOR_SIZE; i++) { - uint16_t x = first_x + i; - uint16_t y = first_y + i; - - display_set_pixel(display, base_x, y, cursor->previous_display_data[i]); - display_set_pixel(display, x, base_y, cursor->previous_display_data[i + CURSOR_SIZE]); + uint16_t first_x = base_x - CURSOR_WIDTH / 2; + uint16_t first_y = base_y - CURSOR_WIDTH / 2; + + for (int iy = 0; iy < CURSOR_WIDTH; iy++) { + for (int ix = 0; ix < CURSOR_WIDTH; ix++) { + uint16_t x = first_x + ix; + uint16_t y = first_y + iy; + + uint8_t colorn = CURSOR[iy * CURSOR_WIDTH + ix]; + switch (colorn) { + case 0x1: + case 0x2: + break; + default: + continue; + } + + display_set_pixel(display, x, y, + cursor->previous_display_data[iy * CURSOR_WIDTH + ix]); + } } cursor->shown = false; -- 2.48.1