1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2008 Blender Foundation.
17  * All rights reserved.
18  * Generic 2d view with should allow drawing grids,
19  * panning, zooming, scrolling, ..
20  */
21 
22 /** \file
23  * \ingroup editorui
24  */
25 
26 #pragma once
27 
28 #include "BLI_compiler_attrs.h"
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 /* ------------------------------------------ */
35 /* Settings and Defines:                      */
36 
37 /* ---- General Defines ---- */
38 
39 /* generic value to use when coordinate lies out of view when converting */
40 #define V2D_IS_CLIPPED 12000
41 
42 /* Common View2D view types
43  * NOTE: only define a type here if it completely sets all (+/- a few) of the relevant flags
44  *       and settings for a View2D region, and that set of settings is used in more
45  *       than one specific place
46  */
47 enum eView2D_CommonViewTypes {
48   /* custom view type (region has defined all necessary flags already) */
49   V2D_COMMONVIEW_CUSTOM = -1,
50   /* standard (only use this when setting up a new view, as a sensible base for most settings) */
51   V2D_COMMONVIEW_STANDARD,
52   /* listview (i.e. Outliner) */
53   V2D_COMMONVIEW_LIST,
54   /* stackview (this is basically a list where new items are added at the top) */
55   V2D_COMMONVIEW_STACK,
56   /* headers (this is basically the same as listview, but no y-panning) */
57   V2D_COMMONVIEW_HEADER,
58   /* ui region containing panels */
59   V2D_COMMONVIEW_PANELS_UI,
60 };
61 
62 /* ---- Defines for Scroller Arguments ----- */
63 
64 /* ------ Defines for Scrollers ----- */
65 
66 /** Scroll bar area. */
67 #define V2D_SCROLL_HEIGHT (0.45f * U.widget_unit)
68 #define V2D_SCROLL_WIDTH (0.45f * U.widget_unit)
69 /** Scroll bars with 'handles' used for scale (zoom). */
70 #define V2D_SCROLL_HANDLE_HEIGHT (0.6f * U.widget_unit)
71 #define V2D_SCROLL_HANDLE_WIDTH (0.6f * U.widget_unit)
72 
73 /** Scroll bar with 'handles' hot-spot radius for cursor proximity. */
74 #define V2D_SCROLL_HANDLE_SIZE_HOTSPOT (0.6f * U.widget_unit)
75 
76 /** Don't allow scroll thumb to show below this size (so it's never too small to click on). */
77 #define V2D_SCROLL_THUMB_SIZE_MIN (30.0 * UI_DPI_FAC)
78 
79 /* ------ Define for UI_view2d_sync ----- */
80 
81 /* means copy it from another v2d */
82 #define V2D_LOCK_SET 0
83 /* means copy it to the other v2ds */
84 #define V2D_LOCK_COPY 1
85 
86 /* ------------------------------------------ */
87 /* Macros:                                    */
88 
89 /* test if mouse in a scrollbar (assume that scroller availability has been tested) */
90 #define IN_2D_VERT_SCROLL(v2d, co) (BLI_rcti_isect_pt_v(&v2d->vert, co))
91 #define IN_2D_HORIZ_SCROLL(v2d, co) (BLI_rcti_isect_pt_v(&v2d->hor, co))
92 
93 #define IN_2D_VERT_SCROLL_RECT(v2d, rct) (BLI_rcti_isect(&v2d->vert, rct, NULL))
94 #define IN_2D_HORIZ_SCROLL_RECT(v2d, rct) (BLI_rcti_isect(&v2d->hor, rct, NULL))
95 
96 /* ------------------------------------------ */
97 /* Type definitions:                          */
98 
99 struct View2D;
100 struct View2DScrollers;
101 
102 struct ARegion;
103 struct Scene;
104 struct ScrArea;
105 struct bContext;
106 struct bScreen;
107 struct rctf;
108 struct wmGizmoGroupType;
109 struct wmKeyConfig;
110 
111 typedef struct View2DScrollers View2DScrollers;
112 
113 /* ----------------------------------------- */
114 /* Prototypes:                               */
115 
116 /* refresh and validation (of view rects) */
117 void UI_view2d_region_reinit(struct View2D *v2d, short type, int winx, int winy);
118 
119 void UI_view2d_curRect_validate(struct View2D *v2d);
120 void UI_view2d_curRect_reset(struct View2D *v2d);
121 void UI_view2d_sync(struct bScreen *screen, struct ScrArea *area, struct View2D *v2dcur, int flag);
122 
123 /* Perform all required updates after `v2d->cur` as been modified.
124  * This includes like validation view validation (#UI_view2d_curRect_validate).
125  *
126  * Current intent is to use it from user code, such as view navigation and zoom operations. */
127 void UI_view2d_curRect_changed(const struct bContext *C, struct View2D *v2d);
128 
129 void UI_view2d_totRect_set(struct View2D *v2d, int width, int height);
130 void UI_view2d_totRect_set_resize(struct View2D *v2d, int width, int height, bool resize);
131 
132 void UI_view2d_mask_from_win(const struct View2D *v2d, struct rcti *r_mask);
133 
134 void UI_view2d_zoom_cache_reset(void);
135 
136 /* view matrix operations */
137 void UI_view2d_view_ortho(const struct View2D *v2d);
138 void UI_view2d_view_orthoSpecial(struct ARegion *region, struct View2D *v2d, const bool xaxis);
139 void UI_view2d_view_restore(const struct bContext *C);
140 
141 /* grid drawing */
142 void UI_view2d_constant_grid_draw(const struct View2D *v2d, float step);
143 void UI_view2d_multi_grid_draw(
144     const struct View2D *v2d, int colorid, float step, int level_size, int totlevels);
145 
146 void UI_view2d_draw_lines_y__values(const struct View2D *v2d);
147 void UI_view2d_draw_lines_x__values(const struct View2D *v2d);
148 void UI_view2d_draw_lines_x__discrete_values(const struct View2D *v2d);
149 void UI_view2d_draw_lines_x__discrete_time(const struct View2D *v2d, const struct Scene *scene);
150 void UI_view2d_draw_lines_x__discrete_frames_or_seconds(const struct View2D *v2d,
151                                                         const struct Scene *scene,
152                                                         bool display_seconds);
153 void UI_view2d_draw_lines_x__frames_or_seconds(const struct View2D *v2d,
154                                                const struct Scene *scene,
155                                                bool display_seconds);
156 
157 float UI_view2d_grid_resolution_x__frames_or_seconds(const struct View2D *v2d,
158                                                      const struct Scene *scene,
159                                                      bool display_seconds);
160 float UI_view2d_grid_resolution_y__values(const struct View2D *v2d);
161 
162 /* scale indicator text drawing */
163 void UI_view2d_draw_scale_y__values(const struct ARegion *region,
164                                     const struct View2D *v2d,
165                                     const struct rcti *rect,
166                                     int colorid);
167 void UI_view2d_draw_scale_y__block(const struct ARegion *region,
168                                    const struct View2D *v2d,
169                                    const struct rcti *rect,
170                                    int colorid);
171 void UI_view2d_draw_scale_x__discrete_frames_or_seconds(const struct ARegion *region,
172                                                         const struct View2D *v2d,
173                                                         const struct rcti *rect,
174                                                         const struct Scene *scene,
175                                                         bool display_seconds,
176                                                         int colorid);
177 void UI_view2d_draw_scale_x__frames_or_seconds(const struct ARegion *region,
178                                                const struct View2D *v2d,
179                                                const struct rcti *rect,
180                                                const struct Scene *scene,
181                                                bool display_seconds,
182                                                int colorid);
183 
184 /* scrollbar drawing */
185 void UI_view2d_scrollers_calc(struct View2D *v2d,
186                               const struct rcti *mask_custom,
187                               struct View2DScrollers *r_scrollers);
188 void UI_view2d_scrollers_draw(struct View2D *v2d, const struct rcti *mask_custom);
189 
190 /* list view tools */
191 void UI_view2d_listview_view_to_cell(float columnwidth,
192                                      float rowheight,
193                                      float startx,
194                                      float starty,
195                                      float viewx,
196                                      float viewy,
197                                      int *column,
198                                      int *row);
199 
200 /* coordinate conversion */
201 float UI_view2d_region_to_view_x(const struct View2D *v2d, float x);
202 float UI_view2d_region_to_view_y(const struct View2D *v2d, float y);
203 void UI_view2d_region_to_view(
204     const struct View2D *v2d, float x, float y, float *r_view_x, float *r_view_y) ATTR_NONNULL();
205 void UI_view2d_region_to_view_rctf(const struct View2D *v2d,
206                                    const struct rctf *rect_src,
207                                    struct rctf *rect_dst) ATTR_NONNULL();
208 
209 float UI_view2d_view_to_region_x(const struct View2D *v2d, float x);
210 float UI_view2d_view_to_region_y(const struct View2D *v2d, float y);
211 bool UI_view2d_view_to_region_clip(
212     const struct View2D *v2d, float x, float y, int *r_region_x, int *r_region_y) ATTR_NONNULL();
213 
214 void UI_view2d_view_to_region(
215     const struct View2D *v2d, float x, float y, int *r_region_x, int *r_region_y) ATTR_NONNULL();
216 void UI_view2d_view_to_region_fl(const struct View2D *v2d,
217                                  float x,
218                                  float y,
219                                  float *r_region_x,
220                                  float *r_region_y) ATTR_NONNULL();
221 void UI_view2d_view_to_region_m4(const struct View2D *v2d, float matrix[4][4]) ATTR_NONNULL();
222 void UI_view2d_view_to_region_rcti(const struct View2D *v2d,
223                                    const struct rctf *rect_src,
224                                    struct rcti *rect_dst) ATTR_NONNULL();
225 bool UI_view2d_view_to_region_rcti_clip(const struct View2D *v2d,
226                                         const struct rctf *rect_src,
227                                         struct rcti *rect_dst) ATTR_NONNULL();
228 
229 /* utilities */
230 struct View2D *UI_view2d_fromcontext(const struct bContext *C);
231 struct View2D *UI_view2d_fromcontext_rwin(const struct bContext *C);
232 
233 void UI_view2d_scroller_size_get(const struct View2D *v2d, float *r_x, float *r_y);
234 void UI_view2d_scale_get(struct View2D *v2d, float *r_x, float *r_y);
235 float UI_view2d_scale_get_x(const struct View2D *v2d);
236 float UI_view2d_scale_get_y(const struct View2D *v2d);
237 void UI_view2d_scale_get_inverse(const struct View2D *v2d, float *r_x, float *r_y);
238 
239 void UI_view2d_center_get(const struct View2D *v2d, float *r_x, float *r_y);
240 void UI_view2d_center_set(struct View2D *v2d, float x, float y);
241 
242 void UI_view2d_offset(struct View2D *v2d, float xfac, float yfac);
243 
244 char UI_view2d_mouse_in_scrollers_ex(
245     const struct ARegion *region, const struct View2D *v2d, int x, int y, int *r_scroll);
246 char UI_view2d_mouse_in_scrollers(const struct ARegion *region,
247                                   const struct View2D *v2d,
248                                   int x,
249                                   int y);
250 char UI_view2d_rect_in_scrollers_ex(const struct ARegion *region,
251                                     const struct View2D *v2d,
252                                     const struct rcti *rect,
253                                     int *r_scroll);
254 char UI_view2d_rect_in_scrollers(const struct ARegion *region,
255                                  const struct View2D *v2d,
256                                  const struct rcti *rect);
257 
258 /* cached text drawing in v2d, to allow pixel-aligned draw as post process */
259 void UI_view2d_text_cache_add(struct View2D *v2d,
260                               float x,
261                               float y,
262                               const char *str,
263                               size_t str_len,
264                               const unsigned char col[4]);
265 void UI_view2d_text_cache_add_rectf(struct View2D *v2d,
266                                     const struct rctf *rect_view,
267                                     const char *str,
268                                     size_t str_len,
269                                     const unsigned char col[4]);
270 void UI_view2d_text_cache_draw(struct ARegion *region);
271 
272 /* operators */
273 void ED_operatortypes_view2d(void);
274 void ED_keymap_view2d(struct wmKeyConfig *keyconf);
275 
276 void UI_view2d_smooth_view(struct bContext *C,
277                            struct ARegion *region,
278                            const struct rctf *cur,
279                            const int smooth_viewtx);
280 
281 #define UI_MARKER_MARGIN_Y (42 * UI_DPI_FAC)
282 #define UI_TIME_SCRUB_MARGIN_Y (23 * UI_DPI_FAC)
283 
284 /* Gizmo Types */
285 
286 /* view2d_gizmo_navigate.c */
287 /* Caller passes in own idname.  */
288 void VIEW2D_GGT_navigate_impl(struct wmGizmoGroupType *gzgt, const char *idname);
289 
290 #ifdef __cplusplus
291 }
292 #endif
293