Merge branch 'master' into blender2.8
[blender.git] / source / blender / blenkernel / intern / freestyle.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) 2013 Blender Foundation
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 /** \file blender/blenkernel/intern/freestyle.c
29  *  \ingroup bke
30  */
31
32 #include "MEM_guardedalloc.h"
33
34 #include "DNA_collection_types.h"
35 #include "DNA_freestyle_types.h"
36
37 #include "BLI_blenlib.h"
38 #include "BLI_math.h"
39 #include "BLI_string_utils.h"
40
41 #include "BKE_freestyle.h"
42 #include "BKE_library.h"
43 #include "BKE_linestyle.h"
44
45 // function declarations
46 static FreestyleLineSet *alloc_lineset(void);
47 static void copy_lineset(FreestyleLineSet *new_lineset, FreestyleLineSet *lineset, const int flag);
48 static FreestyleModuleConfig *alloc_module(void);
49 static void copy_module(FreestyleModuleConfig *new_module, FreestyleModuleConfig *module);
50
51 void BKE_freestyle_config_init(FreestyleConfig *config)
52 {
53         config->mode = FREESTYLE_CONTROL_EDITOR_MODE;
54
55         BLI_listbase_clear(&config->modules);
56         config->flags = 0;
57         config->sphere_radius = 0.1f;
58         config->dkr_epsilon = 0.0f;
59         config->crease_angle = DEG2RADF(134.43f);
60
61         BLI_listbase_clear(&config->linesets);
62 }
63
64 void BKE_freestyle_config_free(FreestyleConfig *config, const bool do_id_user)
65 {
66         FreestyleLineSet *lineset;
67
68         for (lineset = (FreestyleLineSet *)config->linesets.first; lineset; lineset = lineset->next) {
69                 if (lineset->group) {
70                         if (do_id_user) {
71                                 id_us_min(&lineset->group->id);
72                         }
73                         lineset->group = NULL;
74                 }
75                 if (lineset->linestyle) {
76                         if (do_id_user) {
77                                 id_us_min(&lineset->linestyle->id);
78                         }
79                         lineset->linestyle = NULL;
80                 }
81         }
82         BLI_freelistN(&config->linesets);
83         BLI_freelistN(&config->modules);
84 }
85
86 void BKE_freestyle_config_copy(FreestyleConfig *new_config, const FreestyleConfig *config, const int flag)
87 {
88         FreestyleLineSet *lineset, *new_lineset;
89         FreestyleModuleConfig *module, *new_module;
90
91         new_config->mode = config->mode;
92         new_config->flags = config->flags;
93         new_config->sphere_radius = config->sphere_radius;
94         new_config->dkr_epsilon = config->dkr_epsilon;
95         new_config->crease_angle = config->crease_angle;
96
97         BLI_listbase_clear(&new_config->linesets);
98         for (lineset = (FreestyleLineSet *)config->linesets.first; lineset; lineset = lineset->next) {
99                 new_lineset = alloc_lineset();
100                 copy_lineset(new_lineset, lineset, flag);
101                 BLI_addtail(&new_config->linesets, (void *)new_lineset);
102         }
103
104         BLI_listbase_clear(&new_config->modules);
105         for (module = (FreestyleModuleConfig *)config->modules.first; module; module = module->next) {
106                 new_module = alloc_module();
107                 copy_module(new_module, module);
108                 BLI_addtail(&new_config->modules, (void *)new_module);
109         }
110 }
111
112 static void copy_lineset(FreestyleLineSet *new_lineset, FreestyleLineSet *lineset, const int flag)
113 {
114         new_lineset->linestyle = lineset->linestyle;
115         new_lineset->flags = lineset->flags;
116         new_lineset->selection = lineset->selection;
117         new_lineset->qi = lineset->qi;
118         new_lineset->qi_start = lineset->qi_start;
119         new_lineset->qi_end = lineset->qi_end;
120         new_lineset->edge_types = lineset->edge_types;
121         new_lineset->exclude_edge_types = lineset->exclude_edge_types;
122         new_lineset->group = lineset->group;
123         strcpy(new_lineset->name, lineset->name);
124
125         if ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) {
126                 id_us_plus((ID *)new_lineset->linestyle);
127                 id_us_plus((ID *)new_lineset->group);
128         }
129 }
130
131 static FreestyleModuleConfig *alloc_module(void)
132 {
133         return (FreestyleModuleConfig *)MEM_callocN(sizeof(FreestyleModuleConfig), "style module configuration");
134 }
135
136 FreestyleModuleConfig *BKE_freestyle_module_add(FreestyleConfig *config)
137 {
138         FreestyleModuleConfig *module_conf = alloc_module();
139         BLI_addtail(&config->modules, (void *)module_conf);
140         module_conf->script = NULL;
141         module_conf->is_displayed = 1;
142         return module_conf;
143 }
144
145 static void copy_module(FreestyleModuleConfig *new_module, FreestyleModuleConfig *module)
146 {
147         new_module->script = module->script;
148         new_module->is_displayed = module->is_displayed;
149 }
150
151 bool BKE_freestyle_module_delete(FreestyleConfig *config, FreestyleModuleConfig *module_conf)
152 {
153         if (BLI_findindex(&config->modules, module_conf) == -1)
154                 return false;
155         BLI_freelinkN(&config->modules, module_conf);
156         return true;
157 }
158
159 /**
160  * Reinsert \a module_conf offset by \a direction from current position.
161  * \return if position of \a module_conf changed.
162  */
163 bool BKE_freestyle_module_move(FreestyleConfig *config, FreestyleModuleConfig *module_conf, int direction)
164 {
165         return ((BLI_findindex(&config->modules, module_conf) > -1) &&
166                 (BLI_listbase_link_move(&config->modules, module_conf, direction) == true));
167 }
168
169 void BKE_freestyle_lineset_unique_name(FreestyleConfig *config, FreestyleLineSet *lineset)
170 {
171         BLI_uniquename(&config->linesets, lineset, "FreestyleLineSet", '.', offsetof(FreestyleLineSet, name),
172                        sizeof(lineset->name));
173 }
174
175 static FreestyleLineSet *alloc_lineset(void)
176 {
177         return (FreestyleLineSet *)MEM_callocN(sizeof(FreestyleLineSet), "Freestyle line set");
178 }
179
180 FreestyleLineSet *BKE_freestyle_lineset_add(struct Main *bmain, FreestyleConfig *config, const char *name)
181 {
182         int lineset_index = BLI_listbase_count(&config->linesets);
183
184         FreestyleLineSet *lineset = alloc_lineset();
185         BLI_addtail(&config->linesets, (void *)lineset);
186         BKE_freestyle_lineset_set_active_index(config, lineset_index);
187
188         lineset->linestyle = BKE_linestyle_new(bmain, "LineStyle");
189         lineset->flags |= FREESTYLE_LINESET_ENABLED;
190         lineset->selection = FREESTYLE_SEL_VISIBILITY | FREESTYLE_SEL_EDGE_TYPES | FREESTYLE_SEL_IMAGE_BORDER;
191         lineset->qi = FREESTYLE_QI_VISIBLE;
192         lineset->qi_start = 0;
193         lineset->qi_end = 100;
194         lineset->edge_types = FREESTYLE_FE_SILHOUETTE | FREESTYLE_FE_BORDER | FREESTYLE_FE_CREASE;
195         lineset->exclude_edge_types = 0;
196         lineset->group = NULL;
197         if (name) {
198                 BLI_strncpy(lineset->name, name, sizeof(lineset->name));
199         }
200         else if (lineset_index > 0) {
201                 sprintf(lineset->name, "LineSet %i", lineset_index + 1);
202         }
203         else {
204                 strcpy(lineset->name, "LineSet");
205         }
206         BKE_freestyle_lineset_unique_name(config, lineset);
207
208         return lineset;
209 }
210
211 bool BKE_freestyle_lineset_delete(FreestyleConfig *config, FreestyleLineSet *lineset)
212 {
213         if (BLI_findindex(&config->linesets, lineset) == -1)
214                 return false;
215         if (lineset->group) {
216                 id_us_min(&lineset->group->id);
217         }
218         if (lineset->linestyle) {
219                 id_us_min(&lineset->linestyle->id);
220         }
221         BLI_remlink(&config->linesets, lineset);
222         MEM_freeN(lineset);
223         BKE_freestyle_lineset_set_active_index(config, 0);
224         return true;
225 }
226
227 FreestyleLineSet *BKE_freestyle_lineset_get_active(FreestyleConfig *config)
228 {
229         FreestyleLineSet *lineset;
230
231         for (lineset = (FreestyleLineSet *)config->linesets.first; lineset; lineset = lineset->next) {
232                 if (lineset->flags & FREESTYLE_LINESET_CURRENT)
233                         return lineset;
234         }
235         return NULL;
236 }
237
238 short BKE_freestyle_lineset_get_active_index(FreestyleConfig *config)
239 {
240         FreestyleLineSet *lineset;
241         short i;
242
243         for (lineset = (FreestyleLineSet *)config->linesets.first, i = 0; lineset; lineset = lineset->next, i++) {
244                 if (lineset->flags & FREESTYLE_LINESET_CURRENT)
245                         return i;
246         }
247         return 0;
248 }
249
250 void BKE_freestyle_lineset_set_active_index(FreestyleConfig *config, short index)
251 {
252         FreestyleLineSet *lineset;
253         short i;
254
255         for (lineset = (FreestyleLineSet *)config->linesets.first, i = 0; lineset; lineset = lineset->next, i++) {
256                 if (i == index)
257                         lineset->flags |= FREESTYLE_LINESET_CURRENT;
258                 else
259                         lineset->flags &= ~FREESTYLE_LINESET_CURRENT;
260         }
261 }