Split view management out of NJDeviceController. Right now this probably just makes...
[enjoyable.git] / Classes / NJOutputController.m
1 //
2 // NJOutputController.m
3 // Enjoy
4 //
5 // Created by Sam McCall on 5/05/09.
6 //
7
8 #import "NJOutputController.h"
9
10 #import "NJMappingsController.h"
11 #import "NJMapping.h"
12 #import "NJInput.h"
13 #import "NJEvents.h"
14 #import "NJDeviceController.h"
15 #import "NJKeyInputField.h"
16 #import "NJOutputMapping.h"
17 #import "NJOutputController.h"
18 #import "NJOutputKeyPress.h"
19 #import "NJOutputMouseButton.h"
20 #import "NJOutputMouseMove.h"
21 #import "NJOutputMouseScroll.h"
22
23 @implementation NJOutputController
24
25 - (id)init {
26 if ((self = [super init])) {
27 [NSNotificationCenter.defaultCenter
28 addObserver:self
29 selector:@selector(mappingListDidChange:)
30 name:NJEventMappingListChanged
31 object:nil];
32 [NSNotificationCenter.defaultCenter
33 addObserver:self
34 selector:@selector(mappingDidChange:)
35 name:NJEventMappingChanged
36 object:nil];
37 }
38 return self;
39 }
40
41 - (void)dealloc {
42 [NSNotificationCenter.defaultCenter removeObserver:self];
43 }
44
45 - (void)cleanUpInterface {
46 NSInteger row = radioButtons.selectedRow;
47
48 if (row != 1) {
49 keyInput.keyCode = NJKeyInputFieldEmpty;
50 [keyInput resignIfFirstResponder];
51 }
52
53 if (row != 2) {
54 [mappingPopup selectItemAtIndex:-1];
55 [mappingPopup resignIfFirstResponder];
56 } else if (!mappingPopup.selectedItem)
57 [mappingPopup selectItemAtIndex:0];
58
59 if (row != 3) {
60 mouseDirSelect.selectedSegment = -1;
61 mouseSpeedSlider.floatValue = mouseSpeedSlider.minValue;
62 [mouseDirSelect resignIfFirstResponder];
63 } else {
64 if (mouseDirSelect.selectedSegment == -1)
65 mouseDirSelect.selectedSegment = 0;
66 if (!mouseSpeedSlider.floatValue)
67 mouseSpeedSlider.floatValue = 10;
68 }
69
70 if (row != 4) {
71 mouseBtnSelect.selectedSegment = -1;
72 [mouseBtnSelect resignIfFirstResponder];
73 } else if (mouseBtnSelect.selectedSegment == -1)
74 mouseBtnSelect.selectedSegment = 0;
75
76 if (row != 5) {
77 scrollDirSelect.selectedSegment = -1;
78 scrollSpeedSlider.floatValue = scrollSpeedSlider.minValue;
79 smoothCheck.state = NSOffState;
80 [scrollDirSelect resignIfFirstResponder];
81 [scrollSpeedSlider resignIfFirstResponder];
82 [smoothCheck resignIfFirstResponder];
83 } else {
84 if (scrollDirSelect.selectedSegment == -1)
85 scrollDirSelect.selectedSegment = 0;
86 }
87
88 }
89
90 - (IBAction)radioChanged:(NSView *)sender {
91 [sender.window makeFirstResponder:sender];
92 if (radioButtons.selectedRow == 1)
93 [keyInput.window makeFirstResponder:keyInput];
94 [self commit];
95 }
96
97 - (void)keyInputField:(NJKeyInputField *)keyInput didChangeKey:(CGKeyCode)keyCode {
98 [radioButtons selectCellAtRow:1 column:0];
99 [radioButtons.window makeFirstResponder:radioButtons];
100 [self commit];
101 }
102
103 - (void)keyInputFieldDidClear:(NJKeyInputField *)keyInput {
104 [radioButtons selectCellAtRow:0 column:0];
105 [self commit];
106 }
107
108 - (void)mappingChosen:(id)sender {
109 [radioButtons selectCellAtRow:2 column:0];
110 [mappingPopup.window makeFirstResponder:mappingPopup];
111 [self commit];
112 }
113
114 - (void)mdirChanged:(NSView *)sender {
115 [radioButtons selectCellAtRow:3 column:0];
116 [sender.window makeFirstResponder:sender];
117 [self commit];
118 }
119
120 - (void)mouseSpeedChanged:(NSSlider *)sender {
121 [radioButtons selectCellAtRow:3 column:0];
122 [sender.window makeFirstResponder:sender];
123 [self commit];
124 }
125
126 - (void)mbtnChanged:(NSView *)sender {
127 [radioButtons selectCellAtRow:4 column:0];
128 [sender.window makeFirstResponder:sender];
129 [self commit];
130 }
131
132 - (void)sdirChanged:(NSView *)sender {
133 [radioButtons selectCellAtRow:5 column:0];
134 [sender.window makeFirstResponder:sender];
135 [self commit];
136 }
137
138 - (void)scrollSpeedChanged:(NSSlider *)sender {
139 [radioButtons selectCellAtRow:5 column:0];
140 [sender.window makeFirstResponder:sender];
141 [self commit];
142 }
143
144 - (IBAction)scrollTypeChanged:(NSButton *)sender {
145 [radioButtons selectCellAtRow:5 column:0];
146 [sender.window makeFirstResponder:sender];
147 if (sender.state == NSOnState) {
148 scrollSpeedSlider.floatValue =
149 scrollSpeedSlider.minValue + (scrollSpeedSlider.maxValue - scrollSpeedSlider.minValue) / 2;
150 scrollSpeedSlider.enabled = YES;
151 } else {
152 scrollSpeedSlider.floatValue = scrollSpeedSlider.minValue;
153 scrollSpeedSlider.enabled = NO;
154 }
155 [self commit];
156 }
157
158 - (NJOutput *)currentOutput {
159 return mappingsController.currentMapping[inputController.selectedInput];
160 }
161
162 - (NJOutput *)makeOutput {
163 switch (radioButtons.selectedRow) {
164 case 0:
165 return nil;
166 case 1:
167 if (keyInput.hasKeyCode) {
168 NJOutputKeyPress *k = [[NJOutputKeyPress alloc] init];
169 k.keyCode = keyInput.keyCode;
170 return k;
171 } else {
172 return nil;
173 }
174 break;
175 case 2: {
176 NJOutputMapping *c = [[NJOutputMapping alloc] init];
177 c.mapping = mappingsController[mappingPopup.indexOfSelectedItem];
178 return c;
179 }
180 case 3: {
181 NJOutputMouseMove *mm = [[NJOutputMouseMove alloc] init];
182 mm.axis = mouseDirSelect.selectedSegment;
183 mm.speed = mouseSpeedSlider.floatValue;
184 return mm;
185 }
186 case 4: {
187 NJOutputMouseButton *mb = [[NJOutputMouseButton alloc] init];
188 mb.button = [mouseBtnSelect.cell tagForSegment:mouseBtnSelect.selectedSegment];
189 return mb;
190 }
191 case 5: {
192 NJOutputMouseScroll *ms = [[NJOutputMouseScroll alloc] init];
193 ms.direction = [scrollDirSelect.cell tagForSegment:scrollDirSelect.selectedSegment];
194 ms.speed = scrollSpeedSlider.floatValue;
195 ms.smooth = smoothCheck.state == NSOnState;
196 return ms;
197 }
198 default:
199 return nil;
200 }
201 }
202
203 - (void)commit {
204 [self cleanUpInterface];
205 mappingsController.currentMapping[inputController.selectedInput] = [self makeOutput];
206 [mappingsController save];
207 }
208
209 - (BOOL)enabled {
210 return radioButtons.isEnabled;
211 }
212
213 - (void)setEnabled:(BOOL)enabled {
214 radioButtons.enabled = enabled;
215 keyInput.enabled = enabled;
216 mappingPopup.enabled = enabled;
217 mouseDirSelect.enabled = enabled;
218 mouseSpeedSlider.enabled = enabled;
219 mouseBtnSelect.enabled = enabled;
220 scrollDirSelect.enabled = enabled;
221 smoothCheck.enabled = enabled;
222 scrollSpeedSlider.enabled = enabled && smoothCheck.state;
223 }
224
225 - (void)loadOutput:(NJOutput *)output forInput:(NJInput *)input {
226 if (!input) {
227 self.enabled = NO;
228 title.stringValue = @"";
229 } else {
230 self.enabled = YES;
231 NSString *inpFullName = input.name;
232 for (NJInputPathElement *cur = input.parent; cur; cur = cur.parent) {
233 inpFullName = [[NSString alloc] initWithFormat:@"%@ ▸ %@", cur.name, inpFullName];
234 }
235 title.stringValue = inpFullName;
236 }
237
238 if ([output isKindOfClass:NJOutputKeyPress.class]) {
239 [radioButtons selectCellAtRow:1 column:0];
240 keyInput.keyCode = [(NJOutputKeyPress*)output keyCode];
241 } else if ([output isKindOfClass:NJOutputMapping.class]) {
242 [radioButtons selectCellAtRow:2 column:0];
243 NSMenuItem *item = [mappingPopup itemWithRepresentedObject:[(NJOutputMapping *)output mapping]];
244 [mappingPopup selectItem:item];
245 if (!item)
246 [radioButtons selectCellAtRow:self.enabled ? 0 : -1 column:0];
247 }
248 else if ([output isKindOfClass:NJOutputMouseMove.class]) {
249 [radioButtons selectCellAtRow:3 column:0];
250 mouseDirSelect.selectedSegment = [(NJOutputMouseMove *)output axis];
251 mouseSpeedSlider.floatValue = [(NJOutputMouseMove *)output speed];
252 }
253 else if ([output isKindOfClass:NJOutputMouseButton.class]) {
254 [radioButtons selectCellAtRow:4 column:0];
255 [mouseBtnSelect selectSegmentWithTag:[(NJOutputMouseButton *)output button]];
256 }
257 else if ([output isKindOfClass:NJOutputMouseScroll.class]) {
258 [radioButtons selectCellAtRow:5 column:0];
259 int direction = [(NJOutputMouseScroll *)output direction];
260 float speed = [(NJOutputMouseScroll *)output speed];
261 BOOL smooth = [(NJOutputMouseScroll *)output smooth];
262 [scrollDirSelect selectSegmentWithTag:direction];
263 scrollSpeedSlider.floatValue = speed;
264 smoothCheck.state = smooth ? NSOnState : NSOffState;
265 scrollSpeedSlider.enabled = smooth;
266 } else {
267 [radioButtons selectCellAtRow:self.enabled ? 0 : -1 column:0];
268 }
269 [self cleanUpInterface];
270 }
271
272 - (void)loadCurrent {
273 [self loadOutput:self.currentOutput forInput:inputController.selectedInput];
274 }
275
276 - (void)focusKey {
277 if (radioButtons.selectedRow <= 1)
278 [keyInput.window makeFirstResponder:keyInput];
279 else
280 [keyInput resignIfFirstResponder];
281 }
282
283 - (void)mappingListDidChange:(NSNotification *)note {
284 NSArray *mappings = note.userInfo[NJMappingListKey];
285 NJMapping *current = mappingPopup.selectedItem.representedObject;
286 [mappingPopup.menu removeAllItems];
287 for (NJMapping *mapping in mappings) {
288 NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:mapping.name
289 action:@selector(mappingChosen:)
290 keyEquivalent:@""];
291 item.target = self;
292 item.representedObject = mapping;
293 [mappingPopup.menu addItem:item];
294 }
295 [mappingPopup selectItemWithRepresentedObject:current];
296 }
297
298 - (void)mappingDidChange:(NSNotification *)note {
299 [self loadCurrent];
300 }
301
302 @end