2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
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.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * The Original Code is Copyright (C) 2009 by Nicholas Bishop
19 * All rights reserved.
21 * The Original Code is: all of this file.
23 * Contributor(s): none yet.
25 * ***** END GPL LICENSE BLOCK *****
28 #include "MEM_guardedalloc.h"
30 #include "DNA_brush_types.h"
31 #include "DNA_object_types.h"
32 #include "DNA_scene_types.h"
34 #include "BKE_brush.h"
35 #include "BKE_global.h"
36 #include "BKE_library.h"
37 #include "BKE_paint.h"
42 Paint *paint_get_active(Scene *sce)
44 if(sce && sce->basact && sce->basact->object) {
45 ToolSettings *ts = sce->toolsettings;
47 switch(sce->basact->object->mode) {
49 return &ts->sculpt->paint;
50 case OB_MODE_VERTEX_PAINT:
51 return &ts->vpaint->paint;
52 case OB_MODE_WEIGHT_PAINT:
53 return &ts->wpaint->paint;
54 case OB_MODE_TEXTURE_PAINT:
55 return &ts->imapaint.paint;
62 Brush *paint_brush(Paint *p)
64 return p && p->brushes ? p->brushes[p->active_brush_index] : NULL;
67 void paint_brush_set(Paint *p, Brush *br)
70 /* Setting to NULL removes the current slot */
71 paint_brush_slot_remove(p);
79 /* See if there's already a slot with the brush */
80 for(i = 0; i < p->brush_count; ++i) {
81 if(p->brushes[i] == br) {
82 p->active_brush_index = i;
91 paint_brush_slot_add(p);
95 /* Make sure the current slot is the new brush */
96 p->brushes[p->active_brush_index] = br;
100 static void paint_brush_slots_alloc(Paint *p, const int count)
102 p->brush_count = count;
106 p->brushes = MEM_callocN(sizeof(Brush*) * count, "Brush slots");
109 void paint_brush_slot_add(Paint *p)
112 Brush **orig = p->brushes;
113 int orig_count = p->brushes ? p->brush_count : 0;
115 /* Increase size of brush slot array */
116 paint_brush_slots_alloc(p, orig_count + 1);
118 memcpy(p->brushes, orig, sizeof(Brush*) * orig_count);
122 p->active_brush_index = orig_count;
126 void paint_brush_slot_remove(Paint *p)
128 if(p && p->brushes) {
129 Brush **orig = p->brushes;
132 /* Decrease size of brush slot array */
133 paint_brush_slots_alloc(p, p->brush_count - 1);
135 for(src = 0, dst = 0; dst < p->brush_count; ++src) {
136 if(src != p->active_brush_index) {
137 p->brushes[dst] = orig[src];
144 if(p->active_brush_index >= p->brush_count)
145 p->active_brush_index = p->brush_count - 1;
146 if(p->active_brush_index < 0)
147 p->active_brush_index = 0;
151 int paint_facesel_test(Object *ob)
153 return (G.f&G_FACESELECT) && (ob && (ob->mode & (OB_MODE_VERTEX_PAINT|OB_MODE_WEIGHT_PAINT|OB_MODE_TEXTURE_PAINT)));
157 void paint_init(Paint *p, const char *col)
161 /* If there's no brush, create one */
162 brush = paint_brush(p);
163 brush_check_exists(&brush, "Brush");
164 paint_brush_set(p, brush);
167 memcpy(p->paint_cursor_col, col, 3);
169 p->paint_cursor_col[0] = 255;
170 p->paint_cursor_col[1] = 255;
171 p->paint_cursor_col[2] = 255;
173 p->paint_cursor_col[3] = 128;
176 void free_paint(Paint *paint)
179 MEM_freeN(paint->brushes);
182 void copy_paint(Paint *orig, Paint *new)
186 new->brushes = MEM_dupallocN(orig->brushes);
187 for(i = 0; i < orig->brush_count; ++i)
188 id_us_plus((ID *)new->brushes[i]);