Skip to content

Commit 5f4cbc2

Browse files
authored
feat: Finished the drawer menu for the keycode
Basically new editor scripts, fixed a really annoying issue where the keycode enum wouldnt fit the screen, now i handle it with a custom view
2 parents 452397e + 2611114 commit 5f4cbc2

9 files changed

Lines changed: 309 additions & 20 deletions

Editor/DrawerMenu.cs

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using System;
4+
using UnityEditor;
5+
using UnityEngine;
6+
using UnityEngine.UI;
7+
using BigasTools.UI;
8+
9+
namespace BigasTools.Editor{
10+
public class DrawerMenu : EditorWindow
11+
{
12+
Vector2 scrollPos;
13+
static DrawerOption[] options;
14+
public static void ShowWindow(DrawerOption[] _keys) {
15+
var window = GetWindow<DrawerMenu>();
16+
window.titleContent = new GUIContent("Drawer menu");
17+
window.Show();
18+
window.minSize = new Vector2(400,180);
19+
options = _keys;
20+
}
21+
22+
void OnGUI()
23+
{
24+
GUILayout.BeginVertical(EditorStyles.toolbar);
25+
scrollPos = EditorGUILayout.BeginScrollView(scrollPos, GUILayout.Width(400), GUILayout.Height(500));
26+
for (int i = 0; i < options.Length; i++)
27+
{
28+
options[i].Refresh(i);
29+
if(GUILayout.Button(options[i].name, EditorStyles.toolbarButton)){
30+
options[i].onGUI();
31+
options[i].Update();
32+
};
33+
GUILayout.FlexibleSpace();
34+
}
35+
GUILayout.EndVertical();
36+
GUILayout.EndScrollView();
37+
}
38+
}
39+
[System.Serializable]
40+
public abstract class DrawerOption{
41+
public string name {set;get;}
42+
43+
public abstract void onGUI();
44+
public abstract void Refresh(int i);
45+
public abstract void Update();
46+
}
47+
[System.Serializable]
48+
public class DrawerOption<T> : DrawerOption{
49+
private readonly Func<T[]> getValues;
50+
private readonly Action<T> setValue;
51+
private readonly SerializedProperty serializedProperty;
52+
private T[] values;
53+
private T value;
54+
private int selected;
55+
KeyCode _selectedKey;
56+
57+
public DrawerOption(Func<T[]> getValues, string name, Action<T> setValue, SerializedProperty serializedProperty, KeyCode _selectedKey)
58+
{
59+
this.name = name;
60+
this.getValues = getValues;
61+
this.setValue = setValue;
62+
this.serializedProperty = serializedProperty;
63+
this._selectedKey = _selectedKey;
64+
}
65+
66+
public override void onGUI()
67+
{
68+
if(setValue != null){
69+
onValueGUI(value);
70+
//setValue(value);
71+
}
72+
}
73+
void GetEnumIndex(){
74+
string _selectedKeyName = Enum.GetName(typeof(KeyCode), _selectedKey);
75+
var keyCodes = Enum.GetValues(typeof(KeyCode));
76+
77+
int index = 0;
78+
79+
// Find the index of the selected key name.
80+
foreach (string enumName in serializedProperty.enumNames) {
81+
82+
if (enumName == _selectedKeyName) {
83+
break;
84+
}
85+
86+
index++;
87+
}
88+
89+
serializedProperty.enumValueIndex = index;
90+
serializedProperty.serializedObject.ApplyModifiedProperties();
91+
}
92+
public override void Update()
93+
{
94+
GetEnumIndex();
95+
}
96+
void onValueGUI(T val){
97+
setValue(val);
98+
}
99+
public override void Refresh(int i){
100+
values = getValues();
101+
value = values[i];
102+
}
103+
}
104+
}

Editor/DrawerMenu.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/InputProfileDrawer.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
using UnityEditor;
6+
7+
using BigasTools.InputSystem;
8+
9+
[CustomPropertyDrawer(typeof(InputProfile))]
10+
public class InputProfileDrawer : PropertyDrawer
11+
{
12+
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
13+
{
14+
EditorGUI.BeginProperty(position, label, property);
15+
16+
var title = new Rect(position.x, position.y, position.width - 50, position.height);
17+
var name = new Rect(position.x, position.y, position.width - 350, position.height);
18+
var key = new Rect(position.x - 25, position.y, position.width, position.height);
19+
var joyKey = new Rect(position.x + 180, position.y, position.width - 350, position.height);
20+
21+
//EditorGUI.PrefixLabel(title, GUIUtility.GetControlID(FocusType.Passive), label);
22+
23+
var indent = EditorGUI.indentLevel;
24+
EditorGUI.indentLevel = 0;
25+
26+
EditorGUI.PropertyField(name, property.FindPropertyRelative("inputName"), GUIContent.none);
27+
EditorGUI.PropertyField(key, property.FindPropertyRelative("inputKey"), GUIContent.none);
28+
EditorGUI.PropertyField(joyKey, property.FindPropertyRelative("joystickKey"), GUIContent.none);
29+
30+
31+
EditorGUI.indentLevel = indent;
32+
33+
EditorGUI.EndProperty();
34+
}
35+
}

Editor/InputProfileDrawer.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/KeyCodeDrawer.cs

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
using UnityEngine;
5+
using UnityEditor;
6+
7+
using BigasTools.Editor;
8+
[CustomPropertyDrawer(typeof(KeyCode))]
9+
[CanEditMultipleObjects]
10+
public class KeyCodeDrawer : PropertyDrawer
11+
{
12+
SerializedProperty p;
13+
14+
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
15+
{
16+
p = property;
17+
EditorGUI.BeginProperty(position, label, property);
18+
19+
// Display the property name.
20+
//EditorGUI.LabelField(position, ObjectNames.NicifyVariableName(property.name));
21+
22+
// Unity SerializedProperty has no direct way to set an enum, it uses an index value
23+
// which is unrelated to the KeyCode enumeration. The index value is the order of the enum
24+
// in the default pop-up list. KeyCode enumeration might follow something like ASCII (unsure if 100% true).
25+
//
26+
// To bypass this, we can get the names of the KeyCode enums as a string array which are in the same order
27+
// as the popup list order, this order is the SerializedProperty.enumValueIndex.
28+
29+
// To get the true KeyCode from a serialized property we do:
30+
31+
// Get access to the KeyCode enums as a string array of enum names.
32+
string[] enums = property.enumNames;
33+
34+
// Get the current KeyCode name held by the serialized property by accessing the array value at enumValueIndex.
35+
string propEnumName = enums[property.enumValueIndex];
36+
37+
// Get the KeyCode enum by converting the name to KeyCode.
38+
var propKey = (KeyCode)Enum.Parse(typeof(KeyCode), propEnumName);
39+
40+
string keyName = Enum.GetName(typeof(KeyCode), propKey);
41+
42+
// Offset the button from the label.
43+
position.x += 120f;
44+
position.width = 80f;
45+
46+
// Display the button that activates the selection window.
47+
if (EditorGUI.DropdownButton(position, new GUIContent(keyName), FocusType.Keyboard)) {
48+
selectKey();
49+
}
50+
51+
// Apply changes if necessary, this way, the default _selectedKey value
52+
// does not incorrectly override the property enum value.
53+
/*if (_bChangeKey) {
54+
55+
// Since we cannot set the enum value of the serialized property directly,
56+
// we need to get the enumValueIndex associated to the KeyCode name.
57+
//
58+
// To do this, we convert the selected key code to its string name and
59+
// then find its index position in SerializedProperty.enumNames (variable string[] enums)
60+
string _selectedKeyName = Enum.GetName(typeof(KeyCode), _selectedKey);
61+
62+
int index = 0;
63+
64+
// Find the index of the selected key name.
65+
foreach (string enumName in enums) {
66+
67+
if (enumName == _selectedKeyName) {
68+
break;
69+
}
70+
71+
index++;
72+
}
73+
74+
// Set the index which in turn sets the correct key code enum.
75+
property.enumValueIndex = index;
76+
77+
_bChangeKey = false;
78+
return;
79+
}*/
80+
81+
EditorGUI.EndProperty();
82+
}
83+
public void Test(){
84+
}
85+
86+
// Display a menu to select key codes.
87+
private void selectKey()
88+
{
89+
// Get all the keycodes
90+
var keyCodes = Enum.GetValues(typeof(KeyCode));
91+
var keys = new KeyCode[keyCodes.Length];
92+
var drawer = new DrawerOption[keyCodes.Length];
93+
94+
95+
96+
// Set all the keycode values in the array in order to feed it into the selection window.
97+
int i = 0;
98+
foreach (KeyCode k in keyCodes) {
99+
drawer[i] = new DrawerOption<KeyCode>(()=>keys, k.ToString(), key => {
100+
//_selectedKey = key;
101+
p.enumValueIndex = 4;}, p, k);
102+
keys[i++] = k;
103+
}
104+
105+
DrawerMenu.ShowWindow(drawer);
106+
107+
// Display the selection window to pick a keycode.
108+
/*SelectionWindow.Show(new Tab<KeyCode>(
109+
110+
getValues: () => keys,
111+
getCurrent: () => _selectedKey,
112+
setTarget: key => { _selectedKey = key; },
113+
114+
getValueName: key => Enum.GetName(typeof(KeyCode), key),
115+
title: "Keys"
116+
));*/
117+
}
118+
}

Editor/KeyCodeDrawer.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/ViewCreatorEditor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class CreateViewEditor : EditorWindow
1313
Object test;
1414
List<CreateViewObject> components = new List<CreateViewObject>();
1515
[MenuItem("Bigas-Tools/Create View &v")]
16-
private static void ShowWindow() {
16+
public static void ShowWindow() {
1717
var window = GetWindow<CreateViewEditor>();
1818
window.titleContent = new GUIContent("View creator");
1919
window.Show();

Runtime/Input/BGameInput.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -131,16 +131,12 @@ bool GetJoysticKey(InputKeys key){
131131
}
132132
[System.Serializable]
133133
public class InputData{
134-
public List<InputProfile> profile;
134+
public InputProfile[] profile;
135135
public InputData(){
136-
var first = new InputProfile("Interaction", KeyCode.E, InputKeys.A);
137-
var second = new InputProfile("Pause", KeyCode.Escape, InputKeys.PAUSE);
138-
var third = new InputProfile("Info", KeyCode.CapsLock, InputKeys.SELECT);
139-
140-
profile = new List<InputProfile>();
141-
profile.Add(first);
142-
profile.Add(second);
143-
profile.Add(third);
136+
profile = new InputProfile[3];
137+
profile[0] = new InputProfile("Interaction", KeyCode.E, InputKeys.A);
138+
profile[1] = new InputProfile("Pause", KeyCode.Escape, InputKeys.PAUSE);
139+
profile[2] = new InputProfile("Info", KeyCode.CapsLock, InputKeys.SELECT);
144140
}
145141
}
146142
[System.Serializable]

Samples/EntityExamples.unity

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -638,31 +638,34 @@ MonoBehaviour:
638638
inputData:
639639
profile:
640640
- inputName: Interaction
641-
inputKey: 101
641+
inputKey: 105
642642
joystickKey: 0
643643
- inputName: Pause
644-
inputKey: 27
644+
inputKey: 279
645645
joystickKey: 4
646646
- inputName: Info
647-
inputKey: 301
647+
inputKey: 27
648648
joystickKey: 5
649649
- inputName: Example
650-
inputKey: 48
650+
inputKey: 39
651651
joystickKey: 6
652652
- inputName: Example1
653-
inputKey: 49
653+
inputKey: 39
654654
joystickKey: 10
655-
- inputName: Example2
656-
inputKey: 50
655+
- inputName: dada
656+
inputKey: 39
657657
joystickKey: 11
658658
- inputName: Example3
659-
inputKey: 51
659+
inputKey: 27
660660
joystickKey: 7
661661
- inputName: Example4
662-
inputKey: 52
662+
inputKey: 27
663663
joystickKey: 8
664664
- inputName: Example5
665-
inputKey: 53
665+
inputKey: 27
666+
joystickKey: 9
667+
- inputName: Example5
668+
inputKey: 13
666669
joystickKey: 9
667670
--- !u!114 &2027691194
668671
MonoBehaviour:

0 commit comments

Comments
 (0)