Skip to content

Commit

Permalink
fix: AddNewSkinPartsボタンやAddNewSkinStyleボタンを押下した際、1回目のみFoldoutを開き、それ以降…
Browse files Browse the repository at this point in the history
…同じボタンを連打した場合は閉じた状態で増やさせるようにした
  • Loading branch information
pspkurara committed Nov 8, 2020
1 parent 4c33f1f commit 98a59b7
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 3 deletions.
83 changes: 83 additions & 0 deletions Packages/uGUI-Skinner/Editor/GUIUniqueIdentifier.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using System.Collections.Generic;

namespace Pspkurara.UI.Skinner
{

/// <summary>
/// GUIの要素のユニークIDを記録管理する
/// </summary>
internal sealed class GUIUniqueIdentifier
{

private List<int> m_NestedIdentifier = new List<int>();

private List<int> m_LastRecordGuiId = new List<int>();

/// <summary>
/// 最後に触ったGUI番号が現在のGUI番号と一致しているか
/// </summary>
public bool isCurrentLastControlGuiId
{
get
{
if (m_LastRecordGuiId.Count == 0) return false;
if (m_LastRecordGuiId.Count != m_NestedIdentifier.Count) return false;
for (int i = 0; i < m_NestedIdentifier.Count; i++)
{
if (m_NestedIdentifier[i] != m_LastRecordGuiId[i])
{
return false;
}
}
return true;
}
}

public void ResetLastControlGuiId()
{
m_LastRecordGuiId.Clear();
}

public void Initialize()
{
m_NestedIdentifier.Clear();
m_NestedIdentifier.Add(0);
}

/// <summary>
/// ループネストを一段階潜る
/// </summary>
public void BeginNestedLoop()
{
m_NestedIdentifier.Add(0);
}

/// <summary>
/// ループネストを一段階昇る
/// </summary>
public void EndNestedLoop()
{
if (m_NestedIdentifier.Count <= 1) return;
m_NestedIdentifier.RemoveAt(m_NestedIdentifier.Count - 1);
}

/// <summary>
/// GUI番号をカウントする
/// </summary>
public void Next()
{
m_NestedIdentifier[m_NestedIdentifier.Count - 1]++;
}

/// <summary>
/// 最後に触ったGUI番号を記録する
/// </summary>
public void RecordLastGuiId()
{
m_LastRecordGuiId.Clear();
m_LastRecordGuiId.AddRange(m_NestedIdentifier);
}

}

}
11 changes: 11 additions & 0 deletions Packages/uGUI-Skinner/Editor/GUIUniqueIdentifier.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 65 additions & 3 deletions Packages/uGUI-Skinner/Editor/UISkinnerInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public static class FieldName
private GUIContent[] m_SkinnerPartsDisplayNames = null;
private GUIContent m_SkinFoldoutTitle = null;
private GUIContent m_CurrentSelectStyleTitle = null;
private GUIUniqueIdentifier m_GuiUniqueIdCounter = new GUIUniqueIdentifier();

#endregion

Expand Down Expand Up @@ -68,15 +69,19 @@ private void OnEnable()
m_StyleIndex = serializedObject.FindProperty(FieldName.StyleIndex);

m_SkinPartsProperty = new EditorSkinPartsPropertry();

m_GuiUniqueIdCounter.ResetLastControlGuiId();
}

public override void OnInspectorGUI()
{
m_GuiUniqueIdCounter.Initialize();

serializedObject.Update();

int edittedCurrentStyle = currentStyleIndex;
GUILayout.Label(EditorConst.CurrentSelectStyleTitle);
m_GuiUniqueIdCounter.Next();
GUILayout.BeginHorizontal();
{
if (GUILayout.Button(EditorConst.LeftSkinSelectArrow, EditorConst.SkinSelectArrowMaxWidth))
Expand All @@ -98,11 +103,14 @@ public override void OnInspectorGUI()

if (currentStyleIndex != edittedCurrentStyle)
{
m_GuiUniqueIdCounter.RecordLastGuiId();
if (edittedCurrentStyle < 0 || edittedCurrentStyle >= m_SkinStyles.arraySize) return;
currentStyleIndex = edittedCurrentStyle;
ApplySkin();
}

m_GuiUniqueIdCounter.BeginNestedLoop();

for (int skinStylesIndex = 0; skinStylesIndex < m_SkinStyles.arraySize; skinStylesIndex++)
{

Expand All @@ -116,7 +124,13 @@ public override void OnInspectorGUI()

EditorGUILayout.BeginHorizontal();
m_SkinFoldoutTitle.text = hasStyleKey ? string.Format(EditorConst.SkinFoldTitleHasStyleKey, skinStylesIndex, styleKey.stringValue) : string.Format(EditorConst.SkinFoldTitle, skinStylesIndex);
EditorGUI.BeginChangeCheck();
m_GuiUniqueIdCounter.Next();
skinStyleElementProperty.isExpanded = EditorGUILayout.Foldout(skinStyleElementProperty.isExpanded, m_SkinFoldoutTitle, true, style);
if (EditorGUI.EndChangeCheck())
{
m_GuiUniqueIdCounter.RecordLastGuiId();
}

if (skinStyleElementProperty.isExpanded)
{
Expand All @@ -129,6 +143,8 @@ public override void OnInspectorGUI()

EditorGUI.indentLevel -= EditorConst.SkinStyleChildIndent;

m_GuiUniqueIdCounter.BeginNestedLoop();

for (int skinPartsIndex = 0; skinPartsIndex < skinPartsProperty.arraySize; skinPartsIndex++)
{

Expand All @@ -142,13 +158,21 @@ public override void OnInspectorGUI()

EditorGUILayout.BeginHorizontal();

EditorGUI.BeginChangeCheck();
m_GuiUniqueIdCounter.Next();
skinPartsElementProperty.isExpanded = EditorGUILayout.Toggle(GUIContent.none, skinPartsElementProperty.isExpanded, EditorStyles.foldout, GUILayout.Width(12));
if (EditorGUI.EndChangeCheck())
{
m_GuiUniqueIdCounter.RecordLastGuiId();
}

m_GuiUniqueIdCounter.Next();
int skinPartsTypeEditted = EditorGUILayout.IntPopup(skinPartsType, m_SkinnerPartsDisplayNames, m_SkinnerPartsOptionValues);
if (skinPartsTypeEditted != skinPartsType)
{
skinPartsTypeProperty.intValue = skinPartsTypeEditted;
serializedObject.ApplyModifiedProperties();
m_GuiUniqueIdCounter.RecordLastGuiId();
return;
}
EditorGUILayout.EndHorizontal();
Expand All @@ -171,6 +195,7 @@ public override void OnInspectorGUI()
m_SkinPartsProperty.MapProperties(skinPartsElementProperty.FindPropertyRelative(FieldName.Property));

EditorGUI.BeginChangeCheck();
m_GuiUniqueIdCounter.Next();
bool showMixedValue = EditorGUI.showMixedValue;
inspector.DrawInspector(m_SkinPartsProperty);
EditorGUI.showMixedValue = showMixedValue;
Expand All @@ -181,6 +206,7 @@ public override void OnInspectorGUI()
ApplySkin();
if (skinPartsType == 100) inspector.DrawInspector(m_SkinPartsProperty);
}
m_GuiUniqueIdCounter.RecordLastGuiId();
}

}
Expand All @@ -202,10 +228,12 @@ public override void OnInspectorGUI()

EditorGUI.indentLevel += EditorConst.SkinStyleChildIndent;

m_GuiUniqueIdCounter.Next();
if (SkinnerEditorUtility.DrawRemoveButton(EditorConst.RemovePartsButtonTitle, () =>
{
skinPartsProperty.DeleteArrayElementAtIndex(skinPartsIndex);
serializedObject.ApplyModifiedProperties();
m_GuiUniqueIdCounter.RecordLastGuiId();
})) return;

EditorGUI.indentLevel -= EditorConst.SkinStyleChildIndent;
Expand All @@ -221,9 +249,19 @@ public override void OnInspectorGUI()
EditorGUI.indentLevel += EditorConst.SkinStyleChildIndent;

EditorGUILayout.BeginHorizontal();

m_GuiUniqueIdCounter.EndNestedLoop();

m_GuiUniqueIdCounter.Next();
if (SkinnerEditorUtility.DrawAddButton(EditorConst.AddPartsButtonTitle, () => {
skinPartsProperty.InsertArrayElementAtIndex(skinPartsProperty.arraySize);
// 同じボタンを連打した場合は2回目以降Expandedの変更はしない
if (!m_GuiUniqueIdCounter.isCurrentLastControlGuiId)
{
skinPartsProperty.GetArrayElementAtIndex(skinPartsProperty.arraySize - 1).isExpanded = true;
}
serializedObject.ApplyModifiedProperties();
m_GuiUniqueIdCounter.RecordLastGuiId();
})) return;

EditorGUILayout.Space();
Expand All @@ -232,6 +270,7 @@ public override void OnInspectorGUI()

}

m_GuiUniqueIdCounter.Next();
if (SkinnerEditorUtility.DrawRemoveButton(EditorConst.RemoveSkinButtonTitle, () => {
m_SkinStyles.DeleteArrayElementAtIndex(skinStylesIndex);
if (currentStyleIndex >= skinLength)
Expand All @@ -252,6 +291,7 @@ public override void OnInspectorGUI()
}
}
serializedObject.ApplyModifiedProperties();
m_GuiUniqueIdCounter.RecordLastGuiId();
})) return;
EditorGUILayout.EndHorizontal();

Expand All @@ -264,25 +304,47 @@ public override void OnInspectorGUI()
serializedObject.ApplyModifiedProperties();
}

m_GuiUniqueIdCounter.EndNestedLoop();

EditorGUILayout.BeginHorizontal();

m_GuiUniqueIdCounter.Next();
if (SkinnerEditorUtility.DrawAddButton(EditorConst.AddSkinButtonTitle, () => {
m_SkinStyles.InsertArrayElementAtIndex(m_SkinStyles.arraySize);
var addedStyle = m_SkinStyles.GetArrayElementAtIndex(m_SkinStyles.arraySize - 1);
bool expanded = true;
bool addedStyleExpanded = true;
// 2つ目以降のスタイルを増やそうとしたときは増やした側にコピー元のExpandedの設定を継承させる
if (m_SkinStyles.arraySize > 1)
{
expanded = m_SkinStyles.GetArrayElementAtIndex(m_SkinStyles.arraySize - 2).isExpanded;
var preStyle = m_SkinStyles.GetArrayElementAtIndex(m_SkinStyles.arraySize - 2);
var preStylePartsArray = preStyle.FindPropertyRelative(FieldName.Parts);
var addedStylePartsArray = addedStyle.FindPropertyRelative(FieldName.Parts);
for (int i = 0; i < preStylePartsArray.arraySize; i++)
{
var addedStyleParts = addedStylePartsArray.GetArrayElementAtIndex(i);
var preStyleParts = preStylePartsArray.GetArrayElementAtIndex(i);
addedStyleParts.isExpanded = preStyleParts.isExpanded;
}

addedStyleExpanded = preStyle.isExpanded;
}
m_SkinStyles.GetArrayElementAtIndex(m_SkinStyles.arraySize - 1).isExpanded = expanded;

if (!m_GuiUniqueIdCounter.isCurrentLastControlGuiId)
{
addedStyle.isExpanded = addedStyleExpanded;
}

serializedObject.ApplyModifiedProperties();
m_GuiUniqueIdCounter.RecordLastGuiId();
})) return;

EditorGUILayout.Space();

m_GuiUniqueIdCounter.Next();
if (SkinnerEditorUtility.DrawCleanupButton(EditorConst.CleanupButtonTitle, () => {
Cleanup();
serializedObject.ApplyModifiedProperties();
m_GuiUniqueIdCounter.RecordLastGuiId();
})) return;

EditorGUILayout.EndHorizontal();
Expand Down

0 comments on commit 98a59b7

Please sign in to comment.