OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4ConsoleQtState.h
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
5  * Copyright (c) 2013, The OpenClonk Team and contributors
6  *
7  * Distributed under the terms of the ISC license; see accompanying file
8  * "COPYING" for details.
9  *
10  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
11  * See accompanying file "TRADEMARK" for details.
12  *
13  * To redistribute this file separately, substitute the full license texts
14  * for the above references.
15  */
16 
17 /* Editor windows using Qt - internal state */
18 
19 #ifndef INC_C4ConsoleQtState
20 #define INC_C4ConsoleQtState
21 
22 #ifdef WITH_QT_EDITOR
23 
24 #include "C4Include.h" // needed for automoc
25 #include "editor/C4ConsoleGUI.h"
26 #include "editor/C4ToolsDlg.h"
27 #include "editor/C4ConsoleQt.h"
28 #include "ui_C4ConsoleQtMainWindow.h"
29 
30 /* Forward string translation to GetResStr */
31 
32 class C4ConsoleQtTranslator : public QTranslator
33 {
34  Q_OBJECT
35 public:
36  bool isEmpty() const { return false; }
37  QString translate(const char * context, const char * sourceText, const char * disambiguation = 0, int n = -1) const;
38 };
39 
40 extern C4ConsoleQtTranslator qt_translator;
41 
42 class C4ConsoleClientAction : public QAction
43 {
44  Q_OBJECT
45 
46  int32_t client_id;
48 
49 public:
50  C4ConsoleClientAction(int32_t client_id, const char *text, QObject *parent, C4ConsoleGUI::ClientOperation op);
51  int32_t GetClientID() const { return client_id; }
52  private slots:
53  void Execute();
54 };
55 
56 class C4ConsoleRemovePlayerAction : public QAction
57 {
58  Q_OBJECT
59 
60  int32_t player_num;
61 public:
62  C4ConsoleRemovePlayerAction(int32_t player_num, const char *text, QObject *parent);
63  int32_t GetPlayerNum() const { return player_num; }
64  private slots:
65  void Execute();
66 };
67 
68 class C4ConsoleOpenViewportAction : public QAction
69 {
70  Q_OBJECT
71 
72  int32_t player_num;
73 public:
74  C4ConsoleOpenViewportAction(int32_t player_num, const char *text, QObject *parent);
75  int32_t GetPlayerNum() const { return player_num; }
76  private slots:
77  void Execute();
78 };
79 
80 /* Disable shortcut on some actions */
81 
82 class C4DisableShortcutFilter : public QObject
83 {
84  Q_OBJECT
85 
86 public:
87  C4DisableShortcutFilter(QObject *parent) : QObject(parent) {}
88 
89  bool eventFilter(QObject *target, QEvent *event)
90  {
91  if (event->type() == QEvent::Shortcut) return true;
92  return QObject::eventFilter(target, event);
93  }
94 };
95 
96 class C4ConsoleQtMainWindow : public QMainWindow
97 {
98  Q_OBJECT
99  class C4ConsoleGUIState *state;
100 
101 protected:
102  void keyPressEvent(QKeyEvent * event) override;
103  void keyReleaseEvent(QKeyEvent * event) override;
104 
105 public:
106  C4ConsoleQtMainWindow(class C4AbstractApp *app, class C4ConsoleGUIState *state);
107 
108  void closeEvent(class QCloseEvent *event) override;
109  void LoadGeometry();
110  class C4ConsoleGUIState *GetConsoleState() const { return state; }
111 
112 public slots:
113  // Toolbar items
114  void PlayPressed(bool down);
115  void PausePressed(bool down);
116  void CursorGamePressed(bool down);
117  void CursorSelectPressed(bool down);
118  void CursorCreateObjPressed(bool down);
119  void CursorDrawPenPressed(bool down);
120  void CursorDrawLinePressed(bool down);
121  void CursorDrawRectPressed(bool down);
122  void CursorFillPressed(bool down);
123  void CursorPickerPressed(bool down);
124  void DynamicLandscapePressed(bool down);
125  void StaticLandscapePressed(bool down);
126  void StaticFlatLandscapePressed(bool down);
127  void ExactLandscapePressed(bool down);
128  void DrawSizeChanged(int newval);
129  // File menu
130  void FileNew();
131  void FileOpen();
132  void FileOpenInNetwork();
133  void FileOpenWithPlayers();
134  void FileRecord();
135  void FileSave();
136  void FileSaveAs();
137  void FileSaveGameAs();
138  void FileExportPacked();
139  void FileClose();
140  void FileQuit();
141  void FileReInitScenario();
142  // Player menu
143  void PlayerJoin();
144  // Window menu
145  void ViewportNew();
146  // Help menu
147  void HelpAbout();
148  void HelpToggle(bool enabled);
149  // Console edits enter pressed events
150  void MainConsoleEditEnter(); // console edit below log
151  void PropertyConsoleEditEnter(); // console edit of property window
152  // View selection changes
153  void OnCreatorSelectionChanged(const QItemSelection & selected, const QItemSelection & deselected);
154  void OnCreatorCurrentChanged(const QModelIndex & current, const QModelIndex & previous);
155  void OnObjectListSelectionChanged(const QItemSelection & selected, const QItemSelection & deselected);
156  void AscendPropertyPath();
157  void AddArrayElement();
158  void RemoveArrayElement();
159  // Global editor key processing
160  bool HandleEditorKeyDown(QKeyEvent *event);
161  bool HandleEditorKeyUp(QKeyEvent *event);
162  // Material changed in landscape drawing mode
163  void ForegroundMaterialChanged(const QString &new_selection);
164  void BackgroundMaterialChanged(const QString &new_selection);
165  // Links on welcome page
166  void WelcomeLinkActivated(const QString &link);
167  // Object context menu
168  void SelectionDelete();
169  void SelectionDuplicate();
170  void SelectionEjectContents();
171  // Shortcut actions
172  void FocusGlobalScriptBox();
173  void FocusObjectScriptBox();
174  void OpenMaterialSelection();
175  void FocusNextViewport();
176  void GradeUp();
177  void GradeDown();
178 };
179 
180 
181 /* Main Qt editor class controlled through ControlGUI */
182 
183 class C4ConsoleGUIState // Avoid direct declaration of C4ConsoleGUI::State because qmake doesn't like some declarations in the nested class
184 {
185 public:
186  std::unique_ptr<QApplication> application;
187  std::unique_ptr<C4ConsoleQtMainWindow> window;
188  std::unique_ptr<class C4ConsoleQtPropListModel> property_model;
189  std::unique_ptr<class C4PropertyDelegateFactory> property_delegate_factory;
190  std::unique_ptr<class C4PropertyNameDelegate> property_name_delegate;
191  std::unique_ptr<class C4ConsoleQtObjectListModel> object_list_model;
192  std::unique_ptr<class C4ConsoleQtDefinitionListModel> definition_list_model;
193  std::unique_ptr<class C4DisableShortcutFilter> disable_shortcut_filter;
194  std::list<class C4ConsoleQtViewportDockWidget *> viewports;
195  std::list<std::unique_ptr<C4ConsoleClientAction> > client_actions;
196  std::list<std::unique_ptr<C4ConsoleRemovePlayerAction> > player_actions;
197  std::list<std::unique_ptr<C4ConsoleOpenViewportAction> > viewport_actions;
198  Ui::MainWindow ui;
199 
200  // ptrs owned by window
201  QMainWindow *viewport_area;
202  QLabel *status_cursor, *status_framecounter, *status_timefps;
203  QAction *window_menu_separator;
204 
205  // Current editor/tool states
206  // Cannot use direct members in C4EditorCursor because callbacks into the GUI happen before the values change.
207  // If other C4Console implementations are removed, the state could be merged and these members removed.
208  bool enabled, recording, net_enabled;
209  LandscapeMode landscape_mode;
210  bool flat_chunk_shapes;
211  int32_t editcursor_mode, drawing_tool;
212  StdCopyStrBuf material, texture, back_material, back_texture;
213 
214  // Updating states to prevent callbacks on internal selection updates
215  int32_t is_object_selection_updating;
216 
217  // Currently selected single object
218  C4Value action_object;
219 
220  C4ConsoleGUIState(C4ConsoleGUI *console);
221  ~C4ConsoleGUIState();
222 
223  void AddToolbarSpacer(int space);
224  bool CreateConsoleWindow(C4AbstractApp *app);
225  void DeleteConsoleWindow();
226  void Execute(bool redraw_only=false);
227  void Redraw() { Execute(true); }
228  void UpdateActionStates();
229  void UpdateMatTex();
230  void UpdateBackMatTex();
231  // Set modes and tools
232  void SetEnabled(bool to_enabled) { enabled = to_enabled; UpdateActionStates(); ReInitDefinitions(); }
233  void SetLandscapeMode(LandscapeMode to_landscape_mode, bool to_flat_chunk_shapes) { landscape_mode = to_landscape_mode; flat_chunk_shapes = to_flat_chunk_shapes; UpdateActionStates(); }
234  void SetEditCursorMode(int32_t to_editcursor_mode) { editcursor_mode = to_editcursor_mode; UpdateActionStates(); }
235  void SetDrawingTool(int32_t to_drawing_tool) { drawing_tool = to_drawing_tool; UpdateActionStates(); }
236  void SetMaterial(const char *new_material) { material.Copy(new_material); UpdateMatTex(); }
237  void SetTexture(const char *new_texture) { texture.Copy(new_texture); UpdateMatTex(); }
238  void SetBackMaterial(const char *new_material) { back_material.Copy(new_material); UpdateBackMatTex(); }
239  void SetBackTexture(const char *new_texture) { back_texture.Copy(new_texture); UpdateBackMatTex(); }
240  void SetRecording(bool to_recording) { recording = to_recording; UpdateActionStates(); }
241  void SetNetEnabled(bool enabled) { net_enabled = enabled; UpdateActionStates(); }
242 
243  void AddNetMenuItem(int32_t index, const char *text, C4ConsoleGUI::ClientOperation op);
244  void ClearNetMenu();
245  void AddKickPlayerMenuItem(int32_t plr, const char *text, bool item_enabled);
246  void ClearPlayerMenu();
247  void AddPlayerViewportMenuItem(int32_t plr, const char *text);
248  void ClearViewportMenu();
249  void AddViewport(class C4ViewportWindow *cvp);
250  void RemoveViewport(class C4ViewportWindow *cvp);
251  void SetInputFunctions(std::list<const char*> &functions);
252  void PropertyDlgUpdate(C4EditCursorSelection &rSelection, bool force_function_update);
253  void ReInitDefinitions();
254  void OnCreatorSelectionChanged(const QItemSelection & selected, const QItemSelection & deselected);
255  void SetObjectSelection(class C4EditCursorSelection &rSelection);
256  void OnObjectListSelectionChanged(const QItemSelection & selected, const QItemSelection & deselected);
257  void OnCreatorCurrentChanged(const QModelIndex & current, const QModelIndex & previous);
258  void UpdateActionObject(C4Object *new_action_object);
259 
260  bool CreateNewScenario(StdStrBuf *out_filename, bool *out_host_as_network); // show "new scenario" dialogue; return true if new scenario is created
261 
262 #ifdef USE_WIN32_WINDOWS
263  bool HandleWin32KeyboardMessage(MSG *msg);
264 #endif
265 
266  void InitWelcomeScreen();
267  void ShowWelcomeScreen();
268  void HideWelcomeScreen();
269 
270  void ClearGamePointers();
271 
272  void Draw(C4TargetFacet &cgo);
273 };
274 
275 class C4ConsoleGUI::State : public C4ConsoleGUIState
276 {
277 public:
278  State(C4ConsoleGUI *console) : C4ConsoleGUIState(console) {}
279 };
280 
281 class C4ToolsDlg::State : C4ConsoleGUI::InternalState<class C4ToolsDlg>
282 {
283 public:
284  State(C4ToolsDlg *toolsDlg) : C4ConsoleGUI::InternalState<class C4ToolsDlg>(toolsDlg) {}
285  ~State() {}
286 
287  void Clear() {}
288  void Default() {}
289 };
290 
291 #endif // WITH_QT_EDITOR
292 #endif // INC_C4ConsoleQtState
State(C4ToolsDlg *dlg)
Definition: C4Console.cpp:663
C4ConsoleQtTranslator qt_translator
State(C4ConsoleGUI *console)
Definition: C4Console.cpp:659
LandscapeMode
Definition: C4Landscape.h:30
void Copy()
Definition: StdBuf.h:475