HeavenStudioPlus/Assets/Scripts/LevelEditor/EventSelector/GridGameSelector.cs

246 lines
9 KiB
C#
Raw Normal View History

2022-01-17 00:40:14 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using DG.Tweening;
using Starpelly;
2022-03-14 14:21:05 +00:00
using HeavenStudio.Editor.Track;
2022-03-14 14:21:05 +00:00
namespace HeavenStudio.Editor
2022-01-17 00:40:14 +00:00
{
public class GridGameSelector : MonoBehaviour
{
public string SelectedMinigame;
[Header("Components")]
public GameObject SelectedGameIcon;
2022-01-17 00:40:14 +00:00
public GameObject GameEventSelector;
public GameObject EventRef;
public GameObject CurrentSelected;
public RectTransform GameEventSelectorCanScroll;
private RectTransform GameEventSelectorRect;
2022-01-17 00:40:14 +00:00
private RectTransform eventsParent;
[Header("Properties")]
2022-01-17 19:23:18 +00:00
private Minigames.Minigame mg;
2022-01-17 00:40:14 +00:00
private bool gameOpen;
[SerializeField] private int currentEventIndex;
private int dragTimes;
public float posDif;
public int ignoreSelectCount;
private float selectorHeight;
private float eventSize;
2022-01-17 00:40:14 +00:00
private void Start()
{
GameEventSelectorRect = GameEventSelector.GetComponent<RectTransform>();
selectorHeight = GameEventSelectorRect.rect.height;
eventSize = EventRef.GetComponent<RectTransform>().rect.height;
2022-01-17 00:40:14 +00:00
eventsParent = EventRef.transform.parent.GetChild(2).GetComponent<RectTransform>();
SelectGame("Game Manager", 1);
SetColors();
}
private void Update()
{
if(!(EventParameterManager.instance.active || Conductor.instance.NotStopped()))
2022-01-17 00:40:14 +00:00
{
if (gameOpen)
2022-01-17 00:40:14 +00:00
{
if (Input.GetKeyDown(KeyCode.DownArrow))
{
UpdateIndex(currentEventIndex + 1);
}
else if (Input.GetKeyDown(KeyCode.UpArrow))
{
UpdateIndex(currentEventIndex - 1);
}
2022-01-17 00:40:14 +00:00
}
if (RectTransformUtility.RectangleContainsScreenPoint(GameEventSelectorCanScroll, Input.mousePosition, Editor.instance.EditorCamera) && Input.mouseScrollDelta.y != 0)
2022-01-17 00:40:14 +00:00
{
UpdateIndex(currentEventIndex - Mathf.RoundToInt(Input.mouseScrollDelta.y));
2022-01-17 00:40:14 +00:00
}
}
//moved here so this updates dynamically with window scale
UpdateScrollPosition();
2022-01-17 00:40:14 +00:00
}
#region Functions
public void UpdateIndex(int amount, bool updateCol = true)
{
currentEventIndex = amount;
EventRef.transform.parent.DOKill();
CurrentSelected.transform.DOKill();
if (currentEventIndex < 0)
currentEventIndex = eventsParent.childCount - 1;
else if (currentEventIndex > eventsParent.childCount - 1)
currentEventIndex = 0;
CurrentSelected.transform.DOLocalMoveY(eventsParent.transform.GetChild(currentEventIndex).localPosition.y + eventsParent.transform.localPosition.y, 0.35f).SetEase(Ease.OutExpo);
if (updateCol)
SetColors(currentEventIndex);
}
private void UpdateScrollPosition()
{
selectorHeight = GameEventSelectorRect.rect.height;
eventSize = EventRef.GetComponent<RectTransform>().rect.height;
if (currentEventIndex * eventSize >= selectorHeight/2 && eventsParent.childCount * eventSize >= selectorHeight)
2022-01-17 00:40:14 +00:00
{
if (currentEventIndex * eventSize < eventsParent.childCount * eventSize - selectorHeight/2)
2022-01-17 00:40:14 +00:00
{
EventRef.transform.parent.DOLocalMoveY((currentEventIndex * eventSize) - selectorHeight/2, 0.35f).SetEase(Ease.OutExpo);
}
2022-01-17 00:40:14 +00:00
else
{
EventRef.transform.parent.DOLocalMoveY((eventsParent.childCount * eventSize) - selectorHeight + (eventSize*0.33f), 0.35f).SetEase(Ease.OutExpo);
2022-01-17 00:40:14 +00:00
}
}
else
EventRef.transform.parent.DOLocalMoveY(0, 0.35f).SetEase(Ease.OutExpo);
}
public void SelectGame(string gameName, int index)
{
if (SelectedGameIcon != null)
{
SelectedGameIcon.GetComponent<GridGameSelectorGame>().UnClickIcon();
}
2022-01-17 00:40:14 +00:00
mg = EventCaller.instance.minigames.Find(c => c.displayName == gameName);
SelectedMinigame = gameName;
gameOpen = true;
DestroyEvents();
AddEvents();
// transform.GetChild(index).GetChild(0).gameObject.SetActive(true);
SelectedGameIcon = transform.GetChild(index).gameObject;
SelectedGameIcon.GetComponent<GridGameSelectorGame>().ClickIcon();
2022-01-17 00:40:14 +00:00
currentEventIndex = 0;
UpdateIndex(0, false);
2022-03-04 00:15:56 +00:00
Editor.instance.SetGameEventTitle($"Select game event for {gameName.Replace("\n", "")}");
2022-01-17 00:40:14 +00:00
}
private void AddEvents()
{
2022-02-03 03:58:08 +00:00
if (!EventCaller.FXOnlyGames().Contains(EventCaller.instance.GetMinigame(mg.name)))
2022-01-17 00:40:14 +00:00
{
GameObject sg = Instantiate(EventRef, eventsParent);
sg.GetComponent<TMP_Text>().text = "switchGame";
sg.SetActive(true);
sg.GetComponent<TMP_Text>().color = EditorTheme.theme.properties.EventSelectedCol.Hex2RGB();
}
for (int i = 0; i < mg.actions.Count; i++)
{
if (mg.actions[i].actionName == "switchGame" || mg.actions[i].hidden) continue;
2022-01-17 00:40:14 +00:00
GameObject g = Instantiate(EventRef, eventsParent);
g.GetComponent<TMP_Text>().text = mg.actions[i].actionName;
g.SetActive(true);
}
}
private void DestroyEvents()
{
for (int i = 0; i < transform.childCount; i++)
{
transform.GetChild(i).GetChild(0).gameObject.SetActive(false);
}
for (int i = 0; i < eventsParent.childCount; i++)
{
Destroy(eventsParent.GetChild(i).gameObject);
}
}
private void SetColors(int index = 0)
{
CurrentSelected.GetComponent<Image>().color = EditorTheme.theme.properties.EventSelectedCol.Hex2RGB();
for (int i = 0; i < eventsParent.transform.childCount; i++)
eventsParent.GetChild(i).GetComponent<TMP_Text>().color = EditorTheme.theme.properties.EventNormalCol.Hex2RGB();
eventsParent.GetChild(index).GetComponent<TMP_Text>().color = EditorTheme.theme.properties.EventSelectedCol.Hex2RGB();
}
#endregion
#region Events
public void Drag()
{
2022-02-03 07:28:14 +00:00
if (Conductor.instance.NotStopped() || !Timeline.instance.timelineState.selected) return;
2022-01-17 00:40:14 +00:00
if (Timeline.instance.CheckIfMouseInTimeline() && dragTimes < 1)
{
dragTimes++;
2022-01-23 03:40:53 +00:00
TimelineEventObj eventObj;
2022-01-23 03:40:53 +00:00
2022-01-17 00:40:14 +00:00
if (currentEventIndex == 0)
{
2022-02-03 03:58:08 +00:00
if (EventCaller.FXOnlyGames().Contains(EventCaller.instance.GetMinigame(mg.name)))
{
int index = currentEventIndex + 1;
if (currentEventIndex - 1 > mg.actions.Count)
{
index = currentEventIndex;
}
else if (currentEventIndex - 1 < 0)
{
if (mg.actions[0].actionName == "switchGame")
index = 1;
else
index = 0;
}
eventObj = Timeline.instance.AddEventObject(mg.name + "/" + mg.actions[index].actionName, true, new Vector3(0, 0), null, true, Timeline.RandomID());
}
else
eventObj = Timeline.instance.AddEventObject($"gameManager/switchGame/{mg.name}", true, new Vector3(0, 0), null, true, Timeline.RandomID());
2022-01-17 00:40:14 +00:00
}
else
{
2022-02-03 03:58:08 +00:00
int index = currentEventIndex - 1;
if (mg.actions[0].actionName == "switchGame")
{
index = currentEventIndex + 1;
}
else if (EventCaller.FXOnlyGames().Contains(EventCaller.instance.GetMinigame(mg.name)) && mg.actions[0].actionName != "switchGame")
{
index = currentEventIndex;
}
eventObj = Timeline.instance.AddEventObject(mg.name + "/" + mg.actions[index].actionName, true, new Vector3(0, 0), null, true, Timeline.RandomID());
2022-01-17 00:40:14 +00:00
}
eventObj.isCreating = true;
// CommandManager.instance.Execute(new Commands.Place(eventObj));
2022-01-17 00:40:14 +00:00
}
}
public void Drop()
{
if (Conductor.instance.NotStopped()) return;
dragTimes = 0;
}
#endregion
}
}