Blender V5.3
interface_align.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2015 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "DNA_screen_types.h"
10#include "DNA_userdef_types.h"
11
12#include "BLI_listbase.hh"
13#include "BLI_math_vector_c.hh"
15#include "BLI_rect.hh"
16#include "BLI_vector.hh"
17
18#include "interface_intern.hh"
19
20#include "MEM_guardedalloc.h"
21
22namespace blender::ui {
23
45struct ButAlign {
47
48 /* Neighbor buttons */
50
51 /* Pointers to coordinates (rctf values) of the button. */
52 std::array<float *, 4> borders;
53
54 /* Distances to the neighbors. */
56
57 /* Flags, used to mark whether we should 'stitch'
58 * the corners of this button with its neighbors' ones. */
60};
61
62/* Side-related enums and flags. */
63enum {
64 /* Sides (used as indices, order is **crucial**,
65 * this allows us to factorize code in a loop over the four sides). */
66 LEFT = 0,
67 TOP = 1,
68 RIGHT = 2,
69 DOWN = 3,
71
72 /* Stitch flags, built from sides values. */
77};
78
79/* Mapping between 'our' sides and 'public' BUT_ALIGN flags, order must match enum above. */
80#define SIDE_TO_BUT_ALIGN {BUT_ALIGN_LEFT, BUT_ALIGN_TOP, BUT_ALIGN_RIGHT, BUT_ALIGN_DOWN}
81
82/* Given one side, compute the three other ones */
83#define SIDE1(_s) (((_s) + 1) % TOTSIDES)
84#define OPPOSITE(_s) (((_s) + 2) % TOTSIDES)
85#define SIDE2(_s) (((_s) + 3) % TOTSIDES)
86
87/* 0: LEFT/RIGHT sides; 1 = TOP/DOWN sides. */
88#define IS_COLUMN(_s) ((_s) % 2)
89
90/* Stitch flag from side value. */
91#define STITCH(_s) (1 << (_s))
92
93/* Max distance between to buttons for them to be 'mergeable'. */
94#define MAX_DELTA 0.45f * max_ii(UI_UNIT_Y, UI_UNIT_X)
95
96bool button_can_align(const Button *but)
97{
98 const bool btype_can_align = !ELEM(but->type,
106 return (btype_can_align && !BLI_rctf_is_empty(&but->rect));
107}
108
116static void block_align_proximity_compute(ButAlign *butal, ButAlign *butal_other)
117{
118 /* That's the biggest gap between two borders to consider them 'alignable'. */
119 const float max_delta = MAX_DELTA;
120 float delta, delta_side_opp;
121 int side, side_opp;
122
123 const bool butal_can_align = button_can_align(butal->but);
124 const bool butal_other_can_align = button_can_align(butal_other->but);
125
126 const bool buts_share[2] = {
127 /* Sharing same line? */
128 !((*butal->borders[DOWN] >= *butal_other->borders[TOP]) ||
129 (*butal->borders[TOP] <= *butal_other->borders[DOWN])),
130 /* Sharing same column? */
131 !((*butal->borders[LEFT] >= *butal_other->borders[RIGHT]) ||
132 (*butal->borders[RIGHT] <= *butal_other->borders[LEFT])),
133 };
134
135 /* Early out in case buttons share no column or line, or if none can align... */
136 if (!(buts_share[0] || buts_share[1]) || !(butal_can_align || butal_other_can_align)) {
137 return;
138 }
139
140 for (side = 0; side < RIGHT; side++) {
141 /* We are only interested in buttons which share a same line
142 * (LEFT/RIGHT sides) or column (TOP/DOWN sides). */
143 if (buts_share[IS_COLUMN(side)]) {
144 side_opp = OPPOSITE(side);
145
146 /* We check both opposite sides at once, because with very small buttons,
147 * delta could be below max_delta for the wrong side
148 * (that is, in horizontal case, the total width of two buttons can be below max_delta).
149 * We rely on exact zero value here as an 'already processed' flag,
150 * so ensure we never actually set a zero value at this stage.
151 * FLT_MIN is zero-enough for UI position computing. ;) */
152 delta = max_ff(fabsf(*butal->borders[side] - *butal_other->borders[side_opp]), FLT_MIN);
153 delta_side_opp = max_ff(fabsf(*butal->borders[side_opp] - *butal_other->borders[side]),
154 FLT_MIN);
155 if (delta_side_opp < delta) {
156 std::swap(side, side_opp);
157 delta = delta_side_opp;
158 }
159
160 if (delta < max_delta) {
161 /* We are only interested in neighbors that are
162 * at least as close as already found ones. */
163 if (delta <= butal->dists[side]) {
164 {
165 /* We found an as close or closer neighbor.
166 * If both buttons are alignable, we set them as each other neighbors.
167 * Else, we have an unalignable one, we need to reset the others matching
168 * neighbor to nullptr if its 'proximity distance'
169 * is really lower with current one.
170 *
171 * NOTE: We cannot only execute that piece of code in case we found a
172 * **closer** neighbor, due to the limited way we represent neighbors
173 * (buttons only know **one** neighbor on each side, when they can
174 * actually have several ones), it would prevent some buttons to be
175 * properly 'neighborly-initialized'. */
176 if (butal_can_align && butal_other_can_align) {
177 butal->neighbors[side] = butal_other;
178 butal_other->neighbors[side_opp] = butal;
179 }
180 else if (butal_can_align && (delta < butal->dists[side])) {
181 butal->neighbors[side] = nullptr;
182 }
183 else if (butal_other_can_align && (delta < butal_other->dists[side_opp])) {
184 butal_other->neighbors[side_opp] = nullptr;
185 }
186 butal->dists[side] = butal_other->dists[side_opp] = delta;
187 }
188
189 if (butal_can_align && butal_other_can_align) {
190 const int side_s1 = SIDE1(side);
191 const int side_s2 = SIDE2(side);
192
193 const int stitch = STITCH(side);
194 const int stitch_opp = STITCH(side_opp);
195
196 if (butal->neighbors[side] == nullptr) {
197 butal->neighbors[side] = butal_other;
198 }
199 if (butal_other->neighbors[side_opp] == nullptr) {
200 butal_other->neighbors[side_opp] = butal;
201 }
202
203 /* We have a pair of neighbors, we have to check whether we
204 * can stitch their matching corners.
205 * E.g. if butal_other is on the left of butal (that is, side == LEFT),
206 * if both TOP (side_s1) coordinates of buttons are close enough,
207 * we can stitch their upper matching corners,
208 * and same for DOWN (side_s2) side. */
209 delta = fabsf(*butal->borders[side_s1] - *butal_other->borders[side_s1]);
210 if (delta < max_delta) {
211 butal->flags[side_s1] |= stitch;
212 butal_other->flags[side_s1] |= stitch_opp;
213 }
214 delta = fabsf(*butal->borders[side_s2] - *butal_other->borders[side_s2]);
215 if (delta < max_delta) {
216 butal->flags[side_s2] |= stitch;
217 butal_other->flags[side_s2] |= stitch_opp;
218 }
219 }
220 }
221 /* We assume two buttons can only share one side at most - for until
222 * we have spherical UI. */
223 return;
224 }
225 }
226 }
227}
228
251 const int side,
252 const int side_opp,
253 const int side_s1,
254 const int side_s2,
255 const int align,
256 const int align_opp,
257 const float co)
258{
259 ButAlign *butal_neighbor;
260
261 const int stitch_s1 = STITCH(side_s1);
262 const int stitch_s2 = STITCH(side_s2);
263
264 /* We have to check stitching flags on both sides of the stitching,
265 * since we only clear one of them flags to break any future loop on same 'columns/side' case.
266 * Also, if butal is spanning over several rows or columns of neighbors,
267 * it may have both of its stitching flags
268 * set, but would not be the case of its immediate neighbor! */
269 while ((butal->flags[side] & stitch_s1) && (butal = butal->neighbors[side_s1]) &&
270 (butal->flags[side] & stitch_s2))
271 {
272 butal_neighbor = butal->neighbors[side];
273
274 /* If we actually do have a neighbor, we directly set its values accordingly,
275 * and clear its matching 'dist' to prevent it being set again later... */
276 if (butal_neighbor) {
277 butal->but->drawflag |= align;
278 butal_neighbor->but->drawflag |= align_opp;
279 *butal_neighbor->borders[side_opp] = co;
280 butal_neighbor->dists[side_opp] = 0.0f;
281 }
282 /* See definition of BUT_ALIGN_STITCH_LEFT/TOP for reason of this... */
283 else if (side == LEFT) {
285 }
286 else if (side == TOP) {
288 }
289 *butal->borders[side] = co;
290 butal->dists[side] = 0.0f;
291 /* Clearing one of the 'flags pair' here is enough to prevent this loop running on
292 * the same column, side and direction again. */
293 butal->flags[side] &= ~stitch_s2;
294 }
295}
296
303static bool block_align_butal_cmp(const ButAlign &butal, const ButAlign &butal_other)
304{
305 /* Sort by align group. */
306 if (butal.but->alignnr != butal_other.but->alignnr) {
307 return butal.but->alignnr < butal_other.but->alignnr;
308 }
309
310 /* Sort vertically in descending order (first buttons have higher Y value than later ones). */
311 if (*butal.borders[TOP] != *butal_other.borders[TOP]) {
312 return *butal.borders[TOP] > *butal_other.borders[TOP];
313 }
314
315 /* Sort horizontally. */
316 if (*butal.borders[LEFT] != *butal_other.borders[LEFT]) {
317 return *butal.borders[LEFT] < *butal_other.borders[LEFT];
318 }
319 /* In very compressed layouts or overlapping layouts, UI can produce overlapping
320 * widgets which can have the same top-left corner, so do not assert here.
321 */
322 return false;
323}
324
325static void block_align_but_to_region(Button *but, const ARegion *region)
326{
327 rctf *rect = &but->rect;
328 const float but_width = BLI_rctf_size_x(rect);
329 const float but_height = BLI_rctf_size_y(rect);
330 const float outline_px = U.pixelsize; /* This may have to be made more variable. */
331
332 switch (but->drawflag & BUT_ALIGN) {
333 case BUT_ALIGN_TOP:
334 rect->ymax = region->winy + outline_px;
335 rect->ymin = but->rect.ymax - but_height;
336 break;
337 case BUT_ALIGN_DOWN:
338 rect->ymin = -outline_px;
339 rect->ymax = rect->ymin + but_height;
340 break;
341 case BUT_ALIGN_LEFT:
342 rect->xmin = -outline_px;
343 rect->xmax = rect->xmin + but_width;
344 break;
345 case BUT_ALIGN_RIGHT:
346 rect->xmax = region->winx + outline_px;
347 rect->xmin = rect->xmax - but_width;
348 break;
349 default:
350 /* Tabs may be shown in unaligned regions too, they just appear as regular buttons then. */
351 rect->ymin += UI_SCALE_FAC;
352 rect->ymax += UI_SCALE_FAC;
353 break;
354 }
355}
356
357void block_align_calc(Block *block, const ARegion *region)
358{
359
360 const int sides_to_ui_but_align_flags[4] = SIDE_TO_BUT_ALIGN;
361
362 Vector<ButAlign, 256> butal_array(block->buttons_ptrs.size());
363
364 int n = 0;
365 /* First loop: Initialize ButAlign data for each button and clear their align flag.
366 * Tabs get some special treatment here, they get aligned to region border. */
367 for (Button &but : block->buttons()) {
368 /* special case: tabs need to be aligned to a region border, drawflag tells which one */
369 if (but.type == ButtonType::Tab) {
370 block_align_but_to_region(&but, region);
371 }
372 else {
373 /* Clear old align flags. */
374 but.drawflag &= ~BUT_ALIGN_ALL;
375 }
376
377 if (but.alignnr == 0) {
378 continue;
379 }
380 ButAlign &butal = butal_array[n++];
381 butal = {};
382 butal.but = &but;
383 butal.borders[LEFT] = &but.rect.xmin;
384 butal.borders[RIGHT] = &but.rect.xmax;
385 butal.borders[DOWN] = &but.rect.ymin;
386 butal.borders[TOP] = &but.rect.ymax;
387 butal.dists = float4{FLT_MAX};
388 }
389 butal_array.resize(n);
390
391 if (butal_array.size() < 2) {
392 /* No need to go further if we have nothing to align... */
393 return;
394 }
395
396 /* This will give us ButAlign items regrouped by align group, vertical and horizontal location.
397 * Note that, given how buttons are defined in UI code,
398 * butal_array shall already be "nearly sorted"... */
399 std::ranges::sort(butal_array, block_align_butal_cmp);
400
401 /* Second loop: for each pair of buttons in the same align group,
402 * we compute their potential proximity. Note that each pair is checked only once, and that we
403 * break early in case we know all remaining pairs will always be too far away. */
404 for (const int i : butal_array.index_range()) {
405 ButAlign &butal = butal_array[i];
406 const short alignnr = butal.but->alignnr;
407
408 for (ButAlign &butal_other : butal_array.as_mutable_span().drop_front(i + 1)) {
409 const float max_delta = MAX_DELTA;
410
411 /* Since they are sorted, buttons after current butal can only be of same or higher
412 * group, and once they are not of same group, we know we can break this sub-loop and
413 * start checking with next butal. */
414 if (butal_other.but->alignnr != alignnr) {
415 break;
416 }
417
418 /* Since they are sorted vertically first, buttons after current butal can only be at
419 * same or lower height, and once they are lower than a given threshold, we know we can
420 * break this sub-loop and start checking with next butal. */
421 if ((*butal.borders[DOWN] - *butal_other.borders[TOP]) > max_delta) {
422 break;
423 }
424
425 block_align_proximity_compute(&butal, &butal_other);
426 }
427 }
428
429 /* Third loop: we have all our 'aligned' buttons as a 'map' in butal_array. We need to:
430 * - update their relevant coordinates to stitch them.
431 * - assign them valid flags.
432 */
433 for (ButAlign &butal : butal_array) {
434
435 for (int side = 0; side < TOTSIDES; side++) {
436 ButAlign *butal_other = butal.neighbors[side];
437
438 if (butal_other) {
439 const int side_opp = OPPOSITE(side);
440 const int side_s1 = SIDE1(side);
441 const int side_s2 = SIDE2(side);
442
443 const int align = sides_to_ui_but_align_flags[side];
444 const int align_opp = sides_to_ui_but_align_flags[side_opp];
445
446 float co;
447
448 butal.but->drawflag |= align;
449 butal_other->but->drawflag |= align_opp;
450 if (!IS_EQF(butal.dists[side], 0.0f)) {
451 float *delta = &butal.dists[side];
452
453 if (*butal.borders[side] < *butal_other->borders[side_opp]) {
454 *delta *= 0.5f;
455 }
456 else {
457 *delta *= -0.5f;
458 }
459 co = (*butal.borders[side] += *delta);
460
461 if (!IS_EQF(butal_other->dists[side_opp], 0.0f)) {
462 BLI_assert(butal_other->dists[side_opp] * 0.5f == fabsf(*delta));
463 *butal_other->borders[side_opp] = co;
464 butal_other->dists[side_opp] = 0.0f;
465 }
466 *delta = 0.0f;
467 }
468 else {
469 co = *butal.borders[side];
470 }
471
473 &butal, side, side_opp, side_s1, side_s2, align, align_opp, co);
475 &butal, side, side_opp, side_s2, side_s1, align, align_opp, co);
476 }
477 }
478 }
479}
480
481#undef SIDE_TO_BUT_ALIGN
482#undef SIDE1
483#undef OPPOSITE
484#undef SIDE2
485#undef IS_COLUMN
486#undef STITCH
487#undef MAX_DELTA
488
490{
491 const ARegion *align_region = (region->alignment & RGN_SPLIT_PREV && region->prev) ?
492 region->prev :
493 region;
494
495 switch (RGN_ALIGN_ENUM_FROM_MASK(align_region->alignment)) {
496 case RGN_ALIGN_TOP:
497 return BUT_ALIGN_DOWN;
498 case RGN_ALIGN_BOTTOM:
499 return BUT_ALIGN_TOP;
500 case RGN_ALIGN_LEFT:
501 return BUT_ALIGN_RIGHT;
502 case RGN_ALIGN_RIGHT:
503 return BUT_ALIGN_LEFT;
504 default:
505 break;
506 }
507
508 return 0;
509}
510
511} // namespace blender::ui
#define RGN_ALIGN_ENUM_FROM_MASK(align)
#define UI_SCALE_FAC
Read Blender memory allocation and freeing.
#define ELEM(...)
#define IS_EQF(a, b)
IndexRange index_range() const
void resize(const int64_t new_size)
MutableSpan< T > as_mutable_span()
#define FLT_MAX
#define FLT_MIN
#define IS_COLUMN(_s)
#define MAX_DELTA
#define SIDE_TO_BUT_ALIGN
#define SIDE2(_s)
#define STITCH(_s)
#define SIDE1(_s)
#define OPPOSITE(_s)
static void block_align_but_to_region(Button *but, const ARegion *region)
void block_align_calc(Block *block, const ARegion *region)
static bool block_align_butal_cmp(const ButAlign &butal, const ButAlign &butal_other)
static void block_align_proximity_compute(ButAlign *butal, ButAlign *butal_other)
bool button_can_align(const Button *but)
int button_align_opposite_to_area_align_get(const ARegion *region)
static void block_align_stitch_neighbors(ButAlign *butal, const int side, const int side_opp, const int side_s1, const int side_s2, const int align, const int align_opp, const float co)
UserDef U
Definition blender.cc:58
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
VecBase< int8_t, 4 > char4
VecBase< float, 4 > float4
bool BLI_rctf_is_empty(const struct rctf *rect)
BLI_INLINE float BLI_rctf_size_y(const struct rctf *rct)
Definition BLI_rect.hh:215
BLI_INLINE float BLI_rctf_size_x(const struct rctf *rct)
Definition BLI_rect.hh:211
MINLINE float max_ff(float a, float b)
#define fabsf
struct ARegion * prev
eRegion_Alignment alignment
Vector< std::unique_ptr< Button > > buttons_ptrs
std::ranges::transform_view< std::ranges::ref_view< const Vector< std::unique_ptr< Button > > >, Button &(*)(const std::unique_ptr< Button > &)> buttons() const
std::array< float *, 4 > borders