Skip to content

Commit

Permalink
draggables lift and change cursor on hover
Browse files Browse the repository at this point in the history
  • Loading branch information
levimhuillet committed Sep 25, 2024
1 parent 15a82b8 commit 75e613c
Show file tree
Hide file tree
Showing 7 changed files with 225 additions and 1 deletion.
5 changes: 5 additions & 0 deletions Assets/Project/Prefabs/GameConsts.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,8 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
SkyboxDist: 1000
IncorrectIDPenalty: -1
CorrectColor: {r: 0, g: 1, b: 0, a: 1}
IncorrectColor: {r: 1, g: 0, b: 0, a: 1}
DefaultCursor: {fileID: 0}
GrabCursor: {fileID: 2800000, guid: 82073a8bfd6b8488a89b8796d500d12d, type: 3}
4 changes: 4 additions & 0 deletions Assets/Project/Scripts/Data/InstrumentData/DataSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,12 @@ protected override void SetGrabbed(bool grabbed, bool dragging) {
if (grabbed) {
transform.SetAsLastSibling();
GameMgr.Events.Dispatch(GameEvents.DraggableGrabbed, DraggableFlags);
SetGrabCursor();
SetLiftPos(true);
} else {
GameMgr.Events.Dispatch(GameEvents.DraggableDropped, DraggableFlags);
SetDefaultCursor();
SetLiftPos(false);
}

DragVisual.gameObject.SetActive(grabbed);
Expand Down
3 changes: 3 additions & 0 deletions Assets/Project/Scripts/General/GameConsts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public class GameConsts : MonoBehaviour

public Color CorrectColor = Color.green;
public Color IncorrectColor = Color.red;

public Texture2D DefaultCursor;
public Texture2D GrabCursor;
}

static public class GameEvents
Expand Down
71 changes: 70 additions & 1 deletion Assets/Project/Scripts/UI/AbstractDraggable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.InputSystem;

namespace AstroLab {
[Flags]
Expand All @@ -15,15 +16,30 @@ public enum DraggableFlags {
Spectrum = 0x20,
}

public abstract class AbstractDraggable : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler, IPointerClickHandler {
public abstract class AbstractDraggable : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler, IPointerClickHandler, IPointerEnterHandler, IPointerExitHandler {

private static float LIFT_DISTANCE = 5;

[SerializeField] public DraggableFlags DraggableFlags;
[SerializeField] protected CanvasGroup BodyGroup;
[SerializeField] protected float GrabScaleFactor = 1f;

[Header("Hover")]
[SerializeField] protected bool m_ChangesCursor = true;
[SerializeField] protected bool m_LiftOnHover = true;

protected bool m_Grabbed = false;
protected bool m_OverMain = false;
protected float m_LiftOrigin;

#region Unity Callbacks

private void Awake()
{
m_LiftOrigin = BodyGroup.transform.localPosition.y;
}

#endregion // UnityCallbacks

protected virtual void SetGrabbed(bool grabbed, bool dragging) {
if (m_Grabbed == grabbed) return;
Expand All @@ -49,6 +65,32 @@ protected virtual void MoveWithMouse(PointerEventData eventData = null) {
}
}

protected virtual void SetDefaultCursor() {
GameConsts consts = FindObjectOfType<GameConsts>();

Cursor.SetCursor(consts.DefaultCursor, Vector2.zero, CursorMode.Auto);
}

protected virtual void SetGrabCursor() {
GameConsts consts = FindObjectOfType<GameConsts>();

Cursor.SetCursor(consts.GrabCursor, Vector2.zero, CursorMode.Auto);
}

protected virtual void SetLiftPos(bool lifted) {
if (lifted) {
Vector2 currPos = BodyGroup.transform.localPosition;
currPos.y = m_LiftOrigin + LIFT_DISTANCE;
BodyGroup.transform.localPosition = currPos;
}
else
{
Vector2 currPos = BodyGroup.transform.localPosition;
currPos.y = m_LiftOrigin;
BodyGroup.transform.localPosition = currPos;
}
}


#region Pointer Events
public virtual void OnBeginDrag(PointerEventData eventData) {
Expand All @@ -66,6 +108,33 @@ public virtual void OnEndDrag(PointerEventData eventData) {
public virtual void OnPointerClick(PointerEventData eventData) {
// TODO: SetGrabbed(!m_Grabbed, false)
}

public virtual void OnPointerEnter(PointerEventData eventData)
{
m_OverMain = true;

if (m_ChangesCursor) {
SetGrabCursor();
}
if (m_LiftOnHover) {
SetLiftPos(true);
}
}

public virtual void OnPointerExit(PointerEventData eventData)
{
m_OverMain = false;

if (!m_Grabbed) {
if (m_ChangesCursor) {
SetDefaultCursor();
}
if (m_LiftOnHover) {
SetLiftPos(false);
}
}
}

#endregion //Pointer Events
}
}
8 changes: 8 additions & 0 deletions Assets/Project/Textures/Cursors.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added Assets/Project/Textures/Cursors/Touch@2x-blue.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
135 changes: 135 additions & 0 deletions Assets/Project/Textures/Cursors/Touch@2x-blue.png.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 75e613c

Please sign in to comment.