f17d2fbdcac408e8fe964f1b9f60549f55c94e24
[blender-staging.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) {
50                 ToolSettings *ts = sce->toolsettings;
51                 
52                 if(sce->basact && sce->basact->object) {
53                         switch(sce->basact->object->mode) {
54                         case OB_MODE_SCULPT:
55                                 return &ts->sculpt->paint;
56                         case OB_MODE_VERTEX_PAINT:
57                                 return &ts->vpaint->paint;
58                         case OB_MODE_WEIGHT_PAINT:
59                                 return &ts->wpaint->paint;
60                         case OB_MODE_TEXTURE_PAINT:
61                                 return &ts->imapaint.paint;
62                         }
63                 }
64
65                 /* default to image paint */
66                 return &ts->imapaint.paint;
67         }
68
69         return NULL;
70 }
71
72 Brush *paint_brush(Paint *p)
73 {
74         return p && p->brushes ? p->brushes[p->active_brush_index] : NULL;
75 }
76
77 void paint_brush_set(Paint *p, Brush *br)
78 {
79         if(p && !br) {
80                 /* Setting to NULL removes the current slot */
81                 paint_brush_slot_remove(p);
82         }
83         else if(p) {
84                 int found = 0;
85         
86                 if(p->brushes) {
87                         int i;
88                         
89                         /* See if there's already a slot with the brush */
90                         for(i = 0; i < p->brush_count; ++i) {
91                                 if(p->brushes[i] == br) {
92                                         p->active_brush_index = i;
93                                         found = 1;
94                                         break;
95                                 }
96                         }
97                         
98                 }
99                 
100                 if(!found) {
101                         paint_brush_slot_add(p);
102                         id_us_plus(&br->id);
103                 }
104                 
105                 /* Make sure the current slot is the new brush */
106                 p->brushes[p->active_brush_index] = br;
107         }
108 }
109
110 static void paint_brush_slots_alloc(Paint *p, const int count)
111 {
112         p->brush_count = count;
113         if(count == 0)
114                 p->brushes = NULL;
115         else
116                 p->brushes = MEM_callocN(sizeof(Brush*) * count, "Brush slots");
117 }
118
119 void paint_brush_slot_add(Paint *p)
120 {
121         if(p) {
122                 Brush **orig = p->brushes;
123                 int orig_count = p->brushes ? p->brush_count : 0;
124
125                 /* Increase size of brush slot array */
126                 paint_brush_slots_alloc(p, orig_count + 1);
127                 if(orig) {
128                         memcpy(p->brushes, orig, sizeof(Brush*) * orig_count);
129                         MEM_freeN(orig);
130                 }
131
132                 p->active_brush_index = orig_count;
133         }
134 }
135
136 void paint_brush_slot_remove(Paint *p)
137 {
138         if(p && p->brushes) {
139                 Brush **orig = p->brushes;
140                 int src, dst;
141                 
142                 /* Decrease size of brush slot array */
143                 paint_brush_slots_alloc(p, p->brush_count - 1);
144                 if(p->brushes) {
145                         for(src = 0, dst = 0; dst < p->brush_count; ++src) {
146                                 if(src != p->active_brush_index) {
147                                         p->brushes[dst] = orig[src];
148                                         ++dst;
149                                 }
150                         }
151                 }
152                 MEM_freeN(orig);
153
154                 if(p->active_brush_index >= p->brush_count)
155                         p->active_brush_index = p->brush_count - 1;
156                 if(p->active_brush_index < 0)
157                         p->active_brush_index = 0;
158         }
159 }
160
161 int paint_facesel_test(Object *ob)
162 {
163         return (G.f&G_FACESELECT) && (ob && (ob->mode & (OB_MODE_VERTEX_PAINT|OB_MODE_WEIGHT_PAINT|OB_MODE_TEXTURE_PAINT)));
164
165 }
166
167 void paint_init(Paint *p, const char col[3])
168 {
169         Brush *brush;
170
171         /* If there's no brush, create one */
172         brush = paint_brush(p);
173         brush_check_exists(&brush, "Brush");
174         paint_brush_set(p, brush);
175
176         memcpy(p->paint_cursor_col, col, 3);
177         p->paint_cursor_col[3] = 128;
178 }
179
180 void free_paint(Paint *paint)
181 {
182         if(paint->brushes)
183                 MEM_freeN(paint->brushes);
184 }
185
186 void copy_paint(Paint *orig, Paint *new)
187 {
188         if(orig->brushes) {
189                 int i;
190                 new->brushes = MEM_dupallocN(orig->brushes);
191                 for(i = 0; i < orig->brush_count; ++i)
192                         id_us_plus((ID *)new->brushes[i]);
193         }
194 }