app: don't add fg color to history in GimpEraser

Add GimpPaintbrush::get_color_history_color() virtual function,
which should return the color to be added to the color history upon
painting, if any.  The default implementation returns the
foreground color, when not using a pixmap brush or color-from-
gradient.

Override this function in GimpEraser, to return the background
color when the affected drawable has no alpha channel, instead of
overriding GimpPaintCore::paint() for this purpose.  This avoids
erroneously adding both the background and foreground colors to the
history.
This commit is contained in:
Ell 2019-05-29 04:25:38 -04:00
parent 890b554230
commit 86f4d4d41d
3 changed files with 90 additions and 85 deletions

View File

@ -38,27 +38,22 @@
#include "gimp-intl.h" #include "gimp-intl.h"
static void gimp_eraser_paint (GimpPaintCore *paint_core, static gboolean gimp_eraser_get_color_history_color (GimpPaintbrush *paintbrush,
GimpDrawable *drawable, GimpDrawable *drawable,
GimpPaintOptions *paint_options, GimpPaintOptions *paint_options,
GimpSymmetry *sym, GimpRGB *color);
GimpPaintState paint_state, static void gimp_eraser_get_paint_params (GimpPaintbrush *paintbrush,
guint32 time); GimpDrawable *drawable,
GimpPaintOptions *paint_options,
static void gimp_eraser_get_paint_params (GimpPaintbrush *paintbrush, GimpSymmetry *sym,
GimpDrawable *drawable, GimpLayerMode *paint_mode,
GimpPaintOptions *paint_options, GimpPaintApplicationMode *paint_appl_mode,
GimpSymmetry *sym, const GimpTempBuf **paint_pixmap,
GimpLayerMode *paint_mode, GimpRGB *paint_color);
GimpPaintApplicationMode *paint_appl_mode,
const GimpTempBuf **paint_pixmap,
GimpRGB *paint_color);
G_DEFINE_TYPE (GimpEraser, gimp_eraser, GIMP_TYPE_PAINTBRUSH) G_DEFINE_TYPE (GimpEraser, gimp_eraser, GIMP_TYPE_PAINTBRUSH)
#define parent_class gimp_eraser_parent_class
void void
gimp_eraser_register (Gimp *gimp, gimp_eraser_register (Gimp *gimp,
@ -75,12 +70,10 @@ gimp_eraser_register (Gimp *gimp,
static void static void
gimp_eraser_class_init (GimpEraserClass *klass) gimp_eraser_class_init (GimpEraserClass *klass)
{ {
GimpPaintCoreClass *paint_core_class = GIMP_PAINT_CORE_CLASS (klass);
GimpPaintbrushClass *paintbrush_class = GIMP_PAINTBRUSH_CLASS (klass); GimpPaintbrushClass *paintbrush_class = GIMP_PAINTBRUSH_CLASS (klass);
paint_core_class->paint = gimp_eraser_paint; paintbrush_class->get_color_history_color = gimp_eraser_get_color_history_color;
paintbrush_class->get_paint_params = gimp_eraser_get_paint_params;
paintbrush_class->get_paint_params = gimp_eraser_get_paint_params;
} }
static void static void
@ -88,41 +81,25 @@ gimp_eraser_init (GimpEraser *eraser)
{ {
} }
static void static gboolean
gimp_eraser_paint (GimpPaintCore *paint_core, gimp_eraser_get_color_history_color (GimpPaintbrush *paintbrush,
GimpDrawable *drawable, GimpDrawable *drawable,
GimpPaintOptions *paint_options, GimpPaintOptions *paint_options,
GimpSymmetry *sym, GimpRGB *color)
GimpPaintState paint_state,
guint32 time)
{ {
switch (paint_state) /* Erasing on a drawable without alpha is equivalent to
* drawing with background color. So let's save history.
*/
if (! gimp_drawable_has_alpha (drawable))
{ {
case GIMP_PAINT_STATE_INIT: GimpContext *context = GIMP_CONTEXT (paint_options);
{
if (! gimp_drawable_has_alpha (drawable))
{
/* Erasing on a drawable without alpha is equivalent to
* drawing with background color. So let's save history.
*/
GimpContext *context = GIMP_CONTEXT (paint_options);
GimpRGB background;
gimp_context_get_background (context, &background); gimp_context_get_background (context, color);
gimp_palettes_add_color_history (context->gimp,
&background);
} return TRUE;
}
break;
default:
break;
} }
GIMP_PAINT_CORE_CLASS (parent_class)->paint (paint_core, drawable, return FALSE;
paint_options, sym,
paint_state, time);
} }
static void static void

View File

@ -46,21 +46,25 @@
#include "gimp-intl.h" #include "gimp-intl.h"
static void gimp_paintbrush_paint (GimpPaintCore *paint_core, static void gimp_paintbrush_paint (GimpPaintCore *paint_core,
GimpDrawable *drawable, GimpDrawable *drawable,
GimpPaintOptions *paint_options, GimpPaintOptions *paint_options,
GimpSymmetry *sym, GimpSymmetry *sym,
GimpPaintState paint_state, GimpPaintState paint_state,
guint32 time); guint32 time);
static void gimp_paintbrush_real_get_paint_params (GimpPaintbrush *paintbrush, static gboolean gimp_paintbrush_real_get_color_history_color (GimpPaintbrush *paintbrush,
GimpDrawable *drawable, GimpDrawable *drawable,
GimpPaintOptions *paint_options, GimpPaintOptions *paint_options,
GimpSymmetry *sym, GimpRGB *color);
GimpLayerMode *paint_mode, static void gimp_paintbrush_real_get_paint_params (GimpPaintbrush *paintbrush,
GimpPaintApplicationMode *paint_appl_mode, GimpDrawable *drawable,
const GimpTempBuf **paint_pixmap, GimpPaintOptions *paint_options,
GimpRGB *paint_color); GimpSymmetry *sym,
GimpLayerMode *paint_mode,
GimpPaintApplicationMode *paint_appl_mode,
const GimpTempBuf **paint_pixmap,
GimpRGB *paint_color);
G_DEFINE_TYPE (GimpPaintbrush, gimp_paintbrush, GIMP_TYPE_BRUSH_CORE) G_DEFINE_TYPE (GimpPaintbrush, gimp_paintbrush, GIMP_TYPE_BRUSH_CORE)
@ -88,6 +92,7 @@ gimp_paintbrush_class_init (GimpPaintbrushClass *klass)
brush_core_class->handles_changing_brush = TRUE; brush_core_class->handles_changing_brush = TRUE;
klass->get_color_history_color = gimp_paintbrush_real_get_color_history_color;
klass->get_paint_params = gimp_paintbrush_real_get_paint_params; klass->get_paint_params = gimp_paintbrush_real_get_paint_params;
} }
@ -104,24 +109,21 @@ gimp_paintbrush_paint (GimpPaintCore *paint_core,
GimpPaintState paint_state, GimpPaintState paint_state,
guint32 time) guint32 time)
{ {
GimpPaintbrush *paintbrush = GIMP_PAINTBRUSH (paint_core);
switch (paint_state) switch (paint_state)
{ {
case GIMP_PAINT_STATE_INIT: case GIMP_PAINT_STATE_INIT:
{ {
GimpContext *context = GIMP_CONTEXT (paint_options); GimpRGB color;
GimpBrushCore *brush_core = GIMP_BRUSH_CORE (paint_core);
GimpDynamics *dynamics = gimp_context_get_dynamics (context);
if (! gimp_dynamics_is_output_enabled (dynamics, GIMP_DYNAMICS_OUTPUT_COLOR) && if (GIMP_PAINTBRUSH_GET_CLASS (paintbrush)->get_color_history_color &&
(! brush_core->brush || ! gimp_brush_get_pixmap (brush_core->brush))) GIMP_PAINTBRUSH_GET_CLASS (paintbrush)->get_color_history_color (
paintbrush, drawable, paint_options, &color))
{ {
/* We don't save gradient color history and pixmap brushes GimpContext *context = GIMP_CONTEXT (paint_options);
* have no color to save.
*/
GimpRGB foreground;
gimp_context_get_foreground (context, &foreground); gimp_palettes_add_color_history (context->gimp, &color);
gimp_palettes_add_color_history (context->gimp, &foreground);
} }
} }
break; break;
@ -133,8 +135,6 @@ gimp_paintbrush_paint (GimpPaintCore *paint_core,
case GIMP_PAINT_STATE_FINISH: case GIMP_PAINT_STATE_FINISH:
{ {
GimpPaintbrush *paintbrush = GIMP_PAINTBRUSH (paint_core);
if (paintbrush->paint_buffer) if (paintbrush->paint_buffer)
{ {
g_object_remove_weak_pointer ( g_object_remove_weak_pointer (
@ -150,6 +150,30 @@ gimp_paintbrush_paint (GimpPaintCore *paint_core,
} }
} }
static gboolean
gimp_paintbrush_real_get_color_history_color (GimpPaintbrush *paintbrush,
GimpDrawable *drawable,
GimpPaintOptions *paint_options,
GimpRGB *color)
{
GimpContext *context = GIMP_CONTEXT (paint_options);
GimpBrushCore *brush_core = GIMP_BRUSH_CORE (paintbrush);
GimpDynamics *dynamics = gimp_context_get_dynamics (context);
/* We don't save gradient color history and pixmap brushes
* have no color to save.
*/
if (gimp_dynamics_is_output_enabled (dynamics, GIMP_DYNAMICS_OUTPUT_COLOR) ||
(brush_core->brush && gimp_brush_get_pixmap (brush_core->brush)))
{
return FALSE;
}
gimp_context_get_foreground (context, color);
return TRUE;
}
static void static void
gimp_paintbrush_real_get_paint_params (GimpPaintbrush *paintbrush, gimp_paintbrush_real_get_paint_params (GimpPaintbrush *paintbrush,
GimpDrawable *drawable, GimpDrawable *drawable,

View File

@ -46,14 +46,18 @@ struct _GimpPaintbrushClass
GimpBrushCoreClass parent_class; GimpBrushCoreClass parent_class;
/* virtual functions */ /* virtual functions */
void (* get_paint_params) (GimpPaintbrush *paintbrush, gboolean (* get_color_history_color) (GimpPaintbrush *paintbrush,
GimpDrawable *drawable, GimpDrawable *drawable,
GimpPaintOptions *paint_options, GimpPaintOptions *paint_options,
GimpSymmetry *sym, GimpRGB *color);
GimpLayerMode *paint_mode, void (* get_paint_params) (GimpPaintbrush *paintbrush,
GimpPaintApplicationMode *paint_appl_mode, GimpDrawable *drawable,
const GimpTempBuf **paint_pixmap, GimpPaintOptions *paint_options,
GimpRGB *paint_color); GimpSymmetry *sym,
GimpLayerMode *paint_mode,
GimpPaintApplicationMode *paint_appl_mode,
const GimpTempBuf **paint_pixmap,
GimpRGB *paint_color);
}; };