diff options
| author | Louis Burda <quent.burda@gmail.com> | 2024-08-24 18:22:25 +0200 |
|---|---|---|
| committer | Louis Burda <quent.burda@gmail.com> | 2024-08-24 18:22:25 +0200 |
| commit | e00ec83c539b7962d82f2c44a68b1d2c4a26a6ba (patch) | |
| tree | b98b478afd55288f701a1ad76df5826ac492c4f1 | |
| parent | d91d30cfaf5198d512ba3864f4c9cc17cc0504ef (diff) | |
| download | xnote-e00ec83c539b7962d82f2c44a68b1d2c4a26a6ba.tar.gz xnote-e00ec83c539b7962d82f2c44a68b1d2c4a26a6ba.zip | |
Add rectangle drawing
| -rw-r--r-- | xnote.c | 23 |
1 files changed, 21 insertions, 2 deletions
@@ -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); |
