HistoryManager.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // Copyright 2017-2021 marynate. All Rights Reserved.
  2. #include "HistoryManager.h"
  3. #include "Textures/SlateIcon.h"
  4. #include "Framework/Commands/UIAction.h"
  5. #include "Framework/MultiBox/MultiBoxBuilder.h"
  6. #define LOCTEXT_NAMESPACE "ExtContentBrowser"
  7. FHistoryManager::FHistoryManager()
  8. {
  9. CurrentHistoryIndex = 0;
  10. MaxHistoryEntries = 300;
  11. }
  12. void FHistoryManager::SetOnApplyHistoryData(const FOnApplyHistoryData& InOnApplyHistoryData)
  13. {
  14. OnApplyHistoryData = InOnApplyHistoryData;
  15. }
  16. void FHistoryManager::SetOnUpdateHistoryData(const FOnUpdateHistoryData& InOnUpdateHistoryData)
  17. {
  18. OnUpdateHistoryData = InOnUpdateHistoryData;
  19. }
  20. bool FHistoryManager::GoBack()
  21. {
  22. if ( CanGoBack() )
  23. {
  24. // Update the current history data
  25. UpdateCurrentHistoryData();
  26. // if its possible to go back, decrement the index we are at
  27. --CurrentHistoryIndex;
  28. // Update the owner
  29. ApplyCurrentHistoryData();
  30. return true;
  31. }
  32. return false;
  33. }
  34. bool FHistoryManager::GoForward()
  35. {
  36. if ( CanGoForward() )
  37. {
  38. // Update the current history data
  39. UpdateCurrentHistoryData();
  40. // if its possible to go forward, increment the index we are at
  41. ++CurrentHistoryIndex;
  42. // Update the owner
  43. ApplyCurrentHistoryData();
  44. return true;
  45. }
  46. return false;
  47. }
  48. void FHistoryManager::AddHistoryData()
  49. {
  50. if (HistoryData.Num() == 0)
  51. {
  52. // History added to the beginning
  53. HistoryData.Add(FHistoryData());
  54. CurrentHistoryIndex = 0;
  55. }
  56. else if (CurrentHistoryIndex == HistoryData.Num() - 1)
  57. {
  58. // History added to the end
  59. if (HistoryData.Num() == MaxHistoryEntries)
  60. {
  61. // If max history entries has been reached
  62. // remove the oldest history
  63. HistoryData.RemoveAt(0);
  64. }
  65. HistoryData.Add(FHistoryData());
  66. // Current history index is the last index in the list
  67. CurrentHistoryIndex = HistoryData.Num() - 1;
  68. }
  69. else
  70. {
  71. // History added to the middle
  72. // clear out all history after the current history index.
  73. HistoryData.RemoveAt(CurrentHistoryIndex + 1, HistoryData.Num() - (CurrentHistoryIndex + 1));
  74. HistoryData.Add(FHistoryData());
  75. // Current history index is the last index in the list
  76. CurrentHistoryIndex = HistoryData.Num() - 1;
  77. }
  78. // Update the current history data
  79. UpdateCurrentHistoryData();
  80. }
  81. void FHistoryManager::UpdateHistoryData()
  82. {
  83. // Update the current history data
  84. UpdateCurrentHistoryData();
  85. }
  86. bool FHistoryManager::CanGoForward() const
  87. {
  88. // User can go forward if there are items in the history data list,
  89. // and the current history index isn't the last index in the list
  90. return HistoryData.Num() > 0 && CurrentHistoryIndex < HistoryData.Num() - 1;
  91. }
  92. bool FHistoryManager::CanGoBack() const
  93. {
  94. // User can go back if there are items in the history data list,
  95. // and the current history index isn't the first index in the list
  96. return HistoryData.Num() > 0 && CurrentHistoryIndex > 0;
  97. }
  98. FText FHistoryManager::GetBackDesc() const
  99. {
  100. if ( CanGoBack() )
  101. {
  102. return HistoryData[CurrentHistoryIndex - 1].HistoryDesc;
  103. }
  104. return FText::GetEmpty();
  105. }
  106. FText FHistoryManager::GetForwardDesc() const
  107. {
  108. if ( CanGoForward() )
  109. {
  110. return HistoryData[CurrentHistoryIndex + 1].HistoryDesc;
  111. }
  112. return FText::GetEmpty();
  113. }
  114. void FHistoryManager::GetAvailableHistoryMenuItems(bool bGetPrior, FMenuBuilder& MenuBuilder)
  115. {
  116. const FText HistoryHeadingString = (bGetPrior)? LOCTEXT("BackHistory", "Back History") : LOCTEXT("NextHistory", "Next History");
  117. MenuBuilder.BeginSection("HistoryBackNext", HistoryHeadingString);
  118. {
  119. if (HistoryData.Num() > 1)
  120. {
  121. // if there is at least 2 history items...
  122. // Start index is the first snapshot we should make a menu item out of
  123. int32 StartIndex = 0;
  124. // EndIndex is the last snapshot we should make a menu item out of
  125. int32 EndIndex = CurrentHistoryIndex;
  126. if (!bGetPrior)
  127. {
  128. // Need to return only items on or after the current history index
  129. StartIndex = CurrentHistoryIndex;
  130. EndIndex = HistoryData.Num() - 1;
  131. }
  132. // Check to make sure the start and end indices are within the bounds of the history list
  133. if (StartIndex < HistoryData.Num() && EndIndex != -1)
  134. {
  135. // Get all menu items between and including the start index and end index
  136. for (int32 HistoryIdx = StartIndex; HistoryIdx <= EndIndex; ++HistoryIdx)
  137. {
  138. MenuBuilder.AddMenuEntry(
  139. HistoryData[HistoryIdx].HistoryDesc,
  140. FText(),
  141. FSlateIcon(),
  142. FUIAction(
  143. FExecuteAction::CreateRaw( this, &FHistoryManager::ExecuteJumpToHistory, HistoryIdx )
  144. )
  145. );
  146. }
  147. }
  148. }
  149. }
  150. MenuBuilder.EndSection();
  151. }
  152. void FHistoryManager::ApplyCurrentHistoryData()
  153. {
  154. if ( CurrentHistoryIndex >= 0 && CurrentHistoryIndex < HistoryData.Num())
  155. {
  156. OnApplyHistoryData.ExecuteIfBound( HistoryData[CurrentHistoryIndex] );
  157. }
  158. }
  159. void FHistoryManager::UpdateCurrentHistoryData()
  160. {
  161. if ( CurrentHistoryIndex >= 0 && CurrentHistoryIndex < HistoryData.Num())
  162. {
  163. OnUpdateHistoryData.ExecuteIfBound( HistoryData[CurrentHistoryIndex] );
  164. }
  165. }
  166. void FHistoryManager::ExecuteJumpToHistory(int32 HistoryIndex)
  167. {
  168. if (HistoryIndex >= 0 && HistoryIndex < HistoryData.Num())
  169. {
  170. // if the history index is valid, set the current history index to the history index requested by the user
  171. CurrentHistoryIndex = HistoryIndex;
  172. // Update the owner
  173. ApplyCurrentHistoryData();
  174. }
  175. }
  176. #undef LOCTEXT_NAMESPACE