|
| 1 | +// Visual Pinball Engine |
| 2 | +// Copyright (C) 2022 freezy and VPE Team |
| 3 | +// |
| 4 | +// This program is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// This program is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU General Public License |
| 15 | +// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +using System; |
| 18 | +using System.Collections.Generic; |
| 19 | +using System.ComponentModel; |
| 20 | +using System.Linq; |
| 21 | +using System.Reflection; |
| 22 | +using Unity.VisualScripting; |
| 23 | +using UnityEditor; |
| 24 | +using UnityEngine; |
| 25 | + |
| 26 | +namespace VisualPinball.Unity.VisualScripting.Editor |
| 27 | +{ |
| 28 | + [Inspector(typeof(CompareType))] |
| 29 | + public class CompareTypeInspector : Inspector |
| 30 | + { |
| 31 | + private List<CompareType> CompareTypes = Enum.GetValues(typeof(CompareType)).Cast<CompareType>().ToList(); |
| 32 | + private string[] CompareTypeDescriptions = Enum.GetValues(typeof(CompareType)).Cast<CompareType>().Select(x => GetEnumDescription(x)).ToArray(); |
| 33 | + |
| 34 | + public CompareTypeInspector(Metadata metadata) : base(metadata) { |
| 35 | + } |
| 36 | + |
| 37 | + public override void Initialize() |
| 38 | + { |
| 39 | + metadata.instantiate = true; |
| 40 | + |
| 41 | + base.Initialize(); |
| 42 | + } |
| 43 | + |
| 44 | + protected override float GetHeight(float width, GUIContent label) |
| 45 | + { |
| 46 | + return HeightWithLabel(metadata, width, EditorGUIUtility.singleLineHeight, label); |
| 47 | + } |
| 48 | + |
| 49 | + protected override void OnGUI(Rect position, GUIContent label) |
| 50 | + { |
| 51 | + position = BeginLabeledBlock(metadata, position, label); |
| 52 | + |
| 53 | + var fieldPosition = new Rect |
| 54 | + ( |
| 55 | + position.x, |
| 56 | + position.y, |
| 57 | + position.width, |
| 58 | + EditorGUIUtility.singleLineHeight |
| 59 | + ); |
| 60 | + |
| 61 | + var index = CompareTypes.FindIndex(c => c == (CompareType)metadata.value); |
| 62 | + var newIndex = EditorGUI.Popup(fieldPosition, index, CompareTypeDescriptions); |
| 63 | + |
| 64 | + if (EndBlock(metadata)) |
| 65 | + { |
| 66 | + metadata.RecordUndo(); |
| 67 | + metadata.value = CompareTypes[newIndex]; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public override float GetAdaptiveWidth() |
| 72 | + { |
| 73 | + return Mathf.Max(18, EditorStyles.popup.CalcSize(new GUIContent(GetEnumDescription((CompareType)metadata.value))).x + Styles.popup.fixedWidth); |
| 74 | + } |
| 75 | + |
| 76 | + private static string GetEnumDescription(Enum value) |
| 77 | + { |
| 78 | + FieldInfo field = value.GetType().GetField(value.ToString()); |
| 79 | + |
| 80 | + DescriptionAttribute attribute |
| 81 | + = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) |
| 82 | + as DescriptionAttribute; |
| 83 | + |
| 84 | + return (attribute == null ? value.ToString() : attribute.Description).Replace('/', '\u2215'); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + public static class Styles |
| 89 | + { |
| 90 | + static Styles() |
| 91 | + { |
| 92 | + popup = new GUIStyle("TextFieldDropDown"); |
| 93 | + popup.fixedWidth = 10; |
| 94 | + popup.clipping = TextClipping.Clip; |
| 95 | + popup.normal.textColor = ColorPalette.transparent; |
| 96 | + popup.active.textColor = ColorPalette.transparent; |
| 97 | + popup.hover.textColor = ColorPalette.transparent; |
| 98 | + popup.focused.textColor = ColorPalette.transparent; |
| 99 | + popup.onNormal.textColor = ColorPalette.transparent; |
| 100 | + popup.onActive.textColor = ColorPalette.transparent; |
| 101 | + popup.onHover.textColor = ColorPalette.transparent; |
| 102 | + popup.onFocused.textColor = ColorPalette.transparent; |
| 103 | + } |
| 104 | + |
| 105 | + public static readonly GUIStyle textField; |
| 106 | + public static readonly GUIStyle popup; |
| 107 | + } |
| 108 | +} |
0 commit comments