mirror of
https://github.com/Siccity/xNode.git
synced 2025-12-20 01:06:01 +08:00
Added NodeEditorGUILayout static class. Improved int, float, double, long editor fields.
This commit is contained in:
parent
812196184f
commit
427dc8d53c
Binary file not shown.
@ -111,19 +111,11 @@ public class NodeEditor {
|
||||
|
||||
EditorGUI.BeginChangeCheck();
|
||||
if (fieldType == typeof(int)) {
|
||||
Rect rect = EditorGUILayout.GetControlRect();
|
||||
rect.width *= 0.5f;
|
||||
EditorGUI.LabelField(rect, fieldPrettyName);
|
||||
rect.x += rect.width;
|
||||
fieldValue = EditorGUI.IntField(rect, (int) fieldValue);
|
||||
fieldValue = NodeEditorGUILayout.IntField(fieldPrettyName, (int) fieldValue);
|
||||
} else if (fieldType == typeof(bool)) {
|
||||
fieldValue = EditorGUILayout.Toggle(fieldPrettyName, (bool) fieldValue);
|
||||
} else if (fieldType.IsEnum) {
|
||||
Rect rect = EditorGUILayout.GetControlRect();
|
||||
rect.width *= 0.5f;
|
||||
EditorGUI.LabelField(rect, fieldPrettyName);
|
||||
rect.x += rect.width;
|
||||
fieldValue = EditorGUI.EnumPopup(rect, (Enum) fieldValue);
|
||||
fieldValue = NodeEditorGUILayout.EnumField(fieldPrettyName, (Enum)fieldValue);
|
||||
} else if (fieldType == typeof(string)) {
|
||||
|
||||
if (fieldName == "name") return; //Ignore 'name'
|
||||
@ -136,11 +128,11 @@ public class NodeEditor {
|
||||
if (fieldName == "rect") return; //Ignore 'rect'
|
||||
fieldValue = EditorGUILayout.RectField(fieldPrettyName, (Rect) fieldValue);
|
||||
} else if (fieldType == typeof(float)) {
|
||||
Rect rect = EditorGUILayout.GetControlRect();
|
||||
rect.width *= 0.5f;
|
||||
EditorGUI.LabelField(rect, fieldPrettyName);
|
||||
rect.x += rect.width;
|
||||
fieldValue = EditorGUI.FloatField(rect, (float) fieldValue);
|
||||
fieldValue = NodeEditorGUILayout.FloatField(fieldPrettyName, (float) fieldValue);
|
||||
} else if (fieldType == typeof(double)) {
|
||||
fieldValue = NodeEditorGUILayout.DoubleField(fieldPrettyName, (double) fieldValue);
|
||||
} else if (fieldType == typeof(long)) {
|
||||
fieldValue = NodeEditorGUILayout.LongField(fieldPrettyName, (long) fieldValue);
|
||||
} else if (fieldType == typeof(Vector2)) {
|
||||
fieldValue = EditorGUILayout.Vector2Field(fieldPrettyName, (Vector2) fieldValue);
|
||||
} else if (fieldType == typeof(Vector3)) {
|
||||
|
||||
128
Scripts/Editor/NodeEditorGUILayout.cs
Normal file
128
Scripts/Editor/NodeEditorGUILayout.cs
Normal file
@ -0,0 +1,128 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary> Provides unec-specific field editors </summary>
|
||||
public class NodeEditorGUILayout {
|
||||
|
||||
private static double tempValue;
|
||||
|
||||
public static int IntField(string label, int value) {
|
||||
GUIUtility.GetControlID(FocusType.Passive);
|
||||
Rect rect = EditorGUILayout.GetControlRect();
|
||||
rect.width *= 0.5f;
|
||||
if (NodeEditorWindow.current != null) {
|
||||
double v = (double) value;
|
||||
DragNumber(rect, ref v);
|
||||
value = (int) v;
|
||||
if (GUI.changed) NodeEditorWindow.current.Repaint();
|
||||
}
|
||||
EditorGUI.LabelField(rect, label);
|
||||
rect.x += rect.width;
|
||||
if (!GUI.changed) value = EditorGUI.IntField(rect, value);
|
||||
else {
|
||||
EditorGUI.IntField(rect, value);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
public static float FloatField(string label, float value) {
|
||||
GUIUtility.GetControlID(FocusType.Passive);
|
||||
Rect rect = EditorGUILayout.GetControlRect();
|
||||
rect.width *= 0.5f;
|
||||
if (NodeEditorWindow.current != null) {
|
||||
double v = (double) value;
|
||||
DragNumber(rect, ref v);
|
||||
value = (float) v;
|
||||
if (GUI.changed) NodeEditorWindow.current.Repaint();
|
||||
}
|
||||
EditorGUI.LabelField(rect, label);
|
||||
rect.x += rect.width;
|
||||
if (!GUI.changed) value = EditorGUI.FloatField(rect, value);
|
||||
else {
|
||||
EditorGUI.FloatField(rect, value);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
public static double DoubleField(string label, double value) {
|
||||
GUIUtility.GetControlID(FocusType.Passive);
|
||||
Rect rect = EditorGUILayout.GetControlRect();
|
||||
rect.width *= 0.5f;
|
||||
if (NodeEditorWindow.current != null) {
|
||||
double v = (double) value;
|
||||
DragNumber(rect, ref v);
|
||||
value = (double) v;
|
||||
if (GUI.changed) NodeEditorWindow.current.Repaint();
|
||||
}
|
||||
EditorGUI.LabelField(rect, label);
|
||||
rect.x += rect.width;
|
||||
if (!GUI.changed) value = EditorGUI.DoubleField(rect, value);
|
||||
else {
|
||||
EditorGUI.DoubleField(rect, value);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
public static long LongField(string label, long value) {
|
||||
GUIUtility.GetControlID(FocusType.Passive);
|
||||
Rect rect = EditorGUILayout.GetControlRect();
|
||||
rect.width *= 0.5f;
|
||||
if (NodeEditorWindow.current != null) {
|
||||
double v = (double) value;
|
||||
DragNumber(rect, ref v);
|
||||
value = (long) v;
|
||||
if (GUI.changed) NodeEditorWindow.current.Repaint();
|
||||
}
|
||||
EditorGUI.LabelField(rect, label);
|
||||
rect.x += rect.width;
|
||||
if (!GUI.changed) value = EditorGUI.LongField(rect, value);
|
||||
else {
|
||||
EditorGUI.LongField(rect, value);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
public static void DragNumber(Rect rect, ref double value) {
|
||||
double sensitivity = Math.Max(0.09432981473891d, Math.Pow(Math.Abs(value), 0.5d) * 0.03d);
|
||||
|
||||
int id = GUIUtility.GetControlID(FocusType.Passive);
|
||||
Event e = Event.current;
|
||||
switch (e.type) {
|
||||
case EventType.MouseDown:
|
||||
if (rect.Contains(e.mousePosition) && e.button == 0) {
|
||||
tempValue = value;
|
||||
GUIUtility.hotControl = id;
|
||||
e.Use();
|
||||
GUIUtility.keyboardControl = id;
|
||||
EditorGUIUtility.SetWantsMouseJumping(1);
|
||||
}
|
||||
break;
|
||||
case EventType.MouseUp:
|
||||
tempValue = 0;
|
||||
if (GUIUtility.hotControl == id) {
|
||||
GUIUtility.hotControl = 0;
|
||||
e.Use();
|
||||
EditorGUIUtility.SetWantsMouseJumping(0);
|
||||
}
|
||||
break;
|
||||
case EventType.MouseDrag:
|
||||
if (GUIUtility.hotControl == id) {
|
||||
tempValue += HandleUtility.niceMouseDelta * sensitivity;
|
||||
value = tempValue;
|
||||
GUI.changed = true;
|
||||
}
|
||||
break;
|
||||
case EventType.Repaint:
|
||||
if (NodeEditorWindow.current != null && Mathf.Approximately(NodeEditorWindow.current.zoom, 1)) {
|
||||
EditorGUIUtility.AddCursorRect(rect, MouseCursor.SlideArrow);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
public static Enum EnumField(string label, Enum value) {
|
||||
Rect rect = EditorGUILayout.GetControlRect();
|
||||
rect.width *= 0.5f;
|
||||
EditorGUI.LabelField(rect, label);
|
||||
rect.x += rect.width;
|
||||
return EditorGUI.EnumPopup(rect, value);
|
||||
}
|
||||
}
|
||||
12
Scripts/Editor/NodeEditorGUILayout.cs.meta
Normal file
12
Scripts/Editor/NodeEditorGUILayout.cs.meta
Normal file
@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1d6c2d118d1c77948a23f2f4a34d1f64
|
||||
timeCreated: 1507966608
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -8,6 +8,8 @@ using UnityEngine;
|
||||
|
||||
[InitializeOnLoad]
|
||||
public partial class NodeEditorWindow : EditorWindow {
|
||||
public static NodeEditorWindow current;
|
||||
|
||||
/// <summary> Stores node positions for all nodePorts. </summary>
|
||||
public Dictionary<NodePort, Rect> portConnectionPoints { get { return _portConnectionPoints; } }
|
||||
private Dictionary<NodePort, Rect> _portConnectionPoints = new Dictionary<NodePort, Rect>();
|
||||
@ -19,6 +21,7 @@ public partial class NodeEditorWindow : EditorWindow {
|
||||
|
||||
void OnFocus() {
|
||||
AssetDatabase.SaveAssets();
|
||||
current = this;
|
||||
}
|
||||
|
||||
partial void OnEnable();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user