OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4ConsoleCocoa.mm
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2009-2016, The OpenClonk Team and contributors
5  *
6  * Distributed under the terms of the ISC license; see accompanying file
7  * "COPYING" for details.
8  *
9  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
10  * See accompanying file "TRADEMARK" for details.
11  *
12  * To redistribute this file separately, substitute the full license texts
13  * for the above references.
14  */
15 
16 #include <GL/glew.h>
17 
18 #include "C4Include.h"
19 #include "editor/C4Console.h"
20 
21 #include "game/C4Application.h"
22 #include "control/C4GameSave.h"
23 #include "gui/C4MessageInput.h"
24 #include "C4Version.h"
25 #include "player/C4Player.h"
26 #include "landscape/C4Landscape.h"
27 #include "landscape/C4Sky.h"
28 #include "game/C4GraphicsSystem.h"
29 #include "player/C4PlayerList.h"
30 #include "control/C4GameControl.h"
31 #include "landscape/C4Texture.h"
32 
33 #include "platform/StdRegistry.h"
34 
35 #import <Cocoa/Cocoa.h>
38 #import "graphics/C4DrawGLMac.h"
39 
40 // implementation of C4Console GUI for Mac OS X
41 
42 static inline C4EditorWindowController* ctrler(C4ConsoleGUI* gui) {return gui->objectiveCObject<C4EditorWindowController>();}
43 
45 {
46 public:
47  State(C4ConsoleGUI *console): Super(console) {}
48 };
49 
51 {
52 public:
53  State(C4ToolsDlg *toolsDlg): Super(toolsDlg) {}
54  ~State()
55  {
56  [ctrler(&Console).toolsPanel orderOut:nil];
57  }
58  CGImageRef CreatePreviewImage();
59  void Default() {}
60  void Clear() {}
61 };
62 
64 {
65  C4WindowController* controller = [C4EditorWindowController new];
66  setObjectiveCObject(controller);
67  [NSBundle loadNibNamed:@"Editor" owner:controller];
68  [controller setStdWindow:this];
69  this->Active = true;
70  return true;
71 }
72 
74 {
75 }
76 
77 void C4ConsoleGUI::Out(const char* message)
78 {
79  C4EditorWindowController* controller = ctrler(this);
80  if (controller)
81  {
82  NSTextStorage* textStorage = controller.outputTextView.textStorage;
83  [textStorage appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%s\n", message]]];
84  [controller.outputTextView scrollRangeToVisible:NSMakeRange([textStorage length]-1, 1)];
85  }
86 }
87 
89 {
90  [ctrler(this).outputTextView.textStorage
91  replaceCharactersInRange:NSMakeRange(0, ctrler(this).outputTextView.textStorage.length)
92  withAttributedString:[[NSAttributedString alloc] initWithString:@""]];
93  return true;
94 }
95 
97 {
98  NSTextField* label;
99  switch (type)
100  {
102  label = ctrler(this).frameLabel;
103  break;
104  case CONSOLE_TimeFPS:
105  label = ctrler(this).timeLabel;
106  break;
107  case CONSOLE_Cursor:
108  return;
109  default:
110  return;
111  }
112  [label setStringValue:[NSString stringWithUTF8String:text.getData()]];
113 }
114 
115 void C4ConsoleGUI::SetCaptionToFileName(const char* file_name)
116 {
117  [ctrler(this).window setRepresentedFilename:[NSString stringWithUTF8String:file_name]];
118 }
119 
120 bool C4ConsoleGUI::FileSelect(StdStrBuf *sFilename, const char * szFilter, DWORD dwFlags, bool fSave)
121 {
122  NSSavePanel* savePanel = fSave ? [NSSavePanel savePanel] : [NSOpenPanel openPanel];
123  if (!fSave)
124  {
125  [(NSOpenPanel*)savePanel setCanChooseFiles:YES];
126  [(NSOpenPanel*)savePanel setCanChooseDirectories:YES];
127  }
128  if ([savePanel runModal] == NSFileHandlingPanelOKButton && [[savePanel URL] isFileURL])
129  {
130  sFilename->Copy([[savePanel URL].path cStringUsingEncoding:NSUTF8StringEncoding]);
131  return true;
132  }
133  else
134  return false;
135 }
136 
138 {
139  NSMenuItem* item = [
140  [C4AppDelegate instance].addViewportForPlayerMenuItem.submenu
141  addItemWithTitle:[NSString stringWithUTF8String:player_text.getData()] action:@selector(newViewportForPlayer:) keyEquivalent:@""
142  ];
143  [item setTag:player->Number];
144  [item setTarget: C4AppDelegate.instance];
145 }
146 
148 {
149  [[C4AppDelegate instance].addViewportForPlayerMenuItem.submenu removeAllItems];
150 }
151 
152 bool C4ConsoleGUI::Message(const char *message, bool query)
153 {
154  NSAlert* alert = [NSAlert
155  alertWithMessageText:[NSString stringWithUTF8String:C4ENGINECAPTION]
156  defaultButton:query ? @"Yes" : nil
157  alternateButton:query ? @"No" : nil
158  otherButton:nil
159  informativeTextWithFormat:[NSString stringWithUTF8String:message]
160  ];
161  [alert runModal];
162  return true;
163 }
164 
166 {
167  [ctrler(this).objectsPanel orderFront:nil];
168  return true;
169 }
170 
172 {
173  [ctrler(this).objectsPanel orderOut:nil];
174 }
175 
176 void C4ConsoleGUI::PropertyDlgUpdate(C4EditCursorSelection &rSelection, bool force_function_update)
177 {
178  if (![ctrler(this).objectsPanel isVisible])
179  return;
180  StdStrBuf text = rSelection.GetDataString();
181  [ctrler(this).objectPropertiesText.textStorage setAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithUTF8String:text.getData()]]];
182 }
183 
185 {
186  [ctrler(this).toolsPanel orderOut:nil];
187 }
188 
190 {
191  [ctrler(this).toolsPanel orderFront:nil];
192  return true;
193 }
194 
196 {
197  NSPopUpButton* materialsPopup = ctrler(this).materialsPopup;
198  [materialsPopup removeAllItems];
199  [materialsPopup addItemWithTitle:[NSString stringWithUTF8String:C4TLS_MatSky]];
200  NSMutableArray* ary = [[NSMutableArray alloc] initWithCapacity:MaterialMap.Num+1];
201  [ary addObject:[NSString stringWithUTF8String:C4TLS_MatSky]];
202  for (int32_t cnt = 0; cnt < ::MaterialMap.Num; cnt++)
203  {
204  [ary addObject:[NSString stringWithUTF8String: ::MaterialMap.Map[cnt].Name]];
205  }
206  [ary sortUsingComparator:^(id a, id b) {return [a compare:b];}];
207  for (NSString* s in ary)
208  {
209  [materialsPopup addItemWithTitle:s];
210  }
211  [materialsPopup selectItemWithTitle:[NSString stringWithUTF8String:dlg->Material]];
212 }
213 
215 {
216  [ctrler(&Console).toolSelector setSelectedSegment:Tool];
217 }
218 
220 {
221  // Refill dlg
222  NSPopUpButton* texturesPopup = ctrler(&Console).texturesPopup;
223  [texturesPopup removeAllItems];
224  // bottom-most: any invalid textures
225  bool fAnyEntry = false; int32_t cnt; const char *szTexture;
227  for (cnt=0; (szTexture=::TextureMap.GetTexture(cnt)); cnt++)
228  {
229  if (!::TextureMap.GetIndex(Material, szTexture, false))
230  {
231  fAnyEntry = true;
232  [texturesPopup insertItemWithTitle:[NSString stringWithUTF8String:szTexture] atIndex:0];
233  }
234  }
235  // separator
236  if (fAnyEntry)
237  {
238  [texturesPopup insertItemWithTitle:@"-------" atIndex:0];
239  }
240 
241  // atop: valid textures
242  for (cnt=0; (szTexture=::TextureMap.GetTexture(cnt)); cnt++)
243  {
244  // Current material-texture valid? Always valid for exact mode
245  if (::TextureMap.GetIndex(Material,szTexture,false) || ::Landscape.GetMode()==LandscapeMode::Exact)
246  {
247  [texturesPopup insertItemWithTitle:[NSString stringWithUTF8String:szTexture] atIndex:0];
248  }
249  }
250  // reselect current
251  [texturesPopup selectItemWithTitle:[NSString stringWithUTF8String:Texture]];
252 }
253 
255 {
256  CGImageRef image = state->CreatePreviewImage();
257  [ctrler(&Console).previewView setImage:image imageProperties:[NSDictionary dictionary]];
258  CGImageRelease(image);
259 }
260 
261 namespace
262 {
263  // copy-pasta from http://stackoverflow.com/questions/2395650/fastest-way-to-draw-a-screen-buffer-on-the-iphone
264  const void* GetBytePointer(void* info)
265  {
266  // this is currently only called once
267  return info; // info is a pointer to the buffer
268  }
269 
270  void ReleaseBytePointer(void*info, const void* pointer)
271  {
272  // don't care, just using the one static buffer at the moment
273  }
274 
275 
276  size_t GetBytesAtPosition(void* info, void* buffer, off_t position, size_t count)
277  {
278  // I don't think this ever gets called
279  memcpy(buffer, ((char*)info) + position, count);
280  return count;
281  }
282 }
283 
284 CGImageRef C4ToolsDlg::State::CreatePreviewImage()
285 {
286  C4Surface * sfcPreview;
287  int32_t iPrvWdt,iPrvHgt;
288 
289  iPrvWdt = [ctrler(&Console).previewView frame].size.width;
290  iPrvHgt = [ctrler(&Console).previewView frame].size.height;
291 
292  if (!(sfcPreview=new C4Surface(iPrvWdt,iPrvHgt,0))) return NULL;
293 
294  // fill bg
295  BYTE bCol = 0;
296  C4Pattern Pattern;
297  // Sky material: sky as pattern only
299  {
300  Pattern.Set(::Landscape.GetSky().Surface, 0);
301  }
302  // Material-Texture
303  else
304  {
306  // Get/Create TexMap entry
308  if (iTex)
309  {
310  // Define texture pattern
311  const C4TexMapEntry *pTex = ::TextureMap.GetEntry(iTex);
312  // Security
313  if (pTex)
314  {
315  // Set drawing pattern
316  Pattern = pTex->GetPattern();
317  }
318  }
319  }
320 
322  sfcPreview,
323  iPrvWdt/2,iPrvHgt/2,
324  GetOwner()->Grade,
325  bCol, Pattern, *::Landscape.GetPal()
326  );
327  //Application.DDraw->AttachPrimaryPalette(sfcPreview);
328 
329  DWORD *pixels = new DWORD[iPrvWdt*iPrvHgt];
330  for (int x = 0; x < iPrvWdt; x++) for (int y = 0; y < iPrvHgt; y++)
331  {
332  pixels[x+y*iPrvWdt] = sfcPreview->GetPixDw(x, y, true);
333  }
334  CGDataProviderDirectCallbacks callbacks = {0, ::GetBytePointer, ::ReleaseBytePointer, ::GetBytesAtPosition, 0};
335  CGDataProviderRef pixelData = CGDataProviderCreateDirect(pixels, iPrvWdt*iPrvHgt*sizeof(DWORD), &callbacks);
336  CGImageRef image = CGImageCreate(iPrvWdt, iPrvHgt, 8, 4*8, iPrvWdt*4, [[NSColorSpace deviceRGBColorSpace] CGColorSpace], kCGBitmapByteOrder32Little, pixelData, NULL, true, kCGRenderingIntentDefault);
337  CGDataProviderRelease(pixelData);
338  delete sfcPreview;
339  return image;
340 }
341 
343 {
344 }
345 
347 {
348  return true;
349 }
350 
352 {
353  StdStrBuf strMessage; strMessage.Format("%s %s\n\n%s", C4ENGINECAPTION, C4VERSION, copyright.getData());
354  Message(strMessage.getData(), false);
355 }
356 
357 void C4ConsoleGUI::ToolsDlgSelectTexture(C4ToolsDlg *dlg, const char *texture)
358 {
359  [ctrler(this).texturesPopup selectItemWithTitle:[NSString stringWithUTF8String:texture]];
360 }
361 
362 void C4ConsoleGUI::ToolsDlgSelectMaterial(C4ToolsDlg *dlg, const char *material)
363 {
364  [ctrler(this).materialsPopup selectItemWithTitle:[NSString stringWithUTF8String:material]];
365 }
366 
367 void C4ConsoleGUI::ToolsDlgSelectBackTexture(C4ToolsDlg *dlg, const char *texture)
368 {
369  // TODO
370 }
371 
372 void C4ConsoleGUI::ToolsDlgSelectBackMaterial(C4ToolsDlg *dlg, const char *material)
373 {
374  // TODO
375 }
376 
378 {
379 }
380 
382 {
383 }
384 
386 {
387  [C4AppDelegate.instance.recordMenuItem setEnabled:NO];
388 }
389 
391 {
392  [C4AppDelegate.instance.netMenu setHidden:NO];
393 }
394 
396 {
397  [C4AppDelegate.instance.netMenu setHidden:YES];
398 }
399 
400 void C4ConsoleGUI::DoEnableControls(bool fEnable)
401 {
402  [ctrler(this).modeSelector setEnabled:fEnable];
403 }
404 
405 bool C4ConsoleGUI::DoUpdateHaltCtrls(bool fHalt)
406 {
407  return true;
408 }
409 
411 {
413 }
414 
416 {
417 }
418 
420 {
421  NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:[NSString stringWithCString:text.getData() encoding:NSUTF8StringEncoding] action:@selector(kickPlayer:) keyEquivalent:[NSString string]];
422  [item setTarget:ctrler(this)];
423  [C4AppDelegate.instance.netMenu.submenu addItem:item];
424 }
425 
426 void C4ConsoleGUI::SetInputFunctions(std::list<const char*> &functions)
427 {
428  [ctrler(this).consoleCombo setStringValue:[NSString string]];
429  [ctrler(this) setInputFunctions:functions];
430 }
431 
432 void C4ConsoleGUI::AddKickPlayerMenuItem(C4Player *player, StdStrBuf& player_text, bool enabled)
433 {
434  NSMenuItem* item = [
435  [C4AppDelegate instance].kickPlayerMenuItem.submenu
436  addItemWithTitle:[NSString stringWithUTF8String:player_text.getData()] action:@selector(kickPlayer:) keyEquivalent:@""
437  ];
438  [item setEnabled:enabled];
439  [item setTag:player->Number];
440  [item setTarget: ctrler(this)];
441 }
442 
444 {
445  return true;
446 }
447 
449 {
450 }
451 
452 bool C4ConsoleGUI::UpdateModeCtrls(int iMode)
453 {
454  return true;
455 }
456 
457 #define CONSOLEGUICOMMONINCLUDE
const char * getData() const
Definition: StdBuf.h:442
State(C4ToolsDlg *dlg)
Definition: C4Console.cpp:658
C4Console Console
Definition: C4Globals.cpp:45
class C4Surface * Surface
Definition: C4Sky.h:52
void SetCaptionToFileName(const char *file_name)
Definition: C4Console.cpp:689
bool PopMaterial()
Definition: C4Console.cpp:703
void DeleteConsoleWindow()
Definition: C4Console.cpp:678
void ToolsDlgSelectMaterial(C4ToolsDlg *dlg, const char *material)
Definition: C4Console.cpp:695
void ToolsDlgSelectBackTexture(C4ToolsDlg *dlg, const char *texture)
Definition: C4Console.cpp:698
void ToolsDlgInitMaterialCtrls(class C4ToolsDlg *dlg)
Definition: C4Console.cpp:693
uint8_t BYTE
bool DoUpdateHaltCtrls(bool fHalt)
Definition: C4Console.cpp:681
void Format(const char *szFmt,...) GNUC_FORMAT_ATTRIBUTE_O
Definition: StdBuf.cpp:174
C4TextureMap TextureMap
Definition: C4Texture.cpp:576
const C4TexMapEntry * GetEntry(int32_t iIndex) const
Definition: C4Texture.h:85
Definition: C4Texture.h:48
bool SEqual(const char *szStr1, const char *szStr2)
Definition: Standard.h:93
void PropertyDlgUpdate(class C4EditCursorSelection &rSelection, bool force_function_update)
Definition: C4Console.cpp:687
bool PropertyDlgOpen()
Definition: C4Console.cpp:685
int32_t GetIndex(const char *szMaterial, const char *szTexture, bool fAddIfNotExist=true, const char *szErrorIfFailed=nullptr)
Definition: C4Texture.cpp:414
int32_t Grade
Definition: C4ToolsDlg.h:54
const C4Pattern & GetPattern() const
Definition: C4Texture.h:64
char Texture[C4M_MaxName+1]
Definition: C4ToolsDlg.h:57
void ClearNetMenu()
Definition: C4Console.cpp:668
void ToolsDlgSelectTexture(C4ToolsDlg *dlg, const char *texture)
Definition: C4Console.cpp:696
void ShowAboutWithCopyright(StdStrBuf &copyright)
Definition: C4Console.cpp:692
C4Landscape Landscape
char Material[C4M_MaxName+1]
Definition: C4ToolsDlg.h:56
class C4Sky & GetSky()
void Out(const char *message)
Definition: C4Console.cpp:684
C4Draw * pDraw
Definition: C4Draw.cpp:42
int32_t Num
Definition: C4Material.h:168
DWORD GetPixDw(int iX, int iY, bool fApplyModulation)
Definition: C4Surface.cpp:491
void ToolsDlgSelectBackMaterial(C4ToolsDlg *dlg, const char *material)
Definition: C4Console.cpp:697
State(C4ConsoleGUI *console)
Definition: C4Console.cpp:654
LandscapeMode GetMode() const
void InitGradeCtrl()
Definition: C4Console.cpp:701
bool Message(const char *message, bool query)
Definition: C4Console.cpp:683
void AddNetMenuItemForPlayer(int32_t client_id, const char *text, C4ConsoleGUI::ClientOperation co)
Definition: C4Console.cpp:664
void UpdateIFTControls()
Definition: C4Console.cpp:705
void AddKickPlayerMenuItem(C4Player *player, StdStrBuf &player_text, bool enabled)
Definition: C4Console.cpp:662
bool ToolsDlgOpen(class C4ToolsDlg *dlg)
Definition: C4Console.cpp:694
void ClearViewportMenu()
Definition: C4Console.cpp:670
int32_t Get(const char *szMaterial)
Definition: C4Material.cpp:361
void NeedPreviewUpdate()
Definition: C4Console.cpp:702
const char * GetTexture(int32_t iIndex)
Definition: C4Texture.cpp:494
bool UpdateModeCtrls(int iMode)
Definition: C4Console.cpp:699
void UpdateToolCtrls()
Definition: C4Console.cpp:708
void UpdateLandscapeModeCtrls()
Definition: C4Console.cpp:706
void RecordingEnabled()
Definition: C4Console.cpp:688
bool CreateConsoleWindow(C4AbstractApp *application)
Definition: C4Console.cpp:671
bool PopTextures()
Definition: C4Console.cpp:704
C4MaterialMap MaterialMap
Definition: C4Material.cpp:970
CStdPalette * GetPal() const
bool Active
Definition: C4Window.h:273
BYTE Mat2PixColDefault(int32_t mat)
Definition: C4Material.h:235
void SetInputFunctions(std::list< const char * > &functions)
Definition: C4Console.cpp:691
bool FileSelect(StdStrBuf *sFilename, const char *szFilter, DWORD dwFlags, bool fSave)
Definition: C4Console.cpp:682
bool ClearLog()
Definition: C4Console.cpp:667
bool Set(class C4Surface *sfcSource, int iZoom=0)
Definition: C4Draw.cpp:114
void AddMenuItemForPlayer(C4Player *player, StdStrBuf &player_text)
Definition: C4Console.cpp:663
void ToolsDlgClose()
Definition: C4Console.cpp:666
void DoEnableControls(bool fEnable)
Definition: C4Console.cpp:680
#define C4TLS_MatSky
Definition: C4ToolsDlg.h:39
void DrawPatternedCircle(C4Surface *sfcDest, int x, int y, int r, BYTE col, C4Pattern &Pattern, CStdPalette &rPal)
Definition: C4Draw.cpp:675
void AddNetMenu()
Definition: C4Console.cpp:665
void DisplayInfoText(InfoTextType type, StdStrBuf &text)
Definition: C4Console.cpp:679
C4AppDelegate * instance()
void ClearPlayerMenu()
Definition: C4Console.cpp:669
uint32_t DWORD
void Copy()
Definition: StdBuf.h:467
#define s
void SetCursor(Cursor cursor)
Definition: C4Console.cpp:690
StdStrBuf GetDataString() const
void UpdateTextures()
Definition: C4Console.cpp:707
void EnableControls()
Definition: C4Console.cpp:700
void PropertyDlgClose()
Definition: C4Console.cpp:686
class InternalState< class C4ConsoleGUI > Super
Definition: C4ConsoleGUI.h:42