This repository has been archived on 2023-10-09. You can view files and clone it, but cannot push or open issues or pull requests.
Files
blender-archive/source/blender/blenkernel/intern/paint_toolslots.c

128 lines
3.9 KiB
C
Raw Normal View History

/*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* ***** END GPL LICENSE BLOCK *****
*/
/** \file blender/blenkernel/intern/paint_toolslots.c
* \ingroup bke
*/
#include "MEM_guardedalloc.h"
#include "DNA_modifier_types.h"
#include "DNA_scene_types.h"
#include "DNA_brush_types.h"
#include "BLI_utildefines.h"
#include "BKE_main.h"
#include "BKE_library.h"
#include "BKE_paint.h"
void BKE_paint_toolslots_len_ensure(Paint *paint, int len)
{
if (paint->tool_slots_len < len) {
paint->tool_slots = MEM_recallocN(paint->tool_slots, sizeof(*paint->tool_slots) * len);
paint->tool_slots_len = len;
}
}
static void paint_toolslots_init(Main *bmain, Scene *scene, Paint *paint)
{
if (paint == NULL) {
return;
}
uint tool_offset = 0;
eObjectMode ob_mode = 0;
bool ok = BKE_paint_brush_tool_info(scene, paint, &tool_offset, &ob_mode);
BLI_assert(ok);
for (Brush *brush = bmain->brush.first; brush; brush = brush->id.next) {
if (brush->ob_mode & ob_mode) {
const int slot_index = *(char *)POINTER_OFFSET(brush, tool_offset);
BKE_paint_toolslots_len_ensure(paint, slot_index + 1);
if (paint->tool_slots[slot_index].brush == NULL) {
paint->tool_slots[slot_index].brush = brush;
id_us_plus(&brush->id);
}
}
}
}
void BKE_paint_toolslots_init_from_main(struct Main *bmain)
{
for (Scene *scene = bmain->scene.first; scene; scene = scene->id.next) {
ToolSettings *ts = scene->toolsettings;
paint_toolslots_init(bmain, scene, &ts->imapaint.paint);
paint_toolslots_init(bmain, scene, &ts->sculpt->paint);
paint_toolslots_init(bmain, scene, &ts->vpaint->paint);
paint_toolslots_init(bmain, scene, &ts->wpaint->paint);
paint_toolslots_init(bmain, scene, &ts->gp_paint->paint);
}
}
void BKE_paint_toolslots_brush_update_ex(Scene *scene, Paint *paint, Brush *brush)
{
uint tool_offset = 0;
bool ok = BKE_paint_brush_tool_info(scene, paint, &tool_offset, NULL);
BLI_assert(ok);
int slot_index = *(char *)POINTER_OFFSET(brush, tool_offset);
BKE_paint_toolslots_len_ensure(paint, slot_index + 1);
PaintToolSlot *tslot = &paint->tool_slots[slot_index];
id_us_plus(&brush->id);
id_us_min(&tslot->brush->id);
tslot->brush = brush;
}
void BKE_paint_toolslots_brush_update(Scene *scene, Paint *paint)
{
if (paint->brush == NULL) {
return;
}
BKE_paint_toolslots_brush_update_ex(scene, paint, paint->brush);
}
/**
* Run this to ensure brush types are set for each slot on entering modes
* (for new scenes for example).
*/
void BKE_paint_toolslots_brush_validate(Main *bmain, Scene *scene, Paint *paint)
{
/* Clear slots with invalid slots or mode (unlikely but possible). */
uint tool_offset = 0;
eObjectMode ob_mode = 0;
bool ok = BKE_paint_brush_tool_info(scene, paint, &tool_offset, &ob_mode);
BLI_assert(ok);
for (int i = 0; i < paint->tool_slots_len; i++) {
PaintToolSlot *tslot = &paint->tool_slots[i];
if (tslot->brush) {
int slot_index = *(char *)POINTER_OFFSET(tslot->brush, tool_offset);
if ((slot_index != i) || (tslot->brush->ob_mode & ob_mode) == 0) {
id_us_min(&tslot->brush->id);
tslot->brush = NULL;
}
}
}
/* Unlikely but possible the active brush is not currently using a slot. */
BKE_paint_toolslots_brush_update(scene, paint);
/* Fill slots from brushes. */
paint_toolslots_init(bmain, scene, paint);
}