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
26 [radioButtons setState:1 atRow:1 column:0 ];
30 - (IBAction)radioChanged:(id)sender {
32 [radioButtons getRow:&row column:&col ofCell:sender];
33 [[NSApplication sharedApplication].mainWindow makeFirstResponder:sender];
39 [configPopup selectItemAtIndex:-1];
40 else if (!configPopup.selectedItem)
41 [configPopup selectItemAtIndex:0];
44 mouseDirSelect.selectedSegment = -1;
45 else if (mouseDirSelect.selectedSegment == -1)
46 mouseDirSelect.selectedSegment = 0;
49 mouseBtnSelect.selectedSegment = -1;
50 else if (mouseBtnSelect.selectedSegment == -1)
51 mouseBtnSelect.selectedSegment = 0;
54 scrollDirSelect.selectedSegment = -1;
55 else if (scrollDirSelect.selectedSegment == -1)
56 scrollDirSelect.selectedSegment = 0;
61 - (IBAction)mdirChanged:(id)sender {
62 [radioButtons setState:1 atRow:3 column:0];
63 [[NSApplication sharedApplication].mainWindow makeFirstResponder:sender];
67 - (IBAction)mbtnChanged:(id)sender {
68 [radioButtons setState:1 atRow:4 column:0];
69 [[NSApplication sharedApplication].mainWindow makeFirstResponder:sender];
73 - (IBAction)sdirChanged:(id)sender {
74 [radioButtons setState:1 atRow:5 column:0];
75 [[NSApplication sharedApplication].mainWindow makeFirstResponder:sender];
79 - (Target *)makeTarget {
80 switch (radioButtons.selectedRow) {
84 if (keyInput.hasKey) {
85 TargetKeyboard* k = [[TargetKeyboard alloc] init];
93 TargetConfig *c = [[TargetConfig alloc] init];
94 if (!configPopup.selectedItem)
95 [configPopup selectItemAtIndex:0];
96 c.config = configsController.configs[configPopup.indexOfSelectedItem];
100 TargetMouseMove *mm = [[TargetMouseMove alloc] init];
101 mm.dir = mouseDirSelect.selectedSegment;
105 TargetMouseBtn *mb = [[TargetMouseBtn alloc] init];
106 mb.which = mouseBtnSelect.selectedSegment == 0 ? kCGMouseButtonLeft : kCGMouseButtonRight;
110 TargetMouseScroll *ms = [[TargetMouseScroll alloc] init];
111 ms.howMuch = scrollDirSelect.selectedSegment ? 1 : -1;
115 TargetToggleMouseScope *tms = [[TargetToggleMouseScope alloc] init];
123 -(void)configChosen:(id)sender {
124 [radioButtons setState:1 atRow:2 column:0];
129 configsController.currentConfig[joystickController.selectedAction] = [self makeTarget];
134 [radioButtons setState:1 atRow:0 column:0];
135 [self refreshConfigsPreservingSelection:NO];
139 return [radioButtons isEnabled];
142 - (void)setEnabled:(BOOL)enabled {
143 [radioButtons setEnabled:enabled];
144 [keyInput setEnabled:enabled];
145 [configPopup setEnabled:enabled];
146 [mouseDirSelect setEnabled:enabled];
147 [mouseBtnSelect setEnabled:enabled];
148 [scrollDirSelect setEnabled:enabled];
151 id jsaction = joystickController.selectedAction;
152 currentJsaction = jsaction;
155 title.stringValue = @"";
160 Target *target = configsController.currentConfig[jsaction];
163 NSString* actFullName = [act name];
166 actFullName = [[NSString alloc] initWithFormat:@"%@ > %@", [act name], actFullName];
168 title.stringValue = [[NSString alloc] initWithFormat:@"%@ > %@", configsController.currentConfig.name, actFullName];
171 [radioButtons setState:1 atRow:0 column:0];
172 } else 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 NSLog(@"Unknown target type %@.", target.description);
200 Target *currentTarget = configsController.currentConfig[currentJsaction];
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];