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