VoxelFrameAnimationListWindow.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using UnityEngine;
  2. using UnityEngine.Assertions;
  3. using UnityEditor;
  4. using UnityEditorInternal;
  5. using System;
  6. using System.IO;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. namespace VoxelImporter
  10. {
  11. public class VoxelFrameAnimationListWindow : EditorWindow
  12. {
  13. public static VoxelFrameAnimationListWindow instance;
  14. public VoxelFrameAnimationObject objectTarget { get; private set; }
  15. public event Action frameIndexChanged;
  16. public event Action previewCameraModeChanged;
  17. private GUIStyle guiStyleButton;
  18. private GUIStyle guiStyleActiveButton;
  19. private GUIStyle guiStyleNameLabel;
  20. private static float frameIconSize = 64f;
  21. private Vector3 scrollPosition;
  22. public static void Create(VoxelFrameAnimationObject objectTarget)
  23. {
  24. if (instance == null)
  25. {
  26. instance = CreateInstance<VoxelFrameAnimationListWindow>();
  27. }
  28. instance.Initialize(objectTarget);
  29. instance.ShowUtility();
  30. }
  31. public static void Destroy()
  32. {
  33. if (instance != null)
  34. {
  35. instance.Close();
  36. }
  37. }
  38. void OnEnable()
  39. {
  40. InternalEditorUtility.RepaintAllViews();
  41. }
  42. void OnDisable()
  43. {
  44. instance = null;
  45. InternalEditorUtility.RepaintAllViews();
  46. }
  47. void OnDestroy()
  48. {
  49. OnDisable();
  50. }
  51. void OnSelectionChange()
  52. {
  53. var go = Selection.activeGameObject;
  54. if (go != objectTarget)
  55. {
  56. Close();
  57. }
  58. }
  59. private void Initialize(VoxelFrameAnimationObject objectTarget)
  60. {
  61. this.objectTarget = objectTarget;
  62. UpdateTitle();
  63. }
  64. void Update()
  65. {
  66. if (instance == null)
  67. {
  68. Close();
  69. }
  70. }
  71. void OnGUI()
  72. {
  73. #region GUIStyle
  74. if (guiStyleButton == null)
  75. guiStyleButton = new GUIStyle(GUI.skin.button);
  76. guiStyleButton.margin = new RectOffset(0, 0, 0, 0);
  77. guiStyleButton.overflow = new RectOffset(0, 0, 0, 0);
  78. guiStyleButton.padding = new RectOffset(0, 0, 0, 0);
  79. if (guiStyleActiveButton == null)
  80. guiStyleActiveButton = new GUIStyle(GUI.skin.button);
  81. guiStyleActiveButton.margin = new RectOffset(0, 0, 0, 0);
  82. guiStyleActiveButton.overflow = new RectOffset(0, 0, 0, 0);
  83. guiStyleActiveButton.padding = new RectOffset(0, 0, 0, 0);
  84. guiStyleActiveButton.normal = guiStyleActiveButton.active;
  85. if (guiStyleNameLabel == null)
  86. guiStyleNameLabel = new GUIStyle(GUI.skin.label);
  87. guiStyleNameLabel.alignment = TextAnchor.LowerCenter;
  88. #endregion
  89. EditorGUILayout.BeginHorizontal();
  90. {
  91. #region PreviewCameraMode
  92. {
  93. EditorGUI.BeginChangeCheck();
  94. var edit_previewCameraMode = (VoxelFrameAnimationObject.Edit_CameraMode)EditorGUILayout.EnumPopup(objectTarget.edit_previewCameraMode, GUILayout.Width(64));
  95. if (EditorGUI.EndChangeCheck())
  96. {
  97. Undo.RecordObject(objectTarget, "Camera Mode");
  98. objectTarget.edit_previewCameraMode = edit_previewCameraMode;
  99. if (previewCameraModeChanged != null)
  100. previewCameraModeChanged.Invoke();
  101. EditorApplication.delayCall += () =>
  102. {
  103. InternalEditorUtility.RepaintAllViews();
  104. };
  105. }
  106. }
  107. #endregion
  108. EditorGUILayout.Space();
  109. #region Size
  110. {
  111. frameIconSize = EditorGUILayout.Slider(frameIconSize, 32f, 128f);
  112. }
  113. #endregion
  114. }
  115. EditorGUILayout.EndHorizontal();
  116. EditorGUILayout.Space();
  117. scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);
  118. {
  119. int countX = Math.Max(1, Mathf.FloorToInt(position.width / frameIconSize));
  120. int countY = Mathf.CeilToInt(objectTarget.frames.Count / (float)countX);
  121. for (int i = 0; i < countY; i++)
  122. {
  123. EditorGUILayout.BeginHorizontal();
  124. for (int j = 0; j < countX; j++)
  125. {
  126. var index = i * countX + j;
  127. if (index >= objectTarget.frames.Count) break;
  128. var rect = EditorGUILayout.GetControlRect(false, frameIconSize, guiStyleButton, GUILayout.Width(frameIconSize), GUILayout.Height(frameIconSize));
  129. if (GUI.Button(rect, objectTarget.frames[index].icon, index != objectTarget.edit_frameIndex ? guiStyleButton : guiStyleActiveButton))
  130. {
  131. Undo.RecordObject(objectTarget, "Select Frame");
  132. objectTarget.edit_frameIndex = index;
  133. if (frameIndexChanged != null)
  134. frameIndexChanged.Invoke();
  135. UpdateTitle();
  136. InternalEditorUtility.RepaintAllViews();
  137. }
  138. GUI.Label(rect, objectTarget.frames[index].name, guiStyleNameLabel);
  139. }
  140. EditorGUILayout.EndHorizontal();
  141. }
  142. {
  143. var index = -1;
  144. var rect = EditorGUILayout.GetControlRect(false, frameIconSize, guiStyleButton, GUILayout.Width(frameIconSize), GUILayout.Height(frameIconSize));
  145. if (GUI.Button(rect, "", index != objectTarget.edit_frameIndex ? guiStyleButton : guiStyleActiveButton))
  146. {
  147. Undo.RecordObject(objectTarget, "Select Frame");
  148. objectTarget.edit_frameIndex = index;
  149. if (frameIndexChanged != null)
  150. frameIndexChanged.Invoke();
  151. UpdateTitle();
  152. InternalEditorUtility.RepaintAllViews();
  153. }
  154. GUI.Label(rect, "None", guiStyleNameLabel);
  155. }
  156. }
  157. EditorGUILayout.EndScrollView();
  158. }
  159. public void FrameIndexChanged()
  160. {
  161. UpdateTitle();
  162. Repaint();
  163. }
  164. private void UpdateTitle()
  165. {
  166. if (objectTarget.edit_frameEnable)
  167. {
  168. var frame = objectTarget.edit_currentFrame;
  169. instance.titleContent = new GUIContent(string.Format("Frame List ({0}) - ({1} / {2})", frame.name, objectTarget.edit_frameIndex, objectTarget.frames.Count));
  170. }
  171. else
  172. {
  173. instance.titleContent = new GUIContent("Frame List");
  174. }
  175. }
  176. }
  177. }