120 lines
3.2 KiB
C#
120 lines
3.2 KiB
C#
using Cysharp.Threading.Tasks;
|
|
using SampleProject;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
using UVC.Factory.Component;
|
|
using UVC.Locale;
|
|
using UVC.UI.Modal;
|
|
|
|
namespace UVC.Factory.Buttons
|
|
{
|
|
public class UISearchInput : MonoBehaviour
|
|
{
|
|
[SerializeField]
|
|
private TMP_InputField inputField;
|
|
[SerializeField]
|
|
private Button searchButton;
|
|
|
|
private bool initialized = false;
|
|
|
|
private bool isClicked = false;
|
|
|
|
void Awake()
|
|
{
|
|
SceneMain.Instance.Initialized += OnSceneInitialized;
|
|
}
|
|
|
|
private void OnSceneInitialized()
|
|
{
|
|
searchButton.onClick.AddListener(() =>
|
|
{
|
|
isClicked = !isClicked;
|
|
if (isClicked)
|
|
{
|
|
inputField.Select();
|
|
}
|
|
else
|
|
{
|
|
Location(inputField.text);
|
|
inputField.OnDeselect(null);
|
|
}
|
|
});
|
|
|
|
inputField.onEndEdit.AddListener(OnFindLocation);
|
|
|
|
Init();
|
|
}
|
|
|
|
private void OnFindLocation(string locationID)
|
|
{
|
|
if (isClicked == false)
|
|
return;
|
|
if (Input.GetKeyDown(KeyCode.Return) || Input.GetKeyDown(KeyCode.KeypadEnter))
|
|
{
|
|
Location(locationID);
|
|
isClicked = false;
|
|
searchButton.image.color = Color.white;
|
|
inputField.OnDeselect(null);
|
|
}
|
|
}
|
|
|
|
private void Location(string locationID)
|
|
{
|
|
if (string.IsNullOrEmpty(locationID))
|
|
return;
|
|
FactoryObject? factoryObject = FactoryObjectManager.Instance.FindById(locationID);
|
|
if (factoryObject == null)
|
|
{
|
|
Alert.Show(LocalizationManager.Instance.GetString("warning"), locationID + " " +
|
|
LocalizationManager.Instance.GetString("nosearch")).Forget();
|
|
return;
|
|
}
|
|
FactoryCameraController.Instance.FocusOnTarget(factoryObject.transform.position, 15);
|
|
inputField.text = "";
|
|
}
|
|
|
|
private void Init()
|
|
{
|
|
if (!initialized)
|
|
{
|
|
inputField.placeholder.GetComponent<TextMeshProUGUI>().text = LocalizationManager.Instance.GetString("search_location");
|
|
initialized = true;
|
|
}
|
|
isClicked = false;
|
|
}
|
|
|
|
|
|
void OnDestroy()
|
|
{
|
|
SceneMain.Instance.Initialized -= OnSceneInitialized;
|
|
|
|
if (inputField != null)
|
|
{
|
|
inputField = null;
|
|
}
|
|
|
|
if (searchButton != null)
|
|
{
|
|
searchButton.onClick.RemoveAllListeners();
|
|
searchButton = null;
|
|
}
|
|
}
|
|
|
|
|
|
private void Update()
|
|
{
|
|
if (Input.GetMouseButtonDown(0))
|
|
{
|
|
if (EventSystem.current.currentSelectedGameObject != null)
|
|
return;
|
|
inputField.OnDeselect(null);
|
|
inputField.text = "";
|
|
searchButton.image.color = Color.white;
|
|
isClicked = false;
|
|
}
|
|
}
|
|
}
|
|
}
|