Blender V5.3
interface_template_color_ramp.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 "BKE_colorband.hh"
10#include "BKE_context.hh"
11#include "BKE_library.hh"
12
13#include "BLI_listbase.hh"
14#include "BLI_rect.hh"
15#include "BLI_string_ref.hh"
16
17#include "BLT_translation.hh"
18
19#include "DNA_texture_types.h"
20
21#include "ED_screen.hh"
22#include "ED_undo.hh"
23
24#include "RNA_access.hh"
25#include "RNA_prototypes.hh"
26
28#include "interface_intern.hh"
30
31namespace blender::ui {
32
33static void colorband_flip(bContext *C, ColorBand *coba)
34{
35 CBData data_tmp[MAXCOLORBAND];
36
37 for (int a = 0; a < coba->tot; a++) {
38 data_tmp[a] = coba->data[coba->tot - (a + 1)];
39 }
40 for (int a = 0; a < coba->tot; a++) {
41 data_tmp[a].pos = 1.0f - data_tmp[a].pos;
42 coba->data[a] = data_tmp[a];
43 }
44
45 /* May as well flip the `cur`. */
46 coba->cur = coba->tot - (coba->cur + 1);
47
48 ED_undo_push(C, "Flip Color Ramp");
49}
50
51static void colorband_distribute(bContext *C, ColorBand *coba, bool evenly)
52{
53 if (coba->tot > 1) {
54 const int tot = evenly ? coba->tot - 1 : coba->tot;
55 const float gap = 1.0f / tot;
56 float pos = 0.0f;
57 for (int a = 0; a < coba->tot; a++) {
58 coba->data[a].pos = pos;
59 pos += gap;
60 }
61 const char *undo_str = evenly ? CTX_N_(BLT_I18NCONTEXT_OPERATOR_DEFAULT,
62 "Distribute Stops Evenly") :
64 "Distribute Stops from Left");
65 ED_undo_push(C, undo_str);
66 }
67}
68
70 TooltipData &tip,
71 Button *but,
72 void * /*space*/)
73{
74 const bool has_tip = !but->tip.is_empty();
75 if (has_tip) {
76 tooltip_text_field_add(tip, but->tip + ".", {}, TIP_STYLE_HEADER, TIP_LC_NORMAL, false);
77 }
79 TIP_("Disabled: Color ramp only has one stop"),
80 {},
83 has_tip);
84};
85
86static Block *colorband_tools_fn(bContext *C, ARegion *region, void *cb_v)
87{
88 RNAUpdateCb &cb = *static_cast<RNAUpdateCb *>(cb_v);
89 const uiStyle *style = style_get_dpi();
90 PointerRNA coba_ptr = RNA_property_pointer_get(&cb.ptr, cb.prop);
91 ColorBand *coba = static_cast<ColorBand *>(coba_ptr.data);
92 short yco = 0;
93 const short menuwidth = 10 * UI_UNIT_X;
94
95 Block *block = block_begin(C, region, __func__, EmbossType::Pulldown);
96
97 Layout &layout = block_layout(block,
100 0,
101 0,
103 0,
105 style);
106 block_layout_set_current(block, &layout);
107 {
108 layout.context_ptr_set("color_ramp", &coba_ptr);
109 }
110
111 /* We could move these to operators,
112 * although this isn't important unless we want to assign key shortcuts to them. */
113 {
114 Button *but = uiDefIconTextBut(block,
116 ICON_ARROW_LEFTRIGHT,
117 IFACE_("Flip Color Ramp"),
118 0,
119 yco -= UI_UNIT_Y,
120 menuwidth,
121 UI_UNIT_Y,
122 nullptr,
123 "");
124 button_func_set(but, [coba, cb](bContext &C) {
125 colorband_flip(&C, coba);
127 rna_update_cb(C, cb);
128 });
129 }
130 {
131 Button *but = uiDefIconTextBut(block,
133 ICON_BLANK1,
134 IFACE_("Distribute Stops from Left"),
135 0,
136 yco -= UI_UNIT_Y,
137 menuwidth,
138 UI_UNIT_Y,
139 nullptr,
140 "");
141 button_func_set(but, [coba, cb](bContext &C) {
142 colorband_distribute(&C, coba, false);
144 rna_update_cb(C, cb);
145 });
146
147 if (coba->tot < 2) {
150 }
151 }
152 {
153 Button *but = uiDefIconTextBut(block,
155 ICON_BLANK1,
156 IFACE_("Distribute Stops Evenly"),
157 0,
158 yco -= UI_UNIT_Y,
159 menuwidth,
160 UI_UNIT_Y,
161 nullptr,
162 "");
163 button_func_set(but, [coba, cb](bContext &C) {
164 colorband_distribute(&C, coba, true);
166 rna_update_cb(C, cb);
167 });
168
169 if (coba->tot < 2) {
172 }
173 }
174
175 layout.separator();
176
177 layout.op("UI_OT_eyedropper_colorramp", IFACE_("Eyedropper"), ICON_EYEDROPPER);
178
179 layout.separator();
180
181 {
182 Button *but = uiDefIconTextBut(block,
184 ICON_LOOP_BACK,
185 IFACE_("Reset Color Ramp"),
186 0,
187 yco -= UI_UNIT_Y,
188 menuwidth,
189 UI_UNIT_Y,
190 nullptr,
191 "");
192 button_func_set(but, [coba, cb](bContext &C) {
193 BKE_colorband_init(coba, true);
194 ED_undo_push(&C, "Reset Color Ramp");
196 rna_update_cb(C, cb);
197 });
198 }
199
201 block_bounds_set_text(block, 3.0f * UI_UNIT_X);
202
203 return block;
204}
205
206static void colorband_add(bContext &C, const RNAUpdateCb &cb, ColorBand &coba)
207{
208 float pos = 0.5f;
209
210 if (coba.tot > 1) {
211 if (coba.cur > 0) {
212 pos = (coba.data[coba.cur - 1].pos + coba.data[coba.cur].pos) * 0.5f;
213 }
214 else {
215 pos = (coba.data[coba.cur + 1].pos + coba.data[coba.cur].pos) * 0.5f;
216 }
217 }
218
219 if (BKE_colorband_element_add(&coba, pos)) {
220 rna_update_cb(C, cb);
221 ED_undo_push(&C, "Add Color Ramp Stop");
222 }
223}
224
225static void colorband_update_cb(bContext * /*C*/, void *bt_v, void *coba_v)
226{
227 Button *bt = static_cast<Button *>(bt_v);
228 ColorBand *coba = static_cast<ColorBand *>(coba_v);
229
230 /* Sneaky update here, we need to sort the color-band points to be in order,
231 * however the RNA pointer then is wrong, so we update it */
233 bt->rnapoin.data = coba->data + coba->cur;
234}
235
237 Block *block,
238 ColorBand *coba,
239 const rctf *butr,
240 const RNAUpdateCb &cb,
241 int expand)
242{
243 Button *bt;
244 const float unit = BLI_rctf_size_x(butr) / 14.0f;
245 const float xs = butr->xmin;
246 const float ys = butr->ymin;
247
248 PointerRNA ptr = RNA_pointer_create_discrete(cb.ptr.owner_id, RNA_ColorRamp, coba);
249
250 Layout *split = &layout.split(0.4f, false);
251
253 block_align_begin(block);
254 Layout *row = &split->row(false);
255
256 bt = uiDefIconTextBut(block,
258 ICON_ADD,
259 "",
260 0,
261 0,
262 2.0f * unit,
263 UI_UNIT_Y,
264 nullptr,
265 TIP_("Add a new color stop to the color ramp"));
266 button_func_set(bt, [coba, cb](bContext &C) { colorband_add(C, cb, *coba); });
267
268 bt = uiDefIconTextBut(block,
270 ICON_REMOVE,
271 "",
272 xs + 2.0f * unit,
273 ys + UI_UNIT_Y,
274 2.0f * unit,
275 UI_UNIT_Y,
276 nullptr,
277 TIP_("Delete the active position"));
278 button_func_set(bt, [coba, cb](bContext &C) {
279 if (BKE_colorband_element_remove(coba, coba->cur)) {
280 rna_update_cb(C, cb);
281 ED_undo_push(&C, "Delete Color Ramp Stop");
282 }
283 });
284 if (coba->tot < 2) {
287 }
288
289 RNAUpdateCb *tools_cb = MEM_new<RNAUpdateCb>(__func__, cb);
290 bt = uiDefIconBlockBut(block,
292 tools_cb,
293 ICON_DOWNARROW_HLT,
294 xs + 4.0f * unit,
295 ys + UI_UNIT_Y,
296 2.0f * unit,
297 UI_UNIT_Y,
298 TIP_("Tools"));
299 /* Pass ownership of `tools_cb` to the button. */
301 bt,
302 [](bContext *, void *, void *) {},
303 tools_cb,
304 nullptr,
307
308 block_align_end(block);
310
311 row = &split->row(false);
312
313 block_align_begin(block);
314 row->prop(&ptr, "color_mode", UI_ITEM_NONE, "", ICON_NONE);
316 row->prop(&ptr, "hue_interpolation", UI_ITEM_NONE, "", ICON_NONE);
317 }
318 else { /* COLBAND_BLEND_RGB */
319 row->prop(&ptr, "interpolation", UI_ITEM_NONE, "", ICON_NONE);
320 }
321 block_align_end(block);
322
323 row = &layout.row(false);
324
325 bt = uiDefBut(
326 block, ButtonType::ColorBand, "", xs, ys, BLI_rctf_size_x(butr), UI_UNIT_Y, coba, 0, 0, "");
327 bt->rnapoin = cb.ptr;
328 bt->rnaprop = cb.prop;
329 button_func_set(bt, [cb](bContext &C) { rna_update_cb(C, cb); });
330
331 row = &layout.row(false);
332
333 if (coba->tot) {
334 CBData *cbd = coba->data + coba->cur;
335
336 ptr = RNA_pointer_create_discrete(cb.ptr.owner_id, RNA_ColorRampElement, cbd);
337
338 if (!expand) {
339 split = &layout.split(0.3f, false);
340
341 row = &split->row(false);
342 bt = uiDefButV(block,
344 "",
345 0,
346 0,
347 5.0f * UI_UNIT_X,
348 UI_UNIT_Y,
349 &coba->cur,
350 0.0,
351 float(std::max(0, coba->tot - 1)),
352 TIP_("Choose active color stop"));
354
355 row = &split->row(false);
356 row->prop(&ptr, "position", UI_ITEM_NONE, IFACE_("Pos"), ICON_NONE);
357
358 row = &layout.row(false);
359 row->prop(&ptr, "color", UI_ITEM_NONE, "", ICON_NONE);
360 }
361 else {
362 split = &layout.split(0.5f, false);
363 Layout &subsplit = split->split(0.35f, false);
364
365 row = &subsplit.row(false);
366 bt = uiDefButV(block,
368 "",
369 0,
370 0,
371 5.0f * UI_UNIT_X,
372 UI_UNIT_Y,
373 &coba->cur,
374 0.0,
375 float(std::max(0, coba->tot - 1)),
376 TIP_("Choose active color stop"));
378
379 row = &subsplit.row(false);
380 row->prop(&ptr, "position", ITEM_R_SLIDER, IFACE_("Pos"), ICON_NONE);
381
382 row = &split->row(false);
383 row->prop(&ptr, "color", UI_ITEM_NONE, "", ICON_NONE);
384 }
385
386 /* Some special (rather awkward) treatment to update UI state on certain property changes. */
387 for (Button &but : block->buttons() | std::views::reverse) {
388 if (but.rnapoin.data != ptr.data) {
389 continue;
390 }
391 if (!but.rnaprop) {
392 continue;
393 }
394
395 const char *prop_identifier = RNA_property_identifier(but.rnaprop);
396 if (STREQ(prop_identifier, "position")) {
397 button_func_set(&but, colorband_update_cb, &but, coba);
398 }
399
400 if (STREQ(prop_identifier, "color")) {
401 button_func_set(bt, [cb](bContext &C) { rna_update_cb(C, cb); });
402 }
403 }
404 }
405}
406
408 PointerRNA *ptr,
409 const StringRefNull propname,
410 bool expand)
411{
412 PropertyRNA *prop = RNA_struct_find_property(ptr, propname.c_str());
413
414 if (!prop || RNA_property_type(prop) != PROP_POINTER) {
415 return;
416 }
417
418 const PointerRNA cptr = RNA_property_pointer_get(ptr, prop);
419 if (!cptr || !RNA_struct_is_a(cptr.type, RNA_ColorRamp)) {
420 return;
421 }
422
423 rctf rect;
424 rect.xmin = 0;
425 rect.xmax = 10.0f * UI_UNIT_X;
426 rect.ymin = 0;
427 rect.ymax = 19.5f * UI_UNIT_X;
428
429 Block *block = layout->absolute().block();
430
431 ID *id = cptr.owner_id;
433
435 *layout, block, static_cast<ColorBand *>(cptr.data), &rect, RNAUpdateCb{*ptr, prop}, expand);
436
437 block_lock_clear(block);
438}
439
440} // namespace blender::ui
#define MAXCOLORBAND
#define CTX_N_(context, msgid)
#define TIP_(msgid)
#define BLT_I18NCONTEXT_OPERATOR_DEFAULT
#define IFACE_(msgid)
#define ID_IS_EDITABLE(_id)
Definition DNA_ID.h:708
#define C
Definition RandGen.cpp:29
#define UI_UNIT_Y
#define UI_UNIT_X
#define UI_ITEM_NONE
#define ELEM(...)
#define STREQ(a, b)
constexpr const char * c_str() const
constexpr bool is_empty() const
#define UI_MENU_PADDING
#define UI_MENU_WIDTH_MIN
#define ERROR_LIBDATA_MESSAGE
void split(const std::string &s, const char delim, std::vector< std::string > &tokens)
Definition abc_util.cc:68
Button * uiDefIconTextBut(Block *block, ButtonTypeWithPointerType but_and_ptr_type, int icon, StringRef str, int x, int y, short width, short height, void *poin, std::optional< StringRef > tip)
void block_lock_set(Block *block, bool val, const char *lockstr)
static void colorband_flip(bContext *C, ColorBand *coba)
static void colorramp_disabled_tip_func(bContext &, TooltipData &tip, Button *but, void *)
void block_direction_set(Block *block, char direction)
static void colorband_buttons_layout(Layout &layout, Block *block, ColorBand *coba, const rctf *butr, const RNAUpdateCb &cb, int expand)
void button_func_set(Button *but, std::function< void(bContext &)> func)
void block_lock_clear(Block *block)
void block_align_begin(Block *block)
void button_number_step_size_set(Button *but, float step_size)
static void colorband_update_cb(bContext *, void *bt_v, void *coba_v)
static void colorband_distribute(bContext *C, ColorBand *coba, bool evenly)
const uiStyle * style_get_dpi()
void button_func_tooltip_custom_set(Button *but, ButtonToolTipCustomFunc func, void *arg, FreeArgFunc free_arg)
void block_bounds_set_text(Block *block, int addval)
Definition interface.cc:661
static Block * colorband_tools_fn(bContext *C, ARegion *region, void *cb_v)
void button_flag_enable(Button *but, int64_t flag)
Block * block_begin(const bContext *C, ARegion *region, std::string name, EmbossType emboss)
Button * uiDefBut(Block *block, ButtonTypeWithPointerType but_and_ptr_type, StringRef str, int x, int y, short width, short height, void *poin, float min, float max, std::optional< StringRef > tip)
static void colorband_add(bContext &C, const RNAUpdateCb &cb, ColorBand &coba)
void * but_func_argN_copy(const void *argN)
void block_align_end(Block *block)
void button_funcN_set(Button *but, ButtonHandleNFunc funcN, void *argN, void *arg2, ButtonArgNFree func_argN_free_fn=MEM_delete_void, ButtonArgNCopy func_argN_copy_fn=MEM_dupalloc_void)
void tooltip_text_field_add(TooltipData &data, std::string text, std::string suffix, const TooltipStyle style, const TooltipColorID color_id, const bool is_pad=false)
void block_emboss_set(Block *block, EmbossType emboss)
void template_color_ramp(Layout *layout, PointerRNA *ptr, StringRefNull propname, bool expand)
void but_func_argN_free(void *argN)
Button * uiDefButV(Block *block, ButtonType type, StringRef str, int x, int y, short width, short height, T *poin, float min, float max, std::optional< StringRef > tip)
Layout & block_layout(Block *block, LayoutDirection direction, LayoutType type, int x, int y, int size, int em, int padding, const uiStyle *style)
Button * uiDefIconBlockBut(Block *block, BlockCreateFunc func, void *arg, int icon, int x, int y, short width, short height, std::optional< StringRef > tip)
void block_layout_set_current(Block *block, Layout *layout)
PointerRNA RNA_property_pointer_get(PointerRNA *ptr, PropertyRNA *prop)
void BKE_colorband_init(ColorBand *coba, bool rangetype)
Definition colorband.cc:25
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
bool RNA_struct_is_a(const StructRNA *type, const StructRNA *srna)
const char * RNA_property_identifier(const PropertyRNA *prop)
void ED_region_tag_redraw(ARegion *region)
Definition area.cc:681
PropertyType RNA_property_type(PropertyRNA *prop)
@ PROP_POINTER
Definition RNA_types.hh:215
void ED_undo_push(bContext *C, const char *str, UndoEncodeHints hints=UndoEncodeHints(0))
Definition ed_undo.cc:103
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
void BKE_colorband_update_sort(ColorBand *coba)
Definition colorband.cc:587
CBData * BKE_colorband_element_add(ColorBand *coba, float position)
Definition colorband.cc:609
BLI_INLINE float BLI_rctf_size_x(const struct rctf *rct)
Definition BLI_rect.hh:211
bool BKE_colorband_element_remove(ColorBand *coba, int index)
Definition colorband.cc:635
ARegion * CTX_wm_region(const bContext *C)
eColorBand_ColorMode color_mode
StructRNA * type
Definition RNA_types.hh:63
std::ranges::transform_view< std::ranges::ref_view< const Vector< std::unique_ptr< Button > > >, Button &(*)(const std::unique_ptr< Button > &)> buttons() const
PointerRNA op(wmOperatorType *ot, std::optional< StringRef > name, int icon, wm::OpCallContext context, eUI_Item_Flag flag)
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)
Layout & split(float percentage, bool align)
void separator(float factor=1.0f, LayoutSeparatorType type=LayoutSeparatorType::Auto)
Layout & absolute(bool align=false)
Layout & row(bool align)
void context_ptr_set(StringRef name, const PointerRNA *ptr)