3
0

LUAEditorMainWindow.cpp 88 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407
  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->actionAssetBrowser, SIGNAL(triggered(bool)), this, SLOT(assetBrowserPressed()));
  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->actionAssetBrowser);
  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::assetBrowserPressed()
  671. {
  672. // w/out pulling in headers, AssetBrowserUI::AB_DEFAULT_CRC := 0
  673. //EditorFramework::ShowAssetBrowserView( AZ::ScriptAsset::StaticAssetType(), AZ_CRC("LUA CONTEXT", 0xec76567e) );
  674. //AZ_Assert(false, "No asset browser yet!");
  675. const char* rootDirString;
  676. AZ::ComponentApplicationBus::BroadcastResult(rootDirString, &AZ::ComponentApplicationBus::Events::GetExecutableFolder);
  677. QDir rootDir;
  678. rootDir.setPath(rootDirString);
  679. rootDir.cdUp();
  680. QString name = QFileDialog::getOpenFileName(this, "Open lua file", m_lastOpenFilePath.size() > 0 ? m_lastOpenFilePath.c_str() : rootDir.absolutePath(), "Lua files (*.lua)");
  681. if (name.isEmpty())
  682. {
  683. return;
  684. }
  685. AZStd::string assetId(name.toUtf8().data());
  686. Context_DocumentManagement::Bus::Broadcast(&Context_DocumentManagement::Bus::Events::OnLoadDocument, assetId, true);
  687. AzFramework::StringFunc::Path::Split(assetId.c_str(), nullptr, &m_lastOpenFilePath);
  688. }
  689. void LUAEditorMainWindow::OnFileMenuNew()
  690. {
  691. AZStd::string assetId;
  692. if (!OnFileSaveDialog("", assetId))
  693. {
  694. return;
  695. }
  696. if (AzFramework::StringFunc::Find(assetId.c_str(), ".lua") == AZStd::string::npos)
  697. {
  698. assetId += ".lua";
  699. }
  700. Context_DocumentManagement::Bus::Broadcast(&Context_DocumentManagement::Bus::Events::OnNewDocument, assetId);
  701. SetEditContolsToAtLeastOneFileOpen();
  702. }
  703. bool LUAEditorMainWindow::SyncDocumentToContext(const AZStd::string& assetId)
  704. {
  705. if (assetId.empty())
  706. {
  707. return false;
  708. }
  709. TrackedLUAViewMap::iterator viewInfoIter = m_dOpenLUAView.find(assetId);
  710. AZ_Assert(viewInfoIter != m_dOpenLUAView.end(), "OnFileMenuClose() : Cant find view Info.");
  711. TrackedLUAView& viewInfo = viewInfoIter->second;
  712. QByteArray viewBuffer = viewInfo.luaViewWidget()->GetText().toUtf8();
  713. AZStd::size_t viewSize = viewBuffer.size();
  714. Context_DocumentManagement::Bus::Broadcast(
  715. &Context_DocumentManagement::Bus::Events::UpdateDocumentData, assetId, viewBuffer.data(), viewSize);
  716. return true;
  717. }
  718. void LUAEditorMainWindow::OnFileMenuSave()
  719. {
  720. if (m_lastFocusedAssetId.empty())
  721. {
  722. return;
  723. }
  724. TrackedLUAViewMap::iterator viewInfoIter = m_dOpenLUAView.find(m_lastFocusedAssetId);
  725. AZ_Assert(viewInfoIter != m_dOpenLUAView.end(), "OnFileMenuSave() : Cant find view Info.");
  726. TrackedLUAView& viewInfo = viewInfoIter->second;
  727. //has the views text changed?
  728. if (viewInfo.luaViewWidget()->IsReadOnly())
  729. {
  730. AZ_Warning("LUA Editor", false, "Cannot save document - it is read-only (Check out first)");
  731. return;
  732. }
  733. if (SyncDocumentToContext(m_lastFocusedAssetId))
  734. {
  735. Context_DocumentManagement::Bus::Broadcast(
  736. &Context_DocumentManagement::Bus::Events::OnSaveDocument, m_lastFocusedAssetId, false, false);
  737. }
  738. }
  739. void LUAEditorMainWindow::OnFileMenuSaveAs()
  740. {
  741. if (m_lastFocusedAssetId.empty())
  742. {
  743. return;
  744. }
  745. if (SyncDocumentToContext(m_lastFocusedAssetId))
  746. {
  747. bool saveSuccess = false;
  748. Context_DocumentManagement::Bus::BroadcastResult(
  749. saveSuccess, &Context_DocumentManagement::Bus::Events::OnSaveDocumentAs, m_lastFocusedAssetId, false);
  750. }
  751. }
  752. void LUAEditorMainWindow::OnFileMenuSaveAll()
  753. {
  754. for (TrackedLUAViewMap::iterator viewInfoIter = m_dOpenLUAView.begin(); viewInfoIter != m_dOpenLUAView.end(); ++viewInfoIter)
  755. {
  756. TrackedLUAView& viewInfo = viewInfoIter->second;
  757. //has the views text changed?
  758. if (viewInfo.luaViewWidget()->IsReadOnly())
  759. {
  760. continue;
  761. }
  762. if (SyncDocumentToContext(viewInfo.luaViewWidget()->m_Info.m_assetId))
  763. {
  764. Context_DocumentManagement::Bus::Broadcast(
  765. &Context_DocumentManagement::Bus::Events::OnSaveDocument, viewInfo.luaViewWidget()->m_Info.m_assetId, false, false);
  766. }
  767. }
  768. }
  769. void LUAEditorMainWindow::OnFileMenuReload()
  770. {
  771. if (m_lastFocusedAssetId.empty())
  772. {
  773. return;
  774. }
  775. auto currentView = GetCurrentView();
  776. if (!currentView)
  777. {
  778. return;
  779. }
  780. // if this view has been modified, prompt to be sure they want to lose the changes
  781. TrackedLUAViewMap::iterator viewInfoIter = m_dOpenLUAView.find(m_lastFocusedAssetId);
  782. if (viewInfoIter == m_dOpenLUAView.end())
  783. {
  784. return; // no such view, dafuq?
  785. }
  786. TrackedLUAView& viewInfo = viewInfoIter->second;
  787. //has the views text changed?
  788. if (!viewInfo.luaViewWidget()->IsReadOnly() &&
  789. viewInfo.luaViewWidget()->IsModified())
  790. {
  791. QMessageBox msgBox;
  792. msgBox.setText("This file has been modified.\nDo you really want to Reload and lose changes?");
  793. msgBox.setInformativeText(currentView->m_Info.m_assetName.c_str());
  794. msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
  795. msgBox.setDefaultButton(QMessageBox::Cancel);
  796. msgBox.setIcon(QMessageBox::Warning);
  797. int ret = msgBox.exec();
  798. if (ret != QMessageBox::Ok)
  799. {
  800. return;
  801. }
  802. }
  803. // Need to store this off for use on the reload since it will be cleared out/changed as part of the OnCloseDocument call
  804. AZStd::string asset = m_lastFocusedAssetId;
  805. Context_DocumentManagement::Bus::Broadcast(&Context_DocumentManagement::Bus::Events::OnCloseDocument, m_lastFocusedAssetId);
  806. Context_DocumentManagement::Bus::Broadcast(&Context_DocumentManagement::Bus::Events::OnLoadDocument, asset, true);
  807. //not sure about this... looks like legacy, may need to be removed?
  808. // instate the topmost tab as the current asset ID
  809. // so that the window we're going to reopen has something to tabify onto
  810. TrackedLUACtrlTabOrder::iterator tabIter = m_CtrlTabOrder.begin();
  811. if (tabIter != m_CtrlTabOrder.end())
  812. {
  813. m_lastFocusedAssetId = *tabIter;
  814. }
  815. }
  816. void LUAEditorMainWindow::OnFileMenuClose()
  817. {
  818. if (m_lastFocusedAssetId.empty())
  819. {
  820. return;
  821. }
  822. RequestCloseDocument(m_lastFocusedAssetId);
  823. }
  824. void LUAEditorMainWindow::OnFileMenuCloseAll()
  825. {
  826. for (TrackedLUAViewMap::iterator it = m_dOpenLUAView.begin(); it != m_dOpenLUAView.end(); ++it)
  827. {
  828. AZ::SystemTickBus::QueueFunction(&LUAEditorMainWindow::RequestCloseDocument, this, it->first);
  829. }
  830. }
  831. void LUAEditorMainWindow::OnFileMenuCloseAllExcept()
  832. {
  833. for (TrackedLUAViewMap::iterator it = m_dOpenLUAView.begin(); it != m_dOpenLUAView.end(); ++it)
  834. {
  835. if (it->first != m_lastFocusedAssetId)
  836. {
  837. AZ::SystemTickBus::QueueFunction(&LUAEditorMainWindow::RequestCloseDocument, this, it->first);
  838. }
  839. }
  840. }
  841. bool LUAEditorMainWindow::RequestCloseDocument(const AZStd::string id)
  842. {
  843. AZStd::string assetId = id;
  844. //AZ_TracePrintf(LUAEditorDebugName, AZStd::string::format("OnFileMenuClose %s\n", assetId.c_str()).c_str());
  845. TrackedLUAViewMap::iterator viewInfoIter = m_dOpenLUAView.find(assetId);
  846. if (viewInfoIter == m_dOpenLUAView.end())
  847. {
  848. return true; // no such view, probably a double click on close.
  849. }
  850. TrackedLUAView& viewInfo = viewInfoIter->second;
  851. //has the views text changed?
  852. if (!viewInfo.luaViewWidget()->IsReadOnly() &&
  853. viewInfo.luaViewWidget()->IsModified())
  854. {
  855. AzToolsFramework::SaveChangesDialog dialog(this);
  856. dialog.exec();
  857. AzToolsFramework::SaveChangesDialogResult dr = dialog.m_result;
  858. if (dr == AzToolsFramework::SCDR_Save)
  859. {
  860. //the user wants to save before closing
  861. if (!SyncDocumentToContext(assetId))
  862. {
  863. return false;
  864. }
  865. Context_DocumentManagement::Bus::Broadcast(&Context_DocumentManagement::Bus::Events::OnSaveDocument, assetId, true, false);
  866. return true;
  867. }
  868. else if (dr == AzToolsFramework::SCDR_DiscardAndContinue)
  869. {
  870. //the user has chosen to continue to close the document and lose changes
  871. Context_DocumentManagement::Bus::Broadcast(&Context_DocumentManagement::Bus::Events::OnCloseDocument, assetId);
  872. return true;
  873. }
  874. else
  875. {
  876. //the user has canceled the close //or close was pressed
  877. return false;
  878. }
  879. }
  880. else
  881. {
  882. //no changes, just close the document
  883. Context_DocumentManagement::Bus::Broadcast(&Context_DocumentManagement::Bus::Events::OnCloseDocument, assetId);
  884. return true;
  885. }
  886. }
  887. void LUAEditorMainWindow::OnCloseView(const AZStd::string& assetId)
  888. {
  889. //remove it from the tracking list
  890. setAnimated(false);
  891. TrackedLUAViewMap::iterator viewIter = m_dOpenLUAView.find(assetId);
  892. if (viewIter != m_dOpenLUAView.end())
  893. {
  894. delete viewIter->second.luaDockWidget();
  895. m_dOpenLUAView.erase(viewIter);
  896. TrackedLUACtrlTabOrder::iterator tabIter = m_CtrlTabOrder.begin();
  897. while (tabIter != m_CtrlTabOrder.end())
  898. {
  899. if (*tabIter == assetId)
  900. {
  901. m_CtrlTabOrder.erase(tabIter);
  902. break;
  903. }
  904. ++tabIter;
  905. }
  906. }
  907. if (m_lastFocusedAssetId == assetId)
  908. {
  909. m_lastFocusedAssetId = "";
  910. }
  911. //if there are no open views then null out the last focused document it should be invalid now
  912. if (m_dOpenLUAView.empty())
  913. {
  914. m_lastFocusedAssetId = "";
  915. SetEditContolsToNoFilesOpen();
  916. AZ_TracePrintf(LUAEditorDebugName, AZStd::string::format("Last Focused Document ID to nullptr\n").c_str());
  917. }
  918. setAnimated(true);
  919. }
  920. void SendKeys(QWidget* pWidget, Qt::Key keyToSend, Qt::KeyboardModifier modifiers, QAction* pActionToDisable)
  921. {
  922. if (!pWidget)
  923. {
  924. return;
  925. }
  926. if (pActionToDisable)
  927. {
  928. pActionToDisable->setDisabled(true);
  929. }
  930. QKeyEvent PressIt(QEvent::KeyPress, keyToSend, modifiers);
  931. QKeyEvent ReleaseIt(QEvent::KeyRelease, keyToSend, modifiers);
  932. QApplication::sendEvent(pWidget, &PressIt);
  933. QApplication::sendEvent(pWidget, &ReleaseIt);
  934. if (pActionToDisable)
  935. {
  936. pActionToDisable->setDisabled(false);
  937. }
  938. }
  939. //edit menu
  940. void LUAEditorMainWindow::OnEditMenuUndo()
  941. {
  942. SendKeys(QApplication::focusWidget(), Qt::Key_Z, Qt::ControlModifier, m_gui->actionUndo);
  943. }
  944. void LUAEditorMainWindow::OnEditMenuRedo()
  945. {
  946. SendKeys(QApplication::focusWidget(), Qt::Key_Y, Qt::ControlModifier, m_gui->actionRedo);
  947. }
  948. void LUAEditorMainWindow::OnEditMenuCut()
  949. {
  950. auto currentView = GetCurrentView();
  951. if (!currentView)
  952. {
  953. return;
  954. }
  955. int lineFrom;
  956. int indexFrom;
  957. int lineTo;
  958. int indexTo;
  959. currentView->GetSelection(lineFrom, indexFrom, lineTo, indexTo);
  960. // on no selection
  961. if (lineFrom == -1)
  962. {
  963. // get the line and strip the whitespace
  964. currentView->GetCursorPosition(lineFrom, indexFrom);
  965. currentView->SetSelection(lineFrom, 0, lineFrom + 1, 0);
  966. QString cutThis = currentView->GetLineText(lineFrom);
  967. QString finalCut = cutThis.simplified();
  968. if (!finalCut.length())
  969. {
  970. // if the string is now empty, then it was all whitespace and so remove it but not to clipboard
  971. currentView->RemoveSelectedText();
  972. return;
  973. }
  974. }
  975. // drop through to standard cut to clipboard handling with the original or our newly selected line(s)
  976. currentView->Cut();
  977. }
  978. void LUAEditorMainWindow::OnEditMenuCopy()
  979. {
  980. auto currentView = GetCurrentView();
  981. if (!currentView)
  982. {
  983. return;
  984. }
  985. int lineFrom;
  986. int indexFrom;
  987. int lineTo;
  988. int indexTo;
  989. bool wasSelected = true;
  990. currentView->GetSelection(lineFrom, indexFrom, lineTo, indexTo);
  991. // on no selection, force this one line
  992. if (lineFrom == -1)
  993. {
  994. wasSelected = false;
  995. currentView->GetCursorPosition(lineFrom, indexFrom);
  996. currentView->SetSelection(lineFrom, 0, lineFrom + 1, 0);
  997. }
  998. currentView->Copy();
  999. if (!wasSelected)
  1000. {
  1001. currentView->SetCursorPosition(lineFrom, indexFrom);
  1002. }
  1003. }
  1004. void LUAEditorMainWindow::OnEditMenuPaste()
  1005. {
  1006. SendKeys(QApplication::focusWidget(), Qt::Key_V, Qt::ControlModifier, m_gui->actionPaste);
  1007. }
  1008. void LUAEditorMainWindow::OnEditMenuFind()
  1009. {
  1010. m_ptrFindDialog->SaveState();
  1011. m_ptrFindDialog->show();
  1012. m_ptrFindDialog->SetAnyDocumentsOpen(m_StateTrack.atLeastOneFileOpen);
  1013. m_ptrFindDialog->SetToFindInAllOpen(false);
  1014. m_ptrFindDialog->SetNewSearchStarting();
  1015. m_ptrFindDialog->ResetSearch();
  1016. m_ptrFindDialog->activateWindow();
  1017. m_ptrFindDialog->raise();
  1018. }
  1019. void LUAEditorMainWindow::OnEditMenuReplace()
  1020. {
  1021. m_ptrFindDialog->SaveState();
  1022. m_ptrFindDialog->show();
  1023. m_ptrFindDialog->SetAnyDocumentsOpen(m_StateTrack.atLeastOneFileOpen);
  1024. m_ptrFindDialog->SetToFindInAllOpen(false);
  1025. m_ptrFindDialog->SetNewSearchStarting();
  1026. m_ptrFindDialog->ResetSearch();
  1027. m_ptrFindDialog->activateWindow();
  1028. m_ptrFindDialog->raise();
  1029. }
  1030. void LUAEditorMainWindow::OnEditMenuFindInAllOpen()
  1031. {
  1032. m_ptrFindDialog->SaveState();
  1033. m_ptrFindDialog->show();
  1034. m_ptrFindDialog->SetAnyDocumentsOpen(m_StateTrack.atLeastOneFileOpen);
  1035. m_ptrFindDialog->SetToFindInAllOpen(true);
  1036. m_ptrFindDialog->SetNewSearchStarting();
  1037. m_ptrFindDialog->ResetSearch();
  1038. m_ptrFindDialog->activateWindow();
  1039. m_ptrFindDialog->raise();
  1040. }
  1041. void LUAEditorMainWindow::OnEditMenuReplaceInAllOpen()
  1042. {
  1043. m_ptrFindDialog->SaveState();
  1044. m_ptrFindDialog->show();
  1045. m_ptrFindDialog->SetAnyDocumentsOpen(m_StateTrack.atLeastOneFileOpen);
  1046. m_ptrFindDialog->SetToFindInAllOpen(true);
  1047. m_ptrFindDialog->SetNewSearchStarting();
  1048. m_ptrFindDialog->ResetSearch();
  1049. m_ptrFindDialog->activateWindow();
  1050. m_ptrFindDialog->raise();
  1051. }
  1052. void LUAEditorMainWindow::OnEditMenuFindLocal()
  1053. {
  1054. m_ptrFindDialog->SaveState();
  1055. m_ptrFindDialog->SetAnyDocumentsOpen(m_StateTrack.atLeastOneFileOpen);
  1056. m_ptrFindDialog->SetToFindInAllOpen(false);
  1057. m_ptrFindDialog->SetNewSearchStarting(true, true);
  1058. m_ptrFindDialog->OnFindNext();
  1059. }
  1060. void LUAEditorMainWindow::OnEditMenuFindLocalReverse()
  1061. {
  1062. m_ptrFindDialog->SaveState();
  1063. m_ptrFindDialog->SetAnyDocumentsOpen(m_StateTrack.atLeastOneFileOpen);
  1064. m_ptrFindDialog->SetToFindInAllOpen(false);
  1065. m_ptrFindDialog->SetNewSearchStarting(true, false);
  1066. m_ptrFindDialog->OnFindNext();
  1067. }
  1068. void LUAEditorMainWindow::OnEditMenuFindNext()
  1069. {
  1070. if (m_ptrFindDialog)
  1071. {
  1072. m_ptrFindDialog->OnFindNext();
  1073. }
  1074. }
  1075. void LUAEditorMainWindow::OnEditMenuGoToLine()
  1076. {
  1077. auto currentView = GetCurrentView();
  1078. if (!currentView)
  1079. {
  1080. return;
  1081. }
  1082. LUAEditorGoToLineDialog dlg(this);
  1083. int lineNumber = 0, cursorColumn = 0;
  1084. currentView->GetCursorPosition(lineNumber, cursorColumn);
  1085. dlg.setLineNumber(lineNumber + 1);
  1086. if (dlg.exec() != QDialog::Rejected)
  1087. {
  1088. // go to that line of the selected file.
  1089. lineNumber = dlg.getLineNumber();
  1090. currentView->SetCursorPosition(lineNumber, 0);
  1091. }
  1092. }
  1093. void LUAEditorMainWindow::OnEditMenuFoldAll()
  1094. {
  1095. if (auto currentView = GetCurrentView())
  1096. {
  1097. currentView->FoldAll();
  1098. }
  1099. }
  1100. void LUAEditorMainWindow::OnEditMenuUnfoldAll()
  1101. {
  1102. if (auto currentView = GetCurrentView())
  1103. {
  1104. currentView->UnfoldAll();
  1105. }
  1106. }
  1107. void LUAEditorMainWindow::OnEditMenuSelectAll()
  1108. {
  1109. if (auto currentView = GetCurrentView())
  1110. {
  1111. currentView->SelectAll();
  1112. }
  1113. }
  1114. void LUAEditorMainWindow::OnEditMenuSelectToBrace()
  1115. {
  1116. if (auto currentView = GetCurrentView())
  1117. {
  1118. currentView->SelectToMatchingBrace();
  1119. }
  1120. }
  1121. void LUAEditorMainWindow::OnCommentSelectedBlock()
  1122. {
  1123. if (NeedsCheckout())
  1124. {
  1125. return;
  1126. }
  1127. if (auto currentView = GetCurrentView())
  1128. {
  1129. // must check read-only status at every operation that can modify the buffer
  1130. if (!currentView->IsReadOnly())
  1131. {
  1132. currentView->CommentSelectedLines();
  1133. }
  1134. }
  1135. }
  1136. void LUAEditorMainWindow::OnUnCommentSelectedBlock()
  1137. {
  1138. if (NeedsCheckout())
  1139. {
  1140. return;
  1141. }
  1142. if (auto currentView = GetCurrentView())
  1143. {
  1144. // must check read-only status at every operation that can modify the buffer
  1145. if (!currentView->IsReadOnly())
  1146. {
  1147. currentView->UncommentSelectedLines();
  1148. }
  1149. }
  1150. }
  1151. bool LUAEditorMainWindow::NeedsCheckout()
  1152. {
  1153. auto currentView = GetCurrentView();
  1154. if (!currentView)
  1155. {
  1156. return false;
  1157. }
  1158. if (currentView->IsReadOnly())
  1159. {
  1160. QMessageBox msgBox;
  1161. msgBox.setText("Checkout This File To Edit?");
  1162. msgBox.setInformativeText(currentView->m_Info.m_assetName.c_str());
  1163. msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
  1164. msgBox.setDefaultButton(QMessageBox::Cancel);
  1165. msgBox.setIcon(QMessageBox::Warning);
  1166. int ret = msgBox.exec();
  1167. if (ret == QMessageBox::Ok)
  1168. {
  1169. OnRequestCheckOut(currentView->m_Info.m_assetId);
  1170. }
  1171. return true;
  1172. }
  1173. return false;
  1174. }
  1175. void LUAEditorMainWindow::OnEditMenuTransposeUp()
  1176. {
  1177. if (NeedsCheckout())
  1178. {
  1179. return;
  1180. }
  1181. // must check read-only status at every operation that can modify the buffer
  1182. if (auto currentView = GetCurrentView())
  1183. {
  1184. if (!currentView->IsReadOnly())
  1185. {
  1186. currentView->MoveSelectedLinesUp();
  1187. }
  1188. }
  1189. }
  1190. void LUAEditorMainWindow::OnEditMenuTransposeDn()
  1191. {
  1192. if (NeedsCheckout())
  1193. {
  1194. return;
  1195. }
  1196. // must check read-only status at every operation that can modify the buffer
  1197. if (auto currentView = GetCurrentView())
  1198. {
  1199. if (!currentView->IsReadOnly())
  1200. {
  1201. currentView->MoveSelectedLinesDn();
  1202. }
  1203. }
  1204. }
  1205. //view menu
  1206. void LUAEditorMainWindow::OnViewMenuBreakpoints()
  1207. {
  1208. OnOpenBreakpointsView();
  1209. }
  1210. void LUAEditorMainWindow::OnViewMenuStack()
  1211. {
  1212. OnOpenStackView();
  1213. }
  1214. void LUAEditorMainWindow::OnViewMenuLocals()
  1215. {
  1216. OnOpenLocalsView();
  1217. }
  1218. void LUAEditorMainWindow::OnViewMenuWatch()
  1219. {
  1220. OnOpenWatchView();
  1221. }
  1222. void LUAEditorMainWindow::OnViewMenuReference()
  1223. {
  1224. OnOpenReferenceView();
  1225. }
  1226. void LUAEditorMainWindow::OnViewMenuFind1()
  1227. {
  1228. OnOpenFindView(0);
  1229. }
  1230. void LUAEditorMainWindow::OnViewMenuFind2()
  1231. {
  1232. OnOpenFindView(1);
  1233. }
  1234. void LUAEditorMainWindow::OnViewMenuFind3()
  1235. {
  1236. OnOpenFindView(2);
  1237. }
  1238. void LUAEditorMainWindow::OnViewMenuFind4()
  1239. {
  1240. OnOpenFindView(3);
  1241. }
  1242. void LUAEditorMainWindow::OnViewMenuResetZoom()
  1243. {
  1244. if (auto currentView = GetCurrentView())
  1245. {
  1246. currentView->ResetZoom();
  1247. }
  1248. }
  1249. //source control menu
  1250. void LUAEditorMainWindow::OnSourceControlMenuCheckOut()
  1251. {
  1252. // if no last document id then return
  1253. if (m_lastFocusedAssetId.empty())
  1254. {
  1255. return;
  1256. }
  1257. Context_DocumentManagement::Bus::Broadcast(&Context_DocumentManagement::Bus::Events::RefreshAllDocumentPerforceStat);
  1258. if (!SyncDocumentToContext(m_lastFocusedAssetId))
  1259. {
  1260. AZ_Warning(LUAEditorDebugName, false, "Could not sync doc data before checkout, data may be lost.");
  1261. QMessageBox::warning(this, "Error!", "Could not sync document before checkout!");
  1262. return;
  1263. }
  1264. Context_DocumentManagement::Bus::Broadcast(
  1265. &Context_DocumentManagement::Bus::Events::DocumentCheckOutRequested, m_lastFocusedAssetId);
  1266. }
  1267. //tools menu
  1268. // when the Editor Main window is requested to close, it is not destroyed.
  1269. //////////////////////////////////////////////////////////////////////////
  1270. // Qt Events
  1271. void LUAEditorMainWindow::closeEvent(QCloseEvent* event)
  1272. {
  1273. OnMenuCloseCurrentWindow();
  1274. event->ignore();
  1275. }
  1276. bool LUAEditorMainWindow::OnGetPermissionToShutDown()
  1277. {
  1278. bool willShutDown = true;
  1279. AZ_TracePrintf(LUAEditorDebugName, "LUAEditorMainWindow::OnGetPermissionToShutDown()\n");
  1280. for (TrackedLUAViewMap::iterator viewInfoIter = m_dOpenLUAView.begin(); viewInfoIter != m_dOpenLUAView.end(); ++viewInfoIter)
  1281. {
  1282. TrackedLUAView& viewInfo = viewInfoIter->second;
  1283. //have the views' text changed?
  1284. if (!viewInfo.luaViewWidget()->IsReadOnly() &&
  1285. viewInfo.luaViewWidget()->IsModified())
  1286. {
  1287. this->show();
  1288. viewInfo.luaDockWidget()->show();
  1289. viewInfo.luaDockWidget()->raise();
  1290. AzToolsFramework::SaveChangesDialog dialog(this);
  1291. dialog.exec();
  1292. AzToolsFramework::SaveChangesDialogResult dr = dialog.m_result;
  1293. if (dr == AzToolsFramework::SCDR_Save)
  1294. {
  1295. if (!SyncDocumentToContext(viewInfo.luaViewWidget()->m_Info.m_assetId))
  1296. {
  1297. AZ_Warning(LUAEditorDebugName, false, "Could not sync doc data before closing it, data may be lost.");
  1298. willShutDown = false;
  1299. AZ_TracePrintf(LUAEditorInfoName, " SyncDocumentToContext() failure\n");
  1300. break;
  1301. }
  1302. AZ_TracePrintf(LUAEditorDebugName, "LUAEditorMainWindow::OnGetPermissionToShutDown() SAVING %s\n", viewInfo.luaViewWidget()->m_Info.m_assetName.c_str());
  1303. Context_DocumentManagement::Bus::Broadcast(
  1304. &Context_DocumentManagement::Bus::Events::OnSaveDocument, viewInfo.luaViewWidget()->m_Info.m_assetId, false, false);
  1305. }
  1306. else if (dr == AzToolsFramework::SCDR_DiscardAndContinue)
  1307. {
  1308. //the user has chosen to continue and lose changes
  1309. if (viewInfo.luaViewWidget()->m_Info.m_bUntitledDocument)
  1310. {
  1311. AZ_TracePrintf(LUAEditorDebugName, " Forced close\n");
  1312. // all untitled documents are force closed to clear any tracked states that will be serialized by their trackers
  1313. Context_DocumentManagement::Bus::Broadcast(
  1314. &Context_DocumentManagement::Bus::Events::OnCloseDocument, viewInfo.luaViewWidget()->m_Info.m_assetId);
  1315. viewInfoIter = m_dOpenLUAView.begin();
  1316. }
  1317. else // all titled (i.e. preexisting or saved files, simply reload
  1318. {
  1319. AZ_TracePrintf(LUAEditorDebugName, " Forced reload\n");
  1320. Context_DocumentManagement::Bus::Broadcast(
  1321. &Context_DocumentManagement::Bus::Events::OnReloadDocument, viewInfo.luaViewWidget()->m_Info.m_assetId);
  1322. viewInfoIter = m_dOpenLUAView.begin();
  1323. }
  1324. }
  1325. else
  1326. {
  1327. //the user has canceled the close
  1328. willShutDown = false;
  1329. break;
  1330. }
  1331. }
  1332. }
  1333. return willShutDown;
  1334. }
  1335. void LUAEditorMainWindow::SaveWindowState()
  1336. {
  1337. // build state and store it.
  1338. auto newState = AZ::UserSettings::CreateFind<LUAEditorMainWindowSavedState>(AZ_CRC("LUA EDITOR MAIN WINDOW STATE", 0xa181bc4a), AZ::UserSettings::CT_LOCAL);
  1339. newState->Init(saveState(), saveGeometry());
  1340. newState->m_bAutocompleteEnabled = m_bAutocompleteEnabled;
  1341. // gather and store the open files in tab order
  1342. newState->m_openAssetIds.clear();
  1343. // the following is experimental, apparently widget child order doesn't match tab order, which is unfortunate
  1344. QList<QDockWidget*> dockWidgets = qobject_cast<QMainWindow*>(this->centralWidget())->findChildren<QDockWidget*>();
  1345. QList<QDockWidget*>::iterator qlit = dockWidgets.begin();
  1346. while (qlit != dockWidgets.end())
  1347. {
  1348. LUADockWidget* ldw = qobject_cast<LUADockWidget*>(*qlit);
  1349. if (ldw)
  1350. {
  1351. TrackedLUAViewMap::iterator viewInfoIter = m_dOpenLUAView.find(ldw->assetId());
  1352. if (viewInfoIter != m_dOpenLUAView.end())
  1353. {
  1354. TrackedLUAView& viewInfo = viewInfoIter->second;
  1355. newState->m_openAssetIds.push_back(viewInfo.luaViewWidget()->m_Info.m_assetId);
  1356. AZ_TracePrintf(LUAEditorDebugName, " - TAB Saved %s\n", viewInfo.luaViewWidget()->m_Info.m_assetName.c_str());
  1357. }
  1358. }
  1359. qlit++;
  1360. }
  1361. m_gui->m_logPanel->SaveState();
  1362. auto savedState = AZ::UserSettings::CreateFind<AzToolsFramework::MainWindowSavedState>(AZ_CRC("INNER_LUA_WINDOW", 0x52741396), AZ::UserSettings::CT_LOCAL);
  1363. if (savedState)
  1364. {
  1365. // restore state.
  1366. QMainWindow* pMainWindow = static_cast<QMainWindow*>(centralWidget());
  1367. savedState->Init(pMainWindow->saveState(), pMainWindow->saveGeometry());
  1368. }
  1369. }
  1370. void LUAEditorMainWindow::OnLogTabsReset()
  1371. {
  1372. m_gui->m_logPanel->AddLogTab(AzToolsFramework::LogPanel::TabSettings("Lua Editor", "Lua Editor", ""));
  1373. }
  1374. void LUAEditorMainWindow::RestoreWindowState() // call this after you have rebuilt everything.
  1375. {
  1376. if (!m_gui->m_logPanel->LoadState())
  1377. {
  1378. OnLogTabsReset();
  1379. }
  1380. // load the state from our state block:
  1381. auto pEditorMainSavedState = AZ::UserSettings::Find<LUAEditorMainWindowSavedState>(AZ_CRC("LUA EDITOR MAIN WINDOW STATE", 0xa181bc4a), AZ::UserSettings::CT_LOCAL);
  1382. if (pEditorMainSavedState)
  1383. {
  1384. QByteArray editorGeomData((const char*)pEditorMainSavedState->m_windowGeometry.data(), (int)pEditorMainSavedState->m_windowGeometry.size());
  1385. QByteArray editorStateData((const char*)pEditorMainSavedState->GetWindowState().data(), (int)pEditorMainSavedState->GetWindowState().size());
  1386. for (const auto& assetId : pEditorMainSavedState->m_openAssetIds)
  1387. {
  1388. Context_DocumentManagement::Bus::Broadcast(&Context_DocumentManagement::Bus::Events::OnLoadDocument, assetId, false);
  1389. }
  1390. restoreGeometry(editorGeomData);
  1391. if (this->isMaximized())
  1392. {
  1393. this->showNormal();
  1394. this->showMaximized();
  1395. }
  1396. restoreState(editorStateData);
  1397. m_bAutocompleteEnabled = pEditorMainSavedState->m_bAutocompleteEnabled;
  1398. OnAutocompleteChanged(m_bAutocompleteEnabled);
  1399. auto pWindowSavedState = AZ::UserSettings::Find<AzToolsFramework::MainWindowSavedState>(AZ_CRC("INNER_LUA_WINDOW", 0x52741396), AZ::UserSettings::CT_LOCAL);
  1400. if (pWindowSavedState)
  1401. {
  1402. // restore state.
  1403. QByteArray windowGeomData((const char*)pWindowSavedState->m_windowGeometry.data(), (int)pWindowSavedState->m_windowGeometry.size());
  1404. QByteArray windowStateData((const char*)pWindowSavedState->GetWindowState().data(), (int)pWindowSavedState->GetWindowState().size());
  1405. QMainWindow* pMainWindow = static_cast<QMainWindow*>(centralWidget());
  1406. pMainWindow->restoreState(windowStateData);
  1407. }
  1408. }
  1409. else
  1410. {
  1411. // default state!
  1412. }
  1413. }
  1414. LUAViewWidget* LUAEditorMainWindow::GetCurrentView()
  1415. {
  1416. if (m_lastFocusedAssetId.empty())
  1417. {
  1418. return nullptr;
  1419. }
  1420. TrackedLUAViewMap::iterator viewInfoIter = m_dOpenLUAView.find(m_lastFocusedAssetId);
  1421. AZ_Assert(viewInfoIter != m_dOpenLUAView.end(), "OnFileMenuClose() : Cant find view Info.");
  1422. TrackedLUAView& viewInfo = viewInfoIter->second;
  1423. return viewInfo.luaViewWidget();
  1424. }
  1425. AZStd::vector<LUAViewWidget*> LUAEditorMainWindow::GetAllViews()
  1426. {
  1427. AZStd::vector<LUAViewWidget*> dViews;
  1428. for (TrackedLUAViewMap::iterator viewInfoIter = m_dOpenLUAView.begin(); viewInfoIter != m_dOpenLUAView.end(); ++viewInfoIter)
  1429. {
  1430. dViews.push_back(viewInfoIter->second.luaViewWidget());
  1431. }
  1432. return dViews;
  1433. }
  1434. FindResults* LUAEditorMainWindow::GetFindResultsWidget(int index)
  1435. {
  1436. switch (index)
  1437. {
  1438. case 0:
  1439. return m_gui->m_findResults1;
  1440. case 1:
  1441. return m_gui->m_findResults2;
  1442. case 2:
  1443. return m_gui->m_findResults3;
  1444. case 3:
  1445. return m_gui->m_findResults4;
  1446. }
  1447. return nullptr;
  1448. }
  1449. void LUAEditorMainWindow::SetCurrentFindListWidget(int index)
  1450. {
  1451. AZ_Assert(index >= 0 && index < 4, "Only 4 find windows currently");
  1452. m_gui->findTabWidget->setCurrentIndex(index);
  1453. }
  1454. void LUAEditorMainWindow::OnFindResultClicked(FindResultsBlockInfo result)
  1455. {
  1456. if (OnRequestFocusView(result.m_assetId))
  1457. {
  1458. LUAViewWidget* pLUAViewWidget = GetCurrentView();
  1459. if (!pLUAViewWidget)
  1460. {
  1461. return;
  1462. }
  1463. pLUAViewWidget->SetCursorPosition(result.m_lineNumber, result.m_firstMatchPosition);
  1464. }
  1465. else
  1466. {
  1467. // the document was probably closed, request it be reopened
  1468. m_dProcessFindListClicked.push_back(result);
  1469. AZ_Assert(false, "Fix assets!");
  1470. }
  1471. }
  1472. void LUAEditorMainWindow::OnDataLoadedAndSet(const DocumentInfo& info, LUAViewWidget* pLUAViewWidget)
  1473. {
  1474. for (auto iter = m_dProcessFindListClicked.begin(); iter != m_dProcessFindListClicked.end(); ++iter)
  1475. {
  1476. if (iter->m_assetId == info.m_assetId)
  1477. {
  1478. AZ_Assert(iter->m_assignAssetId, "m_assignAssetId was never set");
  1479. iter->m_assetId = pLUAViewWidget->m_Info.m_assetId;
  1480. iter->m_assignAssetId(info.m_assetName, pLUAViewWidget->m_Info.m_assetId);
  1481. AZ::SystemTickBus::QueueFunction(&LUAEditorMainWindow::OnFindResultClicked, this, *iter);
  1482. m_dProcessFindListClicked.erase(iter);
  1483. return;
  1484. }
  1485. }
  1486. }
  1487. bool LUAEditorMainWindow::OnFileSaveDialog(const AZStd::string& assetName, AZStd::string& newAssetName)
  1488. {
  1489. const QDir rootDir { AZ::Utils::GetProjectPath().c_str() };
  1490. 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"));
  1491. if (name.isEmpty())
  1492. {
  1493. return false;
  1494. }
  1495. AzFramework::StringFunc::Path::Split(name.toUtf8().data(), nullptr, &m_lastOpenFilePath);
  1496. newAssetName = name.toUtf8().data();
  1497. return true;
  1498. }
  1499. bool LUAEditorMainWindow::OnFileSaveAsDialog(const AZStd::string& assetName, AZStd::string& newAssetName)
  1500. {
  1501. const char* rootDirString;
  1502. AZ::ComponentApplicationBus::BroadcastResult(rootDirString, &AZ::ComponentApplicationBus::Events::GetExecutableFolder);
  1503. QDir rootDir;
  1504. rootDir.setPath(rootDirString);
  1505. rootDir.cdUp();
  1506. QString name = QFileDialog::getSaveFileName(this, QString(AZStd::string::format("Save File As {%s}", assetName.c_str()).c_str()), rootDir.absolutePath(), QString("*.lua"));
  1507. if (name.isEmpty())
  1508. {
  1509. return false;
  1510. }
  1511. //name has the full path in it, we need to convert it to an asset name
  1512. AZStd::string projectRoot, databaseRoot, databasePath, databaseFile, fileExtension;
  1513. if (!AzFramework::StringFunc::AssetDatabasePath::Split(name.toUtf8().data(), &projectRoot, &databaseRoot, &databasePath, &databaseFile, &fileExtension))
  1514. {
  1515. AZ_Warning("LUAEditorMainWindow", false, AZStd::string::format("<span severity=\"err\">Path is invalid: '%s'</span>", name.toUtf8().data()).c_str());
  1516. return false;
  1517. }
  1518. AzFramework::StringFunc::AssetDatabasePath::Join(databasePath.c_str(), databaseFile.c_str(), newAssetName);
  1519. return true;
  1520. }
  1521. //////////////////////////////////////////////////////////////////////////
  1522. // LUAEditorMainWindow Messages
  1523. void LUAEditorMainWindow::OnFocusInEvent(const AZStd::string& assetId)
  1524. {
  1525. m_lastFocusedAssetId = assetId;
  1526. if (!m_bIgnoreFocusRequests)
  1527. {
  1528. SetGUIToMatch(m_StateTrack);
  1529. }
  1530. }
  1531. void LUAEditorMainWindow::OnFocusOutEvent(const AZStd::string&)
  1532. {
  1533. }
  1534. void LUAEditorMainWindow::OnRequestCheckOut(const AZStd::string& assetId)
  1535. {
  1536. AZStd::string restoreAssetId = m_lastFocusedAssetId;
  1537. m_lastFocusedAssetId = assetId;
  1538. OnSourceControlMenuCheckOut();
  1539. m_lastFocusedAssetId = restoreAssetId;
  1540. }
  1541. //////////////////////////////////////////////////////////////////////////
  1542. bool LUAEditorMainWindow::OnRequestFocusView(const AZStd::string& assetId)
  1543. {
  1544. TrackedLUAViewMap::iterator viewIter = m_dOpenLUAView.find(assetId);
  1545. if (viewIter != m_dOpenLUAView.end())
  1546. {
  1547. viewIter->second.luaDockWidget()->show();
  1548. viewIter->second.luaDockWidget()->raise();
  1549. viewIter->second.luaViewWidget()->RegainFocusFinal();
  1550. return true;
  1551. }
  1552. return false;
  1553. }
  1554. void LUAEditorMainWindow::OnDocumentInfoUpdated(const DocumentInfo& docInfo)
  1555. {
  1556. // document has fresh information available (it was checked out or its data finished or etc)
  1557. TrackedLUAViewMap::iterator viewIter = m_dOpenLUAView.find(docInfo.m_assetId);
  1558. if (viewIter != m_dOpenLUAView.end())
  1559. {
  1560. viewIter->second.luaViewWidget()->OnDocumentInfoUpdated(docInfo);
  1561. }
  1562. }
  1563. // preliminaries for making the debug action buttons context sensitive
  1564. void LUAEditorMainWindow::BreakpointsUpdate(const LUAEditor::BreakpointMap& uniqueBreakpoints)
  1565. {
  1566. (void)uniqueBreakpoints;
  1567. }
  1568. void LUAEditorMainWindow::BreakpointHit(const LUAEditor::Breakpoint& breakpoint)
  1569. {
  1570. (void)breakpoint;
  1571. SetDebugControlsToAtBreak();
  1572. }
  1573. void LUAEditorMainWindow::BreakpointResume()
  1574. {
  1575. SetDebugControlsToRunning();
  1576. }
  1577. //////////////////////////////////////////////////////////////////////////
  1578. // externally driven context sensitive widget states
  1579. void LUAEditorMainWindow::SetDebugControlsToInitial()
  1580. {
  1581. m_StateTrack.Init();
  1582. SetGUIToMatch(m_StateTrack);
  1583. }
  1584. void LUAEditorMainWindow::SetDebugControlsToRunning()
  1585. {
  1586. AZ_TracePrintf(LUAEditorDebugName, "LUAEditorMainWindow::SetDebugControlsToRunning()\n");
  1587. m_StateTrack.scriptRunning = true;
  1588. m_StateTrack.atBreak = false;
  1589. m_StateTrack.hasExecuted = true;
  1590. SetGUIToMatch(m_StateTrack);
  1591. if (!m_lastFocusedAssetId.empty())
  1592. {
  1593. TrackedLUAViewMap::iterator viewInfoIter = m_dOpenLUAView.find(m_lastFocusedAssetId);
  1594. AZ_Assert(viewInfoIter != m_dOpenLUAView.end(), "OnFileMenuClose() : Cant find view Info.");
  1595. TrackedLUAView& viewInfo = viewInfoIter->second;
  1596. viewInfo.luaViewWidget()->UpdateCurrentExecutingLine(-1);
  1597. }
  1598. LUAEditor::LUAStackTrackerMessages::Bus::Broadcast(&LUAEditor::LUAStackTrackerMessages::Bus::Events::StackClear);
  1599. }
  1600. void LUAEditorMainWindow::SetDebugControlsToAtBreak()
  1601. {
  1602. AZ_TracePrintf(LUAEditorDebugName, "LUAEditorMainWindow::SetDebugControlsToAtBreak()\n");
  1603. m_StateTrack.scriptRunning = false;
  1604. m_StateTrack.atBreak = true;
  1605. m_StateTrack.hasExecuted = true;
  1606. SetGUIToMatch(m_StateTrack);
  1607. }
  1608. void LUAEditorMainWindow::SetEditContolsToNoFilesOpen()
  1609. {
  1610. m_StateTrack.atLeastOneFileOpen = false;
  1611. SetGUIToMatch(m_StateTrack);
  1612. }
  1613. void LUAEditorMainWindow::SetEditContolsToAtLeastOneFileOpen()
  1614. {
  1615. m_StateTrack.atLeastOneFileOpen = true;
  1616. SetGUIToMatch(m_StateTrack);
  1617. }
  1618. void LUAEditorMainWindow::luaClassFilterTextChanged(const QString& newPattern)
  1619. {
  1620. m_ClassReferenceFilter->SetFilter(newPattern);
  1621. if (newPattern.isEmpty())
  1622. {
  1623. m_gui->classReferenceTreeView->collapseAll();
  1624. }
  1625. else
  1626. {
  1627. m_gui->classReferenceTreeView->expandAll();
  1628. }
  1629. }
  1630. void LUAEditorMainWindow::OnConnectedToTarget()
  1631. {
  1632. AZ_TracePrintf(LUAEditorDebugName, "LUAEditorMainWindow::OnConnectedToTarget()\n");
  1633. m_StateTrack.targetConnected = true;
  1634. m_StateTrack.debuggerAttached = false;
  1635. m_StateTrack.scriptRunning = false;
  1636. m_StateTrack.atBreak = false;
  1637. m_StateTrack.hasExecuted = false;
  1638. SetGUIToMatch(m_StateTrack);
  1639. }
  1640. void LUAEditorMainWindow::OnDisconnectedFromTarget()
  1641. {
  1642. AZ_TracePrintf(LUAEditorDebugName, "LUAEditorMainWindow::OnDisconnectedFromTarget()\n");
  1643. m_StateTrack.targetConnected = false;
  1644. m_StateTrack.debuggerAttached = false;
  1645. m_StateTrack.scriptRunning = false;
  1646. m_StateTrack.atBreak = false;
  1647. m_StateTrack.hasExecuted = false;
  1648. SetGUIToMatch(m_StateTrack);
  1649. }
  1650. void LUAEditorMainWindow::OnConnectedToDebugger()
  1651. {
  1652. AZ_TracePrintf(LUAEditorDebugName, "LUAEditorMainWindow::OnConnectedToDebugger()\n");
  1653. m_StateTrack.debuggerAttached = true;
  1654. m_StateTrack.scriptRunning = false;
  1655. m_StateTrack.atBreak = false;
  1656. m_StateTrack.hasExecuted = false;
  1657. SetGUIToMatch(m_StateTrack);
  1658. }
  1659. void LUAEditorMainWindow::OnDisconnectedFromDebugger()
  1660. {
  1661. AZ_TracePrintf(LUAEditorDebugName, "LUAEditorMainWindow::OnDisconnectedFromDebugger()\n");
  1662. m_StateTrack.debuggerAttached = false;
  1663. m_StateTrack.scriptRunning = false;
  1664. m_StateTrack.atBreak = false;
  1665. m_StateTrack.hasExecuted = false;
  1666. SetGUIToMatch(m_StateTrack);
  1667. }
  1668. void LUAEditorMainWindow::Repaint()
  1669. {
  1670. SetGUIToMatch(m_StateTrack);
  1671. const AZStd::vector<LUAViewWidget*>& allViews = GetAllViews();
  1672. for (LUAViewWidget* view : allViews)
  1673. {
  1674. view->UpdateFont();
  1675. }
  1676. }
  1677. void LUAEditorMainWindow::OnExecuteScriptResult(bool success)
  1678. {
  1679. if (success)
  1680. {
  1681. m_StateTrack.hasExecuted = true;
  1682. SetDebugControlsToRunning();
  1683. }
  1684. }
  1685. void LUAEditorMainWindow::SetGUIToMatch(StateTrack& track)
  1686. {
  1687. if (track.atLeastOneFileOpen)
  1688. {
  1689. m_gui->actionSave->setEnabled(true);
  1690. m_gui->actionClose->setEnabled(true);
  1691. m_gui->actionSaveAll->setEnabled(true);
  1692. m_gui->actionUndo->setEnabled(true);
  1693. m_gui->actionRedo->setEnabled(true);
  1694. m_gui->actionCut->setEnabled(true);
  1695. m_gui->actionCopy->setEnabled(true);
  1696. m_gui->actionPaste->setEnabled(true);
  1697. m_gui->actionSaveAs->setEnabled(true);
  1698. m_gui->actionCheckOut->setEnabled(true);
  1699. m_gui->actionGoToLine->setEnabled(true);
  1700. m_gui->action_execute->setEnabled(true);
  1701. m_gui->action_togglebreak->setEnabled(true);
  1702. }
  1703. else
  1704. {
  1705. m_gui->actionSave->setEnabled(false);
  1706. m_gui->actionClose->setEnabled(false);
  1707. m_gui->actionSaveAll->setEnabled(false);
  1708. m_gui->actionUndo->setEnabled(false);
  1709. m_gui->actionRedo->setEnabled(false);
  1710. m_gui->actionCut->setEnabled(false);
  1711. m_gui->actionCopy->setEnabled(false);
  1712. m_gui->actionPaste->setEnabled(false);
  1713. m_gui->actionSaveAs->setEnabled(false);
  1714. m_gui->actionCheckOut->setEnabled(false);
  1715. m_gui->actionGoToLine->setEnabled(false);
  1716. m_gui->action_execute->setEnabled(false);
  1717. m_gui->action_togglebreak->setEnabled(false);
  1718. }
  1719. // special handling for the watches
  1720. m_gui->watchDockWidget->setEnabled(false);
  1721. if (track.debuggerAttached)
  1722. {
  1723. m_gui->watchDockWidget->setEnabled(true);
  1724. }
  1725. if ((!track.targetConnected) || (!track.debuggerAttached))
  1726. {
  1727. // turn off any little yellow arrows (QScintilla)
  1728. TrackedLUAViewMap::iterator viewInfoIter = m_dOpenLUAView.find(m_lastFocusedAssetId);
  1729. if (viewInfoIter != m_dOpenLUAView.end())
  1730. {
  1731. TrackedLUAView& viewInfo = viewInfoIter->second;
  1732. if (viewInfo.luaViewWidget())
  1733. {
  1734. viewInfo.luaViewWidget()->UpdateCurrentExecutingLine(-1);
  1735. }
  1736. }
  1737. }
  1738. if (!track.targetConnected)
  1739. {
  1740. m_pDebugAttachmentButton->setEnabled(false);
  1741. m_gui->action_continue->setEnabled(false);
  1742. m_gui->action_ExecuteOnTarget->setEnabled(false);
  1743. m_gui->action_stepover->setEnabled(false);
  1744. m_gui->action_stepin->setEnabled(false);
  1745. m_gui->action_stepout->setEnabled(false);
  1746. // EARLY OUT
  1747. return;
  1748. }
  1749. // TARGET CONNECTED TRUE IS ASSUMED BEYOND THIS POINT
  1750. m_pDebugAttachmentButton->setEnabled(true);
  1751. if (!track.debuggerAttached)
  1752. {
  1753. m_gui->action_ExecuteOnTarget->setEnabled(false);
  1754. m_gui->action_stepover->setEnabled(false);
  1755. m_gui->action_stepin->setEnabled(false);
  1756. m_gui->action_stepout->setEnabled(false);
  1757. m_gui->action_continue->setEnabled(false);
  1758. // EARLY OUT
  1759. return;
  1760. }
  1761. // DEBUGGER ATTACHED TRUE IS ASSUMED BEYOND THIS POINT
  1762. if (track.scriptRunning)
  1763. {
  1764. if (track.atBreak) // running script and at a break
  1765. {
  1766. m_gui->action_ExecuteOnTarget->setEnabled(false);
  1767. m_gui->action_stepover->setEnabled(true);
  1768. m_gui->action_stepin->setEnabled(true);
  1769. m_gui->action_stepout->setEnabled(true);
  1770. m_gui->action_continue->setEnabled(true);
  1771. }
  1772. else // running script and NOT at a break
  1773. {
  1774. m_gui->action_ExecuteOnTarget->setEnabled(true);
  1775. m_gui->action_stepover->setEnabled(false);
  1776. m_gui->action_stepin->setEnabled(false);
  1777. m_gui->action_stepout->setEnabled(false);
  1778. m_gui->action_continue->setEnabled(false); // this will make it execute remotely...
  1779. }
  1780. }
  1781. else // script NOT running
  1782. {
  1783. if (track.atBreak) // script is NOT running and we're at a break
  1784. {
  1785. m_gui->action_ExecuteOnTarget->setEnabled(false);
  1786. m_gui->action_stepover->setEnabled(track.hasExecuted);
  1787. m_gui->action_stepin->setEnabled(track.hasExecuted);
  1788. m_gui->action_stepout->setEnabled(track.hasExecuted);
  1789. m_gui->action_continue->setEnabled(track.hasExecuted);
  1790. }
  1791. else // script is NOT running and NOT at a break
  1792. {
  1793. if (track.atLeastOneFileOpen)
  1794. {
  1795. m_gui->action_ExecuteOnTarget->setEnabled(true);
  1796. m_gui->action_stepover->setEnabled(track.hasExecuted);
  1797. m_gui->action_stepin->setEnabled(track.hasExecuted);
  1798. m_gui->action_stepout->setEnabled(track.hasExecuted);
  1799. m_gui->action_continue->setEnabled(track.hasExecuted);
  1800. }
  1801. else // NO files open
  1802. {
  1803. m_gui->action_ExecuteOnTarget->setEnabled(false);
  1804. m_gui->action_stepover->setEnabled(false);
  1805. m_gui->action_stepin->setEnabled(false);
  1806. m_gui->action_stepout->setEnabled(false);
  1807. m_gui->action_continue->setEnabled(false);
  1808. }
  1809. }
  1810. }
  1811. }
  1812. bool LUAEditorMainWindow::eventFilter(QObject* obj, QEvent* event)
  1813. {
  1814. (void)obj;
  1815. if (event->type() == QEvent::KeyPress)
  1816. {
  1817. QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
  1818. if (keyEvent->key() == Qt::Key_C && (keyEvent->modifiers() & Qt::ControlModifier))
  1819. {
  1820. OnEditMenuCopy();
  1821. return true;
  1822. }
  1823. else if (keyEvent->key() == Qt::Key_X && (keyEvent->modifiers() & Qt::ControlModifier))
  1824. {
  1825. OnEditMenuCut();
  1826. return true;
  1827. }
  1828. }
  1829. else if (event->type() == QEvent::KeyRelease)
  1830. {
  1831. QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
  1832. if (keyEvent->key() == Qt::Key_Control)
  1833. {
  1834. m_StoredTabAssetId = "";
  1835. }
  1836. }
  1837. //return QObject::eventFilter(obj, event);
  1838. return false;
  1839. }
  1840. void LUAEditorMainWindow::OnTabForwards()
  1841. {
  1842. TrackedLUACtrlTabOrder::iterator tabIter = m_CtrlTabOrder.begin();
  1843. while (tabIter != m_CtrlTabOrder.end())
  1844. {
  1845. if (*tabIter == m_lastFocusedAssetId)
  1846. {
  1847. break;
  1848. }
  1849. tabIter++;
  1850. }
  1851. if (tabIter == m_CtrlTabOrder.begin())
  1852. {
  1853. tabIter = m_CtrlTabOrder.end();
  1854. --tabIter;
  1855. }
  1856. else
  1857. {
  1858. --tabIter;
  1859. }
  1860. TrackedLUAViewMap::iterator viewInfoIter = m_dOpenLUAView.find(*tabIter);
  1861. if (viewInfoIter != m_dOpenLUAView.end())
  1862. {
  1863. viewInfoIter->second.luaDockWidget()->show();
  1864. viewInfoIter->second.luaDockWidget()->raise();
  1865. viewInfoIter->second.luaViewWidget()->setFocus();
  1866. m_lastFocusedAssetId = *tabIter;
  1867. }
  1868. }
  1869. void LUAEditorMainWindow::OnTabBackwards()
  1870. {
  1871. TrackedLUACtrlTabOrder::iterator tabIter = m_CtrlTabOrder.begin();
  1872. while (tabIter != m_CtrlTabOrder.end())
  1873. {
  1874. if (*tabIter == m_lastFocusedAssetId)
  1875. {
  1876. break;
  1877. }
  1878. tabIter++;
  1879. }
  1880. if (tabIter == m_CtrlTabOrder.end())
  1881. {
  1882. return;
  1883. }
  1884. tabIter++;
  1885. if (tabIter == m_CtrlTabOrder.end())
  1886. {
  1887. tabIter = m_CtrlTabOrder.begin();
  1888. }
  1889. TrackedLUAViewMap::iterator viewInfoIter = m_dOpenLUAView.find(*tabIter);
  1890. if (viewInfoIter != m_dOpenLUAView.end())
  1891. {
  1892. viewInfoIter->second.luaDockWidget()->show();
  1893. viewInfoIter->second.luaDockWidget()->raise();
  1894. viewInfoIter->second.luaViewWidget()->setFocus();
  1895. m_lastFocusedAssetId = *tabIter;
  1896. }
  1897. }
  1898. void LUAEditorMainWindow::dragEnterEvent(QDragEnterEvent* pEvent)
  1899. {
  1900. if (!pEvent->mimeData()->hasUrls())
  1901. {
  1902. return;
  1903. }
  1904. pEvent->acceptProposedAction();
  1905. }
  1906. void LUAEditorMainWindow::dropEvent(QDropEvent* pEvent)
  1907. {
  1908. if (!pEvent->mimeData()->hasUrls())
  1909. {
  1910. return;
  1911. }
  1912. pEvent->setDropAction(Qt::CopyAction);
  1913. pEvent->accept();
  1914. QList<QUrl> urls = pEvent->mimeData()->urls();
  1915. for (int idx = 0; idx < urls.count(); ++idx)
  1916. {
  1917. QString path = urls[idx].toLocalFile();
  1918. AZ_TracePrintf("Debug", "URL: %s\n", path.toUtf8().data());
  1919. AZStd::string assetId(path.toUtf8().data());
  1920. Context_DocumentManagement::Bus::Broadcast(&Context_DocumentManagement::Bus::Events::OnLoadDocument, assetId, true);
  1921. }
  1922. }
  1923. QTabWidget* LUAEditorMainWindow::GetFindTabWidget()
  1924. {
  1925. return m_gui->findTabWidget;
  1926. }
  1927. void LUAEditorMainWindow::AddMessageToLog(AzToolsFramework::Logging::LogLine::LogType type, const char* window, const char* message, void* userData)
  1928. {
  1929. m_gui->m_logPanel->InsertLogLine(type, window, message, userData);
  1930. }
  1931. void LUAEditorMainWindow::LogLineSelectionChanged(const AzToolsFramework::Logging::LogLine& logLine)
  1932. {
  1933. CompilationErrorData* errorData = static_cast<CompilationErrorData*>(logLine.GetUserData());
  1934. if (errorData)
  1935. {
  1936. // Use the data, if it exists from the logLine to make sure the right tab/line is highlighted in the editor
  1937. if (OnRequestFocusView(errorData->m_filename))
  1938. {
  1939. LUAViewWidget* pLUAViewWidget = GetCurrentView();
  1940. if (pLUAViewWidget)
  1941. {
  1942. pLUAViewWidget->SetCursorPosition(errorData->m_lineNumber, 0);
  1943. }
  1944. }
  1945. }
  1946. }
  1947. void LUAEditorMainWindowSavedState::Reflect(AZ::ReflectContext* reflection)
  1948. {
  1949. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(reflection);
  1950. if (serializeContext)
  1951. {
  1952. serializeContext->Class<LUAEditorMainWindowSavedState, AzToolsFramework::MainWindowSavedState >()
  1953. ->Version(5)
  1954. ->Field("m_openAssetIds", &LUAEditorMainWindowSavedState::m_openAssetIds)
  1955. ->Field("m_bAutocompleteEnabled", &LUAEditorMainWindowSavedState::m_bAutocompleteEnabled)
  1956. ->Field("m_bAutoReloadUnmodifiedFiles", &LUAEditorMainWindowSavedState::m_bAutoReloadUnmodifiedFiles);
  1957. }
  1958. LUAEditorFindDialog::Reflect(reflection);
  1959. }
  1960. void LUAEditorMainWindowLayout::addItem(QLayoutItem* pChild)
  1961. {
  1962. children.push_back(pChild);
  1963. }
  1964. QLayoutItem* LUAEditorMainWindowLayout::itemAt(int index) const
  1965. {
  1966. if (index >= (int)children.size())
  1967. {
  1968. return nullptr;
  1969. }
  1970. return children[index];
  1971. }
  1972. QLayoutItem* LUAEditorMainWindowLayout::takeAt(int index)
  1973. {
  1974. QLayoutItem* pItem = nullptr;
  1975. if (index >= (int)children.size())
  1976. {
  1977. return nullptr;
  1978. }
  1979. pItem = children[index];
  1980. children.erase(children.begin() + index);
  1981. return pItem;
  1982. }
  1983. LUAEditorMainWindowLayout::LUAEditorMainWindowLayout(QWidget* pParent)
  1984. {
  1985. (void)pParent;
  1986. }
  1987. LUAEditorMainWindowLayout::~LUAEditorMainWindowLayout()
  1988. {
  1989. QLayoutItem* item;
  1990. item = takeAt(0);
  1991. while (item)
  1992. {
  1993. delete item;
  1994. item = takeAt(0);
  1995. }
  1996. }
  1997. int LUAEditorMainWindowLayout::count() const
  1998. {
  1999. return (int)children.size();
  2000. }
  2001. void LUAEditorMainWindowLayout::setGeometry (const QRect& r)
  2002. {
  2003. int left, top, right, bottom;
  2004. getContentsMargins(&left, &top, &right, &bottom);
  2005. QRect effectiveRect = r.adjusted(+left, +top, -right, -bottom);
  2006. for (int pos = 0; pos < (int)children.size() - 1; ++pos)
  2007. {
  2008. QLayoutItem* pItem = children[pos];
  2009. pItem->setGeometry(effectiveRect);
  2010. }
  2011. if (children.size())
  2012. {
  2013. // if we have any elements, the last element is top right aligned:
  2014. QLayoutItem* pItem = children[children.size() - 1];
  2015. QSize lastItemSize = pItem->minimumSize();
  2016. const int magicalRightEdgeOffset = pItem->widget()->style()->pixelMetric(QStyle::PM_ScrollBarExtent);
  2017. QRect topRightCorner(effectiveRect.topRight() - QPoint(lastItemSize.width() + magicalRightEdgeOffset, 0) + QPoint(-2, 2), lastItemSize);
  2018. if (pItem->geometry() != topRightCorner)
  2019. {
  2020. pItem->setGeometry(topRightCorner);
  2021. }
  2022. }
  2023. }
  2024. Qt::Orientations LUAEditorMainWindowLayout::expandingDirections() const
  2025. {
  2026. return Qt::Orientations();
  2027. }
  2028. QSize LUAEditorMainWindowLayout::sizeHint() const
  2029. {
  2030. return minimumSize();
  2031. }
  2032. QSize LUAEditorMainWindowLayout::minimumSize() const
  2033. {
  2034. QSize size;
  2035. int left, top, right, bottom;
  2036. getContentsMargins(&left, &top, &right, &bottom);
  2037. for (int pos = 0; pos < (int)children.size(); ++pos)
  2038. {
  2039. QLayoutItem* item = children[pos];
  2040. size = size.expandedTo(item->minimumSize());
  2041. }
  2042. size += (QSize(left + right, top + bottom));
  2043. return size;
  2044. }
  2045. }//namespace LUAEditor
  2046. #include <Source/LUA/moc_LUAEditorMainWindow.cpp>