1 /* GIMP - The GNU Image Manipulation Program
2  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
16  */
17 
18 #include "config.h"
19 
20 #include <gegl.h>
21 #include <gtk/gtk.h>
22 
23 #include "libgimpmath/gimpmath.h"
24 #include "libgimpwidgets/gimpwidgets.h"
25 
26 #include "tools-types.h"
27 
28 #include "paint/gimpmybrushoptions.h"
29 
30 #include "display/gimpdisplay.h"
31 #include "display/gimpdisplayshell.h"
32 #include "display/gimpcanvasarc.h"
33 
34 #include "core/gimp.h"
35 
36 #include "widgets/gimphelp-ids.h"
37 
38 #include "gimpmybrushoptions-gui.h"
39 #include "gimpmybrushtool.h"
40 #include "gimptoolcontrol.h"
41 #include "core/gimpmybrush.h"
42 
43 #include "gimp-intl.h"
44 
45 G_DEFINE_TYPE (GimpMybrushTool, gimp_mybrush_tool, GIMP_TYPE_PAINT_TOOL)
46 
47 #define parent_class gimp_mybrush_tool_parent_class
48 
49 
50 static void   gimp_mybrush_tool_options_notify        (GimpTool         *tool,
51                                                        GimpToolOptions  *options,
52                                                        const GParamSpec *pspec);
53 
54 static GimpCanvasItem * gimp_mybrush_tool_get_outline (GimpPaintTool *paint_tool,
55                                                        GimpDisplay   *display,
56                                                        gdouble        x,
57                                                        gdouble        y);
58 
59 
60 void
gimp_mybrush_tool_register(GimpToolRegisterCallback callback,gpointer data)61 gimp_mybrush_tool_register (GimpToolRegisterCallback  callback,
62                             gpointer                  data)
63 {
64   (* callback) (GIMP_TYPE_MYBRUSH_TOOL,
65                 GIMP_TYPE_MYBRUSH_OPTIONS,
66                 gimp_mybrush_options_gui,
67                 GIMP_CONTEXT_PROP_MASK_FOREGROUND |
68                 GIMP_CONTEXT_PROP_MASK_BACKGROUND |
69                 GIMP_CONTEXT_PROP_MASK_OPACITY    |
70                 GIMP_CONTEXT_PROP_MASK_PAINT_MODE |
71                 GIMP_CONTEXT_PROP_MASK_MYBRUSH,
72                 "gimp-mypaint-brush-tool",
73                 _("MyPaint Brush"),
74                 _("MyPaint Brush Tool: Use MyPaint brushes in GIMP"),
75                 N_("M_yPaint Brush"), "Y",
76                 NULL, GIMP_HELP_TOOL_MYPAINT_BRUSH,
77                 GIMP_ICON_TOOL_MYPAINT_BRUSH,
78                 data);
79 }
80 
81 static void
gimp_mybrush_tool_class_init(GimpMybrushToolClass * klass)82 gimp_mybrush_tool_class_init (GimpMybrushToolClass *klass)
83 {
84   GimpToolClass      *tool_class       = GIMP_TOOL_CLASS (klass);
85   GimpPaintToolClass *paint_tool_class = GIMP_PAINT_TOOL_CLASS (klass);
86 
87   tool_class->options_notify    = gimp_mybrush_tool_options_notify;
88 
89   paint_tool_class->get_outline = gimp_mybrush_tool_get_outline;
90 }
91 
92 static void
gimp_mybrush_tool_init(GimpMybrushTool * mybrush_tool)93 gimp_mybrush_tool_init (GimpMybrushTool *mybrush_tool)
94 {
95   GimpTool *tool = GIMP_TOOL (mybrush_tool);
96 
97   gimp_tool_control_set_tool_cursor     (tool->control, GIMP_TOOL_CURSOR_INK);
98   gimp_tool_control_set_action_size     (tool->control,
99                                          "tools/tools-mypaint-brush-radius-set");
100   gimp_tool_control_set_action_hardness (tool->control,
101                                          "tools/tools-mypaint-brush-hardness-set");
102 
103   gimp_paint_tool_enable_color_picker (GIMP_PAINT_TOOL (mybrush_tool),
104                                        GIMP_COLOR_PICK_TARGET_FOREGROUND);
105 }
106 
107 static void
gimp_mybrush_tool_options_notify(GimpTool * tool,GimpToolOptions * options,const GParamSpec * pspec)108 gimp_mybrush_tool_options_notify (GimpTool         *tool,
109                                   GimpToolOptions  *options,
110                                   const GParamSpec *pspec)
111 {
112   GIMP_TOOL_CLASS (parent_class)->options_notify (tool, options, pspec);
113 
114   if (! strcmp (pspec->name, "radius"))
115     {
116       gimp_draw_tool_pause (GIMP_DRAW_TOOL (tool));
117       gimp_draw_tool_resume (GIMP_DRAW_TOOL (tool));
118     }
119 }
120 
121 static GimpCanvasItem *
gimp_mybrush_tool_get_outline(GimpPaintTool * paint_tool,GimpDisplay * display,gdouble x,gdouble y)122 gimp_mybrush_tool_get_outline (GimpPaintTool *paint_tool,
123                                GimpDisplay   *display,
124                                gdouble        x,
125                                gdouble        y)
126 {
127   GimpMybrushOptions *options = GIMP_MYBRUSH_TOOL_GET_OPTIONS (paint_tool);
128   GimpMybrush        *brush   = gimp_context_get_mybrush (GIMP_CONTEXT (options));
129   GimpCanvasItem     *item    = NULL;
130   GimpDisplayShell   *shell   = gimp_display_get_shell (display);
131 
132   gdouble radius = exp (options->radius) + 2 * options->radius * gimp_mybrush_get_offset_by_random (brush);
133   radius = MAX (MAX (4 / shell->scale_x, 4 / shell->scale_y), radius);
134 
135   item = gimp_mybrush_tool_create_cursor (paint_tool, display, x, y, radius);
136 
137   if (! item)
138     {
139       gimp_paint_tool_set_draw_fallback (paint_tool,
140                                          TRUE, radius);
141     }
142 
143   return item;
144 }
145 
146 GimpCanvasItem *
gimp_mybrush_tool_create_cursor(GimpPaintTool * paint_tool,GimpDisplay * display,gdouble x,gdouble y,gdouble radius)147 gimp_mybrush_tool_create_cursor (GimpPaintTool *paint_tool,
148                                  GimpDisplay   *display,
149                                  gdouble        x,
150                                  gdouble        y,
151                                  gdouble        radius)
152 {
153 
154   GimpDisplayShell     *shell;
155 
156   g_return_val_if_fail (GIMP_IS_PAINT_TOOL (paint_tool), NULL);
157   g_return_val_if_fail (GIMP_IS_DISPLAY (display), NULL);
158 
159   shell = gimp_display_get_shell (display);
160 
161   /*  don't draw the boundary if it becomes too small  */
162   if (SCALEX (shell, radius) > 4 &&
163       SCALEY (shell, radius) > 4)
164     {
165       return gimp_canvas_arc_new(shell, x, y, radius, radius, 0.0, 2 * G_PI, FALSE);
166     }
167 
168   return NULL;
169 }
170