Skip to content

Commit

Permalink
bk138#67 Add LINE tool to draw straight lines
Browse files Browse the repository at this point in the history
  • Loading branch information
natenho committed Oct 13, 2021
1 parent 75c2ef6 commit 38e8f47
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 13 deletions.
3 changes: 2 additions & 1 deletion data/gromit-mpx.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# HOTKEY = "F9";
# UNDOKEY = "F8";

"yellow Line" = LINE (size=5 color="yellow" arrowsize=1 arrowposition="start");
"red Pen" = PEN (size=5 color="red");
"blue Pen" = "red Pen" (color="blue");
"yellow Pen" = "red Pen" (color="yellow");
Expand All @@ -15,7 +16,7 @@

"default" = "red Pen";
"default"[SHIFT] = "blue Pen";
"default"[CONTROL] = "yellow Pen";
"default"[CONTROL] = "yellow Line";
"default"[2] = "green Marker";
"default"[Button3] = "Eraser";

20 changes: 15 additions & 5 deletions src/callbacks.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,12 @@ gboolean on_buttonpress (GtkWidget *win,
devdata->lasty = ev->y;
devdata->motion_time = ev->time;

if(devdata->cur_context->type == GROMIT_LINE)
{
data->motionbuffer = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, data->width, data->height);
copy_surface (data->motionbuffer, data->backbuffer);
}

snap_undo_state (data);

gdk_event_get_axis ((GdkEvent *) ev, GDK_AXIS_PRESSURE, &pressure);
Expand Down Expand Up @@ -359,13 +365,17 @@ gboolean on_motion (GtkWidget *win,
devdata->cur_context->minwidth);

if(data->maxwidth > devdata->cur_context->maxwidth)
data->maxwidth = devdata->cur_context->maxwidth;
data->maxwidth = devdata->cur_context->maxwidth;

if(devdata->motion_time > 0)
{
draw_line (data, ev->device, devdata->lastx, devdata->lasty, ev->x, ev->y);
coord_list_prepend (data, ev->device, ev->x, ev->y, data->maxwidth);
}
{
if(devdata->cur_context->type == GROMIT_LINE)
draw_straight_line_during_motion (ev, devdata, data);
else
draw_line (data, ev->device, devdata->lastx, devdata->lasty, ev->x, ev->y);

coord_list_prepend (data, ev->device, ev->x, ev->y, data->maxwidth);
}
}

devdata->lastx = ev->x;
Expand Down
1 change: 1 addition & 0 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ gboolean parse_config (GromitData *data)
g_scanner_scope_add_symbol (scanner, 0, "PEN", (gpointer) GROMIT_PEN);
g_scanner_scope_add_symbol (scanner, 0, "ERASER", (gpointer) GROMIT_ERASER);
g_scanner_scope_add_symbol (scanner, 0, "RECOLOR",(gpointer) GROMIT_RECOLOR);
g_scanner_scope_add_symbol (scanner, 0, "LINE", (gpointer) GROMIT_LINE);
g_scanner_scope_add_symbol (scanner, 0, "HOTKEY", HOTKEY_SYMBOL_VALUE);
g_scanner_scope_add_symbol (scanner, 0, "UNDOKEY", UNDOKEY_SYMBOL_VALUE);

Expand Down
28 changes: 25 additions & 3 deletions src/drawing.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ void draw_arrow_when_applicable(GdkDevice *device, GromitDeviceData *devdata, Gr
switch (position)
{
case GROMIT_ARROW_AT_START:
if(devdata->cur_context->start_arrow_painted)
if(devdata->cur_context->start_arrow_painted &&
devdata->cur_context->type == GROMIT_PEN)
return;

arrow_point = g_list_last(devdata->coordlist)->data;
Expand Down Expand Up @@ -234,8 +235,8 @@ gboolean coord_list_get_arrow_param (GromitData *data,
(cur_point->y - y0) * (cur_point->y - y0);
width = cur_point->width * devdata->cur_context->arrowsize;
if (width * 2 <= dist &&
(!valid_point || valid_point->width < cur_point->width))
valid_point = cur_point;
(!valid_point || valid_point->width <= cur_point->width))
valid_point = cur_point;
}
}

Expand All @@ -251,3 +252,24 @@ gboolean coord_list_get_arrow_param (GromitData *data,
return success;
}

void draw_straight_line_during_motion (GdkEventMotion *ev, GromitDeviceData *devdata, GromitData *data)
{
GromitStrokeCoordinate *start_point = g_list_last(devdata->coordlist)->data;

gint x0 = start_point->x;
gint y0 = start_point->y;
gint w0 = start_point->width;

//Restore initial surface (vefore start drawing)
copy_surface(data->backbuffer, data->motionbuffer);
GdkRectangle rect = {0, 0, data->width, data->height};
gdk_window_invalidate_rect(gtk_widget_get_window(data->win), &rect, 0);
data->modified = 1;

//Recreate the line every motion
draw_line (data, ev->device, x0, y0, ev->x, ev->y);

//Straight lines only needs 2 coord points
coord_list_free(data, ev->device);
coord_list_prepend (data, ev->device, x0, y0, w0);
}
3 changes: 2 additions & 1 deletion src/drawing.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ typedef struct
void draw_line (GromitData *data, GdkDevice *dev, gint x1, gint y1, gint x2, gint y2);
void draw_arrow (GromitData *data, GdkDevice *dev, gint x1, gint y1, gint width, gfloat direction);
void draw_arrow_when_applicable(GdkDevice *device, GromitDeviceData *devdata, GromitData *data, GromitArrowPosition position);
void draw_straight_line_during_motion (GdkEventMotion *ev, GromitDeviceData *devdata, GromitData *data);
void cleanup_context(GromitPaintContext *context);
gboolean coord_list_get_arrow_param (GromitData *data,
GdkDevice *dev,
gint search_radius,
GromitArrowPosition arrowposition,
gint *ret_width,
gfloat *ret_direction);
void coord_list_prepend (GromitData *data, GdkDevice* dev, gint x, gint y, gint width);
void cleanup_context(GromitPaintContext *context);
void coord_list_free (GromitData *data, GdkDevice* dev);


Expand Down
6 changes: 4 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ GromitPaintContext *paint_context_new (GromitData *data,
context->minwidth = minwidth;
context->maxwidth = maxwidth;
context->paint_color = paint_color;

context->start_arrow_painted = FALSE;

context->paint_ctx = cairo_create (data->backbuffer);

Expand All @@ -71,7 +71,7 @@ GromitPaintContext *paint_context_new (GromitData *data,
else
if (type == GROMIT_RECOLOR)
cairo_set_operator(context->paint_ctx, CAIRO_OPERATOR_ATOP);
else /* GROMIT_PEN */
else
cairo_set_operator(context->paint_ctx, CAIRO_OPERATOR_OVER);

return context;
Expand All @@ -90,6 +90,8 @@ void paint_context_print (gchar *name,
g_printerr ("Eraser, "); break;
case GROMIT_RECOLOR:
g_printerr ("Recolor, "); break;
case GROMIT_LINE:
g_printerr ("Line, "); break;
default:
g_printerr ("UNKNOWN, "); break;
}
Expand Down
5 changes: 4 additions & 1 deletion src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ typedef enum
{
GROMIT_PEN,
GROMIT_ERASER,
GROMIT_RECOLOR
GROMIT_RECOLOR,
GROMIT_LINE
} GromitPaintType;

typedef enum
Expand Down Expand Up @@ -154,6 +155,8 @@ typedef struct

gboolean show_intro_on_startup;

cairo_surface_t *motionbuffer;

} GromitData;


Expand Down

0 comments on commit 38e8f47

Please sign in to comment.