Blender V5.3
time_scrub_ui.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2019 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BKE_context.hh"
10#include "BKE_scene.hh"
11
12#include "GPU_immediate.hh"
13#include "GPU_matrix.hh"
14#include "GPU_state.hh"
15
16#include "ED_time_scrub_ui.hh"
17
18#include "WM_api.hh"
19#include "WM_types.hh"
20
21#include "UI_interface.hh"
22#include "UI_interface_icons.hh"
24#include "UI_resources.hh"
25#include "UI_view2d.hh"
26
27#include "DNA_scene_types.h"
28
29#include "BLI_math_base_c.hh"
30#include "BLI_rect.hh"
31#include "BLI_string_utf8.hh"
32#include "BLI_timecode.hh"
33
34#include "RNA_access.hh"
35#include "RNA_prototypes.hh"
36
37namespace blender {
38
39void ED_time_scrub_region_rect_get(const ARegion *region, rcti *r_rect)
40{
41 r_rect->xmin = 0;
42 r_rect->xmax = region->winx;
43 r_rect->ymax = region->winy;
44 r_rect->ymin = r_rect->ymax - UI_TIME_SCRUB_MARGIN_Y;
45}
46
47static int get_centered_text_y(const rcti *rect)
48{
49 return BLI_rcti_cent_y(rect) - UI_SCALE_FAC * 4;
50}
51
52static void draw_background(const rcti *rect)
53{
54 uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", gpu::VertAttrType::SFLOAT_32_32);
56
58
60
61 immRectf(pos, rect->xmin, rect->ymin, rect->xmax, rect->ymax);
62
64
66}
67
69 const Scene *scene, bool display_seconds, const float frame, char *r_str, uint str_maxncpy)
70{
71 if (display_seconds) {
72 const float frame_len = scene->r.framelen > 0 ? scene->r.framelen : 1.0;
73 const float seconds = (frame / float(scene->frames_per_second())) / frame_len;
75 r_str, str_maxncpy, -1, seconds, scene->frames_per_second(), U.timecode_style);
76 }
77 else if (scene->r.flag & SCER_SHOW_SUBFRAME) {
78 BLI_snprintf_utf8(r_str, str_maxncpy, "%.02f", frame);
79 }
80 else {
81 BLI_snprintf_utf8(r_str, str_maxncpy, "%d", int(frame));
82 }
83}
84
97
99 const rcti *scrub_region_rect,
100 const float current_frame,
101 const bool display_seconds,
102 const float region_x)
103{
104 PlayheadDimensions dimensions;
105 constexpr int max_frame_string_len = 64;
106 char frame_str[max_frame_string_len];
108 scene, display_seconds, current_frame, frame_str, max_frame_string_len);
109
110 /* Forcing the outline width to always be a full pixel solves alignment issues between
111 * the tip, the box and the stalk. */
112 dimensions.outline_width = std::max(1.0f, round(UI_SCALE_FAC));
114 const float text_padding = 4.0f * UI_SCALE_FAC;
115 const float box_min_width = 24.0f * UI_SCALE_FAC;
116 const float box_width = std::max(dimensions.text_width + (2.0f * text_padding), box_min_width);
117 const float box_margin = 2.0f * UI_SCALE_FAC;
118
119 /* Vertical line. */
120 if (UI_SCALE_FAC < 0.91f) {
121 dimensions.stalk_rect.xmin = floor(region_x) - 1;
122 dimensions.stalk_rect.xmax = dimensions.stalk_rect.xmin + U.pixelsize + 2;
123 }
124 else {
125 dimensions.stalk_rect.xmin = floor(region_x - U.pixelsize) - dimensions.outline_width;
126 dimensions.stalk_rect.xmax = floor(region_x + U.pixelsize + 1.0f) + dimensions.outline_width;
127 }
128 dimensions.stalk_rect.ymin = 0.0f;
129 dimensions.stalk_rect.ymax = scrub_region_rect->ymin;
130
131 /* Box where the frame number is drawn. */
132 dimensions.box_rect.xmin = region_x - (box_width / 2.0f);
133 dimensions.box_rect.xmax = region_x + (box_width / 2.0f) + 1.0f;
134 dimensions.box_rect.ymin = floor(scrub_region_rect->ymin +
135 (box_margin - dimensions.outline_width));
136 dimensions.box_rect.ymax = ceil(scrub_region_rect->ymax - box_margin + dimensions.outline_width);
137
138 /* Connecting shape between box and stalk. */
139 dimensions.tip_top = dimensions.box_rect.ymin + dimensions.outline_width;
140 dimensions.tip_bottom = dimensions.stalk_rect.ymax - dimensions.outline_width;
141 dimensions.tip_half_width = 6.0f * UI_SCALE_FAC;
142
143 return dimensions;
144}
145
146static void draw_playhead_stalk(const float region_x,
147 const PlayheadDimensions &dimensions,
148 const float fg_color[4],
149 const float bg_color[4])
150{
151 float outline_width = dimensions.outline_width;
152
153 /* Shadow for triangle below frame box. */
154 GPUVertFormat *format = immVertexFormat();
155 uint pos = GPU_vertformat_attr_add(format, "pos", gpu::VertAttrType::SFLOAT_32_32);
158 GPU_polygon_smooth(true);
159 immUniformColor4fv(bg_color);
161 immVertex2f(pos, region_x - dimensions.tip_half_width - outline_width, dimensions.tip_top);
162 immVertex2f(pos, region_x + dimensions.tip_half_width + outline_width, dimensions.tip_top);
163 immVertex2f(pos, dimensions.stalk_rect.xmin, dimensions.tip_bottom);
164 immVertex2f(pos, dimensions.stalk_rect.xmax, dimensions.tip_bottom);
165
166 immEnd();
168 GPU_polygon_smooth(false);
170
172 &dimensions.stalk_rect, fg_color, nullptr, 1.0f, bg_color, outline_width, 0.0f);
173}
174
175static void draw_playhead_box(const float region_x,
176 const char frame_str[64],
177 const rcti *scrub_region_rect,
178 const PlayheadDimensions &dimensions,
179 const float fg_color[4],
180 const float bg_color[4])
181{
182 draw_roundbox_corner_set(ui::CNR_ALL);
183 const float box_corner_radius = 4.0f * UI_SCALE_FAC;
184
186 fg_color,
187 nullptr,
188 1.0f,
189 bg_color,
190 dimensions.outline_width,
191 box_corner_radius);
192
193 /* Frame number text. */
194 const uiFontStyle *fstyle = UI_FSTYLE_WIDGET;
195 uchar text_color[4];
197 const int y = BLI_rcti_cent_y(scrub_region_rect) - int(fstyle->points * UI_SCALE_FAC * 0.38f);
199 fstyle, region_x - (dimensions.text_width / 2.0f), y, frame_str, text_color);
200}
201
202/* Draws the little tip at the bottom of the playhead. */
203static void draw_playhead_tip(const float region_x,
204 const PlayheadDimensions &dimensions,
205 const float fg_color[4])
206{
207 GPUVertFormat *format = immVertexFormat();
208 uint pos = GPU_vertformat_attr_add(format, "pos", gpu::VertAttrType::SFLOAT_32_32);
209 /* Triangular base under frame number. */
211 GPU_polygon_smooth(true);
214 immUniformColor4fv(fg_color);
215 immVertex2f(pos, region_x - dimensions.tip_half_width, dimensions.tip_top);
216 immVertex2f(pos, region_x + dimensions.tip_half_width, dimensions.tip_top);
217 immVertex2f(pos, dimensions.stalk_rect.xmin + dimensions.outline_width, dimensions.tip_bottom);
218 immVertex2f(pos, dimensions.stalk_rect.xmax - dimensions.outline_width, dimensions.tip_bottom);
219 immEnd();
221 GPU_polygon_smooth(false);
223}
224
228static void draw_playhead_ghost(const float frame,
229 const Scene *scene,
230 const View2D *v2d,
231 const rcti *scrub_region_rect,
232 const bool display_seconds,
233 const bool display_stalk)
234{
235 const float region_x = ui::view2d_view_to_region_x(v2d, frame);
236
238 scene, scrub_region_rect, frame, display_seconds, region_x);
239 float fg_color[4];
241 float bg_color[4];
243 fg_color[3] /= 2;
244 bg_color[3] /= 2;
245 if (display_stalk) {
246 draw_playhead_stalk(region_x, dimensions, fg_color, bg_color);
247 }
248
249 constexpr int max_frame_string_len = 64;
250 char frame_str[max_frame_string_len];
251 ED_time_scrub_frame_str_get(scene, display_seconds, frame, frame_str, max_frame_string_len);
252 draw_playhead_box(region_x, frame_str, scrub_region_rect, dimensions, fg_color, bg_color);
253
254 if (display_stalk) {
255 draw_playhead_tip(region_x, dimensions, fg_color);
256 }
257}
258
259static void draw_current_frame(const Scene *scene,
260 bool display_seconds,
261 const View2D *v2d,
262 const rcti *scrub_region_rect,
263 bool display_stalk = true,
264 bool clamp_playhead = false)
265{
266 const float current_frame = BKE_scene_frame_get(scene);
267 float region_x = ui::view2d_view_to_region_x(v2d, current_frame);
268
269 constexpr int max_frame_string_len = 64;
270 char frame_str[max_frame_string_len];
272 scene, display_seconds, current_frame, frame_str, max_frame_string_len);
273
275 scene, scrub_region_rect, current_frame, display_seconds, region_x);
276
277 if (clamp_playhead) {
278 const float bound_left = scrub_region_rect->xmin + dimensions.text_width / 2.0f;
279 const float bound_right = scrub_region_rect->xmax - dimensions.text_width / 2.0f;
280
281 if (bound_left > bound_right) {
282 /* When the available space is smaller than the width of the playhead,
283 * align the label so its right edge sits flush with the region's edge. */
284 region_x = bound_right;
285 }
286 else {
287 region_x = math::clamp(region_x, bound_left, bound_right);
288 }
289
290 dimensions = get_playhead_dimensions(
291 scene, scrub_region_rect, current_frame, display_seconds, region_x);
292 }
293
294 float fg_color[4];
296 /* Forcing alpha to 1 for the playhead box itself. Only the stalk should have transparency. */
297 fg_color[3] = 1.0;
298
299 float stalk_color[4];
301 float bg_color[4];
303
304 if (display_stalk) {
305 draw_playhead_stalk(region_x, dimensions, stalk_color, bg_color);
306 }
307
308 draw_playhead_box(region_x, frame_str, scrub_region_rect, dimensions, fg_color, bg_color);
309
310 if (display_stalk) {
311 draw_playhead_tip(region_x, dimensions, fg_color);
312 }
313}
314
316 const Scene *scene,
317 bool display_seconds,
318 bool display_stalk,
319 bool clamp_playhead)
320{
321 const View2D *v2d = &region->v2d;
324
325 rcti scrub_region_rect;
326 ED_time_scrub_region_rect_get(region, &scrub_region_rect);
327
328 if (scene->r.framelen != 1.0) {
329 /* In case the time scale feature is active, we draw a second playhead with less opacity to
330 * indicate the remapped time. */
331 const float ctime = BKE_scene_ctime_get(scene);
332 draw_playhead_ghost(ctime, scene, v2d, &scrub_region_rect, display_seconds, display_stalk);
333 }
334
336 scene, display_seconds, v2d, &scrub_region_rect, display_stalk, clamp_playhead);
338}
339
340void ED_time_scrub_draw(const ARegion *region,
341 const Scene *scene,
342 bool display_seconds,
343 bool discrete_frames,
344 const int base)
345{
346 const View2D *v2d = &region->v2d;
347
350
351 rcti scrub_region_rect;
352 ED_time_scrub_region_rect_get(region, &scrub_region_rect);
353
354 draw_background(&scrub_region_rect);
355
356 rcti numbers_rect = scrub_region_rect;
357 numbers_rect.ymin = get_centered_text_y(&scrub_region_rect) - 4 * UI_SCALE_FAC;
359 v2d,
360 &numbers_rect,
361 scene,
362 display_seconds,
363 !discrete_frames,
365 base);
366
368}
369
371{
372 rcti clamped_mask = scroller_mask;
373 clamped_mask.ymax -= UI_TIME_SCRUB_MARGIN_Y;
374 return clamped_mask;
375}
376
377bool ED_time_scrub_event_in_region(const ARegion *region, const wmEvent *event)
378{
379 rcti rect = region->winrct;
380 rect.ymin = rect.ymax - UI_TIME_SCRUB_MARGIN_Y;
381 return BLI_rcti_isect_pt_v(&rect, event->xy);
382}
383
385 const ScrArea * /*area*/,
386 const ARegion *region,
387 const wmEvent *event)
388{
389 return ED_time_scrub_event_in_region(region, event);
390}
391
393{
396
397 rcti rect;
398 rect.xmin = 0;
399 rect.xmax = region->winx;
400 rect.ymin = region->winy - UI_TIME_SCRUB_MARGIN_Y;
401 rect.ymax = region->winy;
402
403 uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", gpu::VertAttrType::SFLOAT_32_32);
406 immRectf(pos, rect.xmin, rect.ymin, rect.xmax, rect.ymax);
408
409 PointerRNA ptr = RNA_pointer_create_discrete(&CTX_wm_screen(C)->id, RNA_DopeSheet, dopesheet);
410
411 const uiStyle *style = ui::style_get_dpi();
412 const float padding_x = 2 * UI_SCALE_FAC;
413 const float padding_y = UI_SCALE_FAC;
414
415 ui::Block *block = block_begin(C, region, __func__, ui::EmbossType::Emboss);
416 ui::Layout &layout = ui::block_layout(block,
419 rect.xmin + padding_x,
420 rect.ymin + UI_UNIT_Y + padding_y,
421 BLI_rcti_size_x(&rect) - 2 * padding_x,
422 1,
423 0,
424 style);
425 layout.scale_y_set((UI_UNIT_Y - padding_y) / UI_UNIT_Y);
426 ui::block_layout_set_current(block, &layout);
427 block_align_begin(block);
428 layout.prop(&ptr, "filter_text", UI_ITEM_NONE, "", ICON_NONE);
429 layout.prop(&ptr, "use_filter_invert", UI_ITEM_NONE, "", ICON_ARROW_LEFTRIGHT);
430 block_align_end(block);
432
433 /* Make sure the events are consumed from the search and don't reach other UI blocks since this
434 * is drawn on top of animation-channels. */
435 block_flag_enable(block, ui::BLOCK_CLIP_EVENTS);
436 block_bounds_set_normal(block, 0);
437 block_end(C, block);
438 block_draw(C, block);
439
441}
442
443} // namespace blender
#define UI_SCALE_FAC
#define C
Definition RandGen.cpp:29
#define UI_UNIT_Y
#define UI_FSTYLE_WIDGET
#define UI_ITEM_NONE
#define UI_TIME_SCRUB_MARGIN_Y
Definition UI_view2d.hh:493
#define round
#define floor
#define ceil
T clamp(const T &a, const T &min, const T &max)
void get_color_4ubv(int colorid, unsigned char col[4])
void get_color_4fv(int colorid, float col[4])
void get_color_shade_4fv(int colorid, int offset, float col[4])
void view2d_draw_scale_x(const ARegion *region, const View2D *v2d, const rcti *rect, const Scene *scene, bool display_seconds, bool show_fractions, int colorid, int base)
int fontstyle_string_width(const uiFontStyle *fs, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
const uiStyle * style_get_dpi()
void fontstyle_draw_simple(const uiFontStyle *fs, float x, float y, const char *str, const uchar col[4])
void draw_roundbox_4fv_ex(const rctf *rect, const float inner1[4], const float inner2[4], float shade_dir, const float outline[4], float outline_width, float rad)
int2 block_layout_resolve(Block *block)
Layout & block_layout(Block *block, LayoutDirection direction, LayoutType type, int x, int y, int size, int em, int padding, const uiStyle *style)
void block_layout_set_current(Block *block, Layout *layout)
float view2d_view_to_region_x(const View2D *v2d, float x)
Definition view2d.cc:1673
BMesh Iterators.
UserDef U
Definition blender.cc:58
static void draw_playhead_box(const float region_x, const char frame_str[64], const rcti *scrub_region_rect, const PlayheadDimensions &dimensions, const float fg_color[4], const float bg_color[4])
size_t size_t size_t BLI_snprintf_utf8(char *__restrict dst, size_t dst_maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
void ED_time_scrub_draw_current_frame(const ARegion *region, const Scene *scene, bool display_seconds, bool display_stalk, bool clamp_playhead)
void ED_time_scrub_draw(const ARegion *region, const Scene *scene, bool display_seconds, bool discrete_frames, const int base)
void wmOrtho2_region_pixelspace(const ARegion *region)
void immEnd()
unsigned int uint
Definition BKE_text.h:55
void GPU_polygon_smooth(bool enable)
Definition gpu_state.cc:85
static int get_centered_text_y(const rcti *rect)
GPUVertFormat * immVertexFormat()
bool ED_time_scrub_event_in_region(const ARegion *region, const wmEvent *event)
void immBegin(GPUPrimType, uint vertex_len)
@ GPU_PRIM_TRI_STRIP
@ TH_TIME_SCRUB_TEXT
@ TH_TIME_SCRUB_BACKGROUND
void immUniformColor4fv(const float rgba[4])
void immUnbindProgram()
static PlayheadDimensions get_playhead_dimensions(const Scene *scene, const rcti *scrub_region_rect, const float current_frame, const bool display_seconds, const float region_x)
@ GPU_BLEND_NONE
Definition GPU_state.hh:88
@ GPU_BLEND_ALPHA
Definition GPU_state.hh:90
void ED_time_scrub_frame_str_get(const Scene *scene, bool display_seconds, const float frame, char *r_str, uint str_maxncpy)
void immBindBuiltinProgram(GPUBuiltinShader shader_id)
void immUniformThemeColor(int color_id)
void immVertex2f(uint attr_id, float x, float y)
unsigned char uchar
Definition BKE_text.h:58
static void draw_playhead_tip(const float region_x, const PlayheadDimensions &dimensions, const float fg_color[4])
BLI_INLINE int BLI_rcti_cent_y(const struct rcti *rct)
Definition BLI_rect.hh:190
float BKE_scene_frame_get(const Scene *scene)
Definition scene.cc:2595
bool ED_time_scrub_event_in_region_poll(const wmWindow *, const ScrArea *, const ARegion *region, const wmEvent *event)
rcti ED_time_scrub_clamp_scroller_mask(const rcti &scroller_mask)
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
static void draw_current_frame(const Scene *scene, bool display_seconds, const View2D *v2d, const rcti *scrub_region_rect, bool display_stalk=true, bool clamp_playhead=false)
uint GPU_vertformat_attr_add(GPUVertFormat *format, StringRef name, gpu::VertAttrType type)
BLI_INLINE int BLI_rcti_size_x(const struct rcti *rct)
Definition BLI_rect.hh:203
size_t BLI_timecode_string_from_time(char *str, size_t maxncpy, int brevity_level, float time_seconds, double fps, short timecode_style) ATTR_NONNULL()
Definition timecode.cc:24
float BKE_scene_ctime_get(const Scene *scene)
Definition scene.cc:2581
void GPU_matrix_pop_projection()
bScreen * CTX_wm_screen(const bContext *C)
bool BLI_rcti_isect_pt_v(const struct rcti *rect, const int xy[2])
void GPU_blend(GPUBlend blend)
Definition gpu_state.cc:44
static void draw_background(const rcti *rect)
void ED_time_scrub_channel_search_draw(const bContext *C, ARegion *region, bDopeSheet *dopesheet)
void GPU_matrix_push_projection()
void immRectf(uint pos, float x1, float y1, float x2, float y2)
void ED_time_scrub_region_rect_get(const ARegion *region, rcti *r_rect)
static void draw_playhead_stalk(const float region_x, const PlayheadDimensions &dimensions, const float fg_color[4], const float bg_color[4])
static void draw_playhead_ghost(const float frame, const Scene *scene, const View2D *v2d, const rcti *scrub_region_rect, const bool display_seconds, const bool display_stalk)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
struct RenderData r
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)
void scale_y_set(float scale)