LUAEditorMainWindow.cpp 90 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzCore/PlatformIncl.h>
  9. #include "LUAEditorMainWindow.hxx"
  10. #include <AzCore/UserSettings/UserSettings.h>
  11. #include <AzCore/Debug/Trace.h>
  12. #include <AzCore/std/containers/map.h>
  13. #include <AzCore/std/delegate/delegate.h>
  14. #include <AzCore/Script/ScriptAsset.h>
  15. #include <AzCore/Component/TickBus.h>
  16. #include <AzCore/IO/Path/Path.h>
  17. #include <AzCore/Settings/SettingsRegistryMergeUtils.h>
  18. #include <AzCore/Utils/Utils.h>
  19. #include <AzFramework/Script/ScriptRemoteDebuggingConstants.h>
  20. #include <AzFramework/StringFunc/StringFunc.h>
  21. #include <AzToolsFramework/AssetBrowser/AssetBrowserBus.h>
  22. #include <AzToolsFramework/AssetBrowser/AssetBrowserModel.h>
  23. #include <AzToolsFramework/AssetBrowser/AssetBrowserFilterModel.h>
  24. #include <AzToolsFramework/AssetBrowser/AssetSelectionModel.h>
  25. #include <AzToolsFramework/AssetBrowser/Entries/SourceAssetBrowserEntry.h>
  26. #include <AzToolsFramework/AssetBrowser/Views/AssetBrowserTreeView.h>
  27. #include <AzToolsFramework/UI/UICore/ProgressShield.hxx>
  28. #include <AzToolsFramework/UI/UICore/SaveChangesDialog.hxx>
  29. #include <AzToolsFramework/UI/LegacyFramework/Core/EditorFrameworkAPI.h>
  30. #include <AzToolsFramework/UI/LegacyFramework/MainWindowSavedState.h>
  31. #include <AzToolsFramework/UI/UICore/TargetSelectorButton.hxx>
  32. #include <AzToolsFramework/UI/UICore/WidgetHelpers.h>
  33. #include <AzToolsFramework/UI/LegacyFramework/CustomMenus/CustomMenusAPI.h>
  34. #include <Source/LUA/TargetContextButton.hxx>
  35. #include <Source/LUA/LUAEditorDebuggerMessages.h>
  36. #include "DebugAttachmentButton.hxx"
  37. #include "ClassReferenceFilter.hxx"
  38. #include "WatchesPanel.hxx"
  39. #include "LUAEditorGoToLineDialog.hxx"
  40. #include "LUAEditorView.hxx"
  41. #include "LUAEditorContextMessages.h"
  42. #include "LUABreakpointTrackerMessages.h"
  43. #include "LUAEditorSettingsDialog.hxx"
  44. #include "RecentFiles.h"
  45. #include <Source/AssetDatabaseLocationListener.h>
  46. #include <Source/LUA/ui_LUAEditorMainWindow.h>
  47. #include <AzCore/Component/ComponentApplicationBus.h>
  48. #include <AzQtComponents/Components/FilteredSearchWidget.h>
  49. #include <AzQtComponents/Components/StyleManager.h>
  50. #include <QTimer>
  51. #include <QDesktopServices>
  52. #include <QLabel>
  53. #include <QDir>
  54. #include <QFileDialog>
  55. #include <QMessageBox>
  56. void initSharedResources()
  57. {
  58. Q_INIT_RESOURCE(sharedResources);
  59. }
  60. namespace
  61. {
  62. [[maybe_unused]] const char* LUAEditorDebugName = "LUA Debug";
  63. [[maybe_unused]] const char* LUAEditorInfoName = "LUA Editor";
  64. }
  65. namespace LUAEditor
  66. {
  67. extern AZ::Uuid ContextID;
  68. //////////////////////////////////////////////////////////////////////////
  69. //LUAEditorMainWindow
  70. LUAEditorMainWindow::LUAEditorMainWindow(QStandardItemModel* dataModel, bool connectedState, QWidget* parent, Qt::WindowFlags flags)
  71. : QMainWindow(parent, flags)
  72. , m_lastFocusedAssetId()
  73. , m_ptrFindDialog(nullptr)
  74. , m_settingsDialog(nullptr)
  75. , m_actionClearRecentFiles(nullptr)
  76. {
  77. initSharedResources();
  78. auto settingsRegistry = AZ::SettingsRegistry::Get();
  79. AZ::IO::FixedMaxPath engineRootPath;
  80. settingsRegistry->Get(engineRootPath.Native(), AZ::SettingsRegistryMergeUtils::FilePathKey_EngineRootFolder);
  81. AzQtComponents::StyleManager* m_styleSheet = new AzQtComponents::StyleManager(this);
  82. m_styleSheet->initialize(qApp, engineRootPath);
  83. LUAViewMessages::Bus::Handler::BusConnect();
  84. //m_currentTabContextMenuUUID = AZ::Uuid::CreateNull();
  85. m_gui = azcreate(Ui::LUAEditorMainWindow, ());
  86. m_gui->setupUi(this);
  87. setAcceptDrops(true);
  88. m_bIgnoreFocusRequests = false;
  89. m_bAutocompleteEnabled = true; // the default
  90. QMenu* theMenu = new QMenu(this);
  91. (void)theMenu->addAction(
  92. "Close Lua Editor App",
  93. this,
  94. SLOT(OnMenuCloseCurrentWindow()),
  95. QKeySequence("Alt+F4")
  96. );
  97. AzToolsFramework::FrameworkMessages::Bus::Broadcast(
  98. &AzToolsFramework::FrameworkMessages::Bus::Events::PopulateApplicationMenu, theMenu);
  99. menuBar()->insertMenu(m_gui->menuFile->menuAction(), theMenu);
  100. m_ptrFindDialog = aznew LUAEditorFindDialog(this);
  101. m_settingsDialog = aznew LUAEditorSettingsDialog(this);
  102. actionTabForwards = new QAction(tr("Next Document Tab"), this);
  103. actionTabBackwards = new QAction(tr("Previous Document Tab"), this);
  104. actionTabForwards->setShortcut(QKeySequence("Ctrl+Tab"));
  105. connect(actionTabForwards, SIGNAL(triggered(bool)), this, SLOT(OnTabForwards()));
  106. actionTabBackwards->setShortcut(QKeySequence("Ctrl+Shift+Tab"));
  107. connect(actionTabBackwards, SIGNAL(triggered(bool)), this, SLOT(OnTabBackwards()));
  108. m_gui->menuView->addAction(actionTabForwards);
  109. m_gui->menuView->addAction(actionTabBackwards);
  110. connect(m_gui->m_findResults1, &FindResults::ResultSelected, this, &LUAEditorMainWindow::OnFindResultClicked);
  111. connect(m_gui->m_findResults2, &FindResults::ResultSelected, this, &LUAEditorMainWindow::OnFindResultClicked);
  112. connect(m_gui->m_findResults3, &FindResults::ResultSelected, this, &LUAEditorMainWindow::OnFindResultClicked);
  113. connect(m_gui->m_findResults4, &FindResults::ResultSelected, this, &LUAEditorMainWindow::OnFindResultClicked);
  114. m_gui->findTabWidget->setCurrentIndex(0);
  115. connect(m_gui->actionCut, SIGNAL(triggered()), this, SLOT(OnEditMenuCut()));
  116. connect(m_gui->actionCopy, SIGNAL(triggered()), this, SLOT(OnEditMenuCopy()));
  117. connect(m_gui->actionSettings, SIGNAL(triggered()), this, SLOT(OnSettings()));
  118. connect(m_gui->actionLuaDocumentation, SIGNAL(triggered()), this, SLOT(OnLuaDocumentation()));
  119. m_gui->localsTreeView->SetOperatingMode(WATCHES_MODE_LOCALS);
  120. m_gui->m_logPanel->SetStorageID(AZ_CRC_CE("LUA Editor Log Panel"));
  121. connect(m_gui->m_logPanel, &AzToolsFramework::LogPanel::BaseLogPanel::TabsReset, this, &LUAEditorMainWindow::OnLogTabsReset);
  122. //m_lastProgramCounterAssetId = "";
  123. QMainWindow* pCentralWidget = new QMainWindow();
  124. setCentralWidget(pCentralWidget);
  125. pCentralWidget->setTabPosition(Qt::AllDockWidgetAreas, QTabWidget::North);
  126. pCentralWidget->setDockOptions(AllowNestedDocks | AllowTabbedDocks | AnimatedDocks);
  127. this->setTabPosition(Qt::AllDockWidgetAreas, QTabWidget::North);
  128. m_pTargetButton = aznew AzToolsFramework::TargetSelectorButtonAction(AzFramework::LuaToolsKey, this);
  129. m_gui->debugToolbar->addAction(m_pTargetButton);
  130. m_gui->menuDebug->addAction(m_pTargetButton);
  131. m_pContextButton = aznew LUA::TargetContextButtonAction(this);
  132. m_gui->debugToolbar->addAction(m_pContextButton);
  133. m_gui->menuDebug->addAction(m_pContextButton);
  134. m_pDebugAttachmentButton = aznew LUAEditor::DebugAttachmentButtonAction(this);
  135. m_gui->debugToolbar->addAction(m_pDebugAttachmentButton);
  136. m_gui->menuDebug->addAction(m_pDebugAttachmentButton);
  137. m_pDebugAttachmentButton->setEnabled(false);
  138. //turn these off by default
  139. m_settingsDialog->hide();
  140. m_ptrFindDialog->hide();
  141. m_gui->watchDockWidget->hide();
  142. m_gui->stackDockWidget->hide();
  143. m_gui->localsDockWidget->hide();
  144. m_gui->breakpointsDockWidget->hide();
  145. m_gui->findResultsDockWidget->hide();
  146. QTimer::singleShot(0, this, SLOT(RestoreWindowState()));
  147. m_gui->watchDockWidget->toggleViewAction()->setIcon(QIcon(":/general/watch_window"));
  148. m_gui->stackDockWidget->toggleViewAction()->setIcon(QIcon(":/general/callstack"));
  149. m_gui->localsDockWidget->toggleViewAction()->setIcon(QIcon(":/general/lua_locals"));
  150. m_gui->breakpointsDockWidget->toggleViewAction()->setIcon(QIcon(":/general/breakpoints"));
  151. m_gui->findResultsDockWidget->toggleViewAction()->setIcon(QIcon(":/general/find_results"));
  152. //construct the viewToolBar and menuView from toggle view actions
  153. m_gui->viewToolBar->addAction(m_gui->watchDockWidget->toggleViewAction());
  154. m_gui->viewToolBar->addAction(m_gui->breakpointsDockWidget->toggleViewAction());
  155. m_gui->viewToolBar->addAction(m_gui->stackDockWidget->toggleViewAction());
  156. m_gui->viewToolBar->addAction(m_gui->localsDockWidget->toggleViewAction());
  157. m_gui->viewToolBar->addAction(m_gui->findResultsDockWidget->toggleViewAction());
  158. m_gui->menuView->addAction(m_gui->watchDockWidget->toggleViewAction());
  159. m_gui->menuView->addAction(m_gui->breakpointsDockWidget->toggleViewAction());
  160. m_gui->menuView->addAction(m_gui->stackDockWidget->toggleViewAction());
  161. m_gui->menuView->addAction(m_gui->localsDockWidget->toggleViewAction());
  162. m_gui->menuView->addAction(m_gui->findResultsDockWidget->toggleViewAction());
  163. m_gui->menuView->addAction(m_gui->classReferenceDockWidget->toggleViewAction());
  164. m_gui->menuView->addAction(m_gui->m_dockLog->toggleViewAction());
  165. m_gui->menuView->addAction(m_gui->luaFilesDockWidget->toggleViewAction());
  166. LUAEditorMainWindowMessages::Handler::BusConnect();
  167. LUABreakpointTrackerMessages::Handler::BusConnect();
  168. Context_DebuggerManagement::Bus::Broadcast(&Context_DebuggerManagement::Bus::Events::CleanUpBreakpoints);
  169. SetDebugControlsToInitial();
  170. SetEditContolsToNoFilesOpen();
  171. // whats the current target anyway?
  172. // dataModel is the sole point of contact between our Context and its LUA debugger class information
  173. m_ClassReferenceFilter = aznew ClassReferenceFilterModel(this);
  174. m_ClassReferenceFilter->setSourceModel(dataModel);
  175. m_gui->classReferenceTreeView->setModel(m_ClassReferenceFilter);
  176. connect(m_gui->m_searchWidget, &AzQtComponents::FilteredSearchWidget::TextFilterChanged, this, &LUAEditorMainWindow::luaClassFilterTextChanged);
  177. connect(m_gui->actionOpen, SIGNAL(triggered(bool)), this, SLOT(OnFileMenuOpen()));
  178. UpdateOpenRecentMenu();
  179. connect(m_gui->actionAutocomplete, SIGNAL(triggered(bool)), this, SLOT(OnAutocompleteChanged(bool)));
  180. auto newState = AZ::UserSettings::CreateFind<LUAEditorMainWindowSavedState>(AZ_CRC_CE("LUA EDITOR MAIN WINDOW STATE"), AZ::UserSettings::CT_LOCAL);
  181. m_gui->actionAutoReloadUnmodifiedFiles->setChecked(newState->m_bAutoReloadUnmodifiedFiles);
  182. connect(m_gui->actionAutoReloadUnmodifiedFiles, &QAction::triggered, this, [](bool newValue)
  183. {
  184. auto newState = AZ::UserSettings::CreateFind<LUAEditorMainWindowSavedState>(AZ_CRC_CE("LUA EDITOR MAIN WINDOW STATE"), AZ::UserSettings::CT_LOCAL);
  185. newState->m_bAutoReloadUnmodifiedFiles = newValue;
  186. });
  187. connect(this, &LUAEditorMainWindow::OnReferenceDataChanged, this, [this]() {
  188. luaClassFilterTextChanged(m_ClassReferenceFilter->GetFilter());
  189. if (auto* view = GetCurrentView())
  190. {
  191. // Update syntax highlighting now that the project libraries are loaded
  192. view->UpdateFont();
  193. }
  194. });
  195. // preset our running state based on outside conditions when we are created
  196. if (connectedState)
  197. {
  198. OnConnectedToTarget();
  199. }
  200. else
  201. {
  202. OnDisconnectedFromTarget();
  203. }
  204. {
  205. // an attempt to make this all more readable!
  206. using namespace AzToolsFramework;
  207. typedef FrameworkMessages::Bus HotkeyBus;
  208. HotkeyBus::Broadcast(&HotkeyBus::Events::RegisterActionToHotkey, AZ_CRC_CE("LUALinesUpTranspose"), m_gui->actionLinesUpTranspose);
  209. HotkeyBus::Broadcast(&HotkeyBus::Events::RegisterActionToHotkey, AZ_CRC_CE("LUALinesDnTranspose"), m_gui->actionLinesDnTranspose);
  210. HotkeyBus::Broadcast(&HotkeyBus::Events::RegisterActionToHotkey, AZ_CRC_CE("GeneralOpenAssetBrowser"), m_gui->actionOpen);
  211. HotkeyBus::Broadcast(&HotkeyBus::Events::RegisterActionToHotkey, AZ_CRC_CE("LUAFind"), m_gui->actionFind);
  212. HotkeyBus::Broadcast(&HotkeyBus::Events::RegisterActionToHotkey, AZ_CRC_CE("LUAQuickFindLocal"), m_gui->actionFindLocal);
  213. HotkeyBus::Broadcast(&HotkeyBus::Events::RegisterActionToHotkey, AZ_CRC_CE("LUAQuickFindLocalReverse"), m_gui->actionFindLocalReverse);
  214. HotkeyBus::Broadcast(&HotkeyBus::Events::RegisterActionToHotkey, AZ_CRC_CE("LUAFindInFiles"), m_gui->actionFindInAllOpen);
  215. HotkeyBus::Broadcast(&HotkeyBus::Events::RegisterActionToHotkey, AZ_CRC_CE("LUAReplace"), m_gui->actionReplace);
  216. HotkeyBus::Broadcast(&HotkeyBus::Events::RegisterActionToHotkey, AZ_CRC_CE("LUAReplaceInFiles"), m_gui->actionReplaceInAllOpen);
  217. HotkeyBus::Broadcast(&HotkeyBus::Events::RegisterActionToHotkey, AZ_CRC_CE("LUAGoToLine"), m_gui->actionGoToLine);
  218. HotkeyBus::Broadcast(&HotkeyBus::Events::RegisterActionToHotkey, AZ_CRC_CE("LUAFold"), m_gui->actionFoldAll);
  219. HotkeyBus::Broadcast(&HotkeyBus::Events::RegisterActionToHotkey, AZ_CRC_CE("LUAUnfold"), m_gui->actionUnfoldAll);
  220. HotkeyBus::Broadcast(&HotkeyBus::Events::RegisterActionToHotkey, AZ_CRC_CE("LUACloseAllExceptCurrent"), m_gui->actionCloseAllExcept);
  221. HotkeyBus::Broadcast(&HotkeyBus::Events::RegisterActionToHotkey, AZ_CRC_CE("LUACloseAll"), m_gui->actionCloseAll);
  222. HotkeyBus::Broadcast(&HotkeyBus::Events::RegisterActionToHotkey, AZ_CRC_CE("LUAComment"), m_gui->actionComment_Selected_Block);
  223. HotkeyBus::Broadcast(&HotkeyBus::Events::RegisterActionToHotkey, AZ_CRC_CE("LUAUncomment"), m_gui->actionUnComment_Selected_Block);
  224. HotkeyBus::Broadcast(&HotkeyBus::Events::RegisterActionToHotkey, AZ_CRC_CE("LUAResetZoom"), m_gui->actionResetZoom);
  225. }
  226. //m_StoredTabAssetId = AZ::Uuid::CreateNull();
  227. installEventFilter(this);
  228. LegacyFramework::CustomMenusMessages::Bus::Broadcast(&LegacyFramework::CustomMenusMessages::Bus::Events::RegisterMenu, LegacyFramework::CustomMenusCommon::LUAEditor::Application, theMenu);
  229. LegacyFramework::CustomMenusMessages::Bus::Broadcast(&LegacyFramework::CustomMenusMessages::Bus::Events::RegisterMenu, LegacyFramework::CustomMenusCommon::LUAEditor::File, m_gui->menuFile);
  230. LegacyFramework::CustomMenusMessages::Bus::Broadcast(&LegacyFramework::CustomMenusMessages::Bus::Events::RegisterMenu, LegacyFramework::CustomMenusCommon::LUAEditor::Edit, m_gui->menuEdit);
  231. LegacyFramework::CustomMenusMessages::Bus::Broadcast(&LegacyFramework::CustomMenusMessages::Bus::Events::RegisterMenu, LegacyFramework::CustomMenusCommon::LUAEditor::View, m_gui->menuView);
  232. LegacyFramework::CustomMenusMessages::Bus::Broadcast(&LegacyFramework::CustomMenusMessages::Bus::Events::RegisterMenu, LegacyFramework::CustomMenusCommon::LUAEditor::Debug, m_gui->menuDebug);
  233. LegacyFramework::CustomMenusMessages::Bus::Broadcast(&LegacyFramework::CustomMenusMessages::Bus::Events::RegisterMenu, LegacyFramework::CustomMenusCommon::LUAEditor::SourceControl, m_gui->menuSource_Control);
  234. LegacyFramework::CustomMenusMessages::Bus::Broadcast(&LegacyFramework::CustomMenusMessages::Bus::Events::RegisterMenu, LegacyFramework::CustomMenusCommon::LUAEditor::Options, m_gui->menu_Options);
  235. QObject::connect(m_gui->menu_Options, &QMenu::aboutToShow, this, &LUAEditorMainWindow::OnOptionsMenuRequested);
  236. connect(m_gui->m_logPanel, &AzToolsFramework::LogPanel::TracePrintFLogPanel::LogLineSelected, this, &LUAEditorMainWindow::LogLineSelectionChanged);
  237. }
  238. void LUAEditorMainWindow::OnOptionsMenuRequested()
  239. {
  240. m_gui->actionAutocomplete->blockSignals(true);
  241. m_gui->actionAutocomplete->setCheckable(true);
  242. m_gui->actionAutocomplete->setChecked(m_bAutocompleteEnabled);
  243. m_gui->actionAutocomplete->blockSignals(false);
  244. }
  245. void LUAEditorMainWindow::UpdateOpenRecentMenu()
  246. {
  247. const QStringList recentFiles = ReadRecentFiles();
  248. QList<QAction*> actions = m_gui->menuOpenRecent->actions();
  249. for (int i = actions.size() - 1; i >= 0; i--)
  250. {
  251. m_gui->menuOpenRecent->removeAction(actions[i]);
  252. }
  253. for (auto& fileName : recentFiles)
  254. {
  255. QAction* action = new QAction(fileName, this);
  256. connect(
  257. action,
  258. &QAction::triggered,
  259. this,
  260. [fileName]([[maybe_unused]] bool checked)
  261. {
  262. constexpr bool errorOnNotFound = true;
  263. Context_DocumentManagement::Bus::Broadcast(
  264. &Context_DocumentManagement::Bus::Events::OnLoadDocument, fileName.toStdString().c_str(), errorOnNotFound);
  265. });
  266. m_gui->menuOpenRecent->addAction(action);
  267. }
  268. m_gui->menuOpenRecent->addSeparator();
  269. m_actionClearRecentFiles = new QAction("Clear Recent Files", this);
  270. connect(
  271. m_actionClearRecentFiles,
  272. &QAction::triggered,
  273. this,
  274. [this]([[maybe_unused]] bool checked)
  275. {
  276. ClearRecentFile();
  277. UpdateOpenRecentMenu();
  278. });
  279. m_gui->menuOpenRecent->addAction(m_actionClearRecentFiles);
  280. m_gui->menuOpenRecent->setEnabled(!recentFiles.isEmpty());
  281. m_actionClearRecentFiles->setEnabled(!recentFiles.isEmpty());
  282. }
  283. LUAEditorMainWindow::~LUAEditorMainWindow(void)
  284. {
  285. removeEventFilter(this);
  286. LUAViewMessages::Bus::Handler::BusDisconnect();
  287. //ClientInterface::Handler::BusDisconnect();
  288. LUAEditorMainWindowMessages::Handler::BusDisconnect();
  289. LUABreakpointTrackerMessages::Handler::BusDisconnect();
  290. azdestroy(m_gui);
  291. delete m_assetDatabaseListener;
  292. m_assetDatabaseListener = nullptr;
  293. }
  294. void LUAEditorMainWindow::SetupLuaFilesPanel()
  295. {
  296. if (m_assetDatabaseListener != nullptr)
  297. {
  298. // We have already setup the panel, so nothing to do
  299. return;
  300. }
  301. AZStd::string cacheRoot;
  302. bool cacheRootFound = false;
  303. AzToolsFramework::AssetSystemRequestBus::BroadcastResult(cacheRootFound, &AzToolsFramework::AssetSystemRequestBus::Events::GetAbsoluteAssetDatabaseLocation, cacheRoot);
  304. if (!cacheRootFound)
  305. {
  306. return;
  307. }
  308. m_assetDatabaseListener = new AssetDatabaseLocationListener();
  309. m_assetDatabaseListener->Init(cacheRoot.c_str());
  310. AZ::Data::AssetCatalogRequestBus::Broadcast(&AZ::Data::AssetCatalogRequests::StartMonitoringAssets);
  311. // Get the asset browser model
  312. AzToolsFramework::AssetBrowser::AssetBrowserModel* assetBrowserModel = nullptr;
  313. AzToolsFramework::AssetBrowser::AssetBrowserComponentRequestBus::BroadcastResult(assetBrowserModel, &AzToolsFramework::AssetBrowser::AssetBrowserComponentRequests::GetAssetBrowserModel);
  314. AZ_Assert(assetBrowserModel, "Failed to get filebrowser model");
  315. // Hook up the data set to the tree view
  316. m_filterModel = aznew AzToolsFramework::AssetBrowser::AssetBrowserFilterModel(this);
  317. m_filterModel->setSourceModel(assetBrowserModel);
  318. // Delay the setting of the filter until everything can be initialized
  319. QTimer::singleShot(1000, [this]()
  320. {
  321. m_filterModel->SetFilter(CreateFilter());
  322. });
  323. m_gui->m_assetBrowserTreeView->setModel(m_filterModel);
  324. m_gui->m_assetBrowserTreeView->SetShowSourceControlIcons(true);
  325. m_gui->m_assetBrowserTreeView->setSelectionMode(QAbstractItemView::SelectionMode::SingleSelection);
  326. // Maintains the tree expansion state between runs
  327. m_gui->m_assetBrowserTreeView->SetName("LuaIDETreeView");
  328. connect(m_gui->m_assetBrowserTreeView, &QTreeView::doubleClicked, this, [this](const QModelIndex&)
  329. {
  330. auto selectedAssets = m_gui->m_assetBrowserTreeView->GetSelectedAssets();
  331. if (selectedAssets.size() == 1)
  332. {
  333. auto selectedAsset = selectedAssets.front();
  334. const AZStd::string filePath = selectedAsset->GetFullPath();
  335. auto entryType = selectedAsset->GetEntryType();
  336. if (entryType == AzToolsFramework::AssetBrowser::AssetBrowserEntry::AssetEntryType::Source)
  337. {
  338. Context_DocumentManagement::Bus::Broadcast(&Context_DocumentManagement::Bus::Events::OnLoadDocument, filePath, true);
  339. }
  340. }
  341. });
  342. }
  343. QSharedPointer<AzToolsFramework::AssetBrowser::CompositeFilter> LUAEditorMainWindow::CreateFilter()
  344. {
  345. using namespace AzToolsFramework::AssetBrowser;
  346. // Only look at Script Assets (.lua files)
  347. // Propagate this down to cover all the parents of a script asset
  348. AssetTypeFilter* assetFilter = new AssetTypeFilter();
  349. assetFilter->SetAssetType(AZ::AzTypeInfo<AZ::ScriptAsset>::Uuid());
  350. assetFilter->SetFilterPropagation(AssetTypeFilter::PropagateDirection::Down);
  351. // We only care about sources (not products)
  352. // Do not propagate this at all
  353. EntryTypeFilter* entryTypeFilter = new EntryTypeFilter();
  354. entryTypeFilter->SetEntryType(AssetBrowserEntry::AssetEntryType::Source);
  355. entryTypeFilter->SetFilterPropagation(AssetTypeFilter::PropagateDirection::None);
  356. // Add in a string filter that comes from user input
  357. StringFilter* stringFilter = new StringFilter();
  358. stringFilter->SetFilterPropagation(AssetTypeFilter::PropagateDirection::Up);
  359. connect(m_gui->m_assetBrowserSearchWidget, &AzQtComponents::FilteredSearchWidget::TextFilterChanged, this, [&, stringFilter](const QString& newString)
  360. {
  361. stringFilter->SetFilterString(newString);
  362. if (newString.isEmpty())
  363. {
  364. m_gui->m_assetBrowserTreeView->collapseAll();
  365. }
  366. else
  367. {
  368. m_gui->m_assetBrowserTreeView->expandAll();
  369. }
  370. });
  371. // Construct the final filter where they are all and'd together
  372. // Propagate the final filter down so that any matches will show the hierarchy of folders down to the appropriate matching leaf node
  373. QSharedPointer<CompositeFilter> finalFilter(new CompositeFilter(CompositeFilter::LogicOperatorType::AND));
  374. finalFilter->AddFilter(FilterConstType(stringFilter));
  375. finalFilter->AddFilter(FilterConstType(assetFilter));
  376. finalFilter->AddFilter(FilterConstType(entryTypeFilter));
  377. finalFilter->SetFilterPropagation(AssetTypeFilter::PropagateDirection::Down);
  378. return finalFilter;
  379. }
  380. void LUAEditorMainWindow::OnSettings()
  381. {
  382. m_settingsDialog->show();
  383. }
  384. void LUAEditorMainWindow::OnLuaDocumentation()
  385. {
  386. QDesktopServices::openUrl(QUrl("https://o3de.org/docs/user-guide/scripting/lua/"));
  387. }
  388. void LUAEditorMainWindow::OnMenuCloseCurrentWindow()
  389. {
  390. AzToolsFramework::FrameworkMessages::Bus::Broadcast(
  391. &AzToolsFramework::FrameworkMessages::Bus::Events::RequestMainWindowClose, ContextID);
  392. }
  393. void LUAEditorMainWindow::OnAutocompleteChanged(bool change)
  394. {
  395. m_bAutocompleteEnabled = change;
  396. m_gui->actionAutocomplete->blockSignals(true);
  397. m_gui->actionAutocomplete->setCheckable(true);
  398. m_gui->actionAutocomplete->setChecked(m_bAutocompleteEnabled);
  399. m_gui->actionAutocomplete->blockSignals(false);
  400. for (TrackedLUAViewMap::iterator viewInfoIter = m_dOpenLUAView.begin(); viewInfoIter != m_dOpenLUAView.end(); ++viewInfoIter)
  401. {
  402. TrackedLUAView& viewInfo = viewInfoIter->second;
  403. viewInfo.luaViewWidget()->SetAutoCompletionEnabled(m_bAutocompleteEnabled);
  404. }
  405. //AZ_TracePrintf("LUA","change %d",change);
  406. }
  407. //////////////////////////////////////////////////////////////////////////
  408. void LUAEditorMainWindow::OnOpenLUAView(const DocumentInfo& docInfo)
  409. {
  410. //char output[64];
  411. //docInfo.m_assetId.ToString(output,AZ_ARRAY_SIZE(output),true,true);
  412. //AZ_TracePrintf(LUAEditorDebugName, AZStd::string::format("OnOpenLuaView %s = %s\n", (docInfo.m_assetName + ".lua").c_str(), output).c_str());
  413. show();
  414. //set focus if already created
  415. TrackedLUAViewMap::iterator viewIter = m_dOpenLUAView.find(docInfo.m_assetId);
  416. if (viewIter != m_dOpenLUAView.end())
  417. {
  418. viewIter->second.luaDockWidget()->show();
  419. viewIter->second.luaDockWidget()->raise();
  420. viewIter->second.luaViewWidget()->setFocus();
  421. return;
  422. }
  423. setAnimated(false);
  424. //make a new one
  425. LUADockWidget* luaDockWidget = aznew LUADockWidget(this->centralWidget());
  426. luaDockWidget->setFeatures(QDockWidget::DockWidgetClosable | QDockWidget::DockWidgetMovable);//do not add floatable!
  427. luaDockWidget->setAssetId(docInfo.m_assetId);
  428. QWidget* luaLayout = new QWidget();
  429. luaLayout->setLayout(aznew LUAEditorMainWindowLayout(luaLayout));
  430. luaLayout->layout()->setContentsMargins(0, 0, 0, 0);
  431. LUAViewWidget* luaViewWidget = aznew LUAViewWidget();
  432. luaViewWidget->SetLuaDockWidget(luaDockWidget);
  433. luaDockWidget->setObjectName(QString::fromUtf8(docInfo.m_displayName.c_str()));
  434. luaViewWidget->setObjectName(QString::fromUtf8(docInfo.m_displayName.c_str()));
  435. luaDockWidget->setWidget(luaLayout);
  436. luaViewWidget->Initialize(docInfo);
  437. luaViewWidget->installEventFilter(this);
  438. m_ptrPerforceStatusWidget = new QLabel(tr("Pending Status"), this);
  439. m_ptrPerforceStatusWidget->setMargin(2);
  440. m_ptrPerforceStatusWidget->setStyleSheet(QString("background: rgba(192,192,192,255); color: black; border-style: inset;\nborder-width: 1px;\nborder-color: rgba(100,100,100,255);\nborder-radius: 8px;"));
  441. m_ptrPerforceStatusWidget->setAutoFillBackground(true);
  442. m_ptrPerforceStatusWidget->setTextInteractionFlags(Qt::NoTextInteraction);
  443. m_ptrPerforceStatusWidget->setAttribute(Qt::WA_TransparentForMouseEvents);
  444. connect(luaViewWidget, SIGNAL(sourceControlStatusUpdated(QString)), m_ptrPerforceStatusWidget, SLOT(setText(QString)));
  445. luaLayout->layout()->addWidget(luaViewWidget);
  446. luaLayout->layout()->addWidget(m_ptrPerforceStatusWidget);
  447. //if we already have an open view tabbify, if not just add it to the side
  448. if (m_dOpenLUAView.size() && !m_lastFocusedAssetId.empty())
  449. {
  450. viewIter = m_dOpenLUAView.find(m_lastFocusedAssetId);
  451. if (viewIter != m_dOpenLUAView.end())
  452. {
  453. qobject_cast<QMainWindow*>(this->centralWidget())->tabifyDockWidget(viewIter->second.luaDockWidget(), luaDockWidget);
  454. }
  455. else
  456. {
  457. qobject_cast<QMainWindow*>(this->centralWidget())->addDockWidget(static_cast<Qt::DockWidgetArea>(0x4), luaDockWidget, Qt::Horizontal);
  458. }
  459. }
  460. else
  461. {
  462. qobject_cast<QMainWindow*>(this->centralWidget())->addDockWidget(static_cast<Qt::DockWidgetArea>(0x4), luaDockWidget, Qt::Horizontal);
  463. }
  464. //track it
  465. if (m_lastFocusedAssetId.empty())
  466. {
  467. m_lastFocusedAssetId = docInfo.m_assetId;
  468. }
  469. m_dOpenLUAView.insert(AZStd::make_pair(docInfo.m_assetId, TrackedLUAView(luaDockWidget, luaViewWidget, docInfo.m_assetId)));
  470. m_CtrlTabOrder.push_front(docInfo.m_assetId);
  471. //if (m_dOpenLUAView.size() == 1)
  472. {
  473. QApplication::processEvents();
  474. }
  475. AZ::SystemTickBus::QueueFunction(&LUAEditorMainWindow::OnDockWidgetLocationChanged, this, docInfo.m_assetId);
  476. luaDockWidget->show();
  477. luaDockWidget->raise();
  478. luaViewWidget->setFocus();
  479. m_ptrPerforceStatusWidget->raise();
  480. connect(luaDockWidget, SIGNAL(visibilityChanged(bool)), luaViewWidget, SLOT(OnVisibilityChanged(bool)));
  481. SetEditContolsToAtLeastOneFileOpen();
  482. setAnimated(true);
  483. };
  484. void LUAEditorMainWindow::OnDockWidgetLocationChanged(const AZStd::string assetId)
  485. {
  486. // find the widget:
  487. TrackedLUAViewMap::iterator viewInfoIter = m_dOpenLUAView.find(assetId);
  488. if (viewInfoIter == m_dOpenLUAView.end())
  489. {
  490. return;
  491. }
  492. QTabBar* pBar = nullptr;
  493. // We need to find out what we are docked to in order for this to work correctly.
  494. // this function was added in, and I can't find anything similar that will let me do the same thing.
  495. // Without this, we lose the ability to have the right click -> close all but this.
  496. //QTabBar *pBar = qobject_cast<QMainWindow*>(this->centralWidget())->tabBar(viewInfoIter->second.luaDockWidget());
  497. if (!pBar)
  498. {
  499. return;
  500. }
  501. // have we ever done this already?
  502. if (pBar->documentMode() == false)
  503. {
  504. pBar->setDocumentMode(true);
  505. pBar->setElideMode(Qt::ElideNone); // do not elide!
  506. pBar->setTabsClosable(true);
  507. connect(pBar, &QTabBar::tabCloseRequested, this,
  508. [assetId, this]()
  509. {
  510. AZ::SystemTickBus::QueueFunction(&LUAEditorMainWindow::RequestCloseDocument, this, assetId);
  511. });
  512. pBar->setContextMenuPolicy(Qt::CustomContextMenu);
  513. connect(pBar,
  514. &QWidget::customContextMenuRequested,
  515. this,
  516. [assetId, this](const QPoint& point)
  517. {
  518. this->showTabContextMenu(assetId, point);
  519. }
  520. );
  521. }
  522. }
  523. void LUAEditorMainWindow::showTabContextMenu(const AZStd::string& assetId, const QPoint& pos)
  524. {
  525. QTabBar* emitter = qobject_cast<QTabBar*>(sender());
  526. if (!emitter)
  527. {
  528. return;
  529. }
  530. int tabIdx = emitter->tabAt(pos);
  531. if (tabIdx < 0)
  532. {
  533. return;
  534. }
  535. m_currentTabContextMenuUUID = assetId;
  536. if (m_currentTabContextMenuUUID.empty())
  537. {
  538. return;
  539. }
  540. QMenu menu(this);
  541. menu.addAction("Close All Except This", this, SLOT(closeAllTabsExceptThisTabContextMenu()));
  542. menu.exec(emitter->mapToGlobal(pos));
  543. }
  544. void LUAEditorMainWindow::closeAllTabsExceptThisTabContextMenu()
  545. {
  546. if (m_currentTabContextMenuUUID.empty())
  547. {
  548. return;
  549. }
  550. for (TrackedLUAViewMap::iterator it = m_dOpenLUAView.begin(); it != m_dOpenLUAView.end(); ++it)
  551. {
  552. if (it->first != m_currentTabContextMenuUUID)
  553. {
  554. AZ::SystemTickBus::QueueFunction(&LUAEditorMainWindow::RequestCloseDocument, this, it->first);
  555. }
  556. }
  557. m_currentTabContextMenuUUID = "";
  558. }
  559. void LUAEditorMainWindow::OnOpenWatchView()
  560. {
  561. show();
  562. m_gui->watchDockWidget->show();
  563. m_gui->watchDockWidget->setFocus();
  564. }
  565. void LUAEditorMainWindow::OnOpenReferenceView()
  566. {
  567. show();
  568. m_gui->classReferenceDockWidget->show();
  569. m_gui->classReferenceDockWidget->setFocus();
  570. }
  571. void LUAEditorMainWindow::OnOpenBreakpointsView()
  572. {
  573. show();
  574. m_gui->breakpointsDockWidget->show();
  575. m_gui->breakpointsDockWidget->raise();
  576. m_gui->breakpointsDockWidget->setFocus();
  577. }
  578. void LUAEditorMainWindow::OnOpenStackView()
  579. {
  580. show();
  581. m_gui->stackDockWidget->show();
  582. m_gui->stackDockWidget->raise();
  583. m_gui->stackDockWidget->setFocus();
  584. }
  585. void LUAEditorMainWindow::OnOpenLocalsView()
  586. {
  587. show();
  588. m_gui->localsDockWidget->show();
  589. m_gui->localsDockWidget->raise();
  590. m_gui->localsDockWidget->setFocus();
  591. }
  592. void LUAEditorMainWindow::OnOpenFindView(int index)
  593. {
  594. show();
  595. m_gui->findResultsDockWidget->show();
  596. m_gui->findResultsDockWidget->raise();
  597. m_gui->findResultsDockWidget->setFocus();
  598. m_gui->findTabWidget->setCurrentIndex(index);
  599. }
  600. void LUAEditorMainWindow::ResetSearchClicks()
  601. {
  602. m_dProcessFindListClicked.clear();
  603. }
  604. void LUAEditorMainWindow::MoveProgramCursor(const AZStd::string& assetId, int lineNumber)
  605. {
  606. if ((m_lastProgramCounterAssetId != assetId) && (!m_lastProgramCounterAssetId.empty()))
  607. {
  608. // the program counter has moved from one document to another.
  609. // remove it from the old one.
  610. TrackedLUAViewMap::iterator viewInfoIter = m_dOpenLUAView.find(m_lastProgramCounterAssetId);
  611. if (viewInfoIter != m_dOpenLUAView.end())
  612. {
  613. TrackedLUAView& viewInfo = viewInfoIter->second;
  614. LUAViewWidget* pTextWidget = viewInfo.luaViewWidget();
  615. pTextWidget->UpdateCurrentExecutingLine(-1);
  616. }
  617. }
  618. m_lastProgramCounterAssetId = "";
  619. // now add it to the new one:
  620. {
  621. TrackedLUAViewMap::iterator viewInfoIter = m_dOpenLUAView.find(assetId);
  622. if (viewInfoIter != m_dOpenLUAView.end())
  623. {
  624. TrackedLUAView& viewInfo = viewInfoIter->second;
  625. LUAViewWidget* pTextWidget = viewInfo.luaViewWidget();
  626. pTextWidget->UpdateCurrentExecutingLine(lineNumber);
  627. m_lastProgramCounterAssetId = assetId;
  628. }
  629. }
  630. if (lineNumber == -1)
  631. {
  632. m_lastProgramCounterAssetId = "";
  633. }
  634. }
  635. void LUAEditorMainWindow::MoveEditCursor(const AZStd::string& assetId, int lineNumber, bool withSelection)
  636. {
  637. TrackedLUAViewMap::iterator viewInfoIter = m_dOpenLUAView.find(assetId);
  638. if (viewInfoIter != m_dOpenLUAView.end())
  639. {
  640. TrackedLUAView& viewInfo = viewInfoIter->second;
  641. LUAViewWidget* pTextWidget = viewInfo.luaViewWidget();
  642. pTextWidget->UpdateCurrentEditingLine(lineNumber);
  643. if (withSelection)
  644. {
  645. pTextWidget->SetSelection(lineNumber, 0, lineNumber + 1, 0);
  646. }
  647. }
  648. }
  649. // debug menu items
  650. void LUAEditorMainWindow::ExecuteScript(bool executeLocally)
  651. {
  652. if (m_lastFocusedAssetId.empty())
  653. {
  654. return;
  655. }
  656. if (SyncDocumentToContext(m_lastFocusedAssetId))
  657. {
  658. Context_DebuggerManagement::Bus::Broadcast(
  659. &Context_DebuggerManagement::Bus::Events::ExecuteScriptBlob, m_lastFocusedAssetId, executeLocally);
  660. }
  661. }
  662. void LUAEditorMainWindow::OnDebugExecute()
  663. {
  664. if (m_lastFocusedAssetId.empty())
  665. {
  666. return;
  667. }
  668. LUAViewWidget* view = GetCurrentView();
  669. if (view)
  670. {
  671. view->UpdateCurrentExecutingLine(-1);
  672. }
  673. TrackedLUAViewMap::iterator viewInfoIter = m_dOpenLUAView.find(m_lastFocusedAssetId);
  674. if (viewInfoIter != m_dOpenLUAView.end())
  675. {
  676. ExecuteScript(true);
  677. }
  678. }
  679. void LUAEditorMainWindow::OnDebugExecuteOnTarget()
  680. {
  681. if (m_lastFocusedAssetId.empty())
  682. {
  683. return;
  684. }
  685. LUAViewWidget* view = GetCurrentView();
  686. if (view)
  687. {
  688. view->UpdateCurrentExecutingLine(-1);
  689. }
  690. }
  691. // execution control
  692. void LUAEditorMainWindow::OnDebugToggleBreakpoint()
  693. {
  694. // current view
  695. LUAViewWidget* view = GetCurrentView();
  696. if (view)
  697. {
  698. // current line
  699. int line, index;
  700. view->GetCursorPosition(line, index);
  701. view->BreakpointToggle(line);
  702. }
  703. }
  704. void LUAEditorMainWindow::OnDebugContinueRunning()
  705. {
  706. LUAEditorDebuggerMessages::Bus::Broadcast(&LUAEditorDebuggerMessages::Bus::Events::DebugRunContinue);
  707. }
  708. void LUAEditorMainWindow::OnDebugStepOver()
  709. {
  710. LUAEditorDebuggerMessages::Bus::Broadcast(&LUAEditorDebuggerMessages::Bus::Events::DebugRunStepOver);
  711. }
  712. void LUAEditorMainWindow::OnDebugStepIn()
  713. {
  714. LUAEditorDebuggerMessages::Bus::Broadcast(&LUAEditorDebuggerMessages::Bus::Events::DebugRunStepIn);
  715. }
  716. void LUAEditorMainWindow::OnDebugStepOut()
  717. {
  718. LUAEditorDebuggerMessages::Bus::Broadcast(&LUAEditorDebuggerMessages::Bus::Events::DebugRunStepOut);
  719. }
  720. //file menu
  721. void LUAEditorMainWindow::OnFileMenuOpen()
  722. {
  723. AssetSelectionModel selection;
  724. StringFilter* stringFilter = new StringFilter();
  725. stringFilter->SetName("Lua file (*.lua)");
  726. stringFilter->SetFilterString(".lua");
  727. stringFilter->SetFilterPropagation(AssetBrowserEntryFilter::PropagateDirection::Down);
  728. auto stringFilterPtr = FilterConstType(stringFilter);
  729. selection.SetDisplayFilter(stringFilterPtr);
  730. selection.SetSelectionFilter(stringFilterPtr);
  731. AssetBrowserComponentRequestBus::Broadcast(
  732. &AssetBrowserComponentRequests::PickAssets, selection, AzToolsFramework::GetActiveWindow());
  733. if (!selection.IsValid())
  734. {
  735. return;
  736. }
  737. auto* result = selection.GetResult();
  738. if (!result)
  739. {
  740. AZ_Assert(false, "Lua script - Incorrect entry type selected during script instantiation.");
  741. return;
  742. }
  743. const AZStd::string assetId(result->GetFullPath().data());
  744. Context_DocumentManagement::Bus::Broadcast(&Context_DocumentManagement::Bus::Events::OnLoadDocument, assetId, true);
  745. AzFramework::StringFunc::Path::Split(assetId.c_str(), nullptr, &m_lastOpenFilePath);
  746. AddRecentFile(result->GetFullPath().c_str());
  747. UpdateOpenRecentMenu();
  748. }
  749. void LUAEditorMainWindow::OnFileMenuNew()
  750. {
  751. AZStd::string assetId;
  752. if (!OnFileSaveDialog("", assetId))
  753. {
  754. return;
  755. }
  756. if (AzFramework::StringFunc::Find(assetId.c_str(), ".lua") == AZStd::string::npos)
  757. {
  758. assetId += ".lua";
  759. }
  760. Context_DocumentManagement::Bus::Broadcast(&Context_DocumentManagement::Bus::Events::OnNewDocument, assetId);
  761. SetEditContolsToAtLeastOneFileOpen();
  762. }
  763. bool LUAEditorMainWindow::SyncDocumentToContext(const AZStd::string& assetId)
  764. {
  765. if (assetId.empty())
  766. {
  767. return false;
  768. }
  769. TrackedLUAViewMap::iterator viewInfoIter = m_dOpenLUAView.find(assetId);
  770. AZ_Assert(viewInfoIter != m_dOpenLUAView.end(), "OnFileMenuClose() : Cant find view Info.");
  771. TrackedLUAView& viewInfo = viewInfoIter->second;
  772. QByteArray viewBuffer = viewInfo.luaViewWidget()->GetText().toUtf8();
  773. AZStd::size_t viewSize = viewBuffer.size();
  774. Context_DocumentManagement::Bus::Broadcast(
  775. &Context_DocumentManagement::Bus::Events::UpdateDocumentData, assetId, viewBuffer.data(), viewSize);
  776. return true;
  777. }
  778. void LUAEditorMainWindow::OnFileMenuSave()
  779. {
  780. if (m_lastFocusedAssetId.empty())
  781. {
  782. return;
  783. }
  784. TrackedLUAViewMap::iterator viewInfoIter = m_dOpenLUAView.find(m_lastFocusedAssetId);
  785. AZ_Assert(viewInfoIter != m_dOpenLUAView.end(), "OnFileMenuSave() : Cant find view Info.");
  786. TrackedLUAView& viewInfo = viewInfoIter->second;
  787. //has the views text changed?
  788. if (viewInfo.luaViewWidget()->IsReadOnly())
  789. {
  790. AZ_Warning("LUA Editor", false, "Cannot save document - it is read-only (Check out first)");
  791. return;
  792. }
  793. if (SyncDocumentToContext(m_lastFocusedAssetId))
  794. {
  795. Context_DocumentManagement::Bus::Broadcast(
  796. &Context_DocumentManagement::Bus::Events::OnSaveDocument, m_lastFocusedAssetId, false, false);
  797. }
  798. }
  799. void LUAEditorMainWindow::OnFileMenuSaveAs()
  800. {
  801. if (m_lastFocusedAssetId.empty())
  802. {
  803. return;
  804. }
  805. if (SyncDocumentToContext(m_lastFocusedAssetId))
  806. {
  807. bool saveSuccess = false;
  808. Context_DocumentManagement::Bus::BroadcastResult(
  809. saveSuccess, &Context_DocumentManagement::Bus::Events::OnSaveDocumentAs, m_lastFocusedAssetId, false);
  810. }
  811. }
  812. void LUAEditorMainWindow::OnFileMenuSaveAll()
  813. {
  814. for (TrackedLUAViewMap::iterator viewInfoIter = m_dOpenLUAView.begin(); viewInfoIter != m_dOpenLUAView.end(); ++viewInfoIter)
  815. {
  816. TrackedLUAView& viewInfo = viewInfoIter->second;
  817. //has the views text changed?
  818. if (viewInfo.luaViewWidget()->IsReadOnly())
  819. {
  820. continue;
  821. }
  822. if (SyncDocumentToContext(viewInfo.luaViewWidget()->m_Info.m_assetId))
  823. {
  824. Context_DocumentManagement::Bus::Broadcast(
  825. &Context_DocumentManagement::Bus::Events::OnSaveDocument, viewInfo.luaViewWidget()->m_Info.m_assetId, false, false);
  826. }
  827. }
  828. }
  829. void LUAEditorMainWindow::OnFileMenuReload()
  830. {
  831. if (m_lastFocusedAssetId.empty())
  832. {
  833. return;
  834. }
  835. auto currentView = GetCurrentView();
  836. if (!currentView)
  837. {
  838. return;
  839. }
  840. // if this view has been modified, prompt to be sure they want to lose the changes
  841. TrackedLUAViewMap::iterator viewInfoIter = m_dOpenLUAView.find(m_lastFocusedAssetId);
  842. if (viewInfoIter == m_dOpenLUAView.end())
  843. {
  844. return; // no such view, dafuq?
  845. }
  846. TrackedLUAView& viewInfo = viewInfoIter->second;
  847. //has the views text changed?
  848. if (!viewInfo.luaViewWidget()->IsReadOnly() &&
  849. viewInfo.luaViewWidget()->IsModified())
  850. {
  851. QMessageBox msgBox;
  852. msgBox.setText("This file has been modified.\nDo you really want to Reload and lose changes?");
  853. msgBox.setInformativeText(currentView->m_Info.m_assetName.c_str());
  854. msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
  855. msgBox.setDefaultButton(QMessageBox::Cancel);
  856. msgBox.setIcon(QMessageBox::Warning);
  857. int ret = msgBox.exec();
  858. if (ret != QMessageBox::Ok)
  859. {
  860. return;
  861. }
  862. }
  863. // Need to store this off for use on the reload since it will be cleared out/changed as part of the OnCloseDocument call
  864. AZStd::string asset = m_lastFocusedAssetId;
  865. Context_DocumentManagement::Bus::Broadcast(&Context_DocumentManagement::Bus::Events::OnCloseDocument, m_lastFocusedAssetId);
  866. Context_DocumentManagement::Bus::Broadcast(&Context_DocumentManagement::Bus::Events::OnLoadDocument, asset, true);
  867. //not sure about this... looks like legacy, may need to be removed?
  868. // instate the topmost tab as the current asset ID
  869. // so that the window we're going to reopen has something to tabify onto
  870. TrackedLUACtrlTabOrder::iterator tabIter = m_CtrlTabOrder.begin();
  871. if (tabIter != m_CtrlTabOrder.end())
  872. {
  873. m_lastFocusedAssetId = *tabIter;
  874. }
  875. }
  876. void LUAEditorMainWindow::OnFileMenuClose()
  877. {
  878. if (m_lastFocusedAssetId.empty())
  879. {
  880. return;
  881. }
  882. RequestCloseDocument(m_lastFocusedAssetId);
  883. }
  884. void LUAEditorMainWindow::OnFileMenuCloseAll()
  885. {
  886. for (TrackedLUAViewMap::iterator it = m_dOpenLUAView.begin(); it != m_dOpenLUAView.end(); ++it)
  887. {
  888. AZ::SystemTickBus::QueueFunction(&LUAEditorMainWindow::RequestCloseDocument, this, it->first);
  889. }
  890. }
  891. void LUAEditorMainWindow::OnFileMenuCloseAllExcept()
  892. {
  893. for (TrackedLUAViewMap::iterator it = m_dOpenLUAView.begin(); it != m_dOpenLUAView.end(); ++it)
  894. {
  895. if (it->first != m_lastFocusedAssetId)
  896. {
  897. AZ::SystemTickBus::QueueFunction(&LUAEditorMainWindow::RequestCloseDocument, this, it->first);
  898. }
  899. }
  900. }
  901. bool LUAEditorMainWindow::RequestCloseDocument(const AZStd::string id)
  902. {
  903. AZStd::string assetId = id;
  904. //AZ_TracePrintf(LUAEditorDebugName, AZStd::string::format("OnFileMenuClose %s\n", assetId.c_str()).c_str());
  905. TrackedLUAViewMap::iterator viewInfoIter = m_dOpenLUAView.find(assetId);
  906. if (viewInfoIter == m_dOpenLUAView.end())
  907. {
  908. return true; // no such view, probably a double click on close.
  909. }
  910. TrackedLUAView& viewInfo = viewInfoIter->second;
  911. //has the views text changed?
  912. if (!viewInfo.luaViewWidget()->IsReadOnly() &&
  913. viewInfo.luaViewWidget()->IsModified())
  914. {
  915. AzToolsFramework::SaveChangesDialog dialog(this);
  916. dialog.exec();
  917. AzToolsFramework::SaveChangesDialogResult dr = dialog.m_result;
  918. if (dr == AzToolsFramework::SCDR_Save)
  919. {
  920. //the user wants to save before closing
  921. if (!SyncDocumentToContext(assetId))
  922. {
  923. return false;
  924. }
  925. Context_DocumentManagement::Bus::Broadcast(&Context_DocumentManagement::Bus::Events::OnSaveDocument, assetId, true, false);
  926. return true;
  927. }
  928. else if (dr == AzToolsFramework::SCDR_DiscardAndContinue)
  929. {
  930. //the user has chosen to continue to close the document and lose changes
  931. Context_DocumentManagement::Bus::Broadcast(&Context_DocumentManagement::Bus::Events::OnCloseDocument, assetId);
  932. return true;
  933. }
  934. else
  935. {
  936. //the user has canceled the close //or close was pressed
  937. return false;
  938. }
  939. }
  940. else
  941. {
  942. //no changes, just close the document
  943. Context_DocumentManagement::Bus::Broadcast(&Context_DocumentManagement::Bus::Events::OnCloseDocument, assetId);
  944. return true;
  945. }
  946. }
  947. void LUAEditorMainWindow::OnCloseView(const AZStd::string& assetId)
  948. {
  949. //remove it from the tracking list
  950. setAnimated(false);
  951. TrackedLUAViewMap::iterator viewIter = m_dOpenLUAView.find(assetId);
  952. if (viewIter != m_dOpenLUAView.end())
  953. {
  954. viewIter->second.luaDockWidget()->deleteLater();
  955. m_dOpenLUAView.erase(viewIter);
  956. TrackedLUACtrlTabOrder::iterator tabIter = m_CtrlTabOrder.begin();
  957. while (tabIter != m_CtrlTabOrder.end())
  958. {
  959. if (*tabIter == assetId)
  960. {
  961. m_CtrlTabOrder.erase(tabIter);
  962. break;
  963. }
  964. ++tabIter;
  965. }
  966. }
  967. if (m_lastFocusedAssetId == assetId)
  968. {
  969. m_lastFocusedAssetId = "";
  970. }
  971. //if there are no open views then null out the last focused document it should be invalid now
  972. if (m_dOpenLUAView.empty())
  973. {
  974. m_lastFocusedAssetId = "";
  975. SetEditContolsToNoFilesOpen();
  976. AZ_TracePrintf(LUAEditorDebugName, AZStd::string::format("Last Focused Document ID to nullptr\n").c_str());
  977. }
  978. setAnimated(true);
  979. }
  980. void SendKeys(QWidget* pWidget, Qt::Key keyToSend, Qt::KeyboardModifier modifiers, QAction* pActionToDisable)
  981. {
  982. if (!pWidget)
  983. {
  984. return;
  985. }
  986. if (pActionToDisable)
  987. {
  988. pActionToDisable->setDisabled(true);
  989. }
  990. QKeyEvent PressIt(QEvent::KeyPress, keyToSend, modifiers);
  991. QKeyEvent ReleaseIt(QEvent::KeyRelease, keyToSend, modifiers);
  992. QApplication::sendEvent(pWidget, &PressIt);
  993. QApplication::sendEvent(pWidget, &ReleaseIt);
  994. if (pActionToDisable)
  995. {
  996. pActionToDisable->setDisabled(false);
  997. }
  998. }
  999. //edit menu
  1000. void LUAEditorMainWindow::OnEditMenuUndo()
  1001. {
  1002. SendKeys(QApplication::focusWidget(), Qt::Key_Z, Qt::ControlModifier, m_gui->actionUndo);
  1003. }
  1004. void LUAEditorMainWindow::OnEditMenuRedo()
  1005. {
  1006. SendKeys(QApplication::focusWidget(), Qt::Key_Y, Qt::ControlModifier, m_gui->actionRedo);
  1007. }
  1008. void LUAEditorMainWindow::OnEditMenuCut()
  1009. {
  1010. auto currentView = GetCurrentView();
  1011. if (!currentView)
  1012. {
  1013. return;
  1014. }
  1015. int lineFrom;
  1016. int indexFrom;
  1017. int lineTo;
  1018. int indexTo;
  1019. currentView->GetSelection(lineFrom, indexFrom, lineTo, indexTo);
  1020. // on no selection
  1021. if (lineFrom == -1)
  1022. {
  1023. // get the line and strip the whitespace
  1024. currentView->GetCursorPosition(lineFrom, indexFrom);
  1025. currentView->SetSelection(lineFrom, 0, lineFrom + 1, 0);
  1026. QString cutThis = currentView->GetLineText(lineFrom);
  1027. QString finalCut = cutThis.simplified();
  1028. if (!finalCut.length())
  1029. {
  1030. // if the string is now empty, then it was all whitespace and so remove it but not to clipboard
  1031. currentView->RemoveSelectedText();
  1032. return;
  1033. }
  1034. }
  1035. // drop through to standard cut to clipboard handling with the original or our newly selected line(s)
  1036. currentView->Cut();
  1037. }
  1038. void LUAEditorMainWindow::OnEditMenuCopy()
  1039. {
  1040. auto currentView = GetCurrentView();
  1041. if (!currentView)
  1042. {
  1043. return;
  1044. }
  1045. int lineFrom;
  1046. int indexFrom;
  1047. int lineTo;
  1048. int indexTo;
  1049. bool wasSelected = true;
  1050. currentView->GetSelection(lineFrom, indexFrom, lineTo, indexTo);
  1051. // on no selection, force this one line
  1052. if (lineFrom == -1)
  1053. {
  1054. wasSelected = false;
  1055. currentView->GetCursorPosition(lineFrom, indexFrom);
  1056. currentView->SetSelection(lineFrom, 0, lineFrom + 1, 0);
  1057. }
  1058. currentView->Copy();
  1059. if (!wasSelected)
  1060. {
  1061. currentView->SetCursorPosition(lineFrom, indexFrom);
  1062. }
  1063. }
  1064. void LUAEditorMainWindow::OnEditMenuPaste()
  1065. {
  1066. SendKeys(QApplication::focusWidget(), Qt::Key_V, Qt::ControlModifier, m_gui->actionPaste);
  1067. }
  1068. void LUAEditorMainWindow::OnEditMenuFind()
  1069. {
  1070. m_ptrFindDialog->SaveState();
  1071. m_ptrFindDialog->show();
  1072. m_ptrFindDialog->SetAnyDocumentsOpen(m_StateTrack.atLeastOneFileOpen);
  1073. m_ptrFindDialog->SetToFindInAllOpen(false);
  1074. m_ptrFindDialog->SetNewSearchStarting();
  1075. m_ptrFindDialog->ResetSearch();
  1076. m_ptrFindDialog->activateWindow();
  1077. m_ptrFindDialog->raise();
  1078. }
  1079. void LUAEditorMainWindow::OnEditMenuReplace()
  1080. {
  1081. m_ptrFindDialog->SaveState();
  1082. m_ptrFindDialog->show();
  1083. m_ptrFindDialog->SetAnyDocumentsOpen(m_StateTrack.atLeastOneFileOpen);
  1084. m_ptrFindDialog->SetToFindInAllOpen(false);
  1085. m_ptrFindDialog->SetNewSearchStarting();
  1086. m_ptrFindDialog->ResetSearch();
  1087. m_ptrFindDialog->activateWindow();
  1088. m_ptrFindDialog->raise();
  1089. }
  1090. void LUAEditorMainWindow::OnEditMenuFindInAllOpen()
  1091. {
  1092. m_ptrFindDialog->SaveState();
  1093. m_ptrFindDialog->show();
  1094. m_ptrFindDialog->SetAnyDocumentsOpen(m_StateTrack.atLeastOneFileOpen);
  1095. m_ptrFindDialog->SetToFindInAllOpen(true);
  1096. m_ptrFindDialog->SetNewSearchStarting();
  1097. m_ptrFindDialog->ResetSearch();
  1098. m_ptrFindDialog->activateWindow();
  1099. m_ptrFindDialog->raise();
  1100. }
  1101. void LUAEditorMainWindow::OnEditMenuReplaceInAllOpen()
  1102. {
  1103. m_ptrFindDialog->SaveState();
  1104. m_ptrFindDialog->show();
  1105. m_ptrFindDialog->SetAnyDocumentsOpen(m_StateTrack.atLeastOneFileOpen);
  1106. m_ptrFindDialog->SetToFindInAllOpen(true);
  1107. m_ptrFindDialog->SetNewSearchStarting();
  1108. m_ptrFindDialog->ResetSearch();
  1109. m_ptrFindDialog->activateWindow();
  1110. m_ptrFindDialog->raise();
  1111. }
  1112. void LUAEditorMainWindow::OnEditMenuFindLocal()
  1113. {
  1114. m_ptrFindDialog->SaveState();
  1115. m_ptrFindDialog->SetAnyDocumentsOpen(m_StateTrack.atLeastOneFileOpen);
  1116. m_ptrFindDialog->SetToFindInAllOpen(false);
  1117. m_ptrFindDialog->SetNewSearchStarting(true, true);
  1118. m_ptrFindDialog->OnFindNext();
  1119. }
  1120. void LUAEditorMainWindow::OnEditMenuFindLocalReverse()
  1121. {
  1122. m_ptrFindDialog->SaveState();
  1123. m_ptrFindDialog->SetAnyDocumentsOpen(m_StateTrack.atLeastOneFileOpen);
  1124. m_ptrFindDialog->SetToFindInAllOpen(false);
  1125. m_ptrFindDialog->SetNewSearchStarting(true, false);
  1126. m_ptrFindDialog->OnFindNext();
  1127. }
  1128. void LUAEditorMainWindow::OnEditMenuFindNext()
  1129. {
  1130. if (m_ptrFindDialog)
  1131. {
  1132. m_ptrFindDialog->OnFindNext();
  1133. }
  1134. }
  1135. void LUAEditorMainWindow::OnEditMenuGoToLine()
  1136. {
  1137. auto currentView = GetCurrentView();
  1138. if (!currentView)
  1139. {
  1140. return;
  1141. }
  1142. LUAEditorGoToLineDialog dlg(this);
  1143. int lineNumber = 0, cursorColumn = 0;
  1144. currentView->GetCursorPosition(lineNumber, cursorColumn);
  1145. dlg.setLineNumber(lineNumber + 1);
  1146. if (dlg.exec() != QDialog::Rejected)
  1147. {
  1148. // go to that line of the selected file.
  1149. lineNumber = dlg.getLineNumber();
  1150. currentView->SetCursorPosition(lineNumber, 0);
  1151. }
  1152. }
  1153. void LUAEditorMainWindow::OnEditMenuFoldAll()
  1154. {
  1155. if (auto currentView = GetCurrentView())
  1156. {
  1157. currentView->FoldAll();
  1158. }
  1159. }
  1160. void LUAEditorMainWindow::OnEditMenuUnfoldAll()
  1161. {
  1162. if (auto currentView = GetCurrentView())
  1163. {
  1164. currentView->UnfoldAll();
  1165. }
  1166. }
  1167. void LUAEditorMainWindow::OnEditMenuSelectAll()
  1168. {
  1169. if (auto currentView = GetCurrentView())
  1170. {
  1171. currentView->SelectAll();
  1172. }
  1173. }
  1174. void LUAEditorMainWindow::OnEditMenuSelectToBrace()
  1175. {
  1176. if (auto currentView = GetCurrentView())
  1177. {
  1178. currentView->SelectToMatchingBrace();
  1179. }
  1180. }
  1181. void LUAEditorMainWindow::OnCommentSelectedBlock()
  1182. {
  1183. if (NeedsCheckout())
  1184. {
  1185. return;
  1186. }
  1187. if (auto currentView = GetCurrentView())
  1188. {
  1189. // must check read-only status at every operation that can modify the buffer
  1190. if (!currentView->IsReadOnly())
  1191. {
  1192. currentView->CommentSelectedLines();
  1193. }
  1194. }
  1195. }
  1196. void LUAEditorMainWindow::OnUnCommentSelectedBlock()
  1197. {
  1198. if (NeedsCheckout())
  1199. {
  1200. return;
  1201. }
  1202. if (auto currentView = GetCurrentView())
  1203. {
  1204. // must check read-only status at every operation that can modify the buffer
  1205. if (!currentView->IsReadOnly())
  1206. {
  1207. currentView->UncommentSelectedLines();
  1208. }
  1209. }
  1210. }
  1211. bool LUAEditorMainWindow::NeedsCheckout()
  1212. {
  1213. auto currentView = GetCurrentView();
  1214. if (!currentView)
  1215. {
  1216. return false;
  1217. }
  1218. if (currentView->IsReadOnly())
  1219. {
  1220. QMessageBox msgBox;
  1221. msgBox.setText("Checkout This File To Edit?");
  1222. msgBox.setInformativeText(currentView->m_Info.m_assetName.c_str());
  1223. msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
  1224. msgBox.setDefaultButton(QMessageBox::Cancel);
  1225. msgBox.setIcon(QMessageBox::Warning);
  1226. int ret = msgBox.exec();
  1227. if (ret == QMessageBox::Ok)
  1228. {
  1229. OnRequestCheckOut(currentView->m_Info.m_assetId);
  1230. }
  1231. return true;
  1232. }
  1233. return false;
  1234. }
  1235. void LUAEditorMainWindow::OnEditMenuTransposeUp()
  1236. {
  1237. if (NeedsCheckout())
  1238. {
  1239. return;
  1240. }
  1241. // must check read-only status at every operation that can modify the buffer
  1242. if (auto currentView = GetCurrentView())
  1243. {
  1244. if (!currentView->IsReadOnly())
  1245. {
  1246. currentView->MoveSelectedLinesUp();
  1247. }
  1248. }
  1249. }
  1250. void LUAEditorMainWindow::OnEditMenuTransposeDn()
  1251. {
  1252. if (NeedsCheckout())
  1253. {
  1254. return;
  1255. }
  1256. // must check read-only status at every operation that can modify the buffer
  1257. if (auto currentView = GetCurrentView())
  1258. {
  1259. if (!currentView->IsReadOnly())
  1260. {
  1261. currentView->MoveSelectedLinesDn();
  1262. }
  1263. }
  1264. }
  1265. //view menu
  1266. void LUAEditorMainWindow::OnViewMenuBreakpoints()
  1267. {
  1268. OnOpenBreakpointsView();
  1269. }
  1270. void LUAEditorMainWindow::OnViewMenuStack()
  1271. {
  1272. OnOpenStackView();
  1273. }
  1274. void LUAEditorMainWindow::OnViewMenuLocals()
  1275. {
  1276. OnOpenLocalsView();
  1277. }
  1278. void LUAEditorMainWindow::OnViewMenuWatch()
  1279. {
  1280. OnOpenWatchView();
  1281. }
  1282. void LUAEditorMainWindow::OnViewMenuReference()
  1283. {
  1284. OnOpenReferenceView();
  1285. }
  1286. void LUAEditorMainWindow::OnViewMenuFind1()
  1287. {
  1288. OnOpenFindView(0);
  1289. }
  1290. void LUAEditorMainWindow::OnViewMenuFind2()
  1291. {
  1292. OnOpenFindView(1);
  1293. }
  1294. void LUAEditorMainWindow::OnViewMenuFind3()
  1295. {
  1296. OnOpenFindView(2);
  1297. }
  1298. void LUAEditorMainWindow::OnViewMenuFind4()
  1299. {
  1300. OnOpenFindView(3);
  1301. }
  1302. void LUAEditorMainWindow::OnViewMenuResetZoom()
  1303. {
  1304. if (auto currentView = GetCurrentView())
  1305. {
  1306. currentView->ResetZoom();
  1307. }
  1308. }
  1309. //source control menu
  1310. void LUAEditorMainWindow::OnSourceControlMenuCheckOut()
  1311. {
  1312. // if no last document id then return
  1313. if (m_lastFocusedAssetId.empty())
  1314. {
  1315. return;
  1316. }
  1317. Context_DocumentManagement::Bus::Broadcast(&Context_DocumentManagement::Bus::Events::RefreshAllDocumentPerforceStat);
  1318. if (!SyncDocumentToContext(m_lastFocusedAssetId))
  1319. {
  1320. AZ_Warning(LUAEditorDebugName, false, "Could not sync doc data before checkout, data may be lost.");
  1321. QMessageBox::warning(this, "Error!", "Could not sync document before checkout!");
  1322. return;
  1323. }
  1324. Context_DocumentManagement::Bus::Broadcast(
  1325. &Context_DocumentManagement::Bus::Events::DocumentCheckOutRequested, m_lastFocusedAssetId);
  1326. }
  1327. //tools menu
  1328. // when the Editor Main window is requested to close, it is not destroyed.
  1329. //////////////////////////////////////////////////////////////////////////
  1330. // Qt Events
  1331. void LUAEditorMainWindow::closeEvent(QCloseEvent* event)
  1332. {
  1333. OnMenuCloseCurrentWindow();
  1334. event->ignore();
  1335. }
  1336. bool LUAEditorMainWindow::OnGetPermissionToShutDown()
  1337. {
  1338. bool willShutDown = true;
  1339. AZ_TracePrintf(LUAEditorDebugName, "LUAEditorMainWindow::OnGetPermissionToShutDown()\n");
  1340. for (TrackedLUAViewMap::iterator viewInfoIter = m_dOpenLUAView.begin(); viewInfoIter != m_dOpenLUAView.end(); ++viewInfoIter)
  1341. {
  1342. TrackedLUAView& viewInfo = viewInfoIter->second;
  1343. //have the views' text changed?
  1344. if (!viewInfo.luaViewWidget()->IsReadOnly() &&
  1345. viewInfo.luaViewWidget()->IsModified())
  1346. {
  1347. this->show();
  1348. viewInfo.luaDockWidget()->show();
  1349. viewInfo.luaDockWidget()->raise();
  1350. AzToolsFramework::SaveChangesDialog dialog(this);
  1351. dialog.exec();
  1352. AzToolsFramework::SaveChangesDialogResult dr = dialog.m_result;
  1353. if (dr == AzToolsFramework::SCDR_Save)
  1354. {
  1355. if (!SyncDocumentToContext(viewInfo.luaViewWidget()->m_Info.m_assetId))
  1356. {
  1357. AZ_Warning(LUAEditorDebugName, false, "Could not sync doc data before closing it, data may be lost.");
  1358. willShutDown = false;
  1359. AZ_TracePrintf(LUAEditorInfoName, " SyncDocumentToContext() failure\n");
  1360. break;
  1361. }
  1362. AZ_TracePrintf(LUAEditorDebugName, "LUAEditorMainWindow::OnGetPermissionToShutDown() SAVING %s\n", viewInfo.luaViewWidget()->m_Info.m_assetName.c_str());
  1363. Context_DocumentManagement::Bus::Broadcast(
  1364. &Context_DocumentManagement::Bus::Events::OnSaveDocument, viewInfo.luaViewWidget()->m_Info.m_assetId, false, false);
  1365. }
  1366. else if (dr == AzToolsFramework::SCDR_DiscardAndContinue)
  1367. {
  1368. //the user has chosen to continue and lose changes
  1369. if (viewInfo.luaViewWidget()->m_Info.m_bUntitledDocument)
  1370. {
  1371. AZ_TracePrintf(LUAEditorDebugName, " Forced close\n");
  1372. // all untitled documents are force closed to clear any tracked states that will be serialized by their trackers
  1373. Context_DocumentManagement::Bus::Broadcast(
  1374. &Context_DocumentManagement::Bus::Events::OnCloseDocument, viewInfo.luaViewWidget()->m_Info.m_assetId);
  1375. viewInfoIter = m_dOpenLUAView.begin();
  1376. }
  1377. else // all titled (i.e. preexisting or saved files, simply reload
  1378. {
  1379. AZ_TracePrintf(LUAEditorDebugName, " Forced reload\n");
  1380. Context_DocumentManagement::Bus::Broadcast(
  1381. &Context_DocumentManagement::Bus::Events::OnReloadDocument, viewInfo.luaViewWidget()->m_Info.m_assetId);
  1382. viewInfoIter = m_dOpenLUAView.begin();
  1383. }
  1384. }
  1385. else
  1386. {
  1387. //the user has canceled the close
  1388. willShutDown = false;
  1389. break;
  1390. }
  1391. }
  1392. }
  1393. return willShutDown;
  1394. }
  1395. void LUAEditorMainWindow::SaveWindowState()
  1396. {
  1397. // build state and store it.
  1398. auto newState = AZ::UserSettings::CreateFind<LUAEditorMainWindowSavedState>(AZ_CRC_CE("LUA EDITOR MAIN WINDOW STATE"), AZ::UserSettings::CT_LOCAL);
  1399. newState->Init(saveState(), saveGeometry());
  1400. newState->m_bAutocompleteEnabled = m_bAutocompleteEnabled;
  1401. // gather and store the open files in tab order
  1402. newState->m_openAssetIds.clear();
  1403. // the following is experimental, apparently widget child order doesn't match tab order, which is unfortunate
  1404. QList<QDockWidget*> dockWidgets = qobject_cast<QMainWindow*>(this->centralWidget())->findChildren<QDockWidget*>();
  1405. QList<QDockWidget*>::iterator qlit = dockWidgets.begin();
  1406. while (qlit != dockWidgets.end())
  1407. {
  1408. LUADockWidget* ldw = qobject_cast<LUADockWidget*>(*qlit);
  1409. if (ldw)
  1410. {
  1411. TrackedLUAViewMap::iterator viewInfoIter = m_dOpenLUAView.find(ldw->assetId());
  1412. if (viewInfoIter != m_dOpenLUAView.end())
  1413. {
  1414. TrackedLUAView& viewInfo = viewInfoIter->second;
  1415. newState->m_openAssetIds.push_back(viewInfo.luaViewWidget()->m_Info.m_assetId);
  1416. AZ_TracePrintf(LUAEditorDebugName, " - TAB Saved %s\n", viewInfo.luaViewWidget()->m_Info.m_assetName.c_str());
  1417. }
  1418. }
  1419. qlit++;
  1420. }
  1421. m_gui->m_logPanel->SaveState();
  1422. auto savedState = AZ::UserSettings::CreateFind<AzToolsFramework::MainWindowSavedState>(AZ_CRC_CE("INNER_LUA_WINDOW"), AZ::UserSettings::CT_LOCAL);
  1423. if (savedState)
  1424. {
  1425. // restore state.
  1426. QMainWindow* pMainWindow = static_cast<QMainWindow*>(centralWidget());
  1427. savedState->Init(pMainWindow->saveState(), pMainWindow->saveGeometry());
  1428. }
  1429. }
  1430. void LUAEditorMainWindow::OnLogTabsReset()
  1431. {
  1432. m_gui->m_logPanel->AddLogTab(AzToolsFramework::LogPanel::TabSettings("Lua Editor", "Lua Editor", ""));
  1433. }
  1434. void LUAEditorMainWindow::RestoreWindowState() // call this after you have rebuilt everything.
  1435. {
  1436. if (!m_gui->m_logPanel->LoadState())
  1437. {
  1438. OnLogTabsReset();
  1439. }
  1440. // load the state from our state block:
  1441. auto pEditorMainSavedState = AZ::UserSettings::Find<LUAEditorMainWindowSavedState>(AZ_CRC_CE("LUA EDITOR MAIN WINDOW STATE"), AZ::UserSettings::CT_LOCAL);
  1442. if (pEditorMainSavedState)
  1443. {
  1444. QByteArray editorGeomData((const char*)pEditorMainSavedState->m_windowGeometry.data(), (int)pEditorMainSavedState->m_windowGeometry.size());
  1445. QByteArray editorStateData((const char*)pEditorMainSavedState->GetWindowState().data(), (int)pEditorMainSavedState->GetWindowState().size());
  1446. for (const auto& assetId : pEditorMainSavedState->m_openAssetIds)
  1447. {
  1448. Context_DocumentManagement::Bus::Broadcast(&Context_DocumentManagement::Bus::Events::OnLoadDocument, assetId, false);
  1449. }
  1450. restoreGeometry(editorGeomData);
  1451. if (this->isMaximized())
  1452. {
  1453. this->showNormal();
  1454. this->showMaximized();
  1455. }
  1456. restoreState(editorStateData);
  1457. m_bAutocompleteEnabled = pEditorMainSavedState->m_bAutocompleteEnabled;
  1458. OnAutocompleteChanged(m_bAutocompleteEnabled);
  1459. auto pWindowSavedState = AZ::UserSettings::Find<AzToolsFramework::MainWindowSavedState>(AZ_CRC_CE("INNER_LUA_WINDOW"), AZ::UserSettings::CT_LOCAL);
  1460. if (pWindowSavedState)
  1461. {
  1462. // restore state.
  1463. QByteArray windowGeomData((const char*)pWindowSavedState->m_windowGeometry.data(), (int)pWindowSavedState->m_windowGeometry.size());
  1464. QByteArray windowStateData((const char*)pWindowSavedState->GetWindowState().data(), (int)pWindowSavedState->GetWindowState().size());
  1465. QMainWindow* pMainWindow = static_cast<QMainWindow*>(centralWidget());
  1466. pMainWindow->restoreState(windowStateData);
  1467. }
  1468. }
  1469. else
  1470. {
  1471. // default state!
  1472. }
  1473. }
  1474. LUAViewWidget* LUAEditorMainWindow::GetCurrentView()
  1475. {
  1476. if (m_lastFocusedAssetId.empty())
  1477. {
  1478. return nullptr;
  1479. }
  1480. TrackedLUAViewMap::iterator viewInfoIter = m_dOpenLUAView.find(m_lastFocusedAssetId);
  1481. AZ_Assert(viewInfoIter != m_dOpenLUAView.end(), "OnFileMenuClose() : Cant find view Info.");
  1482. TrackedLUAView& viewInfo = viewInfoIter->second;
  1483. return viewInfo.luaViewWidget();
  1484. }
  1485. AZStd::vector<LUAViewWidget*> LUAEditorMainWindow::GetAllViews()
  1486. {
  1487. AZStd::vector<LUAViewWidget*> dViews;
  1488. for (TrackedLUAViewMap::iterator viewInfoIter = m_dOpenLUAView.begin(); viewInfoIter != m_dOpenLUAView.end(); ++viewInfoIter)
  1489. {
  1490. dViews.push_back(viewInfoIter->second.luaViewWidget());
  1491. }
  1492. return dViews;
  1493. }
  1494. FindResults* LUAEditorMainWindow::GetFindResultsWidget(int index)
  1495. {
  1496. switch (index)
  1497. {
  1498. case 0:
  1499. return m_gui->m_findResults1;
  1500. case 1:
  1501. return m_gui->m_findResults2;
  1502. case 2:
  1503. return m_gui->m_findResults3;
  1504. case 3:
  1505. return m_gui->m_findResults4;
  1506. }
  1507. return nullptr;
  1508. }
  1509. void LUAEditorMainWindow::SetCurrentFindListWidget(int index)
  1510. {
  1511. AZ_Assert(index >= 0 && index < 4, "Only 4 find windows currently");
  1512. m_gui->findTabWidget->setCurrentIndex(index);
  1513. }
  1514. void LUAEditorMainWindow::OnFindResultClicked(FindResultsBlockInfo result)
  1515. {
  1516. if (OnRequestFocusView(result.m_assetId))
  1517. {
  1518. LUAViewWidget* pLUAViewWidget = GetCurrentView();
  1519. if (!pLUAViewWidget)
  1520. {
  1521. return;
  1522. }
  1523. pLUAViewWidget->SetCursorPosition(result.m_lineNumber, result.m_firstMatchPosition);
  1524. }
  1525. else
  1526. {
  1527. // the document was probably closed, request it be reopened
  1528. m_dProcessFindListClicked.push_back(result);
  1529. AZ_Assert(false, "Fix assets!");
  1530. }
  1531. }
  1532. void LUAEditorMainWindow::OnDataLoadedAndSet(const DocumentInfo& info, LUAViewWidget* pLUAViewWidget)
  1533. {
  1534. for (auto iter = m_dProcessFindListClicked.begin(); iter != m_dProcessFindListClicked.end(); ++iter)
  1535. {
  1536. if (iter->m_assetId == info.m_assetId)
  1537. {
  1538. AZ_Assert(iter->m_assignAssetId, "m_assignAssetId was never set");
  1539. iter->m_assetId = pLUAViewWidget->m_Info.m_assetId;
  1540. iter->m_assignAssetId(info.m_assetName, pLUAViewWidget->m_Info.m_assetId);
  1541. AZ::SystemTickBus::QueueFunction(&LUAEditorMainWindow::OnFindResultClicked, this, *iter);
  1542. m_dProcessFindListClicked.erase(iter);
  1543. return;
  1544. }
  1545. }
  1546. }
  1547. bool LUAEditorMainWindow::OnFileSaveDialog(const AZStd::string& assetName, AZStd::string& newAssetName)
  1548. {
  1549. const QDir rootDir { AZ::Utils::GetProjectPath().c_str() };
  1550. QString name = QFileDialog::getSaveFileName(this, QString(AZStd::string::format("Save File {%s}", assetName.c_str()).c_str()), m_lastOpenFilePath.size() > 0 ? m_lastOpenFilePath.c_str() : rootDir.absolutePath(), QString("*.lua"));
  1551. if (name.isEmpty())
  1552. {
  1553. return false;
  1554. }
  1555. AzFramework::StringFunc::Path::Split(name.toUtf8().data(), nullptr, &m_lastOpenFilePath);
  1556. newAssetName = name.toUtf8().data();
  1557. return true;
  1558. }
  1559. bool LUAEditorMainWindow::OnFileSaveAsDialog(const AZStd::string& assetName, AZStd::string& newAssetName)
  1560. {
  1561. const char* rootDirString;
  1562. AZ::ComponentApplicationBus::BroadcastResult(rootDirString, &AZ::ComponentApplicationBus::Events::GetExecutableFolder);
  1563. QDir rootDir;
  1564. rootDir.setPath(rootDirString);
  1565. rootDir.cdUp();
  1566. QString name = QFileDialog::getSaveFileName(this, QString(AZStd::string::format("Save File As {%s}", assetName.c_str()).c_str()), rootDir.absolutePath(), QString("*.lua"));
  1567. if (name.isEmpty())
  1568. {
  1569. return false;
  1570. }
  1571. //name has the full path in it, we need to convert it to an asset name
  1572. AZStd::string projectRoot, databaseRoot, databasePath, databaseFile, fileExtension;
  1573. if (!AzFramework::StringFunc::AssetDatabasePath::Split(name.toUtf8().data(), &projectRoot, &databaseRoot, &databasePath, &databaseFile, &fileExtension))
  1574. {
  1575. AZ_Warning("LUAEditorMainWindow", false, AZStd::string::format("<span severity=\"err\">Path is invalid: '%s'</span>", name.toUtf8().data()).c_str());
  1576. return false;
  1577. }
  1578. AzFramework::StringFunc::AssetDatabasePath::Join(databasePath.c_str(), databaseFile.c_str(), newAssetName);
  1579. return true;
  1580. }
  1581. //////////////////////////////////////////////////////////////////////////
  1582. // LUAEditorMainWindow Messages
  1583. void LUAEditorMainWindow::OnFocusInEvent(const AZStd::string& assetId)
  1584. {
  1585. m_lastFocusedAssetId = assetId;
  1586. if (!m_bIgnoreFocusRequests)
  1587. {
  1588. SetGUIToMatch(m_StateTrack);
  1589. }
  1590. }
  1591. void LUAEditorMainWindow::OnFocusOutEvent(const AZStd::string&)
  1592. {
  1593. }
  1594. void LUAEditorMainWindow::OnRequestCheckOut(const AZStd::string& assetId)
  1595. {
  1596. AZStd::string restoreAssetId = m_lastFocusedAssetId;
  1597. m_lastFocusedAssetId = assetId;
  1598. OnSourceControlMenuCheckOut();
  1599. m_lastFocusedAssetId = restoreAssetId;
  1600. }
  1601. //////////////////////////////////////////////////////////////////////////
  1602. bool LUAEditorMainWindow::OnRequestFocusView(const AZStd::string& assetId)
  1603. {
  1604. TrackedLUAViewMap::iterator viewIter = m_dOpenLUAView.find(assetId);
  1605. if (viewIter != m_dOpenLUAView.end())
  1606. {
  1607. viewIter->second.luaDockWidget()->show();
  1608. viewIter->second.luaDockWidget()->raise();
  1609. viewIter->second.luaViewWidget()->RegainFocusFinal();
  1610. return true;
  1611. }
  1612. return false;
  1613. }
  1614. void LUAEditorMainWindow::OnDocumentInfoUpdated(const DocumentInfo& docInfo)
  1615. {
  1616. // document has fresh information available (it was checked out or its data finished or etc)
  1617. TrackedLUAViewMap::iterator viewIter = m_dOpenLUAView.find(docInfo.m_assetId);
  1618. if (viewIter != m_dOpenLUAView.end())
  1619. {
  1620. viewIter->second.luaViewWidget()->OnDocumentInfoUpdated(docInfo);
  1621. }
  1622. }
  1623. // preliminaries for making the debug action buttons context sensitive
  1624. void LUAEditorMainWindow::BreakpointsUpdate(const LUAEditor::BreakpointMap& uniqueBreakpoints)
  1625. {
  1626. (void)uniqueBreakpoints;
  1627. }
  1628. void LUAEditorMainWindow::BreakpointHit(const LUAEditor::Breakpoint& breakpoint)
  1629. {
  1630. (void)breakpoint;
  1631. SetDebugControlsToAtBreak();
  1632. }
  1633. void LUAEditorMainWindow::BreakpointResume()
  1634. {
  1635. SetDebugControlsToRunning();
  1636. }
  1637. //////////////////////////////////////////////////////////////////////////
  1638. // externally driven context sensitive widget states
  1639. void LUAEditorMainWindow::SetDebugControlsToInitial()
  1640. {
  1641. m_StateTrack.Init();
  1642. SetGUIToMatch(m_StateTrack);
  1643. }
  1644. void LUAEditorMainWindow::SetDebugControlsToRunning()
  1645. {
  1646. AZ_TracePrintf(LUAEditorDebugName, "LUAEditorMainWindow::SetDebugControlsToRunning()\n");
  1647. m_StateTrack.scriptRunning = true;
  1648. m_StateTrack.atBreak = false;
  1649. m_StateTrack.hasExecuted = true;
  1650. SetGUIToMatch(m_StateTrack);
  1651. if (!m_lastFocusedAssetId.empty())
  1652. {
  1653. TrackedLUAViewMap::iterator viewInfoIter = m_dOpenLUAView.find(m_lastFocusedAssetId);
  1654. AZ_Assert(viewInfoIter != m_dOpenLUAView.end(), "OnFileMenuClose() : Cant find view Info.");
  1655. TrackedLUAView& viewInfo = viewInfoIter->second;
  1656. viewInfo.luaViewWidget()->UpdateCurrentExecutingLine(-1);
  1657. }
  1658. LUAEditor::LUAStackTrackerMessages::Bus::Broadcast(&LUAEditor::LUAStackTrackerMessages::Bus::Events::StackClear);
  1659. }
  1660. void LUAEditorMainWindow::SetDebugControlsToAtBreak()
  1661. {
  1662. AZ_TracePrintf(LUAEditorDebugName, "LUAEditorMainWindow::SetDebugControlsToAtBreak()\n");
  1663. m_StateTrack.scriptRunning = false;
  1664. m_StateTrack.atBreak = true;
  1665. m_StateTrack.hasExecuted = true;
  1666. SetGUIToMatch(m_StateTrack);
  1667. }
  1668. void LUAEditorMainWindow::SetEditContolsToNoFilesOpen()
  1669. {
  1670. m_StateTrack.atLeastOneFileOpen = false;
  1671. SetGUIToMatch(m_StateTrack);
  1672. }
  1673. void LUAEditorMainWindow::SetEditContolsToAtLeastOneFileOpen()
  1674. {
  1675. m_StateTrack.atLeastOneFileOpen = true;
  1676. SetGUIToMatch(m_StateTrack);
  1677. }
  1678. void LUAEditorMainWindow::luaClassFilterTextChanged(const QString& newPattern)
  1679. {
  1680. m_ClassReferenceFilter->SetFilter(newPattern);
  1681. if (newPattern.isEmpty())
  1682. {
  1683. m_gui->classReferenceTreeView->collapseAll();
  1684. }
  1685. else
  1686. {
  1687. m_gui->classReferenceTreeView->expandAll();
  1688. }
  1689. }
  1690. void LUAEditorMainWindow::OnConnectedToTarget()
  1691. {
  1692. AZ_TracePrintf(LUAEditorDebugName, "LUAEditorMainWindow::OnConnectedToTarget()\n");
  1693. m_StateTrack.targetConnected = true;
  1694. m_StateTrack.debuggerAttached = false;
  1695. m_StateTrack.scriptRunning = false;
  1696. m_StateTrack.atBreak = false;
  1697. m_StateTrack.hasExecuted = false;
  1698. SetGUIToMatch(m_StateTrack);
  1699. }
  1700. void LUAEditorMainWindow::OnDisconnectedFromTarget()
  1701. {
  1702. AZ_TracePrintf(LUAEditorDebugName, "LUAEditorMainWindow::OnDisconnectedFromTarget()\n");
  1703. m_StateTrack.targetConnected = false;
  1704. m_StateTrack.debuggerAttached = false;
  1705. m_StateTrack.scriptRunning = false;
  1706. m_StateTrack.atBreak = false;
  1707. m_StateTrack.hasExecuted = false;
  1708. SetGUIToMatch(m_StateTrack);
  1709. }
  1710. void LUAEditorMainWindow::OnConnectedToDebugger()
  1711. {
  1712. AZ_TracePrintf(LUAEditorDebugName, "LUAEditorMainWindow::OnConnectedToDebugger()\n");
  1713. m_StateTrack.debuggerAttached = true;
  1714. m_StateTrack.scriptRunning = false;
  1715. m_StateTrack.atBreak = false;
  1716. m_StateTrack.hasExecuted = false;
  1717. SetGUIToMatch(m_StateTrack);
  1718. }
  1719. void LUAEditorMainWindow::OnDisconnectedFromDebugger()
  1720. {
  1721. AZ_TracePrintf(LUAEditorDebugName, "LUAEditorMainWindow::OnDisconnectedFromDebugger()\n");
  1722. m_StateTrack.debuggerAttached = false;
  1723. m_StateTrack.scriptRunning = false;
  1724. m_StateTrack.atBreak = false;
  1725. m_StateTrack.hasExecuted = false;
  1726. SetGUIToMatch(m_StateTrack);
  1727. }
  1728. void LUAEditorMainWindow::Repaint()
  1729. {
  1730. SetGUIToMatch(m_StateTrack);
  1731. const AZStd::vector<LUAViewWidget*>& allViews = GetAllViews();
  1732. for (LUAViewWidget* view : allViews)
  1733. {
  1734. view->UpdateFont();
  1735. }
  1736. }
  1737. void LUAEditorMainWindow::OnExecuteScriptResult(bool success)
  1738. {
  1739. if (success)
  1740. {
  1741. m_StateTrack.hasExecuted = true;
  1742. SetDebugControlsToRunning();
  1743. }
  1744. }
  1745. void LUAEditorMainWindow::SetGUIToMatch(StateTrack& track)
  1746. {
  1747. if (track.atLeastOneFileOpen)
  1748. {
  1749. m_gui->actionSave->setEnabled(true);
  1750. m_gui->actionClose->setEnabled(true);
  1751. m_gui->actionSaveAll->setEnabled(true);
  1752. m_gui->actionUndo->setEnabled(true);
  1753. m_gui->actionRedo->setEnabled(true);
  1754. m_gui->actionCut->setEnabled(true);
  1755. m_gui->actionCopy->setEnabled(true);
  1756. m_gui->actionPaste->setEnabled(true);
  1757. m_gui->actionSaveAs->setEnabled(true);
  1758. m_gui->actionCheckOut->setEnabled(true);
  1759. m_gui->actionGoToLine->setEnabled(true);
  1760. m_gui->action_execute->setEnabled(true);
  1761. m_gui->action_togglebreak->setEnabled(true);
  1762. }
  1763. else
  1764. {
  1765. m_gui->actionSave->setEnabled(false);
  1766. m_gui->actionClose->setEnabled(false);
  1767. m_gui->actionSaveAll->setEnabled(false);
  1768. m_gui->actionUndo->setEnabled(false);
  1769. m_gui->actionRedo->setEnabled(false);
  1770. m_gui->actionCut->setEnabled(false);
  1771. m_gui->actionCopy->setEnabled(false);
  1772. m_gui->actionPaste->setEnabled(false);
  1773. m_gui->actionSaveAs->setEnabled(false);
  1774. m_gui->actionCheckOut->setEnabled(false);
  1775. m_gui->actionGoToLine->setEnabled(false);
  1776. m_gui->action_execute->setEnabled(false);
  1777. m_gui->action_togglebreak->setEnabled(false);
  1778. }
  1779. // special handling for the watches
  1780. m_gui->watchDockWidget->setEnabled(false);
  1781. if (track.debuggerAttached)
  1782. {
  1783. m_gui->watchDockWidget->setEnabled(true);
  1784. }
  1785. if ((!track.targetConnected) || (!track.debuggerAttached))
  1786. {
  1787. // turn off any little yellow arrows (QScintilla)
  1788. TrackedLUAViewMap::iterator viewInfoIter = m_dOpenLUAView.find(m_lastFocusedAssetId);
  1789. if (viewInfoIter != m_dOpenLUAView.end())
  1790. {
  1791. TrackedLUAView& viewInfo = viewInfoIter->second;
  1792. if (viewInfo.luaViewWidget())
  1793. {
  1794. viewInfo.luaViewWidget()->UpdateCurrentExecutingLine(-1);
  1795. }
  1796. }
  1797. }
  1798. if (!track.targetConnected)
  1799. {
  1800. m_pDebugAttachmentButton->setEnabled(false);
  1801. m_gui->action_continue->setEnabled(false);
  1802. m_gui->action_ExecuteOnTarget->setEnabled(false);
  1803. m_gui->action_stepover->setEnabled(false);
  1804. m_gui->action_stepin->setEnabled(false);
  1805. m_gui->action_stepout->setEnabled(false);
  1806. // EARLY OUT
  1807. return;
  1808. }
  1809. // TARGET CONNECTED TRUE IS ASSUMED BEYOND THIS POINT
  1810. m_pDebugAttachmentButton->setEnabled(true);
  1811. if (!track.debuggerAttached)
  1812. {
  1813. m_gui->action_ExecuteOnTarget->setEnabled(false);
  1814. m_gui->action_stepover->setEnabled(false);
  1815. m_gui->action_stepin->setEnabled(false);
  1816. m_gui->action_stepout->setEnabled(false);
  1817. m_gui->action_continue->setEnabled(false);
  1818. // EARLY OUT
  1819. return;
  1820. }
  1821. // DEBUGGER ATTACHED TRUE IS ASSUMED BEYOND THIS POINT
  1822. if (track.scriptRunning)
  1823. {
  1824. if (track.atBreak) // running script and at a break
  1825. {
  1826. m_gui->action_ExecuteOnTarget->setEnabled(false);
  1827. m_gui->action_stepover->setEnabled(true);
  1828. m_gui->action_stepin->setEnabled(true);
  1829. m_gui->action_stepout->setEnabled(true);
  1830. m_gui->action_continue->setEnabled(true);
  1831. }
  1832. else // running script and NOT at a break
  1833. {
  1834. m_gui->action_ExecuteOnTarget->setEnabled(true);
  1835. m_gui->action_stepover->setEnabled(false);
  1836. m_gui->action_stepin->setEnabled(false);
  1837. m_gui->action_stepout->setEnabled(false);
  1838. m_gui->action_continue->setEnabled(false); // this will make it execute remotely...
  1839. }
  1840. }
  1841. else // script NOT running
  1842. {
  1843. if (track.atBreak) // script is NOT running and we're at a break
  1844. {
  1845. m_gui->action_ExecuteOnTarget->setEnabled(false);
  1846. m_gui->action_stepover->setEnabled(track.hasExecuted);
  1847. m_gui->action_stepin->setEnabled(track.hasExecuted);
  1848. m_gui->action_stepout->setEnabled(track.hasExecuted);
  1849. m_gui->action_continue->setEnabled(track.hasExecuted);
  1850. }
  1851. else // script is NOT running and NOT at a break
  1852. {
  1853. if (track.atLeastOneFileOpen)
  1854. {
  1855. m_gui->action_ExecuteOnTarget->setEnabled(true);
  1856. m_gui->action_stepover->setEnabled(track.hasExecuted);
  1857. m_gui->action_stepin->setEnabled(track.hasExecuted);
  1858. m_gui->action_stepout->setEnabled(track.hasExecuted);
  1859. m_gui->action_continue->setEnabled(track.hasExecuted);
  1860. }
  1861. else // NO files open
  1862. {
  1863. m_gui->action_ExecuteOnTarget->setEnabled(false);
  1864. m_gui->action_stepover->setEnabled(false);
  1865. m_gui->action_stepin->setEnabled(false);
  1866. m_gui->action_stepout->setEnabled(false);
  1867. m_gui->action_continue->setEnabled(false);
  1868. }
  1869. }
  1870. }
  1871. }
  1872. bool LUAEditorMainWindow::HasAtLeastOneFileOpen() const
  1873. {
  1874. return m_StateTrack.atLeastOneFileOpen;
  1875. }
  1876. bool LUAEditorMainWindow::eventFilter(QObject* obj, QEvent* event)
  1877. {
  1878. (void)obj;
  1879. if (event->type() == QEvent::KeyPress)
  1880. {
  1881. QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
  1882. if (keyEvent->key() == Qt::Key_C && (keyEvent->modifiers() & Qt::ControlModifier))
  1883. {
  1884. OnEditMenuCopy();
  1885. return true;
  1886. }
  1887. else if (keyEvent->key() == Qt::Key_X && (keyEvent->modifiers() & Qt::ControlModifier))
  1888. {
  1889. OnEditMenuCut();
  1890. return true;
  1891. }
  1892. }
  1893. else if (event->type() == QEvent::KeyRelease)
  1894. {
  1895. QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
  1896. if (keyEvent->key() == Qt::Key_Control)
  1897. {
  1898. m_StoredTabAssetId = "";
  1899. }
  1900. }
  1901. //return QObject::eventFilter(obj, event);
  1902. return false;
  1903. }
  1904. void LUAEditorMainWindow::OnTabForwards()
  1905. {
  1906. TrackedLUACtrlTabOrder::iterator tabIter = m_CtrlTabOrder.begin();
  1907. while (tabIter != m_CtrlTabOrder.end())
  1908. {
  1909. if (*tabIter == m_lastFocusedAssetId)
  1910. {
  1911. break;
  1912. }
  1913. tabIter++;
  1914. }
  1915. if (tabIter == m_CtrlTabOrder.begin())
  1916. {
  1917. tabIter = m_CtrlTabOrder.end();
  1918. --tabIter;
  1919. }
  1920. else
  1921. {
  1922. --tabIter;
  1923. }
  1924. TrackedLUAViewMap::iterator viewInfoIter = m_dOpenLUAView.find(*tabIter);
  1925. if (viewInfoIter != m_dOpenLUAView.end())
  1926. {
  1927. viewInfoIter->second.luaDockWidget()->show();
  1928. viewInfoIter->second.luaDockWidget()->raise();
  1929. viewInfoIter->second.luaViewWidget()->setFocus();
  1930. m_lastFocusedAssetId = *tabIter;
  1931. }
  1932. }
  1933. void LUAEditorMainWindow::OnTabBackwards()
  1934. {
  1935. TrackedLUACtrlTabOrder::iterator tabIter = m_CtrlTabOrder.begin();
  1936. while (tabIter != m_CtrlTabOrder.end())
  1937. {
  1938. if (*tabIter == m_lastFocusedAssetId)
  1939. {
  1940. break;
  1941. }
  1942. tabIter++;
  1943. }
  1944. if (tabIter == m_CtrlTabOrder.end())
  1945. {
  1946. return;
  1947. }
  1948. tabIter++;
  1949. if (tabIter == m_CtrlTabOrder.end())
  1950. {
  1951. tabIter = m_CtrlTabOrder.begin();
  1952. }
  1953. TrackedLUAViewMap::iterator viewInfoIter = m_dOpenLUAView.find(*tabIter);
  1954. if (viewInfoIter != m_dOpenLUAView.end())
  1955. {
  1956. viewInfoIter->second.luaDockWidget()->show();
  1957. viewInfoIter->second.luaDockWidget()->raise();
  1958. viewInfoIter->second.luaViewWidget()->setFocus();
  1959. m_lastFocusedAssetId = *tabIter;
  1960. }
  1961. }
  1962. void LUAEditorMainWindow::dragEnterEvent(QDragEnterEvent* pEvent)
  1963. {
  1964. if (!pEvent->mimeData()->hasUrls())
  1965. {
  1966. return;
  1967. }
  1968. pEvent->acceptProposedAction();
  1969. }
  1970. void LUAEditorMainWindow::dropEvent(QDropEvent* pEvent)
  1971. {
  1972. if (!pEvent->mimeData()->hasUrls())
  1973. {
  1974. return;
  1975. }
  1976. pEvent->setDropAction(Qt::CopyAction);
  1977. pEvent->accept();
  1978. QList<QUrl> urls = pEvent->mimeData()->urls();
  1979. for (int idx = 0; idx < urls.count(); ++idx)
  1980. {
  1981. QString path = urls[idx].toLocalFile();
  1982. AZ_TracePrintf("Debug", "URL: %s\n", path.toUtf8().data());
  1983. AZStd::string assetId(path.toUtf8().data());
  1984. Context_DocumentManagement::Bus::Broadcast(&Context_DocumentManagement::Bus::Events::OnLoadDocument, assetId, true);
  1985. }
  1986. }
  1987. QTabWidget* LUAEditorMainWindow::GetFindTabWidget()
  1988. {
  1989. return m_gui->findTabWidget;
  1990. }
  1991. void LUAEditorMainWindow::AddMessageToLog(AzToolsFramework::Logging::LogLine::LogType type, const char* window, const char* message, void* userData)
  1992. {
  1993. m_gui->m_logPanel->InsertLogLine(type, window, message, userData);
  1994. }
  1995. void LUAEditorMainWindow::LogLineSelectionChanged(const AzToolsFramework::Logging::LogLine& logLine)
  1996. {
  1997. CompilationErrorData* errorData = static_cast<CompilationErrorData*>(logLine.GetUserData());
  1998. if (errorData)
  1999. {
  2000. // Use the data, if it exists from the logLine to make sure the right tab/line is highlighted in the editor
  2001. if (OnRequestFocusView(errorData->m_filename))
  2002. {
  2003. LUAViewWidget* pLUAViewWidget = GetCurrentView();
  2004. if (pLUAViewWidget)
  2005. {
  2006. pLUAViewWidget->SetCursorPosition(errorData->m_lineNumber, 0);
  2007. }
  2008. }
  2009. }
  2010. }
  2011. void LUAEditorMainWindowSavedState::Reflect(AZ::ReflectContext* reflection)
  2012. {
  2013. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(reflection);
  2014. if (serializeContext)
  2015. {
  2016. serializeContext->Class<LUAEditorMainWindowSavedState, AzToolsFramework::MainWindowSavedState >()
  2017. ->Version(5)
  2018. ->Field("m_openAssetIds", &LUAEditorMainWindowSavedState::m_openAssetIds)
  2019. ->Field("m_bAutocompleteEnabled", &LUAEditorMainWindowSavedState::m_bAutocompleteEnabled)
  2020. ->Field("m_bAutoReloadUnmodifiedFiles", &LUAEditorMainWindowSavedState::m_bAutoReloadUnmodifiedFiles);
  2021. }
  2022. LUAEditorFindDialog::Reflect(reflection);
  2023. }
  2024. void LUAEditorMainWindowLayout::addItem(QLayoutItem* pChild)
  2025. {
  2026. children.push_back(pChild);
  2027. }
  2028. QLayoutItem* LUAEditorMainWindowLayout::itemAt(int index) const
  2029. {
  2030. if (index >= (int)children.size())
  2031. {
  2032. return nullptr;
  2033. }
  2034. return children[index];
  2035. }
  2036. QLayoutItem* LUAEditorMainWindowLayout::takeAt(int index)
  2037. {
  2038. QLayoutItem* pItem = nullptr;
  2039. if (index >= (int)children.size())
  2040. {
  2041. return nullptr;
  2042. }
  2043. pItem = children[index];
  2044. children.erase(children.begin() + index);
  2045. return pItem;
  2046. }
  2047. LUAEditorMainWindowLayout::LUAEditorMainWindowLayout(QWidget* pParent)
  2048. {
  2049. (void)pParent;
  2050. }
  2051. LUAEditorMainWindowLayout::~LUAEditorMainWindowLayout()
  2052. {
  2053. QLayoutItem* item;
  2054. item = takeAt(0);
  2055. while (item)
  2056. {
  2057. delete item;
  2058. item = takeAt(0);
  2059. }
  2060. }
  2061. int LUAEditorMainWindowLayout::count() const
  2062. {
  2063. return (int)children.size();
  2064. }
  2065. void LUAEditorMainWindowLayout::setGeometry (const QRect& r)
  2066. {
  2067. int left, top, right, bottom;
  2068. getContentsMargins(&left, &top, &right, &bottom);
  2069. QRect effectiveRect = r.adjusted(+left, +top, -right, -bottom);
  2070. for (int pos = 0; pos < (int)children.size() - 1; ++pos)
  2071. {
  2072. QLayoutItem* pItem = children[pos];
  2073. pItem->setGeometry(effectiveRect);
  2074. }
  2075. if (children.size())
  2076. {
  2077. // if we have any elements, the last element is top right aligned:
  2078. QLayoutItem* pItem = children[children.size() - 1];
  2079. QSize lastItemSize = pItem->minimumSize();
  2080. const int magicalRightEdgeOffset = pItem->widget()->style()->pixelMetric(QStyle::PM_ScrollBarExtent);
  2081. QRect topRightCorner(effectiveRect.topRight() - QPoint(lastItemSize.width() + magicalRightEdgeOffset, 0) + QPoint(-2, 2), lastItemSize);
  2082. if (pItem->geometry() != topRightCorner)
  2083. {
  2084. pItem->setGeometry(topRightCorner);
  2085. }
  2086. }
  2087. }
  2088. Qt::Orientations LUAEditorMainWindowLayout::expandingDirections() const
  2089. {
  2090. return Qt::Orientations();
  2091. }
  2092. QSize LUAEditorMainWindowLayout::sizeHint() const
  2093. {
  2094. return minimumSize();
  2095. }
  2096. QSize LUAEditorMainWindowLayout::minimumSize() const
  2097. {
  2098. QSize size;
  2099. int left, top, right, bottom;
  2100. getContentsMargins(&left, &top, &right, &bottom);
  2101. for (int pos = 0; pos < (int)children.size(); ++pos)
  2102. {
  2103. QLayoutItem* item = children[pos];
  2104. size = size.expandedTo(item->minimumSize());
  2105. }
  2106. size += (QSize(left + right, top + bottom));
  2107. return size;
  2108. }
  2109. }//namespace LUAEditor
  2110. #include <Source/LUA/moc_LUAEditorMainWindow.cpp>