app: Make layers expand when painting with paintbrush tool

When painting with paintbrush tool, the borders of active layer will
automatically expand to accomodate the stroke. The undo does not work
with expanding layers.
This commit is contained in:
Shubham 2023-06-02 23:13:39 +05:30 committed by Jehan
parent 7201858d65
commit ae7d37ebbd
3 changed files with 97 additions and 4 deletions

View File

@ -906,6 +906,10 @@ gimp_drawable_real_set_buffer (GimpDrawable *drawable,
}
g_set_object (&drawable->private->buffer, buffer);
if (gimp_drawable_is_painting (drawable))
g_set_object (&drawable->private->paint_buffer, buffer);
g_clear_object (&drawable->private->format_profile);
if (drawable->private->buffer_source_node)

View File

@ -815,6 +815,12 @@ gimp_brush_core_get_paint_buffer (GimpPaintCore *paint_core,
gint x1, y1, x2, y2;
gint drawable_width, drawable_height;
gint brush_width, brush_height;
gint new_width, new_height, new_off_x, new_off_y;
GimpContext *context = GIMP_CONTEXT (paint_options);
GimpFillType fill_type = GIMP_FILL_TRANSPARENT;
GeglBuffer *undo_buffer;
GeglBuffer *new_buffer;
const Babl *format;
gimp_brush_transform_size (core->brush,
core->scale, core->aspect_ratio,
@ -834,10 +840,87 @@ gimp_brush_core_get_paint_buffer (GimpPaintCore *paint_core,
drawable_width = gimp_item_get_width (GIMP_ITEM (drawable));
drawable_height = gimp_item_get_height (GIMP_ITEM (drawable));
x1 = CLAMP (x - 1, 0, drawable_width);
y1 = CLAMP (y - 1, 0, drawable_height);
x2 = CLAMP (x + brush_width + 1, 0, drawable_width);
y2 = CLAMP (y + brush_height + 1, 0, drawable_height);
x1 = x - 1;
y1 = y - 1;
x2 = x + brush_width + 1;
y2 = y + brush_height + 1;
/* resize buffers if the current stroke cannot fit in */
new_width = drawable_width;
new_height = drawable_height;
new_off_x = 0;
new_off_y = 0;
if (x1 < 0)
{
new_width += 100 - x1;
new_off_x += 100 - x1;
}
if (y1 < 0)
{
new_height += 100 - y1;
new_off_y += 100 - y1;
}
if (x2 > drawable_width)
{
new_width += x2 - drawable_width + 100;
}
if (y2 > drawable_height)
{
new_height += y2 - drawable_height + 100;
}
if (new_width != drawable_width || new_height != drawable_height || new_off_x || new_off_y)
{
GIMP_ITEM_GET_CLASS (GIMP_ITEM (drawable))->resize (GIMP_ITEM (drawable),
context, fill_type,
new_width, new_height,
new_off_x, new_off_y);
gimp_image_flush (gimp_item_get_image (GIMP_ITEM (drawable)));
format = gegl_buffer_get_format (paint_core->canvas_buffer);
new_buffer = gegl_buffer_new (GEGL_RECTANGLE (0, 0, new_width, new_height),
format);
gimp_gegl_buffer_copy (
paint_core->canvas_buffer,
GEGL_RECTANGLE (0, 0,
gegl_buffer_get_width (paint_core->canvas_buffer),
gegl_buffer_get_height (paint_core->canvas_buffer)),
GEGL_ABYSS_NONE,
new_buffer,
GEGL_RECTANGLE (new_off_x, new_off_y, 0, 0));
g_object_unref (paint_core->canvas_buffer);
paint_core->canvas_buffer = new_buffer;
undo_buffer = g_hash_table_lookup (paint_core->undo_buffers, drawable);
g_object_ref (undo_buffer);
format = gegl_buffer_get_format (undo_buffer);
new_buffer = gegl_buffer_new (GEGL_RECTANGLE (0, 0, new_width, new_height),
format);
gimp_gegl_buffer_copy (
undo_buffer,
GEGL_RECTANGLE (0, 0,
gegl_buffer_get_width (undo_buffer),
gegl_buffer_get_height (undo_buffer)),
GEGL_ABYSS_NONE,
new_buffer,
GEGL_RECTANGLE (new_off_x, new_off_y, 0, 0));
g_hash_table_insert (paint_core->undo_buffers, drawable, new_buffer);
g_object_unref (undo_buffer);
/* recalculating values for new buffers */
drawable_width = gimp_item_get_width (GIMP_ITEM (drawable));
drawable_height = gimp_item_get_height (GIMP_ITEM (drawable));
/* changing frame of reference */
x += new_off_x;
y += new_off_y;
}
x1 = CLAMP (x - 1, 0, drawable_width);
y1 = CLAMP (y - 1, 0, drawable_height);
x2 = CLAMP (x + brush_width + 1, 0, drawable_width);
y2 = CLAMP (y + brush_height + 1, 0, drawable_height);
/* configure the canvas buffer */
if ((x2 - x1) && (y2 - y1))

View File

@ -310,6 +310,12 @@ _gimp_paintbrush_motion (GimpPaintCore *paint_core,
&paint_buffer_y,
&paint_width,
&paint_height);
gimp_item_get_offset (GIMP_ITEM (drawable), &off_x, &off_y);
coords = *(gimp_symmetry_get_coords (sym, i));
coords.x -= off_x;
coords.y -= off_y;
if (! paint_buffer)
continue;