More F-Modifier Tweaks:
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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_mesh_types.h"
33 #include "DNA_scene_types.h"
34
35 #include "BKE_brush.h"
36 #include "BKE_global.h"
37 #include "BKE_library.h"
38 #include "BKE_paint.h"
39
40 #include <stdlib.h>
41 #include <string.h>
42
43 const char PAINT_CURSOR_SCULPT[3] = {255, 100, 100};
44 const char PAINT_CURSOR_VERTEX_PAINT[3] = {255, 255, 255};
45 const char PAINT_CURSOR_WEIGHT_PAINT[3] = {200, 200, 255};
46 const char PAINT_CURSOR_TEXTURE_PAINT[3] = {255, 255, 255};
47
48 Paint *paint_get_active(Scene *sce)
49 {
50         if(sce) {
51                 ToolSettings *ts = sce->toolsettings;
52                 
53                 if(sce->basact && sce->basact->object) {
54                         switch(sce->basact->object->mode) {
55                         case OB_MODE_SCULPT:
56                                 return &ts->sculpt->paint;
57                         case OB_MODE_VERTEX_PAINT:
58                                 return &ts->vpaint->paint;
59                         case OB_MODE_WEIGHT_PAINT:
60                                 return &ts->wpaint->paint;
61                         case OB_MODE_TEXTURE_PAINT:
62                                 return &ts->imapaint.paint;
63                         }
64                 }
65
66                 /* default to image paint */
67                 return &ts->imapaint.paint;
68         }
69
70         return NULL;
71 }
72
73 Brush *paint_brush(Paint *p)
74 {
75         return p && p->brushes ? p->brushes[p->active_brush_index] : NULL;
76 }
77
78 void paint_brush_set(Paint *p, Brush *br)
79 {
80         if(p && !br) {
81                 /* Setting to NULL removes the current slot */
82                 paint_brush_slot_remove(p);
83         }
84         else if(p) {
85                 int found = 0;
86         
87                 if(p->brushes) {
88                         int i;
89                         
90                         /* See if there's already a slot with the brush */
91                         for(i = 0; i < p->brush_count; ++i) {
92                                 if(p->brushes[i] == br) {
93                                         p->active_brush_index = i;
94                                         found = 1;
95                                         break;
96                                 }
97                         }
98                         
99                 }
100                 
101                 if(!found) {
102                         paint_brush_slot_add(p);
103                         id_us_plus(&br->id);
104                 }
105                 
106                 /* Make sure the current slot is the new brush */
107                 p->brushes[p->active_brush_index] = br;
108         }
109 }
110
111 static void paint_brush_slots_alloc(Paint *p, const int count)
112 {
113         p->brush_count = count;
114         if(count == 0)
115                 p->brushes = NULL;
116         else
117                 p->brushes = MEM_callocN(sizeof(Brush*) * count, "Brush slots");
118 }
119
120 void paint_brush_slot_add(Paint *p)
121 {
122         if(p) {
123                 Brush **orig = p->brushes;
124                 int orig_count = p->brushes ? p->brush_count : 0;
125
126                 /* Increase size of brush slot array */
127                 paint_brush_slots_alloc(p, orig_count + 1);
128                 if(orig) {
129                         memcpy(p->brushes, orig, sizeof(Brush*) * orig_count);
130                         MEM_freeN(orig);
131                 }
132
133                 p->active_brush_index = orig_count;
134         }
135 }
136
137 void paint_brush_slot_remove(Paint *p)
138 {
139         if(p && p->brushes) {
140                 Brush **orig = p->brushes;
141                 int src, dst;
142                 
143                 /* Decrease size of brush slot array */
144                 paint_brush_slots_alloc(p, p->brush_count - 1);
145                 if(p->brushes) {
146                         for(src = 0, dst = 0; dst < p->brush_count; ++src) {
147                                 if(src != p->active_brush_index) {
148                                         p->brushes[dst] = orig[src];
149                                         ++dst;
150                                 }
151                         }
152                 }
153                 MEM_freeN(orig);
154
155                 if(p->active_brush_index >= p->brush_count)
156                         p->active_brush_index = p->brush_count - 1;
157                 if(p->active_brush_index < 0)
158                         p->active_brush_index = 0;
159         }
160 }
161
162 int paint_facesel_test(Object *ob)
163 {
164         return (ob && ob->type==OB_MESH && ob->data && (((Mesh *)ob->data)->editflag & ME_EDIT_PAINT_MASK) && (ob->mode & (OB_MODE_VERTEX_PAINT|OB_MODE_WEIGHT_PAINT|OB_MODE_TEXTURE_PAINT)));
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         p->flags |= PAINT_SHOW_BRUSH;
180 }
181
182 void free_paint(Paint *paint)
183 {
184         if(paint->brushes)
185                 MEM_freeN(paint->brushes);
186 }
187
188 void copy_paint(Paint *orig, Paint *new)
189 {
190         if(orig->brushes) {
191                 int i;
192                 new->brushes = MEM_dupallocN(orig->brushes);
193                 for(i = 0; i < orig->brush_count; ++i)
194                         id_us_plus((ID *)new->brushes[i]);
195         }
196 }