//计算时间轴区域的点击事件 void TimeLineInput() { float wOneSce = RectTimeLine.width / (m_handle.m_duration * 1.04f); //1秒的长度 float minMarkUnit = minUnit * wOneSce; //最小单位对应的长度 //鼠标点击事件 if (Event.current.type == EventType.MouseDown && Event.current.button == 0) { //跳至对应时间 if (RectTimeLine.Contains(Event.current.mousePosition)) { CanDragTimeLine = true; CurTime = (Event.current.mousePosition.x - RectTimeLine.x) / wOneSce; Event.current.Use(); } //处理组的展开和拖动 else if (RectHandles.Contains(Event.current.mousePosition)) { float t = (Event.current.mousePosition.x - RectHandles.x) / wOneSce; Handle h = m_subHandles.GetClosest(t, 0, 8 / wOneSce);//5像素内点击成功 if (CurSelectHandles != null) { if (t >= SelectLeft && t <= SelectRight) { CanDragSelect = true; } else { CurSelectHandles = null; } } else if (h != null) { CurDowmHandles = h; } else { CanSelect = true; SelectLeft = t; SelectRight = t; } Event.current.Use(); } //处理的显示和拖动 else if (RectSubHandle.Contains(Event.current.mousePosition)) { CurDowmSubHandle = m_subHandles.GetClosest( (Event.current.mousePosition.x - RectHandles.x) / wOneSce, (int)((Event.current.mousePosition.y - ScrollHandles.y - RectHandles.yMax - 5) / (hHandleRow + 2f)), 7 / wOneSce);//5像素内点击成功 Event.current.Use(); } } else if (Event.current.type == EventType.MouseDrag && Event.current.button == 0) { float t = Mathf.Clamp((Event.current.mousePosition.x - RectTimeLine.x) / wOneSce, 0, m_handle.m_duration); float tClosest = Mathf.Clamp(Util.ClosestOf((Event.current.mousePosition.x - RectTimeLine.x) / wOneSce, minUnit), 0, m_handle.m_duration); bool use = true; //跳至对应时间 if (CanDragTimeLine == true) { CurTime = t; } //处理组的拖动 else if (CurDowmHandles != null) { CurDragHandles = CurDowmHandles; CurDowmHandles = null; DragHandlesTime = tClosest; } else if (CurDragHandles != null) { DragHandlesTime = tClosest; } else if (CurDowmSubHandle != null) { CurDragSubHandle = CurDowmSubHandle; CurDowmSubHandle = null; DragSubHandleTime = tClosest; } else if (CurDragSubHandle != null) { DragSubHandleTime = tClosest; } else if (CanSelect) { SelectRight = t; } else if (CanDragSelect) { SelectOffsetTime = Mathf.Clamp( SelectOffsetTime + Event.current.delta.x / wOneSce, -SelectLeft, m_handle.m_duration - SelectRight); } else { use = false; } if (use) { Event.current.Use(); } } else if (Event.current.type == EventType.MouseUp && Event.current.button == 0) { bool use = true; //处理组的拖动 if (CurDowmHandles != null) { m_subHandles.ToggleExpand(CurDowmHandles.m_delay); } else if (CurDragHandles != null) { m_subHandles.ChangeTime(m_comp, m_handle, CurDragHandles.m_delay, DragHandlesTime); } else if (CurDowmSubHandle != null) { CurDowmSubHandle.m_seqIsShow = !CurDowmSubHandle.m_seqIsShow; } else if (CurDragSubHandle != null) { m_subHandles.ChangeTime(m_comp, m_handle, CurDragSubHandle, DragSubHandleTime); } else if (CanDragSelect) { SelectOffsetTime = Util.ClosestOf(SelectOffsetTime, minUnit); m_subHandles.OffsetTime(m_comp, m_handle, CurSelectHandles, SelectOffsetTime); CurSelectHandles = null; } else if (CanSelect) { CurSelectHandles = m_subHandles.GetRange(SelectLeft, SelectRight); SelectOffsetTime = 0; if (CurSelectHandles.Count == 0) { CurSelectHandles = null; } else { SelectLeft = CurSelectHandles[0].m_delay; SelectRight = CurSelectHandles[CurSelectHandles.Count - 1].m_delay; } } else { use = false; } CanDragTimeLine = false; CurDowmHandles = null; CurDragHandles = null; CurDowmSubHandle = null; CurDragSubHandle = null; CanSelect = false; CanDragSelect = false; this.PreRemoveSubHandles = null; this.PreRemoveSubHandle = null; if (use) { Event.current.Use(); } } else if (Event.current.type == EventType.MouseDown && Event.current.button == 1) { float tClosest = Mathf.Clamp(Util.ClosestOf((Event.current.mousePosition.x - RectTimeLine.x) / wOneSce, minUnit), 0, m_handle.m_duration); bool use = true; //跳至对应时间 if (RectHandles.Contains(Event.current.mousePosition)) { this.PreRemoveSubHandle = null; this.PreRemoveSubHandles = m_subHandles.GetClosest(tClosest, 0, 8 / wOneSce);//5像素内点击成功 } //处理的增加删除 else if (RectSubHandle.Contains(Event.current.mousePosition)) { this.PreRemoveSubHandle = m_subHandles.GetClosest(tClosest, (int)((Event.current.mousePosition.y - ScrollHandles.y - RectHandles.yMax - 5) / (hHandleRow + 2f)), 7 / wOneSce);//5像素内点击成功 this.PreRemoveSubHandles = null; } else { use = false; } if (use) { Event.current.Use(); } } else if (Event.current.type == EventType.MouseUp && Event.current.button == 1) { if (RectHandles.Contains(Event.current.mousePosition) || RectSubHandle.Contains(Event.current.mousePosition)) { float tClosest = Mathf.Clamp(Util.ClosestOf((Event.current.mousePosition.x - RectTimeLine.x) / wOneSce, minUnit), 0, m_handle.m_duration); GenericMenu contextMenu = new GenericMenu(); if (this.PreRemoveSubHandles != null) { contextMenu.AddItem(new GUIContent("删除"), false, () =>//不导出中文 { this.m_subHandles.RemoveSubHandle(this.m_comp, this.m_handle, this.PreRemoveSubHandles.m_delay); this.PreRemoveSubHandles = null; this.PreRemoveSubHandle = null; }); } else if (this.PreRemoveSubHandle != null) { contextMenu.AddItem(new GUIContent("删除"), false, () =>//不导出中文 { this.m_subHandles.RemoveSubHandle(this.m_comp, this.m_handle, this.PreRemoveSubHandle); this.PreRemoveSubHandles = null; this.PreRemoveSubHandle = null; }); } for (int i = 0; i < (int)Handle.Type.max; ++i) { int tem = i; contextMenu.AddItem(new GUIContent("添加/" + Handle.TypeName[tem]), false, () =>//不导出中文 { this.m_subHandles.AddSubHandle(this.m_comp, this.m_handle, (Handle.Type)tem, tClosest); }); } contextMenu.ShowAsContext(); Event.current.Use(); } } }
//右边时间轴 void DrawRightTimeLine() { using (new AutoBeginVertical(GUILayout.ExpandWidth(true), GUILayout.Height(hMiddle))) { //先绘制布局 TimeLineLayout(); //计算鼠标点击事件 TimeLineInput(); //计算相关值 float wOneSce = RectTimeLine.width / (m_handle.m_duration * 1.04f); //1秒的长度 float minMarkUnit = minUnit * wOneSce; //最小单位对应的长度 int oneSceMarkCount = 1; //1秒画多少个刻度,五种规格20,10,5,2,1 if (wOneSce / 20f >= 7f) { oneSceMarkCount = 20; } else if (wOneSce / 10f >= 7f) { oneSceMarkCount = 10; } else if (wOneSce / 5f >= 7f) { oneSceMarkCount = 5; } else if (wOneSce / 2f >= 7f) { oneSceMarkCount = 2; } float wMark = wOneSce / oneSceMarkCount; //画刻度线 int markCount = Mathf.FloorToInt(RectTimeLine.width / wMark) + 1; float hBigMark = hTimeLine * 3 / 4f; float hMidMark = hTimeLine * 2 / 5f; float hSmallMark = 2f; float topBigMark = hToolBar + hTimeLine - hBigMark - 2; float topMidMark = hToolBar + hTimeLine - hMidMark - 2; float topSmallMark = hToolBar + hTimeLine - hSmallMark - 2; for (int i = 0; i < markCount; ++i) { if (i % oneSceMarkCount == 0) { GUI.DrawTexture(new Rect(RectTimeLine.x + i * wMark, topBigMark, 1, hBigMark), EditorUtil.LoadTexture2D("blank"), ScaleMode.ScaleAndCrop); GUI.Label(new Rect(RectTimeLine.x + i * wMark, hToolBar - 3, 30, 15), string.Format("{0:0.##}", i / oneSceMarkCount)); } else if (oneSceMarkCount >= 5 && i % 5 == 0) { GUI.DrawTexture(new Rect(RectTimeLine.x + i * wMark, topMidMark - 3, 1, hMidMark), EditorUtil.LoadTexture2D("blank"), ScaleMode.ScaleAndCrop); GUI.Label(new Rect(RectTimeLine.x + i * wMark, hToolBar - 3, 30, 15), string.Format("{0:0.##}", i / (float)oneSceMarkCount)); } else { GUI.DrawTexture(new Rect(RectTimeLine.x + i * wMark, topSmallMark, 1, hSmallMark), EditorUtil.LoadTexture2D("blank"), ScaleMode.ScaleAndCrop); } Color PreviousColor = GUI.color; GUI.color = i % 2 == 0 ? Color.white * 0.4f : Color.white * 0.5f; GUI.DrawTexture(new Rect(RectTimeLine.x + i * wMark, hToolBar + hTimeLine + hTimeLineHandle + 4, 1, hMiddle - hTimeLineHandle - hTimeLine), EditorUtil.LoadTexture2D("blank"), ScaleMode.ScaleAndCrop); GUI.color = PreviousColor; } //画点 int idx = 0; float time = -1; int idxTime = -1; foreach (var h in this.m_subHandles.m_handles) { //画处理组,大菱形 if (!Mathf.Approximately(h.m_delay, time)) { idx = 0; ++idxTime; time = h.m_delay; Color PreviousColor = GUI.color; GUI.color = ((CurSelectHandles != null && CurSelectHandles.IndexOf(h) != -1) || (PreRemoveSubHandles != null && PreRemoveSubHandles == h)) ? Color.red : (h.m_seqIsExpand ? new Color(0.3f, 0.55f, 0.95f, 1f) : new Color(0.9f, 0.9f, 0.9f, 1f));//区域选中的要显示红色,展开的要显示蓝色 GUI.DrawTexture( new Rect(RectTimeLine.x + h.m_delay * wOneSce - 11, hToolBar + hTimeLine - 4, 22, 22), EditorGUIUtility.IconContent("blendKey").image, ScaleMode.StretchToFill); GUI.color = PreviousColor; GUIStyle style = new GUIStyle(); style.richText = true; GUI.Label(new Rect(RectTimeLine.x + h.m_delay * wOneSce - 4, hToolBar + hTimeLine, 20, 20), string.Format("<b><color=maroon>{0}</color></b>", idxTime), style); } else { ++idx; } //画处理,小菱形 if (idx < ((ScrollHandles.y - hHandleRow / 2) / (hHandleRow + 2)) || idx > (handleCount + ScrollHandles.y / (hHandleRow + 2))) //在滚动区域外不显示 { continue; } float left = RectHandles.x + h.m_delay * wOneSce - 6; float top = -ScrollHandles.y + RectHandles.yMax + (hHandleRow + 2f) * idx; Color PreviousColor2 = GUI.color; GUI.color = this.PreRemoveSubHandle != null && this.PreRemoveSubHandle == h ? Color.red : (h.m_seqIsShow ? new Color(0.3f, 0.55f, 0.95f, 1f) : new Color(0.9f, 0.9f, 0.9f, 1f)); //显示参数详细页面要显示蓝色 //子处理,小菱形 GUI.DrawTexture(new Rect(left, top, 13, 13), EditorGUIUtility.IconContent("blendKey").image, ScaleMode.StretchToFill); GUI.color = PreviousColor2; //如果是展开的话要显示名字 if (h.m_seqIsExpand) { GUI.Label(new Rect(left + 10, top, 40, 15), h.CurTypeName); } } //当前时间线 using (new AutoChangeColor(Color.yellow)) GUI.DrawTexture(new Rect(RectTimeLine.x + CurTime * wOneSce, RectTimeLine.y, 1, hMiddle + 5), EditorUtil.LoadTexture2D("blank"), ScaleMode.ScaleAndCrop); //拖动中的处理组 if (CurDragHandles != null) { using (new AutoChangeColor(Color.blue * 0.6f)) GUI.DrawTexture(new Rect(RectHandles.x + DragHandlesTime * wOneSce, RectHandles.y, 1, hMiddle - hTimeLine), EditorUtil.LoadTexture2D("blank"), ScaleMode.ScaleAndCrop); } //选择中的区域 if (CanSelect || (!CanDragSelect && CurSelectHandles != null)) { float left = SelectLeft <= SelectRight ? SelectLeft : SelectRight; float w = Mathf.Abs(SelectRight - SelectLeft) * wOneSce; using (new AutoChangeColor(Color.red * 0.4f)) GUI.DrawTexture(new Rect( RectHandles.x + left * wOneSce, RectHandles.y, w, RectHandles.height), EditorUtil.LoadTexture2D("blank"), ScaleMode.ScaleAndCrop); } //拖动中的区域 if (CanDragSelect) { float left = SelectLeft <= SelectRight ? SelectLeft : SelectRight; left = left + Util.ClosestOf(SelectOffsetTime, minUnit); float w = Mathf.Max(Mathf.Abs(SelectRight - SelectLeft) * wOneSce, 15f); using (new AutoChangeColor(Color.red * 0.4f)) GUI.DrawTexture(new Rect( RectHandles.x + left * wOneSce, RectHandles.y, w, RectHandles.height), EditorUtil.LoadTexture2D("blank"), ScaleMode.ScaleAndCrop); } //拖动中的处理 if (CurDragSubHandle != null) { using (new AutoChangeColor(Color.blue * 0.6f)) GUI.DrawTexture(new Rect(RectHandles.x + DragSubHandleTime * wOneSce, RectHandles.y, 1, hMiddle - hTimeLine), EditorUtil.LoadTexture2D("blank"), ScaleMode.ScaleAndCrop); } } }