2 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18 * The Original Code is Copyright (C) 2013 Blender Foundation
19 * All rights reserved.
21 * The Original Code is: all of this file.
23 * Contributor(s): none yet.
25 * ***** END GPL LICENSE BLOCK *****
28 /** \file blender/blenkernel/intern/freestyle.c
32 #include "MEM_guardedalloc.h"
34 #include "DNA_freestyle_types.h"
35 #include "DNA_group_types.h"
37 #include "BKE_freestyle.h"
38 #include "BKE_linestyle.h"
40 #include "BLI_blenlib.h"
43 // function declarations
44 static FreestyleLineSet *alloc_lineset(void);
45 static void copy_lineset(FreestyleLineSet *new_lineset, FreestyleLineSet *lineset);
46 static FreestyleModuleConfig *alloc_module(void);
47 static void copy_module(FreestyleModuleConfig *new_module, FreestyleModuleConfig *module);
49 void BKE_freestyle_config_init(FreestyleConfig *config)
51 config->mode = FREESTYLE_CONTROL_EDITOR_MODE;
53 BLI_listbase_clear(&config->modules);
55 config->sphere_radius = 0.1f;
56 config->dkr_epsilon = 0.0f;
57 config->crease_angle = DEG2RADF(134.43f);
59 BLI_listbase_clear(&config->linesets);
62 void BKE_freestyle_config_free(FreestyleConfig *config)
64 FreestyleLineSet *lineset;
66 for (lineset = (FreestyleLineSet *)config->linesets.first; lineset; lineset = lineset->next) {
68 lineset->group->id.us--;
69 lineset->group = NULL;
71 if (lineset->linestyle) {
72 lineset->linestyle->id.us--;
73 lineset->linestyle = NULL;
76 BLI_freelistN(&config->linesets);
77 BLI_freelistN(&config->modules);
80 void BKE_freestyle_config_copy(FreestyleConfig *new_config, FreestyleConfig *config)
82 FreestyleLineSet *lineset, *new_lineset;
83 FreestyleModuleConfig *module, *new_module;
85 new_config->mode = config->mode;
86 new_config->flags = config->flags;
87 new_config->sphere_radius = config->sphere_radius;
88 new_config->dkr_epsilon = config->dkr_epsilon;
89 new_config->crease_angle = config->crease_angle;
91 BLI_listbase_clear(&new_config->linesets);
92 for (lineset = (FreestyleLineSet *)config->linesets.first; lineset; lineset = lineset->next) {
93 new_lineset = alloc_lineset();
94 copy_lineset(new_lineset, lineset);
95 BLI_addtail(&new_config->linesets, (void *)new_lineset);
98 BLI_listbase_clear(&new_config->modules);
99 for (module = (FreestyleModuleConfig *)config->modules.first; module; module = module->next) {
100 new_module = alloc_module();
101 copy_module(new_module, module);
102 BLI_addtail(&new_config->modules, (void *)new_module);
106 static void copy_lineset(FreestyleLineSet *new_lineset, FreestyleLineSet *lineset)
108 new_lineset->linestyle = lineset->linestyle;
109 if (new_lineset->linestyle)
110 new_lineset->linestyle->id.us++;
111 new_lineset->flags = lineset->flags;
112 new_lineset->selection = lineset->selection;
113 new_lineset->qi = lineset->qi;
114 new_lineset->qi_start = lineset->qi_start;
115 new_lineset->qi_end = lineset->qi_end;
116 new_lineset->edge_types = lineset->edge_types;
117 new_lineset->exclude_edge_types = lineset->exclude_edge_types;
118 new_lineset->group = lineset->group;
119 if (new_lineset->group) {
120 new_lineset->group->id.us++;
122 strcpy(new_lineset->name, lineset->name);
125 static FreestyleModuleConfig *alloc_module(void)
127 return (FreestyleModuleConfig *)MEM_callocN(sizeof(FreestyleModuleConfig), "style module configuration");
130 FreestyleModuleConfig *BKE_freestyle_module_add(FreestyleConfig *config)
132 FreestyleModuleConfig *module_conf = alloc_module();
133 BLI_addtail(&config->modules, (void *)module_conf);
134 module_conf->script = NULL;
135 module_conf->is_displayed = 1;
139 static void copy_module(FreestyleModuleConfig *new_module, FreestyleModuleConfig *module)
141 new_module->script = module->script;
142 new_module->is_displayed = module->is_displayed;
145 bool BKE_freestyle_module_delete(FreestyleConfig *config, FreestyleModuleConfig *module_conf)
147 if (BLI_findindex(&config->modules, module_conf) == -1)
149 BLI_freelinkN(&config->modules, module_conf);
153 bool BKE_freestyle_module_move_up(FreestyleConfig *config, FreestyleModuleConfig *module_conf)
155 if (BLI_findindex(&config->modules, module_conf) == -1)
157 BLI_remlink(&config->modules, module_conf);
158 BLI_insertlinkbefore(&config->modules, module_conf->prev, module_conf);
162 bool BKE_freestyle_module_move_down(FreestyleConfig *config, FreestyleModuleConfig *module_conf)
164 if (BLI_findindex(&config->modules, module_conf) == -1)
166 BLI_remlink(&config->modules, module_conf);
167 BLI_insertlinkafter(&config->modules, module_conf->next, module_conf);
171 void BKE_freestyle_lineset_unique_name(FreestyleConfig *config, FreestyleLineSet *lineset)
173 BLI_uniquename(&config->linesets, lineset, "FreestyleLineSet", '.', offsetof(FreestyleLineSet, name),
174 sizeof(lineset->name));
177 static FreestyleLineSet *alloc_lineset(void)
179 return (FreestyleLineSet *)MEM_callocN(sizeof(FreestyleLineSet), "Freestyle line set");
182 FreestyleLineSet *BKE_freestyle_lineset_add(FreestyleConfig *config, const char *name)
184 int lineset_index = BLI_countlist(&config->linesets);
186 FreestyleLineSet *lineset = alloc_lineset();
187 BLI_addtail(&config->linesets, (void *)lineset);
188 BKE_freestyle_lineset_set_active_index(config, lineset_index);
190 lineset->linestyle = BKE_new_linestyle("LineStyle", NULL);
191 lineset->flags |= FREESTYLE_LINESET_ENABLED;
192 lineset->selection = FREESTYLE_SEL_VISIBILITY | FREESTYLE_SEL_EDGE_TYPES | FREESTYLE_SEL_IMAGE_BORDER;
193 lineset->qi = FREESTYLE_QI_VISIBLE;
194 lineset->qi_start = 0;
195 lineset->qi_end = 100;
196 lineset->edge_types = FREESTYLE_FE_SILHOUETTE | FREESTYLE_FE_BORDER | FREESTYLE_FE_CREASE;
197 lineset->exclude_edge_types = 0;
198 lineset->group = NULL;
200 BLI_strncpy(lineset->name, name, sizeof(lineset->name));
202 else if (lineset_index > 0) {
203 sprintf(lineset->name, "LineSet %i", lineset_index + 1);
206 strcpy(lineset->name, "LineSet");
208 BKE_freestyle_lineset_unique_name(config, lineset);
213 bool BKE_freestyle_lineset_delete(FreestyleConfig *config, FreestyleLineSet *lineset)
215 if (BLI_findindex(&config->linesets, lineset) == -1)
217 if (lineset->group) {
218 lineset->group->id.us--;
220 if (lineset->linestyle) {
221 lineset->linestyle->id.us--;
223 BLI_remlink(&config->linesets, lineset);
225 BKE_freestyle_lineset_set_active_index(config, 0);
229 FreestyleLineSet *BKE_freestyle_lineset_get_active(FreestyleConfig *config)
231 FreestyleLineSet *lineset;
233 for (lineset = (FreestyleLineSet *)config->linesets.first; lineset; lineset = lineset->next) {
234 if (lineset->flags & FREESTYLE_LINESET_CURRENT)
240 short BKE_freestyle_lineset_get_active_index(FreestyleConfig *config)
242 FreestyleLineSet *lineset;
245 for (lineset = (FreestyleLineSet *)config->linesets.first, i = 0; lineset; lineset = lineset->next, i++) {
246 if (lineset->flags & FREESTYLE_LINESET_CURRENT)
252 void BKE_freestyle_lineset_set_active_index(FreestyleConfig *config, short index)
254 FreestyleLineSet *lineset;
257 for (lineset = (FreestyleLineSet *)config->linesets.first, i = 0; lineset; lineset = lineset->next, i++) {
259 lineset->flags |= FREESTYLE_LINESET_CURRENT;
261 lineset->flags &= ~FREESTYLE_LINESET_CURRENT;