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]];
58 [k setDescr: [keyInput descr]];
64 TargetConfig* c = [[TargetConfig alloc] init];
65 [c setConfig: [configsController configs][[configPopup indexOfSelectedItem]]];
70 TargetMouseMove *mm = [[TargetMouseMove alloc] init];
71 [mm setDir: [mouseDirSelect selectedSegment]];
76 TargetMouseBtn *mb = [[TargetMouseBtn alloc] init];
77 if ([mouseBtnSelect selectedSegment] == 0) {
78 [mb setWhich: kCGMouseButtonLeft];
81 [mb setWhich: kCGMouseButtonRight];
87 TargetMouseScroll *ms = [[TargetMouseScroll alloc] init];
88 if ([scrollDirSelect selectedSegment] == 0) {
98 TargetToggleMouseScope *tms = [[TargetToggleMouseScope alloc] init];
105 -(void)configChosen:(id)sender {
106 [radioButtons setState: 1 atRow: 2 column: 0];
111 JSAction *action = [joystickController selectedAction];
113 configsController.currentConfig[action] = self.state;
119 [radioButtons setState: 1 atRow: 0 column: 0];
120 [mouseDirSelect setSelectedSegment: 0];
121 [mouseBtnSelect setSelectedSegment: 0];
122 [scrollDirSelect setSelectedSegment: 0];
123 [self refreshConfigsPreservingSelection: NO];
126 -(void) setEnabled: (BOOL) enabled {
127 [radioButtons setEnabled: enabled];
128 [keyInput setEnabled: enabled];
129 [configPopup setEnabled: enabled];
130 [mouseDirSelect setEnabled: enabled];
131 [mouseBtnSelect setEnabled: enabled];
132 [scrollDirSelect setEnabled: enabled];
135 return [radioButtons isEnabled];
139 id jsaction = [joystickController selectedAction];
140 currentJsaction = jsaction;
142 [self setEnabled: NO];
143 [title setStringValue: @""];
146 [self setEnabled: YES];
148 Target* target = configsController.currentConfig[jsaction];
151 NSString* actFullName = [act name];
154 actFullName = [[NSString alloc] initWithFormat: @"%@ > %@", [act name], actFullName];
156 [title setStringValue: [[NSString alloc] initWithFormat: @"%@ > %@", [[configsController currentConfig] name], actFullName]];
160 } else if([target isKindOfClass: [TargetKeyboard class]]) {
161 [radioButtons setState:1 atRow: 1 column: 0];
162 [keyInput setVk: [(TargetKeyboard*)target vk]];
163 } else if([target isKindOfClass: [TargetConfig class]]) {
164 [radioButtons setState:1 atRow: 2 column: 0];
165 [configPopup selectItemAtIndex: [[configsController configs] indexOfObject: [(TargetConfig*)target config]]];
167 else if ([target isKindOfClass: [TargetMouseMove class]]) {
168 [radioButtons setState:1 atRow: 3 column: 0];
169 [mouseDirSelect setSelectedSegment: [(TargetMouseMove *)target dir]];
171 else if ([target isKindOfClass: [TargetMouseBtn class]]) {
172 [radioButtons setState: 1 atRow: 4 column: 0];
173 if ([(TargetMouseBtn *)target which] == kCGMouseButtonLeft)
174 [mouseBtnSelect setSelectedSegment: 0];
176 [mouseBtnSelect setSelectedSegment: 1];
178 else if ([target isKindOfClass: [TargetMouseScroll class]]) {
179 [radioButtons setState: 1 atRow: 5 column: 0];
180 if ([(TargetMouseScroll *)target howMuch] < 0)
181 [scrollDirSelect setSelectedSegment: 0];
183 [scrollDirSelect setSelectedSegment: 1];
185 else if ([target isKindOfClass: [TargetToggleMouseScope class]]) {
186 [radioButtons setState: 1 atRow: 6 column: 0];
188 [NSException raise:@"Unknown target subclass" format:@"Unknown target subclass"];
193 [[[NSApplication sharedApplication] mainWindow] makeFirstResponder: keyInput];
196 -(void) refreshConfigsPreservingSelection: (BOOL) preserve {
197 int initialIndex = [configPopup indexOfSelectedItem];
199 NSArray* configs = [configsController configs];
200 [configPopup removeAllItems];
201 for(int i=0; i<[configs count]; i++) {
202 [configPopup addItemWithTitle: [configs[i]name]];
205 [configPopup selectItemAtIndex:initialIndex];