Blender V4.5
interface_template_color_picker.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_listbase.h"
10#include "BLI_math_vector.h"
11#include "BLI_string_ref.hh"
12
13#include "BLT_translation.hh"
14
15#include "DNA_brush_types.h"
16
17#include "RNA_access.hh"
18#include "RNA_prototypes.hh"
19
20#include "UI_interface.hh"
21#include "interface_intern.hh"
22
24
25#define WHEEL_SIZE (5 * U.widget_unit)
26
29 const StringRefNull propname,
30 bool value_slider,
31 bool lock,
32 bool lock_luminosity,
33 bool cubic)
34{
35 PropertyRNA *prop = RNA_struct_find_property(ptr, propname.c_str());
36 uiBlock *block = uiLayoutGetBlock(layout);
38
39 if (!prop) {
40 RNA_warning("property not found: %s.%s", RNA_struct_identifier(ptr->type), propname.c_str());
41 return;
42 }
43
44 float softmin, softmax, step, precision;
45 RNA_property_float_ui_range(ptr, prop, &softmin, &softmax, &step, &precision);
46
47 uiLayout *col = &layout->column(true);
48 uiLayout *row = &col->row(true);
49
50 uiBut *but = nullptr;
51 uiButHSVCube *hsv_but;
52 switch (U.color_picker_type) {
56 hsv_but = (uiButHSVCube *)uiDefButR_prop(block,
58 0,
59 "",
60 0,
61 0,
64 ptr,
65 prop,
66 -1,
67 0.0,
68 0.0,
69 "");
70 switch (U.color_picker_type) {
72 hsv_but->gradient_type = UI_GRAD_SV;
73 break;
75 hsv_but->gradient_type = UI_GRAD_HS;
76 break;
78 hsv_but->gradient_type = UI_GRAD_HV;
79 break;
80 }
81 but = hsv_but;
82 break;
83
84 /* user default */
87 default:
88 but = uiDefButR_prop(block,
90 0,
91 "",
92 0,
93 0,
96 ptr,
97 prop,
98 -1,
99 0.0,
100 0.0,
101 "");
102 break;
103 }
104
105 but->custom_data = cpicker;
106
107 cpicker->use_color_lock = lock;
108 cpicker->use_color_cubic = cubic;
109 cpicker->use_luminosity_lock = lock_luminosity;
110
111 if (lock_luminosity) {
112 float color[4]; /* in case of alpha */
113 RNA_property_float_get_array(ptr, prop, color);
114 cpicker->luminosity_lock_value = len_v3(color);
115 }
116
117 if (value_slider) {
118 switch (U.color_picker_type) {
120 row->separator();
121 hsv_but = (uiButHSVCube *)uiDefButR_prop(block,
123 0,
124 "",
125 WHEEL_SIZE + 6,
126 0,
127 14 * UI_SCALE_FAC,
129 ptr,
130 prop,
131 -1,
132 softmin,
133 softmax,
134 "");
135 hsv_but->gradient_type = UI_GRAD_L_ALT;
136 break;
138 col->separator();
139 hsv_but = (uiButHSVCube *)uiDefButR_prop(block,
141 0,
142 "",
143 0,
144 4,
146 18 * UI_SCALE_FAC,
147 ptr,
148 prop,
149 -1,
150 softmin,
151 softmax,
152 "");
154 break;
156 col->separator();
157 hsv_but = (uiButHSVCube *)uiDefButR_prop(block,
159 0,
160 "",
161 0,
162 4,
164 18 * UI_SCALE_FAC,
165 ptr,
166 prop,
167 -1,
168 softmin,
169 softmax,
170 "");
172 break;
174 col->separator();
175 hsv_but = (uiButHSVCube *)uiDefButR_prop(block,
177 0,
178 "",
179 0,
180 4,
182 18 * UI_SCALE_FAC,
183 ptr,
184 prop,
185 -1,
186 softmin,
187 softmax,
188 "");
190 break;
191
192 /* user default */
194 default:
195 row->separator();
196 hsv_but = (uiButHSVCube *)uiDefButR_prop(block,
198 0,
199 "",
200 WHEEL_SIZE + 6,
201 0,
202 14 * UI_SCALE_FAC,
204 ptr,
205 prop,
206 -1,
207 softmin,
208 softmax,
209 "");
210 hsv_but->gradient_type = UI_GRAD_V_ALT;
211 break;
212 }
213
214 hsv_but->custom_data = cpicker;
215 }
216}
217
218static void ui_template_palette_menu(bContext * /*C*/, uiLayout *layout, void * /*but_p*/)
219{
220 uiLayout *row;
221
222 layout->label(IFACE_("Sort By:"), ICON_NONE);
223 row = &layout->row(false);
224 uiItemEnumO_value(row, IFACE_("Hue"), ICON_NONE, "PALETTE_OT_sort", "type", 1);
225 row = &layout->row(false);
226 uiItemEnumO_value(row, IFACE_("Saturation"), ICON_NONE, "PALETTE_OT_sort", "type", 2);
227 row = &layout->row(false);
228 uiItemEnumO_value(row, IFACE_("Value"), ICON_NONE, "PALETTE_OT_sort", "type", 3);
229 row = &layout->row(false);
230 uiItemEnumO_value(row, IFACE_("Luminance"), ICON_NONE, "PALETTE_OT_sort", "type", 4);
231}
232
235 const StringRefNull propname,
236 bool /*colors*/)
237{
238 PropertyRNA *prop = RNA_struct_find_property(ptr, propname.c_str());
239 uiBut *but = nullptr;
240
241 const int cols_per_row = std::max(uiLayoutGetWidth(layout) / UI_UNIT_X, 1);
242
243 if (!prop) {
244 RNA_warning("property not found: %s.%s", RNA_struct_identifier(ptr->type), propname.c_str());
245 return;
246 }
247
248 const PointerRNA cptr = RNA_property_pointer_get(ptr, prop);
249 if (!cptr.data || !RNA_struct_is_a(cptr.type, &RNA_Palette)) {
250 return;
251 }
252
253 uiBlock *block = uiLayoutGetBlock(layout);
254
255 Palette *palette = static_cast<Palette *>(cptr.data);
256
257 uiLayout *col = &layout->column(true);
258 col->row(true);
259 uiDefIconButO(block,
261 "PALETTE_OT_color_add",
263 ICON_ADD,
264 0,
265 0,
266 UI_UNIT_X,
267 UI_UNIT_Y,
268 std::nullopt);
269 uiDefIconButO(block,
271 "PALETTE_OT_color_delete",
273 ICON_REMOVE,
274 0,
275 0,
276 UI_UNIT_X,
277 UI_UNIT_Y,
278 std::nullopt);
279 if (palette->colors.first != nullptr) {
280 but = uiDefIconButO(block,
282 "PALETTE_OT_color_move",
284 ICON_TRIA_UP,
285 0,
286 0,
287 UI_UNIT_X,
288 UI_UNIT_Y,
289 std::nullopt);
291 RNA_enum_set(but->opptr, "type", -1);
292
293 but = uiDefIconButO(block,
295 "PALETTE_OT_color_move",
297 ICON_TRIA_DOWN,
298 0,
299 0,
300 UI_UNIT_X,
301 UI_UNIT_Y,
302 std::nullopt);
304 RNA_enum_set(but->opptr, "type", 1);
305
306 /* Menu. */
308 block, ui_template_palette_menu, nullptr, ICON_SORTSIZE, 0, 0, UI_UNIT_X, UI_UNIT_Y, "");
309 }
310
311 col = &layout->column(true);
312 col->row(true);
313
314 int row_cols = 0, col_id = 0;
315 LISTBASE_FOREACH (PaletteColor *, color, &palette->colors) {
316 if (row_cols >= cols_per_row) {
317 col->row(true);
318 row_cols = 0;
319 }
320
321 PointerRNA color_ptr = RNA_pointer_create_discrete(&palette->id, &RNA_PaletteColor, color);
322 uiButColor *color_but = (uiButColor *)uiDefButR(block,
324 0,
325 "",
326 0,
327 0,
328 UI_UNIT_X,
329 UI_UNIT_Y,
330 &color_ptr,
331 "color",
332 -1,
333 0.0,
334 1.0,
335 "");
336 color_but->is_pallete_color = true;
337 color_but->palette_color_index = col_id;
338 row_cols++;
339 col_id++;
340 }
341}
342
345 const StringRefNull propname,
346 int icon)
347{
348 PropertyRNA *prop = RNA_struct_find_property(ptr, propname.c_str());
349
350 if (!prop) {
351 RNA_warning("property not found: %s.%s", RNA_struct_identifier(ptr->type), propname.c_str());
352 return;
353 }
354
355 uiBlock *block = uiLayoutGetBlock(layout);
356
357 uiBut *but = uiDefIconButO(block,
359 "UI_OT_eyedropper_color",
361 icon,
362 0,
363 0,
364 UI_UNIT_X,
365 UI_UNIT_Y,
367 but->rnapoin = *ptr;
368 but->rnaprop = prop;
369 but->rnaindex = -1;
370}
#define LISTBASE_FOREACH(type, var, list)
MINLINE float len_v3(const float a[3]) ATTR_WARN_UNUSED_RESULT
#define IFACE_(msgid)
#define UI_SCALE_FAC
@ USER_CP_SQUARE_SV
@ USER_CP_CIRCLE_HSL
@ USER_CP_SQUARE_HS
@ USER_CP_SQUARE_HV
@ USER_CP_CIRCLE_HSV
#define RNA_warning(format,...)
#define UI_UNIT_Y
uiBut * uiDefButR_prop(uiBlock *block, int type, int retval, std::optional< blender::StringRef > str, int x, int y, short width, short height, PointerRNA *ptr, PropertyRNA *prop, int index, float min, float max, std::optional< blender::StringRef > tip)
uiBut * uiDefIconButO(uiBlock *block, int type, blender::StringRefNull opname, wmOperatorCallContext opcontext, int icon, int x, int y, short width, short height, std::optional< blender::StringRef > tip)
PointerRNA * UI_but_operator_ptr_ensure(uiBut *but)
uiBut * uiDefIconMenuBut(uiBlock *block, uiMenuCreateFunc func, void *arg, int icon, int x, int y, short width, short height, std::optional< blender::StringRef > tip)
eButGradientType
@ UI_GRAD_L_ALT
@ UI_GRAD_SV
@ UI_GRAD_V_ALT
@ UI_GRAD_HV
@ UI_GRAD_HS
#define UI_UNIT_X
@ UI_BTYPE_BUT
@ UI_BTYPE_HSVCIRCLE
@ UI_BTYPE_HSVCUBE
@ UI_BTYPE_COLOR
uiBut * uiDefButR(uiBlock *block, int type, int retval, std::optional< blender::StringRef > str, int x, int y, short width, short height, PointerRNA *ptr, blender::StringRefNull propname, int index, float min, float max, std::optional< blender::StringRef > tip)
uiBlock * uiLayoutGetBlock(uiLayout *layout)
void uiItemEnumO_value(uiLayout *layout, blender::StringRefNull name, int icon, blender::StringRefNull opname, blender::StringRefNull propname, int value)
int uiLayoutGetWidth(uiLayout *layout)
@ WM_OP_INVOKE_DEFAULT
Definition WM_types.hh:238
volatile int lock
#define U
constexpr const char * c_str() const
uint col
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
ColorPicker * ui_block_colorpicker_create(uiBlock *block)
void uiTemplatePalette(uiLayout *layout, PointerRNA *ptr, const StringRefNull propname, bool)
void uiTemplateCryptoPicker(uiLayout *layout, PointerRNA *ptr, const StringRefNull propname, int icon)
static void ui_template_palette_menu(bContext *, uiLayout *layout, void *)
void uiTemplateColorPicker(uiLayout *layout, PointerRNA *ptr, const StringRefNull propname, bool value_slider, bool lock, bool lock_luminosity, bool cubic)
const char * RNA_property_ui_description(const PropertyRNA *prop)
bool RNA_struct_is_a(const StructRNA *type, const StructRNA *srna)
void RNA_property_float_get_array(PointerRNA *ptr, PropertyRNA *prop, float *values)
void RNA_property_float_ui_range(PointerRNA *ptr, PropertyRNA *prop, float *softmin, float *softmax, float *step, float *precision)
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
PointerRNA RNA_property_pointer_get(PointerRNA *ptr, PropertyRNA *prop)
const char * RNA_struct_identifier(const StructRNA *type)
void RNA_enum_set(PointerRNA *ptr, const char *name, int value)
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
float luminosity_lock_value
void * first
ListBase colors
StructRNA * type
Definition RNA_types.hh:52
void * data
Definition RNA_types.hh:53
eButGradientType gradient_type
void * custom_data
PropertyRNA * rnaprop
PointerRNA * opptr
PointerRNA rnapoin
void label(blender::StringRef name, int icon)
uiLayout & column(bool align)
void separator(float factor=1.0f, LayoutSeparatorType type=LayoutSeparatorType::Auto)
uiLayout & row(bool align)
PointerRNA * ptr
Definition wm_files.cc:4226