HistoryManager.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright 2017-2021 marynate. All Rights Reserved.
  2. #pragma once
  3. #include "CoreMinimal.h"
  4. #include "SourcesData.h"
  5. class FMenuBuilder;
  6. struct FSelectionData
  7. {
  8. TSet<FName> SelectedAssets;
  9. TSet<FString> SelectedFolders;
  10. int32 Num() const
  11. {
  12. return SelectedAssets.Num() + SelectedFolders.Num();
  13. }
  14. void Reset()
  15. {
  16. SelectedAssets.Reset();
  17. SelectedFolders.Reset();
  18. }
  19. void Empty()
  20. {
  21. SelectedAssets.Empty();
  22. SelectedFolders.Empty();
  23. }
  24. };
  25. /** The history data object, storing all important history data */
  26. struct FHistoryData
  27. {
  28. /** History Description */
  29. FText HistoryDesc;
  30. /** The base set of filters on the asset view which includes selected paths and collections */
  31. FSourcesData SourcesData;
  32. /** The selection data from before the sources changed */
  33. FSelectionData SelectionData;
  34. };
  35. /** The delegate for when history data should be applied */
  36. DECLARE_DELEGATE_OneParam(FOnApplyHistoryData, const FHistoryData& /*Data*/);
  37. /** The delegate for when history data should be updated */
  38. DECLARE_DELEGATE_OneParam(FOnUpdateHistoryData, FHistoryData& /*Data*/);
  39. /** The class responsible for managing all content browser history */
  40. class FHistoryManager
  41. {
  42. public:
  43. /** Constructor */
  44. FHistoryManager();
  45. /** Set the delegate for applying history data */
  46. void SetOnApplyHistoryData(const FOnApplyHistoryData& InOnApplyHistoryData);
  47. /** Set the delegate for updating history data */
  48. void SetOnUpdateHistoryData(const FOnUpdateHistoryData& InOnUpdateHistoryData);
  49. /** Goes back one history snapshot and returns the history data at that snapshot */
  50. bool GoBack();
  51. /** Goes forward one history snapshot and returns the history data at that snapshot */
  52. bool GoForward();
  53. /** Stores new history data. Called when creating a history snapshot */
  54. void AddHistoryData();
  55. /** Triggers an update for the current history data. This is typically done right before changing the history. */
  56. void UpdateHistoryData();
  57. /** Determines if a user can go forward in history */
  58. bool CanGoForward() const;
  59. /** Determines if a user can go back in history */
  60. bool CanGoBack() const;
  61. /** Gets the description of the previous history entry */
  62. FText GetBackDesc() const;
  63. /** Gets the description of the next history entry */
  64. FText GetForwardDesc() const;
  65. /**
  66. * Populates a list of menu items that can be added to a context menu
  67. * to allow a user to jump to different history snapshots instead of using the back and forward buttons
  68. *
  69. * @param bGetPrior If true gets history snapshots prior to the current history index(for navigating back). If false get history snapshots after the current history index (for navigating forward).
  70. * @param MenuBuilder The menubuilder to populate with menu items
  71. */
  72. void GetAvailableHistoryMenuItems(bool bGetPrior, FMenuBuilder& MenuBuilder);
  73. /** Removes all history data as determined by the passed in predicate */
  74. template <class PREDICATE_CLASS>
  75. void RemoveHistoryData(const PREDICATE_CLASS& Predicate)
  76. {
  77. for (int32 HistoryIndex = 0; HistoryIndex < HistoryData.Num(); HistoryIndex++)
  78. {
  79. if (Predicate(HistoryData[HistoryIndex]))
  80. {
  81. HistoryData.RemoveAt(HistoryIndex);
  82. if (CurrentHistoryIndex >= HistoryIndex)
  83. {
  84. // Ensure if possible that current history index continues to point to the same item if something is removed before it.
  85. CurrentHistoryIndex = FMath::Max(0, CurrentHistoryIndex - 1);
  86. }
  87. HistoryIndex--;
  88. }
  89. }
  90. }
  91. private:
  92. /** Notifies the owner to update to the state described by the current history data */
  93. void ApplyCurrentHistoryData();
  94. /** Notifies the owner to update the current history data */
  95. void UpdateCurrentHistoryData();
  96. /** Handler for when a history item is chosen in the GetAvailableHistroyMenuItems list */
  97. void ExecuteJumpToHistory(int32 HistoryIndex);
  98. private:
  99. /** The delegate for when history data should be applied */
  100. FOnApplyHistoryData OnApplyHistoryData;
  101. /** The delegate for when history data should be updated */
  102. FOnUpdateHistoryData OnUpdateHistoryData;
  103. /** A list of history snapshots */
  104. TArray<FHistoryData> HistoryData;
  105. /** The current history index the user is at (changes when the user goes back,forward, or history snapshots are taken) */
  106. int32 CurrentHistoryIndex;
  107. /** Max number of history items that can be stored. Once the max is reached, the oldest history item is removed */
  108. int32 MaxHistoryEntries;
  109. };