-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Can now drag the window by its edges; position saves
- Loading branch information
1 parent
0621227
commit 0edb758
Showing
5 changed files
with
119 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using System; | ||
using UnityEngine; | ||
using UnityEngine.EventSystems; | ||
|
||
namespace KK_QuickAccessBox.UI | ||
{ | ||
internal class MovableWindow : UIBehaviour, IPointerDownHandler, IDragHandler, IPointerUpHandler | ||
{ | ||
public static MovableWindow MakeObjectDraggable(RectTransform clickableDragZone, RectTransform draggableObject, bool preventCameraControl = true) | ||
{ | ||
MovableWindow mv = clickableDragZone.gameObject.AddComponent<MovableWindow>(); | ||
mv.toDrag = draggableObject; | ||
mv.preventCameraControl = preventCameraControl; | ||
return mv; | ||
} | ||
|
||
private Vector2 _cachedDragPosition; | ||
private Vector2 _cachedMousePosition; | ||
private bool _pointerDownCalled; | ||
private BaseCameraControl _cameraControl; | ||
private BaseCameraControl.NoCtrlFunc _noControlFunctionCached; | ||
|
||
public event Action<PointerEventData> onPointerDown; | ||
public event Action<PointerEventData> onDrag; | ||
public event Action<PointerEventData> onPointerUp; | ||
|
||
public RectTransform toDrag; | ||
public bool preventCameraControl; | ||
|
||
protected override void Awake() | ||
{ | ||
base.Awake(); | ||
_cameraControl = FindObjectOfType<BaseCameraControl>(); | ||
} | ||
|
||
public void OnPointerDown(PointerEventData eventData) | ||
{ | ||
if (preventCameraControl && _cameraControl) | ||
{ | ||
_noControlFunctionCached = _cameraControl.NoCtrlCondition; | ||
_cameraControl.NoCtrlCondition = () => true; | ||
} | ||
_pointerDownCalled = true; | ||
_cachedDragPosition = toDrag.position; | ||
_cachedMousePosition = Input.mousePosition; | ||
onPointerDown?.Invoke(eventData); | ||
} | ||
|
||
public void OnDrag(PointerEventData eventData) | ||
{ | ||
if (_pointerDownCalled == false) | ||
return; | ||
toDrag.position = _cachedDragPosition + ((Vector2)Input.mousePosition - _cachedMousePosition); | ||
onDrag?.Invoke(eventData); | ||
} | ||
|
||
public void OnPointerUp(PointerEventData eventData) | ||
{ | ||
if (_pointerDownCalled == false) | ||
return; | ||
if (preventCameraControl && _cameraControl) | ||
_cameraControl.NoCtrlCondition = _noControlFunctionCached; | ||
_pointerDownCalled = false; | ||
onPointerUp?.Invoke(eventData); | ||
} | ||
} | ||
} |