public void OnDrawGUI(Rect rect) { foreach (var drawer in actionDrawers) { drawer.OnDrawGUI(rect); } int eventBtn = Event.current.button; EventType eventType = Event.current.type; Vector2 mPos = Event.current.mousePosition; bool isContains = rect.Contains(Event.current.mousePosition); if (isContains && eventType == EventType.MouseUp) { if (eventBtn == 0) { IsSelected = true; Event.current.Use(); } else if (eventBtn == 1) { LineSetting setting = LineSetting.Setting; ActionMenu.ShowMenu((actionData) => { float fireTime = (mPos.x + setting.ScrollPosX) / setting.WidthForSecond; actionData.Index = setting.GetActionIndex(); actionData.FireTime = fireTime; OnActionAdded(actionData); }); IsSelected = true; Event.current.Use(); } } }
private void DrawProperty(Rect propertyRect) { EGUI.DrawAreaLine(propertyRect, Color.gray); LineSetting setting = LineSetting.Setting; Rect titleRect = new Rect(propertyRect.x, propertyRect.y, propertyRect.width, TRACK_TITLE_HEIGHT); EditorGUI.LabelField(titleRect, "Property", EditorStyles.toolbar); if (Data == null) { return; } Rect contentRect = new Rect(propertyRect.x, propertyRect.y + TRACK_TITLE_HEIGHT, propertyRect.width, propertyRect.height - TRACK_TITLE_HEIGHT); GUILayout.BeginArea(contentRect); { Data.TimeLength = EditorGUILayout.FloatField("Time Length", Data.TimeLength); EditorGUILayout.Space(); if (selectedTrackIndex >= 0) { trackDrawers[selectedTrackIndex].OnDrawProperty(contentRect); } } GUILayout.EndArea(); }
void OnGUI() { LineSetting setting = LineSetting.Setting; if (setting == null) { return; } setting.TracklineHeight = EditorGUILayout.IntField("Trackline Height", setting.TracklineHeight); setting.TimeStep = EditorGUILayout.FloatField("Time Step", setting.TimeStep); setting.ZoomTimeStep = EditorGUILayout.FloatField("Zoom Time Step", setting.ZoomTimeStep); setting.WidthForSecond = EditorGUILayout.IntField("Width For Second", setting.WidthForSecond); EditorGUILayout.Space(); EditorGUI.BeginDisabledGroup(true); { EditorGUILayout.FloatField("Time Step Width", setting.TimeStepWidth); EditorGUILayout.Vector2Field("Scroll Pos", setting.ScrollPos); EditorGUILayout.IntField("Max Action Index", setting.MaxActionIndex); if (!string.IsNullOrEmpty(setting.CopiedActionData)) { EditorGUILayout.TextField("Copied Action", setting.CopiedActionData, EditorStyles.wordWrappedLabel, GUILayout.Height(60)); } } EditorGUI.EndDisabledGroup(); }
private void DrawLineRuler(Rect rulerRect) { LineSetting setting = LineSetting.Setting; using (new GUI.ClipScope(rulerRect)) { int start = Mathf.FloorToInt(setting.ScrollPosX / setting.WidthForSecond); int end = Mathf.CeilToInt((setting.ScrollPosX + rulerRect.width) / setting.WidthForSecond); int startCount = Mathf.FloorToInt(start / setting.TimeStep); int endCount = Mathf.FloorToInt(end / setting.TimeStep); for (int i = startCount; i <= endCount; i++) { var x = i * setting.TimeStepWidth - setting.ScrollPosX; if (i % 10 == 0) { Handles.color = new Color(0, 0, 0, 0.8f); Handles.DrawLine(new Vector3(x, 0, 0), new Vector3(x, rulerRect.height * 0.8f, 0)); GUI.Label(new Rect(x, 5, 40, 40), (i * setting.TimeStep).ToString("F1")); } else if (i % 5 == 0) { Handles.color = new Color(0, 0, 0, 0.5f); Handles.DrawLine(new Vector3(x, 0, 0), new Vector3(x, rulerRect.height * 0.5f, 0)); } else { Handles.color = new Color(0, 0, 0, 0.5f); Handles.DrawLine(new Vector3(x, 0, 0), new Vector3(x, rulerRect.height * 0.3f, 0)); } } } }
private void DrawTrackline(Rect lineRect) { LineSetting setting = LineSetting.Setting; using (new GUI.ClipScope(lineRect)) { int startY = Mathf.FloorToInt(setting.ScrollPosY / setting.TracklineHeight); int endY = Mathf.CeilToInt((setting.ScrollPosY + lineRect.height) / setting.TracklineHeight); float maxWidth = Data.TimeLength * setting.WidthForSecond; for (int i = startY; i < endY; ++i) { float y = setting.TracklineHeight * i - setting.ScrollPosY; if (i >= trackDrawers.Count) { break; } Rect tRect = new Rect(0, y, lineRect.width, setting.TracklineHeight); tRect.width = Mathf.Min(lineRect.width, maxWidth - setting.ScrollPosX); if (selectedTrackIndex == i) { EGUI.DrawAreaLine(tRect, Color.green); } trackDrawers[i].OnDrawGUI(tRect); } } }
private void DrawLine(Rect lineRect) { EGUI.DrawAreaLine(lineRect, Color.gray); Rect rulerRect = new Rect(lineRect.x, lineRect.y, lineRect.width, LINE_RULER_HEIGHT); EditorGUI.LabelField(rulerRect, GUIContent.none, EditorStyles.toolbar); DrawLineRuler(rulerRect); Rect gridRect = new Rect(lineRect.x, lineRect.y + LINE_RULER_HEIGHT, lineRect.width, lineRect.height - LINE_RULER_HEIGHT); DrawLineGrid(gridRect); if (Data != null) { DrawTrackline(gridRect); LineSetting setting = LineSetting.Setting; using (new GUILayout.AreaScope(gridRect)) { using (var scop = new UnityEditor.EditorGUILayout.ScrollViewScope(setting.ScrollPos)) { float scrollWith = Mathf.Max(Data.TimeLength * setting.WidthForSecond, gridRect.width); float scrollHeight = Mathf.Max(Data.Tracks.Count * setting.TracklineHeight, gridRect.height); GUILayout.Label("", GUILayout.Width(scrollWith), GUILayout.Height(scrollHeight - 20)); setting.ScrollPos = scop.scrollPosition; } } } }
public TimelineDrawer(EditorWindow win, string titleName = null) { Window = win; Window.wantsMouseMove = true; this.titleName = titleName ?? "Timeline"; setting = new LineSetting(); LineSetting.Setting = setting; }
private void DrawLineGrid(Rect gridRect) { LineSetting setting = LineSetting.Setting; using (new GUI.ClipScope(new Rect(gridRect.x, gridRect.y, gridRect.width, gridRect.height))) { int startX = Mathf.FloorToInt(setting.ScrollPosX / setting.WidthForSecond); int endX = Mathf.CeilToInt((setting.ScrollPosX + gridRect.width) / setting.WidthForSecond); int startXCount = Mathf.FloorToInt(startX / setting.TimeStep); int endXCount = Mathf.FloorToInt(endX / setting.TimeStep); for (int i = startXCount; i <= endXCount; i++) { var x = i * setting.TimeStepWidth - setting.ScrollPosX; Color handlesColor = new Color(0, 0, 0, 0.3f); if (i % 10 == 0) { handlesColor = new Color(0, 0, 0, 1.0f); } else if (i % 5 == 0) { handlesColor = new Color(0, 0, 0, 0.8f); } Handles.color = handlesColor; Handles.DrawLine(new Vector3(x, 0, 0), new Vector3(x, gridRect.height, 0)); } if (Data != null) { float stopLineX = Data.TimeLength * setting.WidthForSecond - setting.ScrollPosX; Handles.color = Color.red; Handles.DrawLine(new Vector3(stopLineX, 0, 0), new Vector3(stopLineX, gridRect.height, 0)); } int startY = Mathf.FloorToInt(setting.ScrollPosY / setting.TracklineHeight); int endY = Mathf.CeilToInt((setting.ScrollPosY + gridRect.height) / setting.TracklineHeight); for (int i = startY; i <= endY; i++) { float y = setting.TracklineHeight * i - setting.ScrollPosY; Handles.color = new Color(0, 0, 0, 0.9f); Handles.DrawLine(new Vector3(0, y, 0), new Vector3(gridRect.width, y, 0)); } } }
private void DrawTrack(Rect trackRect) { EGUI.DrawAreaLine(trackRect, Color.gray); LineSetting setting = LineSetting.Setting; Rect titleRect = new Rect(trackRect.x, trackRect.y, trackRect.width, TRACK_TITLE_HEIGHT); EditorGUI.LabelField(titleRect, "Tracks", EditorStyles.toolbar); if (Data == null) { return; } Rect clipRect = new Rect(trackRect.x, trackRect.y + TRACK_TITLE_HEIGHT, trackRect.width, trackRect.height - TRACK_TITLE_HEIGHT); using (new GUI.ClipScope(clipRect)) { int start = Mathf.FloorToInt(setting.ScrollPosY / setting.TracklineHeight); int end = Mathf.CeilToInt((setting.ScrollPosY + clipRect.height) / setting.TracklineHeight); Dictionary <Rect, int> rectIndexDic = new Dictionary <Rect, int>(); for (int i = start; i < end; ++i) { float y = setting.TracklineHeight * i - setting.ScrollPos.y; if (i >= Data.Tracks.Count) { break; } Rect indexRect = new Rect(0, y, trackWidth, setting.TracklineHeight); rectIndexDic.Add(indexRect, i); GUI.Label(indexRect, $"{(Data.Tracks[i].Name ?? "")} ({i.ToString()})", selectedTrackIndex == i ? "flow node 1" : "flow node 0"); } if (Event.current.type == EventType.MouseUp) { int index = -1; foreach (var kvp in rectIndexDic) { if (kvp.Key.Contains(Event.current.mousePosition)) { index = kvp.Value; break; } } if (Event.current.button == 1) { GenericMenu menu = new GenericMenu(); menu.AddItem(new GUIContent(index >= 0 ? "Insert New" : "Add New"), false, () => { OnTrackAdded(new TracklineData(), index); }); if (index >= 0) { menu.AddItem(new GUIContent("Delete"), false, () => { OnTrackDeleted(index); }); } menu.ShowAsContext(); } if (index >= 0) { OnTrackSelected(trackDrawers[index]); } Window.Repaint(); Event.current.Use(); } } }
public void OnDrawGUI(Rect rect) { LineSetting setting = LineSetting.Setting; Rect itemRect = Rect.zero; itemRect.x = Data.FireTime * setting.WidthForSecond - setting.ScrollPosX; itemRect.y = rect.y; itemRect.height = setting.TracklineHeight; itemRect.width = MIN_ACTION_WIDTH; DurationActionData durationActionData = null; if (Data is DurationActionData) { durationActionData = (DurationActionData)Data; itemRect.width = Mathf.Max(itemRect.width, durationActionData.DurationTime * setting.WidthForSecond); } else { itemRect.x -= itemRect.width * 0.5f; } GUI.Label(itemRect, BriefName, IsSelected ? "flow node 6" : "flow node 5"); int eventBtn = Event.current.button; EventType eventType = Event.current.type; bool isContains = itemRect.Contains(Event.current.mousePosition); Rect leftRect = new Rect(itemRect.x, itemRect.y, MIN_ACTION_WIDTH * 0.5f, itemRect.height); Rect rightRect = new Rect(itemRect.x + itemRect.width - MIN_ACTION_WIDTH * 0.5f, itemRect.y, MIN_ACTION_WIDTH * 0.5f, itemRect.height); bool isInLeftRect = false; bool isInRightRect = false; if (durationActionData != null && !durationActionData.IsFixedDurationTime) { EGUI.DrawAreaLine(leftRect, Color.yellow); EGUI.DrawAreaLine(rightRect, Color.yellow); EditorGUIUtility.AddCursorRect(leftRect, MouseCursor.ResizeHorizontal); EditorGUIUtility.AddCursorRect(rightRect, MouseCursor.ResizeHorizontal); isInLeftRect = leftRect.Contains(Event.current.mousePosition); isInRightRect = rightRect.Contains(Event.current.mousePosition); } if (eventBtn == 0) { if (eventType == EventType.MouseDown && isContains) { if (durationActionData != null) { if (isInLeftRect) { dragType = ActionDragType.ItemLeft; } else if (isInRightRect) { dragType = ActionDragType.ItemRight; } else { dragType = ActionDragType.Item; } } else { dragType = ActionDragType.Item; } IsSelected = true; Event.current.Use(); } else if (dragType != ActionDragType.None && eventType == EventType.MouseUp) { dragType = ActionDragType.None; Event.current.Use(); } else if (dragType != ActionDragType.None && IsSelected && eventType == EventType.MouseDrag) { Vector2 deltaPos = Event.current.delta; float deltaTime = deltaPos.x / setting.WidthForSecond; if (dragType == ActionDragType.ItemLeft) { if (Data.FireTime < durationActionData.DurationTime + Data.FireTime || deltaTime < 0) { Data.FireTime += deltaTime; durationActionData.DurationTime -= deltaTime; } } else if (dragType == ActionDragType.ItemRight) { durationActionData.DurationTime += deltaTime; } else if (dragType == ActionDragType.Item) { Data.FireTime += deltaTime; } float timeLength = ParentDrawer.ParentDrawer.Data.TimeLength; if (Data.FireTime < 0) { Data.FireTime = 0; } else if (Data.FireTime > timeLength) { Data.FireTime = timeLength; } if (durationActionData != null) { durationActionData.DurationTime = Mathf.Max(0.0f, durationActionData.DurationTime); float endTime = durationActionData.FireTime + durationActionData.DurationTime; if (endTime > timeLength) { durationActionData.DurationTime = Mathf.Min(durationActionData.DurationTime, timeLength - durationActionData.FireTime); } } Event.current.Use(); } } else if (eventBtn == 1 && isContains && eventType == EventType.MouseUp) { IsSelected = true; GenericMenu menu = new GenericMenu(); menu.AddItem(new GUIContent("Copy"), false, () => { string actionJson = JsonConvert.SerializeObject(Data, new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.All, }); setting.CopiedActionData = actionJson; }); menu.AddSeparator(""); menu.AddItem(new GUIContent("Delete"), false, () => { ParentDrawer.OnActionDelete(this); }); menu.ShowAsContext(); Event.current.Use(); } }