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