Welcome to mirror list, hosted at ThFree Co, Russian Federation.

sequencer_channels_draw.c « space_sequencer « editors « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 99a305fcf66dc1811a183425cc0448b1722750c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/* SPDX-License-Identifier: GPL-2.0-or-later
 * Copyright 2022 Blender Foundation. All rights reserved. */

/** \file
 * \ingroup sequencer
 */

#include "DNA_scene_types.h"
#include "DNA_screen_types.h"

#include "BKE_context.h"

#include "BLI_blenlib.h"
#include "BLI_utildefines.h"

#include "ED_screen.h"

#include "GPU_framebuffer.h"
#include "GPU_immediate.h"
#include "GPU_immediate_util.h"
#include "GPU_matrix.h"
#include "GPU_state.h"
#include "GPU_vertex_buffer.h"
#include "GPU_viewport.h"

#include "RNA_access.h"
#include "RNA_prototypes.h"

#include "SEQ_channels.h"
#include "SEQ_sequencer.h"
#include "SEQ_time.h"

#include "UI_interface.h"
#include "UI_resources.h"
#include "UI_view2d.h"

#include "WM_api.h"

/* Own include. */
#include "sequencer_intern.h"

static ARegion *timeline_region_get(ScrArea *area)
{
  LISTBASE_FOREACH (ARegion *, region, &area->regionbase) {
    if (region->regiontype == RGN_TYPE_WINDOW) {
      return region;
    }
  }

  BLI_assert_unreachable();
  return NULL;
}

static float draw_offset_get(View2D *timeline_region_v2d)
{
  return timeline_region_v2d->cur.ymin;
}

static float channel_height_pixelspace_get(View2D *timeline_region_v2d)
{
  return UI_view2d_view_to_region_y(timeline_region_v2d, 1.0f) -
         UI_view2d_view_to_region_y(timeline_region_v2d, 0.0f);
}

static float frame_width_pixelspace_get(View2D *timeline_region_v2d)
{

  return UI_view2d_view_to_region_x(timeline_region_v2d, 1.0f) -
         UI_view2d_view_to_region_x(timeline_region_v2d, 0.0f);
}

static float icon_width_get(SeqChannelDrawContext *context)
{
  return (U.widget_unit * 0.8 * context->scale);
}

static float widget_y_offset(SeqChannelDrawContext *context)
{
  return (((context->channel_height / context->scale) - icon_width_get(context))) / 2;
}

static float channel_index_y_min(SeqChannelDrawContext *context, const int index)
{
  float y = (index - context->draw_offset) * context->channel_height;
  y /= context->scale;
  return y;
}

static void displayed_channel_range_get(SeqChannelDrawContext *context, int channel_range[2])
{
  /* Channel 0 is not usable, so should never be drawn. */
  channel_range[0] = max_ii(1, floor(context->timeline_region_v2d->cur.ymin));
  channel_range[1] = ceil(context->timeline_region_v2d->cur.ymax);

  rctf strip_boundbox;
  BLI_rctf_init(&strip_boundbox, 0.0f, 0.0f, 1.0f, channel_range[1]);
  SEQ_timeline_expand_boundbox(context->seqbase, &strip_boundbox);
  CLAMP(channel_range[0], strip_boundbox.ymin, strip_boundbox.ymax);
  CLAMP(channel_range[1], strip_boundbox.ymin, MAXSEQ);
}

static float draw_channel_widget_hide(SeqChannelDrawContext *context,
                                      uiBlock *block,
                                      int channel_index,
                                      float offset)
{
  float y = channel_index_y_min(context, channel_index) + widget_y_offset(context);

  const float width = icon_width_get(context);
  SeqTimelineChannel *channel = SEQ_channel_get_by_index(context->channels, channel_index);
  const int icon = SEQ_channel_is_muted(channel) ? ICON_CHECKBOX_DEHLT : ICON_CHECKBOX_HLT;
  const char *tooltip = BLI_sprintfN(
      "%s channel %d", SEQ_channel_is_muted(channel) ? "Unmute" : "Mute", channel_index);

  PointerRNA ptr;
  RNA_pointer_create(&context->scene->id, &RNA_SequenceTimelineChannel, channel, &ptr);
  PropertyRNA *hide_prop = RNA_struct_type_find_property(&RNA_SequenceTimelineChannel, "mute");

  UI_block_emboss_set(block, UI_EMBOSS_NONE);
  uiDefIconButR_prop(block,
                     UI_BTYPE_TOGGLE,
                     1,
                     icon,
                     context->v2d->cur.xmax / context->scale - offset,
                     y,
                     width,
                     width,
                     &ptr,
                     hide_prop,
                     0,
                     0,
                     0,
                     0,
                     0,
                     tooltip);

  return width;
}

static float draw_channel_widget_lock(SeqChannelDrawContext *context,
                                      uiBlock *block,
                                      int channel_index,
                                      float offset)
{

  float y = channel_index_y_min(context, channel_index) + widget_y_offset(context);
  const float width = icon_width_get(context);

  SeqTimelineChannel *channel = SEQ_channel_get_by_index(context->channels, channel_index);
  const int icon = SEQ_channel_is_locked(channel) ? ICON_LOCKED : ICON_UNLOCKED;
  const char *tooltip = BLI_sprintfN(
      "%s channel %d", SEQ_channel_is_locked(channel) ? "Unlock" : "Lock", channel_index);

  PointerRNA ptr;
  RNA_pointer_create(&context->scene->id, &RNA_SequenceTimelineChannel, channel, &ptr);
  PropertyRNA *hide_prop = RNA_struct_type_find_property(&RNA_SequenceTimelineChannel, "lock");

  UI_block_emboss_set(block, UI_EMBOSS_NONE);
  uiDefIconButR_prop(block,
                     UI_BTYPE_TOGGLE,
                     1,
                     icon,
                     context->v2d->cur.xmax / context->scale - offset,
                     y,
                     width,
                     width,
                     &ptr,
                     hide_prop,
                     0,
                     0,
                     0,
                     0,
                     0,
                     tooltip);

  return width;
}

static bool channel_is_being_renamed(SpaceSeq *sseq, int channel_index)
{
  return sseq->runtime.rename_channel_index == channel_index;
}

static float text_size_get(SeqChannelDrawContext *context)
{
  const uiStyle *style = UI_style_get_dpi();
  return UI_fontstyle_height_max(&style->widget) * 1.5f * context->scale;
}

/* Todo: decide what gets priority - label or buttons */
static void label_rect_init(SeqChannelDrawContext *context,
                            int channel_index,
                            float used_width,
                            rctf *r_rect)
{
  float text_size = text_size_get(context);
  float margin = (context->channel_height / context->scale - text_size) / 2.0f;
  float y = channel_index_y_min(context, channel_index) + margin;

  float margin_x = icon_width_get(context) * 0.65;
  float width = max_ff(0.0f, context->v2d->cur.xmax / context->scale - used_width);

  /* Text input has own margin. Prevent text jumping around and use as much space as possible. */
  if (channel_is_being_renamed(CTX_wm_space_seq(context->C), channel_index)) {
    float input_box_margin = icon_width_get(context) * 0.5f;
    margin_x -= input_box_margin;
    width += input_box_margin;
  }

  BLI_rctf_init(r_rect, margin_x, margin_x + width, y, y + text_size);
}

static void draw_channel_labels(SeqChannelDrawContext *context,
                                uiBlock *block,
                                int channel_index,
                                float used_width)
{
  SpaceSeq *sseq = CTX_wm_space_seq(context->C);
  rctf rect;
  label_rect_init(context, channel_index, used_width, &rect);

  if (BLI_rctf_size_y(&rect) <= 1.0f || BLI_rctf_size_x(&rect) <= 1.0f) {
    return;
  }

  if (channel_is_being_renamed(sseq, channel_index)) {
    SeqTimelineChannel *channel = SEQ_channel_get_by_index(context->channels, channel_index);
    PointerRNA ptr = {NULL};
    RNA_pointer_create(&context->scene->id, &RNA_SequenceTimelineChannel, channel, &ptr);
    PropertyRNA *prop = RNA_struct_name_property(ptr.type);

    UI_block_emboss_set(block, UI_EMBOSS);
    uiBut *but = uiDefButR(block,
                           UI_BTYPE_TEXT,
                           1,
                           "",
                           rect.xmin,
                           rect.ymin,
                           BLI_rctf_size_x(&rect),
                           BLI_rctf_size_y(&rect),
                           &ptr,
                           RNA_property_identifier(prop),
                           -1,
                           0,
                           0,
                           0,
                           0,
                           NULL);
    UI_block_emboss_set(block, UI_EMBOSS_NONE);

    if (UI_but_active_only(context->C, context->region, block, but) == false) {
      sseq->runtime.rename_channel_index = 0;
    }

    WM_event_add_notifier(context->C, NC_SCENE | ND_SEQUENCER, context->scene);
  }
  else {
    const char *label = SEQ_channel_name_get(context->channels, channel_index);
    uiDefBut(block,
             UI_BTYPE_LABEL,
             0,
             label,
             rect.xmin,
             rect.ymin,
             rect.xmax - rect.xmin,
             (rect.ymax - rect.ymin),
             NULL,
             0,
             0,
             0,
             0,
             NULL);
  }
}

/* Todo: different text/buttons alignment */
static void draw_channel_header(SeqChannelDrawContext *context, uiBlock *block, int channel_index)
{
  float offset = icon_width_get(context) * 1.5f;
  offset += draw_channel_widget_lock(context, block, channel_index, offset);
  offset += draw_channel_widget_hide(context, block, channel_index, offset);

  draw_channel_labels(context, block, channel_index, offset);
}

static void draw_channel_headers(SeqChannelDrawContext *context)
{
  GPU_matrix_push();
  wmOrtho2_pixelspace(context->region->winx / context->scale,
                      context->region->winy / context->scale);
  uiBlock *block = UI_block_begin(context->C, context->region, __func__, UI_EMBOSS);

  int channel_range[2];
  displayed_channel_range_get(context, channel_range);

  for (int channel = channel_range[0]; channel <= channel_range[1]; channel++) {
    draw_channel_header(context, block, channel);
  }

  UI_block_end(context->C, block);
  UI_block_draw(context->C, block);

  GPU_matrix_pop();
}

static void draw_background(SeqChannelDrawContext *context)
{
  UI_ThemeClearColor(TH_BACK);
}

void channel_draw_context_init(const bContext *C,
                               ARegion *region,
                               SeqChannelDrawContext *r_context)
{
  r_context->C = C;
  r_context->area = CTX_wm_area(C);
  r_context->region = region;
  r_context->v2d = &region->v2d;
  r_context->scene = CTX_data_scene(C);
  r_context->ed = SEQ_editing_get(r_context->scene);
  r_context->seqbase = SEQ_active_seqbase_get(r_context->ed);
  r_context->channels = SEQ_channels_active_get(r_context->ed);
  r_context->timeline_region = timeline_region_get(CTX_wm_area(C));
  r_context->timeline_region_v2d = &r_context->timeline_region->v2d;

  r_context->channel_height = channel_height_pixelspace_get(r_context->timeline_region_v2d);
  r_context->frame_width = frame_width_pixelspace_get(r_context->timeline_region_v2d);
  r_context->draw_offset = draw_offset_get(r_context->timeline_region_v2d);

  r_context->scale = min_ff(r_context->channel_height / (U.widget_unit * 0.6), 1);
}

void draw_channels(const bContext *C, ARegion *region)
{
  SeqChannelDrawContext context;
  channel_draw_context_init(C, region, &context);

  UI_view2d_view_ortho(context.v2d);

  draw_background(&context);
  draw_channel_headers(&context);

  UI_view2d_view_restore(C);
}