Skip to content

Commit

Permalink
feat(draw): draw shape from center if holding control
Browse files Browse the repository at this point in the history
  • Loading branch information
lelgenio authored and jtheoof committed Feb 21, 2021
1 parent 616c6a6 commit d80c361
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 10 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ text_font=sans-serif

<hr>

- `Ctrl`: Center Shape (Rectangle & Ellipse) based on draw start

<hr>

- `Ctrl+z`: Undo
- `Ctrl+Shift+z` or `Ctrl+y`: Redo
- `Ctrl+s`: Save to file (see man page)
Expand Down
2 changes: 1 addition & 1 deletion include/paint.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
void paint_add_temporary(struct swappy_state *state, double x, double y,
enum swappy_paint_type type);
void paint_update_temporary_shape(struct swappy_state *state, double x,
double y);
double y, gboolean is_control_pressed);
void paint_update_temporary_text(struct swappy_state *state,
GdkEventKey *event);
void paint_update_temporary_text_clip(struct swappy_state *state, gdouble x,
Expand Down
1 change: 1 addition & 0 deletions include/swappy.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ struct swappy_paint_shape {
double b;
double a;
double w;
bool should_center_at_from;
struct swappy_point from;
struct swappy_point to;
enum swappy_paint_type type;
Expand Down
1 change: 1 addition & 0 deletions res/swappy.glade
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
<property name="show_menubar">False</property>
<signal name="delete-event" handler="window_delete_handler" swapped="no"/>
<signal name="key-press-event" handler="window_keypress_handler" swapped="no"/>
<signal name="key-release-event" handler="window_keyrelease_handler" swapped="no"/>
<child>
<object class="GtkOverlay">
<property name="visible">True</property>
Expand Down
40 changes: 39 additions & 1 deletion src/application.c
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,22 @@ void copy_clicked_handler(GtkWidget *widget, struct swappy_state *state) {
clipboard_copy_drawing_area_to_selection(state);
}

void control_modifier_changed(bool pressed, struct swappy_state *state) {
if (state->temp_paint != NULL) {
switch (state->temp_paint->type) {
case SWAPPY_PAINT_MODE_ELLIPSE:
case SWAPPY_PAINT_MODE_RECTANGLE:
paint_update_temporary_shape(
state, state->temp_paint->content.shape.to.x,
state->temp_paint->content.shape.to.y, pressed);
render_state(state);
break;
default:
break;
}
}
}

void window_keypress_handler(GtkWidget *widget, GdkEventKey *event,
struct swappy_state *state) {
if (state->temp_paint && state->mode == SWAPPY_PAINT_MODE_TEXT) {
Expand Down Expand Up @@ -384,6 +400,27 @@ void window_keypress_handler(GtkWidget *widget, GdkEventKey *event,
case GDK_KEY_plus:
action_stroke_size_increase(state);
break;
case GDK_KEY_Control_L:
control_modifier_changed(true, state);
break;
default:
break;
}
}
}

void window_keyrelease_handler(GtkWidget *widget, GdkEventKey *event,
struct swappy_state *state) {
if (event->state & GDK_CONTROL_MASK) {
switch (event->keyval) {
case GDK_KEY_Control_L:
control_modifier_changed(false, state);
break;
default:
break;
}
} else {
switch (event->keyval) {
default:
break;
}
Expand Down Expand Up @@ -475,6 +512,7 @@ void draw_area_motion_notify_handler(GtkWidget *widget, GdkEventMotion *event,
gdk_window_set_cursor(window, crosshair);

gboolean is_button1_pressed = event->state & GDK_BUTTON1_MASK;
gboolean is_control_pressed = event->state & GDK_CONTROL_MASK;

switch (state->mode) {
case SWAPPY_PAINT_MODE_BLUR:
Expand All @@ -483,7 +521,7 @@ void draw_area_motion_notify_handler(GtkWidget *widget, GdkEventMotion *event,
case SWAPPY_PAINT_MODE_ELLIPSE:
case SWAPPY_PAINT_MODE_ARROW:
if (is_button1_pressed) {
paint_update_temporary_shape(state, x, y);
paint_update_temporary_shape(state, x, y, is_control_pressed);
render_state(state);
}
break;
Expand Down
8 changes: 7 additions & 1 deletion src/paint.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ void paint_add_temporary(struct swappy_state *state, double x, double y,
}

void paint_update_temporary_shape(struct swappy_state *state, double x,
double y) {
double y, gboolean is_control_pressed) {
struct swappy_paint *paint = state->temp_paint;
struct swappy_point *point;
GList *points;
Expand All @@ -171,6 +171,12 @@ void paint_update_temporary_shape(struct swappy_state *state, double x,
break;
case SWAPPY_PAINT_MODE_RECTANGLE:
case SWAPPY_PAINT_MODE_ELLIPSE:
paint->can_draw = true; // all set

paint->content.shape.should_center_at_from = is_control_pressed;
paint->content.shape.to.x = x;
paint->content.shape.to.y = y;
break;
case SWAPPY_PAINT_MODE_ARROW:
paint->can_draw = true; // all set

Expand Down
34 changes: 27 additions & 7 deletions src/render.c
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,22 @@ static void render_shape_arrow(cairo_t *cr, struct swappy_paint_shape shape) {
static void render_shape_ellipse(cairo_t *cr, struct swappy_paint_shape shape) {
double x = fabs(shape.from.x - shape.to.x);
double y = fabs(shape.from.y - shape.to.y);
double xc = shape.from.x + ((shape.to.x - shape.from.x) / 2);
double yc = shape.from.y + ((shape.to.y - shape.from.y) / 2);

double n = sqrt(x * x + y * y);
double r = n / 2;

double xc, yc, r;

if (shape.should_center_at_from) {
xc = shape.from.x;
yc = shape.from.y;

r = n;
} else {
xc = shape.from.x + ((shape.to.x - shape.from.x) / 2);
yc = shape.from.y + ((shape.to.y - shape.from.y) / 2);

r = n / 2;
}

cairo_set_source_rgba(cr, shape.r, shape.g, shape.b, shape.a);
cairo_set_line_width(cr, shape.w);
Expand All @@ -305,10 +316,19 @@ static void render_shape_ellipse(cairo_t *cr, struct swappy_paint_shape shape) {

static void render_shape_rectangle(cairo_t *cr,
struct swappy_paint_shape shape) {
double x = fmin(shape.from.x, shape.to.x);
double y = fmin(shape.from.y, shape.to.y);
double w = fabs(shape.from.x - shape.to.x);
double h = fabs(shape.from.y - shape.to.y);
double x, y, w, h;

if (shape.should_center_at_from) {
x = shape.from.x - fabs(shape.from.x - shape.to.x);
y = shape.from.y - fabs(shape.from.y - shape.to.y);
w = fabs(shape.from.x - shape.to.x) * 2;
h = fabs(shape.from.y - shape.to.y) * 2;
} else {
x = fmin(shape.from.x, shape.to.x);
y = fmin(shape.from.y, shape.to.y);
w = fabs(shape.from.x - shape.to.x);
h = fabs(shape.from.y - shape.to.y);
}

cairo_set_source_rgba(cr, shape.r, shape.g, shape.b, shape.a);
cairo_set_line_width(cr, shape.w);
Expand Down
4 changes: 4 additions & 0 deletions swappy.1.scd
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ The following lines can be used as swappy's default:
- *Equal*: Reset Stroke Size
- *k*: Clear Paints (cannot be undone)

## MODIFIERS

- *Ctrl*: Center Shape (Rectangle & Ellipse) based on draw start

## HEADER BAR

- *Ctrl+z*: Undo
Expand Down

0 comments on commit d80c361

Please sign in to comment.