protected void OnEnable() { if (simulatedTouchscreen != null) { if (!simulatedTouchscreen.added) { InputSystem.AddDevice(simulatedTouchscreen); } } else { simulatedTouchscreen = InputSystem.GetDevice("Simulated Touchscreen") as Touchscreen; if (simulatedTouchscreen == null) { simulatedTouchscreen = InputSystem.AddDevice <Touchscreen>("Simulated Touchscreen"); } } if (m_Touches == null) { m_Touches = new SimulatedTouch[simulatedTouchscreen.touches.Count]; } foreach (var device in InputSystem.devices) { OnDeviceChange(device, InputDeviceChange.Added); } InputSystem.onDeviceChange += OnDeviceChange; }
/** * verarbeitet die Touch-Eingaben */ void processTouches() { if (InputSimulation.touchCount != 0) { touch = InputSimulation.GetTouch(0); if (touch.phase == TouchPhase.Began) { beginTouch = touch; } else if (touch.phase == TouchPhase.Ended) { if (beginTouch == null) { beginTouch = touch; } Vector2 touchDirection = new Vector3(touch.position.x - beginTouch.position.x, touch.position.y - beginTouch.position.y); Debug.Log("Vec: " + touchDirection); float dist = touchDirection.magnitude; if (dist > 25) { //touchAngleToXaxis = RadianToDegree (AngleToXaxis (touchDirection)); touchAngleToXaxis = RadianToDegree(Mathf.Atan2(touchDirection.y, touchDirection.x)); processNextTouch = true; } } } else { touch = null; } }
private void Placed(object sender) { var isPlaced = ((SurfaceCheckBox)sender).IsChecked == true; if (isPlaced) { _Touch = new SimulatedTouch { Id = _DeviceIdSequence++, IsTag = IsTag, TagValue = TagValue, Center = Position, Axis = new Microsoft.Surface.Presentation.Input.EllipseData(Size.Width, Size.Height, Orientation) }; _TrackedBlob = _TrackingCanvas.ForTestingPurposes_StartTracking(_Touch); } else { _TrackingCanvas.ForTestingPurposes_StopTracking(_TrackedBlob); _Touch = null; _TrackedBlob = null; } }
void FixedUpdate() { //read input and map it to the variables //touch //Touch is simulated with the mouse. //If Mouse is down then there is a touch. -> no Multi-Touch if (Input.GetMouseButton(0)) { touchCount = 1; SimulatedTouch currentTouch = new SimulatedTouch(); currentTouch.position = Input.mousePosition; if (lastTouched) { currentTouch.deltaPosition = currentTouch.position - lastPosition; } else { currentTouch.deltaPosition = Vector2.zero; } currentTouch.deltaTime = Time.deltaTime; currentTouch.fingerId = 0; if (!lastTouched) { currentTouch.phase = TouchPhase.Began; } else if (currentTouch.deltaPosition.magnitude > 0) { currentTouch.phase = TouchPhase.Moved; } else { currentTouch.phase = TouchPhase.Stationary; } currentTouch.tapCount = 1; lastTouched = true; lastPosition = currentTouch.position; touches = new SimulatedTouch[1] { currentTouch }; } else { touchCount = 0; touches = new SimulatedTouch[0]; lastTouched = false; } //If MouseKey is going up then there is a ended touch. if (Input.GetMouseButtonUp(0)) { touchCount = 1; SimulatedTouch currentTouch = new SimulatedTouch(); currentTouch.position = Input.mousePosition; currentTouch.deltaPosition = currentTouch.position - lastPosition; currentTouch.phase = TouchPhase.Ended; currentTouch.fingerId = 0; touches = new SimulatedTouch[1] { currentTouch }; } // acceleration if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D)) { if (acceleration.x < 1.0f) { acceleration.x += 0.04f; } } if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A)) { if (acceleration.x > -1.0f) { acceleration.x -= 0.04f; } } if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W)) { if (acceleration.y < 1.0f) { acceleration.y += 0.04f; } } if (Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S)) { if (acceleration.y > -1.0f) { acceleration.y -= 0.04f; } } }
public ForTestingPurposes_TrackedBlob(TrackingCanvas container, SimulatedTouch touch) : base(container) { Touch = touch; }
internal ITrackedBlob ForTestingPurposes_StartTracking(SimulatedTouch simulatedTouch) { if (_Blobs == null) _Blobs = new List<TrackedBlob>(); var trackedBlob = new ForTestingPurposes_TrackedBlob(this, simulatedTouch); _Blobs.Add(trackedBlob); UpdateLog(); UpdateVisuals(); return trackedBlob; }
protected void OnSourceControlChangedValue(InputControl control, double time, InputEventPtr eventPtr, long sourceDeviceAndButtonIndex) { var sourceDeviceIndex = sourceDeviceAndButtonIndex & 0xffffffff; if (sourceDeviceIndex < 0 && sourceDeviceIndex >= m_NumSources) { throw new ArgumentOutOfRangeException(nameof(sourceDeviceIndex), $"Index {sourceDeviceIndex} out of range; have {m_NumSources} sources"); } ////TODO: this can be simplified a lot if we use events instead of InputState.Change() but doing so requires work on buffering events while processing; also //// needs extra handling to not lag into the next frame if (control is ButtonControl button) { var buttonIndex = (int)(sourceDeviceAndButtonIndex >> 32); var isPressed = button.isPressed; if (isPressed) { // Start new touch. for (var i = 0; i < m_Touches.Length; ++i) { // Find unused touch. if (m_Touches[i].touchId != 0) { continue; } var touchId = ++m_LastTouchId; m_Touches[i] = new SimulatedTouch { touchId = touchId, buttonIndex = buttonIndex, sourceIndex = (int)sourceDeviceIndex, }; var isPrimary = m_PrimaryTouchIndex == -1; var position = m_CurrentPositions[sourceDeviceIndex]; var oldTouch = simulatedTouchscreen.touches[i].ReadValue(); var touch = new TouchState { touchId = touchId, position = position, phase = TouchPhase.Began, startTime = time, startPosition = position, isPrimaryTouch = isPrimary, tapCount = oldTouch.tapCount, }; if (isPrimary) { InputState.Change(simulatedTouchscreen.primaryTouch, touch, eventPtr: eventPtr); m_PrimaryTouchIndex = i; } InputState.Change(simulatedTouchscreen.touches[i], touch, eventPtr: eventPtr); break; } } else { // End ongoing touch. for (var i = 0; i < m_Touches.Length; ++i) { if (m_Touches[i].buttonIndex != buttonIndex || m_Touches[i].sourceIndex != sourceDeviceIndex || m_Touches[i].touchId == 0) { continue; } // Detect taps. var position = m_CurrentPositions[sourceDeviceIndex]; var oldTouch = simulatedTouchscreen.touches[i].ReadValue(); var isTap = time - oldTouch.startTime <= Touchscreen.s_TapTime && (position - oldTouch.startPosition).sqrMagnitude <= Touchscreen.s_TapRadiusSquared; var touch = new TouchState { touchId = m_Touches[i].touchId, phase = TouchPhase.Ended, position = position, tapCount = (byte)(oldTouch.tapCount + (isTap ? 1 : 0)), isTap = isTap, startPosition = oldTouch.startPosition, startTime = oldTouch.startTime, }; if (m_PrimaryTouchIndex == i) { InputState.Change(simulatedTouchscreen.primaryTouch, touch, eventPtr: eventPtr); ////TODO: check if there's an ongoing touch that can take over m_PrimaryTouchIndex = -1; } InputState.Change(simulatedTouchscreen.touches[i], touch, eventPtr: eventPtr); m_Touches[i].touchId = 0; break; } } } else { Debug.Assert(control is InputControl <Vector2>, "Expecting control to be either a button or a position"); var positionControl = (InputControl <Vector2>)control; // Update recorded position. var position = positionControl.ReadValue(); var delta = position - m_CurrentPositions[sourceDeviceIndex]; m_CurrentPositions[sourceDeviceIndex] = position; // Update position of ongoing touches from this pointer. for (var i = 0; i < m_Touches.Length; ++i) { if (m_Touches[i].sourceIndex != sourceDeviceIndex || m_Touches[i].touchId == 0) { continue; } var oldTouch = simulatedTouchscreen.touches[i].ReadValue(); var isPrimary = m_PrimaryTouchIndex == i; var touch = new TouchState { touchId = m_Touches[i].touchId, phase = TouchPhase.Moved, position = position, delta = delta, isPrimaryTouch = isPrimary, tapCount = oldTouch.tapCount, isTap = false, // Can't be tap as it's a move. startPosition = oldTouch.startPosition, startTime = oldTouch.startTime, }; if (isPrimary) { InputState.Change(simulatedTouchscreen.primaryTouch, touch, eventPtr: eventPtr); } InputState.Change(simulatedTouchscreen.touches[i], touch, eventPtr: eventPtr); } } }