Blender V5.0
sequencer_channels_draw.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "MEM_guardedalloc.h"
10
11#include "DNA_scene_types.h"
12#include "DNA_screen_types.h"
13
14#include "BKE_context.hh"
15#include "BKE_screen.hh"
16
17#include "BLI_math_base.h"
18#include "BLI_string.h"
19#include "BLI_utildefines.h"
20
21#include "ED_screen.hh"
22
23#include "GPU_matrix.hh"
24
25#include "RNA_prototypes.hh"
26
27#include "SEQ_channels.hh"
28#include "SEQ_sequencer.hh"
29#include "SEQ_time.hh"
30
31#include "UI_interface.hh"
32#include "UI_resources.hh"
33#include "UI_view2d.hh"
34
35#include "WM_api.hh"
36
37/* Own include. */
38#include "sequencer_intern.hh"
39
40namespace blender::ed::vse {
41
42static float draw_offset_get(const View2D *timeline_region_v2d)
43{
44 return timeline_region_v2d->cur.ymin;
45}
46
47static float channel_height_pixelspace_get(const View2D *timeline_region_v2d)
48{
49 return UI_view2d_view_to_region_y(timeline_region_v2d, 1.0f) -
50 UI_view2d_view_to_region_y(timeline_region_v2d, 0.0f);
51}
52
53static float frame_width_pixelspace_get(const View2D *timeline_region_v2d)
54{
55
56 return UI_view2d_view_to_region_x(timeline_region_v2d, 1.0f) -
57 UI_view2d_view_to_region_x(timeline_region_v2d, 0.0f);
58}
59
60static float icon_width_get(const SeqChannelDrawContext *context)
61{
62 return (U.widget_unit * 0.8 * context->scale);
63}
64
65static float widget_y_offset(const SeqChannelDrawContext *context)
66{
67 return ((context->channel_height / context->scale) - icon_width_get(context)) / 2;
68}
69
70static float channel_index_y_min(const SeqChannelDrawContext *context, const int index)
71{
72 float y = (index - context->draw_offset) * context->channel_height;
73 y /= context->scale;
74 return y;
75}
76
78 int r_channel_range[2])
79{
80 /* Channel 0 is not usable, so should never be drawn. */
81 r_channel_range[0] = max_ii(1, floor(context->timeline_region_v2d->cur.ymin));
82 r_channel_range[1] = ceil(context->timeline_region_v2d->cur.ymax);
83
84 rctf strip_boundbox;
85 BLI_rctf_init(&strip_boundbox, 0.0f, 0.0f, 1.0f, r_channel_range[1]);
86 seq::timeline_expand_boundbox(context->scene, context->seqbase, &strip_boundbox);
87 CLAMP(r_channel_range[0], strip_boundbox.ymin, strip_boundbox.ymax);
88 CLAMP(r_channel_range[1], strip_boundbox.ymin, seq::MAX_CHANNELS);
89}
90
91static std::string draw_channel_widget_tooltip(bContext * /*C*/,
92 void *argN,
93 const blender::StringRef /*tip*/)
94{
95 char *dyn_tooltip = static_cast<char *>(argN);
96 return dyn_tooltip;
97}
98
100 uiBlock *block,
101 const int channel_index,
102 const float offset)
103{
104 float y = channel_index_y_min(context, channel_index) + widget_y_offset(context);
105
106 const float width = icon_width_get(context);
107 SeqTimelineChannel *channel = seq::channel_get_by_index(context->channels, channel_index);
108 const int icon = seq::channel_is_muted(channel) ? ICON_CHECKBOX_DEHLT : ICON_CHECKBOX_HLT;
109
111 &context->scene->id, &RNA_SequenceTimelineChannel, channel);
112 PropertyRNA *hide_prop = RNA_struct_type_find_property(&RNA_SequenceTimelineChannel, "mute");
113
115 uiBut *but = uiDefIconButR_prop(block,
117 1,
118 icon,
119 context->v2d->cur.xmax / context->scale - offset,
120 y,
121 width,
122 width,
123 &ptr,
124 hide_prop,
125 0,
126 0,
127 0,
128 std::nullopt);
129
130 char *tooltip = BLI_sprintfN(
131 "%s channel %d", seq::channel_is_muted(channel) ? "Unmute" : "Mute", channel_index);
133
134 return width;
135}
136
138 uiBlock *block,
139 const int channel_index,
140 const float offset)
141{
142
143 float y = channel_index_y_min(context, channel_index) + widget_y_offset(context);
144 const float width = icon_width_get(context);
145
146 SeqTimelineChannel *channel = seq::channel_get_by_index(context->channels, channel_index);
147 const int icon = seq::channel_is_locked(channel) ? ICON_LOCKED : ICON_UNLOCKED;
148
150 &context->scene->id, &RNA_SequenceTimelineChannel, channel);
151 PropertyRNA *hide_prop = RNA_struct_type_find_property(&RNA_SequenceTimelineChannel, "lock");
152
154 uiBut *but = uiDefIconButR_prop(block,
156 1,
157 icon,
158 context->v2d->cur.xmax / context->scale - offset,
159 y,
160 width,
161 width,
162 &ptr,
163 hide_prop,
164 0,
165 0,
166 0,
167 "");
168
169 char *tooltip = BLI_sprintfN(
170 "%s channel %d", seq::channel_is_locked(channel) ? "Unlock" : "Lock", channel_index);
172
173 return width;
174}
175
176static bool channel_is_being_renamed(const SpaceSeq *sseq, const int channel_index)
177{
178 return sseq->runtime->rename_channel_index == channel_index;
179}
180
181static float text_size_get(const SeqChannelDrawContext *context)
182{
183 const uiStyle *style = UI_style_get_dpi();
184 return UI_fontstyle_height_max(&style->widget) * 1.5f * context->scale;
185}
186
187/* TODO: decide what gets priority - label or buttons. */
189 const int channel_index,
190 const float used_width)
191{
192 float text_size = text_size_get(context);
193 float margin = (context->channel_height / context->scale - text_size) / 2.0f;
194 float y = channel_index_y_min(context, channel_index) + margin;
195
196 float margin_x = icon_width_get(context) * 0.65;
197 float width = max_ff(0.0f, context->v2d->cur.xmax / context->scale - used_width);
198
199 /* Text input has its own margin. Prevent text jumping around and use as much space as possible.
200 */
201 if (channel_is_being_renamed(CTX_wm_space_seq(context->C), channel_index)) {
202 float input_box_margin = icon_width_get(context) * 0.5f;
203 margin_x -= input_box_margin;
204 width += input_box_margin;
205 }
206
207 rctf rect;
208 BLI_rctf_init(&rect, margin_x, margin_x + width, y, y + text_size);
209 return rect;
210}
211
213 uiBlock *block,
214 const int channel_index,
215 const float used_width)
216{
217 SpaceSeq *sseq = CTX_wm_space_seq(context->C);
218 rctf rect = label_rect_init(context, channel_index, used_width);
219
220 if (BLI_rctf_size_y(&rect) <= 1.0f || BLI_rctf_size_x(&rect) <= 1.0f) {
221 return;
222 }
223
224 if (channel_is_being_renamed(sseq, channel_index)) {
225 SeqTimelineChannel *channel = seq::channel_get_by_index(context->channels, channel_index);
227 &context->scene->id, &RNA_SequenceTimelineChannel, channel);
229
231 uiBut *but = uiDefButR(block,
233 1,
234 "",
235 rect.xmin,
236 rect.ymin,
237 BLI_rctf_size_x(&rect),
238 BLI_rctf_size_y(&rect),
239 &ptr,
241 -1,
242 0,
243 0,
244 std::nullopt);
246
247 if (UI_but_active_only(context->C, context->region, block, but) == false) {
248 sseq->runtime->rename_channel_index = 0;
249 }
250
251 WM_event_add_notifier(context->C, NC_SCENE | ND_SEQUENCER, context->scene);
252 }
253 else {
254 const char *label = seq::channel_name_get(context->channels, channel_index);
255 uiDefBut(block,
257 0,
258 label,
259 rect.xmin,
260 rect.ymin,
261 rect.xmax - rect.xmin,
262 (rect.ymax - rect.ymin),
263 nullptr,
264 0,
265 0,
266 std::nullopt);
267 }
268}
269
271{
273 wmOrtho2_pixelspace(context->region->winx / context->scale,
274 context->region->winy / context->scale);
275 uiBlock *block = UI_block_begin(context->C, context->region, __func__, ui::EmbossType::Emboss);
276
277 int channel_range[2];
278 displayed_channel_range_get(context, channel_range);
279
280 const float icon_width = icon_width_get(context);
281 const float offset_lock = icon_width * 1.5f;
282 const float offset_mute = icon_width * 2.5f;
283 const float offset_width = icon_width * 3.5f;
284 /* Draw widgets separately from text labels so they are batched together,
285 * instead of alternating between two fonts (regular and SVG/icons). */
286 for (int channel = channel_range[0]; channel <= channel_range[1]; channel++) {
287 draw_channel_widget_lock(context, block, channel, offset_lock);
288 draw_channel_widget_mute(context, block, channel, offset_mute);
289 }
290 for (int channel = channel_range[0]; channel <= channel_range[1]; channel++) {
291 draw_channel_labels(context, block, channel, offset_width);
292 }
293
294 UI_block_end(context->C, block);
295 UI_block_draw(context->C, block);
296
298}
299
300static void draw_background()
301{
303}
304
306 ARegion *region,
307 SeqChannelDrawContext *r_context)
308{
309 r_context->C = C;
310 r_context->area = CTX_wm_area(C);
311 r_context->region = region;
312 r_context->v2d = &region->v2d;
313 r_context->scene = CTX_data_sequencer_scene(C);
314 r_context->ed = seq::editing_get(r_context->scene);
315 r_context->seqbase = seq::active_seqbase_get(r_context->ed);
316 r_context->channels = seq::channels_displayed_get(r_context->ed);
318 BLI_assert(r_context->timeline_region != nullptr);
319 r_context->timeline_region_v2d = &r_context->timeline_region->v2d;
320
323 r_context->draw_offset = draw_offset_get(r_context->timeline_region_v2d);
324
325 r_context->scale = min_ff(r_context->channel_height / (U.widget_unit * 0.6), 1);
326}
327
328void draw_channels(const bContext *C, ARegion *region)
329{
332 if (!scene) {
333 return;
334 }
335
336 Editing *ed = seq::editing_get(scene);
337 if (ed == nullptr) {
338 return;
339 }
340
341 SeqChannelDrawContext context;
342 channel_draw_context_init(C, region, &context);
343
344 if (round_fl_to_int(context.channel_height) == 0) {
345 return;
346 }
347
348 UI_view2d_view_ortho(context.v2d);
349
350 draw_channel_headers(&context);
351
353}
354
355} // namespace blender::ed::vse
ScrArea * CTX_wm_area(const bContext *C)
SpaceSeq * CTX_wm_space_seq(const bContext *C)
Scene * CTX_data_sequencer_scene(const bContext *C)
ARegion * BKE_area_find_region_type(const ScrArea *area, int region_type)
Definition screen.cc:846
#define BLI_assert(a)
Definition BLI_assert.h:46
MINLINE int round_fl_to_int(float a)
MINLINE float max_ff(float a, float b)
MINLINE float min_ff(float a, float b)
MINLINE int max_ii(int a, int b)
void BLI_rctf_init(struct rctf *rect, float xmin, float xmax, float ymin, float ymax)
Definition rct.cc:404
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
char * BLI_sprintfN(const char *__restrict format,...) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC ATTR_PRINTF_FORMAT(1
#define CLAMP(a, b, c)
@ RGN_TYPE_WINDOW
void GPU_matrix_push()
void GPU_matrix_pop()
Read Guarded memory(de)allocation.
#define C
Definition RandGen.cpp:29
uiBut * uiDefButR(uiBlock *block, ButType type, int retval, std::optional< blender::StringRef > str, int x, int y, short width, short height, PointerRNA *ptr, blender::StringRefNull propname, int index, float min, float max, std::optional< blender::StringRef > tip)
void UI_block_emboss_set(uiBlock *block, blender::ui::EmbossType emboss)
uiBlock * UI_block_begin(const bContext *C, ARegion *region, std::string name, blender::ui::EmbossType emboss)
void UI_but_func_tooltip_set(uiBut *but, uiButToolTipFunc func, void *arg, uiFreeArgFunc free_arg)
const uiStyle * UI_style_get_dpi()
int UI_fontstyle_height_max(const uiFontStyle *fs)
uiBut * uiDefIconButR_prop(uiBlock *block, ButType type, int retval, int icon, int x, int y, short width, short height, PointerRNA *ptr, PropertyRNA *prop, int index, float min, float max, std::optional< blender::StringRef > tip)
void UI_block_draw(const bContext *C, uiBlock *block)
bool UI_but_active_only(const bContext *C, ARegion *region, uiBlock *block, uiBut *but)
void UI_block_end(const bContext *C, uiBlock *block)
uiBut * uiDefBut(uiBlock *block, uiButTypeWithPointerType but_and_ptr_type, int retval, blender::StringRef str, int x, int y, short width, short height, void *poin, float min, float max, std::optional< blender::StringRef > tip)
@ TH_BACK
void UI_ThemeClearColor(int colorid)
void UI_view2d_view_restore(const bContext *C)
Definition view2d.cc:1162
float UI_view2d_view_to_region_y(const View2D *v2d, float y)
Definition view2d.cc:1696
void UI_view2d_view_ortho(const View2D *v2d)
Definition view2d.cc:1095
float UI_view2d_view_to_region_x(const View2D *v2d, float x)
Definition view2d.cc:1691
#define ND_SEQUENCER
Definition WM_types.hh:437
#define NC_SCENE
Definition WM_types.hh:378
#define U
#define floor
#define ceil
void MEM_freeN(void *vmemh)
Definition mallocn.cc:113
static std::string draw_channel_widget_tooltip(bContext *, void *argN, const blender::StringRef)
static bool channel_is_being_renamed(const SpaceSeq *sseq, const int channel_index)
static float icon_width_get(const SeqChannelDrawContext *context)
static void draw_channel_headers(const SeqChannelDrawContext *context)
void channel_draw_context_init(const bContext *C, ARegion *region, SeqChannelDrawContext *r_context)
static float draw_offset_get(const View2D *timeline_region_v2d)
static float text_size_get(const SeqChannelDrawContext *context)
static float draw_channel_widget_mute(const SeqChannelDrawContext *context, uiBlock *block, const int channel_index, const float offset)
static float frame_width_pixelspace_get(const View2D *timeline_region_v2d)
static float draw_channel_widget_lock(const SeqChannelDrawContext *context, uiBlock *block, const int channel_index, const float offset)
static float widget_y_offset(const SeqChannelDrawContext *context)
static void displayed_channel_range_get(const SeqChannelDrawContext *context, int r_channel_range[2])
static rctf label_rect_init(const SeqChannelDrawContext *context, const int channel_index, const float used_width)
void draw_channels(const bContext *C, ARegion *region)
static float channel_height_pixelspace_get(const View2D *timeline_region_v2d)
static float channel_index_y_min(const SeqChannelDrawContext *context, const int index)
static void draw_channel_labels(const SeqChannelDrawContext *context, uiBlock *block, const int channel_index, const float used_width)
SeqTimelineChannel * channel_get_by_index(const ListBase *channels, const int channel_index)
Definition channels.cc:60
ListBase * channels_displayed_get(const Editing *ed)
Definition channels.cc:28
bool channel_is_locked(const SeqTimelineChannel *channel)
Definition channels.cc:76
Editing * editing_get(const Scene *scene)
Definition sequencer.cc:286
constexpr int MAX_CHANNELS
void timeline_expand_boundbox(const Scene *scene, const ListBase *seqbase, rctf *rect)
char * channel_name_get(ListBase *channels, const int channel_index)
Definition channels.cc:65
bool channel_is_muted(const SeqTimelineChannel *channel)
Definition channels.cc:81
ListBase * active_seqbase_get(const Editing *ed)
Definition sequencer.cc:433
PropertyRNA * RNA_struct_type_find_property(StructRNA *srna, const char *identifier)
PropertyRNA * RNA_struct_name_property(const StructRNA *type)
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
const char * RNA_property_identifier(const PropertyRNA *prop)
SpaceSeq_Runtime * runtime
float xmax
float xmin
float ymax
float ymin
uiFontStyle widget
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
PointerRNA * ptr
Definition wm_files.cc:4238
void wmOrtho2_pixelspace(const float x, const float y)