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

163 lines
5.4 KiB
C#
Raw Normal View History

2022-02-03 22:20:26 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
2022-02-03 22:20:26 +00:00
using TMPro;
using Starpelly;
2022-02-03 22:20:26 +00:00
2022-03-14 14:21:05 +00:00
using HeavenStudio.Util;
2022-02-04 22:16:22 +00:00
2022-03-14 14:21:05 +00:00
namespace HeavenStudio.Editor
2022-02-03 22:20:26 +00:00
{
public class EventPropertyPrefab : MonoBehaviour
{
public TMP_Text caption;
2022-02-04 22:16:22 +00:00
[SerializeField] private EventParameterManager parameterManager;
[Header("Integer and Float")]
[Space(10)]
2022-02-03 22:20:26 +00:00
public Slider slider;
public TMP_InputField inputField;
[Header("Boolean")]
[Space(10)]
public Toggle toggle;
2022-02-04 22:16:22 +00:00
[Header("Dropdown")]
[Space(10)]
public TMP_Dropdown dropdown;
[Header("Color")]
[Space(10)]
public Button ColorBTN;
public RectTransform ColorTable;
public bool colorTableActive;
public ColorPreview colorPreview;
2022-02-03 22:20:26 +00:00
private string propertyName;
2022-02-03 22:20:26 +00:00
public void SetProperties(string propertyName, object type, string caption)
{
this.propertyName = propertyName;
this.caption.text = caption;
var objType = type.GetType();
if (objType == typeof(EntityTypes.Integer))
2022-02-04 22:16:22 +00:00
{
var integer = ((EntityTypes.Integer)type);
2022-02-03 22:20:26 +00:00
2022-02-04 22:16:22 +00:00
slider.minValue = integer.min;
slider.maxValue = integer.max;
2022-02-04 22:16:22 +00:00
slider.value = Mathf.RoundToInt(System.Convert.ToSingle(parameterManager.entity[propertyName]));
inputField.text = slider.value.ToString();
2022-02-04 22:16:22 +00:00
slider.onValueChanged.AddListener(delegate
{
inputField.text = slider.value.ToString();
parameterManager.entity[propertyName] = (int)slider.value;
2022-02-06 09:28:32 +00:00
});
inputField.onEndEdit.AddListener(delegate
{
slider.value = Mathf.RoundToInt(System.Convert.ToSingle(System.Convert.ToSingle(inputField.text)));
parameterManager.entity[propertyName] = (int)slider.value;
2022-02-04 22:16:22 +00:00
});
}
else if (objType == typeof(EntityTypes.Float))
{
var fl = ((EntityTypes.Float)type);
slider.minValue = fl.min;
slider.maxValue = fl.max;
slider.value = System.Convert.ToSingle(parameterManager.entity[propertyName]);
inputField.text = slider.value.ToString("G");
slider.onValueChanged.AddListener(delegate
{
var newValue = (float)System.Math.Round(slider.value, 4);
inputField.text = newValue.ToString("G");
parameterManager.entity[propertyName] = newValue;
});
inputField.onEndEdit.AddListener(delegate
{
slider.value = (float)System.Math.Round(System.Convert.ToSingle(inputField.text), 4);
parameterManager.entity[propertyName] = slider.value;
});
}
else if(type is bool)
{
toggle.isOn = (bool)type;
toggle.onValueChanged.AddListener(delegate
{
parameterManager.entity[propertyName] = toggle.isOn;
});
}
else if (objType.IsEnum)
2022-02-04 22:16:22 +00:00
{
List<TMP_Dropdown.OptionData> dropDownData = new List<TMP_Dropdown.OptionData>();
var vals = Enum.GetValues(objType);
var selected = 0;
for (int i = 0; i < vals.Length; i++)
2022-02-04 22:16:22 +00:00
{
string name = Enum.GetNames(objType)[i];
2022-02-04 22:16:22 +00:00
TMP_Dropdown.OptionData optionData = new TMP_Dropdown.OptionData();
2022-02-03 22:20:26 +00:00
2022-02-04 22:16:22 +00:00
optionData.text = name;
dropDownData.Add(optionData);
if ((int)vals.GetValue(i) == (int)parameterManager.entity[propertyName])
selected = i;
2022-02-04 22:16:22 +00:00
}
dropdown.AddOptions(dropDownData);
dropdown.value = selected;
2022-02-04 22:16:22 +00:00
dropdown.onValueChanged.AddListener(delegate
{
parameterManager.entity[propertyName] = (int)Enum.GetValues(objType).GetValue(dropdown.value);
2022-02-04 22:16:22 +00:00
});
}
else if (objType == typeof(Color))
{
colorPreview.colorPicker.onColorChanged += delegate
{
parameterManager.entity[propertyName] = (Color)colorPreview.colorPicker.color;
};
Color paramCol = (Color)parameterManager.entity[propertyName];
ColorBTN.onClick.AddListener(delegate
{
ColorTable.gameObject.SetActive(true);
colorTableActive = true;
colorPreview.ChangeColor(paramCol);
});
colorPreview.ChangeColor(paramCol);
ColorTable.gameObject.SetActive(false);
}
}
private void Update()
{
if (colorTableActive)
{
if (!Editor.MouseInRectTransform(ColorTable))
{
if (Input.GetMouseButtonDown(0))
{
ColorTable.gameObject.SetActive(false);
colorTableActive = false;
}
}
}
2022-02-03 22:20:26 +00:00
}
}
}