Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeroen Bakker <jeroen@blender.org>2022-02-22 13:46:38 +0300
committerJeroen Bakker <jeroen@blender.org>2022-02-22 13:46:38 +0300
commit277c7d96ee6be370efb1d85ed593eaab4ced615e (patch)
tree0c4349416852a393dc4125a6f16c39217a787299
parent683a77256dc8845955a8592a730457e0bdc3c10c (diff)
Move image paint brush to its own compile unit.
-rw-r--r--source/blender/editors/sculpt_paint/CMakeLists.txt1
-rw-r--r--source/blender/editors/sculpt_paint/paint_image.cc380
-rw-r--r--source/blender/editors/sculpt_paint/paint_image_ops_paint.cc422
-rw-r--r--source/blender/editors/sculpt_paint/paint_intern.h1
4 files changed, 425 insertions, 379 deletions
diff --git a/source/blender/editors/sculpt_paint/CMakeLists.txt b/source/blender/editors/sculpt_paint/CMakeLists.txt
index c1febe5c86b..de7888aa1e8 100644
--- a/source/blender/editors/sculpt_paint/CMakeLists.txt
+++ b/source/blender/editors/sculpt_paint/CMakeLists.txt
@@ -30,6 +30,7 @@ set(SRC
paint_curve_undo.c
paint_hide.c
paint_image.cc
+ paint_image_ops_paint.cc
paint_image_2d.c
paint_image_2d_curve_mask.cc
paint_image_proj.c
diff --git a/source/blender/editors/sculpt_paint/paint_image.cc b/source/blender/editors/sculpt_paint/paint_image.cc
index eda686d808d..9413f872c3a 100644
--- a/source/blender/editors/sculpt_paint/paint_image.cc
+++ b/source/blender/editors/sculpt_paint/paint_image.cc
@@ -286,7 +286,7 @@ static bool image_paint_poll_ex(bContext *C, bool check_tool)
return false;
}
-static bool image_paint_poll(bContext *C)
+bool image_paint_poll(bContext *C)
{
return image_paint_poll_ex(C, true);
}
@@ -312,24 +312,6 @@ static bool image_paint_2d_clone_poll(bContext *C)
}
/************************ paint operator ************************/
-enum class eTexPaintMode {
- _2D,
- _3D_PROJECT,
-};
-
-struct PaintOperation {
- eTexPaintMode mode;
-
- void *stroke_handle;
-
- float prevmouse[2];
- float startmouse[2];
- double starttime;
-
- wmPaintCursor *cursor;
- ViewContext vc;
-};
-
bool paint_use_opacity_masking(Brush *brush)
{
return ((brush->flag & BRUSH_AIRBRUSH) || (brush->flag & BRUSH_DRAG_DOT) ||
@@ -419,366 +401,6 @@ void paint_brush_exit_tex(Brush *brush)
}
}
-static void gradient_draw_line(bContext *UNUSED(C), int x, int y, void *customdata)
-{
- PaintOperation *pop = (PaintOperation *)customdata;
-
- if (pop) {
- GPU_line_smooth(true);
- GPU_blend(GPU_BLEND_ALPHA);
-
- GPUVertFormat *format = immVertexFormat();
- uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_I32, 2, GPU_FETCH_INT_TO_FLOAT);
-
- ARegion *region = pop->vc.region;
-
- immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
-
- GPU_line_width(4.0);
- immUniformColor4ub(0, 0, 0, 255);
-
- immBegin(GPU_PRIM_LINES, 2);
- immVertex2i(pos, x, y);
- immVertex2i(
- pos, pop->startmouse[0] + region->winrct.xmin, pop->startmouse[1] + region->winrct.ymin);
- immEnd();
-
- GPU_line_width(2.0);
- immUniformColor4ub(255, 255, 255, 255);
-
- immBegin(GPU_PRIM_LINES, 2);
- immVertex2i(pos, x, y);
- immVertex2i(
- pos, pop->startmouse[0] + region->winrct.xmin, pop->startmouse[1] + region->winrct.ymin);
- immEnd();
-
- immUnbindProgram();
-
- GPU_blend(GPU_BLEND_NONE);
- GPU_line_smooth(false);
- }
-}
-
-static PaintOperation *texture_paint_init(bContext *C, wmOperator *op, const float mouse[2])
-{
- Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
- Scene *scene = CTX_data_scene(C);
- ToolSettings *settings = scene->toolsettings;
- PaintOperation *pop = MEM_cnew<PaintOperation>("PaintOperation"); /* caller frees */
- Brush *brush = BKE_paint_brush(&settings->imapaint.paint);
- int mode = RNA_enum_get(op->ptr, "mode");
- ED_view3d_viewcontext_init(C, &pop->vc, depsgraph);
-
- copy_v2_v2(pop->prevmouse, mouse);
- copy_v2_v2(pop->startmouse, mouse);
-
- /* initialize from context */
- if (CTX_wm_region_view3d(C)) {
- ViewLayer *view_layer = CTX_data_view_layer(C);
- Object *ob = OBACT(view_layer);
- bool uvs, mat, tex, stencil;
- if (!ED_paint_proj_mesh_data_check(scene, ob, &uvs, &mat, &tex, &stencil)) {
- ED_paint_data_warning(op->reports, uvs, mat, tex, stencil);
- MEM_delete(pop);
- WM_event_add_notifier(C, NC_SCENE | ND_TOOLSETTINGS, nullptr);
- return nullptr;
- }
- pop->mode = eTexPaintMode::_3D_PROJECT;
- pop->stroke_handle = paint_proj_new_stroke(C, ob, mouse, mode);
- }
- else {
- pop->mode = eTexPaintMode::_2D;
- pop->stroke_handle = paint_2d_new_stroke(C, op, mode);
- }
-
- if (!pop->stroke_handle) {
- MEM_delete(pop);
- return nullptr;
- }
-
- if ((brush->imagepaint_tool == PAINT_TOOL_FILL) && (brush->flag & BRUSH_USE_GRADIENT)) {
- pop->cursor = WM_paint_cursor_activate(
- SPACE_TYPE_ANY, RGN_TYPE_ANY, image_paint_poll, gradient_draw_line, pop);
- }
-
- settings->imapaint.flag |= IMAGEPAINT_DRAWING;
- ED_image_undo_push_begin(op->type->name, PAINT_MODE_TEXTURE_2D);
-
- return pop;
-}
-
-static void paint_stroke_update_step(bContext *C,
- wmOperator *UNUSED(op),
- struct PaintStroke *stroke,
- PointerRNA *itemptr)
-{
- PaintOperation *pop = static_cast<PaintOperation *>(paint_stroke_mode_data(stroke));
- Scene *scene = CTX_data_scene(C);
- ToolSettings *toolsettings = CTX_data_tool_settings(C);
- UnifiedPaintSettings *ups = &toolsettings->unified_paint_settings;
- Brush *brush = BKE_paint_brush(&toolsettings->imapaint.paint);
-
- float alphafac = (brush->flag & BRUSH_ACCUMULATE) ? ups->overlap_factor : 1.0f;
-
- /* initial brush values. Maybe it should be considered moving these to stroke system */
- float startalpha = BKE_brush_alpha_get(scene, brush);
-
- float mouse[2];
- float pressure;
- float size;
- float distance = paint_stroke_distance_get(stroke);
- int eraser;
-
- RNA_float_get_array(itemptr, "mouse", mouse);
- pressure = RNA_float_get(itemptr, "pressure");
- eraser = RNA_boolean_get(itemptr, "pen_flip");
- size = RNA_float_get(itemptr, "size");
-
- /* stroking with fill tool only acts on stroke end */
- if (brush->imagepaint_tool == PAINT_TOOL_FILL) {
- copy_v2_v2(pop->prevmouse, mouse);
- return;
- }
-
- if (BKE_brush_use_alpha_pressure(brush)) {
- BKE_brush_alpha_set(scene, brush, max_ff(0.0f, startalpha * pressure * alphafac));
- }
- else {
- BKE_brush_alpha_set(scene, brush, max_ff(0.0f, startalpha * alphafac));
- }
-
- if ((brush->flag & BRUSH_DRAG_DOT) || (brush->flag & BRUSH_ANCHORED)) {
- UndoStack *ustack = CTX_wm_manager(C)->undo_stack;
- ED_image_undo_restore(ustack->step_init);
- }
-
- switch (pop->mode) {
- case eTexPaintMode::_2D:
- paint_2d_stroke(pop->stroke_handle, pop->prevmouse, mouse, eraser, pressure, distance, size);
- break;
-
- case eTexPaintMode::_3D_PROJECT:
- paint_proj_stroke(
- C, pop->stroke_handle, pop->prevmouse, mouse, eraser, pressure, distance, size);
- break;
- }
-
- copy_v2_v2(pop->prevmouse, mouse);
-
- /* restore brush values */
- BKE_brush_alpha_set(scene, brush, startalpha);
-}
-
-static void paint_stroke_redraw(const bContext *C, struct PaintStroke *stroke, bool final)
-{
- PaintOperation *pop = static_cast<PaintOperation *>(paint_stroke_mode_data(stroke));
-
- switch (pop->mode) {
- case eTexPaintMode::_2D:
- paint_2d_redraw(C, pop->stroke_handle, final);
- break;
-
- case eTexPaintMode::_3D_PROJECT:
- paint_proj_redraw(C, pop->stroke_handle, final);
- break;
- }
-}
-
-static void paint_stroke_done(const bContext *C, struct PaintStroke *stroke)
-{
- Scene *scene = CTX_data_scene(C);
- ToolSettings *toolsettings = scene->toolsettings;
- PaintOperation *pop = static_cast<PaintOperation *>(paint_stroke_mode_data(stroke));
- Brush *brush = BKE_paint_brush(&toolsettings->imapaint.paint);
-
- toolsettings->imapaint.flag &= ~IMAGEPAINT_DRAWING;
-
- if (brush->imagepaint_tool == PAINT_TOOL_FILL) {
- if (brush->flag & BRUSH_USE_GRADIENT) {
- switch (pop->mode) {
- case eTexPaintMode::_2D:
- paint_2d_gradient_fill(C, brush, pop->startmouse, pop->prevmouse, pop->stroke_handle);
- break;
-
- case eTexPaintMode::_3D_PROJECT:
- paint_proj_stroke(C,
- pop->stroke_handle,
- pop->startmouse,
- pop->prevmouse,
- paint_stroke_flipped(stroke),
- 1.0,
- 0.0,
- BKE_brush_size_get(scene, brush));
- /* two redraws, one for GPU update, one for notification */
- paint_proj_redraw(C, pop->stroke_handle, false);
- paint_proj_redraw(C, pop->stroke_handle, true);
- break;
- }
- }
- else {
- switch (pop->mode) {
- case eTexPaintMode::_2D: {
- float color[3];
- if (paint_stroke_inverted(stroke)) {
- srgb_to_linearrgb_v3_v3(color, BKE_brush_secondary_color_get(scene, brush));
- }
- else {
- srgb_to_linearrgb_v3_v3(color, BKE_brush_color_get(scene, brush));
- }
- paint_2d_bucket_fill(
- C, color, brush, pop->startmouse, pop->prevmouse, pop->stroke_handle);
- break;
- }
-
- case eTexPaintMode::_3D_PROJECT:
- paint_proj_stroke(C,
- pop->stroke_handle,
- pop->startmouse,
- pop->prevmouse,
- paint_stroke_flipped(stroke),
- 1.0,
- 0.0,
- BKE_brush_size_get(scene, brush));
- /* two redraws, one for GPU update, one for notification */
- paint_proj_redraw(C, pop->stroke_handle, false);
- paint_proj_redraw(C, pop->stroke_handle, true);
- break;
- }
- }
- }
-
- switch (pop->mode) {
- case eTexPaintMode::_2D:
- paint_2d_stroke_done(pop->stroke_handle);
- break;
-
- case eTexPaintMode::_3D_PROJECT:
- paint_proj_stroke_done(pop->stroke_handle);
- break;
- }
-
- if (pop->cursor) {
- WM_paint_cursor_end(pop->cursor);
- }
-
- ED_image_undo_push_end();
-
- /* duplicate warning, see texpaint_init */
-#if 0
- if (pop->s.warnmultifile) {
- BKE_reportf(op->reports,
- RPT_WARNING,
- "Image requires 4 color channels to paint: %s",
- pop->s.warnmultifile);
- }
- if (pop->s.warnpackedfile) {
- BKE_reportf(op->reports,
- RPT_WARNING,
- "Packed MultiLayer files cannot be painted: %s",
- pop->s.warnpackedfile);
- }
-#endif
- MEM_delete(pop);
-}
-
-static bool paint_stroke_test_start(bContext *C, wmOperator *op, const float mouse[2])
-{
- PaintOperation *pop;
-
- /* TODO: Should avoid putting this here. Instead, last position should be requested
- * from stroke system. */
-
- if (!(pop = texture_paint_init(C, op, mouse))) {
- return false;
- }
-
- paint_stroke_set_mode_data(static_cast<PaintStroke *>(op->customdata), pop);
-
- return true;
-}
-
-static int paint_invoke(bContext *C, wmOperator *op, const wmEvent *event)
-{
- int retval;
-
- op->customdata = paint_stroke_new(C,
- op,
- nullptr,
- paint_stroke_test_start,
- paint_stroke_update_step,
- paint_stroke_redraw,
- paint_stroke_done,
- event->type);
-
- if ((retval = op->type->modal(C, op, event)) == OPERATOR_FINISHED) {
- paint_stroke_free(C, op, static_cast<PaintStroke *>(op->customdata));
- return OPERATOR_FINISHED;
- }
- /* add modal handler */
- WM_event_add_modal_handler(C, op);
-
- OPERATOR_RETVAL_CHECK(retval);
- BLI_assert(retval == OPERATOR_RUNNING_MODAL);
-
- return OPERATOR_RUNNING_MODAL;
-}
-
-static int paint_exec(bContext *C, wmOperator *op)
-{
- PropertyRNA *strokeprop;
- PointerRNA firstpoint;
- float mouse[2];
-
- strokeprop = RNA_struct_find_property(op->ptr, "stroke");
-
- if (!RNA_property_collection_lookup_int(op->ptr, strokeprop, 0, &firstpoint)) {
- return OPERATOR_CANCELLED;
- }
-
- RNA_float_get_array(&firstpoint, "mouse", mouse);
-
- op->customdata = paint_stroke_new(C,
- op,
- nullptr,
- paint_stroke_test_start,
- paint_stroke_update_step,
- paint_stroke_redraw,
- paint_stroke_done,
- 0);
- /* frees op->customdata */
- return paint_stroke_exec(C, op, static_cast<PaintStroke *>(op->customdata));
-}
-
-static int paint_modal(bContext *C, wmOperator *op, const wmEvent *event)
-{
- return paint_stroke_modal(C, op, event, static_cast<PaintStroke *>(op->customdata));
-}
-
-static void paint_cancel(bContext *C, wmOperator *op)
-{
- paint_stroke_cancel(C, op, static_cast<PaintStroke *>(op->customdata));
-}
-
-void PAINT_OT_image_paint(wmOperatorType *ot)
-{
- /* identifiers */
- ot->name = "Image Paint";
- ot->idname = "PAINT_OT_image_paint";
- ot->description = "Paint a stroke into the image";
-
- /* api callbacks */
- ot->invoke = paint_invoke;
- ot->modal = paint_modal;
- ot->exec = paint_exec;
- ot->poll = image_paint_poll;
- ot->cancel = paint_cancel;
-
- /* flags */
- ot->flag = OPTYPE_BLOCKING;
-
- paint_stroke_operator_properties(ot);
-}
-
bool get_imapaint_zoom(bContext *C, float *zoomx, float *zoomy)
{
ScrArea *area = CTX_wm_area(C);
diff --git a/source/blender/editors/sculpt_paint/paint_image_ops_paint.cc b/source/blender/editors/sculpt_paint/paint_image_ops_paint.cc
new file mode 100644
index 00000000000..05c637531be
--- /dev/null
+++ b/source/blender/editors/sculpt_paint/paint_image_ops_paint.cc
@@ -0,0 +1,422 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later
+ * Copyright 2001-2002 NaN Holding BV. All rights reserved. */
+
+/** \file
+ * \ingroup edsculpt
+ * \brief Functions to paint images in 2D and 3D.
+ */
+
+#include "DNA_brush_types.h"
+#include "DNA_color_types.h"
+#include "DNA_scene_types.h"
+#include "DNA_space_types.h"
+
+#include "BKE_brush.h"
+#include "BKE_context.h"
+#include "BKE_paint.h"
+#include "BKE_undo_system.h"
+
+#include "DEG_depsgraph.h"
+
+#include "ED_paint.h"
+#include "ED_view3d.h"
+
+#include "GPU_immediate.h"
+#include "GPU_state.h"
+
+#include "MEM_guardedalloc.h"
+
+#include "RNA_access.h"
+#include "RNA_define.h"
+
+#include "WM_api.h"
+#include "WM_message.h"
+#include "WM_toolsystem.h"
+#include "WM_types.h"
+
+#include "paint_intern.h"
+
+namespace blender::ed::sculpt_paint::image::ops::paint {
+
+enum class eTexPaintMode {
+ _2D,
+ _3D_PROJECT,
+};
+
+struct PaintOperation {
+ eTexPaintMode mode;
+
+ void *stroke_handle;
+
+ float prevmouse[2];
+ float startmouse[2];
+ double starttime;
+
+ wmPaintCursor *cursor;
+ ViewContext vc;
+};
+
+static void gradient_draw_line(bContext *UNUSED(C), int x, int y, void *customdata)
+{
+ PaintOperation *pop = (PaintOperation *)customdata;
+
+ if (pop) {
+ GPU_line_smooth(true);
+ GPU_blend(GPU_BLEND_ALPHA);
+
+ GPUVertFormat *format = immVertexFormat();
+ uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_I32, 2, GPU_FETCH_INT_TO_FLOAT);
+
+ ARegion *region = pop->vc.region;
+
+ immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
+
+ GPU_line_width(4.0);
+ immUniformColor4ub(0, 0, 0, 255);
+
+ immBegin(GPU_PRIM_LINES, 2);
+ immVertex2i(pos, x, y);
+ immVertex2i(
+ pos, pop->startmouse[0] + region->winrct.xmin, pop->startmouse[1] + region->winrct.ymin);
+ immEnd();
+
+ GPU_line_width(2.0);
+ immUniformColor4ub(255, 255, 255, 255);
+
+ immBegin(GPU_PRIM_LINES, 2);
+ immVertex2i(pos, x, y);
+ immVertex2i(
+ pos, pop->startmouse[0] + region->winrct.xmin, pop->startmouse[1] + region->winrct.ymin);
+ immEnd();
+
+ immUnbindProgram();
+
+ GPU_blend(GPU_BLEND_NONE);
+ GPU_line_smooth(false);
+ }
+}
+
+static PaintOperation *texture_paint_init(bContext *C, wmOperator *op, const float mouse[2])
+{
+ Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
+ Scene *scene = CTX_data_scene(C);
+ ToolSettings *settings = scene->toolsettings;
+ PaintOperation *pop = MEM_cnew<PaintOperation>("PaintOperation"); /* caller frees */
+ Brush *brush = BKE_paint_brush(&settings->imapaint.paint);
+ int mode = RNA_enum_get(op->ptr, "mode");
+ ED_view3d_viewcontext_init(C, &pop->vc, depsgraph);
+
+ copy_v2_v2(pop->prevmouse, mouse);
+ copy_v2_v2(pop->startmouse, mouse);
+
+ /* initialize from context */
+ if (CTX_wm_region_view3d(C)) {
+ ViewLayer *view_layer = CTX_data_view_layer(C);
+ Object *ob = OBACT(view_layer);
+ bool uvs, mat, tex, stencil;
+ if (!ED_paint_proj_mesh_data_check(scene, ob, &uvs, &mat, &tex, &stencil)) {
+ ED_paint_data_warning(op->reports, uvs, mat, tex, stencil);
+ MEM_delete(pop);
+ WM_event_add_notifier(C, NC_SCENE | ND_TOOLSETTINGS, nullptr);
+ return nullptr;
+ }
+ pop->mode = eTexPaintMode::_3D_PROJECT;
+ pop->stroke_handle = paint_proj_new_stroke(C, ob, mouse, mode);
+ }
+ else {
+ pop->mode = eTexPaintMode::_2D;
+ pop->stroke_handle = paint_2d_new_stroke(C, op, mode);
+ }
+
+ if (!pop->stroke_handle) {
+ MEM_delete(pop);
+ return nullptr;
+ }
+
+ if ((brush->imagepaint_tool == PAINT_TOOL_FILL) && (brush->flag & BRUSH_USE_GRADIENT)) {
+ pop->cursor = WM_paint_cursor_activate(
+ SPACE_TYPE_ANY, RGN_TYPE_ANY, image_paint_poll, gradient_draw_line, pop);
+ }
+
+ settings->imapaint.flag |= IMAGEPAINT_DRAWING;
+ ED_image_undo_push_begin(op->type->name, PAINT_MODE_TEXTURE_2D);
+
+ return pop;
+}
+
+static void paint_stroke_update_step(bContext *C,
+ wmOperator *UNUSED(op),
+ struct PaintStroke *stroke,
+ PointerRNA *itemptr)
+{
+ PaintOperation *pop = static_cast<PaintOperation *>(paint_stroke_mode_data(stroke));
+ Scene *scene = CTX_data_scene(C);
+ ToolSettings *toolsettings = CTX_data_tool_settings(C);
+ UnifiedPaintSettings *ups = &toolsettings->unified_paint_settings;
+ Brush *brush = BKE_paint_brush(&toolsettings->imapaint.paint);
+
+ float alphafac = (brush->flag & BRUSH_ACCUMULATE) ? ups->overlap_factor : 1.0f;
+
+ /* initial brush values. Maybe it should be considered moving these to stroke system */
+ float startalpha = BKE_brush_alpha_get(scene, brush);
+
+ float mouse[2];
+ float pressure;
+ float size;
+ float distance = paint_stroke_distance_get(stroke);
+ int eraser;
+
+ RNA_float_get_array(itemptr, "mouse", mouse);
+ pressure = RNA_float_get(itemptr, "pressure");
+ eraser = RNA_boolean_get(itemptr, "pen_flip");
+ size = RNA_float_get(itemptr, "size");
+
+ /* stroking with fill tool only acts on stroke end */
+ if (brush->imagepaint_tool == PAINT_TOOL_FILL) {
+ copy_v2_v2(pop->prevmouse, mouse);
+ return;
+ }
+
+ if (BKE_brush_use_alpha_pressure(brush)) {
+ BKE_brush_alpha_set(scene, brush, max_ff(0.0f, startalpha * pressure * alphafac));
+ }
+ else {
+ BKE_brush_alpha_set(scene, brush, max_ff(0.0f, startalpha * alphafac));
+ }
+
+ if ((brush->flag & BRUSH_DRAG_DOT) || (brush->flag & BRUSH_ANCHORED)) {
+ UndoStack *ustack = CTX_wm_manager(C)->undo_stack;
+ ED_image_undo_restore(ustack->step_init);
+ }
+
+ switch (pop->mode) {
+ case eTexPaintMode::_2D:
+ paint_2d_stroke(pop->stroke_handle, pop->prevmouse, mouse, eraser, pressure, distance, size);
+ break;
+
+ case eTexPaintMode::_3D_PROJECT:
+ paint_proj_stroke(
+ C, pop->stroke_handle, pop->prevmouse, mouse, eraser, pressure, distance, size);
+ break;
+ }
+
+ copy_v2_v2(pop->prevmouse, mouse);
+
+ /* restore brush values */
+ BKE_brush_alpha_set(scene, brush, startalpha);
+}
+
+static void paint_stroke_redraw(const bContext *C, struct PaintStroke *stroke, bool final)
+{
+ PaintOperation *pop = static_cast<PaintOperation *>(paint_stroke_mode_data(stroke));
+
+ switch (pop->mode) {
+ case eTexPaintMode::_2D:
+ paint_2d_redraw(C, pop->stroke_handle, final);
+ break;
+
+ case eTexPaintMode::_3D_PROJECT:
+ paint_proj_redraw(C, pop->stroke_handle, final);
+ break;
+ }
+}
+
+static void paint_stroke_done(const bContext *C, struct PaintStroke *stroke)
+{
+ Scene *scene = CTX_data_scene(C);
+ ToolSettings *toolsettings = scene->toolsettings;
+ PaintOperation *pop = static_cast<PaintOperation *>(paint_stroke_mode_data(stroke));
+ Brush *brush = BKE_paint_brush(&toolsettings->imapaint.paint);
+
+ toolsettings->imapaint.flag &= ~IMAGEPAINT_DRAWING;
+
+ if (brush->imagepaint_tool == PAINT_TOOL_FILL) {
+ if (brush->flag & BRUSH_USE_GRADIENT) {
+ switch (pop->mode) {
+ case eTexPaintMode::_2D:
+ paint_2d_gradient_fill(C, brush, pop->startmouse, pop->prevmouse, pop->stroke_handle);
+ break;
+
+ case eTexPaintMode::_3D_PROJECT:
+ paint_proj_stroke(C,
+ pop->stroke_handle,
+ pop->startmouse,
+ pop->prevmouse,
+ paint_stroke_flipped(stroke),
+ 1.0,
+ 0.0,
+ BKE_brush_size_get(scene, brush));
+ /* two redraws, one for GPU update, one for notification */
+ paint_proj_redraw(C, pop->stroke_handle, false);
+ paint_proj_redraw(C, pop->stroke_handle, true);
+ break;
+ }
+ }
+ else {
+ switch (pop->mode) {
+ case eTexPaintMode::_2D: {
+ float color[3];
+ if (paint_stroke_inverted(stroke)) {
+ srgb_to_linearrgb_v3_v3(color, BKE_brush_secondary_color_get(scene, brush));
+ }
+ else {
+ srgb_to_linearrgb_v3_v3(color, BKE_brush_color_get(scene, brush));
+ }
+ paint_2d_bucket_fill(
+ C, color, brush, pop->startmouse, pop->prevmouse, pop->stroke_handle);
+ break;
+ }
+
+ case eTexPaintMode::_3D_PROJECT:
+ paint_proj_stroke(C,
+ pop->stroke_handle,
+ pop->startmouse,
+ pop->prevmouse,
+ paint_stroke_flipped(stroke),
+ 1.0,
+ 0.0,
+ BKE_brush_size_get(scene, brush));
+ /* two redraws, one for GPU update, one for notification */
+ paint_proj_redraw(C, pop->stroke_handle, false);
+ paint_proj_redraw(C, pop->stroke_handle, true);
+ break;
+ }
+ }
+ }
+
+ switch (pop->mode) {
+ case eTexPaintMode::_2D:
+ paint_2d_stroke_done(pop->stroke_handle);
+ break;
+
+ case eTexPaintMode::_3D_PROJECT:
+ paint_proj_stroke_done(pop->stroke_handle);
+ break;
+ }
+
+ if (pop->cursor) {
+ WM_paint_cursor_end(pop->cursor);
+ }
+
+ ED_image_undo_push_end();
+
+ /* duplicate warning, see texpaint_init */
+#if 0
+ if (pop->s.warnmultifile) {
+ BKE_reportf(op->reports,
+ RPT_WARNING,
+ "Image requires 4 color channels to paint: %s",
+ pop->s.warnmultifile);
+ }
+ if (pop->s.warnpackedfile) {
+ BKE_reportf(op->reports,
+ RPT_WARNING,
+ "Packed MultiLayer files cannot be painted: %s",
+ pop->s.warnpackedfile);
+ }
+#endif
+ MEM_delete(pop);
+}
+
+static bool paint_stroke_test_start(bContext *C, wmOperator *op, const float mouse[2])
+{
+ PaintOperation *pop;
+
+ /* TODO: Should avoid putting this here. Instead, last position should be requested
+ * from stroke system. */
+
+ if (!(pop = texture_paint_init(C, op, mouse))) {
+ return false;
+ }
+
+ paint_stroke_set_mode_data(static_cast<PaintStroke *>(op->customdata), pop);
+
+ return true;
+}
+
+static int paint_invoke(bContext *C, wmOperator *op, const wmEvent *event)
+{
+ int retval;
+
+ op->customdata = paint_stroke_new(C,
+ op,
+ nullptr,
+ paint_stroke_test_start,
+ paint_stroke_update_step,
+ paint_stroke_redraw,
+ paint_stroke_done,
+ event->type);
+
+ if ((retval = op->type->modal(C, op, event)) == OPERATOR_FINISHED) {
+ paint_stroke_free(C, op, static_cast<PaintStroke *>(op->customdata));
+ return OPERATOR_FINISHED;
+ }
+ /* add modal handler */
+ WM_event_add_modal_handler(C, op);
+
+ OPERATOR_RETVAL_CHECK(retval);
+ BLI_assert(retval == OPERATOR_RUNNING_MODAL);
+
+ return OPERATOR_RUNNING_MODAL;
+}
+
+static int paint_exec(bContext *C, wmOperator *op)
+{
+ PropertyRNA *strokeprop;
+ PointerRNA firstpoint;
+ float mouse[2];
+
+ strokeprop = RNA_struct_find_property(op->ptr, "stroke");
+
+ if (!RNA_property_collection_lookup_int(op->ptr, strokeprop, 0, &firstpoint)) {
+ return OPERATOR_CANCELLED;
+ }
+
+ RNA_float_get_array(&firstpoint, "mouse", mouse);
+
+ op->customdata = paint_stroke_new(C,
+ op,
+ nullptr,
+ paint_stroke_test_start,
+ paint_stroke_update_step,
+ paint_stroke_redraw,
+ paint_stroke_done,
+ 0);
+ /* frees op->customdata */
+ return paint_stroke_exec(C, op, static_cast<PaintStroke *>(op->customdata));
+}
+
+static int paint_modal(bContext *C, wmOperator *op, const wmEvent *event)
+{
+ return paint_stroke_modal(C, op, event, static_cast<PaintStroke *>(op->customdata));
+}
+
+static void paint_cancel(bContext *C, wmOperator *op)
+{
+ paint_stroke_cancel(C, op, static_cast<PaintStroke *>(op->customdata));
+}
+} // namespace blender::ed::sculpt_paint::image::ops::paint
+
+extern "C" {
+void PAINT_OT_image_paint(wmOperatorType *ot)
+{
+ using namespace blender::ed::sculpt_paint::image::ops::paint;
+
+ /* identifiers */
+ ot->name = "Image Paint";
+ ot->idname = "PAINT_OT_image_paint";
+ ot->description = "Paint a stroke into the image";
+
+ /* api callbacks */
+ ot->invoke = paint_invoke;
+ ot->modal = paint_modal;
+ ot->exec = paint_exec;
+ ot->poll = image_paint_poll;
+ ot->cancel = paint_cancel;
+
+ /* flags */
+ ot->flag = OPTYPE_BLOCKING;
+
+ paint_stroke_operator_properties(ot);
+}
+}
diff --git a/source/blender/editors/sculpt_paint/paint_intern.h b/source/blender/editors/sculpt_paint/paint_intern.h
index ba363f687dc..82fdf49c28e 100644
--- a/source/blender/editors/sculpt_paint/paint_intern.h
+++ b/source/blender/editors/sculpt_paint/paint_intern.h
@@ -269,6 +269,7 @@ void paint_brush_color_get(struct Scene *scene,
bool paint_use_opacity_masking(struct Brush *brush);
void paint_brush_init_tex(struct Brush *brush);
void paint_brush_exit_tex(struct Brush *brush);
+bool image_paint_poll(struct bContext *C);
void PAINT_OT_grab_clone(struct wmOperatorType *ot);
void PAINT_OT_sample_color(struct wmOperatorType *ot);