Blender  V2.93
paint_toolslots.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
21 #include <limits.h>
22 
23 #include "MEM_guardedalloc.h"
24 
25 #include "DNA_brush_types.h"
26 #include "DNA_modifier_types.h"
27 #include "DNA_scene_types.h"
28 
29 #include "BLI_utildefines.h"
30 
31 #include "BKE_brush.h"
32 #include "BKE_lib_id.h"
33 #include "BKE_main.h"
34 #include "BKE_paint.h"
35 
36 /* -------------------------------------------------------------------- */
44 {
45  /* Tool slots are 'uchar'. */
46  BLI_assert(len <= UCHAR_MAX);
47  if (paint->tool_slots_len < len) {
48  paint->tool_slots = MEM_recallocN(paint->tool_slots, sizeof(*paint->tool_slots) * len);
49  paint->tool_slots_len = len;
50  }
51 }
52 
53 static void paint_toolslots_init(Main *bmain, Paint *paint)
54 {
55  if (paint == NULL) {
56  return;
57  }
58  const eObjectMode ob_mode = paint->runtime.ob_mode;
59  BLI_assert(paint->runtime.tool_offset && ob_mode);
60  for (Brush *brush = bmain->brushes.first; brush; brush = brush->id.next) {
61  if (brush->ob_mode & ob_mode) {
62  const int slot_index = BKE_brush_tool_get(brush, paint);
63  BKE_paint_toolslots_len_ensure(paint, slot_index + 1);
64  if (paint->tool_slots[slot_index].brush == NULL) {
65  paint->tool_slots[slot_index].brush = brush;
66  id_us_plus(&brush->id);
67  }
68  }
69  }
70 }
71 
75 static void paint_toolslots_init_with_runtime(Main *bmain, ToolSettings *ts, Paint *paint)
76 {
77  if (paint == NULL) {
78  return;
79  }
80 
81  /* Needed so #Paint_Runtime is updated when versioning. */
82  BKE_paint_runtime_init(ts, paint);
83  paint_toolslots_init(bmain, paint);
84 }
85 
87 {
88  for (Scene *scene = bmain->scenes.first; scene; scene = scene->id.next) {
91  if (ts->sculpt) {
93  }
94  if (ts->vpaint) {
96  }
97  if (ts->wpaint) {
99  }
100  if (ts->uvsculpt) {
102  }
103  if (ts->gp_paint) {
105  }
106  if (ts->gp_vertexpaint) {
108  }
109  if (ts->gp_sculptpaint) {
111  }
112  if (ts->gp_weightpaint) {
114  }
115  }
116 }
117 
121 {
122  const uint tool_offset = paint->runtime.tool_offset;
123  UNUSED_VARS_NDEBUG(tool_offset);
124  BLI_assert(tool_offset != 0);
125  const int slot_index = BKE_brush_tool_get(brush, paint);
126  BKE_paint_toolslots_len_ensure(paint, slot_index + 1);
127  PaintToolSlot *tslot = &paint->tool_slots[slot_index];
128  id_us_plus(&brush->id);
129  if (tslot->brush) {
130  id_us_min(&tslot->brush->id);
131  }
132  tslot->brush = brush;
133 }
134 
136 {
137  if (paint->brush == NULL) {
138  return;
139  }
141 }
142 
148 {
149  /* Clear slots with invalid slots or mode (unlikely but possible). */
150  const uint tool_offset = paint->runtime.tool_offset;
151  UNUSED_VARS_NDEBUG(tool_offset);
152  const eObjectMode ob_mode = paint->runtime.ob_mode;
153  BLI_assert(tool_offset && ob_mode);
154  for (int i = 0; i < paint->tool_slots_len; i++) {
155  PaintToolSlot *tslot = &paint->tool_slots[i];
156  if (tslot->brush) {
157  if ((i != BKE_brush_tool_get(tslot->brush, paint)) ||
158  (tslot->brush->ob_mode & ob_mode) == 0) {
159  id_us_min(&tslot->brush->id);
160  tslot->brush = NULL;
161  }
162  }
163  }
164 
165  /* Unlikely but possible the active brush is not currently using a slot. */
167 
168  /* Fill slots from brushes. */
169  paint_toolslots_init(bmain, paint);
170 }
171 
172 Brush *BKE_paint_toolslots_brush_get(Paint *paint, int slot_index)
173 {
174  if (slot_index < paint->tool_slots_len) {
175  PaintToolSlot *tslot = &paint->tool_slots[slot_index];
176  return tslot->brush;
177  }
178  return NULL;
179 }
#define BKE_brush_tool_get(brush, p)
Definition: BKE_brush.h:141
void id_us_min(struct ID *id)
Definition: lib_id.c:297
void id_us_plus(struct ID *id)
Definition: lib_id.c:288
void BKE_paint_runtime_init(const struct ToolSettings *ts, struct Paint *paint)
#define BLI_assert(a)
Definition: BLI_assert.h:58
unsigned int uint
Definition: BLI_sys_types.h:83
#define UNUSED_VARS_NDEBUG(...)
eObjectMode
Read Guarded memory(de)allocation.
#define MEM_recallocN(vmemh, len)
Scene scene
static void paint_toolslots_init_with_runtime(Main *bmain, ToolSettings *ts, Paint *paint)
void BKE_paint_toolslots_len_ensure(Paint *paint, int len)
void BKE_paint_toolslots_brush_update_ex(Paint *paint, Brush *brush)
void BKE_paint_toolslots_brush_validate(Main *bmain, Paint *paint)
Brush * BKE_paint_toolslots_brush_get(Paint *paint, int slot_index)
static void paint_toolslots_init(Main *bmain, Paint *paint)
void BKE_paint_toolslots_init_from_main(struct Main *bmain)
void BKE_paint_toolslots_brush_update(Paint *paint)
short ob_mode
void * next
Definition: DNA_ID.h:274
void * first
Definition: DNA_listBase.h:47
Definition: BKE_main.h:116
ListBase brushes
Definition: BKE_main.h:171
ListBase scenes
Definition: BKE_main.h:146
struct Brush * brush
unsigned short ob_mode
unsigned int tool_offset
struct Paint_Runtime runtime
int tool_slots_len
struct PaintToolSlot * tool_slots
struct Brush * brush
struct ToolSettings * toolsettings
Paint paint
GpWeightPaint * gp_weightpaint
struct ImagePaintSettings imapaint
GpPaint * gp_paint
GpSculptPaint * gp_sculptpaint
UvSculpt * uvsculpt
GpVertexPaint * gp_vertexpaint
uint len