Allow clicking on the key field directly.
[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 "KeyInputTextView.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.vk = -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)keyChanged {
66 [radioButtons selectCellAtRow:1 column:0];
67 [radioButtons.window makeFirstResponder:radioButtons];
68 [self commit];
69 }
70
71 - (void)configChosen:(id)sender {
72 [radioButtons selectCellAtRow:2 column:0];
73 [configPopup.window makeFirstResponder:configPopup];
74 [self commit];
75 }
76
77 - (void)mdirChanged:(NSView *)sender {
78 [radioButtons selectCellAtRow:3 column:0];
79 [sender.window makeFirstResponder:sender];
80 [self commit];
81 }
82
83 - (void)mbtnChanged:(NSView *)sender {
84 [radioButtons selectCellAtRow:4 column:0];
85 [sender.window makeFirstResponder:sender];
86 [self commit];
87 }
88
89 - (void)sdirChanged:(NSView *)sender {
90 [radioButtons selectCellAtRow:5 column:0];
91 [sender.window makeFirstResponder:sender];
92 [self commit];
93 }
94
95 - (Target *)currentTarget {
96 return configsController.currentConfig[joystickController.selectedAction];
97 }
98
99 - (Target *)makeTarget {
100 switch (radioButtons.selectedRow) {
101 case 0:
102 return nil;
103 case 1:
104 if (keyInput.hasKey) {
105 TargetKeyboard *k = [[TargetKeyboard alloc] init];
106 k.vk = keyInput.vk;
107 return k;
108 } else {
109 return nil;
110 }
111 break;
112 case 2: {
113 TargetConfig *c = [[TargetConfig alloc] init];
114 c.config = configsController.configs[configPopup.indexOfSelectedItem];
115 return c;
116 }
117 case 3: {
118 TargetMouseMove *mm = [[TargetMouseMove alloc] init];
119 mm.axis = mouseDirSelect.selectedSegment;
120 return mm;
121 }
122 case 4: {
123 TargetMouseBtn *mb = [[TargetMouseBtn alloc] init];
124 mb.button = mouseBtnSelect.selectedSegment == 0 ? kCGMouseButtonLeft : kCGMouseButtonRight;
125 return mb;
126 }
127 case 5: {
128 TargetMouseScroll *ms = [[TargetMouseScroll alloc] init];
129 ms.amount = scrollDirSelect.selectedSegment ? 1 : -1;
130 return ms;
131 }
132 case 6: {
133 TargetToggleMouseScope *tms = [[TargetToggleMouseScope alloc] init];
134 return tms;
135 }
136 default:
137 return nil;
138 }
139 }
140
141 - (void)commit {
142 [self cleanUpInterface];
143 configsController.currentConfig[joystickController.selectedAction] = [self makeTarget];
144 [configsController save];
145 }
146
147 - (BOOL)enabled {
148 return [radioButtons isEnabled];
149 }
150
151 - (void)setEnabled:(BOOL)enabled {
152 [radioButtons setEnabled:enabled];
153 [keyInput setEnabled:enabled];
154 [configPopup setEnabled:enabled];
155 [mouseDirSelect setEnabled:enabled];
156 [mouseBtnSelect setEnabled:enabled];
157 [scrollDirSelect setEnabled:enabled];
158 }
159
160 - (void)loadTarget:(Target *)target forAction:(JSAction *)action {
161 if (!action) {
162 self.enabled = NO;
163 title.stringValue = @"";
164 } else {
165 self.enabled = YES;
166 NSString *actFullName = action.name;
167 for (JSAction *cur = action.base; cur; cur = cur.base) {
168 actFullName = [[NSString alloc] initWithFormat:@"%@ > %@", cur.name, actFullName];
169 }
170 title.stringValue = [[NSString alloc] initWithFormat:@"%@ > %@", configsController.currentConfig.name, actFullName];
171 }
172
173 if ([target isKindOfClass:[TargetKeyboard class]]) {
174 [radioButtons selectCellAtRow:1 column:0];
175 keyInput.vk = [(TargetKeyboard*)target vk];
176 } else if ([target isKindOfClass:[TargetConfig class]]) {
177 [radioButtons selectCellAtRow:2 column:0];
178 [configPopup selectItemAtIndex:[configsController.configs
179 indexOfObject:[(TargetConfig *)target config]]];
180 }
181 else if ([target isKindOfClass:[TargetMouseMove class]]) {
182 [radioButtons selectCellAtRow:3 column:0];
183 [mouseDirSelect setSelectedSegment:[(TargetMouseMove *)target axis]];
184 }
185 else if ([target isKindOfClass:[TargetMouseBtn class]]) {
186 [radioButtons selectCellAtRow:4 column:0];
187 mouseBtnSelect.selectedSegment = [(TargetMouseBtn *)target button] == kCGMouseButtonLeft ? 0 : 1;
188 }
189 else if ([target isKindOfClass:[TargetMouseScroll class]]) {
190 [radioButtons selectCellAtRow:5 column:0];
191 scrollDirSelect.selectedSegment = [(TargetMouseScroll *)target amount] > 0;
192 }
193 else if ([target isKindOfClass:[TargetToggleMouseScope class]]) {
194 [radioButtons selectCellAtRow:6 column:0];
195 } else {
196 [radioButtons selectCellAtRow:self.enabled ? 0 : -1 column:0];
197 }
198 [self cleanUpInterface];
199 }
200
201 - (void)loadCurrent {
202 [self loadTarget:[self currentTarget] forAction:joystickController.selectedAction];
203 }
204
205 - (void)focusKey {
206 if (radioButtons.selectedRow <= 1)
207 [keyInput.window makeFirstResponder:keyInput];
208 else
209 [keyInput resignIfFirstResponder];
210 }
211
212 - (void)refreshConfigs {
213 // TODO: This doesn't work when removing configs.
214 int initialIndex = configPopup.indexOfSelectedItem;
215 [configPopup removeAllItems];
216 for (Config *config in configsController.configs)
217 [configPopup addItemWithTitle:config.name];
218 [configPopup selectItemAtIndex:initialIndex];
219 }
220
221 @end