Forked Enjoy, mouse movement
[enjoyable.git] / TargetController.m
1 //
2 // TargetController.m
3 // Enjoy
4 //
5 // Created by Sam McCall on 5/05/09.
6 //
7
8 @implementation TargetController
9
10 -(void) keyChanged {
11 [radioButtons setState: 1 atRow: 1 column: 0 ];
12 [self commit];
13 }
14 -(IBAction)radioChanged:(id)sender {
15 [[[NSApplication sharedApplication] mainWindow] makeFirstResponder: sender];
16 [self commit];
17 }
18
19
20 -(Target*) state {
21 switch([radioButtons selectedRow]) {
22 case 0: // none
23 return NULL;
24 case 1: // key
25 if([keyInput hasKey]) {
26 TargetKeyboard* k = [[TargetKeyboard alloc] init];
27 [k setVk: [keyInput vk]];
28 [k setDescr: [keyInput descr]];
29 return k;
30 }
31 break;
32 case 2:
33 {
34 TargetConfig* c = [[TargetConfig alloc] init];
35 [c setConfig: [[configsController configs] objectAtIndex: [configPopup indexOfSelectedItem]]];
36 return c;
37 }
38 case 3: {
39 // mouse X
40 TargetMouseMove *mm = [[TargetMouseMove alloc] init];
41 [mm setDir: 0];
42 return mm;
43 }
44 case 4: {
45 // mouse Y
46 TargetMouseMove *mm = [[TargetMouseMove alloc] init];
47 [mm setDir: 1];
48 return mm;
49 }
50 case 5: {
51 // mouse button
52 TargetMouseBtn *mb = [[TargetMouseBtn alloc] init];
53 [mb setWhich: [mouseBtnRadio selectedCol]];
54 return mb;
55 }
56 }
57 return NULL;
58 }
59
60 -(void)configChosen:(id)sender {
61 [radioButtons setState: 1 atRow: 2 column: 0];
62 [self commit];
63 }
64
65 -(void) commit {
66 id action = [joystickController selectedAction];
67 if(action) {
68 Target* target = [self state];
69 [[configsController currentConfig] setTarget: target forAction: action];
70 }
71 }
72
73 -(void) reset {
74 [keyInput clear];
75 [radioButtons setState: 1 atRow: 0 column: 0];
76 [self refreshConfigsPreservingSelection: NO];
77 }
78
79 -(void) setEnabled: (BOOL) enabled {
80 [radioButtons setEnabled: enabled];
81 [keyInput setEnabled: enabled];
82 [configPopup setEnabled: enabled];
83 }
84 -(BOOL) enabled {
85 return [radioButtons isEnabled];
86 }
87
88 -(void) load {
89 id jsaction = [joystickController selectedAction];
90 currentJsaction = jsaction;
91 if(!jsaction) {
92 [self setEnabled: NO];
93 [title setStringValue: @""];
94 return;
95 } else {
96 [self setEnabled: YES];
97 }
98 Target* target = [[configsController currentConfig] getTargetForAction: jsaction];
99
100 id act = jsaction;
101 NSString* actFullName = [act name];
102 while([act base]) {
103 act = [act base];
104 actFullName = [[NSString alloc] initWithFormat: @"%@ > %@", [act name], actFullName];
105 }
106 [title setStringValue: [[NSString alloc] initWithFormat: @"%@ > %@", [[configsController currentConfig] name], actFullName]];
107
108 if(!target) {
109 // already reset
110 } else if([target isKindOfClass: [TargetKeyboard class]]) {
111 [radioButtons setState:1 atRow: 1 column: 0];
112 [keyInput setVk: [(TargetKeyboard*)target vk]];
113 } else if([target isKindOfClass: [TargetConfig class]]) {
114 [radioButtons setState:1 atRow: 2 column: 0];
115 [configPopup selectItemAtIndex: [[configsController configs] indexOfObject: [(TargetConfig*)target config]]];
116 } else if ([target isKindOfClass: [TargetMouseMove class]]) {
117 if ([(TargetMouseMove *)target dir] == 0)
118 [radioButtons setState:1 atRow: 3 column: 0];
119 else
120 [radioButtons setState:1 atRow: 4 column: 0];
121 } else {
122 [NSException raise:@"Unknown target subclass" format:@"Unknown target subclass"];
123 }
124 }
125
126 -(void) focusKey {
127 [[[NSApplication sharedApplication] mainWindow] makeFirstResponder: keyInput];
128 }
129
130 -(void) refreshConfigsPreservingSelection: (BOOL) preserve {
131 int initialIndex = [configPopup indexOfSelectedItem];
132
133 NSArray* configs = [configsController configs];
134 [configPopup removeAllItems];
135 for(int i=0; i<[configs count]; i++) {
136 [configPopup addItemWithTitle: [[configs objectAtIndex:i]name]];
137 }
138 if(preserve)
139 [configPopup selectItemAtIndex:initialIndex];
140
141 }
142
143 @end