1
0
mirror of https://github.com/Siccity/xNode.git synced 2025-12-20 09:16:01 +08:00

Switched Dict.ContainsKey out with Dict.TryGetValue for performance gain.

This commit is contained in:
Thor Brigsted 2018-09-09 18:25:41 +02:00
parent c424dfa4d7
commit ff97cc4494
7 changed files with 46 additions and 33 deletions

View File

@ -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<NodeEditor, NodeEditor.CustomNodeEditorAttribute, XNode.Node>.INodeEditorAttrib {
XNodeEditor.Internal.NodeEditorBase<NodeEditor, NodeEditor.CustomNodeEditorAttribute, XNode.Node>.INodeEditorAttrib {
private Type inspectedType;
/// <summary> Tells a NodeEditor which Node type it is an editor for </summary>
/// <param name="inspectedType">Type that this editor can edit</param>
@ -98,5 +100,4 @@ namespace XNodeEditor {
}
}
}
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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<Vector2> 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

View File

@ -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);

View File

@ -153,7 +153,8 @@ namespace XNode {
/// <summary> Returns port which matches fieldName </summary>
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;
}

View File

@ -16,9 +16,10 @@ namespace XNode {
Dictionary<string, NodePort> staticPorts = new Dictionary<string, NodePort>();
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<NodePort> 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;