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 
17 /** \file
18  * \ingroup RNA
19  */
20 
21 #include <stdio.h>
22 #include <stdlib.h>
23 
24 #include "DNA_curve_types.h"
25 #include "DNA_curveprofile_types.h"
26 #include "DNA_texture_types.h"
27 
28 #include "BLI_utildefines.h"
29 
30 #include "RNA_define.h"
31 #include "rna_internal.h"
32 
33 #include "WM_api.h"
34 #include "WM_types.h"
35 
36 #ifdef RNA_RUNTIME
37 
38 #  include "RNA_access.h"
39 
40 #  include "DNA_image_types.h"
41 #  include "DNA_material_types.h"
42 #  include "DNA_movieclip_types.h"
43 #  include "DNA_node_types.h"
44 #  include "DNA_object_types.h"
45 #  include "DNA_particle_types.h"
46 #  include "DNA_sequence_types.h"
47 
48 #  include "MEM_guardedalloc.h"
49 
50 #  include "BKE_colorband.h"
51 #  include "BKE_curveprofile.h"
52 #  include "BKE_image.h"
53 #  include "BKE_linestyle.h"
54 #  include "BKE_movieclip.h"
55 #  include "BKE_node.h"
56 #  include "BKE_sequencer.h"
57 
58 #  include "DEG_depsgraph.h"
59 
60 #  include "ED_node.h"
61 
62 #  include "IMB_colormanagement.h"
63 #  include "IMB_imbuf.h"
64 
65 /**
66  * Set both handle types for all selected points in the profile-- faster than changing types
67  * for many points individually. Also set both handles for the points.
68  */
rna_CurveProfilePoint_handle_type_set(PointerRNA * ptr,int value)69 static void rna_CurveProfilePoint_handle_type_set(PointerRNA *ptr, int value)
70 {
71   CurveProfilePoint *point = ptr->data;
72   CurveProfile *profile = point->profile;
73 
74   if (profile) {
75     BKE_curveprofile_selected_handle_set(profile, value, value);
76     BKE_curveprofile_update(profile, PROF_UPDATE_NONE);
77     WM_main_add_notifier(NC_GEOM | ND_DATA, NULL);
78   }
79 }
80 
rna_CurveProfile_clip_set(PointerRNA * ptr,bool value)81 static void rna_CurveProfile_clip_set(PointerRNA *ptr, bool value)
82 {
83   CurveProfile *profile = (CurveProfile *)ptr->data;
84 
85   if (value) {
86     profile->flag |= PROF_USE_CLIP;
87   }
88   else {
89     profile->flag &= ~PROF_USE_CLIP;
90   }
91 
92   BKE_curveprofile_update(profile, PROF_UPDATE_CLIP);
93 }
94 
rna_CurveProfile_sample_straight_set(PointerRNA * ptr,bool value)95 static void rna_CurveProfile_sample_straight_set(PointerRNA *ptr, bool value)
96 {
97   CurveProfile *profile = (CurveProfile *)ptr->data;
98 
99   if (value) {
100     profile->flag |= PROF_SAMPLE_STRAIGHT_EDGES;
101   }
102   else {
103     profile->flag &= ~PROF_SAMPLE_STRAIGHT_EDGES;
104   }
105 
106   BKE_curveprofile_update(profile, PROF_UPDATE_NONE);
107 }
108 
rna_CurveProfile_sample_even_set(PointerRNA * ptr,bool value)109 static void rna_CurveProfile_sample_even_set(PointerRNA *ptr, bool value)
110 {
111   CurveProfile *profile = (CurveProfile *)ptr->data;
112 
113   if (value) {
114     profile->flag |= PROF_SAMPLE_EVEN_LENGTHS;
115   }
116   else {
117     profile->flag &= ~PROF_SAMPLE_EVEN_LENGTHS;
118   }
119 
120   BKE_curveprofile_update(profile, PROF_UPDATE_NONE);
121 }
122 
rna_CurveProfile_remove_point(CurveProfile * profile,ReportList * reports,PointerRNA * point_ptr)123 static void rna_CurveProfile_remove_point(CurveProfile *profile,
124                                           ReportList *reports,
125                                           PointerRNA *point_ptr)
126 {
127   CurveProfilePoint *point = point_ptr->data;
128   if (BKE_curveprofile_remove_point(profile, point) == false) {
129     BKE_report(reports, RPT_ERROR, "Unable to remove path point");
130     return;
131   }
132 
133   RNA_POINTER_INVALIDATE(point_ptr);
134 }
135 
rna_CurveProfile_evaluate(struct CurveProfile * profile,ReportList * reports,float length_portion,float * location)136 static void rna_CurveProfile_evaluate(struct CurveProfile *profile,
137                                       ReportList *reports,
138                                       float length_portion,
139                                       float *location)
140 {
141   if (!profile->table) {
142     BKE_report(reports, RPT_ERROR, "CurveProfile table not initialized, call initialize()");
143   }
144   BKE_curveprofile_evaluate_length_portion(profile, length_portion, &location[0], &location[1]);
145 }
146 
rna_CurveProfile_initialize(struct CurveProfile * profile,int segments_len)147 static void rna_CurveProfile_initialize(struct CurveProfile *profile, int segments_len)
148 {
149   BKE_curveprofile_init(profile, (short)segments_len);
150 }
151 
rna_CurveProfile_update(struct CurveProfile * profile)152 static void rna_CurveProfile_update(struct CurveProfile *profile)
153 {
154   BKE_curveprofile_update(profile, PROF_UPDATE_REMOVE_DOUBLES | PROF_UPDATE_CLIP);
155 }
156 
157 #else
158 
159 static const EnumPropertyItem prop_handle_type_items[] = {
160     {HD_AUTO, "AUTO", ICON_HANDLE_AUTO, "Auto Handle", ""},
161     {HD_VECT, "VECTOR", ICON_HANDLE_VECTOR, "Vector Handle", ""},
162     {HD_FREE, "FREE", ICON_HANDLE_FREE, "Free Handle", ""},
163     {HD_ALIGN, "ALIGN", ICON_HANDLE_ALIGNED, "Aligned Free Handles", ""},
164     {0, NULL, 0, NULL, NULL},
165 };
166 
rna_def_curveprofilepoint(BlenderRNA * brna)167 static void rna_def_curveprofilepoint(BlenderRNA *brna)
168 {
169   StructRNA *srna;
170   PropertyRNA *prop;
171 
172   srna = RNA_def_struct(brna, "CurveProfilePoint", NULL);
173   RNA_def_struct_ui_text(srna, "CurveProfilePoint", "Point of a path used to define a profile");
174 
175   prop = RNA_def_property(srna, "location", PROP_FLOAT, PROP_XYZ);
176   RNA_def_property_float_sdna(prop, NULL, "x");
177   RNA_def_property_array(prop, 2);
178   RNA_def_property_ui_text(prop, "Location", "X/Y coordinates of the path point");
179 
180   prop = RNA_def_property(srna, "handle_type_1", PROP_ENUM, PROP_NONE);
181   RNA_def_property_enum_sdna(prop, NULL, "h1");
182   RNA_def_property_enum_items(prop, prop_handle_type_items);
183   RNA_def_property_enum_funcs(prop, NULL, "rna_CurveProfilePoint_handle_type_set", NULL);
184   RNA_def_property_ui_text(prop, "First Handle Type", "Path interpolation at this point");
185 
186   prop = RNA_def_property(srna, "handle_type_2", PROP_ENUM, PROP_NONE);
187   RNA_def_property_enum_sdna(prop, NULL, "h2");
188   RNA_def_property_enum_items(prop, prop_handle_type_items);
189   RNA_def_property_enum_funcs(prop, NULL, "rna_CurveProfilePoint_handle_type_set", NULL);
190   RNA_def_property_ui_text(prop, "Second Handle Type", "Path interpolation at this point");
191 
192   prop = RNA_def_property(srna, "select", PROP_BOOLEAN, PROP_NONE);
193   RNA_def_property_boolean_sdna(prop, NULL, "flag", PROF_SELECT);
194   RNA_def_property_ui_text(prop, "Select", "Selection state of the path point");
195 }
196 
rna_def_curveprofile_points_api(BlenderRNA * brna,PropertyRNA * cprop)197 static void rna_def_curveprofile_points_api(BlenderRNA *brna, PropertyRNA *cprop)
198 {
199   StructRNA *srna;
200   PropertyRNA *parm;
201   FunctionRNA *func;
202 
203   RNA_def_property_srna(cprop, "CurveProfilePoints");
204   srna = RNA_def_struct(brna, "CurveProfilePoints", NULL);
205   RNA_def_struct_sdna(srna, "CurveProfile");
206   RNA_def_struct_ui_text(srna, "Profile Point", "Collection of Profile Points");
207 
208   func = RNA_def_function(srna, "add", "BKE_curveprofile_insert");
209   RNA_def_function_ui_description(func, "Add point to the profile");
210   parm = RNA_def_float(func,
211                        "x",
212                        0.0f,
213                        -FLT_MAX,
214                        FLT_MAX,
215                        "X Position",
216                        "X Position for new point",
217                        -FLT_MAX,
218                        FLT_MAX);
219   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
220   parm = RNA_def_float(func,
221                        "y",
222                        0.0f,
223                        -FLT_MAX,
224                        FLT_MAX,
225                        "Y Position",
226                        "Y Position for new point",
227                        -FLT_MAX,
228                        FLT_MAX);
229   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
230   parm = RNA_def_pointer(func, "point", "CurveProfilePoint", "", "New point");
231   RNA_def_function_return(func, parm);
232 
233   func = RNA_def_function(srna, "remove", "rna_CurveProfile_remove_point");
234   RNA_def_function_flag(func, FUNC_USE_REPORTS);
235   RNA_def_function_ui_description(func, "Delete point from the profile");
236   parm = RNA_def_pointer(func, "point", "CurveProfilePoint", "", "Point to remove");
237   RNA_def_parameter_flags(parm, PROP_NEVER_NULL, PARM_REQUIRED | PARM_RNAPTR);
238   RNA_def_parameter_clear_flags(parm, PROP_THICK_WRAP, 0);
239 }
240 
rna_def_curveprofile(BlenderRNA * brna)241 static void rna_def_curveprofile(BlenderRNA *brna)
242 {
243   StructRNA *srna;
244   PropertyRNA *prop;
245   PropertyRNA *parm;
246   FunctionRNA *func;
247 
248   static const EnumPropertyItem rna_enum_curveprofile_preset_items[] = {
249       {PROF_PRESET_LINE, "LINE", 0, "Line", "Default"},
250       {PROF_PRESET_SUPPORTS, "SUPPORTS", 0, "Support Loops", "Loops on each side of the profile"},
251       {PROF_PRESET_CORNICE, "CORNICE", 0, "Cornice Molding", ""},
252       {PROF_PRESET_CROWN, "CROWN", 0, "Crown Molding", ""},
253       {PROF_PRESET_STEPS, "STEPS", 0, "Steps", "A number of steps defined by the segments"},
254       {0, NULL, 0, NULL, NULL},
255   };
256 
257   srna = RNA_def_struct(brna, "CurveProfile", NULL);
258   RNA_def_struct_ui_text(srna, "CurveProfile", "Profile Path editor used to build a profile path");
259 
260   prop = RNA_def_property(srna, "preset", PROP_ENUM, PROP_NONE);
261   RNA_def_property_enum_sdna(prop, NULL, "preset");
262   RNA_def_property_enum_items(prop, rna_enum_curveprofile_preset_items);
263   RNA_def_property_ui_text(prop, "Preset", "");
264 
265   prop = RNA_def_property(srna, "use_clip", PROP_BOOLEAN, PROP_NONE);
266   RNA_def_property_boolean_sdna(prop, NULL, "flag", PROF_USE_CLIP);
267   RNA_def_property_ui_text(prop, "Clip", "Force the path view to fit a defined boundary");
268   RNA_def_property_boolean_funcs(prop, NULL, "rna_CurveProfile_clip_set");
269 
270   prop = RNA_def_property(srna, "use_sample_straight_edges", PROP_BOOLEAN, PROP_NONE);
271   RNA_def_property_boolean_sdna(prop, NULL, "flag", PROF_SAMPLE_STRAIGHT_EDGES);
272   RNA_def_property_ui_text(prop, "Sample Straight Edges", "Sample edges with vector handles");
273   RNA_def_property_boolean_funcs(prop, NULL, "rna_CurveProfile_sample_straight_set");
274 
275   prop = RNA_def_property(srna, "use_sample_even_lengths", PROP_BOOLEAN, PROP_NONE);
276   RNA_def_property_boolean_sdna(prop, NULL, "flag", PROF_SAMPLE_EVEN_LENGTHS);
277   RNA_def_property_ui_text(prop, "Sample Even Lengths", "Sample edges with even lengths");
278   RNA_def_property_boolean_funcs(prop, NULL, "rna_CurveProfile_sample_even_set");
279 
280   func = RNA_def_function(srna, "update", "rna_CurveProfile_update");
281   RNA_def_function_ui_description(func, "Refresh internal data, remove doubles and clip points");
282 
283   func = RNA_def_function(srna, "initialize", "rna_CurveProfile_initialize");
284   parm = RNA_def_int(func,
285                      "totsegments",
286                      1,
287                      1,
288                      1000,
289                      "",
290                      "The number of segment values to"
291                      " initialize the segments table with",
292                      1,
293                      100);
294   RNA_def_parameter_flags(parm, PROP_NEVER_NULL, PARM_REQUIRED);
295   RNA_def_function_ui_description(func, "Set the number of display segments and fill tables");
296 
297   prop = RNA_def_property(srna, "points", PROP_COLLECTION, PROP_NONE);
298   RNA_def_property_collection_sdna(prop, NULL, "path", "path_len");
299   RNA_def_property_struct_type(prop, "CurveProfilePoint");
300   RNA_def_property_ui_text(prop, "Points", "Profile control points");
301   rna_def_curveprofile_points_api(brna, prop);
302 
303   prop = RNA_def_property(srna, "segments", PROP_COLLECTION, PROP_NONE);
304   RNA_def_property_collection_sdna(prop, NULL, "segments", "segments_len");
305   RNA_def_property_struct_type(prop, "CurveProfilePoint");
306   RNA_def_property_ui_text(prop, "Segments", "Segments sampled from control points");
307 
308   func = RNA_def_function(srna, "evaluate", "rna_CurveProfile_evaluate");
309   RNA_def_function_flag(func, FUNC_USE_REPORTS);
310   RNA_def_function_ui_description(func, "Evaluate the at the given portion of the path length");
311   parm = RNA_def_float(func,
312                        "length_portion",
313                        0.0f,
314                        0.0f,
315                        1.0f,
316                        "Length Portion",
317                        "Portion of the path length to travel before evaluation",
318                        0.0f,
319                        1.0f);
320   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
321   parm = RNA_def_float_vector(func,
322                               "location",
323                               2,
324                               NULL,
325                               -100.0f,
326                               100.0f,
327                               "Location",
328                               "The location at the given portion of the profile",
329                               -100.0f,
330                               100.0f);
331   RNA_def_function_output(func, parm);
332 }
333 
RNA_def_profile(BlenderRNA * brna)334 void RNA_def_profile(BlenderRNA *brna)
335 {
336   rna_def_curveprofilepoint(brna);
337   rna_def_curveprofile(brna);
338 }
339 
340 #endif
341