From f049177e20a276049c61edbad631c1b2bbdd5706 Mon Sep 17 00:00:00 2001 From: chai Date: Thu, 15 Oct 2020 19:05:22 +0800 Subject: -advanced inspector +odin --- .../Scripts/Editor/VectorIntDrawers.cs | 141 +++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 Assets/Plugins/Sirenix/Odin Inspector/Scripts/Editor/VectorIntDrawers.cs (limited to 'Assets/Plugins/Sirenix/Odin Inspector/Scripts/Editor/VectorIntDrawers.cs') diff --git a/Assets/Plugins/Sirenix/Odin Inspector/Scripts/Editor/VectorIntDrawers.cs b/Assets/Plugins/Sirenix/Odin Inspector/Scripts/Editor/VectorIntDrawers.cs new file mode 100644 index 00000000..c7300fbc --- /dev/null +++ b/Assets/Plugins/Sirenix/Odin Inspector/Scripts/Editor/VectorIntDrawers.cs @@ -0,0 +1,141 @@ +//----------------------------------------------------------------------- +// +// Copyright (c) Sirenix IVS. All rights reserved. +// +//----------------------------------------------------------------------- + +#if UNITY_EDITOR && UNITY_2017_2_OR_NEWER + +namespace Sirenix.OdinInspector.Editor.Drawers +{ + using Utilities.Editor; + using UnityEditor; + using UnityEngine; + + /// + /// Vector2Int proprety drawer. + /// + public sealed class Vector2IntDrawer : OdinValueDrawer, IDefinesGenericMenuItems + { + /// + /// Draws the property. + /// + protected override void DrawPropertyLayout(GUIContent label) + { + Rect labelRect; + var contentRect = SirenixEditorGUI.BeginHorizontalPropertyLayout(label, out labelRect); + { + EditorGUI.BeginChangeCheck(); + var val = SirenixEditorFields.VectorPrefixSlideRect(labelRect, (Vector2)this.ValueEntry.SmartValue); + if (EditorGUI.EndChangeCheck()) + { + this.ValueEntry.SmartValue = new Vector2Int((int)val.x, (int)val.y); + } + + var showLabels = SirenixEditorFields.ResponsiveVectorComponentFields && contentRect.width >= 185; + GUIHelper.PushLabelWidth(SirenixEditorFields.SingleLetterStructLabelWidth); + this.ValueEntry.Property.Children[0].Draw(showLabels ? GUIHelper.TempContent("X") : null); + this.ValueEntry.Property.Children[1].Draw(showLabels ? GUIHelper.TempContent("Y") : null); + GUIHelper.PopLabelWidth(); + + } + SirenixEditorGUI.EndHorizontalPropertyLayout(); + } + + /// + /// Populates the generic menu for the property. + /// + public void PopulateGenericMenu(InspectorProperty property, GenericMenu genericMenu) + { + Vector2Int value = (Vector2Int)property.ValueEntry.WeakSmartValue; + + if (genericMenu.GetItemCount() > 0) + { + genericMenu.AddSeparator(""); + } + genericMenu.AddItem(new GUIContent("Zero", "Set the vector to (0, 0)"), value == Vector2Int.zero, () => SetVector(property, Vector2Int.zero)); + genericMenu.AddItem(new GUIContent("One", "Set the vector to (1, 1)"), value == Vector2Int.one, () => SetVector(property, Vector2Int.one)); + genericMenu.AddSeparator(""); + genericMenu.AddItem(new GUIContent("Right", "Set the vector to (1, 0)"), value == Vector2Int.right, () => SetVector(property, Vector2Int.right)); + genericMenu.AddItem(new GUIContent("Left", "Set the vector to (-1, 0)"), value == Vector2Int.left, () => SetVector(property, Vector2Int.left)); + genericMenu.AddItem(new GUIContent("Up", "Set the vector to (0, 1)"), value == Vector2Int.up, () => SetVector(property, Vector2Int.up)); + genericMenu.AddItem(new GUIContent("Down", "Set the vector to (0, -1)"), value == Vector2Int.down, () => SetVector(property, Vector2Int.down)); + } + + private void SetVector(InspectorProperty property, Vector2Int value) + { + property.Tree.DelayActionUntilRepaint(() => + { + for (int i = 0; i < property.ValueEntry.ValueCount; i++) + { + property.ValueEntry.WeakValues[i] = value; + } + }); + } + } + + /// + /// Vector3Int property drawer. + /// + public sealed class Vector3IntDrawer : OdinValueDrawer, IDefinesGenericMenuItems + { + /// + /// Draws the property. + /// + protected override void DrawPropertyLayout(GUIContent label) + { + Rect labelRect; + var contentRect = SirenixEditorGUI.BeginHorizontalPropertyLayout(label, out labelRect); + { + EditorGUI.BeginChangeCheck(); + var val = SirenixEditorFields.VectorPrefixSlideRect(labelRect, (Vector3)this.ValueEntry.SmartValue); + if (EditorGUI.EndChangeCheck()) + { + this.ValueEntry.SmartValue = new Vector3Int((int)val.x, (int)val.y, (int)val.z); + } + + var showLabels = SirenixEditorFields.ResponsiveVectorComponentFields && contentRect.width >= 185; + GUIHelper.PushLabelWidth(SirenixEditorFields.SingleLetterStructLabelWidth); + this.ValueEntry.Property.Children[0].Draw(showLabels ? GUIHelper.TempContent("X") : null); + this.ValueEntry.Property.Children[1].Draw(showLabels ? GUIHelper.TempContent("Y") : null); + this.ValueEntry.Property.Children[2].Draw(showLabels ? GUIHelper.TempContent("Z") : null); + GUIHelper.PopLabelWidth(); + + } + SirenixEditorGUI.EndHorizontalPropertyLayout(); + } + + /// + /// Populates the generic menu for the property. + /// + public void PopulateGenericMenu(InspectorProperty property, GenericMenu genericMenu) + { + Vector3Int value = (Vector3Int)property.ValueEntry.WeakSmartValue; + + if (genericMenu.GetItemCount() > 0) + { + genericMenu.AddSeparator(""); + } + + genericMenu.AddItem(new GUIContent("Zero", "Set the vector to (0, 0, 0)"), value == Vector3Int.zero, () => SetVector(property, Vector3Int.zero)); + genericMenu.AddItem(new GUIContent("One", "Set the vector to (1, 1, 1)"), value == Vector3Int.one, () => SetVector(property, Vector3Int.one)); + genericMenu.AddSeparator(""); + genericMenu.AddItem(new GUIContent("Right", "Set the vector to (1, 0, 0)"), value == Vector3Int.right, () => SetVector(property, Vector3Int.right)); + genericMenu.AddItem(new GUIContent("Left", "Set the vector to (-1, 0, 0)"), value == Vector3Int.left, () => SetVector(property, Vector3Int.left)); + genericMenu.AddItem(new GUIContent("Up", "Set the vector to (0, 1, 0)"), value == Vector3Int.up, () => SetVector(property, Vector3Int.up)); + genericMenu.AddItem(new GUIContent("Down", "Set the vector to (0, -1, 0)"), value == Vector3Int.down, () => SetVector(property, Vector3Int.down)); + genericMenu.AddItem(new GUIContent("Forward", "Set the vector property to (0, 0, 1)"), value == new Vector3Int(0, 0, 1), () => SetVector(property, new Vector3Int(0, 0, 1))); + genericMenu.AddItem(new GUIContent("Back", "Set the vector property to (0, 0, -1)"), value == new Vector3Int(0, 0, -1), () => SetVector(property, new Vector3Int(0, 0, -1))); + } + + private void SetVector(InspectorProperty property, Vector3Int value) + { + property.Tree.DelayActionUntilRepaint(() => + { + property.ValueEntry.WeakSmartValue = value; + }); + } + } +} + +#endif // UNITY_EDITOR && UNITY_2017_2_OR_NEWER \ No newline at end of file -- cgit v1.1-26-g67d0