Reset target settings in UI when switching target types.
[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
30 - (IBAction)radioChanged:(id)sender {
31 NSInteger row, col;
32 [radioButtons getRow:&row column:&col ofCell:sender];
33 [[NSApplication sharedApplication].mainWindow makeFirstResponder:sender];
34
35 if (row != 1)
36 keyInput.vk = -1;
37
38 if (row != 2)
39 [configPopup selectItemAtIndex:-1];
40 else if (!configPopup.selectedItem)
41 [configPopup selectItemAtIndex:0];
42
43 if (row != 3)
44 mouseDirSelect.selectedSegment = -1;
45 else if (mouseDirSelect.selectedSegment == -1)
46 mouseDirSelect.selectedSegment = 0;
47
48 if (row != 4)
49 mouseBtnSelect.selectedSegment = -1;
50 else if (mouseBtnSelect.selectedSegment == -1)
51 mouseBtnSelect.selectedSegment = 0;
52
53 if (row != 5)
54 scrollDirSelect.selectedSegment = -1;
55 else if (scrollDirSelect.selectedSegment == -1)
56 scrollDirSelect.selectedSegment = 0;
57
58 [self commit];
59 }
60
61 - (IBAction)mdirChanged:(id)sender {
62 [radioButtons setState:1 atRow:3 column:0];
63 [[NSApplication sharedApplication].mainWindow makeFirstResponder:sender];
64 [self commit];
65 }
66
67 - (IBAction)mbtnChanged:(id)sender {
68 [radioButtons setState:1 atRow:4 column:0];
69 [[NSApplication sharedApplication].mainWindow makeFirstResponder:sender];
70 [self commit];
71 }
72
73 - (IBAction)sdirChanged:(id)sender {
74 [radioButtons setState:1 atRow:5 column:0];
75 [[NSApplication sharedApplication].mainWindow makeFirstResponder:sender];
76 [self commit];
77 }
78
79 - (Target *)makeTarget {
80 switch (radioButtons.selectedRow) {
81 case 0:
82 return nil;
83 case 1:
84 if (keyInput.hasKey) {
85 TargetKeyboard* k = [[TargetKeyboard alloc] init];
86 k.vk = keyInput.vk;
87 return k;
88 } else {
89 return nil;
90 }
91 break;
92 case 2: {
93 TargetConfig *c = [[TargetConfig alloc] init];
94 if (!configPopup.selectedItem)
95 [configPopup selectItemAtIndex:0];
96 c.config = configsController.configs[configPopup.indexOfSelectedItem];
97 return c;
98 }
99 case 3: {
100 TargetMouseMove *mm = [[TargetMouseMove alloc] init];
101 mm.dir = mouseDirSelect.selectedSegment;
102 return mm;
103 }
104 case 4: {
105 TargetMouseBtn *mb = [[TargetMouseBtn alloc] init];
106 mb.which = mouseBtnSelect.selectedSegment == 0 ? kCGMouseButtonLeft : kCGMouseButtonRight;
107 return mb;
108 }
109 case 5: {
110 TargetMouseScroll *ms = [[TargetMouseScroll alloc] init];
111 ms.howMuch = scrollDirSelect.selectedSegment ? 1 : -1;
112 return ms;
113 }
114 case 6: {
115 TargetToggleMouseScope *tms = [[TargetToggleMouseScope alloc] init];
116 return tms;
117 }
118 default:
119 return nil;
120 }
121 }
122
123 -(void)configChosen:(id)sender {
124 [radioButtons setState:1 atRow:2 column:0];
125 [self commit];
126 }
127
128 - (void)commit {
129 configsController.currentConfig[joystickController.selectedAction] = [self makeTarget];
130 }
131
132 - (void)reset {
133 [keyInput clear];
134 [radioButtons setState:1 atRow:0 column:0];
135 [self refreshConfigsPreservingSelection:NO];
136 }
137
138 - (BOOL)enabled {
139 return [radioButtons isEnabled];
140 }
141
142 - (void)setEnabled:(BOOL)enabled {
143 [radioButtons setEnabled:enabled];
144 [keyInput setEnabled:enabled];
145 [configPopup setEnabled:enabled];
146 [mouseDirSelect setEnabled:enabled];
147 [mouseBtnSelect setEnabled:enabled];
148 [scrollDirSelect setEnabled:enabled];
149 }
150 -(void) load {
151 id jsaction = joystickController.selectedAction;
152 currentJsaction = jsaction;
153 if(!jsaction) {
154 self.enabled = NO;
155 title.stringValue = @"";
156 return;
157 } else {
158 self.enabled = YES;
159 }
160 Target *target = configsController.currentConfig[jsaction];
161
162 id act = jsaction;
163 NSString* actFullName = [act name];
164 while([act base]) {
165 act = [act base];
166 actFullName = [[NSString alloc] initWithFormat:@"%@ > %@", [act name], actFullName];
167 }
168 title.stringValue = [[NSString alloc] initWithFormat:@"%@ > %@", configsController.currentConfig.name, actFullName];
169
170 if(!target) {
171 [radioButtons setState:1 atRow:0 column:0];
172 } else if([target isKindOfClass:[TargetKeyboard class]]) {
173 [radioButtons setState:1 atRow:1 column:0];
174 keyInput.vk = [(TargetKeyboard*)target vk];
175 } else if([target isKindOfClass:[TargetConfig class]]) {
176 [radioButtons setState:1 atRow:2 column:0];
177 [configPopup selectItemAtIndex:[configsController.configs
178 indexOfObject:[(TargetConfig *)target config]]];
179 }
180 else if ([target isKindOfClass:[TargetMouseMove class]]) {
181 [radioButtons setState:1 atRow:3 column:0];
182 [mouseDirSelect setSelectedSegment:[(TargetMouseMove *)target dir]];
183 }
184 else if ([target isKindOfClass:[TargetMouseBtn class]]) {
185 [radioButtons setState:1 atRow:4 column:0];
186 mouseBtnSelect.selectedSegment = [(TargetMouseBtn *)target which] == kCGMouseButtonLeft ? 0 : 1;
187 }
188 else if ([target isKindOfClass:[TargetMouseScroll class]]) {
189 [radioButtons setState:1 atRow:5 column:0];
190 scrollDirSelect.selectedSegment = [(TargetMouseScroll *)target howMuch] > 0;
191 }
192 else if ([target isKindOfClass:[TargetToggleMouseScope class]]) {
193 [radioButtons setState:1 atRow:6 column:0];
194 } else {
195 NSLog(@"Unknown target type %@.", target.description);
196 }
197 }
198
199 -(void) focusKey {
200 Target *currentTarget = configsController.currentConfig[currentJsaction];
201 if (!currentTarget || [currentTarget isKindOfClass:[TargetKeyboard class]])
202 [[[NSApplication sharedApplication] mainWindow] makeFirstResponder:keyInput];
203 else
204 [keyInput resignFirstResponder];
205 }
206
207 - (void)refreshConfigsPreservingSelection:(BOOL)preserve {
208 int initialIndex = [configPopup indexOfSelectedItem];
209 [configPopup removeAllItems];
210 for (Config *config in configsController.configs)
211 [configPopup addItemWithTitle:config.name];
212 [configPopup selectItemAtIndex:preserve ? initialIndex : -1];
213 }
214
215 @end