Blender V5.3
interface_template_curve_profile.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_context.hh"
10#include "BKE_curveprofile.h"
11#include "BKE_library.hh"
12
13#include "BLI_math_base_c.hh"
15#include "BLI_rect.hh"
16#include "BLI_string_ref.hh"
17
18#include "BLT_translation.hh"
19
20#include "ED_screen.hh"
21#include "ED_undo.hh"
22
23#include "RNA_access.hh"
24#include "RNA_prototypes.hh"
25
27#include "interface_intern.hh"
29
30namespace blender::ui {
31
32namespace {
33struct CurveRuntimeProperties {
34 CurveProfilePoint *last_pt = nullptr;
35 float2 last_pos;
36};
37} // namespace
38
39static Block *curve_profile_presets_fn(bContext *C, ARegion *region, void *cb_v)
40{
41 RNAUpdateCb &cb = *static_cast<RNAUpdateCb *>(cb_v);
42 PointerRNA profile_ptr = RNA_property_pointer_get(&cb.ptr, cb.prop);
43 CurveProfile *profile = static_cast<CurveProfile *>(profile_ptr.data);
44 short yco = 0;
45
46 Block *block = block_begin(C, region, __func__, EmbossType::Emboss);
47 for (const auto &item :
48 {std::pair<StringRef, eCurveProfilePresets>(IFACE_("Default"), PROF_PRESET_LINE),
49 std::pair<StringRef, eCurveProfilePresets>(
51 std::pair<StringRef, eCurveProfilePresets>(
53 std::pair<StringRef, eCurveProfilePresets>(
55 std::pair<StringRef, eCurveProfilePresets>(CTX_IFACE_(BLT_I18NCONTEXT_ID_MESH, "Steps"),
57 {
58 Button *but = uiDefIconTextBut(block,
60 ICON_BLANK1,
61 item.first,
62 0,
63 yco -= UI_UNIT_Y,
64 0,
66 nullptr,
67 "");
68 const eCurveProfilePresets preset = item.second;
69 button_func_set(but, [profile, cb, preset](bContext &C) {
70 profile->preset = preset;
73 ED_undo_push(&C, "Reset Curve Profile");
75 rna_update_cb(C, cb);
76 });
77 }
78
80 block_bounds_set_text(block, int(3.0f * UI_UNIT_X));
81
82 return block;
83}
84
85static Block *curve_profile_tools_fn(bContext *C, ARegion *region, void *cb_v)
86{
87 RNAUpdateCb &cb = *static_cast<RNAUpdateCb *>(cb_v);
88 PointerRNA profile_ptr = RNA_property_pointer_get(&cb.ptr, cb.prop);
89 CurveProfile *profile = static_cast<CurveProfile *>(profile_ptr.data);
90 short yco = 0;
91
92 Block *block = block_begin(C, region, __func__, EmbossType::Emboss);
93
94 {
95 Button *but = uiDefIconTextBut(block,
97 ICON_BLANK1,
98 IFACE_("Reset View"),
99 0,
100 yco -= UI_UNIT_Y,
101 0,
102 UI_UNIT_Y,
103 nullptr,
104 "");
105 button_func_set(but, [profile](bContext &C) {
108 });
109 }
110 {
111 Button *but = uiDefIconTextBut(block,
113 ICON_BLANK1,
114 IFACE_("Reset Curve"),
115 0,
116 yco -= UI_UNIT_Y,
117 0,
118 UI_UNIT_Y,
119 nullptr,
120 "");
121 button_func_set(but, [profile, cb](bContext &C) {
122 BKE_curveprofile_reset(profile);
124 ED_undo_push(&C, "Reset Profile");
126 rna_update_cb(C, cb);
127 });
128 }
129
131 block_bounds_set_text(block, int(3.0f * UI_UNIT_X));
132
133 return block;
134}
135
137{
138 return BLI_rctf_size_x(&profile->view_rect) >
140}
141
143{
144 return BLI_rctf_size_x(&profile->view_rect) < BLI_rctf_size_x(&profile->clip_rect);
145}
146
148{
149 if (curve_profile_can_zoom_in(profile)) {
150 const float dx = 0.1154f * BLI_rctf_size_x(&profile->view_rect);
151 profile->view_rect.xmin += dx;
152 profile->view_rect.xmax -= dx;
153 const float dy = 0.1154f * BLI_rctf_size_y(&profile->view_rect);
154 profile->view_rect.ymin += dy;
155 profile->view_rect.ymax -= dy;
156 }
157
159}
160
162{
163 if (curve_profile_can_zoom_out(profile)) {
164 float d = 0.15f * BLI_rctf_size_x(&profile->view_rect);
165 float d1 = d;
166
167 if (profile->flag & PROF_USE_CLIP) {
168 if (profile->view_rect.xmin - d < profile->clip_rect.xmin) {
169 d1 = profile->view_rect.xmin - profile->clip_rect.xmin;
170 }
171 }
172 profile->view_rect.xmin -= d1;
173
174 d1 = d;
175 if (profile->flag & PROF_USE_CLIP) {
176 if (profile->view_rect.xmax + d > profile->clip_rect.xmax) {
177 d1 = -profile->view_rect.xmax + profile->clip_rect.xmax;
178 }
179 }
180 profile->view_rect.xmax += d1;
181
182 d = d1 = 0.15f * BLI_rctf_size_y(&profile->view_rect);
183
184 if (profile->flag & PROF_USE_CLIP) {
185 if (profile->view_rect.ymin - d < profile->clip_rect.ymin) {
186 d1 = profile->view_rect.ymin - profile->clip_rect.ymin;
187 }
188 }
189 profile->view_rect.ymin -= d1;
190
191 d1 = d;
192 if (profile->flag & PROF_USE_CLIP) {
193 if (profile->view_rect.ymax + d > profile->clip_rect.ymax) {
194 d1 = -profile->view_rect.ymax + profile->clip_rect.ymax;
195 }
196 }
197 profile->view_rect.ymax += d1;
198 }
199
201}
202
203static void CurveProfile_buttons_layout(Layout &layout, PointerRNA *ptr, const RNAUpdateCb &cb)
204{
205 CurveProfile *profile = static_cast<CurveProfile *>(ptr->data);
206 Button *bt;
207
208 Block *block = layout.block();
209
211
212 layout.use_property_split_set(false);
213
214 /* Preset selector */
215 /* There is probably potential to use simpler "Layout::prop" functions here, but
216 * automatic updating after a preset is selected would be more complicated. */
217 Layout *row = &layout.row(true);
218 RNAUpdateCb *presets_cb = MEM_new<RNAUpdateCb>(__func__, cb);
219 bt = uiDefBlockBut(block,
221 presets_cb,
222 IFACE_("Preset"),
223 0,
224 0,
225 UI_UNIT_X,
226 UI_UNIT_X,
227 "");
228 /* Pass ownership of `presets_cb` to the button. */
230 bt,
231 [](bContext *, void *, void *) {},
232 presets_cb,
233 nullptr,
236
237 /* Show a "re-apply" preset button when it has been changed from the preset. */
238 if (profile->flag & PROF_DIRTY_PRESET) {
239 /* Only for dynamic presets. */
241 bt = uiDefIconTextBut(block,
243 ICON_NONE,
244 IFACE_("Apply Preset"),
245 0,
246 0,
247 UI_UNIT_X,
248 UI_UNIT_X,
249 nullptr,
250 TIP_("Reapply and update the preset, removing changes"));
251 button_func_set(bt, [profile, cb](bContext &C) {
252 BKE_curveprofile_reset(profile);
254 rna_update_cb(C, cb);
255 });
256 }
257 }
258
259 row = &layout.row(false);
260
261 /* (Left aligned) */
262 Layout *sub = &row->row(true);
264
265 /* Zoom in */
266 bt = uiDefIconBut(block,
268 ICON_ZOOM_IN,
269 0,
270 0,
271 UI_UNIT_X,
272 UI_UNIT_X,
273 nullptr,
274 0.0,
275 0.0,
276 TIP_("Zoom in"));
277 button_func_set(bt, [profile](bContext &C) { curve_profile_zoom_in(&C, profile); });
278 if (!curve_profile_can_zoom_in(profile)) {
279 button_disable(bt, "");
280 }
281
282 /* Zoom out */
283 bt = uiDefIconBut(block,
285 ICON_ZOOM_OUT,
286 0,
287 0,
288 UI_UNIT_X,
289 UI_UNIT_X,
290 nullptr,
291 0.0,
292 0.0,
293 TIP_("Zoom out"));
294 button_func_set(bt, [profile](bContext &C) { curve_profile_zoom_out(&C, profile); });
295 if (!curve_profile_can_zoom_out(profile)) {
296 button_disable(bt, "");
297 }
298
299 /* (Right aligned) */
300 sub = &row->row(true);
302
303 /* Flip path */
304 bt = uiDefIconBut(block,
306 ICON_ARROW_LEFTRIGHT,
307 0,
308 0,
309 UI_UNIT_X,
310 UI_UNIT_X,
311 nullptr,
312 0.0,
313 0.0,
314 TIP_("Reverse Path"));
315 button_func_set(bt, [profile, cb](bContext &C) {
318 rna_update_cb(C, cb);
319 });
320
321 /* Clipping toggle */
322 const int icon = (profile->flag & PROF_USE_CLIP) ? ICON_CLIPUV_HLT : ICON_CLIPUV_DEHLT;
323 bt = uiDefIconBut(block,
325 icon,
326 0,
327 0,
328 UI_UNIT_X,
329 UI_UNIT_X,
330 nullptr,
331 0.0,
332 0.0,
333 TIP_("Toggle Profile Clipping"));
334 button_func_set(bt, [profile, cb](bContext &C) {
335 profile->flag ^= PROF_USE_CLIP;
337 rna_update_cb(C, cb);
338 });
339
340 /* Reset view, reset curve */
341 RNAUpdateCb *tools_cb = MEM_new<RNAUpdateCb>(__func__, cb);
342 bt = uiDefIconBlockBut(block,
344 tools_cb,
345 ICON_NONE,
346 0,
347 0,
348 UI_UNIT_X,
349 UI_UNIT_X,
350 TIP_("Tools"));
351 /* Pass ownership of `presets_cb` to the button. */
353 bt,
354 [](bContext *, void *, void *) {},
355 tools_cb,
356 nullptr,
359
360 block_funcN_set(block,
361 rna_update_cb,
362 MEM_new<RNAUpdateCb>(__func__, cb),
363 nullptr,
366
367 /* The path itself */
368 int path_width = max_ii(layout.width(), UI_UNIT_X);
369 path_width = min_ii(path_width, int(16.0f * UI_UNIT_X));
370 const int path_height = path_width;
371 layout.row(false);
372 uiDefBut(block,
374 "",
375 0,
376 0,
377 short(path_width),
378 short(path_height),
379 profile,
380 0.0f,
381 1.0f,
382 "");
383
384 /* Position sliders for (first) selected point */
385 Vector<CurveProfilePoint *> selected_points;
386 bool point_last_or_first = false;
387 for (int i = 0; i < profile->path_len; i++) {
388 if (profile->path[i].flag & (PROF_SELECT | PROF_H1_SELECT | PROF_H2_SELECT)) {
389 selected_points.append(&profile->path[i]);
390 if (ELEM(i, 0, profile->path_len - 1) && profile->path[i].flag & PROF_SELECT) {
391 point_last_or_first = true;
392 }
393 }
394 }
395
396 /* Selected point data */
397 if (!selected_points.is_empty()) {
398 rctf bounds;
399 if (profile->flag & PROF_USE_CLIP) {
400 bounds = profile->clip_rect;
401 }
402 else {
403 bounds.xmin = bounds.ymin = -1000.0;
404 bounds.xmax = bounds.ymax = 1000.0;
405 }
406
407 row = &layout.row(true);
408
409 auto curve_runtime = std::make_shared<CurveRuntimeProperties>();
410 curve_runtime->last_pt = BKE_curveprofile_active_get(profile);
411
413 ptr->owner_id, RNA_CurveProfilePoint, curve_runtime->last_pt);
414 PropertyRNA *prop_handle_type = RNA_struct_find_property(&point_ptr, "handle_type_1");
415 row->prop(&point_ptr,
416 prop_handle_type,
418 0,
420 "",
421 ICON_NONE);
422
423 /* Position */
424 float *last_x_ptr = BKE_curveprofile_active_location_get(curve_runtime->last_pt);
425 float *last_y_ptr = last_x_ptr + 1;
426 curve_runtime->last_pos.x = *last_x_ptr;
427 curve_runtime->last_pos.y = *last_y_ptr;
428
429 /* While the slider controls the active element, all selected points move together.
430 * Contract the slider range so the outermost selected points stay within the clip region. */
431 rctf slider_bounds = bounds;
432 if (selected_points.size() > 1) {
433 rctf selection_bounds;
434 BLI_rctf_init_minmax(&selection_bounds);
435
436 /* The slider only shows the active point's position, but moves all selected points by the
437 * same delta. Clamp the range so points at the edges of the selection can't be moved outside
438 * the clip region. */
439 for (const CurveProfilePoint *pt : selected_points) {
440 if (pt->flag & PROF_SELECT) {
441 const float loc[2] = {pt->x, pt->y};
442 BLI_rctf_do_minmax_v(&selection_bounds, loc);
443 }
444 if (pt->flag & PROF_H1_SELECT) {
445 BLI_rctf_do_minmax_v(&selection_bounds, pt->h1_loc);
446 }
447 if (pt->flag & PROF_H2_SELECT) {
448 BLI_rctf_do_minmax_v(&selection_bounds, pt->h2_loc);
449 }
450 }
451
452 slider_bounds.xmin += curve_runtime->last_pos.x - selection_bounds.xmin;
453 slider_bounds.xmax += curve_runtime->last_pos.x - selection_bounds.xmax;
454 slider_bounds.ymin += curve_runtime->last_pos.y - selection_bounds.ymin;
455 slider_bounds.ymax += curve_runtime->last_pos.y - selection_bounds.ymax;
456 }
457
458 /* Requires BKE_curveprofile_translate_selection to handle the handle manipulation, no
459 * simplified logic. */
460 const char *const axis_labels[2] = {"X:", "Y:"};
461 float *const axis_ptrs[2] = {last_x_ptr, last_y_ptr};
462 const float axis_min[2] = {slider_bounds.xmin, slider_bounds.ymin};
463 const float axis_max[2] = {slider_bounds.xmax, slider_bounds.ymax};
464 for (int axis = 0; axis < 2; axis++) {
465 bt = uiDefButV(block,
467 axis_labels[axis],
468 0,
469 (2 - axis) * UI_UNIT_Y,
470 UI_UNIT_X * 10,
471 UI_UNIT_Y,
472 axis_ptrs[axis],
473 axis_min[axis],
474 axis_max[axis],
475 "");
478 button_func_set(bt, [profile, cb, curve_runtime, axis](bContext &C) {
479 float *last_pt_co = BKE_curveprofile_active_location_get(curve_runtime->last_pt);
480 const float delta = last_pt_co[axis] - curve_runtime->last_pos[axis];
481 /* Logically `-= delta`, better restore the original value. */
482 last_pt_co[axis] = curve_runtime->last_pos[axis];
483 float2 offset(0.0f);
484 offset[axis] = delta;
487 rna_update_cb(C, cb);
488
489 /* Update the active point if the pointer changed. */
490 curve_runtime->last_pt = BKE_curveprofile_active_get(profile);
491 last_pt_co = BKE_curveprofile_active_location_get(curve_runtime->last_pt);
492 curve_runtime->last_pos[axis] = last_pt_co[axis];
493 });
494 if (point_last_or_first) {
496 }
497 }
498
499 /* Delete points */
500 bt = uiDefIconBut(block,
502 ICON_X,
503 0,
504 0,
505 UI_UNIT_X,
506 UI_UNIT_X,
507 nullptr,
508 0.0,
509 0.0,
510 TIP_("Delete points"));
511 button_func_set(bt, [profile, cb](bContext &C) {
514 rna_update_cb(C, cb);
515 });
516 if (point_last_or_first) {
518 }
519 }
520
521 layout.prop(ptr, "use_sample_straight_edges", UI_ITEM_NONE, std::nullopt, ICON_NONE);
522 layout.prop(ptr, "use_sample_even_lengths", UI_ITEM_NONE, std::nullopt, ICON_NONE);
523
524 block_funcN_set(block, nullptr, nullptr, nullptr);
525}
526
527void template_curve_profile(Layout *layout, PointerRNA *ptr, const StringRefNull propname)
528{
529 PropertyRNA *prop = RNA_struct_find_property(ptr, propname.c_str());
530
531 Block *block = layout->block();
532
533 if (!prop) {
534 RNA_warning("Curve Profile property not found: %s.%s",
536 propname.c_str());
537 return;
538 }
539
540 if (RNA_property_type(prop) != PROP_POINTER) {
541 RNA_warning("Curve Profile is not a pointer: %s.%s",
543 propname.c_str());
544 return;
545 }
546
547 PointerRNA cptr = RNA_property_pointer_get(ptr, prop);
548 if (!cptr || !RNA_struct_is_a(cptr.type, RNA_CurveProfile)) {
549 return;
550 }
551
552 ID *id = cptr.owner_id;
554
555 CurveProfile_buttons_layout(*layout, &cptr, RNAUpdateCb{*ptr, prop});
556
557 block_lock_clear(block);
558}
559
560} // namespace blender::ui
#define SELECT
Definition BKE_mask.hh:19
#define TIP_(msgid)
#define CTX_IFACE_(context, msgid)
#define IFACE_(msgid)
#define BLT_I18NCONTEXT_ID_MESH
#define ID_IS_EDITABLE(_id)
Definition DNA_ID.h:708
#define RNA_warning(format,...)
#define C
Definition RandGen.cpp:29
#define UI_UNIT_Y
#define UI_UNIT_X
#define UI_ITEM_NONE
#define RNA_NO_INDEX
#define ELEM(...)
constexpr const char * c_str() const
void append(const T &value)
#define CURVE_ZOOM_MAX
#define ERROR_LIBDATA_MESSAGE
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)
void block_direction_set(Block *block, char direction)
void template_curve_profile(Layout *layout, PointerRNA *ptr, StringRefNull propname)
void button_func_set(Button *but, std::function< void(bContext &)> func)
void block_lock_clear(Block *block)
void button_number_step_size_set(Button *but, float step_size)
static bool curve_profile_can_zoom_in(CurveProfile *profile)
static void curve_profile_zoom_in(bContext *C, CurveProfile *profile)
Button * uiDefBlockBut(Block *block, BlockCreateFunc func, void *arg, StringRef str, int x, int y, short width, short height, std::optional< StringRef > tip)
static void curve_profile_zoom_out(bContext *C, CurveProfile *profile)
void block_bounds_set_text(Block *block, int addval)
Definition interface.cc:661
static bool curve_profile_can_zoom_out(CurveProfile *profile)
void button_number_precision_set(Button *but, float precision)
void button_disable(Button *but, const char *disabled_hint)
void button_flag_enable(Button *but, int64_t flag)
static Block * curve_profile_presets_fn(bContext *C, ARegion *region, void *cb_v)
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)
void * but_func_argN_copy(const void *argN)
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)
static void CurveProfile_buttons_layout(Layout &layout, PointerRNA *ptr, const RNAUpdateCb &cb)
void block_emboss_set(Block *block, EmbossType emboss)
void but_func_argN_free(void *argN)
Button * uiDefIconBut(Block *block, ButtonTypeWithPointerType but_and_ptr_type, int icon, int x, int y, short width, short height, void *poin, float min, float max, std::optional< StringRef > tip)
static Block * curve_profile_tools_fn(bContext *C, ARegion *region, void *cb_v)
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)
Button * uiDefIconBlockBut(Block *block, BlockCreateFunc func, void *arg, int icon, int x, int y, short width, short height, std::optional< StringRef > tip)
void block_funcN_set(Block *block, ButtonHandleNFunc funcN, void *argN, void *arg2, ButtonArgNFree func_argN_free_fn=MEM_delete_void, ButtonArgNCopy func_argN_copy_fn=MEM_dupalloc_void)
PointerRNA RNA_property_pointer_get(PointerRNA *ptr, PropertyRNA *prop)
void BKE_curveprofile_translate_selection(struct CurveProfile *profile, const blender::float2 &offset)
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
bool RNA_struct_is_a(const StructRNA *type, const StructRNA *srna)
void BLI_rctf_do_minmax_v(struct rctf *rect, const float xy[2])
Definition rct.cc:524
MINLINE int max_ii(int a, int b)
void BKE_curveprofile_remove_by_flag(struct CurveProfile *profile, short flag)
void ED_region_tag_redraw(ARegion *region)
Definition area.cc:681
void BKE_curveprofile_reset(struct CurveProfile *profile)
PropertyType RNA_property_type(PropertyRNA *prop)
VecBase< float, 2 > float2
@ PROP_POINTER
Definition RNA_types.hh:215
BLI_INLINE float BLI_rctf_size_y(const struct rctf *rct)
Definition BLI_rect.hh:215
void ED_undo_push(bContext *C, const char *str, UndoEncodeHints hints=UndoEncodeHints(0))
Definition ed_undo.cc:103
void BKE_curveprofile_reset_view(struct CurveProfile *profile)
void BLI_rctf_init_minmax(struct rctf *rect)
Definition rct.cc:494
@ PROF_UPDATE_REMOVE_DOUBLES
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
const char * RNA_struct_identifier(const StructRNA *type)
BLI_INLINE float BLI_rctf_size_x(const struct rctf *rct)
Definition BLI_rect.hh:211
float * BKE_curveprofile_active_location_get(struct CurveProfilePoint *point)
void BKE_curveprofile_update(struct CurveProfile *profile, int update_flags)
MINLINE int min_ii(int a, int b)
CurveProfilePoint * BKE_curveprofile_active_get(CurveProfile *profile)
ARegion * CTX_wm_region(const bContext *C)
void BKE_curveprofile_reverse(struct CurveProfile *profile)
eCurveProfilePresets preset
StructRNA * type
Definition RNA_types.hh:63
void alignment_set(LayoutAlign alignment)
void use_property_split_set(bool is_sep)
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 & row(bool align)