Blender V4.5
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 "BKE_library.hh"
22# include "BKE_paint.hh"
23# include "BKE_report.hh"
24
25static PaletteColor *rna_Palette_color_new(Palette *palette)
26{
27 if (!ID_IS_EDITABLE(palette) || ID_IS_OVERRIDE_LIBRARY(palette)) {
28 return nullptr;
29 }
30
31 PaletteColor *color = BKE_palette_color_add(palette);
32 return color;
33}
34
35static void rna_Palette_color_remove(Palette *palette, ReportList *reports, PointerRNA *color_ptr)
36{
37 if (!ID_IS_EDITABLE(palette) || ID_IS_OVERRIDE_LIBRARY(palette)) {
38 return;
39 }
40
41 PaletteColor *color = static_cast<PaletteColor *>(color_ptr->data);
42
43 if (BLI_findindex(&palette->colors, color) == -1) {
45 reports, RPT_ERROR, "Palette '%s' does not contain color given", palette->id.name + 2);
46 return;
47 }
48
49 BKE_palette_color_remove(palette, color);
50
51 color_ptr->invalidate();
52}
53
54static void rna_Palette_color_clear(Palette *palette)
55{
56 if (!ID_IS_EDITABLE(palette) || ID_IS_OVERRIDE_LIBRARY(palette)) {
57 return;
58 }
59
60 BKE_palette_clear(palette);
61}
62
63static PointerRNA rna_Palette_active_color_get(PointerRNA *ptr)
64{
65 Palette *palette = static_cast<Palette *>(ptr->data);
66 PaletteColor *color;
67
68 color = static_cast<PaletteColor *>(BLI_findlink(&palette->colors, palette->active_color));
69
70 if (color) {
71 return RNA_pointer_create_with_parent(*ptr, &RNA_PaletteColor, color);
72 }
73
74 return PointerRNA_NULL;
75}
76
77static void rna_Palette_active_color_set(PointerRNA *ptr,
78 PointerRNA value,
79 ReportList * /*reports*/)
80{
81 Palette *palette = static_cast<Palette *>(ptr->data);
82 const PaletteColor *color = static_cast<const PaletteColor *>(value.data);
83
84 /* -1 is ok for an unset index */
85 if (color == nullptr) {
86 palette->active_color = -1;
87 }
88 else {
89 palette->active_color = BLI_findindex(&palette->colors, color);
90 }
91}
92
93#else
94
95/* palette.colors */
97{
98 StructRNA *srna;
99 PropertyRNA *prop;
100
101 FunctionRNA *func;
102 PropertyRNA *parm;
103
104 RNA_def_property_srna(cprop, "PaletteColors");
105 srna = RNA_def_struct(brna, "PaletteColors", nullptr);
106 RNA_def_struct_sdna(srna, "Palette");
107 RNA_def_struct_ui_text(srna, "Palette Splines", "Collection of palette colors");
108
109 func = RNA_def_function(srna, "new", "rna_Palette_color_new");
110 RNA_def_function_ui_description(func, "Add a new color to the palette");
111 parm = RNA_def_pointer(func, "color", "PaletteColor", "", "The newly created color");
112 RNA_def_function_return(func, parm);
113
114 func = RNA_def_function(srna, "remove", "rna_Palette_color_remove");
115 RNA_def_function_ui_description(func, "Remove a color from the palette");
117 parm = RNA_def_pointer(func, "color", "PaletteColor", "", "The color to remove");
120
121 func = RNA_def_function(srna, "clear", "rna_Palette_color_clear");
122 RNA_def_function_ui_description(func, "Remove all colors from the palette");
123
124 prop = RNA_def_property(srna, "active", PROP_POINTER, PROP_NONE);
125 RNA_def_property_struct_type(prop, "PaletteColor");
127 prop, "rna_Palette_active_color_get", "rna_Palette_active_color_set", nullptr, nullptr);
129 RNA_def_property_ui_text(prop, "Active Palette Color", "");
130}
131
133{
134 StructRNA *srna;
135 PropertyRNA *prop;
136
137 srna = RNA_def_struct(brna, "PaletteColor", nullptr);
138 RNA_def_struct_ui_text(srna, "Palette Color", "");
139
140 prop = RNA_def_property(srna, "color", PROP_FLOAT, PROP_COLOR_GAMMA);
141 RNA_def_property_range(prop, 0.0, 1.0);
142 RNA_def_property_float_sdna(prop, nullptr, "rgb");
144 RNA_def_property_array(prop, 3);
145 RNA_def_property_ui_text(prop, "Color", "");
147
148 prop = RNA_def_property(srna, "strength", PROP_FLOAT, PROP_NONE);
149 RNA_def_property_range(prop, 0.0, 1.0);
150 RNA_def_property_float_sdna(prop, nullptr, "value");
151 RNA_def_property_ui_text(prop, "Value", "");
153
154 prop = RNA_def_property(srna, "weight", PROP_FLOAT, PROP_NONE);
155 RNA_def_property_range(prop, 0.0, 1.0);
156 RNA_def_property_float_sdna(prop, nullptr, "value");
157 RNA_def_property_ui_text(prop, "Weight", "");
159}
160
161static void rna_def_palette(BlenderRNA *brna)
162{
163 StructRNA *srna;
164 PropertyRNA *prop;
165
166 srna = RNA_def_struct(brna, "Palette", "ID");
167 RNA_def_struct_ui_text(srna, "Palette", "");
168 RNA_def_struct_ui_icon(srna, ICON_COLOR);
169
170 prop = RNA_def_property(srna, "colors", PROP_COLLECTION, PROP_NONE);
171 RNA_def_property_struct_type(prop, "PaletteColor");
172 rna_def_palettecolors(brna, prop);
173}
174
176{
177 /* *** Non-Animated *** */
180 rna_def_palette(brna);
182}
183
184#endif
PaletteColor * BKE_palette_color_add(Palette *palette)
Definition paint.cc:1390
void BKE_palette_color_remove(Palette *palette, PaletteColor *color)
Definition paint.cc:1362
void BKE_palette_clear(Palette *palette)
Definition paint.cc:1378
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
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
ParameterFlag
Definition RNA_types.hh:510
@ PARM_RNAPTR
Definition RNA_types.hh:513
@ PARM_REQUIRED
Definition RNA_types.hh:511
@ FUNC_USE_REPORTS
Definition RNA_types.hh:805
@ PROP_FLOAT
Definition RNA_types.hh:152
@ PROP_POINTER
Definition RNA_types.hh:155
@ PROP_COLLECTION
Definition RNA_types.hh:156
@ PROP_THICK_WRAP
Definition RNA_types.hh:397
@ PROP_EDITABLE
Definition RNA_types.hh:292
@ PROP_LIB_EXCEPTION
Definition RNA_types.hh:298
@ PROP_NEVER_NULL
Definition RNA_types.hh:351
@ PROP_NONE
Definition RNA_types.hh:221
@ PROP_COLOR_GAMMA
Definition RNA_types.hh:260
#define NC_SCENE
Definition WM_types.hh:375
#define ND_TOOLSETTINGS
Definition WM_types.hh:446
ReportList * reports
Definition WM_types.hh:1025
#define ID_IS_EDITABLE(_id)
#define ID_IS_OVERRIDE_LIBRARY(_id)
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[66]
Definition DNA_ID.h:415
ListBase colors
void invalidate()
Definition RNA_types.hh:110
void * data
Definition RNA_types.hh:53
PointerRNA * ptr
Definition wm_files.cc:4227