Skip to content

Commit

Permalink
Merge pull request #6 from brunomikoski/feature/bug-fixes-and-improve…
Browse files Browse the repository at this point in the history
…ments

add: updates
  • Loading branch information
brunomikoski committed May 9, 2024
2 parents 938162b + 9ff584a commit dce41c8
Show file tree
Hide file tree
Showing 14 changed files with 116 additions and 209 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).


## [0.2.0]
### Changed
- Changed initialization of the toolbar to improve performance
- Updated the toolbar to cache the items only once
- Toolbar Buttons now should be only visible when history has items
- Updated the system to sse the SessionState instead of Editor Prefs

### Added
- Forward button now also has right click support



## [0.1.1]
### Changed
- Changed implementation
Expand All @@ -25,6 +38,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- First initial working version

[0.1.1]: https://github.com/brunomikoski/UnityHistoryPanel/releases/tag/v0.2.0
[0.1.1]: https://github.com/brunomikoski/UnityHistoryPanel/releases/tag/v0.1.1
[0.1.0]: https://github.com/brunomikoski/UnityHistoryPanel/releases/tag/v0.1.0
[0.0.2]: https://github.com/brunomikoski/UnityHistoryPanel/releases/tag/v0.0.2
Expand Down
2 changes: 1 addition & 1 deletion Scripts/Editor/Core.meta

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

14 changes: 7 additions & 7 deletions Scripts/Editor/Core/SelectionData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,26 @@ internal class SelectionData
[SerializeField]
private List<int> instanceIDs = new List<int>();

private string m_displayName;
private string displayName;
public string DisplayName
{
get
{
if (string.IsNullOrEmpty(m_displayName))
if (string.IsNullOrEmpty(displayName))
{
m_displayName = string.Join(", ", GetSelectionObjects().Where(o => o != null).Select(o => o.name));
if (m_displayName.Length > 50)
m_displayName = m_displayName.Substring(0, 47) + "...";
displayName = string.Join(", ", GetSelectionObjects().Where(o => o != null).Select(o => o.name));
if (displayName.Length > 50)
displayName = displayName.Substring(0, 47) + "...";
}
return m_displayName;
return displayName;
}
}

public bool IsValid => GetSelectionObjects().Any(selectionObj => selectionObj != null);

public SelectionData(Object[] objects)
{
m_displayName = string.Empty;
displayName = string.Empty;
for (int i = 0; i < objects.Length; i++)
{
Object o = objects[i];
Expand Down
2 changes: 1 addition & 1 deletion Scripts/Editor/Core/SelectionData.cs.meta

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

5 changes: 5 additions & 0 deletions Scripts/Editor/Core/SelectionHistoryData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,20 @@ public void AddToHistory(Object[] objects)
return;
if (pointInTime < selectionData.Count - 1)
selectionData.RemoveRange(pointInTime, selectionData.Count - 1 - pointInTime);

selectionData.Add(item);

if (selectionData.Count > SelectionHistoryToolbar.MaximumHistoryItems)
selectionData.RemoveAt(0);

pointInTime = selectionData.Count - 1;
}

public void Back()
{
if (pointInTime == 0)
return;

for (int i = pointInTime - 1; i >= 0; i--)
{
SelectionData data = selectionData[i];
Expand All @@ -59,6 +63,7 @@ public void Forward()
return;
if (pointInTime + 1 > selectionData.Count - 1)
return;

pointInTime++;
movingInHistory = true;
selectionData[pointInTime].Select();
Expand Down
2 changes: 1 addition & 1 deletion Scripts/Editor/Core/SelectionHistoryData.cs.meta

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

61 changes: 49 additions & 12 deletions Scripts/Editor/Core/SelectionHistoryToolbar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ private static SelectionHistoryData History
return CACHED_HISTORY;

CACHED_HISTORY = new SelectionHistoryData();
string historyJson = EditorPrefs.GetString(HISTORY_STORAGE_KEY, string.Empty);
string historyJson = SessionState.GetString(HISTORY_STORAGE_KEY, string.Empty);
if (!string.IsNullOrEmpty(historyJson))
EditorJsonUtility.FromJsonOverwrite(historyJson, CACHED_HISTORY);
return CACHED_HISTORY;
Expand All @@ -32,6 +32,7 @@ private static SelectionHistoryData History


private static int? CACHED_MAXIMUM_HISTORY_ITEMS;

public static int MaximumHistoryItems
{
get
Expand All @@ -43,6 +44,9 @@ public static int MaximumHistoryItems
}
}

private static VisualElement backButton;
private static VisualElement forwardButton;


static SelectionHistoryToolbar()
{
Expand Down Expand Up @@ -78,8 +82,10 @@ private static void Initialize()
a => DropdownMenuAction.Status.None);

parent.Add(HISTORY_SELECTION_MENU);
parent.Add(AddButton("d_tab_prev@2x", "Go Back in Selection History", GoBack, ShowHistorySelectionMenu));
parent.Add(AddButton("d_tab_next@2x", "Go Forward in Selection History", GoForward));
backButton = AddButton("d_tab_prev@2x", "Go Back in Selection History", GoBack, ShowBackwardsHistory);
parent.Add(backButton);
forwardButton = AddButton("d_tab_next@2x", "Go Forward in Selection History", GoForward, ShowForwardHistory);
parent.Add(forwardButton);


UnityMainToolbarUtility.AddCustom(UnityMainToolbarUtility.TargetContainer.Left,
Expand All @@ -104,12 +110,19 @@ private static void SaveHistory()
return;

string json = EditorJsonUtility.ToJson(CACHED_HISTORY);
EditorPrefs.SetString(HISTORY_STORAGE_KEY, json);
SessionState.SetString(HISTORY_STORAGE_KEY, json);
}

private static void OnSelectionChanged()
{
History.AddToHistory(Selection.objects);
UpdateButtonsVisibility();
}

private static void UpdateButtonsVisibility()
{
backButton.SetEnabled(History.SelectionData.Count > 1 && History.PointInTime > 0);
forwardButton.SetEnabled(History.SelectionData.Count > 1 && History.PointInTime < History.SelectionData.Count - 1);
}

[MenuItem("Tools/Selection History/Go Back")]
Expand All @@ -128,38 +141,61 @@ private static void ClearHistory()
{
EditorPrefs.DeleteKey(HISTORY_STORAGE_KEY);
CACHED_HISTORY = new SelectionHistoryData();
UpdateButtonsVisibility();
}

private static void SetPointInTime(int itemIndex)
{
History.SetPointInTime(itemIndex);
}

private static void ShowHistorySelectionMenu()
private static void ShowBackwardsHistory()
{
HISTORY_SELECTION_MENU.menu.MenuItems().Clear();

for (int i = 0; i < History.SelectionData.Count; i++)
for (int i = History.PointInTime-1; i >= 0; i--)
{
SelectionData selectionData = History.SelectionData[i];

if (!selectionData.IsValid)
continue;

bool isOnCurrentItem = i == History.PointInTime;
int targetIndex = i;
HISTORY_SELECTION_MENU.menu.AppendAction(selectionData.DisplayName, a =>
{
SetPointInTime(targetIndex);
});
}


HISTORY_SELECTION_MENU.menu.AppendSeparator();
HISTORY_SELECTION_MENU.menu.AppendAction("Clear History", a =>
{
ClearHistory();
}, a => DropdownMenuAction.Status.Normal);

HISTORY_SELECTION_MENU.ShowMenu();
}

private static void ShowForwardHistory()
{
HISTORY_SELECTION_MENU.menu.MenuItems().Clear();

for (int i = History.PointInTime+1; i < History.SelectionData.Count; i++)
{
SelectionData selectionData = History.SelectionData[i];

if (!selectionData.IsValid)
continue;

DropdownMenuAction.Status status = DropdownMenuAction.Status.Normal;
if (isOnCurrentItem)
status = DropdownMenuAction.Status.Checked;

int targetIndex = i;
HISTORY_SELECTION_MENU.menu.AppendAction(selectionData.DisplayName, a =>
{
SetPointInTime(targetIndex);
}, a => status);
});
}


HISTORY_SELECTION_MENU.menu.AppendSeparator();
HISTORY_SELECTION_MENU.menu.AppendAction("Clear History", a =>
{
Expand All @@ -169,6 +205,7 @@ private static void ShowHistorySelectionMenu()
HISTORY_SELECTION_MENU.ShowMenu();
}


#region UI Elements visuals

private static VisualElement AddButton(string iconName, string tooltip, Action leftMouseClickCallback,
Expand Down
2 changes: 1 addition & 1 deletion Scripts/Editor/Core/SelectionHistoryToolbar.cs.meta

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

121 changes: 0 additions & 121 deletions Scripts/Editor/Extensions/ReflectionExtensions.cs

This file was deleted.

11 changes: 0 additions & 11 deletions Scripts/Editor/Extensions/ReflectionExtensions.cs.meta

This file was deleted.

2 changes: 1 addition & 1 deletion Scripts/Editor/Utility.meta

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

Loading

0 comments on commit dce41c8

Please sign in to comment.