Skip to content

Commit 15cc257

Browse files
committed
choice ReorderableList
1 parent f4f85f0 commit 15cc257

File tree

2 files changed

+64
-20
lines changed

2 files changed

+64
-20
lines changed

Core/AvatarChooseMenu.cs

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#if UNITY_EDITOR
99
using UnityEditor;
10+
using UnityEditorInternal;
1011
using net.narazaka.avatarmenucreator.util;
1112
using System.Text.RegularExpressions;
1213
#endif
@@ -189,6 +190,7 @@ protected override void OnMultiGUI(SerializedProperty serializedProperty)
189190
string ChooseNameRenameSearch = "";
190191
string ChooseNameRenameResult = "";
191192
Vector2 ScrollPositionChoises;
193+
ReorderableList ChoiceList = null;
192194

193195
protected override void OnHeaderGUI(IList<string> children)
194196
{
@@ -218,7 +220,10 @@ protected override void OnHeaderGUI(IList<string> children)
218220
EditorGUILayout.BeginHorizontal();
219221
var rect = EditorGUILayout.GetControlRect(false, GUILayout.Width(EditorGUIUtility.labelWidth));
220222
ChoiceFoldout = EditorGUI.Foldout(rect, ChoiceFoldout, T.選択肢の数);
221-
ChooseCount = IntField(ChooseCount);
223+
ChooseCount = IntField(ChooseCount, index =>
224+
{
225+
ChoiceList = null;
226+
});
222227
EditorGUILayout.EndHorizontal();
223228

224229
if (ChooseCount < 1) ChooseCount = 1;
@@ -320,29 +325,41 @@ protected override void OnHeaderGUI(IList<string> children)
320325

321326
void HeaderChoises()
322327
{
323-
EditorGUI.indentLevel++;
324-
EditorGUIUtility.labelWidth = 65;
325-
for (var i = 0; i < ChooseCount; ++i)
328+
if (ChoiceList == null)
326329
{
327-
EditorGUILayout.BeginHorizontal();
328-
ChooseNames[i] = TextField($"{T.選択肢}{i}", ChooseName(i));
329-
ChooseIcons[i] = TextureField(ChooseIcon(i));
330-
if (GUILayout.Button("↑", GUILayout.Width(19)))
330+
ChoiceList = new ReorderableList(Enumerable.Range(0, ChooseCount).ToList(), typeof(int), true, false, true, true);
331+
ChoiceList.drawElementCallback = (rect, index, isActive, isFocused) =>
331332
{
332-
MoveChoice(i, i > 0 ? i - 1 : ChooseCount - 1);
333-
}
334-
if (GUILayout.Button("↓", GUILayout.Width(19)))
333+
rect.height = EditorGUIUtility.singleLineHeight;
334+
rect.y += 2;
335+
rect.width -= EditorGUIUtility.standardVerticalSpacing;
336+
rect.width /= 2;
337+
EditorGUIUtility.labelWidth = 50;
338+
ChooseNames[index] = TextField(rect, $"{T.選択肢}{index}", ChooseName(index));
339+
EditorGUIUtility.labelWidth = 0;
340+
rect.x += rect.width + EditorGUIUtility.standardVerticalSpacing;
341+
ChooseIcons[index] = TextureField(rect, ChooseIcon(index));
342+
};
343+
ChoiceList.onAddCallback = list =>
335344
{
336-
MoveChoice(i, i < ChooseCount - 1 ? i + 1 : 0);
337-
}
338-
if (GUILayout.Button("×", GUILayout.Width(19)))
345+
WillChange();
346+
ChooseCount++;
347+
var index = ChooseCount - 1;
348+
ChooseNames[index] = ChooseName(index);
349+
ChooseIcons[index] = ChooseIcon(index);
350+
list.list.Add(index); // dummy
351+
};
352+
ChoiceList.onRemoveCallback = list =>
339353
{
340-
RemoveChoice(i);
341-
}
342-
EditorGUILayout.EndHorizontal();
354+
RemoveChoice(list.index);
355+
list.list.RemoveAt(list.index);
356+
};
357+
ChoiceList.onReorderCallbackWithDetails = (list, oldIndex, newIndex) =>
358+
{
359+
MoveChoice(oldIndex, newIndex);
360+
};
343361
}
344-
EditorGUIUtility.labelWidth = 0;
345-
EditorGUI.indentLevel--;
362+
ChoiceList.DoLayoutList();
346363
}
347364

348365
protected override bool HasHeaderBulkGUI => true;

Core/AvatarMenuBase.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,19 @@ protected string TextField(string label, string value)
451451
}
452452
}
453453

454+
protected string TextField(Rect rect, string label, string value)
455+
{
456+
using (var check = new EditorGUI.ChangeCheckScope())
457+
{
458+
var newValue = EditorGUI.TextField(rect, label, value);
459+
if (check.changed)
460+
{
461+
WillChange();
462+
}
463+
return newValue;
464+
}
465+
}
466+
454467
protected int IntField(string label, int value)
455468
{
456469
using (var check = new EditorGUI.ChangeCheckScope())
@@ -464,14 +477,15 @@ protected int IntField(string label, int value)
464477
}
465478
}
466479

467-
protected int IntField(int value)
480+
protected int IntField(int value, Action<int> onChange = null)
468481
{
469482
using (var check = new EditorGUI.ChangeCheckScope())
470483
{
471484
var newValue = EditorGUILayout.IntField(value);
472485
if (check.changed)
473486
{
474487
WillChange();
488+
onChange?.Invoke(newValue);
475489
}
476490
return newValue;
477491
}
@@ -529,6 +543,19 @@ protected Texture2D TextureField(Texture2D texture2D)
529543
}
530544
}
531545

546+
protected Texture2D TextureField(Rect rect, Texture2D texture2D)
547+
{
548+
using (var check = new EditorGUI.ChangeCheckScope())
549+
{
550+
var newValue = EditorGUI.ObjectField(rect, texture2D, typeof(Texture2D), false) as Texture2D;
551+
if (check.changed)
552+
{
553+
WillChange();
554+
}
555+
return newValue;
556+
}
557+
}
558+
532559
// for MaterialPickerButton
533560
protected virtual Material[] GetMaterialSlots(string child) { throw new NotImplementedException(); }
534561

0 commit comments

Comments
 (0)