LUAEditorView.hxx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. #ifndef LUAEDITORVIEW_H
  9. #define LUAEDITORVIEW_H
  10. #if !defined(Q_MOC_RUN)
  11. #include <AzCore/base.h>
  12. #include <AzCore/Memory/SystemAllocator.h>
  13. #include <AzCore/std/smart_ptr/unique_ptr.h>
  14. #include <QtWidgets/QDockWidget>
  15. #include "LUAEditorContextInterface.h"
  16. #include "LUABreakpointTrackerMessages.h"
  17. #endif
  18. #pragma once
  19. class QWidget;
  20. class QFocusEvent;
  21. namespace Ui
  22. {
  23. class LUAEditorView;
  24. }
  25. namespace AzToolsFramework
  26. {
  27. class ProgressShield;
  28. }
  29. namespace LUAEditor
  30. {
  31. struct FindOperationImpl;
  32. // this is just a wrapper so we can override close events and so on
  33. class LUADockWidget
  34. : public QDockWidget
  35. {
  36. Q_OBJECT
  37. public:
  38. AZ_CLASS_ALLOCATOR(LUADockWidget,AZ::SystemAllocator,0);
  39. LUADockWidget(QWidget *parent, Qt::WindowFlags flags = Qt::WindowFlags());
  40. virtual void closeEvent(QCloseEvent *event);
  41. const AZStd::string& assetId() const { return m_assetId; }
  42. void setAssetId(const AZStd::string& assetId){ m_assetId = assetId; }
  43. private:
  44. AZStd::string m_assetId;
  45. private slots:
  46. void OnDockLocationChanged(Qt::DockWidgetArea newArea);
  47. };
  48. class LUAViewWidget : public QWidget, LUAEditor::LUABreakpointTrackerMessages::Bus::Handler
  49. {
  50. Q_OBJECT
  51. public:
  52. AZ_CLASS_ALLOCATOR(LUAViewWidget, AZ::SystemAllocator);
  53. LUAViewWidget(QWidget *pParent = NULL);
  54. virtual ~LUAViewWidget();
  55. DocumentInfo m_Info; // last updated doc info - we will get updates to this.
  56. void Initialize(const DocumentInfo& initialInfo);
  57. void OnDocumentInfoUpdated(const DocumentInfo& newInfo);
  58. LUADockWidget* luaDockWidget(){ return m_pLUADockWidget;}
  59. void SetLuaDockWidget(LUADockWidget* pLUADockWidget){m_pLUADockWidget = pLUADockWidget;}
  60. void dropEvent(QDropEvent *e) override;
  61. // point a little arrow at this line. -1 means remove it.
  62. void UpdateCurrentExecutingLine(int lineNumber);
  63. // auto-move the insert cursor to the beginning of lineNumber
  64. void UpdateCurrentEditingLine(int lineNumber);
  65. //////////////////////////////////////////////////////////////////////////
  66. //Debugger Messages, from the LUAEditor::LUABreakpointTrackerMessages::Bus
  67. void BreakpointsUpdate(const LUAEditor::BreakpointMap& uniqueBreakpoints) override;
  68. void BreakpointHit(const LUAEditor::Breakpoint& breakpoint) override;
  69. void BreakpointResume() override;
  70. bool IsReadOnly() const;
  71. bool IsModified() const;
  72. void SelectAll();
  73. bool HasSelectedText() const;
  74. QString GetSelectedText() const;
  75. void RemoveSelectedText();
  76. void ReplaceSelectedText(const QString& newText);
  77. void GetCursorPosition(int& line, int& column) const;
  78. void SetCursorPosition(int line, int column);
  79. //returns false if there is no selection
  80. bool GetSelection(int& lineStart, int& columnStart, int& lineEnd, int& columnEnd) const;
  81. void SetSelection(int lineStart, int columnStart, int lineEnd, int columnEnd);
  82. void MoveCursor(int relativePosition);
  83. QString GetLineText(int line) const;
  84. void FoldAll();
  85. void UnfoldAll();
  86. void SelectToMatchingBrace();
  87. void CommentSelectedLines();
  88. void UncommentSelectedLines();
  89. void MoveSelectedLinesUp();
  90. void MoveSelectedLinesDn();
  91. struct FindOperation
  92. {
  93. FindOperation();
  94. ~FindOperation();
  95. FindOperation(FindOperation&&);
  96. FindOperation& operator=(FindOperation&&);
  97. friend class LUAViewWidget;
  98. operator bool();
  99. private:
  100. FindOperation(FindOperationImpl* impl);
  101. FindOperation(const FindOperation&) = delete;
  102. FindOperation& operator=(const FindOperation&) = delete;
  103. FindOperationImpl* m_impl;
  104. };
  105. FindOperation FindFirst(const QString& searchString, bool isRegularExpression, bool isCaseSensitiveSearch, bool wholeWord, bool wrap, bool searchDown) const;
  106. void FindNext(FindOperation& previousOperation) const;
  107. void SetAutoCompletionEnabled(bool enabled) { m_AutoCompletionEnabled = enabled; }
  108. bool IsAutoCompletionEnabled() const { return m_AutoCompletionEnabled; }
  109. QString GetText();
  110. void Cut();
  111. void Copy();
  112. void ResetZoom();
  113. void UpdateFont();
  114. signals:
  115. void gainedFocus();
  116. void initialUpdate(LUAViewWidget* pThis);
  117. void sourceControlStatusUpdated(QString newValue);
  118. void RegainFocus();
  119. private:
  120. void UpdateModifyFlag();
  121. void keyPressEvent(QKeyEvent *ev) override;
  122. int CalcDocPosition(int line, int column);
  123. template<typename Callable> //callabe must take a const QTextCursor& as a parameter
  124. void UpdateCursor(Callable callable);
  125. template<typename Callable> //callabe sig (QString&, QTextBlock&)
  126. QString AcumulateSelectedLines(int& startLine, int& endLine, Callable callable);
  127. template<typename Callable> //callabe sig (QString&, QTextBlock&)
  128. void CommentHelper(Callable callable);
  129. void SetReadonly(bool readonly);
  130. //will call callable with 2 ints(start and end brace). if no matching braces currently, then callable is not called.
  131. //if currently on a bracket, but its not matched then callable will still get called, but with -1 for end brace
  132. template<typename Callable>
  133. void FindMatchingBrace(Callable callable);
  134. void focusInEvent(QFocusEvent* pEvent) override;
  135. void OnPlainTextFocusChanged(bool hasFocus);
  136. void CreateStyleSheet();
  137. Ui::LUAEditorView* m_gui;
  138. LUADockWidget* m_pLUADockWidget;
  139. AzToolsFramework::ProgressShield *m_pLoadingProgressShield;
  140. AzToolsFramework::ProgressShield *m_pSavingProgressShield;
  141. AzToolsFramework::ProgressShield *m_pRequestingEditProgressShield;
  142. struct BreakpointData
  143. {
  144. AZ::Uuid m_editorId; // globally unique
  145. int m_lastKnownLine; // where it was, for detecting shifts.
  146. BreakpointData(const AZ::Uuid& uuid = AZ::Uuid::CreateNull(), int lastKnownLine = -1) : m_editorId(uuid), m_lastKnownLine(lastKnownLine) {}
  147. };
  148. typedef AZStd::unordered_map<int, BreakpointData> BreakpointMap;
  149. BreakpointMap m_Breakpoints;
  150. void SyncToBreakpointLine( int line, AZ::Uuid existingID );
  151. bool m_PullRequestQueued;
  152. bool m_AutoCompletionEnabled;
  153. class LUASyntaxHighlighter* m_Highlighter;
  154. int m_zoomPercent{100}; //following visual studio, always zoom in or out 10% of current zoom value
  155. AZStd::mutex m_extraHighlightingMutex;
  156. private slots:
  157. void OnBreakpointLineMoved(int fromLineNumber, int toLineNumber);
  158. void OnBreakpointLineDeleted(int removedLineNumber);
  159. public slots:
  160. void modificationChanged(bool m);
  161. void PullFreshBreakpoints();
  162. void RegainFocusFinal();
  163. void UpdateBraceHighlight();
  164. void OnVisibilityChanged(bool vc);
  165. void OnZoomIn();
  166. void OnZoomOut();
  167. void BreakpointToggle(int line);
  168. };
  169. class LUAViewMessages : public AZ::EBusTraits
  170. {
  171. public:
  172. //////////////////////////////////////////////////////////////////////////
  173. // Bus configuration
  174. static const AZ::EBusAddressPolicy AddressPolicy = AZ::EBusAddressPolicy::Single; // we have one bus that we always broadcast to
  175. static const AZ::EBusHandlerPolicy HandlerPolicy = AZ:: EBusHandlerPolicy::Multiple; // we have multiple listeners.
  176. //////////////////////////////////////////////////////////////////////////
  177. typedef AZ::EBus<LUAViewMessages> Bus;
  178. typedef Bus::Handler Handler;
  179. virtual ~LUAViewMessages() {}
  180. virtual void OnDataLoadedAndSet(const DocumentInfo& info, LUAViewWidget* pLUAViewWidget) = 0;
  181. };
  182. };
  183. #endif //LUAEDITORVIEW_H