diff --git a/Scripts/Editor/NodeEditor.cs b/Scripts/Editor/NodeEditor.cs index a00704a..1469e2d 100644 --- a/Scripts/Editor/NodeEditor.cs +++ b/Scripts/Editor/NodeEditor.cs @@ -63,13 +63,15 @@ namespace XNodeEditor { public virtual int GetWidth() { Type type = target.GetType(); - if (NodeEditorWindow.nodeWidth.ContainsKey(type)) return NodeEditorWindow.nodeWidth[type]; + int width; + if (NodeEditorWindow.nodeWidth.TryGetValue(type, out width)) return width; else return 208; } public virtual Color GetTint() { Type type = target.GetType(); - if (NodeEditorWindow.nodeTint.ContainsKey(type)) return NodeEditorWindow.nodeTint[type]; + Color color; + if (NodeEditorWindow.nodeTint.TryGetValue(type, out color)) return color; else return Color.white; } @@ -84,7 +86,7 @@ namespace XNodeEditor { [AttributeUsage(AttributeTargets.Class)] public class CustomNodeEditorAttribute : Attribute, - XNodeEditor.Internal.NodeEditorBase.INodeEditorAttrib { + XNodeEditor.Internal.NodeEditorBase.INodeEditorAttrib { private Type inspectedType; /// Tells a NodeEditor which Node type it is an editor for /// Type that this editor can edit @@ -98,5 +100,4 @@ namespace XNodeEditor { } } } - -} +} \ No newline at end of file diff --git a/Scripts/Editor/NodeEditorAction.cs b/Scripts/Editor/NodeEditorAction.cs index 260705d..9682d17 100644 --- a/Scripts/Editor/NodeEditorAction.cs +++ b/Scripts/Editor/NodeEditorAction.cs @@ -90,8 +90,8 @@ namespace XNodeEditor { // Offset portConnectionPoints instantly if a node is dragged so they aren't delayed by a frame. Vector2 offset = node.position - initial; - if (offset.sqrMagnitude > 0){ - foreach (XNode.NodePort output in node.Outputs){ + if (offset.sqrMagnitude > 0) { + foreach (XNode.NodePort output in node.Outputs) { Rect rect; if (portConnectionPoints.TryGetValue(output, out rect)) { rect.position += offset; @@ -381,9 +381,8 @@ namespace XNodeEditor { XNode.NodePort inputPort = port.direction == XNode.NodePort.IO.Input ? port : port.GetConnection(c); XNode.NodePort outputPort = port.direction == XNode.NodePort.IO.Output ? port : port.GetConnection(c); - if (substitutes.ContainsKey(inputPort.node) && substitutes.ContainsKey(outputPort.node)) { - XNode.Node newNodeIn = substitutes[inputPort.node]; - XNode.Node newNodeOut = substitutes[outputPort.node]; + XNode.Node newNodeIn, newNodeOut; + if (substitutes.TryGetValue(inputPort.node, out newNodeIn) && substitutes.TryGetValue(outputPort.node, out newNodeOut)) { newNodeIn.UpdateStaticPorts(); newNodeOut.UpdateStaticPorts(); inputPort = newNodeIn.GetInputPort(inputPort.fieldName); @@ -402,9 +401,10 @@ namespace XNodeEditor { if (IsDraggingPort) { Color col = NodeEditorPreferences.GetTypeColor(draggedOutput.ValueType); - if (!_portConnectionPoints.ContainsKey(draggedOutput)) return; + Rect fromRect; + if (!_portConnectionPoints.TryGetValue(draggedOutput, out fromRect)) return; + Vector2 from = fromRect.center; col.a = 0.6f; - Vector2 from = _portConnectionPoints[draggedOutput].center; Vector2 to = Vector2.zero; for (int i = 0; i < draggedOutputReroutes.Count; i++) { to = draggedOutputReroutes[i]; @@ -435,8 +435,10 @@ namespace XNodeEditor { Vector2 mousePos = Event.current.mousePosition; //Get node position Vector2 nodePos = GridToWindowPosition(node.position); - float width = 200; - if (nodeSizes.ContainsKey(node)) width = nodeSizes[node].x; + float width; + Vector2 size; + if (nodeSizes.TryGetValue(node, out size)) width = size.x; + else width = 200; Rect windowRect = new Rect(nodePos, new Vector2(width / zoom, 30 / zoom)); return windowRect.Contains(mousePos); } diff --git a/Scripts/Editor/NodeEditorBase.cs b/Scripts/Editor/NodeEditorBase.cs index 8ab610e..2a16f18 100644 --- a/Scripts/Editor/NodeEditorBase.cs +++ b/Scripts/Editor/NodeEditorBase.cs @@ -16,14 +16,15 @@ namespace XNodeEditor.Internal { public static T GetEditor(K target) { if (target == null) return null; - if (!editors.ContainsKey(target)) { + T editor; + if (!editors.TryGetValue(target, out editor)) { Type type = target.GetType(); Type editorType = GetEditorType(type); - editors.Add(target, Activator.CreateInstance(editorType) as T); - editors[target].target = target; - editors[target].serializedObject = new SerializedObject(target); + editor = Activator.CreateInstance(editorType) as T; + editor.target = target; + editor.serializedObject = new SerializedObject(target); + editors.Add(target, editor); } - T editor = editors[target]; if (editor.target == null) editor.target = target; if (editor.serializedObject == null) editor.serializedObject = new SerializedObject(target); return editor; @@ -32,7 +33,8 @@ namespace XNodeEditor.Internal { private static Type GetEditorType(Type type) { if (type == null) return null; if (editorTypes == null) CacheCustomEditors(); - if (editorTypes.ContainsKey(type)) return editorTypes[type]; + Type result; + if (editorTypes.TryGetValue(type, out result)) return result; //If type isn't found, try base type return GetEditorType(type.BaseType); } diff --git a/Scripts/Editor/NodeEditorGUI.cs b/Scripts/Editor/NodeEditorGUI.cs index 36823b8..0cfec5b 100644 --- a/Scripts/Editor/NodeEditorGUI.cs +++ b/Scripts/Editor/NodeEditorGUI.cs @@ -226,7 +226,8 @@ namespace XNodeEditor { // Draw full connections and output > reroute foreach (XNode.NodePort output in node.Outputs) { //Needs cleanup. Null checks are ugly - if (!portConnectionPoints.ContainsKey(output)) continue; + Rect fromRect; + if (!_portConnectionPoints.TryGetValue(output, out fromRect)) continue; Color connectionColor = graphEditor.GetTypeColor(output.ValueType); @@ -236,9 +237,10 @@ namespace XNodeEditor { // Error handling if (input == null) continue; //If a script has been updated and the port doesn't exist, it is removed and null is returned. If this happens, return. if (!input.IsConnectedTo(output)) input.Connect(output); - if (!_portConnectionPoints.ContainsKey(input)) continue; + Rect toRect; + if (!_portConnectionPoints.TryGetValue(input, out toRect)) continue; - Vector2 from = _portConnectionPoints[output].center; + Vector2 from = fromRect.center; Vector2 to = Vector2.zero; List reroutePoints = output.GetReroutePoints(k); // Loop through reroute points and draw the path @@ -247,7 +249,8 @@ namespace XNodeEditor { DrawConnection(from, to, connectionColor); from = to; } - to = _portConnectionPoints[input].center; + to = toRect.center; + DrawConnection(from, to, connectionColor); // Loop through reroute points again and draw the points diff --git a/Scripts/Editor/NodeEditorGUILayout.cs b/Scripts/Editor/NodeEditorGUILayout.cs index 405a6e6..3a90e36 100644 --- a/Scripts/Editor/NodeEditorGUILayout.cs +++ b/Scripts/Editor/NodeEditorGUILayout.cs @@ -93,7 +93,8 @@ namespace XNodeEditor { rect.size = new Vector2(16, 16); Color backgroundColor = new Color32(90, 97, 105, 255); - if (NodeEditorWindow.nodeTint.ContainsKey(port.node.GetType())) backgroundColor *= NodeEditorWindow.nodeTint[port.node.GetType()]; + Color tint; + if (NodeEditorWindow.nodeTint.TryGetValue(port.node.GetType(), out tint)) backgroundColor *= tint; Color col = NodeEditorWindow.current.graphEditor.GetTypeColor(port.ValueType); DrawPortHandle(rect, backgroundColor, col); @@ -135,7 +136,8 @@ namespace XNodeEditor { rect.size = new Vector2(16, 16); Color backgroundColor = new Color32(90, 97, 105, 255); - if (NodeEditorWindow.nodeTint.ContainsKey(port.node.GetType())) backgroundColor *= NodeEditorWindow.nodeTint[port.node.GetType()]; + Color tint; + if (NodeEditorWindow.nodeTint.TryGetValue(port.node.GetType(), out tint)) backgroundColor *= tint; Color col = NodeEditorWindow.current.graphEditor.GetTypeColor(port.ValueType); DrawPortHandle(rect, backgroundColor, col); @@ -163,7 +165,8 @@ namespace XNodeEditor { rect.size = new Vector2(16, 16); Color backgroundColor = new Color32(90, 97, 105, 255); - if (NodeEditorWindow.nodeTint.ContainsKey(port.node.GetType())) backgroundColor *= NodeEditorWindow.nodeTint[port.node.GetType()]; + Color tint; + if (NodeEditorWindow.nodeTint.TryGetValue(port.node.GetType(), out tint)) backgroundColor *= tint; Color col = NodeEditorWindow.current.graphEditor.GetTypeColor(port.ValueType); DrawPortHandle(rect, backgroundColor, col); diff --git a/Scripts/Node.cs b/Scripts/Node.cs index 7c52d9b..90b65e3 100644 --- a/Scripts/Node.cs +++ b/Scripts/Node.cs @@ -153,7 +153,8 @@ namespace XNode { /// Returns port which matches fieldName public NodePort GetPort(string fieldName) { - if (ports.ContainsKey(fieldName)) return ports[fieldName]; + NodePort port; + if (ports.TryGetValue(fieldName, out port)) return port; else return null; } diff --git a/Scripts/NodeDataCache.cs b/Scripts/NodeDataCache.cs index b194230..283cc9d 100644 --- a/Scripts/NodeDataCache.cs +++ b/Scripts/NodeDataCache.cs @@ -16,9 +16,10 @@ namespace XNode { Dictionary staticPorts = new Dictionary(); System.Type nodeType = node.GetType(); - if (portDataCache.ContainsKey(nodeType)) { - for (int i = 0; i < portDataCache[nodeType].Count; i++) { - staticPorts.Add(portDataCache[nodeType][i].fieldName, portDataCache[nodeType][i]); + List typePortCache; + if (portDataCache.TryGetValue(nodeType, out typePortCache)) { + for (int i = 0; i < typePortCache.Count; i++) { + staticPorts.Add(typePortCache[i].fieldName, portDataCache[nodeType][i]); } } @@ -26,8 +27,8 @@ namespace XNode { // Loop through current node ports foreach (NodePort port in ports.Values.ToList()) { // If port still exists, check it it has been changed - if (staticPorts.ContainsKey(port.fieldName)) { - NodePort staticPort = staticPorts[port.fieldName]; + NodePort staticPort; + if (staticPorts.TryGetValue(port.fieldName, out staticPort)) { // If port exists but with wrong settings, remove it. Re-add it later. if (port.connectionType != staticPort.connectionType || port.IsDynamic || port.direction != staticPort.direction) ports.Remove(port.fieldName); else port.ValueType = staticPort.ValueType;