8afaaf2fc6056d2f79140875fde15dff400f767d
[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 return k;
59 }
60 break;
61 case 2:
62 {
63 TargetConfig* c = [[TargetConfig alloc] init];
64 [c setConfig: [configsController configs][[configPopup indexOfSelectedItem]]];
65 return c;
66 }
67 case 3: {
68 // mouse X/Y
69 TargetMouseMove *mm = [[TargetMouseMove alloc] init];
70 [mm setDir: [mouseDirSelect selectedSegment]];
71 return mm;
72 }
73 case 4: {
74 // mouse button
75 TargetMouseBtn *mb = [[TargetMouseBtn alloc] init];
76 if ([mouseBtnSelect selectedSegment] == 0) {
77 [mb setWhich: kCGMouseButtonLeft];
78 }
79 else {
80 [mb setWhich: kCGMouseButtonRight];
81 }
82 return mb;
83 }
84 case 5: {
85 // scroll
86 TargetMouseScroll *ms = [[TargetMouseScroll alloc] init];
87 if ([scrollDirSelect selectedSegment] == 0) {
88 [ms setHowMuch: -1];
89 }
90 else {
91 [ms setHowMuch: 1];
92 }
93 return ms;
94 }
95 case 6: {
96 // toggle mouse scope
97 TargetToggleMouseScope *tms = [[TargetToggleMouseScope alloc] init];
98 return tms;
99 }
100 }
101 return NULL;
102 }
103
104 -(void)configChosen:(id)sender {
105 [radioButtons setState: 1 atRow: 2 column: 0];
106 [self commit];
107 }
108
109 -(void) commit {
110 JSAction *action = [joystickController selectedAction];
111 if (action) {
112 configsController.currentConfig[action] = self.state;
113 }
114 }
115
116 -(void) reset {
117 [keyInput clear];
118 [radioButtons setState: 1 atRow: 0 column: 0];
119 [mouseDirSelect setSelectedSegment: 0];
120 [mouseBtnSelect setSelectedSegment: 0];
121 [scrollDirSelect setSelectedSegment: 0];
122 [self refreshConfigsPreservingSelection: NO];
123 }
124
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];
132 }
133 -(BOOL) enabled {
134 return [radioButtons isEnabled];
135 }
136
137 -(void) load {
138 id jsaction = [joystickController selectedAction];
139 currentJsaction = jsaction;
140 if(!jsaction) {
141 [self setEnabled: NO];
142 [title setStringValue: @""];
143 return;
144 } else {
145 [self setEnabled: YES];
146 }
147 Target* target = configsController.currentConfig[jsaction];
148
149 id act = jsaction;
150 NSString* actFullName = [act name];
151 while([act base]) {
152 act = [act base];
153 actFullName = [[NSString alloc] initWithFormat: @"%@ > %@", [act name], actFullName];
154 }
155 [title setStringValue: [[NSString alloc] initWithFormat: @"%@ > %@", [[configsController currentConfig] name], actFullName]];
156
157 if(!target) {
158 // already reset
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]]];
165 }
166 else if ([target isKindOfClass: [TargetMouseMove class]]) {
167 [radioButtons setState:1 atRow: 3 column: 0];
168 [mouseDirSelect setSelectedSegment: [(TargetMouseMove *)target dir]];
169 }
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];
174 else
175 [mouseBtnSelect setSelectedSegment: 1];
176 }
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];
181 else
182 [scrollDirSelect setSelectedSegment: 1];
183 }
184 else if ([target isKindOfClass: [TargetToggleMouseScope class]]) {
185 [radioButtons setState: 1 atRow: 6 column: 0];
186 } else {
187 [NSException raise:@"Unknown target subclass" format:@"Unknown target subclass"];
188 }
189 }
190
191 -(void) focusKey {
192 [[[NSApplication sharedApplication] mainWindow] makeFirstResponder: keyInput];
193 }
194
195 -(void) refreshConfigsPreservingSelection: (BOOL) preserve {
196 int initialIndex = [configPopup indexOfSelectedItem];
197
198 NSArray* configs = [configsController configs];
199 [configPopup removeAllItems];
200 for(int i=0; i<[configs count]; i++) {
201 [configPopup addItemWithTitle: [configs[i]name]];
202 }
203 if(preserve)
204 [configPopup selectItemAtIndex:initialIndex];
205
206 }
207
208 @end