private void CutsceneEngine_TimeLabelDataEnter(object sender, TimeLabelData label) { CutsceneEngine ce = sender as CutsceneEngine; float currentTime = ce.Time; Debug.Log(ce.Time + ": Time label data entered - " + label.Name); }
private void HandleLabelAdd(object obj) { TimeSpan coursorTime = _timeLineEngine.Time; TimeSpan duration = new TimeSpan(0, 0, 10); if (obj != null && obj is TimeLabelData) { duration = (obj as TimeLabelData).Duration; } foreach (TimeLabel timeLabel in _labels) { if (coursorTime >= timeLabel.SourceData.Start && coursorTime < timeLabel.SourceData.End) { EditorUtility.DisplayDialog("Error!", "You cannot add time label on the occupied time.", "Ok"); return; } if (coursorTime < timeLabel.SourceData.Start && coursorTime + duration > timeLabel.SourceData.Start) { TimeSpan gap = timeLabel.SourceData.Start - coursorTime; if (gap < duration) { duration = gap; } } } if (coursorTime + duration > _timeLineEngine.Duration) { duration = _timeLineEngine.Duration - coursorTime; } TimeLabelData data; if (obj != null && obj is TimeLabelData) { data = new TimeLabelData(obj as TimeLabelData); } else { data = new TimeLabelData() { Name = "Time label", Color = Color.cyan }; } data.Start = coursorTime; data.End = coursorTime + duration; TimeLabel newTimeLabel = new TimeLabel(this, data); newTimeLabel.TimeEnter += ((TimedData timeLabelData) => { if (LabelTimeEnter != null) { LabelTimeEnter.Invoke(timeLabelData as TimeLabelData); } }); newTimeLabel.TimeExit += ((TimedData timeLabelData) => { if (LabelTimeExit != null) { LabelTimeExit.Invoke(timeLabelData as TimeLabelData); } }); _labels.Add(newTimeLabel); if (LabelCreate != null) { LabelCreate.Invoke(data); } }
/// <summary> Draws the component [have to be invoked in OnGUI].</summary> /// <param name="rect">X position for the timeline.</param> /// <param name="scrollDelta">Scroll delta.</param> /// <returns>Real component size.</returns> public Rect GUIDraw(float xPos, float scrollDelta) { base.Draw(_rect, _rectWasChanged); if (_rect.Contains(Event.current.mousePosition)) { if (Event.current.type == EventType.ContextClick) { GenericMenu menu = new GenericMenu(); menu.AddItem(new GUIContent("Add"), false, HandleLabelAdd, null); if (CopyDataAvailable) { menu.AddItem(new GUIContent("Paste"), false, HandleLabelPaste, null); } else { menu.AddItem(new GUIContent("Paste"), false, null, null); } menu.ShowAsContext(); } if (Event.current.type == EventType.MouseDown && Event.current.button == 0) { _wasMouseDown = true; } if (Event.current.type == EventType.MouseDrag) { foreach (Label label in _labels) { if (label.OnDrag) { _wasMouseDown = false; break; } } } if (_wasMouseDown && (Event.current.type == EventType.MouseDrag || Event.current.type == EventType.MouseUp)) { _timeLineEngine.SetTime(WorldXToTime(Event.current.mousePosition.x)); } if (Event.current.type == EventType.MouseUp) { _wasMouseDown = false; } } if (_strokeTexture == null || _durationWasChanged || _rectWasChanged || _lastXPos != xPos || scrollDelta != 0 || _zoomChanged) { RecalcScale(_rect.width); _zoomChanged = false; int textureWidth = (int)_rect.width; int textureHeight = (int)_rect.height; if (_rectWasChanged) { _timelineBackgroundColor = new Color[textureHeight * textureWidth]; _timelineBackgroundColor.Fill(new Color(1, 1, 1, 0)); } _strokeTexture = new Texture2D(textureWidth, textureHeight, TextureFormat.ARGB32, false); _strokeTexture.SetPixels(_timelineBackgroundColor); _strokeTextureOryginal = new Texture2D(textureWidth, textureHeight, TextureFormat.ARGB32, false); _smallStroke = (int)GUI.skin.label.CalcSize(new GUIContent("text")).y + 5; _bigStroke = _smallStroke + 10; _lastXPos = xPos; _rectWasChanged = false; _durationWasChanged = false; _GUILabelDatas.Clear(); float significantStepDuration = Mathf.Ceil((_timeLabelSize.x * 1.5f) / _timeLineEngine.PixelsPerSecond) * _timeLineEngine.PixelsPerSecond; float significantStrokeBuffor = (_timeLabelSize.x * 1.5f); Color[] strokeLong = new Color[_bigStroke * 2]; strokeLong.Fill(Color.black); float offset = significantStepDuration - ((xPos / significantStepDuration) - (int)(xPos / significantStepDuration)) * significantStepDuration; int divider = Mathf.CeilToInt((_timeLabelSize.x * 1.5f) / _timeLineEngine.PixelsPerSecond) - 1; if (divider < 2) { divider = 2; } int minorStrokeCount = divider - ((divider - 1) / 5) * 5; // do not simplify! Int type is not by accident! if (minorStrokeCount < 2) { minorStrokeCount = 2; } float step = (_timeLineEngine.PixelsPerSecond * (divider - 1)) / (minorStrokeCount - 1); for (float x = offset - significantStepDuration; x + _labelStrokesWidth * 2 < _rect.width; x += step) { if (significantStrokeBuffor >= (_timeLabelSize.x * 1.5f)) { if (x >= 0) { _strokeTexture.SetPixels((int)x, Mathf.RoundToInt(_controlRect.height - _bigStroke), _labelStrokesWidth * 2, _bigStroke, strokeLong); } TimeSpan strokeTime = new TimeSpan(0, 0, Mathf.RoundToInt((x + xPos) / _timeLineEngine.PixelsPerSecond)); float labelX = TimeToWorldX(strokeTime) - _timeLabelSize.x / 2; if (strokeTime == TimeSpan.Zero) { labelX = TimeToWorldX(strokeTime); } _GUILabelDatas.Add(new GUITimeLabelData(labelX, _controlRect.y + _smallStroke, _timeLabelSize, strokeTime.ToStringSpecial())); significantStrokeBuffor = step; } else { if (x >= 0) { _strokeTexture.SetPixels((int)x, Mathf.RoundToInt(_controlRect.height - _smallStroke), _labelStrokesWidth, _smallStroke, strokeLong); } significantStrokeBuffor += step; } } _GUILabelDatas.Add(new GUITimeLabelData(TimeToWorldX(_timeLineEngine.Duration) - _timeLabelSize.x, _controlRect.y + _smallStroke, _timeLabelSize, _timeLineEngine.Duration.ToStringSpecial())); if (_GUILabelDatas.Count >= 2 && _GUILabelDatas[_GUILabelDatas.Count - 2].Rect.x + _GUILabelDatas[_GUILabelDatas.Count - 2].Rect.width > _GUILabelDatas[_GUILabelDatas.Count - 1].Rect.x) { _GUILabelDatas.RemoveAt(_GUILabelDatas.Count - 2); } _strokeTextureStyle.normal.background = _strokeTexture; foreach (Label label in _labels) { label.Refresh(); } _strokeTexture.Apply(); _strokeTextureOryginal.SetPixels(_strokeTexture.GetPixels()); } bool redrawRequest = false; // checking if any label need redraw for (int i = _labels.Count - 1; i >= 0; i--) { if (_labels[i].DrawRequest || _labels[i].SourceData.RemoveRequest) { // reset to oryginal if (!redrawRequest) { _strokeTexture.SetPixels(_strokeTextureOryginal.GetPixels()); } redrawRequest = true; // removing requested label if (_labels[i].SourceData.RemoveRequest) { TimeLabelData data = _labels[i].SourceData as TimeLabelData; _labels.RemoveAt(i); if (LabelDelete != null) { LabelDelete.Invoke(data); } } } } if (redrawRequest) { foreach (Label label in _labels) { label.Draw(_strokeTextureOryginal, _strokeTexture); } _strokeTexture.Apply(); } GUI.Box(_rect, GUIContent.none, _strokeTextureStyle); foreach (Label label in _labels) { label.DrawGUI(); } foreach (GUITimeLabelData label in _GUILabelDatas) { label.Draw(_controlRect.y + _bigStroke); } return(_controlRect); }
/// <summary> Create new time bookmark label. </summary> /// <param name="owner"> Owning timeline.></param> /// <param name="sourceData">Source data.</param> public TimeLabel(TimeLinePanel owner, TimeLabelData sourceData) : base(owner, owner, sourceData) { }