Blender V4.5
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_rect.h"
12#include "BLI_time.h"
13
14#include "ED_screen.hh"
15
16#include "RNA_access.hh"
17#include "RNA_define.hh"
18
19#include "UI_view2d.hh"
20
21#include "WM_api.hh"
22#include "WM_types.hh"
23
24#include "view2d_intern.hh"
25
26/* -------------------------------------------------------------------- */
29
31{
32 ARegion *region = CTX_wm_region(C);
33
34 /* Check if there's a region in context to work with. */
35 if (region == nullptr) {
36 return false;
37 }
38
39 View2D *v2d = &region->v2d;
40
41 /* Check that 2d-view can pan. */
42 if ((v2d->keepofs & V2D_LOCKOFS_X) && (v2d->keepofs & V2D_LOCKOFS_Y)) {
43 return false;
44 }
45
46 /* View can pan. */
47 return true;
48}
49
52 float inside_pad,
53 float outside_pad,
54 float speed_ramp,
55 float max_speed,
56 float delay,
57 float zoom_influence)
58{
59 if (!view2d_edge_pan_poll(C)) {
60 return;
61 }
62
63 /* Set pointers to owners. */
64 vpd->screen = CTX_wm_screen(C);
65 vpd->area = CTX_wm_area(C);
66 vpd->region = CTX_wm_region(C);
67 vpd->v2d = &vpd->region->v2d;
69
70 BLI_assert(speed_ramp > 0.0f);
71 vpd->inside_pad = inside_pad;
72 vpd->outside_pad = outside_pad;
73 vpd->speed_ramp = speed_ramp;
74 vpd->max_speed = max_speed;
75 vpd->delay = delay;
76 vpd->zoom_influence = zoom_influence;
77
78 vpd->enabled = false;
79
80 /* Calculate translation factor, based on size of view. */
81 const float winx = float(BLI_rcti_size_x(&vpd->region->winrct) + 1);
82 const float winy = float(BLI_rcti_size_y(&vpd->region->winrct) + 1);
83 vpd->facx = BLI_rctf_size_x(&vpd->v2d->cur) / winx;
84 vpd->facy = BLI_rctf_size_y(&vpd->v2d->cur) / winy;
85
87}
88
90 View2DEdgePanData *vpd, float xmin, float xmax, float ymin, float ymax)
91{
92 BLI_rctf_init(&vpd->limit, xmin, xmax, ymin, ymax);
93}
94
102
108 int pan_dir_x,
109 int pan_dir_y,
110 const double current_time)
111{
112 if (pan_dir_x == 0) {
113 vpd->edge_pan_start_time_x = 0.0;
114 }
115 else if (vpd->edge_pan_start_time_x == 0.0) {
116 vpd->edge_pan_start_time_x = current_time;
117 }
118 if (pan_dir_y == 0) {
119 vpd->edge_pan_start_time_y = 0.0;
120 }
121 else if (vpd->edge_pan_start_time_y == 0.0) {
122 vpd->edge_pan_start_time_y = current_time;
123 }
124}
125
132static float smootherstep(const float domain_max, float x)
133{
134 x = clamp_f(x / domain_max, 0.0, 1.0);
135 return x * x * x * (x * (x * 6.0 - 15.0) + 10.0);
136}
137
139 int event_loc,
140 bool x_dir,
141 const double current_time)
142{
143 ARegion *region = vpd->region;
144
145 /* Find the distance from the start of the drag zone. */
146 const int pad = vpd->inside_pad * U.widget_unit;
147 const int min = (x_dir ? region->winrct.xmin : region->winrct.ymin) + pad;
148 const int max = (x_dir ? region->winrct.xmax : region->winrct.ymax) - pad;
149 int distance = 0.0;
150 if (event_loc > max) {
151 distance = event_loc - max;
152 }
153 else if (event_loc < min) {
154 distance = min - event_loc;
155 }
156 else {
157 BLI_assert_msg(0, "Calculating speed outside of pan zones");
158 return 0.0f;
159 }
160 float distance_factor = distance / (vpd->speed_ramp * U.widget_unit);
161 CLAMP(distance_factor, 0.0f, 1.0f);
162
163 /* Apply a fade in to the speed based on a start time delay. */
164 const double start_time = x_dir ? vpd->edge_pan_start_time_x : vpd->edge_pan_start_time_y;
165 const float delay_factor = vpd->delay > 0.01f ?
166 smootherstep(vpd->delay, float(current_time - start_time)) :
167 1.0f;
168
169 /* Zoom factor increases speed when zooming in and decreases speed when zooming out. */
170 const float zoomx = float(BLI_rcti_size_x(&region->winrct) + 1) /
171 BLI_rctf_size_x(&region->v2d.cur);
172 const float zoom_factor = 1.0f + std::clamp(vpd->zoom_influence, 0.0f, 1.0f) * (zoomx - 1.0f);
173
174 return distance_factor * delay_factor * zoom_factor * vpd->max_speed * U.widget_unit *
175 float(UI_SCALE_FAC);
176}
177
178static void edge_pan_apply_delta(bContext *C, View2DEdgePanData *vpd, float dx, float dy)
179{
180 View2D *v2d = vpd->v2d;
181 if (!v2d) {
182 return;
183 }
184
185 /* Calculate amount to move view by. */
186 dx *= vpd->facx;
187 dy *= vpd->facy;
188
189 /* Only move view on an axis if change is allowed. */
190 if ((v2d->keepofs & V2D_LOCKOFS_X) == 0) {
191 v2d->cur.xmin += dx;
192 v2d->cur.xmax += dx;
193 }
194 if ((v2d->keepofs & V2D_LOCKOFS_Y) == 0) {
195 v2d->cur.ymin += dy;
196 v2d->cur.ymax += dy;
197 }
198
199 if (dx != 0.0f || dy != 0.0f) {
200 /* Inform v2d about changes after this operation. */
202
203 /* Don't rebuild full tree in outliner, since we're just changing our view. */
205
206 /* Request updates to be done. */
208
209 UI_view2d_sync(vpd->screen, vpd->area, v2d, V2D_LOCK_COPY);
210 }
211}
212
214{
215 ARegion *region = vpd->region;
216
217 rcti inside_rect, outside_rect;
218 inside_rect = region->winrct;
219 outside_rect = region->winrct;
220 BLI_rcti_pad(&inside_rect, -vpd->inside_pad * U.widget_unit, -vpd->inside_pad * U.widget_unit);
221 BLI_rcti_pad(&outside_rect, vpd->outside_pad * U.widget_unit, vpd->outside_pad * U.widget_unit);
222
223 /* Check if we can actually start the edge pan (e.g. adding nodes outside the view will start
224 * disabled). */
225 if (BLI_rcti_isect_pt_v(&inside_rect, xy)) {
226 /* We are inside once, can start. */
227 vpd->enabled = true;
228 }
229
230 const rctf *cur = &vpd->v2d->cur;
231 const rctf *limit = &vpd->limit;
232
233 int pan_dir_x = 0;
234 int pan_dir_y = 0;
235 if (vpd->enabled && ((vpd->outside_pad == 0) || BLI_rcti_isect_pt_v(&outside_rect, xy))) {
236 /* Find whether the mouse is beyond X and Y edges. */
237 if (xy[0] > inside_rect.xmax && cur->xmax < limit->xmax) {
238 pan_dir_x = 1;
239 }
240 else if (xy[0] < inside_rect.xmin && cur->xmin > limit->xmin) {
241 pan_dir_x = -1;
242 }
243 if (xy[1] > inside_rect.ymax && cur->ymax < limit->ymax) {
244 pan_dir_y = 1;
245 }
246 else if (xy[1] < inside_rect.ymin && cur->ymin > limit->ymin) {
247 pan_dir_y = -1;
248 }
249 }
250
251 const double current_time = BLI_time_now_seconds();
252 edge_pan_manage_delay_timers(vpd, pan_dir_x, pan_dir_y, current_time);
253
254 /* Calculate the delta since the last time the operator was called. */
255 const float dtime = float(current_time - vpd->edge_pan_last_time);
256 float dx = 0.0f, dy = 0.0f;
257 if (pan_dir_x != 0) {
258 const float speed = edge_pan_speed(vpd, xy[0], true, current_time);
259 dx = dtime * speed * float(pan_dir_x);
260 }
261 if (pan_dir_y != 0) {
262 const float speed = edge_pan_speed(vpd, xy[1], false, current_time);
263 dy = dtime * speed * float(pan_dir_y);
264 }
265 vpd->edge_pan_last_time = current_time;
266
267 /* Pan, clamping inside the regions total bounds. */
268 edge_pan_apply_delta(C, vpd, dx, dy);
269}
270
272{
273 /* Only mouse-move events matter here, ignore others. */
274 if (event->type != MOUSEMOVE) {
275 return;
276 }
277
278 UI_view2d_edge_pan_apply(C, vpd, event->xy);
279}
280
282{
283 View2D *v2d = vpd->v2d;
284 if (!v2d) {
285 return;
286 }
287
288 v2d->cur = vpd->initial_rect;
289
290 /* Inform v2d about changes after this operation. */
292
293 /* Don't rebuild full tree in outliner, since we're just changing our view. */
295
296 /* Request updates to be done. */
298
299 UI_view2d_sync(vpd->screen, vpd->area, v2d, V2D_LOCK_COPY);
300}
301
303{
304 /* Default values for edge panning operators. */
306 /*inside_pad*/ 1.0f,
307 /*outside_pad*/ 0.0f,
308 /*speed_ramp*/ 1.0f,
309 /*max_speed*/ 500.0f,
310 /*delay*/ 1.0f,
311 /*zoom_influence*/ 0.0f);
312}
313
315 float inside_pad,
316 float outside_pad,
317 float speed_ramp,
318 float max_speed,
319 float delay,
320 float zoom_influence)
321{
323 ot->srna,
324 "inside_padding",
325 inside_pad,
326 0.0f,
327 100.0f,
328 "Inside Padding",
329 "Inside distance in UI units from the edge of the region within which to start panning",
330 0.0f,
331 100.0f);
333 ot->srna,
334 "outside_padding",
335 outside_pad,
336 0.0f,
337 100.0f,
338 "Outside Padding",
339 "Outside distance in UI units from the edge of the region at which to stop panning",
340 0.0f,
341 100.0f);
342 RNA_def_float(ot->srna,
343 "speed_ramp",
344 speed_ramp,
345 0.0f,
346 100.0f,
347 "Speed Ramp",
348 "Width of the zone in UI units where speed increases with distance from the edge",
349 0.0f,
350 100.0f);
351 RNA_def_float(ot->srna,
352 "max_speed",
353 max_speed,
354 0.0f,
355 10000.0f,
356 "Max Speed",
357 "Maximum speed in UI units per second",
358 0.0f,
359 10000.0f);
360 RNA_def_float(ot->srna,
361 "delay",
362 delay,
363 0.0f,
364 10.0f,
365 "Delay",
366 "Delay in seconds before maximum speed is reached",
367 0.0f,
368 10.0f);
369 RNA_def_float(ot->srna,
370 "zoom_influence",
371 zoom_influence,
372 0.0f,
373 1.0f,
374 "Zoom Influence",
375 "Influence of the zoom factor on scroll speed",
376 0.0f,
377 1.0f);
378}
379
381{
383 vpd,
384 RNA_float_get(op->ptr, "inside_padding"),
385 RNA_float_get(op->ptr, "outside_padding"),
386 RNA_float_get(op->ptr, "speed_ramp"),
387 RNA_float_get(op->ptr, "max_speed"),
388 RNA_float_get(op->ptr, "delay"),
389 RNA_float_get(op->ptr, "zoom_influence"));
390}
391
bScreen * CTX_wm_screen(const bContext *C)
ScrArea * CTX_wm_area(const bContext *C)
wmWindow * CTX_wm_window(const bContext *C)
ARegion * CTX_wm_region(const bContext *C)
#define BLI_assert(a)
Definition BLI_assert.h:46
#define BLI_assert_msg(a, msg)
Definition BLI_assert.h:53
MINLINE float clamp_f(float value, float min, float max)
bool BLI_rcti_isect_pt_v(const struct rcti *rect, const int xy[2])
BLI_INLINE int BLI_rcti_size_y(const struct rcti *rct)
Definition BLI_rect.h:198
void BLI_rcti_pad(struct rcti *rect, int pad_x, int pad_y)
Definition rct.cc:629
void BLI_rctf_init(struct rctf *rect, float xmin, float xmax, float ymin, float ymax)
Definition rct.cc:404
BLI_INLINE int BLI_rcti_size_x(const struct rcti *rct)
Definition BLI_rect.h:194
BLI_INLINE float BLI_rctf_size_x(const struct rctf *rct)
Definition BLI_rect.h:202
BLI_INLINE float BLI_rctf_size_y(const struct rctf *rct)
Definition BLI_rect.h:206
Platform independent time functions.
double BLI_time_now_seconds(void)
Definition time.cc:65
#define CLAMP(a, b, c)
#define UI_SCALE_FAC
@ V2D_LOCKOFS_X
@ V2D_LOCKOFS_Y
void ED_region_tag_redraw_no_rebuild(ARegion *region)
Definition area.cc:659
#define C
Definition RandGen.cpp:29
void UI_view2d_sync(bScreen *screen, ScrArea *area, View2D *v2dcur, int flag)
Definition view2d.cc:865
#define V2D_LOCK_COPY
Definition UI_view2d.hh:85
void UI_view2d_curRect_changed(const bContext *C, View2D *v2d)
Definition view2d.cc:833
int pad[32 - sizeof(int)]
#define U
float distance(VecOp< float, D >, VecOp< float, D >) RET
float RNA_float_get(PointerRNA *ptr, const char *name)
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)
#define min(a, b)
Definition sort.cc:36
#define FLT_MAX
Definition stdcycles.h:14
double edge_pan_last_time
Definition UI_view2d.hh:540
double edge_pan_start_time_x
Definition UI_view2d.hh:541
struct rctf limit
Definition UI_view2d.hh:508
double edge_pan_start_time_y
Definition UI_view2d.hh:541
short keepofs
float xmax
float xmin
float ymax
float ymin
int ymin
int ymax
int xmin
int xmax
wmEventType type
Definition WM_types.hh:754
int xy[2]
Definition WM_types.hh:758
struct PointerRNA * ptr
max
Definition text_draw.cc:251
static void edge_pan_apply_delta(bContext *C, View2DEdgePanData *vpd, float dx, float dy)
static void edge_pan_manage_delay_timers(View2DEdgePanData *vpd, int pan_dir_x, int pan_dir_y, const double current_time)
void UI_view2d_edge_pan_operator_init(bContext *C, View2DEdgePanData *vpd, wmOperator *op)
void UI_view2d_edge_pan_set_limits(View2DEdgePanData *vpd, float xmin, float xmax, float ymin, float ymax)
void UI_view2d_edge_pan_cancel(bContext *C, View2DEdgePanData *vpd)
void UI_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 UI_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)
static float edge_pan_speed(View2DEdgePanData *vpd, int event_loc, bool x_dir, const double current_time)
void UI_view2d_edge_pan_apply(bContext *C, View2DEdgePanData *vpd, const int xy[2])
void UI_view2d_edge_pan_operator_properties(wmOperatorType *ot)
static float smootherstep(const float domain_max, float x)
void UI_view2d_edge_pan_reset(View2DEdgePanData *vpd)
void UI_view2d_edge_pan_apply_event(bContext *C, View2DEdgePanData *vpd, const wmEvent *event)
bool view2d_edge_pan_poll(bContext *C)
int xy[2]
Definition wm_draw.cc:174
void WM_event_add_mousemove(wmWindow *win)
@ MOUSEMOVE
wmOperatorType * ot
Definition wm_files.cc:4225