/// <summary> /// 每帧绘制从 节点到所有子节点的连线 /// </summary> /// <param name="nodeView"></param> public void DrawToChildCurve() { for (int i = childNodeList.Count - 1; i >= 0; --i) { NodeView <DataT> childNode = childNodeList[i]; // 删除无效节点 if (childNode.isRelease) { childNodeList.RemoveAt(i); dataNode.childNodes.Remove(childNode.dataNode); continue; } var startPos = windowRect.GetPos(Dir.right); var endPos = childNode.windowRect.GetPos(Dir.left); ViewTool.DrawCurve(startPos, endPos); var rect = ((startPos + endPos) / 2).GetRect(ConnectRect); ConnectShow(rect, childNode); } }
// controlID will get hotControl if using arrow keys while shortcut context is not active // passing a value of zero disables the arrow keys public InputSamplingScope(CameraFlyModeContext context, ViewTool currentViewTool, int controlID, EditorWindow window, bool orthographic = false) { m_ArrowKeysActive = false; m_Disposed = false; m_Context = context; m_Context.active = currentViewTool == ViewTool.FPS; m_Context.m_Window = window; if (m_Context.active) { ShortcutIntegration.instance.contextManager.RegisterToolContext(context); ForceArrowKeysUp(orthographic); } else { ShortcutIntegration.instance.contextManager.DeregisterToolContext(context); m_ArrowKeysActive = DoArrowKeys(controlID, orthographic); } if (currentlyMoving && Mathf.Approximately(m_Context.m_PreviousVector.sqrMagnitude, 0f)) { s_Timer.Begin(); } }
protected void HandleMouseUp(Event evt, int id) { if (GUIUtility.hotControl == id) { this.m_ViewTool = ViewTool.None; GUIUtility.hotControl = 0; EditorGUIUtility.SetWantsMouseJumping(0); evt.Use(); } }
public override void Update(CameraState cameraState, Camera cam) { Event current = Event.current; if (current.type == EventType.MouseUp) { this.m_CurrentViewTool = ViewTool.None; } if (current.type == EventType.MouseDown) { int button = current.button; bool flag = current.control && (Application.platform == RuntimePlatform.OSXEditor); if (button == 2) { this.m_CurrentViewTool = ViewTool.Pan; } else if (((button <= 0) && flag) || ((button == 1) && current.alt)) { this.m_CurrentViewTool = ViewTool.Zoom; this.m_StartZoom = cameraState.viewSize.value; this.m_ZoomSpeed = Mathf.Max(Mathf.Abs(this.m_StartZoom), 0.3f); this.m_TotalMotion = 0f; } else if (button <= 0) { this.m_CurrentViewTool = ViewTool.Orbit; } else if ((button == 1) && !current.alt) { this.m_CurrentViewTool = ViewTool.FPS; } } switch (current.type) { case EventType.MouseUp: this.HandleCameraMouseUp(); break; case EventType.MouseDrag: this.HandleCameraMouseDrag(cameraState, cam); break; case EventType.KeyDown: this.HandleCameraKeyDown(); break; case EventType.KeyUp: this.HandleCameraKeyUp(); break; case EventType.ScrollWheel: this.HandleCameraScrollWheel(cameraState); break; case EventType.Layout: { Vector3 movementDirection = this.GetMovementDirection(); if (movementDirection.sqrMagnitude != 0f) { cameraState.pivot.value += cameraState.rotation.value * movementDirection; } break; } } }
private void ResetCameraControl() { this.m_CurrentViewTool = ViewTool.None; this.m_Motion = Vector3.zero; }
void LateUpdate() { if(Input.GetMouseButtonUp(0) || Input.GetMouseButtonUp(1) || Input.GetMouseButtonUp(2)) { if(cameraState != ViewTool.None && OnCameraFinishMove != null) OnCameraFinishMove(this); currentActionValid = true; eatMouse = false; } else if(Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1) || Input.GetMouseButtonDown(2)) { currentActionValid = CheckMouseOverGUI(); } else { if(transform.hasChanged && OnCameraMove != null) { OnCameraMove(this); transform.hasChanged = false; } } cameraState = ViewTool.None; /** * Camera is flying itself to a target */ if(zooming) { transform.position = Vector3.Lerp(previousPosition, targetPosition, (zoomProgress += Time.deltaTime)/zoomSpeed); if( Vector3.Distance(transform.position, targetPosition) < .1f) zooming = false; } if( (Input.GetAxis(INPUT_MOUSE_SCROLLWHEEL) != 0f || (Input.GetMouseButton(RIGHT_MOUSE) && Input.GetKey(KeyCode.LeftAlt))) && CheckMouseOverGUI()) { float delta = Input.GetAxis(INPUT_MOUSE_SCROLLWHEEL); if( Mathf.Approximately(delta, 0f) ) { cameraState = ViewTool.Dolly; delta = pb_HandleUtility.CalcSignedMouseDelta(Input.mousePosition, prev_mousePosition); } distanceToCamera -= delta * (distanceToCamera/MAX_CAM_DISTANCE) * scrollModifier; distanceToCamera = Mathf.Clamp(distanceToCamera, MIN_CAM_DISTANCE, MAX_CAM_DISTANCE); transform.position = transform.localRotation * (Vector3.forward * -distanceToCamera) + pivot; } bool viewTool = editor == null || editor.EnableCameraControls(); /** * If the current tool isn't View, or no mouse button is pressed, record the mouse position then early exit. */ if( !currentActionValid || (viewTool #if !CONTROLLER && !Input.GetMouseButton(LEFT_MOUSE) && !Input.GetMouseButton(RIGHT_MOUSE) && !Input.GetMouseButton(MIDDLE_MOUSE) && !Input.GetKey(KeyCode.LeftAlt) #endif ) ) { Rect screen = new Rect(0,0,Screen.width,Screen.height); if(screen.Contains(Input.mousePosition)) prev_mousePosition = Input.mousePosition; return; } /** * Flying Hammer Style */ if( Input.GetMouseButton(RIGHT_MOUSE) && !Input.GetKey(KeyCode.LeftAlt) )//|| Input.GetKey(KeyCode.LeftShift) ) { cameraState = ViewTool.Look; eatMouse = true; // Rotation float rot_x = Input.GetAxis(INPUT_MOUSE_X); float rot_y = Input.GetAxis(INPUT_MOUSE_Y); Vector3 eulerRotation = transform.localRotation.eulerAngles; #if USE_DELTA_TIME eulerRotation.x -= rot_y * lookSpeed * Time.deltaTime; // Invert Y axis eulerRotation.y += rot_x * lookSpeed * Time.deltaTime; #else eulerRotation.x -= rot_y * lookSpeed; eulerRotation.y += rot_x * lookSpeed; #endif eulerRotation.z = 0f; transform.localRotation = Quaternion.Euler(eulerRotation); // PositionHandle-- Always use delta time when flying float speed = moveSpeed * Time.deltaTime; transform.position += transform.forward * speed * Input.GetAxis("Vertical"); transform.position += transform.right * speed * Input.GetAxis("Horizontal"); try { transform.position += transform.up * speed * Input.GetAxis("CameraUp"); } catch { Debug.LogWarning("CameraUp input is not configured. Open \"Edit/Project Settings/Input\" and add an input named \"CameraUp\", mapping q and e to Negative and Positive buttons."); } pivot = transform.position + transform.forward * distanceToCamera; } else /** * Orbit */ if(Input.GetKey(KeyCode.LeftAlt) && Input.GetMouseButton(LEFT_MOUSE)) { cameraState = ViewTool.Orbit; eatMouse = true; float rot_x = Input.GetAxis(INPUT_MOUSE_X); float rot_y = -Input.GetAxis(INPUT_MOUSE_Y); Vector3 eulerRotation = transform.localRotation.eulerAngles; if( (Mathf.Approximately(eulerRotation.x, 90f) && rot_y > 0f) || (Mathf.Approximately(eulerRotation.x, 270f) && rot_y < 0f) ) rot_y = 0f; #if USE_DELTA_TIME eulerRotation.x += rot_y * orbitSpeed * Time.deltaTime; eulerRotation.y += rot_x * orbitSpeed * Time.deltaTime; #else eulerRotation.x += rot_y * orbitSpeed; eulerRotation.y += rot_x * orbitSpeed; #endif eulerRotation.z = 0f; transform.localRotation = Quaternion.Euler( eulerRotation ); transform.position = CalculateCameraPosition(pivot); } else /** * Pan */ if(Input.GetMouseButton(MIDDLE_MOUSE) || (Input.GetMouseButton(LEFT_MOUSE) && viewTool ) ) { cameraState = ViewTool.Pan; Vector2 delta = Input.mousePosition - prev_mousePosition; delta.x = ScreenToWorldDistance(delta.x, distanceToCamera); delta.y = ScreenToWorldDistance(delta.y, distanceToCamera); transform.position -= transform.right * delta.x; transform.position -= transform.up * delta.y; pivot = transform.position + transform.forward * distanceToCamera; } prev_mousePosition = Input.mousePosition; }
public void OnGUI() { Event guiEvent = Event.current; activeViewTool = ViewTool.None; if (guiEvent.isScrollWheel) { activeViewTool = ViewTool.Zoom; } else if ((activeTool == ToolSet.View) || (guiEvent.alt)) { if (guiEvent.control) { activeViewTool = ViewTool.Zoom; } else { activeViewTool = ViewTool.Pan; } } if (guiEvent.type == EventType.MouseDown) { mouseDownPosition = guiEvent.mousePosition; mouseDownTime = Time.time; mouseIsDown = true; } else if (guiEvent.type == EventType.MouseUp) { mouseIsDown = false; } Rect toolRect = new Rect(0, 0, this.position.width, toolbarHeight + toolbarPadding * 2); Rect toolbarRect = new Rect(toolbarPadding * 2, toolbarPadding, toolbarWidth * toolIcons.Length, toolbarHeight); OnToolbarArea(guiEvent, toolbarRect); Rect separatorRect = new Rect(toolbarRect.xMax + toolbarPadding, 0, separatorWidth, toolRect.height); GUI.DrawTexture(separatorRect, separatorTexture); Rect infoRect = new Rect(separatorRect.xMax, 0, toolRect.width - separatorRect.xMax, toolRect.height); switch (activeTool) { case ToolSet.View: OnInfoAreaView(guiEvent, infoRect); break; case ToolSet.Select: OnInfoAreaSelect(guiEvent, infoRect); break; case ToolSet.Brush: OnInfoAreaBrush(guiEvent, infoRect); break; case ToolSet.Shape: OnInfoAreaShape(guiEvent, infoRect); break; } Rect viewRect = new Rect(0, toolRect.yMax, this.position.width, this.position.height - toolRect.yMax); MouseCursor activeCursor = MouseCursor.Arrow; switch (activeViewTool) { case ViewTool.Zoom: activeCursor = MouseCursor.Zoom; break; case ViewTool.Pan: activeCursor = MouseCursor.Pan; break; } EditorGUIUtility.AddCursorRect(viewRect, activeCursor); bool handled = false; if (guiEvent.type == EventType.Repaint) { renderUtil.BeginPreview(viewRect, GUIStyle.none); // renderUtil.camera.backgroundColor is reset in BeginPreview() renderUtil.camera.backgroundColor = backgroundColor; foreach (VectorShape shape in shapes) { renderUtil.DrawMesh(shape.ShapeMesh, Matrix4x4.identity, renderMaterial, 0); //Debug.Log(shape.ShapeMesh.vertexCount); } renderUtil.Render(); Handles.SetCamera(renderUtil.camera); VectorShape.handleDrawSize = HandleUtility.GetHandleSize(Vector3.zero) * viewRect.height / viewRect.width; foreach (VectorShape selected in selection) { selected.DrawEditorHandles(true, (selected == focusedShape)); } if (selectionRect != Rect.zero) { Color outlineColor = Handles.color; Color fillColor = new Color(outlineColor.r, outlineColor.g, outlineColor.b, 0.2f); Handles.DrawSolidRectangleWithOutline(selectionRect, fillColor, outlineColor); } renderUtil.EndAndDrawPreview(viewRect); } else if (guiEvent.isScrollWheel && (EditorWindow.mouseOverWindow == this)) { float zoomFactor = HandleUtility.niceMouseDeltaZoom; renderUtil.camera.orthographicSize *= (1 - zoomFactor * .02f); // Update matrix from mouse to shape space mouseToShapeScale = renderUtil.camera.orthographicSize * 2f / viewRect.height; mouseToShapeMatrix = Matrix2D.Translate(renderUtil.camera.transform.position) * Matrix2D.Scale(new Vector2(mouseToShapeScale, -mouseToShapeScale)) * Matrix2D.Translate(-viewRect.center); handled = true; } else if (guiEvent.isMouse) { // Update mouse position and matrix from mouse to shape space mouseToShapeScale = renderUtil.camera.orthographicSize * 2f / viewRect.height; mousePosition = guiEvent.mousePosition; mouseToShapeMatrix = Matrix2D.Translate(renderUtil.camera.transform.position) * Matrix2D.Scale(new Vector2(mouseToShapeScale, -mouseToShapeScale)) * Matrix2D.Translate(-viewRect.center); mouseInContent = (viewRect.Contains(mousePosition)); if (activeViewTool != ViewTool.None) { handled = OnViewToolMouse(guiEvent); } else { switch (activeTool) { case ToolSet.Select: handled = OnSelectToolMouse(guiEvent); break; case ToolSet.Brush: handled = OnBrushToolMouse(guiEvent); break; case ToolSet.Shape: handled = OnShapeToolMouse(guiEvent); break; } } if (!handled) { Vector2 delta = guiEvent.delta; guiEvent.mousePosition = MouseToShapePoint(mousePosition); guiEvent.delta = guiEvent.delta * new Vector2(mouseToShapeScale, -mouseToShapeScale); foreach (VectorShape selected in selection) { handled |= selected.HandleEditorEvent(guiEvent, true); } guiEvent.mousePosition = mousePosition; guiEvent.delta = delta; } } else if (guiEvent.type == EventType.ValidateCommand) { switch (guiEvent.commandName) { case Cmd_Delete: if (selection.Count > 0) { handled = true; } break; case Cmd_SelectAll: if (shapes.Count > 0) { handled = true; } break; } } else if (guiEvent.type == EventType.ExecuteCommand) { switch (guiEvent.commandName) { case Cmd_Delete: foreach (VectorShape selected in selection) { shapes.Remove(selected); } selection.Clear(); handled = true; break; case Cmd_SelectAll: foreach (VectorShape selected in shapes) { selection.Add(selected); } handled = true; break; case Cmd_ModifierKeysChanged: Repaint(); handled = true; break; } } if (handled) { guiEvent.Use(); } }
public override void Update(CameraState cameraState, Camera cam) { Event evt = Event.current; if (evt.type == EventType.MouseUp) { m_CurrentViewTool = ViewTool.None; } if (evt.type == EventType.MouseDown) { int button = evt.button; bool controlKeyOnMac = (evt.control && Application.platform == RuntimePlatform.OSXEditor); if (button == 2) { m_CurrentViewTool = ViewTool.Pan; } else if ((button <= 0 && controlKeyOnMac) || (button == 1 && evt.alt)) { m_CurrentViewTool = ViewTool.Zoom; m_StartZoom = cameraState.viewSize.value; m_ZoomSpeed = Mathf.Max(Mathf.Abs(m_StartZoom), .3f); m_TotalMotion = 0; } else if (button <= 0) { m_CurrentViewTool = ViewTool.Orbit; } else if (button == 1 && !evt.alt) { m_CurrentViewTool = ViewTool.FPS; } } switch (evt.type) { case EventType.ScrollWheel: HandleCameraScrollWheel(cameraState); break; case EventType.MouseUp: HandleCameraMouseUp(); break; case EventType.MouseDrag: HandleCameraMouseDrag(cameraState, cam); break; case EventType.KeyDown: HandleCameraKeyDown(); break; case EventType.KeyUp: HandleCameraKeyUp(); break; case EventType.Layout: { Vector3 motion = GetMovementDirection(); if (motion.sqrMagnitude != 0) { cameraState.pivot.value = cameraState.pivot.value + cameraState.rotation.value * motion; } } break; } }
public void OnGUI() { Event guiEvent = Event.current; Rect toolRect = EditorGUILayout.BeginVertical(); EditorGUILayout.Space(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.Space(); UpdateToolbar(guiEvent); // FIXME - I get jitter in the toolbar when the window resizes even if I give it a constant // rectangle here, so just do this as the simplest version. selectedTool = GUILayout.Toolbar(selectedTool, toolbar); GUILayout.FlexibleSpace(); EditorGUILayout.EndHorizontal(); EditorGUILayout.Space(); EditorGUILayout.EndVertical(); Rect viewRect = new Rect(0, toolRect.yMax, this.position.width, this.position.height - toolRect.yMax); activeViewTool = ViewTool.None; if (guiEvent.isScrollWheel) { activeViewTool = ViewTool.Zoom; } if (guiEvent.alt) { if (guiEvent.control) { activeViewTool = ViewTool.Zoom; } else { activeViewTool = ViewTool.Pan; } } MouseCursor activeCursor = MouseCursor.Arrow; switch (activeViewTool) { case ViewTool.Zoom: activeCursor = MouseCursor.Zoom; break; case ViewTool.Pan: activeCursor = MouseCursor.Pan; break; } EditorGUIUtility.AddCursorRect(viewRect, activeCursor); bool handled = false; if (guiEvent.type == EventType.Repaint) { //GUILayout.BeginArea(toolbarRect); //selectedTool = GUILayout.Toolbar(selectedTool, toolbar); //GUILayout.BeginHorizontal(); //GUILayout.Toolbar(0, images, EditorStyles.toolbarButton); //GUILayout.EndHorizontal(); //GUILayout.EndArea(); renderUtil.BeginPreview(viewRect, GUIStyle.none); // renderUtil.camera.backgroundColor is reset in BeginPreview() renderUtil.camera.backgroundColor = backgroundColor; foreach (VectorShape shape in shapes) { renderUtil.DrawMesh(shape.ShapeMesh, Matrix4x4.identity, renderMaterial, 0); } renderUtil.Render(); Handles.SetCamera(renderUtil.camera); VectorShape.handleDrawSize = HandleUtility.GetHandleSize(Vector3.zero) * viewRect.height / viewRect.width; foreach (VectorShape selected in selection) { selected.DrawEditorHandles(true); } renderUtil.EndAndDrawPreview(viewRect); } else if (guiEvent.isScrollWheel && (EditorWindow.mouseOverWindow == this)) { float zoomFactor = HandleUtility.niceMouseDeltaZoom; renderUtil.camera.orthographicSize *= (1 - zoomFactor * .02f); handled = true; } else if (guiEvent.isMouse) { Camera camera = renderUtil.camera; float viewScale = camera.orthographicSize * 2f / viewRect.height; if (activeViewTool == ViewTool.Pan) { camera.transform.position += (Vector3)(guiEvent.delta * new Vector2(-viewScale, viewScale)); handled = true; } else if (activeViewTool == ViewTool.Zoom) { float zoomFactor = HandleUtility.niceMouseDeltaZoom; renderUtil.camera.orthographicSize *= (1 + zoomFactor * .005f); handled = true; } if (!handled) { // Convert the event to shape space for convenience Vector2 mousePosition = guiEvent.mousePosition; Vector2 delta = guiEvent.delta; Vector2 cameraPos = camera.transform.position; Vector2 viewPos = mousePosition - viewRect.center; guiEvent.mousePosition = new Vector2( viewPos.x * viewScale + cameraPos.x, viewPos.y * -viewScale + cameraPos.y ); guiEvent.delta = guiEvent.delta * new Vector2(viewScale, -viewScale); foreach (VectorShape selected in selection) { handled |= selected.HandleEditorEvent(guiEvent, true); } guiEvent.mousePosition = mousePosition; guiEvent.delta = delta; } } else if (guiEvent.type == EventType.ValidateCommand) { switch (guiEvent.commandName) { case Cmd_Delete: if (selection.Count > 0) { handled = true; } break; case Cmd_SelectAll: if (shapes.Count > 0) { handled = true; } break; } } else if (guiEvent.type == EventType.ExecuteCommand) { switch (guiEvent.commandName) { case Cmd_Delete: foreach (VectorShape selected in selection) { shapes.Remove(selected); } selection.Clear(); handled = true; break; case Cmd_SelectAll: foreach (VectorShape selected in shapes) { selection.Add(selected); } handled = true; break; case Cmd_ModifierKeysChanged: Repaint(); handled = true; break; } } if (handled) { guiEvent.Use(); } }
void LeftGroup() { var leftRect = new Rect(0, 0, position.width * midLinePos, position.height); GUI.BeginGroup(leftRect); if (backTex != null) { var xTex = leftRect.width / backTex.width; var yTex = leftRect.height / backTex.height; var xStart = viewOffset.x.Fix(-backTex.width, 0, backTex.width); var yStart = viewOffset.y.Fix(-backTex.height, 0, backTex.height); for (int x = 0; x <= xTex + 1; x++) { for (int y = 0; y <= yTex + 1; y++) { GUI.DrawTexture(new Rect(xStart + backTex.width * x, yStart + backTex.height * y, backTex.width, backTex.height), backTex); } } } else { //Debug.LogError("background.png is null"); backTex = Resources.Load <Texture2D>("background"); //backTex=Resources.Load<Texture2D>("background.png"); } //遍历所有节点,移除无效节点 for (int i = nodeRootList.Count - 1; i >= 0; --i) { if (nodeRootList[i].isRelease) { nodeRootList.RemoveAt(i); } } if (curEvent.button == 1) // 鼠标右键 { if (curEvent.type == EventType.MouseDown) { if (!makeTransitionMode) { bool clickedOnNode = false; selectNode = GetMouseInNode(); clickedOnNode = (selectNode != null); if (!clickedOnNode) { ShowMenu(0); } else { ShowMenu(1); } } } } // 选择节点为空时,无法连线 if (selectNode == null) { makeTransitionMode = false; } if (!makeTransitionMode) { if (curEvent.type == EventType.MouseUp) { selectNode = null; } } // 在连线状态,按下鼠标 if (makeTransitionMode && curEvent.type == EventType.MouseDown) { NodeView <DataT> newSelectNode = GetMouseInNode(); // 如果按下鼠标时,选中了一个节点,则将 新选中根节点 添加为 selectNode 的子节点 if (newSelectNode == null) { } else if (selectNode != newSelectNode) { selectNode.AddChild(newSelectNode); } // 取消连线状态 makeTransitionMode = false; // 清空选择节点 selectNode = null; } // 连线状态下 选择节点不为空 if (makeTransitionMode && selectNode != null) { ViewTool.DrawCurve(selectNode.windowRect.GetPos(Dir.right), mousePos); } // 开始绘制节点 // 注意:必须在 BeginWindows(); 和 EndWindows(); 之间 调用 GUI.Window 才能显示 BeginWindows(); for (int i = 0; i < nodeRootList.Count; i++) { NodeView <DataT> nodeView = nodeRootList[i]; nodeView.windowRect = GUI.Window(i, nodeView.windowRect, DrawNodeWindow, ""); nodeView.DrawToChildCurve(); } EndWindows(); if (curEvent.button == 0) { if (curEvent.type == EventType.MouseDrag) { selectNode = GetMouseInNode(); if (selectNode == null) { viewOffset += curEvent.delta; } } } GUI.EndGroup(); }