LUAEditorMainWindow.cpp 88 KB

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