part 1 of cleaning up my little array macro library to be a formal API. also removed...
[blender.git] / source / blender / blenkernel / intern / paint.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
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.
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, write to the Free Software  Foundation,
16  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  *
18  * The Original Code is Copyright (C) 2009 by Nicholas Bishop
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */ 
27
28 #include "MEM_guardedalloc.h"
29
30 #include "DNA_brush_types.h"
31 #include "DNA_object_types.h"
32 #include "DNA_scene_types.h"
33
34 #include "BKE_brush.h"
35 #include "BKE_global.h"
36 #include "BKE_library.h"
37 #include "BKE_paint.h"
38
39 #include <stdlib.h>
40 #include <string.h>
41
42 const char PAINT_CURSOR_SCULPT[3] = {255, 100, 100};
43 const char PAINT_CURSOR_VERTEX_PAINT[3] = {255, 255, 255};
44 const char PAINT_CURSOR_WEIGHT_PAINT[3] = {200, 200, 255};
45 const char PAINT_CURSOR_TEXTURE_PAINT[3] = {255, 255, 255};
46
47 Paint *paint_get_active(Scene *sce)
48 {
49         if(sce && sce->basact && sce->basact->object) {
50                 ToolSettings *ts = sce->toolsettings;
51
52                 switch(sce->basact->object->mode) {
53                 case OB_MODE_SCULPT:
54                         return &ts->sculpt->paint;
55                 case OB_MODE_VERTEX_PAINT:
56                         return &ts->vpaint->paint;
57                 case OB_MODE_WEIGHT_PAINT:
58                         return &ts->wpaint->paint;
59                 case OB_MODE_TEXTURE_PAINT:
60                         return &ts->imapaint.paint;
61                 }
62         }
63
64         return NULL;
65 }
66
67 Brush *paint_brush(Paint *p)
68 {
69         return p && p->brushes ? p->brushes[p->active_brush_index] : NULL;
70 }
71
72 void paint_brush_set(Paint *p, Brush *br)
73 {
74         if(p && !br) {
75                 /* Setting to NULL removes the current slot */
76                 paint_brush_slot_remove(p);
77         }
78         else if(p) {
79                 int found = 0;
80         
81                 if(p->brushes) {
82                         int i;
83                         
84                         /* See if there's already a slot with the brush */
85                         for(i = 0; i < p->brush_count; ++i) {
86                                 if(p->brushes[i] == br) {
87                                         p->active_brush_index = i;
88                                         found = 1;
89                                         break;
90                                 }
91                         }
92                         
93                 }
94                 
95                 if(!found) {
96                         paint_brush_slot_add(p);
97                         id_us_plus(&br->id);
98                 }
99                 
100                 /* Make sure the current slot is the new brush */
101                 p->brushes[p->active_brush_index] = br;
102         }
103 }
104
105 static void paint_brush_slots_alloc(Paint *p, const int count)
106 {
107         p->brush_count = count;
108         if(count == 0)
109                 p->brushes = NULL;
110         else
111                 p->brushes = MEM_callocN(sizeof(Brush*) * count, "Brush slots");
112 }
113
114 void paint_brush_slot_add(Paint *p)
115 {
116         if(p) {
117                 Brush **orig = p->brushes;
118                 int orig_count = p->brushes ? p->brush_count : 0;
119
120                 /* Increase size of brush slot array */
121                 paint_brush_slots_alloc(p, orig_count + 1);
122                 if(orig) {
123                         memcpy(p->brushes, orig, sizeof(Brush*) * orig_count);
124                         MEM_freeN(orig);
125                 }
126
127                 p->active_brush_index = orig_count;
128         }
129 }
130
131 void paint_brush_slot_remove(Paint *p)
132 {
133         if(p && p->brushes) {
134                 Brush **orig = p->brushes;
135                 int src, dst;
136                 
137                 /* Decrease size of brush slot array */
138                 paint_brush_slots_alloc(p, p->brush_count - 1);
139                 if(p->brushes) {
140                         for(src = 0, dst = 0; dst < p->brush_count; ++src) {
141                                 if(src != p->active_brush_index) {
142                                         p->brushes[dst] = orig[src];
143                                         ++dst;
144                                 }
145                         }
146                 }
147                 MEM_freeN(orig);
148
149                 if(p->active_brush_index >= p->brush_count)
150                         p->active_brush_index = p->brush_count - 1;
151                 if(p->active_brush_index < 0)
152                         p->active_brush_index = 0;
153         }
154 }
155
156 int paint_facesel_test(Object *ob)
157 {
158         return (G.f&G_FACESELECT) && (ob && (ob->mode & (OB_MODE_VERTEX_PAINT|OB_MODE_WEIGHT_PAINT|OB_MODE_TEXTURE_PAINT)));
159
160 }
161
162 void paint_init(Paint *p, const char col[3])
163 {
164         Brush *brush;
165
166         /* If there's no brush, create one */
167         brush = paint_brush(p);
168         brush_check_exists(&brush, "Brush");
169         paint_brush_set(p, brush);
170
171         memcpy(p->paint_cursor_col, col, 3);
172         p->paint_cursor_col[3] = 128;
173 }
174
175 void free_paint(Paint *paint)
176 {
177         if(paint->brushes)
178                 MEM_freeN(paint->brushes);
179 }
180
181 void copy_paint(Paint *orig, Paint *new)
182 {
183         if(orig->brushes) {
184                 int i;
185                 new->brushes = MEM_dupallocN(orig->brushes);
186                 for(i = 0; i < orig->brush_count; ++i)
187                         id_us_plus((ID *)new->brushes[i]);
188         }
189 }