OpenClonk
C4ConsoleQt.cpp
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*/
18 
19 #include "C4Include.h"
20 #include "landscape/C4Texture.h"
21 #include "landscape/C4Landscape.h"
22 // Make sure to include landscape/* first. Otherwise, Qt will either include gltypes and that forbids including glew, or, if glew is inlcuded first, QT will undefine glew partially, and then it can't be included again.
29 #include "editor/C4Console.h"
30 #include "editor/C4ConsoleGUI.h"
31 #include "script/C4AulExec.h"
32 #include "C4Version.h"
33 
34 #include "editor/C4ConsoleQt.h"
35 
37 {
38  // Welcome screen made invisible on first game load
39  state->HideWelcomeScreen();
40 }
41 
42 void C4ConsoleGUI::Execute() { state->Execute(); }
43 
45 {
46 
47 }
48 
50 {
51  if (Active) state->SetRecording(true); // TODO this is never reset. Noone uses it anyway...
52 }
53 
55 {
56  QMessageBox::about(state->window.get(), QString(LoadResStr("IDS_MENU_ABOUT")), QString(copyright.getData()));
57 }
58 
59 bool C4ConsoleGUI::UpdateModeCtrls(int iMode)
60 {
61  if (!Active) return false;
62  state->SetEditCursorMode(iMode);
63  return true;
64 }
65 
67 {
68  if (Active) state->SetNetEnabled(true);
69 }
70 
72 {
73  if (Active) state->ClearNetMenu();
74 }
75 
76 void C4ConsoleGUI::AddNetMenuItemForPlayer(int32_t client_id, const char *text, C4ConsoleGUI::ClientOperation op)
77 {
78  if (Active) state->AddNetMenuItem(client_id, text, op);
79 }
80 
82 {
83  if (Active) state->ClearPlayerMenu();
84 }
85 
86 void C4ConsoleGUI::SetInputFunctions(std::list<const char*> &functions)
87 {
88  if (Active) state->SetInputFunctions(functions);
89 }
90 
92 {
93  if (!state->CreateConsoleWindow(application)) return false;
94  Active = true;
96  return true;
97 }
98 
100 {
101  if (Active)
102  {
103  Active = false;
104  state->DeleteConsoleWindow();
105  }
106 }
107 
108 void C4ConsoleGUI::Out(const char* message)
109 {
110  // Log text: Add to log window
111  if (state->window.get())
112  {
113  // Append text
114  state->ui.logView->append(QString(message));
115  // Scroll to end to display it
116  QScrollBar *sb = state->ui.logView->verticalScrollBar();
117  if (sb) sb->setValue(sb->maximum());
118  state->Redraw();
119  }
120 }
121 
123 {
124  // Empty log window
125  if (!Active) return false;
126  state->ui.logView->clear();
127  return true;
128 }
129 
131 {
132  QLabel *target = nullptr;
133  switch (type)
134  {
135  case CONSOLE_Cursor: target = state->status_cursor; break;
136  case CONSOLE_FrameCounter: target = state->status_framecounter; break;
137  case CONSOLE_TimeFPS: target = state->status_timefps; break;
138  }
139  if (!target) return;
140  target->setText(text.getData());
141 }
142 
143 void C4ConsoleGUI::SetCaptionToFileName(const char* file_name) { /* This is never even called? */ }
144 
145 bool C4ConsoleGUI::FileSelect(StdStrBuf *sFilename, const char * szFilter, DWORD dwFlags, bool fSave)
146 {
147  // Prepare filters from double-zero-terminated list to ";;"-separated list in Qt format
148  QString filter="", selected_filter, filename;
149  QStringList filenames; bool has_multi = (dwFlags & OpenFileFlags::OFN_ALLOWMULTISELECT);
150  if (szFilter)
151  {
152  while (*szFilter)
153  {
154  if (filter.length() > 0) filter.append(";;");
155  filter.append(szFilter);
156  szFilter += strlen(szFilter) + 1;
157  if (*szFilter)
158  {
159  filter.append(" (");
160  filter.append(szFilter);
161  filter.append(")");
162  szFilter += strlen(szFilter) + 1;
163  }
164  if (selected_filter.length() <= 0) selected_filter = filter;
165  }
166  }
167 #ifdef USE_WIN32_WINDOWS
168  // cwd backup
169  size_t l = GetCurrentDirectoryW(0, nullptr);
170  std::unique_ptr<wchar_t []> wd(new wchar_t[l]);
171  GetCurrentDirectoryW(l, wd.get());
172 #endif
173  // Show dialogue
174  if (fSave)
175  filename = QFileDialog::getSaveFileName(state->window.get(), LoadResStr("IDS_DLG_SAVE"), QString(sFilename->getData()), filter, &selected_filter);
176  else if (!has_multi)
177  filename = QFileDialog::getOpenFileName(state->window.get(), LoadResStr("IDS_DLG_OPEN"), QString(), filter);
178  else
179  filenames = QFileDialog::getOpenFileNames(state->window.get(), LoadResStr("IDS_DLG_OPEN"), QString(), filter, &selected_filter);
180 #ifdef USE_WIN32_WINDOWS
181  // Restore cwd; may have been changed in open/save dialogue
182  SetCurrentDirectoryW(wd.get());
183 #endif
184  // Process multi vs single file select
185  if (has_multi)
186  {
187  // Multi-select: Return double-zero-terminated string list
188  if (!filenames.length()) return false;
189  for (auto fn : filenames)
190  {
191  sFilename->Append(fn.toUtf8());
192  sFilename->AppendChar('\0');
193  }
194  return true;
195  }
196  // Cancelled?
197  if (filename.length() <= 0) return false;
198  // File selected!
199  sFilename->Copy(filename.toUtf8());
200  sFilename->AppendChar('\0');
201  return true;
202 }
203 
205 {
206  // Add "new viewport for X" to window menu
207  if (Active) state->AddPlayerViewportMenuItem(player->Number, player_text.getData());
208 }
209 
210 void C4ConsoleGUI::AddKickPlayerMenuItem(C4Player *player, StdStrBuf& player_text, bool enabled)
211 {
212  // Add "kick X" to player menu
213  if (Active) state->AddKickPlayerMenuItem(player->Number, player_text.getData(), enabled);
214 }
215 
217 {
218  // Remove all "new viewport for X" entries from window menu
219  if (Active) state->ClearViewportMenu();
220 }
221 
222 bool C4ConsoleGUI::Message(const char *message, bool query)
223 {
224  // Show a message through Qt
225  if (query)
226  {
227  auto result = QMessageBox::question(state->window.get(), C4ENGINECAPTION, message, QMessageBox::StandardButton::Ok | QMessageBox::StandardButton::Cancel);
228  return (result == QMessageBox::StandardButton::Ok);
229  }
230  else
231  {
232  QMessageBox::information(state->window.get(), C4ENGINECAPTION, message, QMessageBox::StandardButton::Ok);
233  return true;
234  }
235 }
236 
237 void C4ConsoleGUI::DoEnableControls(bool fEnable)
238 {
239  if (!Active) return;
240  state->SetEnabled(fEnable);
241  state->SetLandscapeMode(::Landscape.GetMode(), ::Game.C4S.Landscape.FlatChunkShapes); // initial setting
242  // If disabling controls, also stop translation editing
243  if (!fEnable) state->translation_overview_dialogue.reset();
244 }
245 
246 bool C4ConsoleGUI::DoUpdateHaltCtrls(bool fHalt)
247 {
248  // Reflect halt state in play/pause buttons
249  if (!Active) return false;
250  state->ui.actionPlay->setChecked(!fHalt);
251  state->ui.actionPause->setChecked(fHalt);
252  return true;
253 }
254 
255 bool C4ConsoleGUI::PropertyDlgOpen() { /* Always open */ return true; }
256 void C4ConsoleGUI::PropertyDlgClose() { /* Always open */ }
257 
258 void C4ConsoleGUI::PropertyDlgUpdate(C4EditCursorSelection &rSelection, bool force_function_update)
259 {
260  if (Active) state->PropertyDlgUpdate(rSelection, force_function_update);
261 }
262 
263 bool C4ConsoleGUI::ToolsDlgOpen(class C4ToolsDlg *dlg) { /* Always open */ return true; }
264 void C4ConsoleGUI::ToolsDlgClose() { /* Always open */ }
265 
267 {
268  // All foreground materials
269  assert(Active);
270  if (!Active) return;
271  if (state->ui.foregroundMatTexComboBox->count()) return; // already initialized
272  state->ui.foregroundMatTexComboBox->clear();
273  state->ui.foregroundMatTexComboBox->addItem(QString(C4TLS_MatSky));
274  QStringList items;
275  const C4TexMapEntry *entry; int32_t i = 0;
276  while ((entry = ::TextureMap.GetEntry(i++)))
277  {
278  if (!entry->isNull())
279  {
280  const char *material_name = entry->GetMaterialName();
281  if (strcmp(material_name, "Vehicle") && strcmp(material_name, "HalfVehicle"))
282  {
283  items.append(QString(FormatString("%s-%s", material_name, entry->GetTextureName()).getData()));
284  }
285  }
286  }
287  items.sort();
288  for (QString &item : items) state->ui.foregroundMatTexComboBox->addItem(item);
289  auto width = 130; /* The ToolBar randomly resizes the control */
290  state->ui.foregroundMatTexComboBox->view()->setMinimumWidth(width);
291  state->ui.foregroundMatTexComboBox->setFixedWidth(width);
292  // Background materials: True background materials first; then the "funny" stuff
293  state->ui.backgroundMatTexComboBox->addItem(QString(C4TLS_MatSky));
294  items.clear();
295  i = 0;
296  while ((entry = ::TextureMap.GetEntry(i++)))
297  {
298  if (!entry->isNull())
299  {
300  const char *material_name = entry->GetMaterialName();
301  C4Material *mat = entry->GetMaterial();
302  if (strcmp(material_name, "Vehicle") && strcmp(material_name, "HalfVehicle") && mat->Density == C4M_Background)
303  {
304  items.append(QString(FormatString("%s-%s", material_name, entry->GetTextureName()).getData()));
305  }
306  }
307  }
308  items.sort();
309  for (QString &item : items) state->ui.backgroundMatTexComboBox->addItem(item);
310  state->ui.backgroundMatTexComboBox->addItem(QString("----------"));
311  items.clear();
312  i = 0;
313  while ((entry = ::TextureMap.GetEntry(i++)))
314  {
315  if (!entry->isNull())
316  {
317  const char *material_name = entry->GetMaterialName();
318  C4Material *mat = entry->GetMaterial();
319  if (strcmp(material_name, "Vehicle") && strcmp(material_name, "HalfVehicle") && mat->Density != C4M_Background)
320  {
321  items.append(QString(FormatString("%s-%s", material_name, entry->GetTextureName()).getData()));
322  }
323  }
324  }
325  items.sort();
326  for (QString &item : items) state->ui.backgroundMatTexComboBox->addItem(item);
327  state->ui.backgroundMatTexComboBox->view()->setMinimumWidth(width);
328  state->ui.backgroundMatTexComboBox->setFixedWidth(width);
329  // Select current materials
330  state->SetMaterial(dlg->Material);
331  state->SetTexture(dlg->Texture);
332  state->SetBackMaterial(dlg->BackMaterial);
333  state->SetBackTexture(dlg->BackTexture);
334  state->UpdateMatTex();
335  state->UpdateBackMatTex();
336 }
337 
338 void C4ConsoleGUI::ToolsDlgSelectTexture(C4ToolsDlg *dlg, const char *texture) { if (!Active) return; state->SetTexture(texture); }
339 void C4ConsoleGUI::ToolsDlgSelectMaterial(C4ToolsDlg *dlg, const char *material) { if (!Active) return; state->SetMaterial(material); }
340 void C4ConsoleGUI::ToolsDlgSelectBackTexture(C4ToolsDlg *dlg, const char *texture) { if (!Active) return; state->SetBackTexture(texture); }
341 void C4ConsoleGUI::ToolsDlgSelectBackMaterial(C4ToolsDlg *dlg, const char *material) { if (!Active) return; state->SetBackMaterial(material); }
342 
343 #ifdef USE_WIN32_WINDOWS
344 void C4ConsoleGUI::Win32KeepDialogsFloating(HWND hwnd) { /* Dialogues float nicely */ }
345 bool C4ConsoleGUI::Win32DialogMessageHandling(MSG *msg) { return false; /* message handling done through Qt (somehow?) */ }
346 void C4ConsoleGUI::UpdateMenuText(HMENU hMenu) { /* Translation done through QTranslator */ }
347 #endif
348 
350 {
351  // Add surrounding widget for viewport
352  state->AddViewport(cvp);
353 }
354 
356  {
357  // Add surrounding widget for viewport
358  state->RemoveViewport(cvp);
359  }
360 
361 bool C4ConsoleGUI::CreateNewScenario(StdStrBuf *out_filename, bool *out_host_as_network)
362 {
363 #ifdef WITH_QT_EDITOR
364  return state->CreateNewScenario(out_filename, out_host_as_network);
365 #else
366  return false
367 #endif
368 }
369 
371  {
372  // selection changed (through other means than creator or object list view)
373  // reflect selection change in dialogues
374  state->SetObjectSelection(selection);
375  }
376 
377 void C4ConsoleGUI::ClearGamePointers()
378 {
379  state->ClearGamePointers();
380 }
381 
383 {
384  state->definition_list_model->EnsureInit();
385 }
386 
388 {
389  if (state && state->window) state->window->close();
390 }
391 
392 void C4ConsoleGUI::ClearPointers(class C4Object *obj)
393 {
394  if (state && state->object_list_model) state->object_list_model->Invalidate();
395 }
396 
397 void C4ConsoleGUI::EditGraphControl(const class C4ControlEditGraph *control)
398 {
399  if (state && state->property_model)
400  {
401  const char *path = control->GetPath();
402  if (path && *path)
403  {
404  // Apply control to value: Resolve value
405  C4Value graph_value = AulExec.DirectExec(::ScriptEngine.GetPropList(), path, "resolve graph edit", false, nullptr);
406  // ...and apply changes (will check for value validity)
407  C4ConsoleQtGraph::EditGraphValue(graph_value, control->GetAction(), control->GetIndex(), control->GetX(), control->GetY());
408  // For remote clients, also update any edited shapes
409  if (!control->LocalControl())
410  {
411  C4ConsoleQtShape *shape = state->property_model->GetShapeByPropertyPath(path);
412  if (shape)
413  {
414  C4ConsoleQtGraph *shape_graph = shape->GetGraphShape();
415  if (shape_graph)
416  {
417  shape_graph->EditGraph(false, control->GetAction(), control->GetIndex(), control->GetX(), control->GetY());
418  }
419  }
420  }
421  }
422  }
423 }
424 
426 {
427  // Set selected drawing tool
428  if (::Console.Active) ::Console.state->SetDrawingTool(Tool);
429 }
430 
431 void C4ToolsDlg::UpdateTextures() { /* Textures are done with materials */ }
432 void C4ToolsDlg::NeedPreviewUpdate() { /* No preview */}
433 
435 {
436  // Update current grade
437  if (::Console.Active) ::Console.state->ui.drawSizeSlider->setValue(Grade);
438 }
439 
441 {
442  // Show material selection
443  if (!::Console.Active) return false;
444  ::Console.state->ui.foregroundMatTexComboBox->setFocus();
445  ::Console.state->ui.foregroundMatTexComboBox->showPopup();
446  return true;
447 }
448 
450 {
451  // Show texture selection
452  if (!::Console.Active) return false;
453  ::Console.state->ui.foregroundMatTexComboBox->setFocus();
454  ::Console.state->ui.foregroundMatTexComboBox->showPopup();
455  return true;
456 }
457 
458 void C4ToolsDlg::UpdateIFTControls() { /* not using IFT */ }
459 
461 {
462  // Update button down states for landscape mode
463  if (::Console.Active) ::Console.state->SetLandscapeMode(::Landscape.GetMode(), ::Game.C4S.Landscape.FlatChunkShapes);
464 }
465 
466 
467 void C4ToolsDlg::EnableControls() { /* Handled internally by tool selection */ }
468 
C4AulExec AulExec
Definition: C4AulExec.cpp:29
const int32_t C4M_Background
Definition: C4Constants.h:175
C4Game Game
Definition: C4Globals.cpp:52
C4AulScriptEngine ScriptEngine
Definition: C4Globals.cpp:43
C4Console Console
Definition: C4Globals.cpp:45
C4Landscape Landscape
const char * LoadResStr(const char *id)
Definition: C4Language.h:83
C4TextureMap TextureMap
Definition: C4Texture.cpp:576
#define C4TLS_MatSky
Definition: C4ToolsDlg.h:39
uint32_t DWORD
StdStrBuf FormatString(const char *szFmt,...)
Definition: StdBuf.cpp:270
C4Value DirectExec(C4PropList *p, const char *szScript, const char *szContext, bool fPassErrors=false, C4AulScriptContext *context=nullptr, bool parse_function=false)
Definition: C4AulExec.cpp:1012
C4PropListStatic * GetPropList()
Definition: C4Aul.h:151
void AddKickPlayerMenuItem(C4Player *player, StdStrBuf &player_text, bool enabled)
Definition: C4Console.cpp:662
void EditGraphControl(const class C4ControlEditGraph *control)
Definition: C4ConsoleGUI.h:118
void AddNetMenu()
Definition: C4Console.cpp:665
void SetCursor(Cursor cursor)
Definition: C4Console.cpp:690
void EnsureDefinitionListInitialized()
Definition: C4ConsoleGUI.h:115
void EnableControls(bool fEnable)
Definition: C4ConsoleGUI.h:143
void ToolsDlgSelectBackMaterial(C4ToolsDlg *dlg, const char *material)
Definition: C4Console.cpp:697
void ClearPlayerMenu()
Definition: C4Console.cpp:669
void ClearViewportMenu()
Definition: C4Console.cpp:670
void OnObjectSelectionChanged(class C4EditCursorSelection &selection)
Definition: C4ConsoleGUI.h:112
void SetCaptionToFileName(const char *file_name)
Definition: C4Console.cpp:689
void ClearNetMenu()
Definition: C4Console.cpp:668
bool PropertyDlgOpen()
Definition: C4Console.cpp:685
void DoEnableControls(bool fEnable)
Definition: C4Console.cpp:680
void AddMenuItemForPlayer(C4Player *player, StdStrBuf &player_text)
Definition: C4Console.cpp:663
void DeleteConsoleWindow()
Definition: C4Console.cpp:678
void CloseConsoleWindow()
Definition: C4ConsoleGUI.h:116
bool CreateNewScenario(StdStrBuf *out_filename, bool *out_host_as_network)
Definition: C4ConsoleGUI.h:113
bool FileSelect(StdStrBuf *sFilename, const char *szFilter, DWORD dwFlags, bool fSave)
Definition: C4Console.cpp:682
void AddNetMenuItemForPlayer(int32_t client_id, const char *text, C4ConsoleGUI::ClientOperation co)
Definition: C4Console.cpp:664
void AddViewport(C4ViewportWindow *cvp)
Definition: C4ConsoleGUI.h:110
bool UpdateModeCtrls(int iMode)
Definition: C4Console.cpp:699
void Execute()
Definition: C4ConsoleGUI.h:109
bool CreateConsoleWindow(C4AbstractApp *application)
Definition: C4Console.cpp:671
void ToolsDlgClose()
Definition: C4Console.cpp:666
void ClearPointers(class C4Object *obj)
Definition: C4ConsoleGUI.h:117
void Out(const char *message)
Definition: C4Console.cpp:684
void RecordingEnabled()
Definition: C4Console.cpp:688
bool ClearLog()
Definition: C4Console.cpp:667
void RemoveViewport(C4ViewportWindow *cvp)
Definition: C4ConsoleGUI.h:111
void ToolsDlgSelectMaterial(C4ToolsDlg *dlg, const char *material)
Definition: C4Console.cpp:695
void ShowAboutWithCopyright(StdStrBuf &copyright)
Definition: C4Console.cpp:692
void PropertyDlgUpdate(class C4EditCursorSelection &rSelection, bool force_function_update)
Definition: C4Console.cpp:687
void PropertyDlgClose()
Definition: C4Console.cpp:686
bool ToolsDlgOpen(class C4ToolsDlg *dlg)
Definition: C4Console.cpp:694
void SetInputFunctions(std::list< const char * > &functions)
Definition: C4Console.cpp:691
void DisplayInfoText(InfoTextType type, StdStrBuf &text)
Definition: C4Console.cpp:679
bool DoUpdateHaltCtrls(bool fHalt)
Definition: C4Console.cpp:681
bool Message(const char *message, bool query)
Definition: C4Console.cpp:683
@ CONSOLE_FrameCounter
Definition: C4ConsoleGUI.h:53
void OnStartGame()
Definition: C4ConsoleGUI.h:114
void ToolsDlgSelectTexture(C4ToolsDlg *dlg, const char *texture)
Definition: C4Console.cpp:696
void ToolsDlgSelectBackTexture(C4ToolsDlg *dlg, const char *texture)
Definition: C4Console.cpp:698
void ToolsDlgInitMaterialCtrls(class C4ToolsDlg *dlg)
Definition: C4Console.cpp:693
int32_t GetIndex() const
Definition: C4Control.h:639
Action GetAction() const
Definition: C4Control.h:638
DECLARE_C4CONTROL_VIRTUALS const char * GetPath() const
Definition: C4Control.h:637
int32_t GetY() const
Definition: C4Control.h:641
int32_t GetX() const
Definition: C4Control.h:640
bool LocalControl() const
Definition: C4Control.cpp:60
C4Scenario C4S
Definition: C4Game.h:74
LandscapeMode GetMode() const
int32_t Density
Definition: C4Material.h:92
bool FlatChunkShapes
Definition: C4Scenario.h:188
C4SLandscape Landscape
Definition: C4Scenario.h:236
Definition: C4Texture.h:49
const char * GetTextureName() const
Definition: C4Texture.h:61
C4Material * GetMaterial() const
Definition: C4Texture.h:63
const char * GetMaterialName() const
Definition: C4Texture.h:60
bool isNull() const
Definition: C4Texture.h:59
const C4TexMapEntry * GetEntry(int32_t iIndex) const
Definition: C4Texture.h:85
void UpdateIFTControls()
Definition: C4Console.cpp:705
void InitGradeCtrl()
Definition: C4Console.cpp:701
void UpdateLandscapeModeCtrls()
Definition: C4Console.cpp:706
int32_t Grade
Definition: C4ToolsDlg.h:54
void UpdateTextures()
Definition: C4Console.cpp:707
int32_t Tool
Definition: C4ToolsDlg.h:53
char BackTexture[C4M_MaxName+1]
Definition: C4ToolsDlg.h:60
char Texture[C4M_MaxName+1]
Definition: C4ToolsDlg.h:57
void UpdateToolCtrls()
Definition: C4Console.cpp:708
void NeedPreviewUpdate()
Definition: C4Console.cpp:702
char BackMaterial[C4M_MaxName+1]
Definition: C4ToolsDlg.h:59
char Material[C4M_MaxName+1]
Definition: C4ToolsDlg.h:56
bool PopMaterial()
Definition: C4Console.cpp:703
bool PopTextures()
Definition: C4Console.cpp:704
void EnableControls()
Definition: C4Console.cpp:700
bool Active
Definition: C4Window.h:274
const char * getData() const
Definition: StdBuf.h:442
void AppendChar(char cChar)
Definition: StdBuf.h:588
void Copy()
Definition: StdBuf.h:467
void Append(const char *pnData, size_t iChars)
Definition: StdBuf.h:519
void Ok(T &keys)
void Cancel(T &keys)
const DWORD OFN_ALLOWMULTISELECT
Definition: C4ConsoleGUI.h:29