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 ];
29 -(IBAction)radioChanged:(id)sender {
30 [[[NSApplication sharedApplication] mainWindow] makeFirstResponder: sender];
33 -(IBAction)mdirChanged:(id)sender {
34 [radioButtons setState: 1 atRow: 3 column: 0];
35 [[[NSApplication sharedApplication] mainWindow] makeFirstResponder: sender];
38 -(IBAction)mbtnChanged:(id)sender {
39 [radioButtons setState: 1 atRow: 4 column: 0];
40 [[[NSApplication sharedApplication] mainWindow] makeFirstResponder: sender];
43 -(IBAction)sdirChanged:(id)sender {
44 [radioButtons setState: 1 atRow: 5 column: 0];
45 [[[NSApplication sharedApplication] mainWindow] makeFirstResponder: sender];
51 switch([radioButtons selectedRow]) {
56 TargetKeyboard* k = [[TargetKeyboard alloc] init];
57 [k setVk: [keyInput vk]];
63 TargetConfig* c = [[TargetConfig alloc] init];
64 [c setConfig: [configsController configs][[configPopup indexOfSelectedItem]]];
69 TargetMouseMove *mm = [[TargetMouseMove alloc] init];
70 [mm setDir: [mouseDirSelect selectedSegment]];
75 TargetMouseBtn *mb = [[TargetMouseBtn alloc] init];
76 if ([mouseBtnSelect selectedSegment] == 0) {
77 [mb setWhich: kCGMouseButtonLeft];
80 [mb setWhich: kCGMouseButtonRight];
86 TargetMouseScroll *ms = [[TargetMouseScroll alloc] init];
87 if ([scrollDirSelect selectedSegment] == 0) {
97 TargetToggleMouseScope *tms = [[TargetToggleMouseScope alloc] init];
104 -(void)configChosen:(id)sender {
105 [radioButtons setState: 1 atRow: 2 column: 0];
110 JSAction *action = [joystickController selectedAction];
112 configsController.currentConfig[action] = self.state;
118 [radioButtons setState: 1 atRow: 0 column: 0];
119 [mouseDirSelect setSelectedSegment: 0];
120 [mouseBtnSelect setSelectedSegment: 0];
121 [scrollDirSelect setSelectedSegment: 0];
122 [self refreshConfigsPreservingSelection: NO];
125 -(void) setEnabled: (BOOL) enabled {
126 [radioButtons setEnabled: enabled];
127 [keyInput setEnabled: enabled];
128 [configPopup setEnabled: enabled];
129 [mouseDirSelect setEnabled: enabled];
130 [mouseBtnSelect setEnabled: enabled];
131 [scrollDirSelect setEnabled: enabled];
134 return [radioButtons isEnabled];
138 id jsaction = [joystickController selectedAction];
139 currentJsaction = jsaction;
141 [self setEnabled: NO];
142 [title setStringValue: @""];
145 [self setEnabled: YES];
147 Target* target = configsController.currentConfig[jsaction];
150 NSString* actFullName = [act name];
153 actFullName = [[NSString alloc] initWithFormat: @"%@ > %@", [act name], actFullName];
155 [title setStringValue: [[NSString alloc] initWithFormat: @"%@ > %@", [[configsController currentConfig] name], actFullName]];
159 } else if([target isKindOfClass: [TargetKeyboard class]]) {
160 [radioButtons setState:1 atRow: 1 column: 0];
161 [keyInput setVk: [(TargetKeyboard*)target vk]];
162 } else if([target isKindOfClass: [TargetConfig class]]) {
163 [radioButtons setState:1 atRow: 2 column: 0];
164 [configPopup selectItemAtIndex: [[configsController configs] indexOfObject: [(TargetConfig*)target config]]];
166 else if ([target isKindOfClass: [TargetMouseMove class]]) {
167 [radioButtons setState:1 atRow: 3 column: 0];
168 [mouseDirSelect setSelectedSegment: [(TargetMouseMove *)target dir]];
170 else if ([target isKindOfClass: [TargetMouseBtn class]]) {
171 [radioButtons setState: 1 atRow: 4 column: 0];
172 if ([(TargetMouseBtn *)target which] == kCGMouseButtonLeft)
173 [mouseBtnSelect setSelectedSegment: 0];
175 [mouseBtnSelect setSelectedSegment: 1];
177 else if ([target isKindOfClass: [TargetMouseScroll class]]) {
178 [radioButtons setState: 1 atRow: 5 column: 0];
179 if ([(TargetMouseScroll *)target howMuch] < 0)
180 [scrollDirSelect setSelectedSegment: 0];
182 [scrollDirSelect setSelectedSegment: 1];
184 else if ([target isKindOfClass: [TargetToggleMouseScope class]]) {
185 [radioButtons setState: 1 atRow: 6 column: 0];
187 [NSException raise:@"Unknown target subclass" format:@"Unknown target subclass"];
192 Target *currentTarget = configsController.currentConfig[currentJsaction];
193 if (!currentTarget || [currentTarget isKindOfClass:[TargetKeyboard class]])
194 [[[NSApplication sharedApplication] mainWindow] makeFirstResponder: keyInput];
196 [keyInput resignFirstResponder];
199 -(void) refreshConfigsPreservingSelection: (BOOL) preserve {
200 int initialIndex = [configPopup indexOfSelectedItem];
202 NSArray* configs = [configsController configs];
203 [configPopup removeAllItems];
204 for(int i=0; i<[configs count]; i++) {
205 [configPopup addItemWithTitle: [configs[i]name]];
208 [configPopup selectItemAtIndex:initialIndex];