using System; using System.Collections.Generic; using System.Linq; using Nullframes.Intrigues.Graph.Nodes; using Nullframes.Intrigues.Utils; using UnityEditor; using UnityEditor.Experimental.GraphView; using UnityEditor.UIElements; using UnityEngine; using UnityEngine.UIElements; namespace Nullframes.Intrigues.Graph { public static class IEGraphUtility { public static TextField CreateTextField(string value = null, string label = null, EventCallback> onValueChanged = null) { var textField = new TextField() { value = value, label = label }; if (onValueChanged != null) textField.RegisterValueChangedCallback(onValueChanged); return textField; } public static IntegerField CreateIntField(int value = 0, string label = null, EventCallback> onValueChanged = null) { var intField = new IntegerField() { value = value, label = label }; if (onValueChanged != null) intField.RegisterValueChangedCallback(onValueChanged); return intField; } public static void Disable(this VisualElement visualElement) { visualElement.SetEnabled(false); } public static void Enable(this VisualElement visualElement) { visualElement.SetEnabled(true); } public static void Hide(this VisualElement visualElement) { visualElement.style.display = DisplayStyle.None; } public static Rect CalculateRectToFitAllEx(this GraphView graphView, VisualElement container, bool reachedFirstChild = false) { Rect rectToFit = container.layout; graphView.graphElements.ForEach(ge => { var rect = new Rect(0.0f, 0.0f, ge.layout.width, ge.layout.height); if (ge is Edge or Port or Group) return; if (!reachedFirstChild) { rectToFit = ge.ChangeCoordinatesTo(graphView.contentViewContainer, rect); reachedFirstChild = true; } else rectToFit = RectUtils.Encompass(rectToFit, ge.ChangeCoordinatesTo(graphView.contentViewContainer, rect)); }); return rectToFit; } public static Rect CalculateRectToFitAllEx(this GraphView graphView, VisualElement container, Func rule, bool reachedFirstChild = false) { Rect rectToFit = container.layout; graphView.graphElements.ForEach(ge => { var rect = new Rect(0.0f, 0.0f, ge.layout.width, ge.layout.height); if (ge is Edge or Port or Group) return; if (!rule.Invoke(ge)) { return; } if (!reachedFirstChild) { rectToFit = ge.ChangeCoordinatesTo(graphView.contentViewContainer, rect); reachedFirstChild = true; } else rectToFit = RectUtils.Encompass(rectToFit, ge.ChangeCoordinatesTo(graphView.contentViewContainer, rect)); }); return rectToFit; } public static void Show(this VisualElement visualElement) { visualElement.style.display = DisplayStyle.Flex; } public static void GetChilds(this VisualElement element, Action action) where T : VisualElement { foreach (var e in element.Children()) if (e is T visualElement) action.Invoke(visualElement); } public static void GetChilds(this VisualElement element, Action action) { foreach (var _ in element.Children()) action.Invoke(); } public static T GetChild(this VisualElement element) where T : VisualElement { foreach (var child in element.Children()) if (child is T visualElement) return visualElement; return null; } public static List CloneNodeOutputs(this IEnumerable nodeOutputs) { return nodeOutputs.Select(outputData => { var dataCollection = new List(); foreach (var data in outputData.DataCollection) { dataCollection.Add(new PortData(data.NextID, data.NextName, data.ActorID)); } return new OutputData { Name = outputData.Name, Sprite = outputData.Sprite, DataCollection = new List(dataCollection), Disabled = outputData.Disabled, Primary = outputData.Primary, ValidatorMode = outputData.ValidatorMode, HideIfDisable = outputData.HideIfDisable }; }) .ToList(); } public static T GetChild(this VisualElement element, string name) where T : VisualElement { foreach (var child in element.Children().Where(e => e.name == name)) if (child is T visualElement) return visualElement; return null; } public static int GetActiveSlot(this IEGraphView graphView, string groupid, string roleId) { var lst = from ClanMemberNode element in graphView.graphElements.OfType().Where(member => member.RoleID == roleId && member.Group.ID == groupid && member.actor.CurrentState == Actor.IState.Active) select element.RoleID; return lst.Count(); } public static Role GetMisson(this IEDatabase ieDatabase, string missionName) { return ieDatabase.roleDefinitions.FirstOrDefault(m => m.RoleName == missionName); } public static Label CreateLabel(string text) { var label = new Label() { text = text }; return label; } public static ObjectField CreateObjectField(Type type) { var objectField = new ObjectField() { objectType = type, }; return objectField; } public static FloatField CreateFloatField(float value = 0f, string label = null, EventCallback> onValueChanged = null) { var floatField = new FloatField() { value = value, label = label }; if (onValueChanged != null) floatField.RegisterValueChangedCallback(onValueChanged); return floatField; } public static TextField CreateTextArea(string value = null, string label = null, EventCallback> onValueChanged = null) { var textArea = CreateTextField(value, label, onValueChanged); textArea.multiline = true; return textArea; } public static Foldout CreateFoldout(string title, bool collapsed = false) { var foldout = new Foldout() { text = title, value = !collapsed }; return foldout; } public static Button CreateButton(string text, Action onClick = null) { var btn = new Button() { text = text }; btn.RegisterCallback(_ => onClick?.Invoke()); btn.SetBorderRadius(0f); return btn; } public static Toggle CreateToggle(string label) { var toggle = new Toggle(label); return toggle; } public static Port CreatePort(this INode node, string portName = "", Type type = null, Orientation orientation = Orientation.Horizontal, Direction direction = Direction.Output, Port.Capacity capacity = Port.Capacity.Single) { var port = node.InstantiatePort(orientation, direction, capacity, type ?? typeof(bool)); port.portColor = STATIC.DefaultColor; port.ElementAt(1).pickingMode = PickingMode.Position; port.style.height = 35; var connector = port.Q("connector"); connector.style.width = 12; connector.style.height = 12; connector.pickingMode = PickingMode.Position; var cap = connector.Q("cap"); cap.style.width = 8; cap.style.height = 8; if (node.GenericType == GenericNodeType.Family) { if (orientation == Orientation.Horizontal && direction == Direction.Output) { connector.style.borderBottomLeftRadius = 0; connector.style.borderTopLeftRadius = 0; cap.style.borderBottomLeftRadius = 0; cap.style.borderTopLeftRadius = 0; } if (orientation == Orientation.Horizontal && direction == Direction.Input) { connector.style.borderBottomRightRadius = 0; connector.style.borderTopRightRadius = 0; cap.style.borderBottomRightRadius = 0; cap.style.borderTopRightRadius = 0; } if (orientation == Orientation.Vertical && direction == Direction.Output) { connector.style.borderTopLeftRadius = 0; connector.style.borderTopRightRadius = 0; cap.style.borderTopLeftRadius = 0; cap.style.borderTopRightRadius = 0; } if (orientation == Orientation.Vertical && direction == Direction.Input) { connector.style.borderBottomLeftRadius = 0; connector.style.borderBottomRightRadius = 0; cap.style.borderBottomLeftRadius = 0; cap.style.borderBottomRightRadius = 0; } } if (direction == Direction.Input) { port.style.flexDirection = new StyleEnum(FlexDirection.Row); } port.portName = portName; port.name = portName; if (string.IsNullOrEmpty(portName)) port.GetChild