StringTableListGUI.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. namespace Funly.SkyStudio {
  6. public abstract class StringTableListGUI
  7. {
  8. public static float RowHeight = 40.0f;
  9. private static Texture2D _deleteRowIcon;
  10. private static Texture2D _upRowIcon;
  11. private static Texture2D _downRowIcon;
  12. // Returns the index
  13. public static bool RenderTableList(
  14. List<string> list, out int deleteIndex, out bool didSwapRows, out int swapIndex1, out int swapIndex2)
  15. {
  16. bool didModifyList = false;
  17. deleteIndex = -1;
  18. didSwapRows = false;
  19. swapIndex1 = -1;
  20. swapIndex2 = -1;
  21. EditorGUILayout.BeginVertical(GUI.skin.box);
  22. GUIStyle rowStyle = new GUIStyle(GUI.skin.label);
  23. const float buttonHeight = 15.0f;
  24. for (int i = 0; i < list.Count; i++)
  25. {
  26. string item = list[i];
  27. EditorGUILayout.BeginVertical();
  28. EditorGUILayout.BeginHorizontal(rowStyle);
  29. EditorGUILayout.LabelField(item, GUI.skin.label);
  30. GUILayout.FlexibleSpace();
  31. if (_deleteRowIcon == null)
  32. {
  33. _deleteRowIcon = SkyEditorUtility.LoadEditorResourceTexture("RowDelete");
  34. }
  35. if (_upRowIcon == null)
  36. {
  37. _upRowIcon = SkyEditorUtility.LoadEditorResourceTexture("RowUp");
  38. }
  39. if (_downRowIcon == null)
  40. {
  41. _downRowIcon = SkyEditorUtility.LoadEditorResourceTexture("RowDown");
  42. }
  43. // Tint our images to match the editor skin text.
  44. Color originalColor = GUI.contentColor;
  45. GUI.contentColor = GUI.skin.label.normal.textColor;
  46. // Move row up.
  47. if (i - 1 >= 0)
  48. {
  49. EditorGUI.BeginChangeCheck();
  50. GUILayout.Button(new GUIContent(_upRowIcon), GUILayout.Height(buttonHeight));
  51. if (EditorGUI.EndChangeCheck()) {
  52. // Swap with row above.
  53. swapIndex1 = i;
  54. swapIndex2 = i - 1;
  55. didSwapRows = true;
  56. didModifyList = true;
  57. }
  58. }
  59. // Move row down.
  60. if (i + 1 < list.Count)
  61. {
  62. EditorGUI.BeginChangeCheck();
  63. GUILayout.Button(new GUIContent(_downRowIcon), GUILayout.Height(buttonHeight));
  64. if (EditorGUI.EndChangeCheck()) {
  65. swapIndex1 = i;
  66. swapIndex2 = i + 1;
  67. didSwapRows = true;
  68. didModifyList = true;
  69. }
  70. }
  71. // Delete this row.
  72. EditorGUI.BeginChangeCheck();
  73. GUILayout.Button(new GUIContent(_deleteRowIcon), GUILayout.Height(buttonHeight));
  74. if (EditorGUI.EndChangeCheck())
  75. {
  76. didModifyList = true;
  77. deleteIndex = i;
  78. }
  79. GUI.contentColor = originalColor;
  80. EditorGUILayout.EndHorizontal();
  81. // Draw a divider between rows.
  82. if (i != list.Count - 1)
  83. {
  84. Rect dividerRect = EditorGUILayout.GetControlRect(false, 1.0f);
  85. EditorGUI.DrawRect(dividerRect, Color.gray);
  86. }
  87. EditorGUILayout.EndVertical();
  88. }
  89. GUILayout.EndVertical();
  90. GUI.changed = didModifyList;
  91. return didModifyList;
  92. }
  93. public Texture2D TextureFromColor(Color c)
  94. {
  95. Texture2D tex = new Texture2D(1, 1);
  96. tex.SetPixel(0, 0, c);
  97. tex.Apply();
  98. return tex;
  99. }
  100. }
  101. }