Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config, callback, drawing, main: add LINE and RECT tools, and arrowtype… #192

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 115 additions & 39 deletions src/callbacks.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <math.h>
#include "main.h"
#include "input.h"
#include "callbacks.h"
Expand Down Expand Up @@ -122,7 +123,11 @@ void on_monitors_changed ( GdkScreen *screen,
cairo_destroy (cr);
cairo_surface_destroy(data->backbuffer);
data->backbuffer = new_shape;


// recreate temp_buffer
cairo_surface_destroy(data->temp_buffer);
data->temp_buffer = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, data->width, data->height);

/*
these depend on the shape surface
*/
Expand All @@ -137,10 +142,10 @@ void on_monitors_changed ( GdkScreen *screen,
parse_config(data); // also calls paint_context_new() :-(


data->default_pen = paint_context_new (data, GROMIT_PEN,
data->red, 7, 0, 1, G_MAXUINT);
data->default_eraser = paint_context_new (data, GROMIT_ERASER,
data->red, 75, 0, 1, G_MAXUINT);
data->default_pen = paint_context_new (data, GROMIT_PEN, data->red, 7,
0, GROMIT_ARROW_NONE, 1, G_MAXUINT);
data->default_eraser = paint_context_new (data, GROMIT_ERASER, data->red, 75,
0, GROMIT_ARROW_NONE, 1, G_MAXUINT);

if(!data->composited) // set shape
{
Expand Down Expand Up @@ -273,6 +278,14 @@ gboolean on_buttonpress (GtkWidget *win,
devdata->lastslave != gdk_event_get_source_device ((GdkEvent *) ev))
select_tool (data, ev->device, gdk_event_get_source_device ((GdkEvent *) ev), ev->state);

GromitPaintType type = devdata->cur_context->type;

// store original state to have dynamic update of line and rect
if (type == GROMIT_LINE || type == GROMIT_RECT)
{
copy_surface(data->temp_buffer, data->backbuffer);
}

devdata->lastx = ev->x;
devdata->lasty = ev->y;
devdata->motion_time = ev->time;
Expand Down Expand Up @@ -319,38 +332,43 @@ gboolean on_motion (GtkWidget *win,
devdata->lastslave != gdk_event_get_source_device ((GdkEvent *) ev))
select_tool (data, ev->device, gdk_event_get_source_device ((GdkEvent *) ev), ev->state);

GromitPaintType type = devdata->cur_context->type;

gdk_device_get_history (ev->device, ev->window,
devdata->motion_time, ev->time,
&coords, &nevents);

if(!data->xinerama && nevents > 0)
{
for (i=0; i < nevents; i++)
if (type != GROMIT_LINE && type != GROMIT_RECT)
{
gdouble x, y;

gdk_device_get_axis (ev->device, coords[i]->axes,
GDK_AXIS_PRESSURE, &pressure);
if (pressure > 0)
for (i=0; i < nevents; i++)
{
data->maxwidth = (CLAMP (pressure + line_thickener, 0, 1) *
(double) (devdata->cur_context->width -
devdata->cur_context->minwidth) +
devdata->cur_context->minwidth);

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

gdk_device_get_axis(ev->device, coords[i]->axes,
GDK_AXIS_X, &x);
gdk_device_get_axis(ev->device, coords[i]->axes,
GDK_AXIS_Y, &y);

draw_line (data, ev->device, devdata->lastx, devdata->lasty, x, y);

coord_list_prepend (data, ev->device, x, y, data->maxwidth);
devdata->lastx = x;
devdata->lasty = y;
gdouble x, y;

gdk_device_get_axis (ev->device, coords[i]->axes,
GDK_AXIS_PRESSURE, &pressure);
if (pressure > 0)
{
data->maxwidth = (CLAMP (pressure + line_thickener, 0, 1) *
(double) (devdata->cur_context->width -
devdata->cur_context->minwidth) +
devdata->cur_context->minwidth);

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

gdk_device_get_axis(ev->device, coords[i]->axes,
GDK_AXIS_X, &x);
gdk_device_get_axis(ev->device, coords[i]->axes,
GDK_AXIS_Y, &y);

draw_line (data, ev->device, devdata->lastx, devdata->lasty, x, y);

coord_list_prepend (data, ev->device, x, y, data->maxwidth);
devdata->lastx = x;
devdata->lasty = y;
}
}
}

Expand All @@ -373,13 +391,46 @@ gboolean on_motion (GtkWidget *win,

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 (type == GROMIT_LINE || type == GROMIT_RECT) {
copy_surface(data->backbuffer, data->temp_buffer);
GdkRectangle rect = {0, 0, data->width, data->height};
gdk_window_invalidate_rect(gtk_widget_get_window(data->win), &rect, 0);
}
if (type == GROMIT_LINE)
{
GromitArrowType atype = devdata->cur_context->arrow_type;
draw_line (data, ev->device, devdata->lastx, devdata->lasty, ev->x, ev->y);
if (atype != GROMIT_ARROW_NONE)
{
gint width = devdata->cur_context->arrowsize * devdata->cur_context->width / 2;
gfloat direction =
atan2(ev->y - devdata->lasty, ev->x - devdata->lastx);
if (atype & GROMIT_ARROW_END)
draw_arrow(data, ev->device, ev->x, ev->y, width * 2, direction);
if (atype & GROMIT_ARROW_START)
draw_arrow(data, ev->device, devdata->lastx, devdata->lasty, width * 2, M_PI + direction);
}
}
else if (type == GROMIT_RECT)
{
draw_line (data, ev->device, devdata->lastx, devdata->lasty, ev->x, devdata->lasty);
draw_line (data, ev->device, ev->x, devdata->lasty, ev->x, ev->y);
draw_line (data, ev->device, ev->x, ev->y, devdata->lastx, ev->y);
draw_line (data, ev->device, devdata->lastx, ev->y, devdata->lastx, devdata->lasty);
}
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;
devdata->lasty = ev->y;
if (type != GROMIT_LINE && type != GROMIT_RECT)
{
devdata->lastx = ev->x;
devdata->lasty = ev->y;
}
devdata->motion_time = ev->time;

return TRUE;
Expand All @@ -406,11 +457,34 @@ gboolean on_buttonrelease (GtkWidget *win,

if (!devdata->is_grabbed)
return FALSE;

if (devdata->cur_context->arrowsize != 0 &&
coord_list_get_arrow_param (data, ev->device, width * 3,
&width, &direction))
draw_arrow (data, ev->device, ev->x, ev->y, width, direction);

GromitPaintType type = devdata->cur_context->type;

if (devdata->cur_context->arrowsize != 0)
{
GromitArrowType atype = devdata->cur_context->arrow_type;
if (type == GROMIT_LINE)
{
direction = atan2 (ev->y - devdata->lasty, ev->x - devdata->lastx);
if (atype & GROMIT_ARROW_END)
draw_arrow(data, ev->device, ev->x, ev->y, width * 2, direction);
if (atype & GROMIT_ARROW_START)
draw_arrow(data, ev->device, devdata->lastx, devdata->lasty, width * 2, M_PI + direction);
}
else
{
gint x0, y0;
if ((atype & GROMIT_ARROW_END) &&
coord_list_get_arrow_param (data, ev->device, width * 3,
GROMIT_ARROW_END, &x0, &y0, &width, &direction))
draw_arrow (data, ev->device, x0, y0, width, direction);
if ((atype & GROMIT_ARROW_START) &&
coord_list_get_arrow_param (data, ev->device, width * 3,
GROMIT_ARROW_START, &x0, &y0, &width, &direction)) {
draw_arrow (data, ev->device, x0, y0, width, direction);
}
}
}

coord_list_free (data, ev->device);

Expand Down Expand Up @@ -540,7 +614,9 @@ void on_mainapp_selection_received (GtkWidget *widget,
g_printerr ("Unable to parse color. "
"Keeping default.\n");
}
GromitPaintContext* line_ctx = paint_context_new(data, GROMIT_PEN, fg_color, thickness, 0, thickness, thickness);
GromitPaintContext* line_ctx =
paint_context_new(data, GROMIT_PEN, fg_color, thickness,
0, GROMIT_ARROW_NONE, thickness, thickness);

GdkRectangle rect;
rect.x = MIN (startX,endX) - thickness / 2;
Expand Down
44 changes: 40 additions & 4 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ gboolean parse_config (GromitData *data)
GromitPaintType type;
GdkRGBA *fg_color=NULL;
guint width, arrowsize, minwidth, maxwidth;
GromitArrowType arrowtype;

/* try user config location */
filename = g_strjoin (G_DIR_SEPARATOR_S,
Expand Down Expand Up @@ -168,6 +169,8 @@ gboolean parse_config (GromitData *data)
scanner->config->int_2_float = 1;

g_scanner_scope_add_symbol (scanner, 0, "PEN", (gpointer) GROMIT_PEN);
g_scanner_scope_add_symbol (scanner, 0, "LINE", (gpointer) GROMIT_LINE);
g_scanner_scope_add_symbol (scanner, 0, "RECT", (gpointer) GROMIT_RECT);
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, "HOTKEY", HOTKEY_SYMBOL_VALUE);
Expand All @@ -186,8 +189,9 @@ gboolean parse_config (GromitData *data)
g_scanner_scope_add_symbol (scanner, 2, "size", (gpointer) 1);
g_scanner_scope_add_symbol (scanner, 2, "color", (gpointer) 2);
g_scanner_scope_add_symbol (scanner, 2, "arrowsize", (gpointer) 3);
g_scanner_scope_add_symbol (scanner, 2, "minsize", (gpointer) 4);
g_scanner_scope_add_symbol (scanner, 2, "maxsize", (gpointer) 5);
g_scanner_scope_add_symbol (scanner, 2, "arrowtype", (gpointer) 4);
g_scanner_scope_add_symbol (scanner, 2, "minsize", (gpointer) 5);
g_scanner_scope_add_symbol (scanner, 2, "maxsize", (gpointer) 6);

g_scanner_set_scope (scanner, 0);
scanner->config->scope_0_fallback = 0;
Expand Down Expand Up @@ -223,6 +227,7 @@ gboolean parse_config (GromitData *data)
type = GROMIT_PEN;
width = 7;
arrowsize = 0;
arrowtype = GROMIT_ARROW_NONE;
minwidth = 1;
maxwidth = G_MAXUINT;
fg_color = data->red;
Expand All @@ -244,6 +249,7 @@ gboolean parse_config (GromitData *data)
type = context_template->type;
width = context_template->width;
arrowsize = context_template->arrowsize;
arrowtype = context_template->arrow_type;
minwidth = context_template->minwidth;
maxwidth = context_template->maxwidth;
fg_color = context_template->paint_color;
Expand Down Expand Up @@ -334,8 +340,37 @@ gboolean parse_config (GromitData *data)
goto cleanup;
}
arrowsize = scanner->value.v_float;
arrowtype = GROMIT_ARROW_END;
}
else if ((intptr_t) scanner->value.v_symbol == 4)
{
token = g_scanner_get_next_token (scanner);
if (token != G_TOKEN_EQUAL_SIGN)
{
g_printerr ("Missing \"=\"... aborting\n");
goto cleanup;
}
token = g_scanner_get_next_token (scanner);
if (token != G_TOKEN_STRING)
{
g_printerr ("Missing Arrowsize (string)... "
"aborting\n");
goto cleanup;
}
if (! strcmp(scanner->value.v_string, "->"))
arrowtype = GROMIT_ARROW_END;
else if (! strcmp(scanner->value.v_string, "<-"))
arrowtype = GROMIT_ARROW_START;
else if (! strcmp(scanner->value.v_string, "<->"))
arrowtype = GROMIT_ARROW_DOUBLE;
else
{
g_printerr ("Arrow type must be one of -> <- <-> ... "
"aborting\n");
goto cleanup;
}
}
else if ((intptr_t) scanner->value.v_symbol == 5)
{
token = g_scanner_get_next_token (scanner);
if (token != G_TOKEN_EQUAL_SIGN)
Expand All @@ -352,7 +387,7 @@ gboolean parse_config (GromitData *data)
}
minwidth = scanner->value.v_float;
}
else if ((intptr_t) scanner->value.v_symbol == 5)
else if ((intptr_t) scanner->value.v_symbol == 6)
{
token = g_scanner_get_next_token (scanner);
if (token != G_TOKEN_EQUAL_SIGN)
Expand Down Expand Up @@ -394,7 +429,8 @@ gboolean parse_config (GromitData *data)
goto cleanup;
}

context = paint_context_new (data, type, fg_color, width, arrowsize, minwidth, maxwidth);
context = paint_context_new (data, type, fg_color, width,
arrowsize, arrowtype, minwidth, maxwidth);
g_hash_table_insert (data->tool_config, name, context);
}
else if (token == G_TOKEN_SYMBOL &&
Expand Down
38 changes: 25 additions & 13 deletions src/drawing.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

#include <math.h>
#include "drawing.h"
#include "main.h"

typedef struct
{
Expand Down Expand Up @@ -153,14 +154,20 @@ void coord_list_free (GromitData *data,
devdata->coordlist = NULL;
}


gboolean coord_list_get_arrow_param (GromitData *data,
GdkDevice *dev,
gint search_radius,
gint *ret_width,
gfloat *ret_direction)
/*
* for double-ended arrows, two separate calls are required
*/

gboolean coord_list_get_arrow_param (GromitData *data,
GdkDevice *dev,
gint search_radius,
GromitArrowType arrow_end,
gint *x0,
gint *y0,
gint *ret_width,
gfloat *ret_direction)
{
gint x0, y0, r2, dist;
gint r2, dist;
gboolean success = FALSE;
GromitStrokeCoordinate *cur_point, *valid_point;
/* get the data for this device */
Expand All @@ -172,20 +179,25 @@ gboolean coord_list_get_arrow_param (GromitData *data,

if (ptr)
{
if (arrow_end == GROMIT_ARROW_START)
ptr = g_list_last(ptr);
cur_point = ptr->data;
x0 = cur_point->x;
y0 = cur_point->y;
*x0 = cur_point->x;
*y0 = cur_point->y;
r2 = search_radius * search_radius;
dist = 0;

while (ptr && dist < r2)
{
ptr = ptr->next;
if (arrow_end == GROMIT_ARROW_END)
ptr = ptr->next;
else
ptr = ptr->prev;
if (ptr)
{
cur_point = ptr->data;
dist = (cur_point->x - x0) * (cur_point->x - x0) +
(cur_point->y - y0) * (cur_point->y - y0);
dist = (cur_point->x - *x0) * (cur_point->x - *x0) +
(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))
Expand All @@ -197,7 +209,7 @@ gboolean coord_list_get_arrow_param (GromitData *data,
{
*ret_width = MAX (valid_point->width * devdata->cur_context->arrowsize,
2);
*ret_direction = atan2 (y0 - valid_point->y, x0 - valid_point->x);
*ret_direction = atan2 (*y0 - valid_point->y, *x0 - valid_point->x);
success = TRUE;
}
}
Expand Down
Loading
Loading