Skip to content

Commit

Permalink
Scroll on top of Nikke to change scale
Browse files Browse the repository at this point in the history
Signed-off-by: Bayu Satiyo <[email protected]>
  • Loading branch information
kiraio-moe committed Nov 13, 2024
1 parent a2af3db commit 817c445
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 3 deletions.
52 changes: 50 additions & 2 deletions Assets/Scripts/Base/Components/NikkeViewerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Cysharp.Threading.Tasks;
using NikkeViewerEX.Core;
using NikkeViewerEX.Serialization;
using NikkeViewerEX.Utils;
using UnityEngine;
using UnityEngine.InputSystem;

Expand All @@ -25,11 +26,16 @@ public abstract class NikkeViewerBase : MonoBehaviour
public MainControl MainControl { get; private set; }
public SettingsManager SettingsManager { get; private set; }
public InputManager InputManager { get; private set; }
readonly SpineHelperBase spineHelper = new SpineHelperBase();

readonly float dragSmoothTime = .1f;
Vector2 dragObjectVelocity;
Vector3 dragObjectOffset;

readonly float _scrollSensitivity = 0.05f;
readonly float _nikkeMinScale = 0.2f;
readonly float _nikkeMaxScale = 5f;

/// <summary>
/// Does Nikke currently being dragged?
/// </summary>
Expand Down Expand Up @@ -77,6 +83,12 @@ public virtual void OnDestroy()
InputManager.PointerHold.started -= DragNikke;
}

private void Update()
{
// if (!NikkeData.Lock)
ChangeNikkeScale();
}

/// <summary>
/// Invoke OnSkinChanged event.
/// </summary>
Expand All @@ -94,7 +106,7 @@ public void AddMeshCollider()
meshCollider.sharedMesh = meshFilter.sharedMesh;
}

#region Drag & Drop Nikke
#region Drag & Drop Nikke
/// <summary>
/// Perform Raycast from pointer to start dragging.
/// </summary>
Expand Down Expand Up @@ -165,6 +177,42 @@ private async UniTask PostDragNikke()
await SettingsManager.SaveSettings();
}
}
#endregion

private void ChangeNikkeScale()
{
if (!NikkeData.Lock)
{
Ray ray = Camera.main.ScreenPointToRay(Mouse.current.position.ReadValue());
if (Physics.Raycast(ray, out RaycastHit hit))
{
if (hit.collider.TryGetComponent(out NikkeViewerBase viewer))
{
float scrollDelta = Mouse.current.scroll.ReadValue().y;
if (scrollDelta != 0 && !viewer.NikkeData.Lock)
{
Vector3 newScale = spineHelper.ClampVector3(
hit.transform.localScale
+ _scrollSensitivity * scrollDelta * Vector3.one,
_nikkeMinScale,
_nikkeMaxScale
);
hit.transform.localScale = Vector3.Lerp(
hit.transform.localScale,
newScale,
0.5f
);
NikkeData.Scale = newScale;
SettingsManager.SaveSettings().Forget();
}
}
}
}
}

public void ResetNikkeScale()
{
transform.localScale = Vector3.one;
}
}
#endregion
}
2 changes: 2 additions & 0 deletions Assets/Scripts/Base/Core/InputManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public class InputManager : MonoBehaviour
{
public InputAction PointerClick { get; private set; }
public InputAction PointerHold { get; private set; }
public InputAction PointerPosition { get; private set; }

public InputAction ToggleUI { get; private set; }

[SerializeField]
Expand Down
1 change: 1 addition & 0 deletions Assets/Scripts/Base/Core/SettingsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ private async UniTask InitializeViewerData(NikkeListItem item, Nikke nikkeData)
item.Viewer = viewer;
viewer.NikkeData = nikkeData;
viewer.gameObject.transform.position = nikkeData.Position;
viewer.gameObject.transform.localScale = nikkeData.Scale;

viewer.TouchVoices = (
await UniTask.WhenAll(
Expand Down
1 change: 1 addition & 0 deletions Assets/Scripts/Base/Serialization/NikkeSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class Nikke
public List<string> VoicesSource = new();
public List<string> VoicesPath = new();
public string Skin = "default";
public Vector3 Scale = Vector3.one;
public Vector2 Position;
public bool Lock;
}
Expand Down
16 changes: 16 additions & 0 deletions Assets/Scripts/Base/Utils/SpineHelperBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,21 @@ public static float GetAngle(Vector3 a, Vector3 b)
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
return (angle + 360) % 360;
}

/// <summary>
/// Clamp Vector3.
/// </summary>
/// <param name="value"></param>
/// <param name="min"></param>
/// <param name="max"></param>
/// <returns></returns>
public Vector3 ClampVector3(Vector3 value, float min, float max)
{
return new Vector3(
Mathf.Clamp(value.x, min, max),
Mathf.Clamp(value.y, min, max),
Mathf.Clamp(value.z, min, max)
);
}
}
}
42 changes: 41 additions & 1 deletion Assets/Settings/InputSettings.inputactions
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,28 @@
"name": "PointerClick",
"type": "Button",
"id": "d47a17b9-e7ef-458e-8f90-cc53d9ba5581",
"expectedControlType": "Button",
"expectedControlType": "",
"processors": "",
"interactions": "Tap",
"initialStateCheck": true
},
{
"name": "Scroll",
"type": "PassThrough",
"id": "1da98fcc-e6b2-4d80-9dec-2e9624eb2e14",
"expectedControlType": "Vector2",
"processors": "",
"interactions": "",
"initialStateCheck": true
},
{
"name": "PointerPosition",
"type": "PassThrough",
"id": "3990d670-479d-46ab-ab7a-cb9b993ce2a5",
"expectedControlType": "Vector2",
"processors": "",
"interactions": "",
"initialStateCheck": true
}
],
"bindings": [
Expand Down Expand Up @@ -112,6 +130,28 @@
"action": "PointerClick",
"isComposite": false,
"isPartOfComposite": false
},
{
"name": "",
"id": "2da8bed8-975a-4518-88bf-962695586971",
"path": "<Mouse>/scroll",
"interactions": "",
"processors": "",
"groups": ";Keyboard&Mouse",
"action": "Scroll",
"isComposite": false,
"isPartOfComposite": false
},
{
"name": "",
"id": "854e22fb-9821-4ba8-9560-cb51d671a2a3",
"path": "<Mouse>/position",
"interactions": "",
"processors": "",
"groups": ";Keyboard&Mouse",
"action": "PointerPosition",
"isComposite": false,
"isPartOfComposite": false
}
]
},
Expand Down

0 comments on commit 817c445

Please sign in to comment.