Rewrite KeyInputTextView into NJKeyInputField. New class communicates using a proper...
[enjoyable.git] / TargetController.m
1 //
2 // TargetController.m
3 // Enjoy
4 //
5 // Created by Sam McCall on 5/05/09.
6 //
7
8 #import "TargetController.h"
9
10 #import "ConfigsController.h"
11 #import "Config.h"
12 #import "JSAction.h"
13 #import "JoystickController.h"
14 #import "NJKeyInputField.h"
15 #import "TargetConfig.h"
16 #import "TargetController.h"
17 #import "TargetKeyboard.h"
18 #import "TargetMouseBtn.h"
19 #import "TargetMouseMove.h"
20 #import "TargetMouseScroll.h"
21 #import "TargetToggleMouseScope.h"
22
23 @implementation TargetController
24
25 - (void)cleanUpInterface {
26 NSInteger row = radioButtons.selectedRow;
27
28 if (row != 1) {
29 keyInput.keyCode = -1;
30 [keyInput resignIfFirstResponder];
31 }
32
33 if (row != 2) {
34 [configPopup selectItemAtIndex:-1];
35 [configPopup resignIfFirstResponder];
36 } else if (!configPopup.selectedItem)
37 [configPopup selectItemAtIndex:0];
38
39 if (row != 3) {
40 mouseDirSelect.selectedSegment = -1;
41 [mouseDirSelect resignIfFirstResponder];
42 } else if (mouseDirSelect.selectedSegment == -1)
43 mouseDirSelect.selectedSegment = 0;
44
45 if (row != 4) {
46 mouseBtnSelect.selectedSegment = -1;
47 [mouseBtnSelect resignIfFirstResponder];
48 } else if (mouseBtnSelect.selectedSegment == -1)
49 mouseBtnSelect.selectedSegment = 0;
50
51 if (row != 5) {
52 scrollDirSelect.selectedSegment = -1;
53 [scrollDirSelect resignIfFirstResponder];
54 } else if (scrollDirSelect.selectedSegment == -1)
55 scrollDirSelect.selectedSegment = 0;
56 }
57
58 - (IBAction)radioChanged:(NSView *)sender {
59 [sender.window makeFirstResponder:sender];
60 if (radioButtons.selectedRow == 1)
61 [keyInput.window makeFirstResponder:keyInput];
62 [self commit];
63 }
64
65 - (void)keyInputField:(NJKeyInputField *)keyInput didChangeKey:(CGKeyCode)keyCode {
66 [radioButtons selectCellAtRow:1 column:0];
67 [radioButtons.window makeFirstResponder:radioButtons];
68 [self commit];
69 }
70
71 - (void)keyInputFieldDidClear:(NJKeyInputField *)keyInput {
72 [radioButtons selectCellAtRow:0 column:0];
73 [self commit];
74 }
75
76 - (void)configChosen:(id)sender {
77 [radioButtons selectCellAtRow:2 column:0];
78 [configPopup.window makeFirstResponder:configPopup];
79 [self commit];
80 }
81
82 - (void)mdirChanged:(NSView *)sender {
83 [radioButtons selectCellAtRow:3 column:0];
84 [sender.window makeFirstResponder:sender];
85 [self commit];
86 }
87
88 - (void)mbtnChanged:(NSView *)sender {
89 [radioButtons selectCellAtRow:4 column:0];
90 [sender.window makeFirstResponder:sender];
91 [self commit];
92 }
93
94 - (void)sdirChanged:(NSView *)sender {
95 [radioButtons selectCellAtRow:5 column:0];
96 [sender.window makeFirstResponder:sender];
97 [self commit];
98 }
99
100 - (Target *)currentTarget {
101 return configsController.currentConfig[joystickController.selectedAction];
102 }
103
104 - (Target *)makeTarget {
105 switch (radioButtons.selectedRow) {
106 case 0:
107 return nil;
108 case 1:
109 if (keyInput.hasKeyCode) {
110 TargetKeyboard *k = [[TargetKeyboard alloc] init];
111 k.vk = keyInput.keyCode;
112 return k;
113 } else {
114 return nil;
115 }
116 break;
117 case 2: {
118 TargetConfig *c = [[TargetConfig alloc] init];
119 c.config = configsController.configs[configPopup.indexOfSelectedItem];
120 return c;
121 }
122 case 3: {
123 TargetMouseMove *mm = [[TargetMouseMove alloc] init];
124 mm.axis = mouseDirSelect.selectedSegment;
125 return mm;
126 }
127 case 4: {
128 TargetMouseBtn *mb = [[TargetMouseBtn alloc] init];
129 mb.button = mouseBtnSelect.selectedSegment == 0 ? kCGMouseButtonLeft : kCGMouseButtonRight;
130 return mb;
131 }
132 case 5: {
133 TargetMouseScroll *ms = [[TargetMouseScroll alloc] init];
134 ms.amount = scrollDirSelect.selectedSegment ? 1 : -1;
135 return ms;
136 }
137 case 6: {
138 TargetToggleMouseScope *tms = [[TargetToggleMouseScope alloc] init];
139 return tms;
140 }
141 default:
142 return nil;
143 }
144 }
145
146 - (void)commit {
147 [self cleanUpInterface];
148 configsController.currentConfig[joystickController.selectedAction] = [self makeTarget];
149 [configsController save];
150 }
151
152 - (BOOL)enabled {
153 return [radioButtons isEnabled];
154 }
155
156 - (void)setEnabled:(BOOL)enabled {
157 [radioButtons setEnabled:enabled];
158 [keyInput setEnabled:enabled];
159 [configPopup setEnabled:enabled];
160 [mouseDirSelect setEnabled:enabled];
161 [mouseBtnSelect setEnabled:enabled];
162 [scrollDirSelect setEnabled:enabled];
163 }
164
165 - (void)loadTarget:(Target *)target forAction:(JSAction *)action {
166 if (!action) {
167 self.enabled = NO;
168 title.stringValue = @"";
169 } else {
170 self.enabled = YES;
171 NSString *actFullName = action.name;
172 for (id <NJActionPathElement> cur = action.base; cur; cur = cur.base) {
173 actFullName = [[NSString alloc] initWithFormat:@"%@ > %@", cur.name, actFullName];
174 }
175 title.stringValue = [[NSString alloc] initWithFormat:@"%@ > %@", configsController.currentConfig.name, actFullName];
176 }
177
178 if ([target isKindOfClass:TargetKeyboard.class]) {
179 [radioButtons selectCellAtRow:1 column:0];
180 keyInput.keyCode = [(TargetKeyboard*)target vk];
181 } else if ([target isKindOfClass:TargetConfig.class]) {
182 [radioButtons selectCellAtRow:2 column:0];
183 NSUInteger idx = [configsController.configs
184 indexOfObject:[(TargetConfig *)target config]];
185 if (idx == NSNotFound) {
186 [radioButtons selectCellAtRow:self.enabled ? 0 : -1 column:0];
187 [configPopup selectItemAtIndex:-1];
188 } else
189 [configPopup selectItemAtIndex:idx];
190 }
191 else if ([target isKindOfClass:TargetMouseMove.class]) {
192 [radioButtons selectCellAtRow:3 column:0];
193 [mouseDirSelect setSelectedSegment:[(TargetMouseMove *)target axis]];
194 }
195 else if ([target isKindOfClass:TargetMouseBtn.class]) {
196 [radioButtons selectCellAtRow:4 column:0];
197 mouseBtnSelect.selectedSegment = [(TargetMouseBtn *)target button] == kCGMouseButtonLeft ? 0 : 1;
198 }
199 else if ([target isKindOfClass:TargetMouseScroll.class]) {
200 [radioButtons selectCellAtRow:5 column:0];
201 scrollDirSelect.selectedSegment = [(TargetMouseScroll *)target amount] > 0;
202 }
203 else if ([target isKindOfClass:TargetToggleMouseScope.class]) {
204 [radioButtons selectCellAtRow:6 column:0];
205 } else {
206 [radioButtons selectCellAtRow:self.enabled ? 0 : -1 column:0];
207 }
208 [self cleanUpInterface];
209 }
210
211 - (void)loadCurrent {
212 [self loadTarget:self.currentTarget forAction:joystickController.selectedAction];
213 }
214
215 - (void)focusKey {
216 if (radioButtons.selectedRow <= 1)
217 [keyInput.window makeFirstResponder:keyInput];
218 else
219 [keyInput resignIfFirstResponder];
220 }
221
222 - (void)refreshConfigs {
223 NSInteger initialIndex = configPopup.indexOfSelectedItem;
224 [configPopup.menu removeAllItems];
225 for (Config *config in configsController.configs) {
226 NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:config.name
227 action:@selector(configChosen:)
228 keyEquivalent:@""];
229 item.target = self;
230 [configPopup.menu addItem:item];
231 }
232 [configPopup selectItemAtIndex:initialIndex];
233 }
234
235 @end