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