Skip to content

Commit 835bf87

Browse files
committed
remove toolbar and implement eraser
1 parent 6f5a704 commit 835bf87

6 files changed

Lines changed: 71 additions & 332 deletions

File tree

src/board.c

Lines changed: 21 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Board *board_create(int width, int height) {
4848

4949
cairo_t *canvas = cairo_create(cr_surface);
5050

51-
SDL_SetRenderDrawColor(renderer, BOARD_BG);
51+
SDL_SetRenderDrawColor(renderer, BOARD_BG_CAIRO);
5252
SDL_RenderClear(renderer);
5353

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

71-
#ifdef USE_TOOLBAR
72-
ToolBar *toolbar = toolbar_create(50, sdl_surface->format);
73-
if (toolbar == NULL)
74-
goto defer;
75-
#endif
76-
7771
board->window = window;
7872
board->renderer = renderer;
7973
board->sdl_surface = sdl_surface;
@@ -84,21 +78,14 @@ Board *board_create(int width, int height) {
8478
board->cr = canvas;
8579
board->width = window_width;
8680
board->height = window_height;
87-
#ifdef USE_TOOLBAR
88-
board->toolbar = toolbar;
89-
#endif
9081
board->current_stroke_points = current_stroke_points;
9182
board->current_stroke_paths = current_stroke_paths;
9283
board->strokes = strokes;
9384
board->dx = 0;
9485
board->dy = 0;
95-
#ifdef USE_TOOLBAR
96-
board->stroke_width = get_width(toolbar->selected_width);
97-
board->stroke_color = get_color(toolbar->selected_color);
98-
#else
99-
board->stroke_width = get_width(STROKE_WIDTH_MEDIUM);
100-
board->stroke_color = get_color(COLOR_PRIMARY);
101-
#endif
86+
board->stroke_width = STROKE_WIDTH_MEDIUM;
87+
board->stroke_color = COLOR_PRIMARY;
88+
board->stroke_width_previous = board->stroke_width;
10289
board->mouse_x = 0;
10390
board->mouse_x_raw = 0;
10491
board->mouse_y = 0;
@@ -127,10 +114,6 @@ Board *board_create(int width, int height) {
127114
vector_free(current_stroke_paths);
128115
if (strokes != NULL)
129116
vector_free(strokes);
130-
#ifdef USE_TOOLBAR
131-
if (toolbar != NULL)
132-
toolbar_free(toolbar);
133-
#endif
134117
if (default_cursor != NULL)
135118
SDL_FreeCursor(default_cursor);
136119
return NULL;
@@ -150,17 +133,11 @@ void board_free(Board *board) {
150133
SDL_DestroyRenderer(board->renderer);
151134
SDL_DestroyWindow(board->window);
152135

153-
#ifdef USE_TOOLBAR
154-
toolbar_free(board->toolbar);
155-
#endif
156136
free(board);
157137
}
158138

159139
void board_resize_surface(Board *board) {
160140
SDL_GetWindowSize(board->window, &board->width, &board->height);
161-
#ifdef USE_TOOLBAR
162-
board_update_toolbar_area(board);
163-
#endif
164141

165142
int renderer_width;
166143
int renderer_height;
@@ -206,7 +183,7 @@ void board_resize_surface(Board *board) {
206183
}
207184

208185
void board_clear(Board *board) {
209-
cairo_set_source_rgba(board->cr, BOARD_BG);
186+
cairo_set_source_rgba(board->cr, BOARD_BG_CAIRO);
210187
cairo_paint(board->cr);
211188
cairo_fill(board->cr);
212189
}
@@ -231,16 +208,6 @@ void board_render(Board *board, SDL_Rect *update_area) {
231208
}
232209
SDL_UpdateTexture(board->sdl_texture, update_area, data, board->sdl_surface->pitch);
233210

234-
#ifdef USE_TOOLBAR
235-
SDL_Rect result;
236-
bool is_intersecting = SDL_IntersectRect(update_area, &board->toolbar_area, &result) == SDL_TRUE;
237-
if (board->toolbar->visible && (is_intersecting || update_area == NULL)) {
238-
toolbar_render(board->toolbar);
239-
// draw toolbar on texture
240-
unsigned char *toolbar_data = cairo_image_surface_get_data(board->toolbar->cr_surface);
241-
SDL_UpdateTexture(board->sdl_texture, &board->toolbar_area, toolbar_data, board->toolbar->pitch);
242-
}
243-
#endif
244211
SDL_RenderClear(board->renderer);
245212
SDL_RenderCopy(board->renderer, board->sdl_texture, NULL, NULL);
246213
SDL_RenderPresent(board->renderer);
@@ -329,6 +296,18 @@ void board_update_cursor(Board *board) {
329296
cairo_arc(cr, w / 2, h / 2, 0, 0, M_PI * 2);
330297
cairo_stroke(cr);
331298

299+
// if stroke_color is the same as the background color
300+
// add an outline to the cursor
301+
if (board->stroke_color == BOARD_BG) {
302+
Uint8 r, g, b, a;
303+
SDL_GetRGBA(BOARD_BG_INVERTED, cursor_surface->format, &r, &g, &b, &a);
304+
cairo_set_source_rgba(cr, r / 255.0, g / 255.0, b / 255.0, a / 255.0);
305+
306+
cairo_set_line_width(cr, 2);
307+
cairo_arc(cr, w / 2, h / 2, width / 2, 0, M_PI * 2);
308+
cairo_stroke(cr);
309+
}
310+
332311
// render to sdl surface
333312
unsigned char *data = cairo_image_surface_get_data(cr_surface);
334313
Uint32 *cursor_surface_pixels = cursor_surface->pixels;
@@ -347,52 +326,19 @@ void board_update_cursor(Board *board) {
347326
SDL_FreeSurface(cursor_surface);
348327
}
349328

350-
#ifdef USE_TOOLBAR
351-
void board_update_toolbar_area(Board *board) {
352-
SDL_Rect toolbar_area = {
353-
.x = (board->width - board->toolbar->width) / 2,
354-
.y = board->height - board->toolbar->height,
355-
.w = board->toolbar->width,
356-
.h = board->toolbar->height,
357-
};
358-
board->toolbar_area = toolbar_area;
359-
}
360-
361-
void board_click_toolbar(Board *board, double x) {
362-
int color, width;
363-
toolbar_select_button(board->toolbar, x - board->toolbar_area.x, &width, &color);
364-
if (width >= 0) {
365-
board->stroke_width = get_width(width);
366-
}
367-
368-
if (color >= 0) {
369-
board->stroke_color = get_color(color);
370-
}
371-
372-
board_update_cursor(board);
373-
board_refresh(board);
374-
}
375-
#endif
376-
377329
void board_reset_current_stroke(Board *board) {
378330
vector_reset(board->current_stroke_points);
379331
vector_reset(board->current_stroke_paths);
380332
}
381333

382-
void board_set_stroke_width(Board *board, StrokeWidth width) {
383-
#ifdef USE_TOOLBAR
384-
board->toolbar->selected_width = width;
385-
#endif
386-
board->stroke_width = get_width(width);
334+
void board_set_stroke_width(Board *board, double width) {
335+
board->stroke_width = width;
387336
board_update_cursor(board);
388337
board_refresh(board);
389338
}
390339

391-
void board_set_stroke_color(Board *board, Color color) {
392-
#ifdef USE_TOOLBAR
393-
board->toolbar->selected_color = color;
394-
#endif
395-
board->stroke_color = get_color(color);
340+
void board_set_stroke_color(Board *board, unsigned int color) {
341+
board->stroke_color = color;
396342
board_update_cursor(board);
397343
board_refresh(board);
398344
}

src/board.h

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#define SB_BOARD_H
33

44
#include "config.h"
5-
#include "toolbar.h"
65
#include "vector.h"
76

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

3534
double stroke_width;
35+
double stroke_width_previous;
3636
double stroke_color;
37+
double stroke_color_previous;
3738

38-
#ifdef USE_TOOLBAR
39-
ToolBar *toolbar;
40-
SDL_Rect toolbar_area;
41-
#endif
4239
Vector *current_stroke_points; // contains Point
4340
Vector *current_stroke_paths; // contains cairo_path_t
4441
Vector *strokes; // contains Path
@@ -61,11 +58,7 @@ void board_reset_translation(Board *board);
6158
void board_refresh(Board *board);
6259
void board_update_cursor(Board *board);
6360
void board_update_mouse_state(Board *board);
64-
#ifdef USE_TOOLBAR
65-
void board_update_toolbar_area(Board *board);
66-
void board_click_toolbar(Board *board, double x);
67-
#endif
6861
void board_reset_current_stroke(Board *board);
69-
void board_set_stroke_width(Board *board, StrokeWidth width);
70-
void board_set_stroke_color(Board *board, Color color);
62+
void board_set_stroke_width(Board *board, double width);
63+
void board_set_stroke_color(Board *board, unsigned int color);
7164
#endif // SB_BOARD_H

src/config.h

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,46 @@
44
#define THEME THEME_DARK
55

66
// color format is - 0xAARRGGBB
7-
// board bg is in cario format
8-
// dark theme colors definitions
97
#define _COLOR_PRIMARY_DARK 0xFFFFFFFF
108
#define _COLOR_SECNDARY_DARK 0xFFFF0000
11-
#define _BOARD_BG_DARK 0, 0, 0, 1
9+
#define _BOARD_BG_DARK 0xFF000000
1210

1311
// light theme colors definitions
1412
#define _COLOR_PRIMARY_LIGHT 0xFF000000
1513
#define _COLOR_SECNDARY_LIGHT 0xFFFF0000
16-
#define _BOARD_BG_LIGHT 1, 1, 1, 1
14+
#define _BOARD_BG_LIGHT 0xFFFFFFFF
1715

1816
// stroke widths
19-
#define _STROKE_WIDTH_THIN 1.5
20-
#define _STROKE_WIDTH_MEDIUM 3.0
21-
#define _STROKE_WIDTH_THICK 6.0
22-
23-
// display toolbar
24-
// #define USE_TOOLBAR
17+
#define STROKE_WIDTH_THIN 1.5
18+
#define STROKE_WIDTH_MEDIUM 3.0
19+
#define STROKE_WIDTH_THICK 6.0
20+
#define STROKE_WIDTH_THICKER 12.0
21+
#define STROKE_WIDTH_THICKEST 24.0
22+
#define STROKES_AMOUNT 5
23+
#define COLORS_AMOUNT 2
2524

2625
// --------------------------------------------
2726
#if THEME
2827
#define BOARD_BG _BOARD_BG_LIGHT
2928
#define BOARD_BG_INVERTED _BOARD_BG_DARK
30-
#define _COLOR_PRIMARY _COLOR_PRIMARY_LIGHT
31-
#define _COLOR_SECONDARY _COLOR_SECNDARY_LIGHT
29+
#define COLOR_PRIMARY _COLOR_PRIMARY_LIGHT
30+
#define COLOR_SECONDARY _COLOR_SECNDARY_LIGHT
3231

3332
#else
3433
#define BOARD_BG _BOARD_BG_DARK
3534
#define BOARD_BG_INVERTED _BOARD_BG_LIGHT
36-
#define _COLOR_PRIMARY _COLOR_PRIMARY_DARK
37-
#define _COLOR_SECONDARY _COLOR_SECNDARY_DARK
35+
#define COLOR_PRIMARY _COLOR_PRIMARY_DARK
36+
#define COLOR_SECONDARY _COLOR_SECNDARY_DARK
3837

3938
#endif
39+
40+
#define A_MASK 0xFF000000
41+
#define R_MASK 0x00FF0000
42+
#define G_MASK 0x0000FF00
43+
#define B_MASK 0x000000FF
44+
45+
#define BOARD_BG_CAIRO \
46+
((BOARD_BG & R_MASK) >> 16), ((BOARD_BG & G_MASK) >> 8), (BOARD_BG & B_MASK), ((BOARD_BG & A_MASK) >> 24)
47+
#define BOARD_BG_INVERTED_CAIRO \
48+
((BOARD_BG_INVERTED & R_MASK) >> 16), ((BOARD_BG_INVERTED & G_MASK) >> 8), (BOARD_BG_INVERTED & B_MASK), \
49+
((BOARD_BG_INVERTED & A_MASK) >> 24)

src/sb.c

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,6 @@ void on_window_event(Board *board, SDL_Event *sdl_event) {
5858
void on_mouse_left_button_down(Board *board) {
5959
board_update_mouse_state(board);
6060

61-
#ifdef USE_TOOLBAR
62-
if (is_inside_rect(&board->toolbar_area, board->mouse_x_raw, board->mouse_y_raw)) {
63-
board_click_toolbar(board, board->mouse_x_raw);
64-
return;
65-
}
66-
#endif
67-
6861
// draw the initial point where the user clicked.
6962
board->state = STATE_DRAWING;
7063
board_reset_current_stroke(board);
@@ -111,13 +104,6 @@ void on_mouse_left_button_up(Board *board) {
111104
return;
112105
}
113106

114-
#ifdef USE_TOOLBAR
115-
if (board->toolbar->visible == false) {
116-
board->toolbar->visible = true;
117-
board_render(board, &board->toolbar_are);
118-
}
119-
#endif
120-
121107
cairo_path_t *stroke = merge_paths(board->cr, board->current_stroke_paths);
122108
cairo_new_path(board->cr);
123109
Path *colored_stroke = path_create(stroke, board->stroke_color, board->stroke_width);
@@ -192,14 +178,6 @@ void draw_smooth_stroke(Board *board, Vector *stroke) {
192178

193179
void on_mouse_motion(Board *board) {
194180
if (board->state == STATE_IDLE) {
195-
#ifdef USE_TOOLBAR
196-
board_update_mouse_state(board);
197-
if (is_inside_rect(&board->toolbar_area, board->mouse_x_raw, board->mouse_y_raw)) {
198-
SDL_SetCursor(board->default_cursor);
199-
} else {
200-
SDL_SetCursor(board->cursor);
201-
}
202-
#endif
203181
return;
204182
}
205183

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

219-
#ifdef USE_TOOLBAR
220-
// determine visibility of the toolbar
221-
bool previous_visibility_state = board->toolbar->visible;
222-
if (is_inside_rect(&board->toolbar_area, board->mouse_x_raw, board->mouse_y_raw)) {
223-
board->toolbar->visible = false;
224-
} else {
225-
board->toolbar->visible = true;
226-
}
227-
228-
// re-render toolbar if needed.
229-
if (previous_visibility_state != board->toolbar->visible) {
230-
board_render(board, &board->toolbar_area);
231-
}
232-
#endif
233-
234197
// don't draw the same point twice.
235198
Point *last_point = vector_top(board->current_stroke_points);
236199
if (last_point->x == board->mouse_x && last_point->y == board->mouse_y) {
@@ -264,26 +227,48 @@ void on_key_down(Board *board) {
264227
}
265228

266229
if (keys[SDL_SCANCODE_1]) {
230+
if (board->stroke_color == BOARD_BG) {
231+
board_set_stroke_color(board, board->stroke_color_previous);
232+
}
267233
board_set_stroke_width(board, STROKE_WIDTH_THIN);
268234
}
269235

270236
if (keys[SDL_SCANCODE_2]) {
237+
if (board->stroke_color == BOARD_BG) {
238+
board_set_stroke_color(board, board->stroke_color_previous);
239+
}
271240
board_set_stroke_width(board, STROKE_WIDTH_MEDIUM);
272241
return;
273242
}
274243

275244
if (keys[SDL_SCANCODE_3]) {
245+
if (board->stroke_color == BOARD_BG) {
246+
board_set_stroke_color(board, board->stroke_color_previous);
247+
}
276248
board_set_stroke_width(board, STROKE_WIDTH_THICK);
277249
return;
278250
}
279251

280252
if (keys[SDL_SCANCODE_MINUS]) {
253+
if (board->stroke_width == STROKE_WIDTH_THICKEST) {
254+
board_set_stroke_width(board, board->stroke_width_previous);
255+
}
281256
board_set_stroke_color(board, COLOR_PRIMARY);
282257
}
283258

284259
if (keys[SDL_SCANCODE_EQUALS]) {
260+
if (board->stroke_width == STROKE_WIDTH_THICKEST) {
261+
board_set_stroke_width(board, board->stroke_width_previous);
262+
}
285263
board_set_stroke_color(board, COLOR_SECONDARY);
286264
}
265+
266+
if (keys[SDL_SCANCODE_BACKSPACE]) {
267+
board->stroke_color_previous = board->stroke_color;
268+
board->stroke_width_previous = board->stroke_width;
269+
board_set_stroke_color(board, BOARD_BG);
270+
board_set_stroke_width(board, STROKE_WIDTH_THICKEST);
271+
}
287272
}
288273

289274
int main() {

0 commit comments

Comments
 (0)