Rename 'stringify' to 'uid' where it pertains to unique action IDs.
[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 JSAction *action = [joystickController selectedAction];
97 if (action) {
98 configsController.currentConfig[action] = self.state;
99 }
100 }
101
102 -(void) reset {
103 [keyInput clear];
104 [radioButtons setState: 1 atRow: 0 column: 0];
105 [mouseDirSelect setSelectedSegment: 0];
106 [mouseBtnSelect setSelectedSegment: 0];
107 [scrollDirSelect setSelectedSegment: 0];
108 [self refreshConfigsPreservingSelection: NO];
109 }
110
111 -(void) setEnabled: (BOOL) enabled {
112 [radioButtons setEnabled: enabled];
113 [keyInput setEnabled: enabled];
114 [configPopup setEnabled: enabled];
115 [mouseDirSelect setEnabled: enabled];
116 [mouseBtnSelect setEnabled: enabled];
117 [scrollDirSelect setEnabled: enabled];
118 }
119 -(BOOL) enabled {
120 return [radioButtons isEnabled];
121 }
122
123 -(void) load {
124 id jsaction = [joystickController selectedAction];
125 currentJsaction = jsaction;
126 if(!jsaction) {
127 [self setEnabled: NO];
128 [title setStringValue: @""];
129 return;
130 } else {
131 [self setEnabled: YES];
132 }
133 Target* target = configsController.currentConfig[jsaction];
134
135 id act = jsaction;
136 NSString* actFullName = [act name];
137 while([act base]) {
138 act = [act base];
139 actFullName = [[NSString alloc] initWithFormat: @"%@ > %@", [act name], actFullName];
140 }
141 [title setStringValue: [[NSString alloc] initWithFormat: @"%@ > %@", [[configsController currentConfig] name], actFullName]];
142
143 if(!target) {
144 // already reset
145 } else if([target isKindOfClass: [TargetKeyboard class]]) {
146 [radioButtons setState:1 atRow: 1 column: 0];
147 [keyInput setVk: [(TargetKeyboard*)target vk]];
148 } else if([target isKindOfClass: [TargetConfig class]]) {
149 [radioButtons setState:1 atRow: 2 column: 0];
150 [configPopup selectItemAtIndex: [[configsController configs] indexOfObject: [(TargetConfig*)target config]]];
151 }
152 else if ([target isKindOfClass: [TargetMouseMove class]]) {
153 [radioButtons setState:1 atRow: 3 column: 0];
154 [mouseDirSelect setSelectedSegment: [(TargetMouseMove *)target dir]];
155 }
156 else if ([target isKindOfClass: [TargetMouseBtn class]]) {
157 [radioButtons setState: 1 atRow: 4 column: 0];
158 if ([(TargetMouseBtn *)target which] == kCGMouseButtonLeft)
159 [mouseBtnSelect setSelectedSegment: 0];
160 else
161 [mouseBtnSelect setSelectedSegment: 1];
162 }
163 else if ([target isKindOfClass: [TargetMouseScroll class]]) {
164 [radioButtons setState: 1 atRow: 5 column: 0];
165 if ([(TargetMouseScroll *)target howMuch] < 0)
166 [scrollDirSelect setSelectedSegment: 0];
167 else
168 [scrollDirSelect setSelectedSegment: 1];
169 }
170 else if ([target isKindOfClass: [TargetToggleMouseScope class]]) {
171 [radioButtons setState: 1 atRow: 6 column: 0];
172 } else {
173 [NSException raise:@"Unknown target subclass" format:@"Unknown target subclass"];
174 }
175 }
176
177 -(void) focusKey {
178 [[[NSApplication sharedApplication] mainWindow] makeFirstResponder: keyInput];
179 }
180
181 -(void) refreshConfigsPreservingSelection: (BOOL) preserve {
182 int initialIndex = [configPopup indexOfSelectedItem];
183
184 NSArray* configs = [configsController configs];
185 [configPopup removeAllItems];
186 for(int i=0; i<[configs count]; i++) {
187 [configPopup addItemWithTitle: [configs[i]name]];
188 }
189 if(preserve)
190 [configPopup selectItemAtIndex:initialIndex];
191
192 }
193
194 @end