mirror of
https://github.com/Siccity/xNode.git
synced 2025-12-21 01:36:03 +08:00
Removed RenamePopup in favour of an in-place text field
This commit is contained in:
parent
0eb52dd626
commit
4323beb731
@ -78,6 +78,11 @@ namespace XNodeEditor
|
|||||||
var e = Event.current;
|
var e = Event.current;
|
||||||
var node = target as XNode.GroupNode;
|
var node = target as XNode.GroupNode;
|
||||||
|
|
||||||
|
for (int i = node.children.Count - 1; i >= 0; i--)
|
||||||
|
{
|
||||||
|
if (node.children[i] == null) node.children.RemoveAt(i);
|
||||||
|
}
|
||||||
|
|
||||||
var nodes = node.graph.nodes;
|
var nodes = node.graph.nodes;
|
||||||
if (e.type == EventType.Repaint)
|
if (e.type == EventType.Repaint)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -31,11 +31,9 @@ namespace XNodeEditor {
|
|||||||
{
|
{
|
||||||
var content = new GUIContent(target.name);
|
var content = new GUIContent(target.name);
|
||||||
var type = target.GetType();
|
var type = target.GetType();
|
||||||
if (type.TryGetAttributeHeader(out GUIContent attrContent))
|
if (type.TryGetAttributeHeader(out GUIContent attrContent) && content.text == NodeEditorUtilities.NodeDefaultName(type))
|
||||||
{
|
{
|
||||||
if (content.text != NodeEditorUtilities.NodeDefaultName(type))
|
content = new GUIContent(attrContent);
|
||||||
attrContent.text = content.text;
|
|
||||||
content = attrContent;
|
|
||||||
}
|
}
|
||||||
GUILayout.Label(content, NodeEditorResources.styles.nodeHeader, GUILayout.Height(30));
|
GUILayout.Label(content, NodeEditorResources.styles.nodeHeader, GUILayout.Height(30));
|
||||||
}
|
}
|
||||||
|
|||||||
@ -415,16 +415,61 @@ namespace XNodeEditor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Node currentRenamingNode
|
||||||
|
{
|
||||||
|
get => _currentRename;
|
||||||
|
set
|
||||||
|
{
|
||||||
|
if (_currentRename == value) return;
|
||||||
|
_currentRename = value;
|
||||||
|
if (value != null)
|
||||||
|
{
|
||||||
|
_currentRenameString = value.name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private Node _currentRename;
|
||||||
|
private const string renameControlName = "renameNodeInput";
|
||||||
|
private string _currentRenameString;
|
||||||
|
|
||||||
|
private void RenameGUI()
|
||||||
|
{
|
||||||
|
var node = currentRenamingNode;
|
||||||
|
if (node == null) return;
|
||||||
|
GUI.SetNextControlName(renameControlName);
|
||||||
|
var rect = GridToWindowRectNoClipped(
|
||||||
|
new Rect(node.position + new Vector2(16,9),
|
||||||
|
(nodeSizes.TryGetValue(node, out var size) ? size : new Vector2(180, EditorGUIUtility.singleLineHeight))
|
||||||
|
- new Vector2(32, 18)
|
||||||
|
) {height = EditorGUIUtility.singleLineHeight});
|
||||||
|
_currentRenameString = GUI.TextField(rect, _currentRenameString);
|
||||||
|
EditorGUI.FocusTextInControl(renameControlName);
|
||||||
|
var e = Event.current;
|
||||||
|
if ((e.isKey && (e.keyCode == KeyCode.Return || e.keyCode == KeyCode.Escape)) ||
|
||||||
|
(e.isMouse && e.type == EventType.MouseDown && !rect.Contains(e.mousePosition)))
|
||||||
|
{
|
||||||
|
var newName = (e.keyCode == KeyCode.Escape || _currentRenameString == null || _currentRenameString.Trim() == "") ?
|
||||||
|
NodeEditorUtilities.NodeDefaultName(node.GetType()) :
|
||||||
|
_currentRenameString;
|
||||||
|
|
||||||
|
node.name = newName;
|
||||||
|
NodeEditor.GetEditor(node, this).OnRename();
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(AssetDatabase.GetAssetPath(node))) {
|
||||||
|
AssetDatabase.SetMainObject((node).graph, AssetDatabase.GetAssetPath(node));
|
||||||
|
AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(node));
|
||||||
|
}
|
||||||
|
currentRenamingNode = null;
|
||||||
|
EditorGUIUtility.editingTextField = false;
|
||||||
|
node.TriggerOnValidate();
|
||||||
|
}
|
||||||
|
}
|
||||||
/// <summary> Initiate a rename on the currently selected node </summary>
|
/// <summary> Initiate a rename on the currently selected node </summary>
|
||||||
public void RenameSelectedNode() {
|
public void RenameSelectedNode() {
|
||||||
if (Selection.objects.Length == 1 && Selection.activeObject is XNode.Node) {
|
if (Selection.objects.Length == 1 && Selection.activeObject is XNode.Node)
|
||||||
|
{
|
||||||
XNode.Node node = Selection.activeObject as XNode.Node;
|
XNode.Node node = Selection.activeObject as XNode.Node;
|
||||||
Vector2 size;
|
currentRenamingNode = node;
|
||||||
if (nodeSizes.TryGetValue(node, out size)) {
|
|
||||||
RenamePopup.Show(Selection.activeObject, size.x);
|
|
||||||
} else {
|
|
||||||
RenamePopup.Show(Selection.activeObject);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -34,6 +34,7 @@ namespace XNodeEditor {
|
|||||||
DrawNodes();
|
DrawNodes();
|
||||||
DrawSelectionBox();
|
DrawSelectionBox();
|
||||||
DrawTooltip();
|
DrawTooltip();
|
||||||
|
RenameGUI();
|
||||||
graphEditor.OnGUI();
|
graphEditor.OnGUI();
|
||||||
|
|
||||||
// Run and reset onLateGUI
|
// Run and reset onLateGUI
|
||||||
|
|||||||
@ -1,87 +0,0 @@
|
|||||||
using UnityEditor;
|
|
||||||
using UnityEngine;
|
|
||||||
|
|
||||||
namespace XNodeEditor {
|
|
||||||
/// <summary> Utility for renaming assets </summary>
|
|
||||||
public class RenamePopup : EditorWindow {
|
|
||||||
private const string inputControlName = "nameInput";
|
|
||||||
|
|
||||||
public static RenamePopup current { get; private set; }
|
|
||||||
public Object target;
|
|
||||||
public string input;
|
|
||||||
|
|
||||||
private bool firstFrame = true;
|
|
||||||
|
|
||||||
/// <summary> Show a rename popup for an asset at mouse position. Will trigger reimport of the asset on apply.
|
|
||||||
public static RenamePopup Show(Object target, float width = 200) {
|
|
||||||
RenamePopup window = EditorWindow.GetWindow<RenamePopup>(true, "Rename " + target.name, true);
|
|
||||||
if (current != null) current.Close();
|
|
||||||
current = window;
|
|
||||||
window.target = target;
|
|
||||||
window.input = target.name;
|
|
||||||
window.minSize = new Vector2(100, 44);
|
|
||||||
window.position = new Rect(0, 0, width, 44);
|
|
||||||
window.UpdatePositionToMouse();
|
|
||||||
return window;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void UpdatePositionToMouse() {
|
|
||||||
if (Event.current == null) return;
|
|
||||||
Vector3 mousePoint = GUIUtility.GUIToScreenPoint(Event.current.mousePosition);
|
|
||||||
Rect pos = position;
|
|
||||||
pos.x = mousePoint.x - position.width * 0.5f;
|
|
||||||
pos.y = mousePoint.y - 10;
|
|
||||||
position = pos;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void OnLostFocus() {
|
|
||||||
// Make the popup close on lose focus
|
|
||||||
Close();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void OnGUI() {
|
|
||||||
if (firstFrame) {
|
|
||||||
UpdatePositionToMouse();
|
|
||||||
firstFrame = false;
|
|
||||||
}
|
|
||||||
GUI.SetNextControlName(inputControlName);
|
|
||||||
input = EditorGUILayout.TextField(input);
|
|
||||||
EditorGUI.FocusTextInControl(inputControlName);
|
|
||||||
Event e = Event.current;
|
|
||||||
// If input is empty, revert name to default instead
|
|
||||||
if (input == null || input.Trim() == "") {
|
|
||||||
if (GUILayout.Button("Revert to default") || (e.isKey && e.keyCode == KeyCode.Return)) {
|
|
||||||
target.name = NodeEditorUtilities.NodeDefaultName(target.GetType());
|
|
||||||
NodeEditor.GetEditor((XNode.Node)target, NodeEditorWindow.current).OnRename();
|
|
||||||
if (!string.IsNullOrEmpty(AssetDatabase.GetAssetPath(target))) {
|
|
||||||
AssetDatabase.SetMainObject((target as XNode.Node).graph, AssetDatabase.GetAssetPath(target));
|
|
||||||
AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(target));
|
|
||||||
}
|
|
||||||
Close();
|
|
||||||
target.TriggerOnValidate();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Rename asset to input text
|
|
||||||
else {
|
|
||||||
if (GUILayout.Button("Apply") || (e.isKey && e.keyCode == KeyCode.Return)) {
|
|
||||||
target.name = input;
|
|
||||||
NodeEditor.GetEditor((XNode.Node)target, NodeEditorWindow.current).OnRename();
|
|
||||||
if (!string.IsNullOrEmpty(AssetDatabase.GetAssetPath(target))) {
|
|
||||||
AssetDatabase.SetMainObject((target as XNode.Node).graph, AssetDatabase.GetAssetPath(target));
|
|
||||||
AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(target));
|
|
||||||
}
|
|
||||||
Close();
|
|
||||||
target.TriggerOnValidate();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (e.isKey && e.keyCode == KeyCode.Escape) {
|
|
||||||
Close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void OnDestroy() {
|
|
||||||
EditorGUIUtility.editingTextField = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,13 +0,0 @@
|
|||||||
fileFormatVersion: 2
|
|
||||||
guid: 4ef3ddc25518318469bce838980c64be
|
|
||||||
timeCreated: 1552067957
|
|
||||||
licenseType: Free
|
|
||||||
MonoImporter:
|
|
||||||
externalObjects: {}
|
|
||||||
serializedVersion: 2
|
|
||||||
defaultReferences: []
|
|
||||||
executionOrder: 0
|
|
||||||
icon: {instanceID: 0}
|
|
||||||
userData:
|
|
||||||
assetBundleName:
|
|
||||||
assetBundleVariant:
|
|
||||||
Loading…
x
Reference in New Issue
Block a user