Skip to content

Commit 075f528

Browse files
author
lawwong
committed
Add EnumArray.EnumNameWithAlias
1 parent 1e6431f commit 075f528

File tree

3 files changed

+47
-18
lines changed

3 files changed

+47
-18
lines changed

Assets/HTC.UnityPlugin/Utility/Container/EnumArray.cs

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ public abstract class EnumArrayBase
168168
public abstract int Length { get; }
169169
public abstract int Capacity { get; }
170170
public abstract string EnumName(int enumInt);
171+
public abstract string EnumNameWithAlias(int enumInt);
171172
public abstract bool IsValidIndex(int enumInt);
172173
public abstract void Clear();
173174
public abstract void FillCapacityToLength();
@@ -320,6 +321,7 @@ public bool MoveNext()
320321

321322
private static readonly TEnum[] enums;
322323
private static readonly string[] enumNames;
324+
private static readonly string[] enumNameWithAliases;
323325
private static Func<TEnum, int> funcE2I;
324326
public static readonly int StaticMinInt;
325327
public static readonly TEnum StaticMin;
@@ -434,15 +436,40 @@ static EnumArrayBase()
434436
// create an int array with invalid enum values
435437
enums = new TEnum[StaticLength];
436438
enumNames = new string[StaticLength];
439+
enumNameWithAliases = new string[StaticLength];
437440
for (int fi = 0, imax = fields.Length; fi < imax; ++fi)
438441
{
439442
if (fieldNames[fi] == null) { continue; }
440443

441444
var i = fieldValues[fi] - StaticMinInt;
442-
if (enumNames[i] != null) { continue; }
445+
if (enumNames[i] == null)
446+
{
447+
enums[i] = fieldEnums[fi];
448+
enumNames[i] = fieldNames[fi];
449+
}
450+
else if (enumNameWithAliases[i] == null)
451+
{
452+
enumNameWithAliases[i] = fieldNames[fi];
453+
}
454+
else
455+
{
456+
enumNameWithAliases[i] += ", " + fieldNames[fi];
457+
}
458+
}
443459

444-
enums[i] = fieldEnums[fi];
445-
enumNames[i] = fieldNames[fi];
460+
for (int i = 0, imax = StaticLength; i < imax; ++i)
461+
{
462+
if (enumNames[i] != null)
463+
{
464+
if (enumNameWithAliases[i] != null)
465+
{
466+
enumNameWithAliases[i] = enumNames[i] + " (" + enumNameWithAliases[i] + ")";
467+
}
468+
else
469+
{
470+
enumNameWithAliases[i] = enumNames[i];
471+
}
472+
}
446473
}
447474
}
448475

@@ -453,6 +480,11 @@ public override string EnumName(int enumInt)
453480
return StaticEnumName(enumInt);
454481
}
455482

483+
public override string EnumNameWithAlias(int enumInt)
484+
{
485+
return StaticEnumNameWithAlias(enumInt);
486+
}
487+
456488
public static string StaticEnumName(TEnum e)
457489
{
458490
return StaticEnumName(E2I(e));
@@ -463,6 +495,11 @@ public static string StaticEnumName(int enumInt)
463495
return enumNames[enumInt - StaticMinInt];
464496
}
465497

498+
public static string StaticEnumNameWithAlias(int enumInt)
499+
{
500+
return enumNameWithAliases[enumInt - StaticMinInt];
501+
}
502+
466503
public override bool IsValidIndex(int enumInt)
467504
{
468505
return StaticIsValidIndex(enumInt);

Assets/HTC.UnityPlugin/ViveInputUtility/Scripts/ControllerTooltip/Editor/TooltipRenderDataAssetBaseEditor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ private void ShowAddTriggerMenu()
107107
{
108108
var button = ev.Key;
109109
var visible = ev.Value;
110-
var name = visibleInList.EnumName((int)button);
110+
var name = visibleInList.EnumNameWithAlias((int)button);
111111
if (visible)
112112
{
113113
menu.AddDisabledItem(new GUIContent(name));

Assets/HTC.UnityPlugin/ViveInputUtility/Scripts/ControllerTooltip/Editor/TooltipRigAssetEditor.cs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -125,26 +125,18 @@ public override void OnInspectorGUI()
125125
private void ShowAddTriggerMenu()
126126
{
127127
var menu = new GenericMenu();
128-
var displayedNames = new List<GUIContent>(btnEnumInfo.displayedLength);
129-
var displayedValues = new List<int>(btnEnumInfo.displayedLength);
130-
for (int i = 0, imax = btnEnumInfo.displayedLength; i < imax; ++i)
128+
foreach (var ev in visibleInList.EnumValues)
131129
{
132-
var v = btnEnumInfo.displayedValues[i];
133-
displayedNames.Add(new GUIContent(btnEnumInfo.displayedNames[i]));
134-
displayedValues.Add(v);
135-
}
136-
for (int i = 0, imax = btnEnumInfo.displayedLength; i < imax; ++i)
137-
{
138-
var value = btnEnumInfo.displayedValues[i];
139-
//if (value == (int)ControllerButton.None) { continue; }
140-
var name = btnEnumInfo.displayedNames[i];
141-
if (visibleInList[value])
130+
var button = ev.Key;
131+
var visible = ev.Value;
132+
var name = visibleInList.EnumNameWithAlias((int)button);
133+
if (visible)
142134
{
143135
menu.AddDisabledItem(new GUIContent(name));
144136
}
145137
else
146138
{
147-
menu.AddItem(new GUIContent(name), false, OnAddNewSelected, value);
139+
menu.AddItem(new GUIContent(name), false, OnAddNewSelected, (int)button);
148140
}
149141
}
150142
menu.ShowAsContext();

0 commit comments

Comments
 (0)