Skip to content

Commit

Permalink
remove toolbar and implement eraser
Browse files Browse the repository at this point in the history
  • Loading branch information
mtshrmn committed Jan 13, 2024
1 parent 6f5a704 commit 835bf87
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 332 deletions.
96 changes: 21 additions & 75 deletions src/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Board *board_create(int width, int height) {

cairo_t *canvas = cairo_create(cr_surface);

SDL_SetRenderDrawColor(renderer, BOARD_BG);
SDL_SetRenderDrawColor(renderer, BOARD_BG_CAIRO);
SDL_RenderClear(renderer);

if (canvas == NULL)
Expand All @@ -68,12 +68,6 @@ Board *board_create(int width, int height) {
if (strokes == NULL)
goto defer;

#ifdef USE_TOOLBAR
ToolBar *toolbar = toolbar_create(50, sdl_surface->format);
if (toolbar == NULL)
goto defer;
#endif

board->window = window;
board->renderer = renderer;
board->sdl_surface = sdl_surface;
Expand All @@ -84,21 +78,14 @@ Board *board_create(int width, int height) {
board->cr = canvas;
board->width = window_width;
board->height = window_height;
#ifdef USE_TOOLBAR
board->toolbar = toolbar;
#endif
board->current_stroke_points = current_stroke_points;
board->current_stroke_paths = current_stroke_paths;
board->strokes = strokes;
board->dx = 0;
board->dy = 0;
#ifdef USE_TOOLBAR
board->stroke_width = get_width(toolbar->selected_width);
board->stroke_color = get_color(toolbar->selected_color);
#else
board->stroke_width = get_width(STROKE_WIDTH_MEDIUM);
board->stroke_color = get_color(COLOR_PRIMARY);
#endif
board->stroke_width = STROKE_WIDTH_MEDIUM;
board->stroke_color = COLOR_PRIMARY;
board->stroke_width_previous = board->stroke_width;
board->mouse_x = 0;
board->mouse_x_raw = 0;
board->mouse_y = 0;
Expand Down Expand Up @@ -127,10 +114,6 @@ Board *board_create(int width, int height) {
vector_free(current_stroke_paths);
if (strokes != NULL)
vector_free(strokes);
#ifdef USE_TOOLBAR
if (toolbar != NULL)
toolbar_free(toolbar);
#endif
if (default_cursor != NULL)
SDL_FreeCursor(default_cursor);
return NULL;
Expand All @@ -150,17 +133,11 @@ void board_free(Board *board) {
SDL_DestroyRenderer(board->renderer);
SDL_DestroyWindow(board->window);

#ifdef USE_TOOLBAR
toolbar_free(board->toolbar);
#endif
free(board);
}

void board_resize_surface(Board *board) {
SDL_GetWindowSize(board->window, &board->width, &board->height);
#ifdef USE_TOOLBAR
board_update_toolbar_area(board);
#endif

int renderer_width;
int renderer_height;
Expand Down Expand Up @@ -206,7 +183,7 @@ void board_resize_surface(Board *board) {
}

void board_clear(Board *board) {
cairo_set_source_rgba(board->cr, BOARD_BG);
cairo_set_source_rgba(board->cr, BOARD_BG_CAIRO);
cairo_paint(board->cr);
cairo_fill(board->cr);
}
Expand All @@ -231,16 +208,6 @@ void board_render(Board *board, SDL_Rect *update_area) {
}
SDL_UpdateTexture(board->sdl_texture, update_area, data, board->sdl_surface->pitch);

#ifdef USE_TOOLBAR
SDL_Rect result;
bool is_intersecting = SDL_IntersectRect(update_area, &board->toolbar_area, &result) == SDL_TRUE;
if (board->toolbar->visible && (is_intersecting || update_area == NULL)) {
toolbar_render(board->toolbar);
// draw toolbar on texture
unsigned char *toolbar_data = cairo_image_surface_get_data(board->toolbar->cr_surface);
SDL_UpdateTexture(board->sdl_texture, &board->toolbar_area, toolbar_data, board->toolbar->pitch);
}
#endif
SDL_RenderClear(board->renderer);
SDL_RenderCopy(board->renderer, board->sdl_texture, NULL, NULL);
SDL_RenderPresent(board->renderer);
Expand Down Expand Up @@ -329,6 +296,18 @@ void board_update_cursor(Board *board) {
cairo_arc(cr, w / 2, h / 2, 0, 0, M_PI * 2);
cairo_stroke(cr);

// if stroke_color is the same as the background color
// add an outline to the cursor
if (board->stroke_color == BOARD_BG) {
Uint8 r, g, b, a;
SDL_GetRGBA(BOARD_BG_INVERTED, cursor_surface->format, &r, &g, &b, &a);
cairo_set_source_rgba(cr, r / 255.0, g / 255.0, b / 255.0, a / 255.0);

cairo_set_line_width(cr, 2);
cairo_arc(cr, w / 2, h / 2, width / 2, 0, M_PI * 2);
cairo_stroke(cr);
}

// render to sdl surface
unsigned char *data = cairo_image_surface_get_data(cr_surface);
Uint32 *cursor_surface_pixels = cursor_surface->pixels;
Expand All @@ -347,52 +326,19 @@ void board_update_cursor(Board *board) {
SDL_FreeSurface(cursor_surface);
}

#ifdef USE_TOOLBAR
void board_update_toolbar_area(Board *board) {
SDL_Rect toolbar_area = {
.x = (board->width - board->toolbar->width) / 2,
.y = board->height - board->toolbar->height,
.w = board->toolbar->width,
.h = board->toolbar->height,
};
board->toolbar_area = toolbar_area;
}

void board_click_toolbar(Board *board, double x) {
int color, width;
toolbar_select_button(board->toolbar, x - board->toolbar_area.x, &width, &color);
if (width >= 0) {
board->stroke_width = get_width(width);
}

if (color >= 0) {
board->stroke_color = get_color(color);
}

board_update_cursor(board);
board_refresh(board);
}
#endif

void board_reset_current_stroke(Board *board) {
vector_reset(board->current_stroke_points);
vector_reset(board->current_stroke_paths);
}

void board_set_stroke_width(Board *board, StrokeWidth width) {
#ifdef USE_TOOLBAR
board->toolbar->selected_width = width;
#endif
board->stroke_width = get_width(width);
void board_set_stroke_width(Board *board, double width) {
board->stroke_width = width;
board_update_cursor(board);
board_refresh(board);
}

void board_set_stroke_color(Board *board, Color color) {
#ifdef USE_TOOLBAR
board->toolbar->selected_color = color;
#endif
board->stroke_color = get_color(color);
void board_set_stroke_color(Board *board, unsigned int color) {
board->stroke_color = color;
board_update_cursor(board);
board_refresh(board);
}
15 changes: 4 additions & 11 deletions src/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#define SB_BOARD_H

#include "config.h"
#include "toolbar.h"
#include "vector.h"

#include <SDL2/SDL.h>
Expand Down Expand Up @@ -33,12 +32,10 @@ typedef struct Board {
double dy;

double stroke_width;
double stroke_width_previous;
double stroke_color;
double stroke_color_previous;

#ifdef USE_TOOLBAR
ToolBar *toolbar;
SDL_Rect toolbar_area;
#endif
Vector *current_stroke_points; // contains Point
Vector *current_stroke_paths; // contains cairo_path_t
Vector *strokes; // contains Path
Expand All @@ -61,11 +58,7 @@ void board_reset_translation(Board *board);
void board_refresh(Board *board);
void board_update_cursor(Board *board);
void board_update_mouse_state(Board *board);
#ifdef USE_TOOLBAR
void board_update_toolbar_area(Board *board);
void board_click_toolbar(Board *board, double x);
#endif
void board_reset_current_stroke(Board *board);
void board_set_stroke_width(Board *board, StrokeWidth width);
void board_set_stroke_color(Board *board, Color color);
void board_set_stroke_width(Board *board, double width);
void board_set_stroke_color(Board *board, unsigned int color);
#endif // SB_BOARD_H
38 changes: 24 additions & 14 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,46 @@
#define THEME THEME_DARK

// color format is - 0xAARRGGBB
// board bg is in cario format
// dark theme colors definitions
#define _COLOR_PRIMARY_DARK 0xFFFFFFFF
#define _COLOR_SECNDARY_DARK 0xFFFF0000
#define _BOARD_BG_DARK 0, 0, 0, 1
#define _BOARD_BG_DARK 0xFF000000

// light theme colors definitions
#define _COLOR_PRIMARY_LIGHT 0xFF000000
#define _COLOR_SECNDARY_LIGHT 0xFFFF0000
#define _BOARD_BG_LIGHT 1, 1, 1, 1
#define _BOARD_BG_LIGHT 0xFFFFFFFF

// stroke widths
#define _STROKE_WIDTH_THIN 1.5
#define _STROKE_WIDTH_MEDIUM 3.0
#define _STROKE_WIDTH_THICK 6.0

// display toolbar
// #define USE_TOOLBAR
#define STROKE_WIDTH_THIN 1.5
#define STROKE_WIDTH_MEDIUM 3.0
#define STROKE_WIDTH_THICK 6.0
#define STROKE_WIDTH_THICKER 12.0
#define STROKE_WIDTH_THICKEST 24.0
#define STROKES_AMOUNT 5
#define COLORS_AMOUNT 2

// --------------------------------------------
#if THEME
#define BOARD_BG _BOARD_BG_LIGHT
#define BOARD_BG_INVERTED _BOARD_BG_DARK
#define _COLOR_PRIMARY _COLOR_PRIMARY_LIGHT
#define _COLOR_SECONDARY _COLOR_SECNDARY_LIGHT
#define COLOR_PRIMARY _COLOR_PRIMARY_LIGHT
#define COLOR_SECONDARY _COLOR_SECNDARY_LIGHT

#else
#define BOARD_BG _BOARD_BG_DARK
#define BOARD_BG_INVERTED _BOARD_BG_LIGHT
#define _COLOR_PRIMARY _COLOR_PRIMARY_DARK
#define _COLOR_SECONDARY _COLOR_SECNDARY_DARK
#define COLOR_PRIMARY _COLOR_PRIMARY_DARK
#define COLOR_SECONDARY _COLOR_SECNDARY_DARK

#endif

#define A_MASK 0xFF000000
#define R_MASK 0x00FF0000
#define G_MASK 0x0000FF00
#define B_MASK 0x000000FF

#define BOARD_BG_CAIRO \
((BOARD_BG & R_MASK) >> 16), ((BOARD_BG & G_MASK) >> 8), (BOARD_BG & B_MASK), ((BOARD_BG & A_MASK) >> 24)
#define BOARD_BG_INVERTED_CAIRO \
((BOARD_BG_INVERTED & R_MASK) >> 16), ((BOARD_BG_INVERTED & G_MASK) >> 8), (BOARD_BG_INVERTED & B_MASK), \
((BOARD_BG_INVERTED & A_MASK) >> 24)
59 changes: 22 additions & 37 deletions src/sb.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,6 @@ void on_window_event(Board *board, SDL_Event *sdl_event) {
void on_mouse_left_button_down(Board *board) {
board_update_mouse_state(board);

#ifdef USE_TOOLBAR
if (is_inside_rect(&board->toolbar_area, board->mouse_x_raw, board->mouse_y_raw)) {
board_click_toolbar(board, board->mouse_x_raw);
return;
}
#endif

// draw the initial point where the user clicked.
board->state = STATE_DRAWING;
board_reset_current_stroke(board);
Expand Down Expand Up @@ -111,13 +104,6 @@ void on_mouse_left_button_up(Board *board) {
return;
}

#ifdef USE_TOOLBAR
if (board->toolbar->visible == false) {
board->toolbar->visible = true;
board_render(board, &board->toolbar_are);
}
#endif

cairo_path_t *stroke = merge_paths(board->cr, board->current_stroke_paths);
cairo_new_path(board->cr);
Path *colored_stroke = path_create(stroke, board->stroke_color, board->stroke_width);
Expand Down Expand Up @@ -192,14 +178,6 @@ void draw_smooth_stroke(Board *board, Vector *stroke) {

void on_mouse_motion(Board *board) {
if (board->state == STATE_IDLE) {
#ifdef USE_TOOLBAR
board_update_mouse_state(board);
if (is_inside_rect(&board->toolbar_area, board->mouse_x_raw, board->mouse_y_raw)) {
SDL_SetCursor(board->default_cursor);
} else {
SDL_SetCursor(board->cursor);
}
#endif
return;
}

Expand All @@ -216,21 +194,6 @@ void on_mouse_motion(Board *board) {
// it is now guaranteed that board->state == STATE_DRAWING
board_update_mouse_state(board);

#ifdef USE_TOOLBAR
// determine visibility of the toolbar
bool previous_visibility_state = board->toolbar->visible;
if (is_inside_rect(&board->toolbar_area, board->mouse_x_raw, board->mouse_y_raw)) {
board->toolbar->visible = false;
} else {
board->toolbar->visible = true;
}

// re-render toolbar if needed.
if (previous_visibility_state != board->toolbar->visible) {
board_render(board, &board->toolbar_area);
}
#endif

// don't draw the same point twice.
Point *last_point = vector_top(board->current_stroke_points);
if (last_point->x == board->mouse_x && last_point->y == board->mouse_y) {
Expand Down Expand Up @@ -264,26 +227,48 @@ void on_key_down(Board *board) {
}

if (keys[SDL_SCANCODE_1]) {
if (board->stroke_color == BOARD_BG) {
board_set_stroke_color(board, board->stroke_color_previous);
}
board_set_stroke_width(board, STROKE_WIDTH_THIN);
}

if (keys[SDL_SCANCODE_2]) {
if (board->stroke_color == BOARD_BG) {
board_set_stroke_color(board, board->stroke_color_previous);
}
board_set_stroke_width(board, STROKE_WIDTH_MEDIUM);
return;
}

if (keys[SDL_SCANCODE_3]) {
if (board->stroke_color == BOARD_BG) {
board_set_stroke_color(board, board->stroke_color_previous);
}
board_set_stroke_width(board, STROKE_WIDTH_THICK);
return;
}

if (keys[SDL_SCANCODE_MINUS]) {
if (board->stroke_width == STROKE_WIDTH_THICKEST) {
board_set_stroke_width(board, board->stroke_width_previous);
}
board_set_stroke_color(board, COLOR_PRIMARY);
}

if (keys[SDL_SCANCODE_EQUALS]) {
if (board->stroke_width == STROKE_WIDTH_THICKEST) {
board_set_stroke_width(board, board->stroke_width_previous);
}
board_set_stroke_color(board, COLOR_SECONDARY);
}

if (keys[SDL_SCANCODE_BACKSPACE]) {
board->stroke_color_previous = board->stroke_color;
board->stroke_width_previous = board->stroke_width;
board_set_stroke_color(board, BOARD_BG);
board_set_stroke_width(board, STROKE_WIDTH_THICKEST);
}
}

int main() {
Expand Down
Loading

0 comments on commit 835bf87

Please sign in to comment.