3fc0f288ad9ce7033e17e70732e0cf5028a316b9
[enjoyable.git] / TargetController.m
1 //
2 // TargetController.m
3 // Enjoy
4 //
5 // Created by Sam McCall on 5/05/09.
6 //
7
8 #import "TargetController.h"
9
10 #import "ConfigsController.h"
11 #import "Config.h"
12 #import "JSAction.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"
22
23 @implementation TargetController
24
25 -(void) keyChanged {
26 [radioButtons setState: 1 atRow: 1 column: 0 ];
27 [self commit];
28 }
29 -(IBAction)radioChanged:(id)sender {
30 [[[NSApplication sharedApplication] mainWindow] makeFirstResponder: sender];
31 [self commit];
32 }
33 -(IBAction)mdirChanged:(id)sender {
34 [radioButtons setState: 1 atRow: 3 column: 0];
35 [[[NSApplication sharedApplication] mainWindow] makeFirstResponder: sender];
36 [self commit];
37 }
38 -(IBAction)mbtnChanged:(id)sender {
39 [radioButtons setState: 1 atRow: 4 column: 0];
40 [[[NSApplication sharedApplication] mainWindow] makeFirstResponder: sender];
41 [self commit];
42 }
43 -(IBAction)sdirChanged:(id)sender {
44 [radioButtons setState: 1 atRow: 5 column: 0];
45 [[[NSApplication sharedApplication] mainWindow] makeFirstResponder: sender];
46 [self commit];
47 }
48
49
50 -(Target*) state {
51 switch([radioButtons selectedRow]) {
52 case 0: // none
53 return NULL;
54 case 1: // key
55 if(keyInput.hasKey) {
56 TargetKeyboard* k = [[TargetKeyboard alloc] init];
57 [k setVk: [keyInput vk]];
58 [k setDescr: [keyInput descr]];
59 return k;
60 }
61 break;
62 case 2:
63 {
64 TargetConfig* c = [[TargetConfig alloc] init];
65 [c setConfig: [configsController configs][[configPopup indexOfSelectedItem]]];
66 return c;
67 }
68 case 3: {
69 // mouse X/Y
70 TargetMouseMove *mm = [[TargetMouseMove alloc] init];
71 [mm setDir: [mouseDirSelect selectedSegment]];
72 return mm;
73 }
74 case 4: {
75 // mouse button
76 TargetMouseBtn *mb = [[TargetMouseBtn alloc] init];
77 if ([mouseBtnSelect selectedSegment] == 0) {
78 [mb setWhich: kCGMouseButtonLeft];
79 }
80 else {
81 [mb setWhich: kCGMouseButtonRight];
82 }
83 return mb;
84 }
85 case 5: {
86 // scroll
87 TargetMouseScroll *ms = [[TargetMouseScroll alloc] init];
88 if ([scrollDirSelect selectedSegment] == 0) {
89 [ms setHowMuch: -1];
90 }
91 else {
92 [ms setHowMuch: 1];
93 }
94 return ms;
95 }
96 case 6: {
97 // toggle mouse scope
98 TargetToggleMouseScope *tms = [[TargetToggleMouseScope alloc] init];
99 return tms;
100 }
101 }
102 return NULL;
103 }
104
105 -(void)configChosen:(id)sender {
106 [radioButtons setState: 1 atRow: 2 column: 0];
107 [self commit];
108 }
109
110 -(void) commit {
111 JSAction *action = [joystickController selectedAction];
112 if (action) {
113 configsController.currentConfig[action] = self.state;
114 }
115 }
116
117 -(void) reset {
118 [keyInput clear];
119 [radioButtons setState: 1 atRow: 0 column: 0];
120 [mouseDirSelect setSelectedSegment: 0];
121 [mouseBtnSelect setSelectedSegment: 0];
122 [scrollDirSelect setSelectedSegment: 0];
123 [self refreshConfigsPreservingSelection: NO];
124 }
125
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];
133 }
134 -(BOOL) enabled {
135 return [radioButtons isEnabled];
136 }
137
138 -(void) load {
139 id jsaction = [joystickController selectedAction];
140 currentJsaction = jsaction;
141 if(!jsaction) {
142 [self setEnabled: NO];
143 [title setStringValue: @""];
144 return;
145 } else {
146 [self setEnabled: YES];
147 }
148 Target* target = configsController.currentConfig[jsaction];
149
150 id act = jsaction;
151 NSString* actFullName = [act name];
152 while([act base]) {
153 act = [act base];
154 actFullName = [[NSString alloc] initWithFormat: @"%@ > %@", [act name], actFullName];
155 }
156 [title setStringValue: [[NSString alloc] initWithFormat: @"%@ > %@", [[configsController currentConfig] name], actFullName]];
157
158 if(!target) {
159 // already reset
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]]];
166 }
167 else if ([target isKindOfClass: [TargetMouseMove class]]) {
168 [radioButtons setState:1 atRow: 3 column: 0];
169 [mouseDirSelect setSelectedSegment: [(TargetMouseMove *)target dir]];
170 }
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];
175 else
176 [mouseBtnSelect setSelectedSegment: 1];
177 }
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];
182 else
183 [scrollDirSelect setSelectedSegment: 1];
184 }
185 else if ([target isKindOfClass: [TargetToggleMouseScope class]]) {
186 [radioButtons setState: 1 atRow: 6 column: 0];
187 } else {
188 [NSException raise:@"Unknown target subclass" format:@"Unknown target subclass"];
189 }
190 }
191
192 -(void) focusKey {
193 [[[NSApplication sharedApplication] mainWindow] makeFirstResponder: keyInput];
194 }
195
196 -(void) refreshConfigsPreservingSelection: (BOOL) preserve {
197 int initialIndex = [configPopup indexOfSelectedItem];
198
199 NSArray* configs = [configsController configs];
200 [configPopup removeAllItems];
201 for(int i=0; i<[configs count]; i++) {
202 [configPopup addItemWithTitle: [configs[i]name]];
203 }
204 if(preserve)
205 [configPopup selectItemAtIndex:initialIndex];
206
207 }
208
209 @end