OpenClonk
C4EditorWindowController.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 "C4Include.h"
17 #include "editor/C4Console.h"
18 #include "player/C4Player.h"
19 #include "player/C4PlayerList.h"
20 
21 #include <epoxy/gl.h>
22 
23 #import <Cocoa/Cocoa.h>
25 #import "graphics/C4DrawGLMac.h"
27 
28 #ifdef USE_COCOA
29 
30 @interface InputFunctions : NSObject<NSComboBoxDataSource> {
31  NSArray* items;
32 }
33 - (void) setFunctions:(std::list<const char*>) functions;
34 @end
35 @implementation InputFunctions
36 - (void) setFunctions:(std::list<const char *>)functions {
37  NSMutableArray* _items = [NSMutableArray arrayWithCapacity:functions.size()];
38  for (auto f : functions)
39  if (f != NULL)
40  [_items addObject:[NSString stringWithUTF8String:f]];;
41  [_items sortUsingSelector:@selector(compare:)];
42  items = _items;
43 }
44 - (NSInteger)numberOfItemsInComboBox:(NSComboBox *)aComboBox {
45  return [items count];
46 }
47 - (id)comboBox:(NSComboBox *)aComboBox objectValueForItemAtIndex:(NSInteger)index {
48  return [items objectAtIndex:index];
49 }
50 - (NSUInteger)comboBox:(NSComboBox *)aComboBox indexOfItemWithStringValue:(NSString *)string {
51  return [items indexOfObject:string];
52 }
53 - (NSString *)comboBox:(NSComboBox *)aComboBox completedString:(NSString *)string {
54  int x;
55  for (x = [string length]-1;
56  x >= 0 && [[NSCharacterSet letterCharacterSet] characterIsMember:[string characterAtIndex:x]]; x--);
57  x++;
58  auto pfx = [string substringWithRange:NSMakeRange(0, x)];
59  auto sub = [string substringFromIndex:x];
60  auto ndx = [items indexOfObjectPassingTest:^(NSString* item, NSUInteger x, BOOL* stop) { return [item hasPrefix:sub]; }];
61  return ndx != NSNotFound ? [pfx stringByAppendingString:[items objectAtIndex:ndx]] : nil;
62 }
63 @end
64 
65 @implementation C4EditorWindowController
66 {
67  InputFunctions* inputFunctions;
68 }
69 
70 @synthesize
71  frameLabel, timeLabel, outputTextView, objectPropertiesText,
72  materialsPopup, texturesPopup, outputScrollView, previewView,
73  objectsPanel, toolsPanel, toolSelector, modeSelector, objectCombo, consoleCombo;
74 
75 - (void) awakeFromNib
76 {
77  [super awakeFromNib];
78  C4AppDelegate.instance.editorWindowController = self;
79  NSWindow* window = self.window;
80  [window makeKeyAndOrderFront:self];
81  [window makeMainWindow];
82  [toolsPanel setBecomesKeyOnlyIfNeeded:YES];
83  [objectsPanel setBecomesKeyOnlyIfNeeded:YES];
84  inputFunctions = [InputFunctions new];
85  [consoleCombo setUsesDataSource:YES];
86  [consoleCombo setDataSource:inputFunctions];
87  [consoleCombo setCompletes:YES];
88 }
89 
90 - (void) setInputFunctions:(std::list<const char*>)functions {
91  [inputFunctions setFunctions:functions];
92  [consoleCombo reloadData];
93 }
94 
95 - (void) windowWillClose:(NSNotification*)notification
96 {
97  if (notification.object == objectsPanel)
98  {
99  Console.PropertyDlgClose();
100  }
101  else if (notification.object == toolsPanel)
102  {
103  Console.ToolsDlg.Clear();
104  }
105 }
106 
107 int indexFromSender(id sender)
108 {
109  if ([sender respondsToSelector:@selector(selectedSegment)])
110  return [sender selectedSegment];
111  else if ([sender respondsToSelector:@selector(tag)])
112  return [sender tag];
113  else
114  return -1;
115 }
116 
117 - (IBAction) selectMode:(id)sender
118 {
119  Console.EditCursor.SetMode(indexFromSender(sender));
120  for (NSWindow* w in [[NSApplication sharedApplication] windows])
121  {
122  if ([[w windowController] isKindOfClass:[C4WindowController class]])
123  {
124  [w invalidateCursorRectsForView:[[w windowController] openGLView]];
125  }
126  }
127 }
128 
129 - (IBAction) play:(id)sender
130 {
131  Console.DoPlay();
132 }
133 
134 - (IBAction) halt:(id)sender
135 {
136  Console.DoHalt();
137 }
138 
139 - (IBAction) selectMaterial:(id)sender
140 {
141  Console.ToolsDlg.SetMaterial([[(NSPopUpButton*)sender titleOfSelectedItem] cStringUsingEncoding:NSUTF8StringEncoding]);
142 }
143 
144 - (IBAction) selectTexture:(id)sender
145 {
146  Console.ToolsDlg.SetTexture([[(NSPopUpButton*)sender titleOfSelectedItem] cStringUsingEncoding:NSUTF8StringEncoding]);
147 }
148 
149 - (IBAction) selectTool:(id)sender
150 {
151  Console.ToolsDlg.SetTool(indexFromSender(sender), NO);
152 }
153 
154 - (IBAction) selectIFT:(id)sender
155 {
156  Console.ToolsDlg.SetIFT([sender selectedSegment] == 1);
157 }
158 
159 - (IBAction) selectLandscapeMode:(id)sender
160 {
161  // add one since 0 is "undefined"
162  Console.ToolsDlg.SetLandscapeMode((LandscapeMode)([sender selectedSegment]+1), NO, NO);
163 }
164 
165 - (IBAction) setGrade:(id)sender
166 {
167  Console.ToolsDlg.SetGrade([sender intValue]);
168 }
169 
170 // manually catch case of game not running since button validation would require key value binding -.-
171 
172 - (IBAction) consoleIn:(id)sender
173 {
174  if (![C4AppDelegate isEditorAndGameRunning])
175  return;
176  Console.In([[consoleCombo stringValue] cStringUsingEncoding:NSUTF8StringEncoding]);
177 }
178 
179 - (IBAction) objectIn:(id)sender
180 {
181  if (![C4AppDelegate isEditorAndGameRunning])
182  return;
183  Console.EditCursor.In([[objectCombo stringValue] cStringUsingEncoding:NSUTF8StringEncoding]);
184 }
185 
186 - (IBAction) kickPlayer:(id)sender
187 {
188  if (!::Control.isCtrlHost())
189  return;
190  ::Game.Clients.CtrlRemove(::Game.Clients.getClientByID([sender tag]), LoadResStr("IDS_MSG_KICKBYMENU"));
191 }
192 
193 - (BOOL)validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)item
194 {
195  // enabled when game running and in editor mode
196  SEL gameRunningInConsoleModeSelectors[] =
197  {
198  @selector(play:),
199  @selector(halt:),
200  @selector(selectMode:),
201  nil
202  };
203  int i = 0;
204  SEL s;
205  while ((s = gameRunningInConsoleModeSelectors[i++]) != nil)
206  {
207  if (s == [item action])
209  }
210 
211  // always enabled
212  return YES;
213 }
214 
215 @end
216 
217 #endif
#define s
C4GameControl Control
C4Game Game
Definition: C4Globals.cpp:52
C4Console Console
Definition: C4Globals.cpp:45
LandscapeMode
Definition: C4Landscape.h:30
const char * LoadResStr(const char *id)
Definition: C4Language.h:83
C4Client * getClientByID(int32_t iID) const
Definition: C4Client.cpp:200
void CtrlRemove(const C4Client *pClient, const char *szReason)
Definition: C4Client.cpp:333
bool In(const char *szText)
Definition: C4Console.cpp:65
void DoPlay()
Definition: C4Console.cpp:95
void DoHalt()
Definition: C4Console.cpp:100
C4EditCursor EditCursor
Definition: C4Console.h:90
C4ToolsDlg ToolsDlg
Definition: C4Console.h:88
bool SetMode(int32_t iMode)
bool In(const char *szText)
bool isCtrlHost() const
Definition: C4GameControl.h:99
C4ClientList & Clients
Definition: C4Game.h:69
void SetMaterial(const char *szMaterial)
Definition: C4ToolsDlg.cpp:60
bool SetTool(int32_t iTool, bool fTemp)
Definition: C4ToolsDlg.cpp:43
bool SetIFT(bool fIFT)
Definition: C4ToolsDlg.cpp:131
void SetTexture(const char *szTexture)
Definition: C4ToolsDlg.cpp:71
bool SetGrade(int32_t iGrade)
Definition: C4ToolsDlg.cpp:166
bool SetLandscapeMode(LandscapeMode iMode, bool flat_chunk_shapes, bool fThroughControl=false)
Definition: C4ToolsDlg.cpp:181
BOOL isEditorAndGameRunning()
C4AppDelegate * instance()