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