void Start() { if (_layerNames.Length == 0) { throw new IndexOutOfRangeException("index is empty"); } if (_target == null) { throw new NullReferenceException("target is empty"); } _mouseUtility = FindObjectOfType <MouseUtility>(); if (_mouseUtility == null) { throw new NullReferenceException("MouseUtility is nothing"); } ReadJson(); LAYER_MASK = LayerMask.GetMask(_layerNames); POINT = _target.position; var offSet = OFF_SET + POINT; gameObject.transform.position = new Vector3(offSet.x, offSet.y, offSet.z); StartCoroutine(Control()); _audioPlayer = FindObjectOfType <AudioPlayer>(); //gameObject.transform.LookAt(POINT); }
void Update() { if (ItemToPlace != null) { ItemToPlace.transform.position = MouseUtility.MouseRaycastPositionRounded(Scale, Mask); VisualUpdate(); } }
public void OnSceneGUI() { Event e = Event.current; bool elementClicked = false; if (creator.displayNet) { #region ProcessClickEvent DrawNodeNet(ref elementClicked); if (e.type == EventType.MouseDown && e.button == 0 && !elementClicked) { if (!e.shift) { selectedNode = null; } transformingNode = false; selectedStretch = null; if (e.shift) { Vector3 mouseClickPos = MouseUtility.MouseToFloorRaycast(); Undo.RecordObject(creator, "stretch created"); Node newNode = creator.CreateNode(mouseClickPos); if (selectedNode != null) { Undo.RecordObject(creator, "stretch created"); creator.CreateStretch(selectedNode, newNode); } Undo.RegisterCreatedObjectUndo(newNode.gameObject, "node created" + newNode.name); selectedNode = newNode; } else { selectedNode = null; } } else if (e.type == EventType.Layout) { HandleUtility.AddDefaultControl(0); } #endregion if (selectedNode != null && !transformingNode) { Ray mouseRay = HandleUtility.GUIPointToWorldRay(e.mousePosition); Vector3 rayEnd = mouseRay.GetPoint(Camera.current.nearClipPlane); Handles.DrawLine(selectedNode.Pos, rayEnd); SceneView.RepaintAll(); } } }
public void OnSceneGUI() { Event e = Event.current; bool elementClicked = false; #region ProcessClickEvent DrawAnchorPoints(ref elementClicked); if (creator.curveEditing && !elementClicked) { DrawControlPoints(ref elementClicked); } if (e.type == EventType.MouseDown && e.button == 0 && !elementClicked) { selectedNode = null; if (e.shift) { Vector3 mouseClickPos = MouseUtility.MouseToFloorRaycast(); GameObject o = new GameObject(); Node node = o.AddComponent <Node>(); o.transform.SetParent(creator.transform); int id = creator.transform.childCount; node.InitializeNode(id); node.transform.position = mouseClickPos; node.name = "NODE_" + id; } } else if (e.type == EventType.Layout) { HandleUtility.AddDefaultControl(0); } #endregion if (selectedToolbar == (int)ToolbarOptions.NodeEdition) { if (selectedNode != null) { Ray mouseRay = HandleUtility.GUIPointToWorldRay(e.mousePosition); Vector3 rayEnd = mouseRay.GetPoint(Camera.current.nearClipPlane); Handles.DrawLine(selectedNode.transform.position, rayEnd); SceneView.RepaintAll(); } DeleteStretchesButtons(); } DrawBeziers(); DrawStretches(); if (creator.showLabels) { DrawLabels(); } }
private void Input() { Event guiEvent = Event.current; Vector3 mousePos = MouseUtility.GetMouseWorldPosition(); if (guiEvent.type == EventType.MouseDown && guiEvent.button == 0 && guiEvent.shift) { Undo.RecordObject(t, "Add point"); t.points.Add(new Path_Point(mousePos, Quaternion.identity)); } }
void OnDrawGizmosSelected() { if (bg != null) { bg.DebugGizmoDisplay(Vector3.zero); } CurrentMousePos = MouseUtility.MouseRaycastPosition(); Gizmos.color = Color.black; Gizmos.DrawSphere(CurrentMousePos, 1f); }
// ---------------------------------------------------------------------------------------------------- // Returns a vector representing the difference between last touch's position and current position // ---------------------------------------------------------------------------------------------------- private static Vector2 GetDeltaPosition(int _touchIndex, Vector2 _newTouchPosition) { if (_touchIndex == 0) { return(MouseUtility.GetDeltaPosition(_newTouchPosition)); } else if (_touchIndex == 1) { return(KeyboardUtility.GetDeltaPosition(_newTouchPosition)); } return(new Vector2(0.0f, 0.0f)); }
private void OnMouseClick() { if (Input.GetMouseButtonDown(0)) { Vector3 newPos = Camera.main.ViewportToWorldPoint(Input.mousePosition); Vector3 pos = Input.mousePosition / 2f; pos.x = (pos.x - (Screen.width / 4)) / 32; pos.y = (pos.y - (Screen.height / 4)) / 32; Selector.transform.localPosition = MouseUtility.MouseToWorld(true); } }
void Input() { RaycastHit hit; bool mouseHit = MouseUtility.GetMouseWorldPosition(out hit); if (Event.current.type == EventType.MouseDown && mouseHit) { if (Event.current.button == 0 && Event.current.shift) { if (closestSegmentIndex != -1) { Undo.RecordObject(creator, "Split segment"); path.SplitSegment(hit.point, closestSegmentIndex); } else if (!path.IsLoop) { Undo.RecordObject(creator, "Add segment"); path.AddSegment(hit.point); } } if (Event.current.button == 1 && Event.current.shift) { if (closestAnchorIndex != -1) { Undo.RecordObject(creator, "Delete segment"); path.DeleteSegment(closestAnchorIndex); } } } if (Event.current.type == EventType.MouseMove) { List <float> distToSegment = new List <float>(); List <float> distToAnchor = new List <float>(); for (int i = 0; i < path.NumSegments + (path.IsLoop ? 0 : 1); i++) { List <Vector3> handles = path.GetSegmentHandles(i % path.NumSegments); distToSegment.Add(HandleUtility.DistancePointBezier(hit.point, handles[0], handles[3], handles[1], handles[2])); distToAnchor.Add(Vector3.Distance(hit.point, path[i * 3])); } int newClosestSegmentIndex = distToSegment.IndexOf(distToSegment.Min()); int newClosestAnchorIndex = distToAnchor.IndexOf(distToAnchor.Min()) * 3; closestSegmentIndex = distToSegment.Min() < minDistFromElement ? (closestSegmentIndex != newClosestSegmentIndex ? newClosestSegmentIndex : closestSegmentIndex) : -1; closestAnchorIndex = distToAnchor.Min() < minDistFromElement ? (closestAnchorIndex != newClosestAnchorIndex ? newClosestAnchorIndex : closestAnchorIndex) : -1; } }
protected override void Update(GameTime gameTime) { KeyboardWrapper.UpdateState(); GamePadWrapper.UpdateAllGamePads(); MouseUtility.Update(); FrameCounter.IncrementFrameCount(); myStateStack.ResolveQueuedThings(); if (myStateStack.GetCurrentState() == null) { Exit(); return; } myStateStack.Update(); base.Update(gameTime); NetPostMaster.Master.ResolveMessages(); }
void HandleInput(Event guiEvent) { Ray ray = HandleUtility.GUIPointToWorldRay(guiEvent.mousePosition); float planeHeight = movingSpace == MovingSpace.XY ? smoothLine.Nodes[smoothLine.LastNodeIndex].z : smoothLine.Nodes[smoothLine.LastNodeIndex].y; Vector3 mousePos = MouseUtility.GetMousePosWithMoveSpace(ray, guiEvent.mousePosition, movingSpace, planeHeight); if (guiEvent.type == EventType.MouseDown && guiEvent.button == 0 && guiEvent.modifiers == EventModifiers.Shift) { ShiftLeftMouseDown(mousePos, ray); } else if (guiEvent.type == EventType.MouseDown && guiEvent.button == 0 && guiEvent.modifiers == EventModifiers.Control) { CtrlLeftMouseDown(mousePos); } else if (guiEvent.type == EventType.MouseDown && guiEvent.button == 0) { LeftMouseDown(mousePos); } else if (guiEvent.type == EventType.MouseDrag && guiEvent.button == 0) { LeftMouseDrag(mousePos, ray); } else if (guiEvent.type == EventType.MouseUp && guiEvent.button == 0) { LeftMouseUp(); } else if (guiEvent.type == EventType.MouseMove) { MouseMove(ray, mousePos); } else if (guiEvent.type == EventType.KeyDown && guiEvent.keyCode == KeyCode.Space) { ChangeSpace(); if (info.selectedNodeIndex != -1) { pressedPoint = smoothLine.Nodes[info.selectedNodeIndex]; } Repaint(); } }
// ---------------------------------------------------------------------------------------------------- // Returns an array of iPhoneTouchSim representing all touches that are currently active // ---------------------------------------------------------------------------------------------------- private static void UpdateTouches() { // Put the touches in an array to have same code behavior as on iPhone m_TouchCount = 0; if (Input.GetMouseButton(0) || Input.GetMouseButtonUp(0)) { m_Touches[m_TouchCount] = new iPhoneTouchSim(GetDeltaPosition(m_TouchCount, Input.mousePosition), Input.mousePosition, iPhoneTouchSim.FingerId.Mouse, MouseUtility.GetMousePhase()); MouseUtility.LastPosition = m_Touches[m_TouchCount].position; m_TouchCount++; } // Also move the jump button to the suggested position so it does not interact with other control setups if (KeyboardUtility.KeyboardTouched()) { m_Touches[m_TouchCount] = new iPhoneTouchSim(GetDeltaPosition(m_TouchCount, KeyboardUtility.Position), KeyboardUtility.Position, iPhoneTouchSim.FingerId.Arrows, KeyboardUtility.GetKeyboardPhase()); KeyboardUtility.LastPosition = m_Touches[m_TouchCount].position; m_TouchCount++; } // On the iPhone, taps are ordered if (Input.GetMouseButtonDown(0)) { m_LastPcFingerId = iPhoneTouchSim.FingerId.Mouse; } else if (KeyboardUtility.KeyboardJustPressed()) { m_LastPcFingerId = iPhoneTouchSim.FingerId.Arrows; } // Swap mouse & keyboard if mouse is last if (m_TouchCount == 2 && m_LastPcFingerId == iPhoneTouchSim.FingerId.Mouse) { iPhoneTouchSim touch = m_Touches[0]; m_Touches[0] = m_Touches[1]; m_Touches[1] = touch; } }
public Vector2 GetLocation() { Vector3 MousePos = MouseUtility.MouseRaycastPositionRounded(Scale, Mask); return(new Vector2(MousePos.x / Scale, MousePos.z / Scale)); }
// Update is called once per frame void Update() { // reset moveInput = Vector3.zero; moveVelocity = Vector3.zero; if (Input.GetKeyDown(click)) // Process click movement. { RaycastHit hit; Ray pos = Camera.main.ScreenPointToRay(Input.mousePosition); playerAgent.isStopped = false; if (Physics.Raycast(pos, out hit, 1000)) { playerAgent.destination = hit.point; } } else // Get movement input. No movement along an axis if opposite directions pressed at same time. { if (Input.GetKey(up) && !Input.GetKey(down)) { print("up"); moveInput.z = 1; } else if (!Input.GetKey(up) && Input.GetKey(down)) { print("down"); moveInput.z = -1; } if (Input.GetKey(left) && !Input.GetKey(right)) { print("left"); moveInput.x = -1; } else if (!Input.GetKey(left) && Input.GetKey(right)) { print("right"); moveInput.x = 1; } } Vector3 moveDirection = moveInput; // Camera relative movement if (cameraRelative) { Camera camera = Camera.main; Vector3 camForward = camera.transform.forward; Vector3 camRight = camera.transform.right; // Project camera onto horizontal plane camForward.y = 0; camRight.y = 0; camForward = camForward.normalized; camRight = camRight.normalized; moveDirection = camForward * moveInput.z + camRight * moveInput.x; } moveVelocity = Vector3.ClampMagnitude(moveDirection * moveSpeed, moveSpeed); // Ensure that velocity magnitude is no greater than moveSpeed if (moveInput != Vector3.zero) { playerAgent.isStopped = true; } // Dashing if (canMove && Input.GetKeyDown(dash)) { StartCoroutine(dashAction(moveVelocity)); } // Rotate to face mouse position. if (Input.mousePosition != lastMousePosition) // Only update when mouse actually moves. { lastMousePosition = Input.mousePosition; bool hit = false; playerPlane.SetNormalAndPosition(transform.up, transform.position); mouseWorldPosition = MouseUtility.MouseWorldPoint(playerPlane, out hit); if (hit && Vector3.Distance(transform.position, mouseWorldPosition) > centerMouseDeadzoneRadius) { transform.LookAt(mouseWorldPosition); } } // move if (canMove && moveVelocity != Vector3.zero) { playerAgent.velocity = moveVelocity; } // is player moving? is_moving = (playerAgent.velocity != Vector3.zero); }
void ProcessBezierPathInput(Event e) { // Update path pivot point on mouse up if (e.type == EventType.MouseUp) { _currentHandleRot = Quaternion.identity; BezierPath.Pivot = BezierPath.PathBounds.center; } // Find which handle mouse is over. Start by looking at previous handle index first, as most likely to still be closest to mouse var previousMouseOverHandleIndex = (_mouseOverHandleIndex == -1) ? 0 : _mouseOverHandleIndex; _mouseOverHandleIndex = -1; for (var i = 0; i < BezierPath.NumPoints; i += 3) { var handleIndex = (previousMouseOverHandleIndex + i) % BezierPath.NumPoints; var handleRadius = GetHandleDiameter( _globalDisplaySettings.anchorSize * Data.bezierHandleScale, BezierPath[handleIndex]) / 2f; var dst = HandleUtility.DistanceToCircle(BezierPath[handleIndex], handleRadius); if (Math.Abs(dst) <= float.Epsilon) { _mouseOverHandleIndex = handleIndex; break; } } // Shift-left click (when mouse not over a handle) to split or add segment if (_mouseOverHandleIndex == -1) { if (e.type == EventType.MouseDown && e.button == 0 && e.shift) { UpdatePathMouseInfo(); // Insert point along selected segment if (_selectedSegmentIndex != -1 && _selectedSegmentIndex < BezierPath.NumSegments) { var newPathPoint = _pathMouseInfo.ClosestWorldPointToMouse; Undo.RecordObject(_creator, "Split segment"); BezierPath.SplitSegment(newPathPoint, _selectedSegmentIndex, _pathMouseInfo.TimeOnBezierSegment); } // If path is not a closed loop, add new point on to the end of the path else if (!BezierPath.IsClosed) { // insert new point at same dst from scene camera as the point that comes before it (for a 3d path) var dstCamToEndpoint = (Camera.current.transform.position - BezierPath[BezierPath.NumPoints - 1]).magnitude; var newPathPoint = MouseUtility.GetMouseWorldPosition(BezierPath.Space, dstCamToEndpoint); Undo.RecordObject(_creator, "Add segment"); if (e.control || e.command) { BezierPath.AddSegmentToStart(newPathPoint); } else { BezierPath.AddSegmentToEnd(newPathPoint); } } } } // Control click or backspace/delete to remove point if (e.keyCode == KeyCode.Backspace || e.keyCode == KeyCode.Delete || ((e.control || e.command) && e.type == EventType.MouseDown && e.button == 0)) { if (_mouseOverHandleIndex != -1) { Undo.RecordObject(_creator, "Delete segment"); BezierPath.DeleteSegment(_mouseOverHandleIndex); if (_mouseOverHandleIndex == _handleIndexToDisplayAsTransform) { _handleIndexToDisplayAsTransform = -1; } _mouseOverHandleIndex = -1; } } // Holding shift and moving mouse (but mouse not over a handle/dragging a handle) if (_draggingHandleIndex == -1 && _mouseOverHandleIndex == -1) { var shiftDown = e.shift && !_shiftLastFrame; if (shiftDown || ((e.type == EventType.MouseMove || e.type == EventType.MouseDrag) && e.shift)) { UpdatePathMouseInfo(); if (_pathMouseInfo.MouseDstToLine < SegmentSelectDistanceThreshold) { if (_pathMouseInfo.ClosestSegmentIndex != _selectedSegmentIndex) { _selectedSegmentIndex = _pathMouseInfo.ClosestSegmentIndex; HandleUtility.Repaint(); } } else { _selectedSegmentIndex = -1; HandleUtility.Repaint(); } } } if (_shareTransformsWithPath) { // Move bezier path if creator's transform position has changed if (_creator.transform.position != _positionOld) { BezierPath.Position += (_creator.transform.position - _positionOld); _positionOld = _creator.transform.position; } // Rotate bezier path if creator's transform rotation has changed if (_creator.transform.rotation != _rotationOld) { BezierPath.Rotation = _creator.transform.rotation; _creator.transform.rotation = BezierPath.Rotation; // set to constrained value _rotationOld = _creator.transform.rotation; } // Scale bezier path if creator's transform scale has changed if (_creator.transform.localScale != _scaleOld) { BezierPath.Scale = _creator.transform.localScale; _creator.transform.localScale = BezierPath.Scale; // set to constrained value _scaleOld = _creator.transform.localScale; } } _shiftLastFrame = e.shift; }
private GameObject GetClickedGameObject() { return(MouseUtility.SelectGameObjectWithRayCast(mainCamera, Input.mousePosition, 100)); }
void Start() { if (_layerNames.Length == 0) throw new IndexOutOfRangeException("index is empty"); if (_target == null) throw new NullReferenceException("target is empty"); _mouseUtility = FindObjectOfType<MouseUtility>(); if (_mouseUtility == null) throw new NullReferenceException("MouseUtility is nothing"); ReadJson(); LAYER_MASK = LayerMask.GetMask(_layerNames); POINT = _target.position; var offSet = OFF_SET + POINT; gameObject.transform.position = new Vector3(offSet.x, offSet.y, offSet.z); StartCoroutine(Control()); _audioPlayer = FindObjectOfType<AudioPlayer>(); //gameObject.transform.LookAt(POINT); }