LUAEditorMainWindow.cpp 87 KB

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