Super Sexy Properties Panel

Properties panel now allows nested lists with foldouts, item selection and click callbacks
Properties Panel UI has a new look
Refactored lambdas in control module code into methods for readability
This commit is contained in:
hyperion51 2020-05-31 03:10:11 +01:00
commit 50a8059ed4
43 changed files with 894 additions and 916 deletions

View file

@ -8,12 +8,11 @@ using UnityEngine.UI;
public class FlatButton : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerClickHandler
{
public event Action<FlatButton> OnClick;
public event Action OnClick;
public float StateDamping;
// public RectTransform Tab;
public Image Fill;
public Image Outline;
public TextMeshProUGUI Label;
public VerticalLayoutGroup LabelLayout;
public FlatButtonState CurrentState = FlatButtonState.Unselected;
public bool DisableClickWhenSelected = true;
@ -50,7 +49,7 @@ public class FlatButton : MonoBehaviour, IPointerEnterHandler, IPointerExitHandl
{
Fill.color = ClickAppearance.FillColor;
Outline.color = ClickAppearance.OutlineColor;
OnClick?.Invoke(this);
OnClick?.Invoke();
}
}

View file

@ -0,0 +1,68 @@
using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class FlatFlatButton : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerClickHandler
{
public event Action OnClick;
public float StateDamping;
// public RectTransform Tab;
public Image Fill;
public FlatButtonState CurrentState = FlatButtonState.Unselected;
public bool DisableClickWhenSelected = true;
public Color UnselectedColor;
public Color SelectedColor;
public Color HoverColor;
public Color ClickColor;
public void OnPointerEnter(PointerEventData eventData)
{
if (OnClick != null && CurrentState == FlatButtonState.Unselected)
CurrentState = FlatButtonState.Hover;
}
public void OnPointerExit(PointerEventData eventData)
{
if (CurrentState == FlatButtonState.Hover)
CurrentState = FlatButtonState.Unselected;
}
public void OnPointerClick(PointerEventData eventData)
{
if (!DisableClickWhenSelected || CurrentState != FlatButtonState.Selected)
{
if (OnClick != null)
{
Fill.color = ClickColor;
OnClick();
}
}
}
private void Update()
{
Color appearance;
switch (CurrentState)
{
case FlatButtonState.Unselected:
appearance = UnselectedColor;
break;
case FlatButtonState.Selected:
appearance = SelectedColor;
break;
case FlatButtonState.Hover:
appearance = HoverColor;
break;
default:
appearance = Color.white;
break;
}
Fill.color = Color.Lerp(Fill.color, appearance, StateDamping * Time.deltaTime);
}
}

View file

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

View file

@ -24,7 +24,7 @@ public class LoginScreen : MonoBehaviour
LoginButton.CurrentState = FlatButtonState.Selected;
CultClient.AddMessageListener<LoginSuccessMessage>(success => SceneManager.LoadScene("Main"));
CultClient.OnError += s => Error.text = s;
RegisterButton.OnClick += _ =>
RegisterButton.OnClick += () =>
{
if(!CultClient.Connected)
CultClient.Connect(Server.text);
@ -42,7 +42,7 @@ public class LoginScreen : MonoBehaviour
RegisterButton.CurrentState = FlatButtonState.Selected;
}
};
LoginButton.OnClick += _ =>
LoginButton.OnClick += () =>
{
if(!CultClient.Connected)
CultClient.Connect(Server.text);

View file

@ -0,0 +1,68 @@
using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class PropertiesList : ReadOnlyPropertiesPanel
{
public Image FoldoutIcon;
public float FoldoutRotationDamping;
public FlatFlatButton Button;
public VerticalLayoutGroup LayoutGroup;
public int FoldedPadding = -8;
public int ExpandedPadding = 8;
private bool _expanded = false;
private float _targetFoldoutRotation = 0;
private float _foldoutRotation = 0;
public bool Expanded
{
set
{
_expanded = value;
var padding = LayoutGroup.padding;
padding = new RectOffset(padding.left, padding.right, padding.top, _expanded ? ExpandedPadding : FoldedPadding);
LayoutGroup.padding = padding;
foreach (var property in Properties) property.SetActive(_expanded);
_targetFoldoutRotation = _expanded ? -90 : 0;
}
}
public void Start()
{
Button.OnClick += ToggleExpand;
}
public void ToggleExpand() => Expanded = !_expanded;
private void Update()
{
_foldoutRotation =
Mathf.Lerp(_foldoutRotation, _targetFoldoutRotation, FoldoutRotationDamping * Time.deltaTime);
FoldoutIcon.transform.localRotation = Quaternion.Euler(0,0, _foldoutRotation);
}
public new PropertyLabel AddProperty(string name, Func<string> value = null, Action onClick = null)
{
var prop = base.AddProperty(name, value, onClick);
prop.gameObject.SetActive(false);
return prop;
}
public new PropertiesList AddList(string name)
{
var list = base.AddList(name);
list.gameObject.SetActive(false);
return list;
}
public new RectTransform AddSection(string name)
{
var section = base.AddSection(name);
section.gameObject.SetActive(false);
return section;
}
}

View file

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

View file

@ -9,9 +9,11 @@ public class PropertyLabel : MonoBehaviour
public TextMeshProUGUI Name;
public TextMeshProUGUI Value;
public Func<string> ValueFunction;
public FlatFlatButton Button;
private void Update()
{
Value.text = ValueFunction?.Invoke() ?? "";
if(ValueFunction != null)
Value.text = ValueFunction();
}
}

View file

@ -8,60 +8,67 @@ public class ReadOnlyPropertiesPanel : MonoBehaviour
{
public TextMeshProUGUI Title;
public RectTransform SectionPrefab;
public RectTransform ListPrefab;
public RectTransform ListItemPrefab;
public PropertyLabel ListPropertyPrefab;
public PropertiesList ListPrefab;
public PropertyLabel PropertyPrefab;
public bool ChildRadioSelection;
private List<GameObject> _properties = new List<GameObject>();
protected List<GameObject> Properties = new List<GameObject>();
protected List<FlatFlatButton> Buttons = new List<FlatFlatButton>();
protected FlatFlatButton SelectedChild;
public void Clear()
{
foreach(var property in _properties)
foreach(var property in Properties)
Destroy(property);
Properties.Clear();
Buttons.Clear();
SelectedChild = null;
}
public void AddProperty(string name, Func<string> value)
public PropertyLabel AddProperty(string name, Func<string> value = null, Action onClick = null)
{
var property = Instantiate(PropertyPrefab, transform);
property.Name.text = name;
property.Value.text = value();
property.Value.text = value?.Invoke() ?? "";
property.ValueFunction = value;
_properties.Add(property.gameObject);
if (ChildRadioSelection)
{
Buttons.Add(property.Button);
property.Button.OnClick += () =>
{
if (SelectedChild != null)
SelectedChild.CurrentState = FlatButtonState.Unselected;
SelectedChild = property.Button;
SelectedChild.CurrentState = FlatButtonState.Selected;
onClick?.Invoke();
};
}
else if(onClick!=null) property.Button.OnClick += onClick;
Properties.Add(property.gameObject);
return property;
}
public void AddSection(string name)
public RectTransform AddSection(string name)
{
var section = Instantiate(SectionPrefab, transform);
section.GetComponentInChildren<TextMeshProUGUI>().text = name;
_properties.Add(section.gameObject);
Properties.Add(section.gameObject);
return section;
}
public void AddList(string name, IEnumerable<string> elements)
public PropertiesList AddList(string name) //, IEnumerable<(string, Func<string>)> elements)
{
var list = Instantiate(ListPrefab, transform);
list.GetComponentInChildren<TextMeshProUGUI>().text = name;
foreach (var element in elements)
{
var item = Instantiate(ListItemPrefab, list);
item.GetComponentInChildren<TextMeshProUGUI>().text = element;
_properties.Add(item.gameObject);
}
_properties.Add(list.gameObject);
}
public void AddList(string name, IEnumerable<(string, Func<string>)> elements)
{
var list = Instantiate(ListPrefab, transform);
list.GetComponentInChildren<TextMeshProUGUI>().text = name;
foreach (var element in elements)
{
var item = Instantiate(ListPropertyPrefab, list);
item.Name.text = element.Item1;
item.Value.text = element.Item2();
item.ValueFunction = element.Item2;
_properties.Add(item.gameObject);
}
_properties.Add(list.gameObject);
list.Title.text = name;
// foreach (var element in elements)
// {
// var item = Instantiate(PropertyPrefab, list);
// item.Name.text = element.Item1;
// item.Value.text = element.Item2();
// item.ValueFunction = element.Item2;
// Properties.Add(item.gameObject);
// }
Properties.Add(list.gameObject);
return list;
}
}

View file

@ -15,7 +15,7 @@ public class TabGroup : MonoBehaviour
_tabButtons = GetComponentsInChildren<TabButton>();
foreach (var thisButton in _tabButtons)
{
thisButton.OnClick += button =>
thisButton.OnClick += () =>
{
if (_currentButton != null)
{