split event properties into own scripts

This commit is contained in:
minenice55
2022-08-23 10:27:30 -04:00
parent 2f05667126
commit a0f25ad4a4
14 changed files with 409 additions and 256 deletions

View File

@ -99,46 +99,70 @@ namespace HeavenStudio.Editor
private void AddParam(string propertyName, object type, string caption, string tooltip = "")
{
GameObject prefab = IntegerP;
GameObject input;
var objType = type.GetType();
if (objType == typeof(EntityTypes.Integer))
{
prefab = IntegerP;
input = InitPrefab(prefab, tooltip);
var property = input.GetComponent<NumberPropertyPrefab>();
property.SetProperties(propertyName, type, caption);
}
else if (objType == typeof(EntityTypes.Float))
{
prefab = FloatP;
input = InitPrefab(prefab, tooltip);
var property = input.GetComponent<NumberPropertyPrefab>();
property.SetProperties(propertyName, type, caption);
}
else if(type is bool)
{
prefab = BooleanP;
input = InitPrefab(prefab, tooltip);
var property = input.GetComponent<BoolPropertyPrefab>();
property.SetProperties(propertyName, type, caption);
}
else if (objType.IsEnum)
{
prefab = DropdownP;
input = InitPrefab(prefab, tooltip);
var property = input.GetComponent<EnumPropertyPrefab>();
property.SetProperties(propertyName, type, caption);
}
else if (objType == typeof(Color))
{
prefab = ColorP;
input = InitPrefab(prefab, tooltip);
var property = input.GetComponent<ColorPropertyPrefab>();
property.SetProperties(propertyName, type, caption);
}
else if(objType == typeof(string))
{
prefab = StringP;
input = InitPrefab(prefab, tooltip);
var property = input.GetComponent<StringPropertyPrefab>();
property.SetProperties(propertyName, type, caption);
}
else
{
Debug.LogError("Can't make property interface of type: " + type.GetType());
return;
}
}
private GameObject InitPrefab(GameObject prefab, string tooltip = "")
{
GameObject input = Instantiate(prefab);
input.transform.SetParent(this.gameObject.transform);
input.SetActive(true);
input.transform.localScale = Vector2.one;
if(tooltip != "")
{
if(tooltip != string.Empty)
Tooltip.AddTooltip(input, "", tooltip);
}
var property = input.GetComponent<EventPropertyPrefab>();
property.SetProperties(propertyName, type, caption);
return input;
}
private void DestroyParams()

View File

@ -14,189 +14,16 @@ namespace HeavenStudio.Editor
public class EventPropertyPrefab : MonoBehaviour
{
public TMP_Text caption;
[SerializeField] private EventParameterManager parameterManager;
public EventParameterManager parameterManager;
public string propertyName;
[Header("Integer and Float")]
[Space(10)]
public Slider slider;
public TMP_InputField inputField;
public void SetProperties(string propertyName, object type, string caption) {}
[Header("Boolean")]
[Space(10)]
public Toggle toggle;
[Header("Dropdown")]
[Space(10)]
public TMP_Dropdown dropdown;
[Header("Color")]
[Space(10)]
public Button ColorBTN;
public RectTransform ColorTable;
public bool colorTableActive;
public ColorPreview colorPreview;
[Header("String")] //why wasn't this a thing before
[Space(10)]
public TMP_InputField inputFieldString;
private string propertyName;
public void SetProperties(string propertyName, object type, string caption)
public void InitProperties(string propertyName, string caption)
{
this.parameterManager = EventParameterManager.instance;
this.propertyName = propertyName;
this.caption.text = caption;
switch (type)
{
case EntityTypes.Integer integer:
slider.minValue = integer.min;
slider.maxValue = integer.max;
slider.wholeNumbers = true;
slider.value = Convert.ToSingle(parameterManager.entity[propertyName]);
inputField.text = slider.value.ToString();
slider.onValueChanged.AddListener(
_ =>
{
inputField.text = slider.value.ToString();
parameterManager.entity[propertyName] = (int) slider.value;
}
);
inputField.onSelect.AddListener(
_ =>
Editor.instance.editingInputField = true
);
inputField.onEndEdit.AddListener(
_ =>
{
slider.value = Convert.ToSingle(inputField.text);
parameterManager.entity[propertyName] = (int) slider.value;
Editor.instance.editingInputField = false;
}
);
break;
case EntityTypes.Float fl:
slider.minValue = fl.min;
slider.maxValue = fl.max;
slider.value = Convert.ToSingle(parameterManager.entity[propertyName]);
inputField.text = slider.value.ToString("G");
slider.onValueChanged.AddListener(
_ =>
{
var newValue = (float) Math.Round(slider.value, 4);
inputField.text = newValue.ToString("G");
parameterManager.entity[propertyName] = newValue;
}
);
inputField.onSelect.AddListener(
_ =>
Editor.instance.editingInputField = true
);
inputField.onEndEdit.AddListener(
_ =>
{
slider.value = (float) Math.Round(Convert.ToSingle(inputField.text), 4);
parameterManager.entity[propertyName] = slider.value;
Editor.instance.editingInputField = false;
}
);
break;
case bool _:
// ' (bool)type ' always results in false
toggle.isOn = Convert.ToBoolean(parameterManager.entity[propertyName]);
toggle.onValueChanged.AddListener(
_ => parameterManager.entity[propertyName] = toggle.isOn
);
break;
// case EntityTypes.SerializableColor _:
case Color _:
colorPreview.colorPicker.onColorChanged += _ =>
parameterManager.entity[propertyName] = colorPreview.colorPicker.color;
Color paramCol = parameterManager.entity[propertyName];
ColorBTN.onClick.AddListener(
() =>
{
ColorTable.gameObject.SetActive(true);
colorTableActive = true;
colorPreview.ChangeColor(paramCol);
}
);
colorPreview.ChangeColor(paramCol);
ColorTable.gameObject.SetActive(false);
break;
case string _:
inputFieldString.text = (string) parameterManager.entity[propertyName];
inputFieldString.onSelect.AddListener(
_ =>
Editor.instance.editingInputField = true
);
inputFieldString.onEndEdit.AddListener(
_ =>
{;
parameterManager.entity[propertyName] = inputFieldString.text;
Editor.instance.editingInputField = false;
}
);
break;
case Enum enumKind:
var enumType = enumKind.GetType();
var enumVals = Enum.GetValues(enumType);
var enumNames = Enum.GetNames(enumType).ToList();
// Can we assume non-holey enum?
// If we can we can simplify to dropdown.value = (int) parameterManager.entity[propertyName]
var currentlySelected = (int) parameterManager.entity[propertyName];
var selected = enumVals
.Cast<object>()
.ToList()
.FindIndex(val => (int) val == currentlySelected);
dropdown.AddOptions(enumNames);
dropdown.value = selected;
dropdown.onValueChanged.AddListener(_ =>
parameterManager.entity[propertyName] = (int) enumVals.GetValue(dropdown.value)
);
break;
default:
throw new ArgumentOutOfRangeException(
nameof(type), type, "I don't know how to make a property of this type!"
);
}
}
private void Update()
{
if (colorTableActive)
{
if (!Editor.MouseInRectTransform(ColorTable))
{
if (Input.GetMouseButtonDown(0))
{
ColorTable.gameObject.SetActive(false);
colorTableActive = false;
}
}
}
}
}
}

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: b86ddd3d47bb04e48a61db47242e02ed
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,37 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
using System.Linq;
using TMPro;
using Starpelly;
using HeavenStudio.Util;
using HeavenStudio.Editor;
namespace HeavenStudio.Editor
{
public class BoolPropertyPrefab : EventPropertyPrefab
{
[Header("Boolean")]
[Space(10)]
public Toggle toggle;
new public void SetProperties(string propertyName, object type, string caption)
{
InitProperties(propertyName, caption);
// ' (bool)type ' always results in false
toggle.isOn = Convert.ToBoolean(parameterManager.entity[propertyName]);
toggle.onValueChanged.AddListener(
_ => parameterManager.entity[propertyName] = toggle.isOn
);
}
private void Update()
{
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9aa690f14ccbf9e4bb6bd339d500c3e7
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,61 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
using System.Linq;
using TMPro;
using Starpelly;
using HeavenStudio.Util;
using HeavenStudio.Editor;
namespace HeavenStudio.Editor
{
public class ColorPropertyPrefab : EventPropertyPrefab
{
[Header("Color")]
[Space(10)]
public Button ColorBTN;
public RectTransform ColorTable;
public bool colorTableActive;
public ColorPreview colorPreview;
new public void SetProperties(string propertyName, object type, string caption)
{
InitProperties(propertyName, caption);
colorPreview.colorPicker.onColorChanged += _ =>
parameterManager.entity[propertyName] = colorPreview.colorPicker.color;
Color paramCol = parameterManager.entity[propertyName];
ColorBTN.onClick.AddListener(
() =>
{
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;
}
}
}
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c1c576a0586b70d4395de537078023d5
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,49 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
using System.Linq;
using TMPro;
using Starpelly;
using HeavenStudio.Util;
using HeavenStudio.Editor;
namespace HeavenStudio.Editor
{
public class EnumPropertyPrefab : EventPropertyPrefab
{
[Header("Dropdown")]
[Space(10)]
public TMP_Dropdown dropdown;
new public void SetProperties(string propertyName, object type, string caption)
{
InitProperties(propertyName, caption);
var enumType = type.GetType();
var enumVals = Enum.GetValues(enumType);
var enumNames = Enum.GetNames(enumType).ToList();
// Can we assume non-holey enum?
// If we can we can simplify to dropdown.value = (int) parameterManager.entity[propertyName]
var currentlySelected = (int) parameterManager.entity[propertyName];
var selected = enumVals
.Cast<object>()
.ToList()
.FindIndex(val => (int) val == currentlySelected);
dropdown.AddOptions(enumNames);
dropdown.value = selected;
dropdown.onValueChanged.AddListener(_ =>
parameterManager.entity[propertyName] = (int) enumVals.GetValue(dropdown.value)
);
}
private void Update()
{
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8ada001011e54c74b87c04d7186d5f3c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,101 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
using System.Linq;
using TMPro;
using Starpelly;
using HeavenStudio.Util;
using HeavenStudio.Editor;
namespace HeavenStudio.Editor
{
public class NumberPropertyPrefab : EventPropertyPrefab
{
[Header("Integer and Float")]
[Space(10)]
public Slider slider;
public TMP_InputField inputField;
new public void SetProperties(string propertyName, object type, string caption)
{
InitProperties(propertyName, caption);
switch (type)
{
case EntityTypes.Integer integer:
slider.minValue = integer.min;
slider.maxValue = integer.max;
slider.wholeNumbers = true;
slider.value = Convert.ToSingle(parameterManager.entity[propertyName]);
inputField.text = slider.value.ToString();
slider.onValueChanged.AddListener(
_ =>
{
inputField.text = slider.value.ToString();
parameterManager.entity[propertyName] = (int) slider.value;
}
);
inputField.onSelect.AddListener(
_ =>
Editor.instance.editingInputField = true
);
inputField.onEndEdit.AddListener(
_ =>
{
slider.value = Convert.ToSingle(inputField.text);
parameterManager.entity[propertyName] = (int) slider.value;
Editor.instance.editingInputField = false;
}
);
break;
case EntityTypes.Float fl:
slider.minValue = fl.min;
slider.maxValue = fl.max;
slider.value = Convert.ToSingle(parameterManager.entity[propertyName]);
inputField.text = slider.value.ToString("G");
slider.onValueChanged.AddListener(
_ =>
{
var newValue = (float) Math.Round(slider.value, 4);
inputField.text = newValue.ToString("G");
parameterManager.entity[propertyName] = newValue;
}
);
inputField.onSelect.AddListener(
_ =>
Editor.instance.editingInputField = true
);
inputField.onEndEdit.AddListener(
_ =>
{
slider.value = (float) Math.Round(Convert.ToSingle(inputField.text), 4);
parameterManager.entity[propertyName] = slider.value;
Editor.instance.editingInputField = false;
}
);
break;
default:
throw new ArgumentOutOfRangeException(
nameof(type), type, "I don't know how to make a property of this type!"
);
}
}
private void Update()
{
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 6e9ad350a96f5644dbb1e4686a6bcaed
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,44 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
using System.Linq;
using TMPro;
using Starpelly;
using HeavenStudio.Util;
using HeavenStudio.Editor;
namespace HeavenStudio.Editor
{
public class StringPropertyPrefab : EventPropertyPrefab
{
[Header("String")] //why wasn't this a thing before
[Space(10)]
public TMP_InputField inputFieldString;
new public void SetProperties(string propertyName, object type, string caption)
{
InitProperties(propertyName, caption);
inputFieldString.text = (string) parameterManager.entity[propertyName];
inputFieldString.onSelect.AddListener(
_ =>
Editor.instance.editingInputField = true
);
inputFieldString.onEndEdit.AddListener(
_ =>
{;
parameterManager.entity[propertyName] = inputFieldString.text;
Editor.instance.editingInputField = false;
}
);
}
private void Update()
{
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c01fcc0bb14adee46a4869c1c009850e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: