ExtDependencyHistoryManager.cpp 5.6 KB

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