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 - (IBAction)radioChanged:(id)sender {
27 [radioButtons getRow:&row column:&col ofCell:sender];
28 [[NSApplication sharedApplication].mainWindow makeFirstResponder:sender];
34 [configPopup selectItemAtIndex:-1];
35 else if (!configPopup.selectedItem)
36 [configPopup selectItemAtIndex:0];
39 mouseDirSelect.selectedSegment = -1;
40 else if (mouseDirSelect.selectedSegment == -1)
41 mouseDirSelect.selectedSegment = 0;
44 mouseBtnSelect.selectedSegment = -1;
45 else if (mouseBtnSelect.selectedSegment == -1)
46 mouseBtnSelect.selectedSegment = 0;
49 scrollDirSelect.selectedSegment = -1;
50 else if (scrollDirSelect.selectedSegment == -1)
51 scrollDirSelect.selectedSegment = 0;
57 [radioButtons setState:1 atRow:1 column:0];
61 - (void)configChosen:(id)sender {
62 [radioButtons setState:1 atRow:2 column:0];
66 - (void)mdirChanged:(id)sender {
67 [radioButtons setState:1 atRow:3 column:0];
68 [[NSApplication sharedApplication].mainWindow makeFirstResponder:sender];
72 - (void)mbtnChanged:(id)sender {
73 [radioButtons setState:1 atRow:4 column:0];
74 [[NSApplication sharedApplication].mainWindow makeFirstResponder:sender];
78 - (void)sdirChanged:(id)sender {
79 [radioButtons setState:1 atRow:5 column:0];
80 [[NSApplication sharedApplication].mainWindow makeFirstResponder:sender];
84 - (Target *)currentTarget {
85 return configsController.currentConfig[joystickController.selectedAction];
88 - (Target *)makeTarget {
89 switch (radioButtons.selectedRow) {
93 if (keyInput.hasKey) {
94 TargetKeyboard *k = [[TargetKeyboard alloc] init];
102 TargetConfig *c = [[TargetConfig alloc] init];
103 if (!configPopup.selectedItem)
104 [configPopup selectItemAtIndex:0];
105 c.config = configsController.configs[configPopup.indexOfSelectedItem];
109 TargetMouseMove *mm = [[TargetMouseMove alloc] init];
110 mm.dir = mouseDirSelect.selectedSegment;
114 TargetMouseBtn *mb = [[TargetMouseBtn alloc] init];
115 mb.which = mouseBtnSelect.selectedSegment == 0 ? kCGMouseButtonLeft : kCGMouseButtonRight;
119 TargetMouseScroll *ms = [[TargetMouseScroll alloc] init];
120 ms.howMuch = scrollDirSelect.selectedSegment ? 1 : -1;
124 TargetToggleMouseScope *tms = [[TargetToggleMouseScope alloc] init];
133 configsController.currentConfig[joystickController.selectedAction] = [self makeTarget];
134 [configsController save];
139 [radioButtons setState:1 atRow:0 column:0];
140 [self refreshConfigsPreservingSelection:NO];
144 return [radioButtons isEnabled];
147 - (void)setEnabled:(BOOL)enabled {
148 [radioButtons setEnabled:enabled];
149 [keyInput setEnabled:enabled];
150 [configPopup setEnabled:enabled];
151 [mouseDirSelect setEnabled:enabled];
152 [mouseBtnSelect setEnabled:enabled];
153 [scrollDirSelect setEnabled:enabled];
157 JSAction *act = joystickController.selectedAction;
160 title.stringValue = @"";
166 Target *target = [self currentTarget];
167 NSString *actFullName = act.name;
168 for (JSAction *cur = act.base; cur; cur = cur.base) {
169 actFullName = [[NSString alloc] initWithFormat:@"%@ > %@", cur.name, actFullName];
171 title.stringValue = [[NSString alloc] initWithFormat:@"%@ > %@", configsController.currentConfig.name, actFullName];
173 if ([target isKindOfClass:[TargetKeyboard class]]) {
174 [radioButtons setState:1 atRow:1 column:0];
175 keyInput.vk = [(TargetKeyboard*)target vk];
176 } else if ([target isKindOfClass:[TargetConfig class]]) {
177 [radioButtons setState:1 atRow:2 column:0];
178 [configPopup selectItemAtIndex:[configsController.configs
179 indexOfObject:[(TargetConfig *)target config]]];
181 else if ([target isKindOfClass:[TargetMouseMove class]]) {
182 [radioButtons setState:1 atRow:3 column:0];
183 [mouseDirSelect setSelectedSegment:[(TargetMouseMove *)target dir]];
185 else if ([target isKindOfClass:[TargetMouseBtn class]]) {
186 [radioButtons setState:1 atRow:4 column:0];
187 mouseBtnSelect.selectedSegment = [(TargetMouseBtn *)target which] == kCGMouseButtonLeft ? 0 : 1;
189 else if ([target isKindOfClass:[TargetMouseScroll class]]) {
190 [radioButtons setState:1 atRow:5 column:0];
191 scrollDirSelect.selectedSegment = [(TargetMouseScroll *)target howMuch] > 0;
193 else if ([target isKindOfClass:[TargetToggleMouseScope class]]) {
194 [radioButtons setState:1 atRow:6 column:0];
196 [radioButtons setState:1 atRow:0 column:0];
201 Target *currentTarget = [self currentTarget];
202 if (!currentTarget || [currentTarget isKindOfClass:[TargetKeyboard class]])
203 [[[NSApplication sharedApplication] mainWindow] makeFirstResponder:keyInput];
205 [keyInput resignFirstResponder];
208 - (void)refreshConfigsPreservingSelection:(BOOL)preserve {
209 int initialIndex = [configPopup indexOfSelectedItem];
210 [configPopup removeAllItems];
211 for (Config *config in configsController.configs)
212 [configPopup addItemWithTitle:config.name];
213 [configPopup selectItemAtIndex:preserve ? initialIndex : -1];