126 lines
4.0 KiB
C#
126 lines
4.0 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
|
|
public class EmojiButton : MonoBehaviour
|
|
{
|
|
public static EmojiButton instance { get; private set;}
|
|
[SerializeField]private EventTrigger trigger;
|
|
[SerializeField]private RectTransform radialMenu;
|
|
[SerializeField]private RectTransform button;
|
|
[SerializeField]private float TriggerDistance = 25;
|
|
[SerializeField]private float RadialLayoutSize = 2.7f;
|
|
[SerializeField]private float ButtonSize = 1;
|
|
[SerializeField]private GameObject[] emojiPrefabs;
|
|
[Header("Debug")]
|
|
public float angle;
|
|
public float distance;
|
|
public int curIndex = 0;
|
|
void Awake()
|
|
{
|
|
instance = this;
|
|
|
|
EventTrigger.Entry pointer_down = new EventTrigger.Entry();
|
|
pointer_down.eventID = EventTriggerType.PointerDown;
|
|
pointer_down.callback.AddListener( (eventData) => { OnPointerDown((PointerEventData)eventData); } );
|
|
trigger.triggers.Add(pointer_down);
|
|
EventTrigger.Entry pointer_up = new EventTrigger.Entry();
|
|
pointer_up.eventID = EventTriggerType.PointerUp;
|
|
pointer_up.callback.AddListener( (eventData) => { OnPointerUp((PointerEventData)eventData); } );
|
|
trigger.triggers.Add(pointer_up);
|
|
EventTrigger.Entry drag = new EventTrigger.Entry();
|
|
drag.eventID = EventTriggerType.Drag;
|
|
drag.callback.AddListener( (eventData) => { OnDrag((PointerEventData)eventData); } );
|
|
trigger.triggers.Add(drag);
|
|
}
|
|
|
|
void Update(){
|
|
if(opened){
|
|
for(int i=0; i < radialMenu.childCount-1; i++){
|
|
Transform item = radialMenu.GetChild(radialMenu.childCount-1 -i);
|
|
if(i == curIndex){
|
|
item.localScale = Vector3.Lerp(item.localScale, Vector3.one * 1.2f, 0.1f);
|
|
}else{
|
|
item.localScale = Vector3.Lerp(item.localScale, Vector3.one * 0.75f, 0.1f);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
bool isDown = false;
|
|
Vector2 startPos;
|
|
void OnPointerDown(PointerEventData e){
|
|
isDown = true;
|
|
startPos = e.position;
|
|
}
|
|
void OnPointerUp(PointerEventData e){
|
|
isDown =false;
|
|
if(opened && SceneData.localPlayer != null){
|
|
//Trigger send
|
|
SceneData.localPlayer.GetComponent<SpaceshipController>().ShowEmoji(curIndex, SceneData.localPlayer.transform.position);
|
|
}
|
|
ToggleState(false);
|
|
}
|
|
|
|
public void showEmoji(int index, Vector3 position){
|
|
EffectPool.Spawn(emojiPrefabs[index], position);
|
|
}
|
|
|
|
|
|
void OnDrag(PointerEventData e){
|
|
if(!isDown){
|
|
return;
|
|
}
|
|
|
|
Vector3 targetDir = e.position - (Vector2)button.position;
|
|
angle = Vector3.SignedAngle(targetDir, Vector3.up, Vector3.forward);
|
|
if(angle < 0){
|
|
angle = 360 + angle;
|
|
}
|
|
|
|
distance= Vector2.Distance(startPos, e.position);
|
|
|
|
if(distance > TriggerDistance && !opened){
|
|
ToggleState(true);
|
|
}
|
|
|
|
|
|
if(!opened){return;}
|
|
|
|
curIndex = (int)(((angle / 360f) * (radialMenu.childCount-1)));
|
|
}
|
|
|
|
bool opened =false;
|
|
public void ToggleState(bool val){
|
|
if(opened != val){
|
|
StartCoroutine(toggleState(val));
|
|
}
|
|
}
|
|
|
|
IEnumerator toggleState(bool val){
|
|
opened = val;
|
|
float t = 0;
|
|
float time = 0.25f;
|
|
if(opened){
|
|
radialMenu.gameObject.SetActive(true);
|
|
}else{
|
|
button.gameObject.SetActive(true);
|
|
}
|
|
|
|
while(t < time){
|
|
t+= Time.deltaTime;
|
|
float t2 = t * (1f / time);
|
|
radialMenu.localScale = Vector3.one * (opened ? t2 : (1-t2))* RadialLayoutSize;
|
|
button.localScale = Vector3.one * (opened ? (1-t2) : t2) * ButtonSize;
|
|
yield return new WaitForEndOfFrame();
|
|
}
|
|
|
|
if(opened){
|
|
button.gameObject.SetActive(false);
|
|
}else{
|
|
radialMenu.gameObject.SetActive(false);
|
|
}
|
|
}
|
|
}
|