public void OnMouseDrag() { if (state.CurrentState == State.DISABLED || state.CurrentState == State.READY) { return; } if (Input.touchCount > 1) { return; } if (SceneExtension.IsAnyTouchOverUI()) { return; // Ignore if touch is over any UI element } //Debug.Log("DRAG " + gameObject.name + " inputPos: " + Input.mousePosition + "st: " + state.CurrentState.ToString()); if (state.CurrentState == State.WAITING_TO_DRAG || state.CurrentState == State.DRAGGING) { Vector3 vpStart = Camera.main.ScreenToViewportPoint(inputStartPosition); Vector3 vpNow = Camera.main.ScreenToViewportPoint(Input.mousePosition); if ((vpNow - vpStart).magnitude > MOVE_DIST) { movingStartLocalPos = transform.localPosition; movingVelocity = Vector3.zero; if (VERTICAL_MOVEMENT_ENABLED && Mathf.Abs(vpNow.x - vpStart.x) < VERTICAL_MOVEMENT_WIDTH_TOLERANCE) { state.SetState(State.MOVING_VERTICAL); } else { state.SetState(State.MOVING_IN_PLANE); } } } if (state.CurrentState == State.MOVING_IN_PLANE) { // Raycast and set new position UpdateMoveInPlane(); } else if (state.CurrentState == State.MOVING_VERTICAL) { // Raycast and set new position UpdateMoveInVertical(); } }
public void OnMouseUp() { if (state.CurrentState == State.DISABLED || state.CurrentState == State.READY) { return; } if (state.CurrentState >= State.TWO_FINGERS) { return; } if (SceneExtension.IsAnyTouchOverUI()) { return; // Ignore if touch is over any UI element } Debug.Log("furniture UP " + gameObject.name + " st:" + state.ToString()); if (state.CurrentState == State.WAITING_TO_DRAG) { // TAPPED if (wasSelectedBefore) { // unselect if (worldObjects != null) { worldObjects.SelectedObject = null; } } else { // keep it selected } } else if (state.CurrentState == State.MOVING_IN_PLANE) { // was moving in plane } else if (state.CurrentState == State.MOVING_VERTICAL) { // was moving in vertical } // Stop tapping/moving state.SetState(State.READY); }
public void OnMouseDown() { //Debug.Log("DOWN PRE First finger " + gameObject.name); if (state.CurrentState == State.DISABLED) { return; } if (Input.touchCount > 1) { return; } if (SceneExtension.IsAnyTouchOverUI()) { return; // Ignore if touch is over any UI element } Debug.Log("DOWN First finger " + gameObject.name); if (state.CurrentState == State.READY) { // FIRST FINGER if (worldObjects != null) { wasSelectedBefore = (worldObjects.SelectedObject == gameObject); // Select it worldObjects.SelectedObject = gameObject; } else { wasSelectedBefore = true; } state.SetState(State.WAITING_TO_DRAG); inputStartTime = Time.time; inputStartPosition = Input.mousePosition; firstFingerId = -1; Debug.Log("furniture DOWN touches:" + Input.touchCount); if (Input.touchCount > 0) { Touch touch = Input.touches[0]; Debug.Log("furniture DOWN touch id:" + touch.fingerId); firstFingerId = touch.fingerId; } } }
private void Update() { if (state.CurrentState == State.DISABLED) { return; } if (state.CurrentState == State.MOVING_TO_TARGET) { UpdateToTarget(); return; } if (SceneExtension.IsAnyTouchOverUI()) { return; // Ignore if touch is over any UI element } if (state.CurrentState == State.READY && (worldObjects == null || worldObjects.SelectedObject == gameObject)) { if (Input.touchCount == 2) { // Allow to scale/rotate with fingers anywhere (not over item), if its selected inputStartTime = Time.time; Touch touch = Input.touches[0]; Debug.Log("furniture 1 touch id:" + touch.fingerId); firstFingerId = touch.fingerId; inputStartPosition = touch.position; touch = Input.touches[1]; Debug.Log("furniture 2 touch id:" + touch.fingerId); secondFingerId = touch.fingerId; secondInputStartPosition = touch.position; state.SetState(State.TWO_FINGERS); } } if (state.CurrentState == State.WAITING_TO_DRAG) { if (Time.time - inputStartTime > TAP_TIME) { state.SetState(State.DRAGGING); } } if (state.CurrentState == State.WAITING_TO_DRAG || state.CurrentState == State.DRAGGING || state.CurrentState == State.MOVING_IN_PLANE || state.CurrentState == State.MOVING_VERTICAL) { // 1 finger down - Check for second finger if (Input.touchCount > 1) { // Start to track SECOND FINGER Touch touch = Input.touches[1]; Debug.Log("furniture DOWN SECOND touch id:" + touch.fingerId + " touches:" + Input.touchCount); secondFingerId = touch.fingerId; secondInputStartPosition = touch.position; state.SetState(State.TWO_FINGERS); } } if (state.CurrentState >= State.TWO_FINGERS) { if (Input.touchCount != 2) { Debug.Log("TWO_FINGERS Stopped, touches now: " + Input.touchCount); state.SetState(State.READY); return; } if (state.CurrentState == State.TWO_FINGERS) { // Check to start Scale / Rotate Vector2 firstPos = Input.touches[0].position; Vector2 secondPos = Input.touches[1].position; // check angle to start Rotating float origAngle = Vector2.SignedAngle(Vector2.up, secondInputStartPosition - inputStartPosition); float nowAngle = Vector2.SignedAngle(Vector2.up, secondPos - firstPos); // check distance to start scaling float origDistance = (secondInputStartPosition - inputStartPosition).magnitude; float nowDistance = (secondPos - firstPos).magnitude; if (ROTATION_ENABLED && Mathf.Abs(nowAngle - origAngle) > ROTATE_ANGLE) { state.SetState(State.ROTATING); rotatingStartRotation = transform.localRotation; } else if (SCALING_ENABLED && (nowDistance < origDistance * (1f - SCALING_DISTANCE_MULT_TRESHOLD) || nowDistance > origDistance * (1f + SCALING_DISTANCE_MULT_TRESHOLD))) { state.SetState(State.SCALING); scalingStartScale = transform.localScale; scalingVelocity = Vector3.zero; } } if (state.CurrentState == State.SCALING) { UpdateScaling(); } else if (state.CurrentState == State.ROTATING) { UpdateRotating(); } } }