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:
isEmpty()36 	bool isEmpty() const override { return false; }
37 	QString translate(const char * context, const char * sourceText, const char * disambiguation = nullptr, int n = -1) const override;
38 };
39 
40 extern C4ConsoleQtTranslator qt_translator;
41 
42 class C4ConsoleClientAction : public QAction
43 {
44 	Q_OBJECT
45 
46 	int32_t client_id;
47 	C4ConsoleGUI::ClientOperation op;
48 
49 public:
50 	C4ConsoleClientAction(int32_t client_id, const char *text, QObject *parent, C4ConsoleGUI::ClientOperation op);
GetClientID()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);
GetPlayerNum()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);
GetPlayerNum()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:
C4DisableShortcutFilter(QObject * parent)87 	C4DisableShortcutFilter(QObject *parent) : QObject(parent) {}
88 
eventFilter(QObject * target,QEvent * event)89 	bool eventFilter(QObject *target, QEvent *event) override
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();
GetConsoleState()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 	void OpenTranslationsOverview();
130 	// File menu
131 	void FileNew();
132 	void FileOpen();
133 	void FileOpenInNetwork();
134 	void FileOpenWithPlayers();
135 	void FileRecord();
136 	void FileSave();
137 	void FileSaveAs();
138 	void FileSaveGameAs();
139 	void FileExportPacked();
140 	void FileClose();
141 	void FileQuit();
142 	void FileReInitScenario();
143 	// Player menu
144 	void PlayerJoin();
145 	// Window menu
146 	void ViewportNew();
147 	// Help menu
148 	void HelpAbout();
149 	void HelpToggle(bool enabled);
150 	// Console edits enter pressed events
151 	void MainConsoleEditEnter(); // console edit below log
152 	void PropertyConsoleEditEnter(); // console edit of property window
153 	// View selection changes
154 	void OnCreatorSelectionChanged(const QItemSelection & selected, const QItemSelection & deselected);
155 	void OnCreatorCurrentChanged(const QModelIndex & current, const QModelIndex & previous);
156 	void OnObjectListSelectionChanged(const QItemSelection & selected, const QItemSelection & deselected);
157 	void AscendPropertyPath();
158 	void AddArrayElement();
159 	void RemoveArrayElement();
160 	// Global editor key processing
161 	bool HandleEditorKeyDown(QKeyEvent *event);
162 	bool HandleEditorKeyUp(QKeyEvent *event);
163 	// Material changed in landscape drawing mode
164 	void ForegroundMaterialChanged(const QString &new_selection);
165 	void BackgroundMaterialChanged(const QString &new_selection);
166 	// Links on welcome page
167 	void WelcomeLinkActivated(const QString &link);
168 	// Object context menu
169 	void SelectionDelete();
170 	void SelectionDuplicate();
171 	void SelectionEjectContents();
172 	// Shortcut actions
173 	void FocusGlobalScriptBox();
174 	void FocusObjectScriptBox();
175 	void OpenMaterialSelection();
176 	void FocusNextViewport();
177 	void GradeUp();
178 	void GradeDown();
179 };
180 
181 
182 /* Main Qt editor class controlled through ControlGUI */
183 
184 class C4ConsoleGUIState // Avoid direct declaration of C4ConsoleGUI::State because qmake doesn't like some declarations in the nested class
185 {
186 public:
187 	std::unique_ptr<QApplication> application;
188 	std::unique_ptr<C4ConsoleQtMainWindow> window;
189 	std::unique_ptr<class C4ConsoleQtPropListModel> property_model;
190 	std::unique_ptr<class C4PropertyDelegateFactory> property_delegate_factory;
191 	std::unique_ptr<class C4PropertyNameDelegate> property_name_delegate;
192 	std::unique_ptr<class C4ConsoleQtObjectListModel> object_list_model;
193 	std::unique_ptr<class C4ConsoleQtDefinitionListModel> definition_list_model;
194 	std::unique_ptr<class C4DisableShortcutFilter> disable_shortcut_filter;
195 	std::unique_ptr<class C4ConsoleQtLocalizeOverviewDlg> translation_overview_dialogue;
196 	std::list<class C4ConsoleQtViewportDockWidget *> viewports;
197 	std::list<std::unique_ptr<C4ConsoleClientAction> > client_actions;
198 	std::list<std::unique_ptr<C4ConsoleRemovePlayerAction> > player_actions;
199 	std::list<std::unique_ptr<C4ConsoleOpenViewportAction> > viewport_actions;
200 	Ui::MainWindow ui;
201 
202 	// ptrs owned by window
203 	QMainWindow *viewport_area;
204 	QLabel *status_cursor, *status_framecounter, *status_timefps;
205 	QAction *window_menu_separator;
206 
207 	// Current editor/tool states
208 	// Cannot use direct members in C4EditorCursor because callbacks into the GUI happen before the values change.
209 	// If other C4Console implementations are removed, the state could be merged and these members removed.
210 	bool enabled, recording, net_enabled;
211 	LandscapeMode landscape_mode;
212 	bool flat_chunk_shapes;
213 	int32_t editcursor_mode, drawing_tool;
214 	StdCopyStrBuf material, texture, back_material, back_texture;
215 
216 	// Updating states to prevent callbacks on internal selection updates
217 	int32_t is_object_selection_updating;
218 
219 	// Currently selected single object
220 	C4Value action_object;
221 
222 	C4ConsoleGUIState(C4ConsoleGUI *console);
223 	~C4ConsoleGUIState();
224 
225 	void AddToolbarSpacer(int space);
226 	bool CreateConsoleWindow(C4AbstractApp *app);
227 	void DeleteConsoleWindow();
228 	void Execute(bool redraw_only=false);
Redraw()229 	void Redraw() { Execute(true); }
230 	void UpdateActionStates();
231 	void UpdateMatTex();
232 	void UpdateBackMatTex();
233 	// Set modes and tools
SetEnabled(bool to_enabled)234 	void SetEnabled(bool to_enabled) { enabled = to_enabled; UpdateActionStates(); ReInitDefinitions(); }
SetLandscapeMode(LandscapeMode to_landscape_mode,bool to_flat_chunk_shapes)235 	void SetLandscapeMode(LandscapeMode to_landscape_mode, bool to_flat_chunk_shapes) { landscape_mode = to_landscape_mode; flat_chunk_shapes = to_flat_chunk_shapes; UpdateActionStates(); }
SetEditCursorMode(int32_t to_editcursor_mode)236 	void SetEditCursorMode(int32_t to_editcursor_mode) { editcursor_mode = to_editcursor_mode; UpdateActionStates(); }
SetDrawingTool(int32_t to_drawing_tool)237 	void SetDrawingTool(int32_t to_drawing_tool) { drawing_tool = to_drawing_tool; UpdateActionStates(); }
SetMaterial(const char * new_material)238 	void SetMaterial(const char *new_material) { material.Copy(new_material); UpdateMatTex(); }
SetTexture(const char * new_texture)239 	void SetTexture(const char *new_texture) { texture.Copy(new_texture); UpdateMatTex(); }
SetBackMaterial(const char * new_material)240 	void SetBackMaterial(const char *new_material) { back_material.Copy(new_material); UpdateBackMatTex(); }
SetBackTexture(const char * new_texture)241 	void SetBackTexture(const char *new_texture) { back_texture.Copy(new_texture); UpdateBackMatTex(); }
SetRecording(bool to_recording)242 	void SetRecording(bool to_recording) { recording = to_recording; UpdateActionStates(); }
SetNetEnabled(bool enabled)243 	void SetNetEnabled(bool enabled) { net_enabled = enabled; UpdateActionStates(); }
244 
245 	void AddNetMenuItem(int32_t index, const char *text, C4ConsoleGUI::ClientOperation op);
246 	void ClearNetMenu();
247 	void AddKickPlayerMenuItem(int32_t plr, const char *text, bool item_enabled);
248 	void ClearPlayerMenu();
249 	void AddPlayerViewportMenuItem(int32_t plr, const char *text);
250 	void ClearViewportMenu();
251 	void AddViewport(class C4ViewportWindow *cvp);
252 	void RemoveViewport(class C4ViewportWindow *cvp);
253 	void SetInputFunctions(std::list<const char*> &functions);
254 	void PropertyDlgUpdate(C4EditCursorSelection &rSelection, bool force_function_update);
255 	void ReInitDefinitions();
256 	void OnCreatorSelectionChanged(const QItemSelection & selected, const QItemSelection & deselected);
257 	void SetObjectSelection(class C4EditCursorSelection &rSelection);
258 	void OnObjectListSelectionChanged(const QItemSelection & selected, const QItemSelection & deselected);
259 	void OnCreatorCurrentChanged(const QModelIndex & current, const QModelIndex & previous);
260 	void UpdateActionObject(C4Object *new_action_object);
261 
262 	bool CreateNewScenario(StdStrBuf *out_filename, bool *out_host_as_network); // show "new scenario" dialogue; return true if new scenario is created
263 
264 #ifdef USE_WIN32_WINDOWS
265 	bool HandleWin32KeyboardMessage(MSG *msg);
266 #endif
267 
268 	void InitWelcomeScreen();
269 	void ShowWelcomeScreen();
270 	void HideWelcomeScreen();
271 
272 	void ClearGamePointers();
273 
274 	void Draw(C4TargetFacet &cgo);
275 };
276 
277 class C4ConsoleGUI::State : public C4ConsoleGUIState
278 {
279 public:
State(C4ConsoleGUI * console)280 	State(C4ConsoleGUI *console) : C4ConsoleGUIState(console) {}
281 };
282 
283 class C4ToolsDlg::State : C4ConsoleGUI::InternalState<class C4ToolsDlg>
284 {
285 public:
State(C4ToolsDlg * toolsDlg)286 	State(C4ToolsDlg *toolsDlg) : C4ConsoleGUI::InternalState<class C4ToolsDlg>(toolsDlg) {}
287 	~State() = default;
288 
Clear()289 	void Clear() {}
Default()290 	void Default() {}
291 };
292 
293 #endif // WITH_QT_EDITOR
294 #endif // INC_C4ConsoleQtState
295