From e00ec83c539b7962d82f2c44a68b1d2c4a26a6ba Mon Sep 17 00:00:00 2001 From: Louis Burda Date: Sat, 24 Aug 2024 18:22:25 +0200 Subject: Add rectangle drawing --- xnote.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/xnote.c b/xnote.c index 0541374..b6f6de7 100644 --- a/xnote.c +++ b/xnote.c @@ -29,6 +29,7 @@ struct note { enum event { EVENT_START, EVENT_START_LINE, + EVENT_START_RECT, EVENT_END_LINE, EVENT_START_NOTE, EVENT_MOUSE_MOVE, @@ -186,7 +187,7 @@ mouse_move_cb(GLFWwindow *window, double x, double y) { cursor_x = x; cursor_y = y; - if (last_event() == EVENT_START_LINE && line_count) { + if (last_event() == EVENT_START_LINE) { struct line *line = &lines[line_count-1]; if (line->count > 0) { struct point *last_point = &line->points[line->count-1]; @@ -197,6 +198,12 @@ mouse_move_cb(GLFWwindow *window, double x, double y) &line->count, &line->cap, sizeof(struct point)); point->x = canvas_x + cursor_x; point->y = canvas_y + cursor_y; + } else if (last_event() == EVENT_START_RECT) { + struct line *line = &lines[line_count-1]; + line->points[1].x = canvas_x + cursor_x; + line->points[2].x = canvas_x + cursor_x; + line->points[2].y = canvas_y + cursor_y; + line->points[3].y = canvas_y + cursor_y; } else if (last_event() == EVENT_MOUSE_DRAG) { canvas_x = drag_canvas_x - (cursor_x - drag_cursor_x); canvas_y = drag_canvas_y - (cursor_y - drag_cursor_y); @@ -220,7 +227,8 @@ mouse_press_cb(GLFWwindow *window, int button, int action, int mods) point->y = canvas_y + cursor_y; push_event(EVENT_START_LINE); } else if (action == GLFW_RELEASE) { - if (last_event() == EVENT_START_LINE) { + if (last_event() == EVENT_START_LINE + || last_event() == EVENT_START_RECT) { struct line *line = &lines[line_count-1]; shrink(&line->points, line->count, &line->cap, sizeof(struct point)); @@ -247,6 +255,17 @@ key_press_cb(GLFWwindow *window, int key, int scancode, int action, int mods) const char *keysym = glfwGetKeyName(key, scancode); if ((mods & GLFW_MOD_CONTROL) && keysym && !strcmp(keysym, "z")) { if (event_count) undo(); + } else if (action == GLFW_PRESS && (mods & GLFW_MOD_CONTROL) + && keysym && !strcmp(keysym, "r")) { + struct line *line = append(&lines, + &line_count, &line_cap, sizeof(struct line)); + line->count = line->cap = 5; + line->points = malloc(line->count * sizeof(struct point)); + for (size_t i = 0; i < line->count; i++) { + line->points[i].x = canvas_x + cursor_x; + line->points[i].y = canvas_y + cursor_y; + } + push_event(EVENT_START_RECT); } else if ((mods & GLFW_MOD_CONTROL) && keysym && !strcmp(keysym, "c")) { for (size_t i = 0; i < line_count; i++) free(lines[i].points); -- cgit v1.2.3-71-gd317