Blender V5.3
rna_palette.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cstdlib>
10
11#include "RNA_define.hh"
12
13#include "rna_internal.hh"
14
15#include "WM_types.hh"
16
17#ifdef RNA_RUNTIME
18
19# include "DNA_brush_types.h"
20
21# include "BLI_listbase.hh"
22# include "BLI_math_color_c.hh"
23
24# include "BKE_library.hh"
25# include "BKE_paint.hh"
26# include "BKE_report.hh"
27
28namespace blender {
29
30static PaletteColor *rna_Palette_color_new(Palette *palette)
31{
32 if (!ID_IS_EDITABLE(palette) || ID_IS_OVERRIDE_LIBRARY(palette)) {
33 return nullptr;
34 }
35
37 return color;
38}
39
40static void rna_Palette_color_remove(Palette *palette, ReportList *reports, PointerRNA *color_ptr)
41{
42 if (!ID_IS_EDITABLE(palette) || ID_IS_OVERRIDE_LIBRARY(palette)) {
43 return;
44 }
45
46 PaletteColor *color = static_cast<PaletteColor *>(color_ptr->data);
47
48 if (BLI_findindex(&palette->colors, color) == -1) {
50 reports, RPT_ERROR, "Palette '%s' does not contain color given", palette->id.name + 2);
51 return;
52 }
53
55
56 color_ptr->invalidate();
57}
58
59static void rna_Palette_color_clear(Palette *palette)
60{
61 if (!ID_IS_EDITABLE(palette) || ID_IS_OVERRIDE_LIBRARY(palette)) {
62 return;
63 }
64
65 BKE_palette_clear(palette);
66}
67
68static void rna_PaletteColor_color_update(Main * /*bmain*/, Scene * /*scene*/, PointerRNA *ptr)
69{
70 /* For forward compatibility. */
71 PaletteColor *color = static_cast<PaletteColor *>(ptr->data);
73}
74
75static PointerRNA rna_Palette_active_color_get(PointerRNA *ptr)
76{
77 Palette *palette = static_cast<Palette *>(ptr->data);
79
80 color = static_cast<PaletteColor *>(BLI_findlink(&palette->colors, palette->active_color));
81
82 if (color) {
83 return RNA_pointer_create_with_parent(*ptr, RNA_PaletteColor, color);
84 }
85
86 return {};
87}
88
89static void rna_Palette_active_color_set(PointerRNA *ptr,
90 PointerRNA value,
91 ReportList * /*reports*/)
92{
93 Palette *palette = static_cast<Palette *>(ptr->data);
94 const PaletteColor *color = static_cast<const PaletteColor *>(value.data);
95
96 /* -1 is ok for an unset index */
97 if (color == nullptr) {
98 palette->active_color = -1;
99 }
100 else {
101 palette->active_color = BLI_findindex(&palette->colors, color);
102 }
103}
104
105} // namespace blender
106
107#else
108
109namespace blender {
110
111/* palette.colors */
113{
114 StructRNA *srna;
115 PropertyRNA *prop;
116
117 FunctionRNA *func;
118 PropertyRNA *parm;
119
120 RNA_def_property_srna(cprop, "PaletteColors");
121 srna = RNA_def_struct(brna, "PaletteColors", nullptr);
122 RNA_def_struct_sdna(srna, "Palette");
123 RNA_def_struct_ui_text(srna, "Palette Splines", "Collection of palette colors");
124
125 func = RNA_def_function(srna, "new", "rna_Palette_color_new");
126 RNA_def_function_ui_description(func, "Add a new color to the palette");
127 parm = RNA_def_pointer(func, "color", "PaletteColor", "", "The newly created color");
128 RNA_def_function_return(func, parm);
129
130 func = RNA_def_function(srna, "remove", "rna_Palette_color_remove");
131 RNA_def_function_ui_description(func, "Remove a color from the palette");
133 parm = RNA_def_pointer(func, "color", "PaletteColor", "", "The color to remove");
136
137 func = RNA_def_function(srna, "clear", "rna_Palette_color_clear");
138 RNA_def_function_ui_description(func, "Remove all colors from the palette");
139
140 prop = RNA_def_property(srna, "active", PROP_POINTER, PROP_NONE);
141 RNA_def_property_struct_type(prop, "PaletteColor");
143 prop, "rna_Palette_active_color_get", "rna_Palette_active_color_set", nullptr, nullptr);
145 RNA_def_property_ui_text(prop, "Active Palette Color", "");
146}
147
149{
150 StructRNA *srna;
151 PropertyRNA *prop;
152
153 srna = RNA_def_struct(brna, "PaletteColor", nullptr);
154 RNA_def_struct_ui_text(srna, "Palette Color", "");
155
156 prop = RNA_def_property(srna, "color", PROP_FLOAT, PROP_COLOR);
157 RNA_def_property_range(prop, 0.0, 1.0);
158 RNA_def_property_float_sdna(prop, nullptr, "color");
160 RNA_def_property_array(prop, 3);
161 RNA_def_property_ui_text(prop, "Color", "");
162 RNA_def_property_update(prop, NC_SCENE | ND_TOOLSETTINGS, "rna_PaletteColor_color_update");
163
164 prop = RNA_def_property(srna, "strength", PROP_FLOAT, PROP_NONE);
165 RNA_def_property_range(prop, 0.0, 1.0);
166 RNA_def_property_float_sdna(prop, nullptr, "value");
167 RNA_def_property_ui_text(prop, "Value", "");
169
170 prop = RNA_def_property(srna, "weight", PROP_FLOAT, PROP_NONE);
171 RNA_def_property_range(prop, 0.0, 1.0);
172 RNA_def_property_float_sdna(prop, nullptr, "value");
173 RNA_def_property_ui_text(prop, "Weight", "");
175}
176
177static void rna_def_palette(BlenderRNA *brna)
178{
179 StructRNA *srna;
180 PropertyRNA *prop;
181
182 srna = RNA_def_struct(brna, "Palette", "ID");
183 RNA_def_struct_ui_text(srna, "Palette", "");
184 RNA_def_struct_ui_icon(srna, ICON_COLOR);
185
186 prop = RNA_def_property(srna, "colors", PROP_COLLECTION, PROP_NONE);
187 RNA_def_property_struct_type(prop, "PaletteColor");
188 rna_def_palettecolors(brna, prop);
189}
190
192{
193 /* *** Non-Animated *** */
196 rna_def_palette(brna);
198}
199
200} // namespace blender
201
202#endif
#define ID_IS_EDITABLE(_id)
Definition DNA_ID.h:708
#define ID_IS_OVERRIDE_LIBRARY(_id)
Definition DNA_ID.h:736
#define NC_SCENE
Definition WM_types.hh:391
#define ND_TOOLSETTINGS
Definition WM_types.hh:468
BMesh Iterators.
void RNA_def_property_srna(PropertyRNA *prop, const char *type)
void RNA_def_struct_ui_text(StructRNA *srna, const char *name, const char *description)
void RNA_def_property_ui_text(PropertyRNA *prop, const char *name, const char *description)
void RNA_def_struct_ui_icon(StructRNA *srna, int icon)
void RNA_def_property_pointer_funcs(PropertyRNA *prop, const char *get, const char *set, const char *type_fn, const char *poll)
void RNA_def_palette(BlenderRNA *brna)
void RNA_def_function_ui_description(FunctionRNA *func, const char *description)
@ FUNC_USE_REPORTS
PropertyRNA * RNA_def_pointer(StructOrFunctionRNA *cont_, const char *identifier, const char *type, const char *ui_name, const char *ui_description)
void RNA_def_property_struct_type(PropertyRNA *prop, const char *type)
void RNA_define_animate_sdna(bool animate)
void RNA_def_property_float_sdna(PropertyRNA *prop, const char *structname, const char *propname)
PropertyRNA * RNA_def_property(StructOrFunctionRNA *cont_, const char *identifier_c_str, int type, int subtype)
static void rna_def_palette(BlenderRNA *brna)
void * BLI_findlink(const ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:541
int BLI_findindex(const ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:593
void RNA_def_struct_sdna(StructRNA *srna, const char *structname)
void BKE_palette_color_sync_legacy(PaletteColor *color)
void RNA_def_parameter_clear_flags(PropertyRNA *prop, PropertyFlag flag_property, ParameterFlag flag_parameter)
@ PROP_POINTER
Definition RNA_types.hh:215
@ PROP_COLLECTION
Definition RNA_types.hh:216
void RNA_def_parameter_flags(PropertyRNA *prop, PropertyFlag flag_property, ParameterFlag flag_parameter)
void RNA_def_property_range(PropertyRNA *prop, double min, double max)
void RNA_def_function_return(FunctionRNA *func, PropertyRNA *ret)
FunctionRNA * RNA_def_function(StructRNA *srna, const char *identifier_c_str, const char *call)
void BKE_palette_color_remove(Palette *palette, PaletteColor *color)
PaletteColor * BKE_palette_color_add(Palette *palette)
void RNA_def_function_flag(FunctionRNA *func, FunctionFlag flag)
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
static void rna_def_palettecolor(BlenderRNA *brna)
StructRNA * RNA_def_struct(BlenderRNA *brna, const char *identifier, const char *from)
@ PROP_EDITABLE
Definition RNA_types.hh:376
@ PROP_THICK_WRAP
Definition RNA_types.hh:493
@ PROP_NEVER_NULL
Definition RNA_types.hh:447
@ PROP_LIB_EXCEPTION
Definition RNA_types.hh:382
void RNA_def_property_update(PropertyRNA *prop, int noteflag, const char *func)
PointerRNA RNA_pointer_create_with_parent(const PointerRNA &parent, StructRNA *type, void *data)
void BKE_palette_clear(Palette *palette)
static void rna_def_palettecolors(BlenderRNA *brna, PropertyRNA *cprop)
@ PARM_REQUIRED
Definition RNA_types.hh:631
@ PARM_RNAPTR
Definition RNA_types.hh:633
void RNA_def_property_array(PropertyRNA *prop, int length)