public static void InitViewportControl(DragReceiver drag_) { _drag = drag_; _drag.WhenDragBegin = d => { _focusOverride = _dragFocusOverride; _dragInertiaValue = 0; CameraSystem.WorldPosition = MainCamera.transform.position; }; _drag.WhenDrag = (d, offset, pos) => { var targetX = CameraSystem.X + offset.x * UnitPerPixel; var targetY = CameraSystem.Y + offset.y * UnitPerPixel; if (PositionLimit != null) { (targetX, targetY) = PositionLimit.Limit(targetX, targetY); } MainCamera.transform.position = CameraSystem.SystemToWorld( targetX, targetY, CameraSystem.Z); OnDrag.Invoke(offset, new Vector3(targetX, targetY, CameraSystem.Z)); }; _drag.WhenDragEnd = (d, delta) => { _focusOverride = false; _dragInertiaValue = delta.magnitude; _dragInertiaDelta = delta / _dragInertiaValue; CameraSystem.WorldPosition = MainCamera.transform.position; }; _drag.WhenPinch = (d, start, distance) => { var diff = (distance - start) * zoomSetting.dragFactor * zoomSetting.range * _screenHeightInverse; TargetZoom(_zoomTarget - diff); }; ViewportControl(true); }
public void Init(Vector2 extent, Vector2 center) { ViewEnv.ViewportLimit = this; limit = new PositionLimit(); borderCenter = center; borderExtent = extent; ViewEnv.PositionLimit = limit; OnZoom(ViewEnv.Zoom); }
public static void TryLimitPosition() { if (PositionLimit is null) { return; } (CameraSystem.X, CameraSystem.Y) = PositionLimit.Limit(CameraSystem.X, CameraSystem.Y); MainCamera.transform.position = CameraSystem.WorldPosition; }
private void Update(float deltaTime) { CheckScreenSize(); if (_fadeMessage != null) { foreach (var m in _fadeMessage) { m.Fade(deltaTime); } } if (Focus != null && !_focusOverride) { var focus = CameraSystem.WorldToSystemXY(Focus.position) + FocusOffset; var dir = focus - CameraSystem.PositionXY; var dist = dir.magnitude; var move = _focusStep * deltaTime; if (move >= dist) { CameraSystem.PositionXY = focus; if (_focusOnce) { Focus = null; } } else { CameraSystem.PositionXY += dir / dist * Mathf.Lerp(0.5f, 2f, dist) * _focusStep * deltaTime; } MainCamera.transform.position = CameraSystem.WorldPosition; } if (_dragInertia && _dragInertiaValue > _dragInertiaThreshold) { _dragInertiaValue *= _dragInertiaDamping; var inertia = _dragInertiaValue * _dragInertiaDelta * UnitPerPixel; var targetX = CameraSystem.X - inertia.x; var targetY = CameraSystem.Y - inertia.y; if (PositionLimit != null) { (targetX, targetY) = PositionLimit.Limit(targetX, targetY); } MoveToSystemPos(new Vector2(targetX, targetY)); } if (PositionLimit != null && PositionLimit.Unstable && !Input.anyKey) { (CameraSystem.X, CameraSystem.Y) = PositionLimit.ElasticPull(); MainCamera.transform.position = CameraSystem.WorldPosition; } if (_zoomTarget != Zoom) { if (_zoomTransitTime > 0) { _zoomTransitTime -= deltaTime; ZoomValue(Mathf.SmoothStep(_zoomTarget, _zoomStart, _zoomTransitTime / _zoomTransitDuration)); } else { ZoomValue(_zoomTarget); } } Vector2 mp = Input.mousePosition; if (mp.x < 0 || mp.y < 0 || mp.x > Screen.width || mp.y > Screen.height) { return; } if (Input.GetMouseButtonDown(0)) { ClickVisual?.EmitAt(UICamera.ScreenToWorldPoint(mp), UIViewScale); } if (_drag != null && _drag.interactable && Input.mouseScrollDelta.y != 0) { _zoomTransitTime = 0; TargetZoom(_zoomTarget - Input.mouseScrollDelta.y * zoomSetting.scrollStep); } }