Blender V4.5
area_query.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
10
11#include "BLI_listbase.h"
12#include "BLI_math_base.h"
13#include "BLI_utildefines.h"
14
15#include "ED_screen.hh"
16
17#include "UI_interface.hh"
18#include "UI_view2d.hh"
19
20bool ED_region_overlap_isect_x(const ARegion *region, const int event_x)
21{
22 BLI_assert(region->overlap);
23 /* No contents, skip it. */
24 if (region->v2d.mask.xmin == region->v2d.mask.xmax) {
25 return false;
26 }
27 return BLI_rctf_isect_x(&region->v2d.tot,
28 UI_view2d_region_to_view_x(&region->v2d, event_x - region->winrct.xmin));
29}
30
31bool ED_region_overlap_isect_y(const ARegion *region, const int event_y)
32{
33 BLI_assert(region->overlap);
34 /* No contents, skip it. */
35 if (region->v2d.mask.ymin == region->v2d.mask.ymax) {
36 return false;
37 }
38 return BLI_rctf_isect_y(&region->v2d.tot,
39 UI_view2d_region_to_view_y(&region->v2d, event_y - region->winrct.ymin));
40}
41
42bool ED_region_overlap_isect_xy(const ARegion *region, const int event_xy[2])
43{
44 return (ED_region_overlap_isect_x(region, event_xy[0]) &&
45 ED_region_overlap_isect_y(region, event_xy[1]));
46}
47
48bool ED_region_overlap_isect_any_xy(const ScrArea *area, const int event_xy[2])
49{
50 LISTBASE_FOREACH (ARegion *, region, &area->regionbase) {
51 if (ED_region_is_overlap(area->spacetype, region->regiontype)) {
52 if (ED_region_overlap_isect_xy(region, event_xy)) {
53 return true;
54 }
55 }
56 }
57 return false;
58}
59
60bool ED_region_panel_category_gutter_calc_rect(const ARegion *region, rcti *r_region_gutter)
61{
62 *r_region_gutter = region->winrct;
63 if (UI_panel_category_is_visible(region)) {
64 const int category_tabs_width = round_fl_to_int(UI_view2d_scale_get_x(&region->v2d) *
66 const int alignment = RGN_ALIGN_ENUM_FROM_MASK(region->alignment);
67
68 if (alignment == RGN_ALIGN_LEFT) {
69 r_region_gutter->xmax = r_region_gutter->xmin + category_tabs_width;
70 }
71 else if (alignment == RGN_ALIGN_RIGHT) {
72 r_region_gutter->xmin = r_region_gutter->xmax - category_tabs_width;
73 }
74 else {
75 BLI_assert_msg(0, "Unsupported alignment");
76 }
77 return true;
78 }
79 return false;
80}
81
82bool ED_region_panel_category_gutter_isect_xy(const ARegion *region, const int event_xy[2])
83{
84 rcti region_gutter;
85 if (ED_region_panel_category_gutter_calc_rect(region, &region_gutter)) {
86 return BLI_rcti_isect_pt_v(&region_gutter, event_xy);
87 }
88 return false;
89}
90
92 const int event_x,
93 const int margin)
94{
95 BLI_assert(region->overlap);
96 /* No contents, skip it. */
97 if (region->v2d.mask.xmin == region->v2d.mask.xmax) {
98 return false;
99 }
100 int region_x = event_x - region->winrct.xmin;
101 return ((region->v2d.tot.xmin <= UI_view2d_region_to_view_x(&region->v2d, region_x + margin)) &&
102 (region->v2d.tot.xmax >= UI_view2d_region_to_view_x(&region->v2d, region_x - margin)));
103}
104
106 const int event_y,
107 const int margin)
108{
109 BLI_assert(region->overlap);
110 /* No contents, skip it. */
111 if (region->v2d.mask.ymin == region->v2d.mask.ymax) {
112 return false;
113 }
114 int region_y = event_y - region->winrct.ymin;
115 return ((region->v2d.tot.ymin <= UI_view2d_region_to_view_y(&region->v2d, region_y + margin)) &&
116 (region->v2d.tot.ymax >= UI_view2d_region_to_view_y(&region->v2d, region_y - margin)));
117}
118
120 const int event_xy[2],
121 const int margin)
122{
123 return (ED_region_overlap_isect_x_with_margin(region, event_xy[0], margin) &&
124 ED_region_overlap_isect_y_with_margin(region, event_xy[1], margin));
125}
126
127bool ED_region_contains_xy(const ARegion *region, const int event_xy[2])
128{
129 /* Only use the margin when inside the region. */
130 if (BLI_rcti_isect_pt_v(&region->winrct, event_xy)) {
131 if (region->overlap) {
132 const int overlap_margin = UI_REGION_OVERLAP_MARGIN;
133 /* Note the View2D.tot isn't reliable for headers with spacers otherwise
134 * we'd check #ED_region_overlap_isect_xy_with_margin for both bases. */
135 if (region->v2d.keeptot == V2D_KEEPTOT_STRICT) {
136 /* Header. */
137 rcti rect;
138 BLI_rcti_init_pt_radius(&rect, event_xy, overlap_margin);
139 if (UI_region_but_find_rect_over(region, &rect) == nullptr) {
140 return false;
141 }
142 }
143 else {
144 /* Side-bar & any other kind of overlapping region. */
145
146 const int alignment = RGN_ALIGN_ENUM_FROM_MASK(region->alignment);
147
148 /* Check alignment to avoid region tabs being clipped out
149 * by only clipping a single axis for aligned regions. */
150 if (ELEM(alignment, RGN_ALIGN_TOP, RGN_ALIGN_BOTTOM)) {
151 if (!ED_region_overlap_isect_x_with_margin(region, event_xy[0], overlap_margin)) {
152 return false;
153 }
154 }
155 else if (ELEM(alignment, RGN_ALIGN_LEFT, RGN_ALIGN_RIGHT)) {
156 if (ED_region_panel_category_gutter_isect_xy(region, event_xy)) {
157 /* pass */
158 }
159 else if (!ED_region_overlap_isect_y_with_margin(region, event_xy[1], overlap_margin)) {
160 return false;
161 }
162 }
163 else {
164 /* No panel categories for horizontal regions currently. */
165 if (!ED_region_overlap_isect_xy_with_margin(region, event_xy, overlap_margin)) {
166 return false;
167 }
168 }
169 }
170 }
171 return true;
172 }
173 return false;
174}
175
177 const int regiontype,
178 const int event_xy[2])
179{
180 if (!area) {
181 return nullptr;
182 }
183
184 /* Check overlapped regions first. */
185 LISTBASE_FOREACH (ARegion *, region, &area->regionbase) {
186 if (!region->overlap) {
187 continue;
188 }
189 if (ELEM(regiontype, RGN_TYPE_ANY, region->regiontype)) {
190 if (ED_region_contains_xy(region, event_xy)) {
191 return region;
192 }
193 }
194 }
195 /* Now non-overlapping ones. */
196 LISTBASE_FOREACH (ARegion *, region, &area->regionbase) {
197 if (region->overlap) {
198 continue;
199 }
200 if (ELEM(regiontype, RGN_TYPE_ANY, region->regiontype)) {
201 if (ED_region_contains_xy(region, event_xy)) {
202 return region;
203 }
204 }
205 }
206
207 return nullptr;
208}
#define BLI_assert(a)
Definition BLI_assert.h:46
#define BLI_assert_msg(a, msg)
Definition BLI_assert.h:53
#define LISTBASE_FOREACH(type, var, list)
MINLINE int round_fl_to_int(float a)
bool BLI_rcti_isect_pt_v(const struct rcti *rect, const int xy[2])
bool BLI_rctf_isect_x(const rctf *rect, float x)
Definition rct.cc:93
void BLI_rcti_init_pt_radius(struct rcti *rect, const int xy[2], int size)
Definition rct.cc:466
bool BLI_rctf_isect_y(const rctf *rect, float y)
Definition rct.cc:104
#define ELEM(...)
#define RGN_ALIGN_ENUM_FROM_MASK(align)
#define RGN_TYPE_ANY
@ RGN_ALIGN_BOTTOM
@ RGN_ALIGN_LEFT
@ RGN_ALIGN_TOP
@ RGN_ALIGN_RIGHT
@ V2D_KEEPTOT_STRICT
bool ED_region_is_overlap(int spacetype, int regiontype)
Definition area.cc:1490
#define UI_REGION_OVERLAP_MARGIN
uiBut * UI_region_but_find_rect_over(const ARegion *region, const rcti *rect_px)
bool UI_panel_category_is_visible(const ARegion *region)
#define UI_PANEL_CATEGORY_MARGIN_WIDTH
float UI_view2d_region_to_view_y(const View2D *v2d, float y)
Definition view2d.cc:1661
float UI_view2d_region_to_view_x(const View2D *v2d, float x)
Definition view2d.cc:1656
float UI_view2d_scale_get_x(const View2D *v2d)
Definition view2d.cc:1920
bool ED_region_panel_category_gutter_isect_xy(const ARegion *region, const int event_xy[2])
Definition area_query.cc:82
ARegion * ED_area_find_region_xy_visual(const ScrArea *area, const int regiontype, const int event_xy[2])
bool ED_region_overlap_isect_any_xy(const ScrArea *area, const int event_xy[2])
Definition area_query.cc:48
bool ED_region_contains_xy(const ARegion *region, const int event_xy[2])
bool ED_region_overlap_isect_x_with_margin(const ARegion *region, const int event_x, const int margin)
Definition area_query.cc:91
bool ED_region_overlap_isect_y(const ARegion *region, const int event_y)
Definition area_query.cc:31
bool ED_region_overlap_isect_xy(const ARegion *region, const int event_xy[2])
Definition area_query.cc:42
bool ED_region_panel_category_gutter_calc_rect(const ARegion *region, rcti *r_region_gutter)
Definition area_query.cc:60
bool ED_region_overlap_isect_x(const ARegion *region, const int event_x)
Definition area_query.cc:20
bool ED_region_overlap_isect_y_with_margin(const ARegion *region, const int event_y, const int margin)
bool ED_region_overlap_isect_xy_with_margin(const ARegion *region, const int event_xy[2], const int margin)
ListBase regionbase
short keeptot
float xmax
float xmin
float ymax
float ymin
int ymin
int ymax
int xmin
int xmax