Blender V5.0
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_math_color.h"
22
23# include "BKE_library.hh"
24# include "BKE_paint.hh"
25# include "BKE_report.hh"
26
27static PaletteColor *rna_Palette_color_new(Palette *palette)
28{
29 if (!ID_IS_EDITABLE(palette) || ID_IS_OVERRIDE_LIBRARY(palette)) {
30 return nullptr;
31 }
32
33 PaletteColor *color = BKE_palette_color_add(palette);
34 return color;
35}
36
37static void rna_Palette_color_remove(Palette *palette, ReportList *reports, PointerRNA *color_ptr)
38{
39 if (!ID_IS_EDITABLE(palette) || ID_IS_OVERRIDE_LIBRARY(palette)) {
40 return;
41 }
42
43 PaletteColor *color = static_cast<PaletteColor *>(color_ptr->data);
44
45 if (BLI_findindex(&palette->colors, color) == -1) {
47 reports, RPT_ERROR, "Palette '%s' does not contain color given", palette->id.name + 2);
48 return;
49 }
50
51 BKE_palette_color_remove(palette, color);
52
53 color_ptr->invalidate();
54}
55
56static void rna_Palette_color_clear(Palette *palette)
57{
58 if (!ID_IS_EDITABLE(palette) || ID_IS_OVERRIDE_LIBRARY(palette)) {
59 return;
60 }
61
62 BKE_palette_clear(palette);
63}
64
65static void rna_PaletteColor_color_update(Main * /*bmain*/, Scene * /*scene*/, PointerRNA *ptr)
66{
67 /* For forward compatibility. */
68 PaletteColor *color = static_cast<PaletteColor *>(ptr->data);
70}
71
72static PointerRNA rna_Palette_active_color_get(PointerRNA *ptr)
73{
74 Palette *palette = static_cast<Palette *>(ptr->data);
75 PaletteColor *color;
76
77 color = static_cast<PaletteColor *>(BLI_findlink(&palette->colors, palette->active_color));
78
79 if (color) {
80 return RNA_pointer_create_with_parent(*ptr, &RNA_PaletteColor, color);
81 }
82
83 return PointerRNA_NULL;
84}
85
86static void rna_Palette_active_color_set(PointerRNA *ptr,
87 PointerRNA value,
88 ReportList * /*reports*/)
89{
90 Palette *palette = static_cast<Palette *>(ptr->data);
91 const PaletteColor *color = static_cast<const PaletteColor *>(value.data);
92
93 /* -1 is ok for an unset index */
94 if (color == nullptr) {
95 palette->active_color = -1;
96 }
97 else {
98 palette->active_color = BLI_findindex(&palette->colors, color);
99 }
100}
101
102#else
103
104/* palette.colors */
106{
107 StructRNA *srna;
108 PropertyRNA *prop;
109
110 FunctionRNA *func;
111 PropertyRNA *parm;
112
113 RNA_def_property_srna(cprop, "PaletteColors");
114 srna = RNA_def_struct(brna, "PaletteColors", nullptr);
115 RNA_def_struct_sdna(srna, "Palette");
116 RNA_def_struct_ui_text(srna, "Palette Splines", "Collection of palette colors");
117
118 func = RNA_def_function(srna, "new", "rna_Palette_color_new");
119 RNA_def_function_ui_description(func, "Add a new color to the palette");
120 parm = RNA_def_pointer(func, "color", "PaletteColor", "", "The newly created color");
121 RNA_def_function_return(func, parm);
122
123 func = RNA_def_function(srna, "remove", "rna_Palette_color_remove");
124 RNA_def_function_ui_description(func, "Remove a color from the palette");
126 parm = RNA_def_pointer(func, "color", "PaletteColor", "", "The color to remove");
129
130 func = RNA_def_function(srna, "clear", "rna_Palette_color_clear");
131 RNA_def_function_ui_description(func, "Remove all colors from the palette");
132
133 prop = RNA_def_property(srna, "active", PROP_POINTER, PROP_NONE);
134 RNA_def_property_struct_type(prop, "PaletteColor");
136 prop, "rna_Palette_active_color_get", "rna_Palette_active_color_set", nullptr, nullptr);
138 RNA_def_property_ui_text(prop, "Active Palette Color", "");
139}
140
142{
143 StructRNA *srna;
144 PropertyRNA *prop;
145
146 srna = RNA_def_struct(brna, "PaletteColor", nullptr);
147 RNA_def_struct_ui_text(srna, "Palette Color", "");
148
149 prop = RNA_def_property(srna, "color", PROP_FLOAT, PROP_COLOR);
150 RNA_def_property_range(prop, 0.0, 1.0);
151 RNA_def_property_float_sdna(prop, nullptr, "color");
153 RNA_def_property_array(prop, 3);
154 RNA_def_property_ui_text(prop, "Color", "");
155 RNA_def_property_update(prop, NC_SCENE | ND_TOOLSETTINGS, "rna_PaletteColor_color_update");
156
157 prop = RNA_def_property(srna, "strength", PROP_FLOAT, PROP_NONE);
158 RNA_def_property_range(prop, 0.0, 1.0);
159 RNA_def_property_float_sdna(prop, nullptr, "value");
160 RNA_def_property_ui_text(prop, "Value", "");
162
163 prop = RNA_def_property(srna, "weight", PROP_FLOAT, PROP_NONE);
164 RNA_def_property_range(prop, 0.0, 1.0);
165 RNA_def_property_float_sdna(prop, nullptr, "value");
166 RNA_def_property_ui_text(prop, "Weight", "");
168}
169
170static void rna_def_palette(BlenderRNA *brna)
171{
172 StructRNA *srna;
173 PropertyRNA *prop;
174
175 srna = RNA_def_struct(brna, "Palette", "ID");
176 RNA_def_struct_ui_text(srna, "Palette", "");
177 RNA_def_struct_ui_icon(srna, ICON_COLOR);
178
179 prop = RNA_def_property(srna, "colors", PROP_COLLECTION, PROP_NONE);
180 RNA_def_property_struct_type(prop, "PaletteColor");
181 rna_def_palettecolors(brna, prop);
182}
183
185{
186 /* *** Non-Animated *** */
189 rna_def_palette(brna);
191}
192
193#endif
PaletteColor * BKE_palette_color_add(Palette *palette)
Definition paint.cc:1433
void BKE_palette_color_remove(Palette *palette, PaletteColor *color)
Definition paint.cc:1394
void BKE_palette_clear(Palette *palette)
Definition paint.cc:1410
void BKE_palette_color_sync_legacy(PaletteColor *color)
Definition paint.cc:1422
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
@ RPT_ERROR
Definition BKE_report.hh:39
int BLI_findindex(const ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:586
void * BLI_findlink(const ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:534
#define ID_IS_EDITABLE(_id)
Definition DNA_ID.h:705
#define ID_IS_OVERRIDE_LIBRARY(_id)
Definition DNA_ID.h:730
ParameterFlag
Definition RNA_types.hh:544
@ PARM_RNAPTR
Definition RNA_types.hh:547
@ PARM_REQUIRED
Definition RNA_types.hh:545
@ FUNC_USE_REPORTS
Definition RNA_types.hh:914
@ PROP_FLOAT
Definition RNA_types.hh:164
@ PROP_POINTER
Definition RNA_types.hh:167
@ PROP_COLLECTION
Definition RNA_types.hh:168
@ PROP_THICK_WRAP
Definition RNA_types.hh:423
@ PROP_EDITABLE
Definition RNA_types.hh:306
@ PROP_LIB_EXCEPTION
Definition RNA_types.hh:312
@ PROP_NEVER_NULL
Definition RNA_types.hh:377
@ PROP_COLOR
Definition RNA_types.hh:260
@ PROP_NONE
Definition RNA_types.hh:233
#define NC_SCENE
Definition WM_types.hh:378
#define ND_TOOLSETTINGS
Definition WM_types.hh:449
const PointerRNA PointerRNA_NULL
PointerRNA RNA_pointer_create_with_parent(const PointerRNA &parent, StructRNA *type, void *data)
void RNA_define_animate_sdna(bool animate)
void RNA_def_parameter_clear_flags(PropertyRNA *prop, PropertyFlag flag_property, ParameterFlag flag_parameter)
void RNA_def_function_return(FunctionRNA *func, PropertyRNA *ret)
void RNA_def_property_ui_text(PropertyRNA *prop, const char *name, const char *description)
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_struct_sdna(StructRNA *srna, const char *structname)
FunctionRNA * RNA_def_function(StructRNA *srna, const char *identifier, const char *call)
void RNA_def_property_array(PropertyRNA *prop, int length)
void RNA_def_property_range(PropertyRNA *prop, double min, double max)
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_def_function_ui_description(FunctionRNA *func, const char *description)
void RNA_def_property_update(PropertyRNA *prop, int noteflag, const char *func)
PropertyRNA * RNA_def_property(StructOrFunctionRNA *cont_, const char *identifier, int type, int subtype)
StructRNA * RNA_def_struct(BlenderRNA *brna, const char *identifier, const char *from)
void RNA_def_function_flag(FunctionRNA *func, int flag)
void RNA_def_property_pointer_funcs(PropertyRNA *prop, const char *get, const char *set, const char *type_fn, const char *poll)
void RNA_def_struct_ui_icon(StructRNA *srna, int icon)
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
void RNA_def_property_float_sdna(PropertyRNA *prop, const char *structname, const char *propname)
void RNA_def_parameter_flags(PropertyRNA *prop, PropertyFlag flag_property, ParameterFlag flag_parameter)
void RNA_def_palette(BlenderRNA *brna)
static void rna_def_palette(BlenderRNA *brna)
static void rna_def_palettecolor(BlenderRNA *brna)
static void rna_def_palettecolors(BlenderRNA *brna, PropertyRNA *cprop)
char name[258]
Definition DNA_ID.h:432
ListBase colors
void invalidate()
Definition RNA_types.hh:110
void * data
Definition RNA_types.hh:53
PointerRNA * ptr
Definition wm_files.cc:4238