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];
138 [radioButtons setState:1 atRow:0 column:0];
139 [self refreshConfigsPreservingSelection:NO];
143 return [radioButtons isEnabled];
146 - (void)setEnabled:(BOOL)enabled {
147 [radioButtons setEnabled:enabled];
148 [keyInput setEnabled:enabled];
149 [configPopup setEnabled:enabled];
150 [mouseDirSelect setEnabled:enabled];
151 [mouseBtnSelect setEnabled:enabled];
152 [scrollDirSelect setEnabled:enabled];
156 JSAction *act = joystickController.selectedAction;
159 title.stringValue = @"";
165 Target *target = [self currentTarget];
166 NSString *actFullName = act.name;
167 for (JSAction *cur = act.base; cur; cur = cur.base) {
168 actFullName = [[NSString alloc] initWithFormat:@"%@ > %@", cur.name, actFullName];
170 title.stringValue = [[NSString alloc] initWithFormat:@"%@ > %@", configsController.currentConfig.name, actFullName];
172 if ([target isKindOfClass:[TargetKeyboard class]]) {
173 [radioButtons setState:1 atRow:1 column:0];
174 keyInput.vk = [(TargetKeyboard*)target vk];
175 } else if ([target isKindOfClass:[TargetConfig class]]) {
176 [radioButtons setState:1 atRow:2 column:0];
177 [configPopup selectItemAtIndex:[configsController.configs
178 indexOfObject:[(TargetConfig *)target config]]];
180 else if ([target isKindOfClass:[TargetMouseMove class]]) {
181 [radioButtons setState:1 atRow:3 column:0];
182 [mouseDirSelect setSelectedSegment:[(TargetMouseMove *)target dir]];
184 else if ([target isKindOfClass:[TargetMouseBtn class]]) {
185 [radioButtons setState:1 atRow:4 column:0];
186 mouseBtnSelect.selectedSegment = [(TargetMouseBtn *)target which] == kCGMouseButtonLeft ? 0 : 1;
188 else if ([target isKindOfClass:[TargetMouseScroll class]]) {
189 [radioButtons setState:1 atRow:5 column:0];
190 scrollDirSelect.selectedSegment = [(TargetMouseScroll *)target howMuch] > 0;
192 else if ([target isKindOfClass:[TargetToggleMouseScope class]]) {
193 [radioButtons setState:1 atRow:6 column:0];
195 [radioButtons setState:1 atRow:0 column:0];
200 Target *currentTarget = [self currentTarget];
201 if (!currentTarget || [currentTarget isKindOfClass:[TargetKeyboard class]])
202 [[[NSApplication sharedApplication] mainWindow] makeFirstResponder:keyInput];
204 [keyInput resignFirstResponder];
207 - (void)refreshConfigsPreservingSelection:(BOOL)preserve {
208 int initialIndex = [configPopup indexOfSelectedItem];
209 [configPopup removeAllItems];
210 for (Config *config in configsController.configs)
211 [configPopup addItemWithTitle:config.name];
212 [configPopup selectItemAtIndex:preserve ? initialIndex : -1];