LUAEditorMainWindow.cpp 90 KB

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