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