5 // Created by Sam McCall on 5/05/09.
8 #import "TargetController.h"
10 #import "ConfigsController.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"
23 @implementation TargetController
25 - (void)cleanUpInterface {
26 NSInteger row = radioButtons.selectedRow;
30 [keyInput resignIfFirstResponder];
34 [configPopup selectItemAtIndex:-1];
35 [configPopup resignIfFirstResponder];
36 } else if (!configPopup.selectedItem)
37 [configPopup selectItemAtIndex:0];
40 mouseDirSelect.selectedSegment = -1;
41 [mouseDirSelect resignIfFirstResponder];
42 } else if (mouseDirSelect.selectedSegment == -1)
43 mouseDirSelect.selectedSegment = 0;
46 mouseBtnSelect.selectedSegment = -1;
47 [mouseBtnSelect resignIfFirstResponder];
48 } else if (mouseBtnSelect.selectedSegment == -1)
49 mouseBtnSelect.selectedSegment = 0;
52 scrollDirSelect.selectedSegment = -1;
53 [scrollDirSelect resignIfFirstResponder];
54 } else if (scrollDirSelect.selectedSegment == -1)
55 scrollDirSelect.selectedSegment = 0;
58 - (IBAction)radioChanged:(NSView *)sender {
59 [sender.window makeFirstResponder:sender];
60 if (radioButtons.selectedRow == 1)
61 [keyInput.window makeFirstResponder:keyInput];
66 [radioButtons selectCellAtRow:1 column:0];
67 [radioButtons.window makeFirstResponder:radioButtons];
71 - (void)configChosen:(id)sender {
72 [radioButtons selectCellAtRow:2 column:0];
73 [configPopup.window makeFirstResponder:configPopup];
77 - (void)mdirChanged:(NSView *)sender {
78 [radioButtons selectCellAtRow:3 column:0];
79 [sender.window makeFirstResponder:sender];
83 - (void)mbtnChanged:(NSView *)sender {
84 [radioButtons selectCellAtRow:4 column:0];
85 [sender.window makeFirstResponder:sender];
89 - (void)sdirChanged:(NSView *)sender {
90 [radioButtons selectCellAtRow:5 column:0];
91 [sender.window makeFirstResponder:sender];
95 - (Target *)currentTarget {
96 return configsController.currentConfig[joystickController.selectedAction];
99 - (Target *)makeTarget {
100 switch (radioButtons.selectedRow) {
104 if (keyInput.hasKey) {
105 TargetKeyboard *k = [[TargetKeyboard alloc] init];
113 TargetConfig *c = [[TargetConfig alloc] init];
114 c.config = configsController.configs[configPopup.indexOfSelectedItem];
118 TargetMouseMove *mm = [[TargetMouseMove alloc] init];
119 mm.axis = mouseDirSelect.selectedSegment;
123 TargetMouseBtn *mb = [[TargetMouseBtn alloc] init];
124 mb.button = mouseBtnSelect.selectedSegment == 0 ? kCGMouseButtonLeft : kCGMouseButtonRight;
128 TargetMouseScroll *ms = [[TargetMouseScroll alloc] init];
129 ms.amount = scrollDirSelect.selectedSegment ? 1 : -1;
133 TargetToggleMouseScope *tms = [[TargetToggleMouseScope alloc] init];
142 [self cleanUpInterface];
143 configsController.currentConfig[joystickController.selectedAction] = [self makeTarget];
144 [configsController save];
148 return [radioButtons isEnabled];
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];
160 - (void)loadTarget:(Target *)target forAction:(JSAction *)action {
163 title.stringValue = @"";
166 NSString *actFullName = action.name;
167 for (JSAction *cur = action.base; cur; cur = cur.base) {
168 actFullName = [[NSString alloc] initWithFormat:@"%@ > %@", cur.name, actFullName];
170 title.stringValue = [[NSString alloc] initWithFormat:@"%@ > %@", configsController.currentConfig.name, actFullName];
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]]];
181 else if ([target isKindOfClass:[TargetMouseMove class]]) {
182 [radioButtons selectCellAtRow:3 column:0];
183 [mouseDirSelect setSelectedSegment:[(TargetMouseMove *)target axis]];
185 else if ([target isKindOfClass:[TargetMouseBtn class]]) {
186 [radioButtons selectCellAtRow:4 column:0];
187 mouseBtnSelect.selectedSegment = [(TargetMouseBtn *)target button] == kCGMouseButtonLeft ? 0 : 1;
189 else if ([target isKindOfClass:[TargetMouseScroll class]]) {
190 [radioButtons selectCellAtRow:5 column:0];
191 scrollDirSelect.selectedSegment = [(TargetMouseScroll *)target amount] > 0;
193 else if ([target isKindOfClass:[TargetToggleMouseScope class]]) {
194 [radioButtons selectCellAtRow:6 column:0];
196 [radioButtons selectCellAtRow:self.enabled ? 0 : -1 column:0];
198 [self cleanUpInterface];
201 - (void)loadCurrent {
202 [self loadTarget:[self currentTarget] forAction:joystickController.selectedAction];
206 if (radioButtons.selectedRow <= 1)
207 [keyInput.window makeFirstResponder:keyInput];
209 [keyInput resignIfFirstResponder];
212 - (void)refreshConfigs {
213 // TODO: This doesn't work when removing configs.
214 NSInteger initialIndex = configPopup.indexOfSelectedItem;
215 [configPopup removeAllItems];
216 for (Config *config in configsController.configs)
217 [configPopup addItemWithTitle:config.name];
218 [configPopup selectItemAtIndex:initialIndex];