Merge branch 'blender2.7'
[blender.git] / source / blender / blenkernel / intern / freestyle.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2013 Blender Foundation
17  * All rights reserved.
18  */
19
20 /** \file \ingroup bke
21  */
22
23 #include "MEM_guardedalloc.h"
24
25 #include "DNA_collection_types.h"
26 #include "DNA_freestyle_types.h"
27
28 #include "BLI_blenlib.h"
29 #include "BLI_math.h"
30 #include "BLI_string_utils.h"
31
32 #include "BKE_freestyle.h"
33 #include "BKE_library.h"
34 #include "BKE_linestyle.h"
35
36 // function declarations
37 static FreestyleLineSet *alloc_lineset(void);
38 static void copy_lineset(FreestyleLineSet *new_lineset, FreestyleLineSet *lineset, const int flag);
39 static FreestyleModuleConfig *alloc_module(void);
40 static void copy_module(FreestyleModuleConfig *new_module, FreestyleModuleConfig *module);
41
42 void BKE_freestyle_config_init(FreestyleConfig *config)
43 {
44         config->mode = FREESTYLE_CONTROL_EDITOR_MODE;
45
46         BLI_listbase_clear(&config->modules);
47         config->flags = 0;
48         config->sphere_radius = 0.1f;
49         config->dkr_epsilon = 0.0f;
50         config->crease_angle = DEG2RADF(134.43f);
51
52         BLI_listbase_clear(&config->linesets);
53 }
54
55 void BKE_freestyle_config_free(FreestyleConfig *config, const bool do_id_user)
56 {
57         FreestyleLineSet *lineset;
58
59         for (lineset = (FreestyleLineSet *)config->linesets.first; lineset; lineset = lineset->next) {
60                 if (lineset->group) {
61                         if (do_id_user) {
62                                 id_us_min(&lineset->group->id);
63                         }
64                         lineset->group = NULL;
65                 }
66                 if (lineset->linestyle) {
67                         if (do_id_user) {
68                                 id_us_min(&lineset->linestyle->id);
69                         }
70                         lineset->linestyle = NULL;
71                 }
72         }
73         BLI_freelistN(&config->linesets);
74         BLI_freelistN(&config->modules);
75 }
76
77 void BKE_freestyle_config_copy(FreestyleConfig *new_config, const FreestyleConfig *config, const int flag)
78 {
79         FreestyleLineSet *lineset, *new_lineset;
80         FreestyleModuleConfig *module, *new_module;
81
82         new_config->mode = config->mode;
83         new_config->flags = config->flags;
84         new_config->sphere_radius = config->sphere_radius;
85         new_config->dkr_epsilon = config->dkr_epsilon;
86         new_config->crease_angle = config->crease_angle;
87
88         BLI_listbase_clear(&new_config->linesets);
89         for (lineset = (FreestyleLineSet *)config->linesets.first; lineset; lineset = lineset->next) {
90                 new_lineset = alloc_lineset();
91                 copy_lineset(new_lineset, lineset, flag);
92                 BLI_addtail(&new_config->linesets, (void *)new_lineset);
93         }
94
95         BLI_listbase_clear(&new_config->modules);
96         for (module = (FreestyleModuleConfig *)config->modules.first; module; module = module->next) {
97                 new_module = alloc_module();
98                 copy_module(new_module, module);
99                 BLI_addtail(&new_config->modules, (void *)new_module);
100         }
101 }
102
103 static void copy_lineset(FreestyleLineSet *new_lineset, FreestyleLineSet *lineset, const int flag)
104 {
105         new_lineset->linestyle = lineset->linestyle;
106         new_lineset->flags = lineset->flags;
107         new_lineset->selection = lineset->selection;
108         new_lineset->qi = lineset->qi;
109         new_lineset->qi_start = lineset->qi_start;
110         new_lineset->qi_end = lineset->qi_end;
111         new_lineset->edge_types = lineset->edge_types;
112         new_lineset->exclude_edge_types = lineset->exclude_edge_types;
113         new_lineset->group = lineset->group;
114         strcpy(new_lineset->name, lineset->name);
115
116         if ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) {
117                 id_us_plus((ID *)new_lineset->linestyle);
118                 id_us_plus((ID *)new_lineset->group);
119         }
120 }
121
122 static FreestyleModuleConfig *alloc_module(void)
123 {
124         return (FreestyleModuleConfig *)MEM_callocN(sizeof(FreestyleModuleConfig), "style module configuration");
125 }
126
127 FreestyleModuleConfig *BKE_freestyle_module_add(FreestyleConfig *config)
128 {
129         FreestyleModuleConfig *module_conf = alloc_module();
130         BLI_addtail(&config->modules, (void *)module_conf);
131         module_conf->script = NULL;
132         module_conf->is_displayed = 1;
133         return module_conf;
134 }
135
136 static void copy_module(FreestyleModuleConfig *new_module, FreestyleModuleConfig *module)
137 {
138         new_module->script = module->script;
139         new_module->is_displayed = module->is_displayed;
140 }
141
142 bool BKE_freestyle_module_delete(FreestyleConfig *config, FreestyleModuleConfig *module_conf)
143 {
144         if (BLI_findindex(&config->modules, module_conf) == -1)
145                 return false;
146         BLI_freelinkN(&config->modules, module_conf);
147         return true;
148 }
149
150 /**
151  * Reinsert \a module_conf offset by \a direction from current position.
152  * \return if position of \a module_conf changed.
153  */
154 bool BKE_freestyle_module_move(FreestyleConfig *config, FreestyleModuleConfig *module_conf, int direction)
155 {
156         return ((BLI_findindex(&config->modules, module_conf) > -1) &&
157                 (BLI_listbase_link_move(&config->modules, module_conf, direction) == true));
158 }
159
160 void BKE_freestyle_lineset_unique_name(FreestyleConfig *config, FreestyleLineSet *lineset)
161 {
162         BLI_uniquename(&config->linesets, lineset, "FreestyleLineSet", '.', offsetof(FreestyleLineSet, name),
163                        sizeof(lineset->name));
164 }
165
166 static FreestyleLineSet *alloc_lineset(void)
167 {
168         return (FreestyleLineSet *)MEM_callocN(sizeof(FreestyleLineSet), "Freestyle line set");
169 }
170
171 FreestyleLineSet *BKE_freestyle_lineset_add(struct Main *bmain, FreestyleConfig *config, const char *name)
172 {
173         int lineset_index = BLI_listbase_count(&config->linesets);
174
175         FreestyleLineSet *lineset = alloc_lineset();
176         BLI_addtail(&config->linesets, (void *)lineset);
177         BKE_freestyle_lineset_set_active_index(config, lineset_index);
178
179         lineset->linestyle = BKE_linestyle_new(bmain, "LineStyle");
180         lineset->flags |= FREESTYLE_LINESET_ENABLED;
181         lineset->selection = FREESTYLE_SEL_VISIBILITY | FREESTYLE_SEL_EDGE_TYPES | FREESTYLE_SEL_IMAGE_BORDER;
182         lineset->qi = FREESTYLE_QI_VISIBLE;
183         lineset->qi_start = 0;
184         lineset->qi_end = 100;
185         lineset->edge_types = FREESTYLE_FE_SILHOUETTE | FREESTYLE_FE_BORDER | FREESTYLE_FE_CREASE;
186         lineset->exclude_edge_types = 0;
187         lineset->group = NULL;
188         if (name) {
189                 BLI_strncpy(lineset->name, name, sizeof(lineset->name));
190         }
191         else if (lineset_index > 0) {
192                 sprintf(lineset->name, "LineSet %i", lineset_index + 1);
193         }
194         else {
195                 strcpy(lineset->name, "LineSet");
196         }
197         BKE_freestyle_lineset_unique_name(config, lineset);
198
199         return lineset;
200 }
201
202 bool BKE_freestyle_lineset_delete(FreestyleConfig *config, FreestyleLineSet *lineset)
203 {
204         if (BLI_findindex(&config->linesets, lineset) == -1)
205                 return false;
206         if (lineset->group) {
207                 id_us_min(&lineset->group->id);
208         }
209         if (lineset->linestyle) {
210                 id_us_min(&lineset->linestyle->id);
211         }
212         BLI_remlink(&config->linesets, lineset);
213         MEM_freeN(lineset);
214         BKE_freestyle_lineset_set_active_index(config, 0);
215         return true;
216 }
217
218 FreestyleLineSet *BKE_freestyle_lineset_get_active(FreestyleConfig *config)
219 {
220         FreestyleLineSet *lineset;
221
222         for (lineset = (FreestyleLineSet *)config->linesets.first; lineset; lineset = lineset->next) {
223                 if (lineset->flags & FREESTYLE_LINESET_CURRENT)
224                         return lineset;
225         }
226         return NULL;
227 }
228
229 short BKE_freestyle_lineset_get_active_index(FreestyleConfig *config)
230 {
231         FreestyleLineSet *lineset;
232         short i;
233
234         for (lineset = (FreestyleLineSet *)config->linesets.first, i = 0; lineset; lineset = lineset->next, i++) {
235                 if (lineset->flags & FREESTYLE_LINESET_CURRENT)
236                         return i;
237         }
238         return 0;
239 }
240
241 void BKE_freestyle_lineset_set_active_index(FreestyleConfig *config, short index)
242 {
243         FreestyleLineSet *lineset;
244         short i;
245
246         for (lineset = (FreestyleLineSet *)config->linesets.first, i = 0; lineset; lineset = lineset->next, i++) {
247                 if (i == index)
248                         lineset->flags |= FREESTYLE_LINESET_CURRENT;
249                 else
250                         lineset->flags &= ~FREESTYLE_LINESET_CURRENT;
251         }
252 }