using System.Collections.Generic; using System.Linq; using RosettaUI.UndoSystem; using UnityEditor; using UnityEngine; using UnityEngine.UIElements; namespace RosettaUI.Editor.UndoSystem { public class UndoHistoryEditorWindow : EditorWindow { private const string TitlePrefix = "Undo History"; [UnityEditor.MenuItem("Window/RosettaUI/Undo History")] public static void Open() { var window = GetWindow(); window.titleContent = new GUIContent("Undo History"); window.Show(); } private MultiColumnListView _multiColumnListView; private void OnEnable() { EditorApplication.update += OnEditorUpdate; } private void OnDisable() { EditorApplication.update -= OnEditorUpdate; } private void CreateGUI() { _multiColumnListView = new MultiColumnListView() { selectionType = SelectionType.None, showAlternatingRowBackgrounds = AlternatingRowBackground.ContentOnly }; var nameColumn = new Column() { name = nameof(IUndoRecord.Name), title = nameof(IUndoRecord.Name), bindCell = BindCellName, width = 200, }; var availableColumn = new Column() { name = nameof(IUndoRecord.IsAvailable), title = nameof(IUndoRecord.IsAvailable), makeCell = () => { var container = new VisualElement(){ style = { justifyContent = Justify.Center } }; container.Add(new Toggle()); return container; }, bindCell = BindCellIsAvailable, width = 100, }; _multiColumnListView.columns.Add(nameColumn); _multiColumnListView.columns.Add(availableColumn); rootVisualElement.Add(_multiColumnListView ); return; void BindCellName(VisualElement element, int i) { var list = (List)_multiColumnListView.itemsSource; ((Label)element).text = list[i].Name; SetCellStyle(element, i); } void BindCellIsAvailable(VisualElement element, int i) { var list = (List)_multiColumnListView.itemsSource; var toggle = element.Q(); toggle.value = list[i].IsAvailable; toggle.style.justifyContent = Justify.Center; SetCellStyle(toggle, i); } void SetCellStyle(VisualElement element, int i) { var redoRecordCount = UndoHistory.RedoRecords.Count(); // ReSharper disable once AccessToModifiedClosure var opacity = i < redoRecordCount ? 0.5f : 1f; element.style.opacity = opacity; } } private void OnEditorUpdate() { titleContent.text = $"{TitlePrefix}: {UndoHistory.HistoryStackNames.FirstOrDefault()}"; var list = UndoHistory.RedoRecords.Reverse().Concat(UndoHistory.UndoRecords).ToList(); _multiColumnListView.itemsSource = list; } } }