2.5 Paint:
[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_paint.h"
37
38 #include <stdlib.h>
39 #include <string.h>
40
41 Paint *paint_get_active(Scene *sce)
42 {
43         if(sce && sce->basact && sce->basact->object) {
44                 ToolSettings *ts = sce->toolsettings;
45
46                 switch(sce->basact->object->mode) {
47                 case OB_MODE_SCULPT:
48                         return &ts->sculpt->paint;
49                 case OB_MODE_VERTEX_PAINT:
50                         return &ts->vpaint->paint;
51                 case OB_MODE_WEIGHT_PAINT:
52                         return &ts->wpaint->paint;
53                 case OB_MODE_TEXTURE_PAINT:
54                         break;
55                         //return &ts->imapaint->paint;
56                 }
57         }
58
59         return NULL;
60 }
61
62 Brush *paint_brush(Paint *p)
63 {
64         return p && p->brushes ? p->brushes[p->active_brush_index] : NULL;
65 }
66
67 void paint_brush_set(Paint *p, Brush *br)
68 {
69         if(p && p->brushes) {
70                 int i;
71
72                 /* See if there's already a slot with the brush */
73                 for(i = 0; i < p->brush_count; ++i) {
74                         if(p->brushes[i] == br) {
75                                 p->active_brush_index = i;
76                                 break;
77                         }
78                 }
79                 
80         }
81         else
82                 paint_brush_slot_add(p);
83
84         /* Make sure the current slot is the new brush */
85         p->brushes[p->active_brush_index] = br;
86 }
87
88 static void paint_brush_slots_alloc(Paint *p, const int count)
89 {
90         p->brush_count = count;
91         if(count == 0)
92                 p->brushes = NULL;
93         else
94                 p->brushes = MEM_callocN(sizeof(Brush*) * count, "Brush slots");
95 }
96
97 void paint_brush_slot_add(Paint *p)
98 {
99         Brush **orig = p->brushes;
100         int orig_count = p->brushes ? p->brush_count : 0;
101
102         /* Increase size of brush slot array */
103         paint_brush_slots_alloc(p, orig_count + 1);
104         if(orig) {
105                 memcpy(p->brushes, orig, sizeof(Brush*) * orig_count);
106                 MEM_freeN(orig);
107         }
108
109         p->active_brush_index = orig_count;
110 }
111
112 void paint_brush_slot_remove(Paint *p)
113 {
114         if(p->brushes) {
115                 Brush **orig = p->brushes;
116                 int src, dst;
117                 
118                 /* Decrease size of brush slot array */
119                 paint_brush_slots_alloc(p, p->brush_count - 1);
120                 if(p->brushes) {
121                         for(src = 0, dst = 0; dst < p->brush_count; ++src) {
122                                 if(src != p->active_brush_index) {
123                                         p->brushes[dst] = orig[src];
124                                         ++dst;
125                                 }
126                         }
127                 }
128                 MEM_freeN(orig);
129
130                 if(p->active_brush_index >= p->brush_count)
131                         p->active_brush_index = p->brush_count - 1;
132                 if(p->active_brush_index < 0)
133                         p->active_brush_index = 0;
134         }
135 }
136
137 int paint_facesel_test(Object *ob)
138 {
139         return (G.f&G_FACESELECT) && (ob && (ob->mode & (OB_MODE_VERTEX_PAINT|OB_MODE_WEIGHT_PAINT|OB_MODE_TEXTURE_PAINT)));
140
141 }
142
143 void paint_init(Paint *p, const char *name)
144 {
145         Brush *brush;
146
147         /* If there's no brush, create one */
148         brush = paint_brush(p);
149         brush_check_exists(&brush, name);
150         paint_brush_set(p, brush);
151 }
152
153 void free_paint(Paint *paint)
154 {
155         if(paint->brushes)
156                 MEM_freeN(paint->brushes);
157 }
158
159 void copy_paint(Paint *orig, Paint *new)
160 {
161         if(orig->brushes)
162                 new->brushes = MEM_dupallocN(orig->brushes);
163 }