Blender V5.3
view2d_edge_pan.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2021 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BKE_context.hh"
10
11#include "BLI_math_base_c.hh"
12#include "BLI_rect.hh"
13#include "BLI_time.hh"
14
15#include "ED_screen.hh"
16
17#include "RNA_access.hh"
18#include "RNA_define.hh"
19
20#include "UI_view2d.hh"
21
22#include "WM_api.hh"
23#include "WM_types.hh"
24
25#include "view2d_intern.hh"
26
27namespace blender::ui {
28
29/* -------------------------------------------------------------------- */
32
34{
35 ARegion *region = CTX_wm_region(C);
36
37 /* Check if there's a region in context to work with. */
38 if (region == nullptr) {
39 return false;
40 }
41
42 View2D *v2d = &region->v2d;
43
44 /* Some spaces (e.g. image) overwrite `v2d` using their own view state; they never initialize. */
45 if ((v2d->flag & V2D_IS_INIT) == 0) {
46 return false;
47 }
48
49 /* Check that 2d-view can pan. */
50 if ((v2d->keepofs & V2D_LOCKOFS_X) && (v2d->keepofs & V2D_LOCKOFS_Y)) {
51 return false;
52 }
53
54 /* View can pan. */
55 return true;
56}
57
60 float inside_pad,
61 float outside_pad,
62 float speed_ramp,
63 float max_speed,
64 float delay,
65 float zoom_influence)
66{
67 if (!view2d_edge_pan_poll(C)) {
68 return;
69 }
70
71 /* Set pointers to owners. */
72 vpd->screen = CTX_wm_screen(C);
73 vpd->area = CTX_wm_area(C);
74 vpd->region = CTX_wm_region(C);
75 vpd->v2d = &vpd->region->v2d;
77
78 BLI_assert(speed_ramp > 0.0f);
79 vpd->inside_pad = inside_pad;
80 vpd->outside_pad = outside_pad;
81 vpd->speed_ramp = speed_ramp;
82 vpd->max_speed = max_speed;
83 vpd->delay = delay;
84 vpd->zoom_influence = zoom_influence;
85
86 vpd->enabled = false;
87
88 /* Calculate translation factor, based on size of view. */
89 const float winx = float(BLI_rcti_size_x(&vpd->region->winrct) + 1);
90 const float winy = float(BLI_rcti_size_y(&vpd->region->winrct) + 1);
91 vpd->facx = BLI_rctf_size_x(&vpd->v2d->cur) / winx;
92 vpd->facy = BLI_rctf_size_y(&vpd->v2d->cur) / winy;
93
95}
96
98 View2DEdgePanData *vpd, float xmin, float xmax, float ymin, float ymax)
99{
100 BLI_rctf_init(&vpd->limit, xmin, xmax, ymin, ymax);
101}
102
110
116 int pan_dir_x,
117 int pan_dir_y,
118 const double current_time)
119{
120 if (pan_dir_x == 0) {
121 vpd->edge_pan_start_time_x = 0.0;
122 }
123 else if (vpd->edge_pan_start_time_x == 0.0) {
124 vpd->edge_pan_start_time_x = current_time;
125 }
126 if (pan_dir_y == 0) {
127 vpd->edge_pan_start_time_y = 0.0;
128 }
129 else if (vpd->edge_pan_start_time_y == 0.0) {
130 vpd->edge_pan_start_time_y = current_time;
131 }
132}
133
140static float smootherstep(const float domain_max, float x)
141{
142 x = clamp_f(x / domain_max, 0.0, 1.0);
143 return x * x * x * (x * (x * 6.0 - 15.0) + 10.0);
144}
145
147 int event_loc,
148 bool x_dir,
149 const double current_time)
150{
151 ARegion *region = vpd->region;
152
153 /* Find the distance from the start of the drag zone. */
154 const int pad = vpd->inside_pad * U.widget_unit;
155 const int min = (x_dir ? region->winrct.xmin : region->winrct.ymin) + pad;
156 const int max = (x_dir ? region->winrct.xmax : region->winrct.ymax) - pad;
157 int distance = 0.0;
158 if (event_loc > max) {
159 distance = event_loc - max;
160 }
161 else if (event_loc < min) {
162 distance = min - event_loc;
163 }
164 else {
165 BLI_assert_msg(0, "Calculating speed outside of pan zones");
166 return 0.0f;
167 }
168 float distance_factor = distance / (vpd->speed_ramp * U.widget_unit);
169 CLAMP(distance_factor, 0.0f, 1.0f);
170
171 /* Apply a fade in to the speed based on a start time delay. */
172 const double start_time = x_dir ? vpd->edge_pan_start_time_x : vpd->edge_pan_start_time_y;
173 const float delay_factor = vpd->delay > 0.01f ?
174 smootherstep(vpd->delay, float(current_time - start_time)) :
175 1.0f;
176
177 /* Zoom factor increases speed when zooming in and decreases speed when zooming out. */
178 const float zoomx = float(BLI_rcti_size_x(&region->winrct) + 1) /
179 BLI_rctf_size_x(&region->v2d.cur);
180 const float zoom_factor = 1.0f + std::clamp(vpd->zoom_influence, 0.0f, 1.0f) * (zoomx - 1.0f);
181
182 return distance_factor * delay_factor * zoom_factor * vpd->max_speed * U.widget_unit *
184}
185
186static void edge_pan_apply_delta(bContext *C, View2DEdgePanData *vpd, float dx, float dy)
187{
188 View2D *v2d = vpd->v2d;
189 if (!v2d) {
190 return;
191 }
192
193 /* Calculate amount to move view by. */
194 dx *= vpd->facx;
195 dy *= vpd->facy;
196
197 /* Only move view on an axis if change is allowed. */
198 if ((v2d->keepofs & V2D_LOCKOFS_X) == 0) {
199 v2d->cur.xmin += dx;
200 v2d->cur.xmax += dx;
201 }
202 if ((v2d->keepofs & V2D_LOCKOFS_Y) == 0) {
203 v2d->cur.ymin += dy;
204 v2d->cur.ymax += dy;
205 }
206
207 if (dx != 0.0f || dy != 0.0f) {
208 /* Inform v2d about changes after this operation. */
210
211 /* Don't rebuild full tree in outliner, since we're just changing our view. */
213
214 /* Request updates to be done. */
216
217 view2d_sync(vpd->screen, vpd->area, v2d, V2D_LOCK_COPY);
218 }
219}
220
222{
223 ARegion *region = vpd->region;
224
225 rcti inside_rect, outside_rect;
226 inside_rect = region->winrct;
227 outside_rect = region->winrct;
228 BLI_rcti_pad(&inside_rect, -vpd->inside_pad * U.widget_unit, -vpd->inside_pad * U.widget_unit);
229 BLI_rcti_pad(&outside_rect, vpd->outside_pad * U.widget_unit, vpd->outside_pad * U.widget_unit);
230
231 /* Check if we can actually start the edge pan (e.g. adding nodes outside the view will start
232 * disabled). */
233 if (BLI_rcti_isect_pt_v(&inside_rect, xy)) {
234 /* We are inside once, can start. */
235 vpd->enabled = true;
236 }
237
238 const rctf *cur = &vpd->v2d->cur;
239 const rctf *limit = &vpd->limit;
240
241 int pan_dir_x = 0;
242 int pan_dir_y = 0;
243 if (vpd->enabled && ((vpd->outside_pad == 0) || BLI_rcti_isect_pt_v(&outside_rect, xy))) {
244 /* Find whether the mouse is beyond X and Y edges. */
245 if (xy[0] > inside_rect.xmax && cur->xmax < limit->xmax) {
246 pan_dir_x = 1;
247 }
248 else if (xy[0] < inside_rect.xmin && cur->xmin > limit->xmin) {
249 pan_dir_x = -1;
250 }
251 if (xy[1] > inside_rect.ymax && cur->ymax < limit->ymax) {
252 pan_dir_y = 1;
253 }
254 else if (xy[1] < inside_rect.ymin && cur->ymin > limit->ymin) {
255 pan_dir_y = -1;
256 }
257 }
258
259 const double current_time = BLI_time_now_seconds();
260 edge_pan_manage_delay_timers(vpd, pan_dir_x, pan_dir_y, current_time);
261
262 /* Calculate the delta since the last time the operator was called. */
263 const float dtime = float(current_time - vpd->edge_pan_last_time);
264 float dx = 0.0f, dy = 0.0f;
265 if (pan_dir_x != 0) {
266 const float speed = view2d_edge_pan_speed(vpd, xy[0], true, current_time);
267 dx = dtime * speed * float(pan_dir_x);
268 }
269 if (pan_dir_y != 0) {
270 const float speed = view2d_edge_pan_speed(vpd, xy[1], false, current_time);
271 dy = dtime * speed * float(pan_dir_y);
272 }
273 vpd->edge_pan_last_time = current_time;
274
275 /* Pan, clamping inside the regions total bounds. */
276 edge_pan_apply_delta(C, vpd, dx, dy);
277}
278
280{
281 /* Only mouse-move events matter here, ignore others. */
282 if (event->type != MOUSEMOVE) {
283 return;
284 }
285
286 view2d_edge_pan_apply(C, vpd, event->xy);
287}
288
290{
291 View2D *v2d = vpd->v2d;
292 if (!v2d) {
293 return;
294 }
295
296 v2d->cur = vpd->initial_rect;
297
298 /* Inform v2d about changes after this operation. */
300
301 /* Don't rebuild full tree in outliner, since we're just changing our view. */
303
304 /* Request updates to be done. */
306
307 view2d_sync(vpd->screen, vpd->area, v2d, V2D_LOCK_COPY);
308}
309
311{
312 /* Default values for edge panning operators. */
314 /*inside_pad*/ 1.0f,
315 /*outside_pad*/ 0.0f,
316 /*speed_ramp*/ 1.0f,
317 /*max_speed*/ 500.0f,
318 /*delay*/ 1.0f,
319 /*zoom_influence*/ 0.0f);
320}
321
323 float inside_pad,
324 float outside_pad,
325 float speed_ramp,
326 float max_speed,
327 float delay,
328 float zoom_influence)
329{
331 ot->srna,
332 "inside_padding",
333 inside_pad,
334 0.0f,
335 100.0f,
336 "Inside Padding",
337 "Inside distance in UI units from the edge of the region within which to start panning",
338 0.0f,
339 100.0f);
341 ot->srna,
342 "outside_padding",
343 outside_pad,
344 0.0f,
345 100.0f,
346 "Outside Padding",
347 "Outside distance in UI units from the edge of the region at which to stop panning",
348 0.0f,
349 100.0f);
351 "speed_ramp",
352 speed_ramp,
353 0.0f,
354 100.0f,
355 "Speed Ramp",
356 "Width of the zone in UI units where speed increases with distance from the edge",
357 0.0f,
358 100.0f);
360 "max_speed",
361 max_speed,
362 0.0f,
363 10000.0f,
364 "Max Speed",
365 "Maximum speed in UI units per second",
366 0.0f,
367 10000.0f);
369 "delay",
370 delay,
371 0.0f,
372 10.0f,
373 "Delay",
374 "Delay in seconds before maximum speed is reached",
375 0.0f,
376 10.0f);
378 "zoom_influence",
379 zoom_influence,
380 0.0f,
381 1.0f,
382 "Zoom Influence",
383 "Influence of the zoom factor on scroll speed",
384 0.0f,
385 1.0f);
386}
387
389{
391 vpd,
392 RNA_float_get(op->ptr, "inside_padding"),
393 RNA_float_get(op->ptr, "outside_padding"),
394 RNA_float_get(op->ptr, "speed_ramp"),
395 RNA_float_get(op->ptr, "max_speed"),
396 RNA_float_get(op->ptr, "delay"),
397 RNA_float_get(op->ptr, "zoom_influence"));
398}
399
401
402} // namespace blender::ui
Platform independent time functions.
#define UI_SCALE_FAC
#define C
Definition RandGen.cpp:29
#define V2D_LOCK_COPY
Definition UI_view2d.hh:110
int pad[32 - sizeof(int)]
#define CLAMP(a, b, c)
#define BLI_assert_msg(a, msg)
btScalar max
#define FLT_MAX
float distance(float, float) RET
static void edge_pan_apply_delta(bContext *C, View2DEdgePanData *vpd, float dx, float dy)
void view2d_edge_pan_apply(bContext *C, View2DEdgePanData *vpd, const int xy[2]) ATTR_NONNULL(1
void view2d_edge_pan_operator_properties_ex(wmOperatorType *ot, float inside_pad, float outside_pad, float speed_ramp, float max_speed, float delay, float zoom_influence)
void view2d_edge_pan_operator_init(bContext *C, View2DEdgePanData *vpd, wmOperator *op)
static void edge_pan_manage_delay_timers(View2DEdgePanData *vpd, int pan_dir_x, int pan_dir_y, const double current_time)
void view2d_edge_pan_cancel(bContext *C, View2DEdgePanData *vpd)
static float smootherstep(const float domain_max, float x)
void view2d_edge_pan_reset(View2DEdgePanData *vpd)
static float view2d_edge_pan_speed(View2DEdgePanData *vpd, int event_loc, bool x_dir, const double current_time)
void view2d_sync(bScreen *screen, ScrArea *area, View2D *v2dcur, int flag)
Definition view2d.cc:850
void view2d_curRect_changed(const bContext *C, View2D *v2d)
Definition view2d.cc:815
void view2d_edge_pan_set_limits(View2DEdgePanData *vpd, float xmin, float xmax, float ymin, float ymax)
void view2d_edge_pan_init(bContext *C, View2DEdgePanData *vpd, float inside_pad, float outside_pad, float speed_ramp, float max_speed, float delay, float zoom_influence)
void void view2d_edge_pan_apply_event(bContext *C, View2DEdgePanData *vpd, const wmEvent *event)
void view2d_edge_pan_operator_properties(wmOperatorType *ot)
bool view2d_edge_pan_poll(bContext *C)
UserDef U
Definition blender.cc:58
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
ScrArea * CTX_wm_area(const bContext *C)
wmWindow * CTX_wm_window(const bContext *C)
BLI_INLINE int BLI_rcti_size_y(const struct rcti *rct)
Definition BLI_rect.hh:207
BLI_INLINE float BLI_rctf_size_y(const struct rctf *rct)
Definition BLI_rect.hh:215
BLI_INLINE int BLI_rcti_size_x(const struct rcti *rct)
Definition BLI_rect.hh:203
BLI_INLINE float BLI_rctf_size_x(const struct rctf *rct)
Definition BLI_rect.hh:211
double BLI_time_now_seconds()
Definition time.cc:30
bScreen * CTX_wm_screen(const bContext *C)
bool BLI_rcti_isect_pt_v(const struct rcti *rect, const int xy[2])
void BLI_rcti_pad(struct rcti *rect, int pad_x, int pad_y)
Definition rct.cc:643
void WM_event_add_mousemove(wmWindow *win)
PropertyRNA * RNA_def_float(StructOrFunctionRNA *cont_, const char *identifier, const float default_value, const float hardmin, const float hardmax, const char *ui_name, const char *ui_description, const float softmin, const float softmax)
ARegion * CTX_wm_region(const bContext *C)
void ED_region_tag_redraw_no_rebuild(ARegion *region)
Definition area.cc:727
float RNA_float_get(PointerRNA *ptr, const char *name)
void BLI_rctf_init(struct rctf *rect, float xmin, float xmax, float ymin, float ymax)
Definition rct.cc:418
MINLINE float clamp_f(float value, float min, float max)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define min(a, b)
Definition sort.cc:38
eView2D_KeepOfs keepofs
wmEventType type
Definition WM_types.hh:817