//method trying to simulate pulse between two values private IEnumerator fluctuateBetween() { System.Random rnd = new System.Random(); startedFluctuateRoutine = true; while (true) { // Randomly fluctuate if we are inside the range // Otherwise slowly make our way into the range if (fluctuateMinimum <= BPM && BPM <= fluctuateMaximum) { bool upOrDown = rnd.Next(0, 101) > 49; BPM = MathHelper.Approach(BPM, targetBpm, Time.deltaTime * rnd.Next(1, rnd.Next(2, 5))); BPM = Mathf.Clamp(BPM, fluctuateMinimum, fluctuateMaximum); // try to make target BPM trend towards being inside the range if (fluctuateMinimum <= targetBpm && targetBpm <= fluctuateMaximum) { targetBpm += upOrDown ? 1 : -1; } else { if (targetBpm < fluctuateMinimum) { targetBpm += rnd.Next(0, 3) > 0 ? 1 : -1; } else { targetBpm += rnd.Next(0, 3) > 0 ? -1 : 1; } } } else { int maxStep = Mathf.Max(5 * Mathf.FloorToInt(Mathf.Log(BPM < fluctuateMinimum ? fluctuateMinimum - BPM : BPM - fluctuateMaximum)), 3); BPM = MathHelper.Approach(BPM, BPM <= fluctuateMinimum ? fluctuateMinimum : fluctuateMaximum, Time.deltaTime * rnd.Next(1, rnd.Next(2, maxStep * (BPM < fluctuateMinimum ? 1 : -1)))); } yield return(0); } }
void Update() { if (m_dragging) { var pos = content.position; m_velocity = (pos - m_lastPos) / Time.deltaTime; m_lastPos = pos; } else if (m_settling) { // Debug.LogFormat("before: t={0} a={1}", m_targetPosition.x, content.anchoredPosition.x); var x = MathHelper.Approach(content.anchoredPosition.x, m_targetPosition.x, m_snapInertia, Time.deltaTime); var y = MathHelper.Approach(content.anchoredPosition.y, m_targetPosition.y, m_snapInertia, Time.deltaTime); SetContentAnchoredPosition(new Vector2(x, y)); // Debug.LogFormat("after: x={0} t={1} a={2}", x, m_targetPosition.x, content.anchoredPosition.x); } }
protected virtual void Update() { if (!m_firstUpdateComplete) { if (ForegroundPositionService.Instance.HasLocationData) { if (Ready != null) { Ready.Invoke(new MapControllerEventArgs(ForegroundPositionService.Instance.Position)); } m_userAnnotation = new UserAnnotation(); m_userAnnotation.Coordinates = ForegroundPositionService.Instance.Position; if (LockToUser) { LockOnUser(true); } MapView.AddAnnotation(m_userAnnotation); var startCoords = m_startCoordinates ?? ForegroundPositionService.Instance.Position; SetMapRegion(startCoords, ZoomLevel); //m_startMapZoom = ZoomLevel; m_firstUpdateComplete = true; } } else { // Add/remove any annotations on the correct thread if (m_toAdd.Count > 0) { lock (m_toAdd) { var toAdd = m_toAdd.ToArray(); m_toAdd.Clear(); foreach (var ann in toAdd) { MapView.AddAnnotation(ann); } } } if (m_toRemove.Count > 0) { lock (m_toRemove) { var toRemove = m_toRemove.ToArray(); m_toRemove.Clear(); foreach (var ann in toRemove) { MapView.RemoveAnnotation(ann); } } } // The map view will not necessarily re-check the coordinates unless the // told directly. m_userAnnotation.UpdateCoordinates(ForegroundPositionService.Instance.Position, Time.deltaTime); MapView.UpdateAnnotation(m_userAnnotation); if (m_lockAnnotation != null) { SetMapRegion(m_lockAnnotation.Coordinates, MapView.Zoom); if (MapInput.PointerDown && DeselectAnnotationOnMapInput) { SelectAnnotation(null); } } else { if (MapInput.IsTranslating) { m_panToCoordinates = null; m_panToZoom = null; if (DeselectAnnotationOnMapInput) { SelectAnnotation(null); } if (MapInput.Translation.sqrMagnitude > 0) { var tx = -MapInput.Translation.x / (m_mapPixWidth); var ty = MapInput.Translation.y / (m_mapPixWidth); MapView.Translate(tx, ty); } } else { //m_currTranslate = Vector2.zero; if (m_panToCoordinates != null) { var zoom = MapView.Zoom == 0 ? ZoomLevel : MapView.Zoom; Coordinates coordinates = null; if (m_panDuration.HasValue) { // Use smoothstep for time-based transitions var dt = Mathf.Min((Time.time - m_panStartTime) / m_panDuration.Value, 1f); var delta = Mathf.SmoothStep(0, 1, dt); coordinates = m_panStartCoordinates.Interpolate(m_panToCoordinates, delta); if (m_panToZoom.HasValue) { zoom = Mathf.SmoothStep((float)m_panStartZoom, (float)m_panToZoom, dt); } } else { // Use exponential - good for general purpose as it takes longer over long // distances coordinates = MapView.CenterCoordinates.Approach(m_panToCoordinates, PanSpeed, Time.deltaTime); if (m_panToZoom.HasValue) { zoom = MathHelper.Approach(zoom, m_panToZoom.Value, PanSpeed, Time.deltaTime); } } var dz = m_panToZoom.HasValue ? Math.Abs(zoom - m_panToZoom.Value) : 0; SetMapRegion(coordinates, zoom); if (coordinates.GetDistanceFrom(m_panToCoordinates) < 0.5 && dz < 0.1) { SetMapRegion(m_panToCoordinates, m_panToZoom ?? MapView.Zoom); m_panToCoordinates = null; if (m_onPanComplete != null) { m_onPanComplete(); } } } } } if (MapInput.IsPinching) { SetMapRegion(MapView.CenterCoordinates, MapView.Zoom + MapInput.ZoomDelta); } } }
// Update is called once per frame void Update() { bool pinching = false; if (UnityEngine.Application.isMobilePlatform) { pinching = Input.touchCount > 1; } else { pinching = m_pointerDown && (Input.GetKey(KeyCode.Space) || Input.GetKey(KeyCode.LeftShift)); } if (AllowPinchZoom && pinching) { IsTranslating = false; Vector2 touch1Pos; Vector2 touch2Pos; if (UnityEngine.Application.isMobilePlatform) { touch1Pos = Input.touches[0].position; touch2Pos = Input.touches[1].position; } else { touch1Pos = Input.mousePosition; touch2Pos = new Vector2(Screen.width / 2, Screen.height / 2); } Vector2 currentPinch = (touch1Pos - touch2Pos); if (!IsPinching) { PinchStarted(); } else { var pinchFactor = (currentPinch.magnitude / m_lastPinchMagnitude); ZoomDelta = Math.Log(pinchFactor) / m_log2; } m_lastPinchMagnitude = currentPinch.magnitude; } else { if (IsPinching) { PinchEnded(); } } if (!pinching && m_pointerDown) { if (!IsTranslating) { m_lastTranslatePos = Input.mousePosition; } IsTranslating = !m_waitUnhold; var lastV = m_translateVelocity; var currV = GetVTranslate(); m_translateVelocity = ((lastV + currV * 3) / 4); if (m_translateVelocity.magnitude < HoldMaxTranslate) { if (m_holding) { if (Time.time - m_holdTime > ClickHoldDuration && !m_waitUnhold) { m_waitUnhold = true; IsTranslating = false; if (OnHold != null) { OnHold.Invoke(new MapInputEventArgs(Input.mousePosition)); } } } else { m_holding = true; m_holdTime = Time.time; } } else { m_holding = false; } if (IsTranslating) { m_translation = m_translateVelocity * Time.smoothDeltaTime; } } else { m_holding = false; if (m_translateVelocity.sqrMagnitude > TranslateEpsilon) { var vx = MathHelper.Approach(m_translateVelocity.x, 0, TranslateDamp, Time.deltaTime); var vy = MathHelper.Approach(m_translateVelocity.y, 0, TranslateDamp, Time.deltaTime); m_translateVelocity = new Vector2(vx, vy); m_translation = m_translateVelocity * Time.deltaTime; } else { IsTranslating = false; } } }