Blender V5.3
screen_geometry.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
12
13#include "BLI_listbase.hh"
14#include "BLI_math_vector_c.hh"
15#include "BLI_rect.hh"
16
17#include "BKE_screen.hh"
18
19#include "DNA_screen_types.h"
21
22#include "ED_screen.hh"
23
24#include "MEM_guardedalloc.h"
25
26#include "WM_api.hh"
27
28#include "screen_intern.hh"
29
30namespace blender {
31
33{
34 return area->v2->vec.y - area->v1->vec.y + 1;
35}
37{
38 return area->v4->vec.x - area->v1->vec.x + 1;
39}
40
42{
43 ScrVert *sv = MEM_new<ScrVert>("addscrvert");
44 sv->vec.x = x;
45 sv->vec.y = y;
46
47 BLI_addtail(&area_map->vertbase, sv);
48 return sv;
49}
50ScrVert *screen_geom_vertex_add(bScreen *screen, short x, short y)
51{
53}
54
56{
57 ScrEdge *se = MEM_new<ScrEdge>("addscredge");
58
59 se->v1 = v1;
60 se->v2 = v2;
61
62 BLI_addtail(&area_map->edgebase, se);
63 return se;
64}
69
71{
72 return (se->v1->vec.y == se->v2->vec.y);
73}
74
76 const ScrAreaMap *area_map, const rcti *bounds_rect, const int mx, const int my, int safety)
77{
78 CLAMP_MIN(safety, 2);
79
80 for (ScrEdge &se : area_map->edgebase) {
82 if ((se.v1->vec.y > bounds_rect->ymin) && (se.v1->vec.y < (bounds_rect->ymax - 1))) {
83 short min, max;
84 min = std::min(se.v1->vec.x, se.v2->vec.x);
85 max = std::max(se.v1->vec.x, se.v2->vec.x);
86
87 if (abs(my - se.v1->vec.y) <= safety && mx >= min && mx <= max) {
88 return &se;
89 }
90 }
91 }
92 else {
93 if ((se.v1->vec.x > bounds_rect->xmin) && (se.v1->vec.x < (bounds_rect->xmax - 1))) {
94 short min, max;
95 min = std::min(se.v1->vec.y, se.v2->vec.y);
96 max = std::max(se.v1->vec.y, se.v2->vec.y);
97
98 if (abs(mx - se.v1->vec.x) <= safety && my >= min && my <= max) {
99 return &se;
100 }
101 }
102 }
103 }
104
105 return nullptr;
106}
107
109 const bScreen *screen,
110 const int mx,
111 const int my)
112{
113 if (U.app_flag & USER_APP_LOCK_EDGE_RESIZE) {
114 return nullptr;
115 }
116
117 /* Use layout size (screen excluding global areas) for screen-layout area edges */
118 rcti screen_rect;
119 WM_window_screen_rect_calc(win, &screen_rect);
121 AREAMAP_FROM_SCREEN(screen), &screen_rect, mx, my, BORDERPADDING);
122
123 if (!se) {
124 /* Use entire window size (screen including global areas) for global area edges */
125 rcti win_rect;
126 WM_window_rect_calc(win, &win_rect);
128 &win->global_areas, &win_rect, mx, my, int(BORDERPADDING_GLOBAL));
129 }
130 return se;
131}
132
138 const bScreen *screen,
139 const rcti *screen_rect)
140{
141
142 const int screen_size_x = BLI_rcti_size_x(screen_rect);
143 const int screen_size_y = BLI_rcti_size_y(screen_rect);
144 bool needs_another_pass = false;
145
146 /* calculate size */
147 float min[2] = {20000.0f, 20000.0f};
148 float max[2] = {0.0f, 0.0f};
149
150 for (ScrVert &sv : screen->vertbase) {
151 const float fv[2] = {float(sv.vec.x), float(sv.vec.y)};
152 minmax_v2v2_v2(min, max, fv);
153 }
154
155 int screen_size_x_prev = (max[0] - min[0]) + 1;
156 int screen_size_y_prev = (max[1] - min[1]) + 1;
157
158 if (screen_size_x_prev != screen_size_x || screen_size_y_prev != screen_size_y) {
159 const float facx = (float(screen_size_x) - 1) / (float(screen_size_x_prev) - 1);
160 const float facy = (float(screen_size_y) - 1) / (float(screen_size_y_prev) - 1);
161
162 /* make sure it fits! */
163 for (ScrVert &sv : screen->vertbase) {
164 sv.vec.x = screen_rect->xmin + round_fl_to_short((sv.vec.x - min[0]) * facx);
165 CLAMP(sv.vec.x, screen_rect->xmin, screen_rect->xmax - 1);
166
167 sv.vec.y = screen_rect->ymin + round_fl_to_short((sv.vec.y - min[1]) * facy);
168 CLAMP(sv.vec.y, screen_rect->ymin, screen_rect->ymax - 1);
169 }
170
171 /* test for collapsed areas. This could happen in some blender version... */
172 /* ton: removed option now, it needs Context... */
173
174 if (facy > 1) {
175 /* Keep timeline small in video edit workspace. */
176 for (ScrArea &area : screen->areabase) {
177 const int border_width = int(ceil(float(U.border_width) * UI_SCALE_FAC));
178 int min = ED_area_headersize() + border_width;
179 if (area.v1->vec.y > screen_rect->ymin) {
180 min += border_width;
181 }
182 if (area.spacetype == SPACE_ACTION && area.v1->vec.y == screen_rect->ymin &&
183 screen_geom_area_height(&area) <= int(min * 1.5f))
184 {
185 ScrEdge *se = BKE_screen_find_edge(screen, area.v2, area.v3);
186 if (se) {
187 const int yval = area.v1->vec.y + min - 1;
188
190
191 /* all selected vertices get the right offset */
192 for (ScrVert &sv : screen->vertbase) {
193 /* if is a collapsed area */
194 if (!ELEM(&sv, area.v1, area.v4)) {
195 if (sv.flag) {
196 sv.vec.y = yval;
197 /* Changed size of a area. Run another pass to ensure everything still fits. */
198 needs_another_pass = true;
199 }
200 }
201 }
202 }
203 }
204 }
205 }
206
207 /* Make each window at least ED_area_headersize() high. This
208 * should be done whether we are increasing or decreasing the
209 * vertical size since this is called on file load, not just
210 * during resize operations. */
211 for (ScrArea &area : screen->areabase) {
212 const int border_width = int(ceil(float(U.border_width) * UI_SCALE_FAC));
213 int min = ED_area_headersize() + border_width + border_width - U.pixelsize;
214 if (area.v3->vec.y >= (screen_rect->ymax - 1)) {
215 /* Area aligned to top screen edge. */
216 min = ED_area_headersize() + border_width;
217 }
218 else if (area.v4->vec.y <= (screen_rect->ymin + 1)) {
219 /* Area aligned to bottom screen edge. */
220 min = ED_area_headersize() + border_width + 1;
221 }
222
223 const int height = screen_geom_area_height(&area);
224 if (height < min) {
225 /* lower edge */
226 ScrEdge *se = BKE_screen_find_edge(screen, area.v4, area.v1);
227 if (se && area.v1 != area.v2) {
228 const int yval = area.v2->vec.y - min;
229
231
232 /* all selected vertices get the right offset */
233 for (ScrVert &sv : screen->vertbase) {
234 /* if is not a collapsed area */
235 if (!ELEM(&sv, area.v2, area.v3)) {
236 if (sv.flag) {
237 sv.vec.y = yval;
238 /* Changed size of a area. Run another pass to ensure everything still fits. */
239 needs_another_pass = true;
240 }
241 }
242 }
243 }
244 }
245 }
246 }
247
248 return needs_another_pass;
249}
250
252{
253 rcti window_rect, screen_rect;
254 WM_window_rect_calc(win, &window_rect);
255 WM_window_screen_rect_calc(win, &screen_rect);
256
257 bool needs_another_pass;
258 int max_passes_left = 10; /* Avoids endless loop. Number is rather arbitrary. */
259 do {
260 needs_another_pass = screen_geom_vertices_scale_pass(win, screen, &screen_rect);
261 max_passes_left--;
262 } while (needs_another_pass && (max_passes_left > 0));
263
264 /* Global areas have a fixed size that only changes with the DPI.
265 * Here we ensure that exactly this size is set. */
266 for (ScrArea &area : win->global_areas.areabase) {
267 if (area.global->flag & GLOBAL_AREA_IS_HIDDEN) {
268 continue;
269 }
270
271 int height = ED_area_global_size_y(&area) - 1;
272
273 if (area.v1->vec.y > window_rect.ymin) {
274 height += U.pixelsize;
275 }
276 if (area.v2->vec.y < (window_rect.ymax - 1)) {
277 height += U.pixelsize;
278 }
279
280 /* width */
281 area.v1->vec.x = area.v2->vec.x = window_rect.xmin;
282 area.v3->vec.x = area.v4->vec.x = window_rect.xmax - 1;
283 /* height */
284 area.v1->vec.y = area.v4->vec.y = window_rect.ymin;
285 area.v2->vec.y = area.v3->vec.y = window_rect.ymax - 1;
286
287 switch (area.global->align) {
289 area.v1->vec.y = area.v4->vec.y = area.v2->vec.y - height;
290 break;
292 area.v2->vec.y = area.v3->vec.y = area.v1->vec.y + height;
293 break;
294 }
295 }
296}
297
299 const rcti *window_rect,
300 const eScreenAxis dir_axis,
301 float fac)
302{
303 const int cur_area_width = screen_geom_area_width(area);
304 const int cur_area_height = screen_geom_area_height(area);
305 const short area_min_x = AREAMINX * UI_SCALE_FAC;
306 const short area_min_y = ED_area_headersize();
307
308 /* area big enough? */
309 if (dir_axis == SCREEN_AXIS_V) {
310 if (cur_area_width <= 2 * area_min_x) {
311 return 0;
312 }
313 }
314 else if (dir_axis == SCREEN_AXIS_H) {
315 if (cur_area_height <= 2 * area_min_y) {
316 return 0;
317 }
318 }
319
320 /* to be sure */
321 CLAMP(fac, 0.0f, 1.0f);
322
323 if (dir_axis == SCREEN_AXIS_H) {
324 short y = area->v1->vec.y + round_fl_to_short(fac * cur_area_height);
325
326 int area_min = area_min_y;
327
328 if (area->v1->vec.y > window_rect->ymin) {
329 area_min += U.pixelsize;
330 }
331 if (area->v2->vec.y < (window_rect->ymax - 1)) {
332 area_min += U.pixelsize;
333 }
334
335 if (y - area->v1->vec.y < area_min) {
336 y = area->v1->vec.y + area_min;
337 }
338 else if (area->v2->vec.y - y < area_min) {
339 y = area->v2->vec.y - area_min;
340 }
341
342 return y;
343 }
344
345 short x = area->v1->vec.x + round_fl_to_short(fac * cur_area_width);
346
347 int area_min = area_min_x;
348
349 if (area->v1->vec.x > window_rect->xmin) {
350 area_min += U.pixelsize;
351 }
352 if (area->v4->vec.x < (window_rect->xmax - 1)) {
353 area_min += U.pixelsize;
354 }
355
356 if (x - area->v1->vec.x < area_min) {
357 x = area->v1->vec.x + area_min;
358 }
359 else if (area->v4->vec.x - x < area_min) {
360 x = area->v4->vec.x - area_min;
361 }
362
363 return x;
364}
365
367{
369
370 /* 'dir_axis' is the direction of EDGE */
371 eScreenAxis dir_axis;
372 if (edge->v1->vec.x == edge->v2->vec.x) {
373 dir_axis = SCREEN_AXIS_V;
374 }
375 else {
376 dir_axis = SCREEN_AXIS_H;
377 }
378
379 ED_screen_verts_iter(win, screen, sv)
380 {
381 sv->flag = 0;
382 }
383
384 edge->v1->flag = 1;
385 edge->v2->flag = 1;
386
387 /* select connected, only in the right direction */
388 bool oneselected = true;
389 while (oneselected) {
390 oneselected = false;
391 for (ScrEdge &se : screen->edgebase) {
392 if (se.v1->flag + se.v2->flag == 1) {
393 if (dir_axis == SCREEN_AXIS_H) {
394 if (se.v1->vec.y == se.v2->vec.y) {
395 se.v1->flag = se.v2->flag = 1;
396 oneselected = true;
397 }
398 }
399 else if (dir_axis == SCREEN_AXIS_V) {
400 if (se.v1->vec.x == se.v2->vec.x) {
401 se.v1->flag = se.v2->flag = 1;
402 oneselected = true;
403 }
404 }
405 }
406 }
407 }
408}
409
411{
413 bool can_extend = false;
414 const eScreenAxis dir_axis = (edge->v1->vec.x == edge->v2->vec.x) ? SCREEN_AXIS_V :
417
418 for (ScrEdge &se : screen->edgebase) {
419 if (se.v1->flag + se.v2->flag != 0) {
420 continue;
421 }
422 if (dir_axis == SCREEN_AXIS_H) {
423 for (ScrVert &v : screen->vertbase) {
424 if (v.flag && v.vec.x == se.v1->vec.x &&
425 (abs(v.vec.y - se.v1->vec.y) < EDGE_ALIGN_TOLERANCE ||
426 abs(v.vec.y - se.v2->vec.y) < EDGE_ALIGN_TOLERANCE))
427 {
428 se.v1->flag = se.v2->flag = 1;
429 can_extend = true;
430 break;
431 }
432 }
433 }
434 else if (dir_axis == SCREEN_AXIS_V) {
435 for (ScrVert &v : screen->vertbase) {
436 if (v.flag && v.vec.y == se.v1->vec.y &&
437 (abs(v.vec.x - se.v1->vec.x) < EDGE_ALIGN_TOLERANCE ||
438 abs(v.vec.x - se.v2->vec.x) < EDGE_ALIGN_TOLERANCE))
439 {
440 se.v1->flag = se.v2->flag = 1;
441 can_extend = true;
442 break;
443 }
444 }
445 }
446 }
447
448 ED_screen_verts_iter(win, screen, sv)
449 {
450 sv->flag = 0;
451 }
452
453 return can_extend;
454}
455
457{
459 const eScreenAxis dir_axis = (edge->v1->vec.x == edge->v2->vec.x) ? SCREEN_AXIS_V :
461 ED_screen_verts_iter(win, screen, sv)
462 {
463 sv->flag = 0;
464 }
465
466 for (ScrVert &v : screen->vertbase) {
467 if (dir_axis == SCREEN_AXIS_H) {
468 if (abs(v.vec.y - edge->v1->vec.y) < EDGE_ALIGN_TOLERANCE) {
469 v.flag = 1;
470 }
471 }
472 else if (dir_axis == SCREEN_AXIS_V) {
473 if (abs(v.vec.x - edge->v1->vec.x) < EDGE_ALIGN_TOLERANCE) {
474 v.flag = 1;
475 }
476 }
477 }
478}
479
481{
484 const eScreenAxis dir_axis = (edge->v1->vec.x == edge->v2->vec.x) ? SCREEN_AXIS_V :
486 /* Align the vertices if close. */
487 for (ScrVert &v : screen->vertbase) {
488 if (dir_axis == SCREEN_AXIS_V && abs(v.vec.x - edge->v2->vec.x) < EDGE_ALIGN_TOLERANCE) {
489 v.vec.x = edge->v2->vec.x;
490 }
491 else if (abs(v.vec.y - edge->v2->vec.y) < EDGE_ALIGN_TOLERANCE) {
492 v.vec.y = edge->v2->vec.y;
493 }
494 }
495
496 for (ScrVert &v : screen->vertbase) {
497 if (v.flag == 1 && v.newv == nullptr) { /* !!! */
498 ScrVert *v1 = v.next;
499 while (v1) {
500 if (v1->newv == nullptr) { /* !?! */
501 if (abs(v1->vec.x - v.vec.x) < EDGE_ALIGN_TOLERANCE &&
502 abs(v1->vec.y - v.vec.y) < EDGE_ALIGN_TOLERANCE)
503 {
504 v1->newv = &v;
505 }
506 }
507 v1 = v1->next;
508 }
509 }
510 }
511
512 /* Replace pointers in edges and faces. */
513 for (ScrEdge &se : screen->edgebase) {
514 if (se.v1->newv) {
515 se.v1 = se.v1->newv;
516 }
517 if (se.v2->newv) {
518 se.v2 = se.v2->newv;
519 }
520 }
521 for (ScrArea &area : screen->areabase) {
522 if (area.v1->newv) {
523 area.v1 = area.v1->newv;
524 }
525 if (area.v2->newv) {
526 area.v2 = area.v2->newv;
527 }
528 if (area.v3->newv) {
529 area.v3 = area.v3->newv;
530 }
531 if (area.v4->newv) {
532 area.v4 = area.v4->newv;
533 }
534 }
535
536 /* Remove. */
537 for (ScrVert &v : screen->vertbase.items_mutable()) {
538 if (v.newv) {
539 BLI_remlink(&screen->vertbase, &v);
540 MEM_delete(&v);
541 }
542 }
543
544 ED_screen_verts_iter(win, screen, sv)
545 {
546 sv->flag = 0;
547 }
548}
549
550} // namespace blender
#define AREAMAP_FROM_SCREEN(screen)
#define AREAMINX
#define UI_SCALE_FAC
#define ED_screen_verts_iter(win, screen, vert_name)
Definition ED_screen.hh:325
Read Blender memory allocation and freeing.
#define CLAMP(a, b, c)
#define ELEM(...)
#define CLAMP_MIN(a, b)
btScalar max
#define abs
#define ceil
BMesh Iterators.
UserDef U
Definition blender.cc:58
MINLINE short round_fl_to_short(float a)
ScrEdge * screen_geom_find_active_scredge(const wmWindow *win, const bScreen *screen, const int mx, const int my)
ScrVert * screen_geom_vertex_add_ex(ScrAreaMap *area_map, short x, short y)
@ GLOBAL_AREA_ALIGN_BOTTOM
bool screen_geom_edge_can_extend(const wmWindow *win, ScrEdge *edge)
int screen_geom_area_width(const ScrArea *area)
int ED_area_headersize()
Definition area.cc:4418
ATTR_WARN_UNUSED_RESULT const BMVert * v
ScrVert * screen_geom_vertex_add(bScreen *screen, short x, short y)
BLI_INLINE int BLI_rcti_size_y(const struct rcti *rct)
Definition BLI_rect.hh:207
void screen_geom_vertices_scale(const wmWindow *win, bScreen *screen)
Main screen-layout calculation function.
void WM_window_rect_calc(const wmWindow *win, rcti *r_rect)
short screen_geom_find_area_split_point(const ScrArea *area, const rcti *window_rect, const eScreenAxis dir_axis, float fac)
void minmax_v2v2_v2(float min[2], float max[2], const float vec[2])
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
ScrEdge * screen_geom_area_map_find_active_scredge(const ScrAreaMap *area_map, const rcti *bounds_rect, const int mx, const int my, int safety)
ScrEdge * BKE_screen_find_edge(const bScreen *screen, ScrVert *v1, ScrVert *v2)
Definition screen.cc:840
bool screen_geom_edge_is_horizontal(ScrEdge *se)
int ED_area_global_size_y(const ScrArea *area)
Definition area.cc:4429
ScrEdge * screen_geom_edge_add_ex(ScrAreaMap *area_map, ScrVert *v1, ScrVert *v2)
BLI_INLINE int BLI_rcti_size_x(const struct rcti *rct)
Definition BLI_rect.hh:203
void screen_geom_select_connected_edge(const wmWindow *win, ScrEdge *edge)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
ATTR_WARN_UNUSED_RESULT const BMVert * v2
void screen_geom_select_extended_edge(const wmWindow *win, ScrEdge *edge)
ScrEdge * screen_geom_edge_add(bScreen *screen, ScrVert *v1, ScrVert *v2)
int screen_geom_area_height(const ScrArea *area)
void WM_window_screen_rect_calc(const wmWindow *win, rcti *r_rect)
bScreen * WM_window_get_active_screen(const wmWindow *win)
@ USER_APP_LOCK_EDGE_RESIZE
static bool screen_geom_vertices_scale_pass(const wmWindow *win, const bScreen *screen, const rcti *screen_rect)
void screen_geom_edge_aligned_merge(const wmWindow *win, ScrEdge *edge)
#define BORDERPADDING
#define EDGE_ALIGN_TOLERANCE
#define BORDERPADDING_GLOBAL
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define min(a, b)
Definition sort.cc:38
ListBaseT< ScrArea > areabase
ListBaseT< ScrVert > vertbase
ListBaseT< ScrEdge > edgebase
ScrGlobalAreaData * global
struct ScrVert * newv
struct ScrVert * next
ListBaseT< struct ScrEdge > edgebase
ListBaseT< struct ScrVert > vertbase
ListBaseT< struct ScrArea > areabase