TO SQUASH: started menu callback calling
This commit is contained in:
@@ -17,6 +17,16 @@ public class MenuButton : MonoBehaviour
|
||||
|
||||
private static Color opaque = new Color(1f, 1f, 1f, 1f);
|
||||
|
||||
private const float holdThreshold = 0.15f;
|
||||
|
||||
private bool pointerDownOnButton = false;
|
||||
|
||||
private bool holdInteraction = false;
|
||||
|
||||
private bool pointerInsideDuringHold = false;
|
||||
|
||||
private float pointerDownTime = 0f;
|
||||
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start()
|
||||
{
|
||||
@@ -27,18 +37,83 @@ public class MenuButton : MonoBehaviour
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
if (Mouse.current == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Check for mouse button press to request key translation and handle hold interactions
|
||||
if (Mouse.current.leftButton.wasPressedThisFrame)
|
||||
{
|
||||
Vector2 mousePosition = Mouse.current.position.ReadValue();
|
||||
Vector2 worldMousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
|
||||
|
||||
if (boxCollider.OverlapPoint(worldMousePosition))
|
||||
if (IsPointerInsideButton())
|
||||
{
|
||||
isButtonPressed = true;
|
||||
key.TranslateTo(row);
|
||||
} else {
|
||||
isButtonPressed = false;
|
||||
pointerDownOnButton = true;
|
||||
holdInteraction = false;
|
||||
pointerInsideDuringHold = true;
|
||||
pointerDownTime = Time.unscaledTime;
|
||||
}
|
||||
else
|
||||
{
|
||||
isButtonPressed = false;
|
||||
pointerDownOnButton = false;
|
||||
holdInteraction = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (pointerDownOnButton && Mouse.current.leftButton.isPressed)
|
||||
{
|
||||
bool pointerInside = IsPointerInsideButton();
|
||||
|
||||
if (!holdInteraction && (Time.unscaledTime - pointerDownTime) >= holdThreshold)
|
||||
{
|
||||
holdInteraction = true;
|
||||
key.OpenKey(false);
|
||||
pointerInsideDuringHold = pointerInside;
|
||||
|
||||
if (!pointerInside)
|
||||
{
|
||||
key.CloseKey();
|
||||
}
|
||||
}
|
||||
|
||||
if (holdInteraction)
|
||||
{
|
||||
if (pointerInside && !pointerInsideDuringHold)
|
||||
{
|
||||
key.OpenKey(false);
|
||||
}
|
||||
else if (!pointerInside && pointerInsideDuringHold)
|
||||
{
|
||||
key.CloseKey();
|
||||
}
|
||||
|
||||
pointerInsideDuringHold = pointerInside;
|
||||
}
|
||||
}
|
||||
|
||||
if (pointerDownOnButton && Mouse.current.leftButton.wasReleasedThisFrame)
|
||||
{
|
||||
bool pointerInside = IsPointerInsideButton();
|
||||
|
||||
if (holdInteraction)
|
||||
{
|
||||
if (pointerInside)
|
||||
{
|
||||
key.InvokeCallback();
|
||||
}
|
||||
|
||||
key.ReleaseKey();
|
||||
}
|
||||
else
|
||||
{
|
||||
key.OpenKey(true);
|
||||
}
|
||||
|
||||
pointerDownOnButton = false;
|
||||
holdInteraction = false;
|
||||
pointerInsideDuringHold = false;
|
||||
}
|
||||
|
||||
if (isButtonPressed)
|
||||
@@ -50,4 +125,16 @@ public class MenuButton : MonoBehaviour
|
||||
spriteRenderer.color = transparent;
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsPointerInsideButton()
|
||||
{
|
||||
if (Camera.main == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
Vector2 mousePosition = Mouse.current.position.ReadValue();
|
||||
Vector2 worldMousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
|
||||
return boxCollider.OverlapPoint(worldMousePosition);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,8 @@ public class MenuKey : MonoBehaviour
|
||||
|
||||
private bool isTranslating = false;
|
||||
|
||||
private bool triggered = false;
|
||||
|
||||
private float translationElapsed = 0f;
|
||||
|
||||
private float translationStartY = 0f;
|
||||
@@ -95,9 +97,42 @@ public class MenuKey : MonoBehaviour
|
||||
targetRow = row;
|
||||
}
|
||||
|
||||
public void InvokeCallback(int _)
|
||||
public void OpenKey(bool trigger)
|
||||
{
|
||||
callbackHolder.InvokeCallback(currentRow);
|
||||
this.triggered = trigger;
|
||||
animator.ResetTrigger("buttonReleased");
|
||||
animator.SetTrigger("buttonClicked");
|
||||
if (!trigger)
|
||||
animator.SetBool("inButton", true);
|
||||
}
|
||||
|
||||
public void CloseKey()
|
||||
{
|
||||
animator.SetBool("inButton", false);
|
||||
}
|
||||
|
||||
public void ReleaseKey()
|
||||
{
|
||||
animator.ResetTrigger("buttonClicked");
|
||||
animator.SetTrigger("buttonReleased");
|
||||
}
|
||||
|
||||
public void InvokeCallback()
|
||||
{
|
||||
if (!isTranslating)
|
||||
{
|
||||
callbackHolder.InvokeCallback(currentRow);
|
||||
}
|
||||
}
|
||||
|
||||
public void TriggerCallback(int _)
|
||||
{
|
||||
if (!isTranslating && triggered)
|
||||
{
|
||||
triggered = false;
|
||||
callbackHolder.InvokeCallback(currentRow);
|
||||
ReleaseKey();
|
||||
}
|
||||
}
|
||||
|
||||
private float EaseOutQuad(float t)
|
||||
|
||||
Reference in New Issue
Block a user