using System.Collections.Generic; using System.Linq; using Superlazy; using Superlazy.UI; using UnityEditor; using UnityEngine; public static class SLBindingEntityViewUtil { private static SLUIComponent currentComponent; private static readonly Dictionary sessionView = new Dictionary(); public static void ViewEntity(this SLUIComponent component) { if (Application.isPlaying == false) return; if (component.isActiveAndEnabled == false) return; var parent = component.bindParent; if (component is SLUIObjectOnOff) { parent = component as SLUIObjectOnOff; } if (parent == null) return; var on = EditorGUILayout.Foldout(component == currentComponent, parent.BindPath); if (on) { if (currentComponent != component) sessionView.Clear(); currentComponent = component; SLBindingEntityView.UpdateView(SLGame.Session.Get(parent.BindPath), "", 1, sessionView); } } } public class SLBindingEntityView { public static void UpdateView(SLEntity e, string prefix, int depth, Dictionary view) { var changed = SLEntity.Empty; ViewTree(e, prefix, depth, view, changed); ChangeView(e, changed); } private static void ViewTree(SLEntity e, string prefix, int depth, Dictionary view, SLEntity changed) { foreach (var tree in e.OrderBy(child => child.IsValue ? "B" + child.ID : "A" + child.ID)) { var id = prefix + tree.ID; if (view.ContainsKey(id) == false) { view[id] = false; } if (tree.IsValue == false) { EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("", GUILayout.Width(10 * depth)); view[id] = EditorGUILayout.Foldout(view[id], tree.ToString()); EditorGUILayout.EndHorizontal(); if (view[id]) { ViewTree(tree, id + ".", depth + 1, view, changed[tree.ID]); } } else { EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("", GUILayout.Width(10 * depth)); string changeValue; if (tree.ToString() == "True") { changeValue = EditorGUILayout.Toggle(tree.ID, e[tree.ID]).ToString(); } else if (tree.IsNumeric) { changeValue = EditorGUILayout.DoubleField(tree.ID, e[tree.ID]).ToString(); } else { changeValue = EditorGUILayout.TextField(tree.ID, e[tree.ID]); } if (changeValue != e[tree.ID]) { changed[tree.ID] = changeValue; } EditorGUILayout.EndHorizontal(); } } } private static void ChangeView(SLEntity e, SLEntity changed) { if (changed) { foreach (var entity in changed) { if (entity.IsValue == false) { ChangeView(e[entity.ID], entity); } else { if (e[entity.ID].IsNumeric) { e[entity.ID] = double.Parse(entity); } else if (entity == "False") { e[entity.ID] = false; } else { e[entity.ID] = entity; } } } } } }