/
ImageSequencerCanvas.cs
571 lines (483 loc) · 22 KB
/
ImageSequencerCanvas.cs
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
using UnityEngine;
using System.Linq;
using System;
namespace UnityEditor.VFXToolbox.ImageSequencer
{
internal class ImageSequencerCanvas : VFXToolboxCanvas
{
public bool showExtraInfo
{
get
{
return m_bShowExtraInfo;
}
set
{
m_bShowExtraInfo = value;
}
}
public int numFrames
{
get
{
return m_PreviewSequence.length;
}
}
public ProcessingFrameSequence sequence
{
get
{
return m_PreviewSequence;
}
set
{
m_PreviewSequence = value;
}
}
public ProcessingFrame currentFrame
{
get {
return m_ProcessingFrame;
}
set {
m_ProcessingFrame = value;
if(value != null)
InvalidateRenderTarget();
}
}
public int currentFrameIndex
{
get
{
return m_CurrentFrame;
}
set
{
if (m_CurrentFrame != value)
{
m_CurrentFrame = value;
UpdateCanvasSequence();
}
}
}
public bool isPlaying
{
get
{
return m_IsPlaying;
}
}
private bool m_bShowExtraInfo = false;
private int m_CurrentFrame = 0;
private ProcessingFrameSequence m_PreviewSequence;
private ProcessingFrame m_ProcessingFrame;
private Rect m_PlayControlsRect;
private ImageSequencer m_ImageSequencerWindow;
private bool m_IsPlaying = false;
private float m_PlayFramerate = 30.0f;
private float m_PlayTime = 0.0f;
private double m_EditorTime;
private bool m_IsScrobbing;
public ImageSequencerCanvas(Rect displayRect, ImageSequencer editorWindow)
:base(displayRect)
{
m_ImageSequencerWindow = editorWindow;
m_IsScrobbing = false;
m_PlayControlsRect = new Rect(16, 16, 420, 26);
}
public override void Invalidate(bool needRedraw)
{
base.Invalidate(needRedraw);
m_ImageSequencerWindow.Invalidate();
}
protected override void SetTexture(Texture tex)
{
// Never should Happen
throw new NotImplementedException();
}
protected override Texture GetTexture()
{
if (currentFrame != null)
{
return currentFrame.texture;
}
else
return null;
}
protected override int GetMipMapCount()
{
if (currentFrame != null)
return currentFrame.mipmapCount;
else
return 0;
}
protected override void HandleKeyboardEvents()
{
base.HandleKeyboardEvents();
if(Event.current.type == EventType.KeyDown)
{
switch(Event.current.keyCode)
{
// Viewport Toggles
case KeyCode.H:
showExtraInfo = !showExtraInfo;
break;
// Play Controls
case KeyCode.Space:
TogglePlaySequence();
break;
case KeyCode.LeftArrow:
if (Event.current.shift)
FirstFrame();
else
PreviousFrame();
break;
case KeyCode.RightArrow:
if (Event.current.shift)
LastFrame();
else
NextFrame();
break;
default:
return; // Return without using event.
}
Invalidate(false);
Event.current.Use();
}
}
protected override void DrawGrid()
{
int GridNumU = m_PreviewSequence.numU;
int GridNumV = m_PreviewSequence.numV;
Vector2 src, dst;
float v;
Texture texture = currentFrame.texture;
if(BackgroundBrightness < 0.5f)
Handles.color = new Color(1.0f,1.0f,1.0f,0.33333f);
else
Handles.color = new Color(0.0f,0.0f,0.0f,0.66666f);
for(int i = 0; i <= GridNumV; i++)
{
v = -(texture.height * 0.5f) + (float)i / GridNumV * texture.height;
src = CanvasToScreen(new Vector2(-texture.width * 0.5f, v));
dst = CanvasToScreen(new Vector2(texture.width * 0.5f, v));
Handles.DrawLine(src, dst);
}
for(int j = 0; j <= GridNumU; j++)
{
v = -(texture.width * 0.5f) + (float)j / GridNumU * texture.width;
src = CanvasToScreen(new Vector2(v,-texture.height * 0.5f));
dst = CanvasToScreen(new Vector2(v, texture.height * 0.5f));
Handles.DrawLine(src, dst);
}
Handles.color = Color.white;
}
public void OnGUI(ImageSequencer editor)
{
OnGUI();
// Processor extra info
GUI.BeginGroup(m_Rect);
if (editor.currentProcessor != null && editor.currentProcessor.Enabled && m_bShowExtraInfo && editor.sidePanelViewMode == ImageSequencer.SidePanelMode.Processors)
editor.currentProcessor.OnCanvasGUI(this);
GUI.EndGroup();
// Everytime text
string procName = (editor.sidePanelViewMode == ImageSequencer.SidePanelMode.Export) ? "Export" : (sequence.processor == null ? "Input Frames" : sequence.processor.ToString());
GUI.Label(new RectOffset(24,24,24,24).Remove(m_Rect), procName , styles.largeLabel);
GUI.Label(new RectOffset(24,24,64,24).Remove(m_Rect), GetDebugInfoString() , styles.label);
//EditorGUI.DrawRect(m_Rect, Color.red);
// Play Controls
if(sequence != null && sequence.length > 1)
{
DrawSequenceControls(displayRect, editor);
}
if (m_IsPlaying)
UpdatePlay();
}
private GUIContent GetDebugInfoString()
{
string output = "";
if(m_ProcessingFrame != null)
{
output += "Frame Size : " + m_ProcessingFrame.texture.width + " x " + m_ProcessingFrame.texture.height;
output += "\nMipMaps : " + m_ProcessingFrame.mipmapCount;
}
return new GUIContent(output);
}
public void DrawSequenceControls(Rect ViewportArea, ImageSequencer editor)
{
m_PlayControlsRect = new Rect(ViewportArea.x , (ViewportArea.y + ViewportArea.height), ViewportArea.width , 100);
using (new GUILayout.AreaScope(m_PlayControlsRect, GUIContent.none, ImageSequencer.styles.playbackControlWindow))
{
Rect area = new Rect(16,16,m_PlayControlsRect.width-32,m_PlayControlsRect.height-32);
//GUILayout.BeginArea(area);
using (new GUILayout.VerticalScope())
{
// TRACKBAR
int count = sequence.length;
GUILayout.Space(16); // Reserve Layout for labels
Rect bar_rect = GUILayoutUtility.GetRect(area.width, 16);
EditorGUIUtility.AddCursorRect(bar_rect, MouseCursor.ResizeHorizontal);
if(Event.current.type == EventType.MouseDown && bar_rect.Contains(Event.current.mousePosition))
{
m_IsScrobbing = true;
}
if(Event.current.type == EventType.MouseUp || Event.current.rawType == EventType.MouseUp)
{
m_IsScrobbing = false;
}
if(m_IsScrobbing && (Event.current.type == EventType.MouseDrag || Event.current.type == EventType.MouseDown))
{
float pos = (Event.current.mousePosition.x - bar_rect.x) / bar_rect.width;
int frame = (int)Mathf.Round(pos * numFrames);
if (frame != currentFrameIndex)
{
currentFrameIndex = frame;
Invalidate(true);
}
}
EditorGUI.DrawRect(bar_rect, ImageSequencer.styles.CookBarDirty);
float width = bar_rect.width / count;
Rect textpos;
for (int i = 0; i < count; i++)
{
if(!sequence.frames[i].dirty)
{
Rect cell = new Rect(bar_rect.x + i * width, bar_rect.y, width, bar_rect.height);
EditorGUI.DrawRect(cell, ImageSequencer.styles.CookBarCooked);
}
if(i == currentFrameIndex)
{
Rect cursor = new Rect(bar_rect.x + i * width, bar_rect.y, width, bar_rect.height);
EditorGUI.DrawRect(cursor, new Color(1.0f,1.0f,1.0f,0.5f));
}
// Labels : Every multiple of 10 based on homemade formula
int step = 10 * (int)Mathf.Max(1,Mathf.Floor(8*(float)count / bar_rect.width));
if( ((i+1) % step) == 0 )
{
textpos = new Rect(bar_rect.x + i * width, bar_rect.y - 16, 32, 16);
GUI.Label(textpos, (i+1).ToString(), EditorStyles.largeLabel);
Rect cursor = new Rect(bar_rect.x + i * width, bar_rect.y, 1, bar_rect.height);
EditorGUI.DrawRect(cursor, new Color(1.0f,1.0f,1.0f,0.2f));
}
}
// Labels : First
textpos = new Rect(bar_rect.x, bar_rect.y - 16, 32, 16);
GUI.Label(textpos, VFXToolboxGUIUtility.Get("1"), EditorStyles.largeLabel);
GUILayout.Space(16);
// PLAY CONTROLS
bool lastplay;
using (new GUILayout.HorizontalScope(EditorStyles.toolbar))
{
lastplay = m_IsPlaying;
if(GUILayout.Button(ImageSequencer.styles.iconFirst, VFXToolboxStyles.toolbarButton, GUILayout.Width(32)))
{
FirstFrame();
}
if(GUILayout.Button(ImageSequencer.styles.iconBack, VFXToolboxStyles.toolbarButton, GUILayout.Width(24)))
{
PreviousFrame();
}
bool playing = GUILayout.Toggle(m_IsPlaying,ImageSequencer.styles.iconPlay, VFXToolboxStyles.toolbarButton, GUILayout.Width(24));
if(m_IsPlaying != playing)
{
TogglePlaySequence();
}
if(GUILayout.Button(ImageSequencer.styles.iconForward, VFXToolboxStyles.toolbarButton, GUILayout.Width(24)))
{
NextFrame();
}
if(GUILayout.Button(ImageSequencer.styles.iconLast, VFXToolboxStyles.toolbarButton, GUILayout.Width(32)))
{
LastFrame();
}
if (lastplay != m_IsPlaying)
{
m_EditorTime = EditorApplication.timeSinceStartup;
}
GUILayout.FlexibleSpace();
GUILayout.Label(VFXToolboxGUIUtility.GetTextAndIcon("Frame : ","Profiler.Record"), VFXToolboxStyles.toolbarButton);
m_CurrentFrame = Mathf.Clamp(EditorGUILayout.IntField(m_CurrentFrame+1, VFXToolboxStyles.toolbarTextField, GUILayout.Width(42))-1,0,numFrames-1);
GUILayout.Label(" on " + numFrames + " ( TCR : " + GetTCR(m_CurrentFrame, (int)m_PlayFramerate) + " ) " , VFXToolboxStyles.toolbarButton);
GUILayout.FlexibleSpace();
ShowFrameratePopup();
}
}
//GUILayout.EndArea();
}
}
public void UpdateCanvasSequence()
{
int length;
if (sequence.processor != null)
length = sequence.processor.GetProcessorSequenceLength();
else
length = sequence.length;
if (length > 0)
{
currentFrameIndex = Mathf.Clamp(currentFrameIndex, 0, length - 1);
currentFrame = sequence.RequestFrame(currentFrameIndex);
}
else
currentFrame = null;
}
#region PLAY CONTROLS
private void TogglePlaySequence()
{
if(m_IsPlaying)
StopSequence();
else
PlaySequence();
}
private void PlaySequence()
{
m_IsPlaying = true;
m_PlayTime = currentFrameIndex / m_PlayFramerate;
m_EditorTime = EditorApplication.timeSinceStartup;
}
private void StopSequence()
{
m_IsPlaying = false;
}
private void NextFrame()
{
int frame = currentFrameIndex + 1;
if (frame >= numFrames) frame = 0;
currentFrameIndex = frame;
}
private void PreviousFrame()
{
int frame = currentFrameIndex - 1;
if (frame < 0) frame = numFrames - 1;
currentFrameIndex = frame;
}
private void FirstFrame()
{
currentFrameIndex = 0;
m_PlayTime = 0;
}
private void LastFrame()
{
currentFrameIndex = numFrames - 1;
m_PlayTime = (numFrames - 1) / m_PlayFramerate ;
}
private string GetTCR(int frame, int framerate)
{
int frames = frame % framerate;
int seconds = frame / framerate;
int minutes = seconds / 60;
seconds %= 60;
minutes %= 60;
int numbering = (int)Mathf.Max(2,Mathf.Floor(Mathf.Log10(framerate))+1); // Minimum 2 digits
return minutes.ToString("D2") + ":" + seconds.ToString("D2") + ":" + frames.ToString("D"+numbering);
}
private void ShowFrameratePopup()
{
if(GUILayout.Button(VFXToolboxGUIUtility.GetTextAndIcon("Speed","SpeedScale"),EditorStyles.toolbarDropDown))
{
GenericMenu menu = new GenericMenu();
menu.AddItem(VFXToolboxGUIUtility.Get("5 fps"),false, () =>{ m_PlayFramerate = 5; });
menu.AddItem(VFXToolboxGUIUtility.Get("10 fps"),false, () =>{ m_PlayFramerate = 10; });
menu.AddItem(VFXToolboxGUIUtility.Get("15 fps"),false, () =>{ m_PlayFramerate = 15; });
menu.AddItem(VFXToolboxGUIUtility.Get("20 fps"),false, () =>{ m_PlayFramerate = 20; });
menu.AddItem(VFXToolboxGUIUtility.Get("24 fps (Cine)"),false, () =>{ m_PlayFramerate = 24; });
menu.AddItem(VFXToolboxGUIUtility.Get("25 fps (PAL)"),false, () =>{ m_PlayFramerate = 25; });
menu.AddItem(VFXToolboxGUIUtility.Get("29.97 fps (NTSC)"),false, () =>{ m_PlayFramerate = 29.97f; });
menu.AddItem(VFXToolboxGUIUtility.Get("30 fps"),false, () =>{ m_PlayFramerate = 30; });
menu.AddItem(VFXToolboxGUIUtility.Get("50 fps"),false, () =>{ m_PlayFramerate = 50; });
menu.AddItem(VFXToolboxGUIUtility.Get("60 fps"),false, () =>{ m_PlayFramerate = 60; });
menu.ShowAsContext();
}
m_PlayFramerate = EditorGUILayout.FloatField(m_PlayFramerate, VFXToolboxStyles.toolbarTextField,GUILayout.Width(24));
EditorGUILayout.LabelField(VFXToolboxGUIUtility.Get("fps"),GUILayout.Width(24));
}
private void UpdatePlay()
{
double deltaTime = EditorApplication.timeSinceStartup - m_EditorTime;
m_PlayTime += (float)deltaTime;
m_PlayTime = m_PlayTime % ((1.0f / m_PlayFramerate) * numFrames);
currentFrameIndex = (int)Mathf.Floor(m_PlayTime*m_PlayFramerate);
m_EditorTime = EditorApplication.timeSinceStartup;
}
#endregion
#region STYLES
public new class Styles
{
public GUIStyle miniLabel
{
get { return m_Canvas.BackgroundBrightness < 0.5f ? m_ViewportMiniLabel : m_ViewportMiniLabelDark; }
}
public GUIStyle miniLabelRight
{
get { return m_Canvas.BackgroundBrightness < 0.5f ? m_ViewportMiniLabelRight : m_ViewportMiniLabelRightDark; }
}
public GUIStyle miniLabelCenter
{
get { return m_Canvas.BackgroundBrightness < 0.5f ? m_ViewportMiniLabelCenter : m_ViewportMiniLabelCenterDark; }
}
public GUIStyle label
{
get { return m_Canvas.BackgroundBrightness < 0.5f ? m_ViewportLabel : m_ViewportLabelDark; }
}
public GUIStyle largeLabel
{
get { return m_Canvas.BackgroundBrightness < 0.5f ? m_ViewportLargeLabel : m_ViewportLargeLabelDark; }
}
public Color backgroundPanelColor
{
get { return m_Canvas.BackgroundBrightness < 0.5f ? m_BackgroundPanelColor : m_BackgroundPanelColorDark; }
}
public Color red { get { return m_Canvas.BackgroundBrightness < 0.5f ? new Color(1, 0, 0, 1) : new Color(0.7f, 0, 0, 1); } }
public Color green { get { return m_Canvas.BackgroundBrightness < 0.5f ? new Color(0, 1, 0, 1) : new Color(0, 0.5f, 0, 1); } }
public Color blue { get { return m_Canvas.BackgroundBrightness < 0.5f ? new Color(0, 0, 1, 1) : new Color(0, 0, 0.5f, 1); } }
public Color white { get { return m_Canvas.BackgroundBrightness < 0.5f ? new Color(1, 1, 1, 1) : new Color(0, 0, 0, 1); } }
public Color black { get { return m_Canvas.BackgroundBrightness < 0.5f ? new Color(0, 0, 0, 1) : new Color(1, 1, 1, 1); } }
public Color yellow { get { return m_Canvas.BackgroundBrightness < 0.5f ? new Color(1.0f, 0.8f, 0.25f) : new Color(0.5f, 0.4f, 0.1f); } }
public Color cyan { get { return m_Canvas.BackgroundBrightness < 0.5f ? new Color(0.25f, 0.8f, 1.0f) : new Color(0.1f, 0.4f, 0.5f); } }
public Color fadewhite { get { return m_Canvas.BackgroundBrightness < 0.5f ? new Color(1, 1, 1, 0.25f) : new Color(0, 0, 0, 0.25f); } }
private GUIStyle m_ViewportMiniLabel;
private GUIStyle m_ViewportMiniLabelDark;
private GUIStyle m_ViewportMiniLabelRight;
private GUIStyle m_ViewportMiniLabelRightDark;
private GUIStyle m_ViewportMiniLabelCenter;
private GUIStyle m_ViewportMiniLabelCenterDark;
private GUIStyle m_ViewportLabel;
private GUIStyle m_ViewportLabelDark;
private GUIStyle m_ViewportLargeLabel;
private GUIStyle m_ViewportLargeLabelDark;
private ImageSequencerCanvas m_Canvas;
private Color m_BackgroundPanelColor;
private Color m_BackgroundPanelColorDark;
public Styles(ImageSequencerCanvas canvas)
{
m_Canvas = canvas;
Color lightGray = new Color(0.8f, 0.8f, 0.8f, 1.0f);
Color darkGray = new Color(0.2f, 0.2f, 0.2f, 1.0f);
m_ViewportMiniLabel = new GUIStyle(EditorStyles.miniLabel);
m_ViewportMiniLabel.normal.textColor = lightGray;
m_ViewportMiniLabelDark = new GUIStyle(EditorStyles.miniLabel);
m_ViewportMiniLabelDark.normal.textColor = darkGray;
m_ViewportMiniLabelRight = new GUIStyle(m_ViewportMiniLabel);
m_ViewportMiniLabelRight.alignment = TextAnchor.MiddleRight;
m_ViewportMiniLabelRightDark = new GUIStyle(m_ViewportMiniLabelDark);
m_ViewportMiniLabelRightDark.alignment = TextAnchor.MiddleRight;
m_ViewportMiniLabelCenter = new GUIStyle(m_ViewportMiniLabel);
m_ViewportMiniLabelCenter.alignment = TextAnchor.MiddleCenter;
m_ViewportMiniLabelCenterDark = new GUIStyle(m_ViewportMiniLabelDark);
m_ViewportMiniLabelCenterDark.alignment = TextAnchor.MiddleCenter;
m_ViewportLabel = new GUIStyle(EditorStyles.largeLabel);
m_ViewportLabel.normal.textColor = lightGray;
m_ViewportLabelDark = new GUIStyle(EditorStyles.largeLabel);
m_ViewportLabelDark.normal.textColor = darkGray;
m_ViewportLargeLabel = new GUIStyle(EditorStyles.largeLabel);
m_ViewportLargeLabel.fontSize = 24;
m_ViewportLargeLabel.normal.textColor = lightGray;
m_ViewportLargeLabelDark = new GUIStyle(EditorStyles.largeLabel);
m_ViewportLargeLabelDark.fontSize = 24;
m_ViewportLargeLabelDark.normal.textColor = darkGray;
m_BackgroundPanelColor = new Color(0.02f, 0.02f, 0.02f, 0.85f);
m_BackgroundPanelColorDark = new Color(0.5f, 0.5f, 0.5f, 0.5f);
}
}
#endregion
}
}