using System.Collections.Generic; using System.Linq; using RMGUI.GraphView; using UnityEngine; using UnityEngine.RMGUI; namespace UnityEditor.Graphing.Drawing { // TODO JOCE Maybe bring SimpleGraphView public. This implements pretty much all that it does. [StyleSheet("Assets/GraphFramework/SerializableGraph/Editor/Drawing/Styles/SerializableGraph.uss")] public class SerializableGraphView : GraphView { public SerializableGraphView() { // Shortcut handler to delete elements AddManipulator(new ShortcutHandler( new Dictionary { {Event.KeyboardEvent("a"), FrameAll}, {Event.KeyboardEvent("f"), FrameSelection}, {Event.KeyboardEvent("o"), FrameOrigin}, {Event.KeyboardEvent("delete"), DeleteSelection}, {Event.KeyboardEvent("#tab"), FramePrev}, {Event.KeyboardEvent("tab"), FrameNext} })); AddManipulator(new ContentZoomer()); AddManipulator(new ContentDragger()); AddManipulator(new RectangleSelector()); AddManipulator(new SelectionDragger()); AddManipulator(new ClickSelector()); InsertChild(0, new GridBackground()); dataMapper[typeof(NodeDrawData)] = typeof(NodeDrawer); } // TODO JOCE Remove the "new" here. Use the base class' impl private new EventPropagation DeleteSelection() { var nodalViewData = GetPresenter(); if (nodalViewData == null) return EventPropagation.Stop; nodalViewData.RemoveElements( selection.OfType().Select(x => x.GetPresenter()), selection.OfType().Select(x => x.GetPresenter()) ); return EventPropagation.Stop; } public override void OnDataChanged() { base.OnDataChanged(); var graphDataSource = GetPresenter(); if (graphDataSource == null) return; var graphAsset = graphDataSource.graphAsset; if (graphAsset == null || selection.Count != 0 || !graphAsset.drawingData.selection.Any()) return; var selectedDrawers = graphDataSource.graphAsset.drawingData.selection .Select(guid => contentViewContainer.children .OfType() .FirstOrDefault(drawer => ((NodeDrawData) drawer.presenter).node.guid == guid)) .ToList(); foreach (var drawer in selectedDrawers) AddToSelection(drawer); } private void PropagateSelection() { var graphDataSource = GetPresenter(); if (graphDataSource == null) return; var selectedNodeGuids = selection.OfType().Select(x => ((NodeDrawData) x.presenter).node.guid); graphDataSource.graphAsset.drawingData.selection = selectedNodeGuids; // TODO: Maybe put somewhere else Selection.activeObject = graphDataSource.graphAsset.GetScriptableObject(); } public override void AddToSelection(ISelectable selectable) { base.AddToSelection(selectable); PropagateSelection(); } public override void RemoveFromSelection(ISelectable selectable) { base.RemoveFromSelection(selectable); PropagateSelection(); } public override void ClearSelection() { base.ClearSelection(); PropagateSelection(); } } }