DetailListBox.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System.Collections.Generic;
  2. using UnityEditor;
  3. using UnityEngine;
  4. namespace MTE
  5. {
  6. internal abstract class DetailListBox<T> where T: Detail
  7. {
  8. private Vector2 scrollPos = Vector2.zero;
  9. public int DoGUI(int selectedDetailIndex)
  10. {
  11. scrollPos = EditorGUILayout.BeginScrollView(scrollPos, GUILayout.Height(160f));
  12. EditorGUILayout.BeginVertical("box", GUILayout.MinHeight(64));
  13. {
  14. if (detailList == null || this.detailList.Count == 0)
  15. {
  16. NoDetailGUI();
  17. }
  18. else
  19. {
  20. for (int i = 0; i < this.detailList.Count; i += 4)
  21. {
  22. EditorGUILayout.BeginHorizontal(GUILayout.ExpandHeight(false));
  23. {
  24. for (int j = 0; j < 4; j++)
  25. {
  26. if (i + j >= this.detailList.Count) break;
  27. EditorGUILayout.BeginVertical(GUILayout.ExpandHeight(false));
  28. //toggle button
  29. bool toggleOn = selectedDetailIndex == i + j;
  30. var oldBgColor = GUI.backgroundColor;
  31. if (toggleOn)
  32. {
  33. GUI.backgroundColor = new Color(62/255.0f, 125/255.0f, 231/255.0f);
  34. }
  35. var new_toggleOn = GUILayout.Toggle(toggleOn,
  36. GUIContent.none,
  37. GUI.skin.button,
  38. GUILayout.Width(72), GUILayout.Height(72));
  39. GUI.backgroundColor = oldBgColor;
  40. var rect = GUILayoutUtility.GetLastRect();
  41. var detailIndex = i + j;
  42. DrawButtonBackground(detailIndex, rect);
  43. if (new_toggleOn && !toggleOn)
  44. {
  45. selectedDetailIndex = i + j;
  46. }
  47. EditorGUILayout.EndVertical();
  48. }
  49. }
  50. EditorGUILayout.EndHorizontal();
  51. }
  52. }
  53. }
  54. EditorGUILayout.EndVertical();
  55. EditorGUILayout.EndScrollView();
  56. MenuButton(new GUIContent($"{StringTable.Get(C.Edit)} {StringTable.Get(DetailType)}"));
  57. this.selectedIndex = selectedDetailIndex;
  58. return selectedDetailIndex;
  59. }
  60. public virtual void NoDetailGUI()
  61. {
  62. EditorGUILayout.LabelField(StringTable.Get(C.Warning_NoDetail));
  63. }
  64. private void MenuButton(GUIContent title)
  65. {
  66. GUIContent content = new GUIContent(title.text, EditorGUIUtility.IconContent("TerrainInspector.TerrainToolSettings").image, title.tooltip);
  67. Rect rect = GUILayoutUtility.GetRect(content, "Button");
  68. if (GUI.Button(rect, content))
  69. {
  70. GenericMenu menu = new GenericMenu();
  71. menu.AddItem(new GUIContent(StringTable.Get(C.Add)), false, AddCallback);
  72. if (this.detailList.Count > 0)
  73. {
  74. menu.AddItem(new GUIContent(StringTable.Get(C.Edit)), false, EditCallback);
  75. }
  76. else
  77. {
  78. menu.AddDisabledItem(new GUIContent(StringTable.Get(C.Edit)));
  79. }
  80. if (this.detailList.Count > 0)
  81. {
  82. menu.AddItem(new GUIContent(StringTable.Get(C.Remove)), false, RemoveCallback);
  83. }
  84. else
  85. {
  86. menu.AddDisabledItem(new GUIContent(StringTable.Get(C.Remove)));
  87. }
  88. menu.ShowAsContext();
  89. }
  90. }
  91. public virtual void DrawButtonBackground(int detailIndex, Rect buttonRect)
  92. {
  93. }
  94. public virtual void SetEditingTarget(IList<T> targetDetailList)
  95. {
  96. detailList = targetDetailList;
  97. }
  98. protected virtual void AddCallback()
  99. {
  100. }
  101. protected virtual void EditCallback()
  102. {
  103. }
  104. private void RemoveCallback()
  105. {
  106. bool confirmed = EditorUtility.DisplayDialog(
  107. StringTable.Get(C.Warning),
  108. StringTable.Get(C.Warning_Confirm),
  109. StringTable.Get(C.Yes), StringTable.Get(C.No));
  110. if (confirmed)
  111. {
  112. this.detailList.RemoveAt(this.selectedIndex);
  113. this.SaveDetailList();
  114. MTEEditorWindow.Instance.Repaint();
  115. }
  116. }
  117. protected abstract void SaveDetailList();
  118. protected abstract C DetailType { get; }
  119. protected IList<T> detailList;
  120. protected int selectedIndex;
  121. }
  122. }