2 // NJOutputController.m
5 // Created by Sam McCall on 5/05/09.
8 #import "NJOutputController.h"
10 #import "NJMappingsController.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"
23 @implementation NJOutputController
26 if ((self = [super init])) {
27 [NSNotificationCenter.defaultCenter
29 selector:@selector(mappingListDidChange:)
30 name:NJEventMappingListChanged
37 [NSNotificationCenter.defaultCenter removeObserver:self];
40 - (void)cleanUpInterface {
41 NSInteger row = radioButtons.selectedRow;
44 keyInput.keyCode = NJKeyInputFieldEmpty;
45 [keyInput resignIfFirstResponder];
49 [mappingPopup selectItemAtIndex:-1];
50 [mappingPopup resignIfFirstResponder];
51 } else if (!mappingPopup.selectedItem)
52 [mappingPopup selectItemAtIndex:0];
55 mouseDirSelect.selectedSegment = -1;
56 mouseSpeedSlider.floatValue = mouseSpeedSlider.minValue;
57 [mouseDirSelect resignIfFirstResponder];
59 if (mouseDirSelect.selectedSegment == -1)
60 mouseDirSelect.selectedSegment = 0;
61 if (!mouseSpeedSlider.floatValue)
62 mouseSpeedSlider.floatValue = 10;
66 mouseBtnSelect.selectedSegment = -1;
67 [mouseBtnSelect resignIfFirstResponder];
68 } else if (mouseBtnSelect.selectedSegment == -1)
69 mouseBtnSelect.selectedSegment = 0;
72 scrollDirSelect.selectedSegment = -1;
73 scrollSpeedSlider.floatValue = scrollSpeedSlider.minValue;
74 [scrollDirSelect resignIfFirstResponder];
76 if (scrollDirSelect.selectedSegment == -1)
77 scrollDirSelect.selectedSegment = 0;
78 if (scrollDirSelect.selectedSegment < 2
79 && !scrollSpeedSlider.floatValue)
80 scrollSpeedSlider.floatValue = 15;
81 else if (scrollDirSelect.selectedSegment >= 2
82 && scrollSpeedSlider.floatValue)
83 scrollSpeedSlider.floatValue = scrollSpeedSlider.minValue;
88 - (IBAction)radioChanged:(NSView *)sender {
89 [sender.window makeFirstResponder:sender];
90 if (radioButtons.selectedRow == 1)
91 [keyInput.window makeFirstResponder:keyInput];
95 - (void)keyInputField:(NJKeyInputField *)keyInput didChangeKey:(CGKeyCode)keyCode {
96 [radioButtons selectCellAtRow:1 column:0];
97 [radioButtons.window makeFirstResponder:radioButtons];
101 - (void)keyInputFieldDidClear:(NJKeyInputField *)keyInput {
102 [radioButtons selectCellAtRow:0 column:0];
106 - (void)mappingChosen:(id)sender {
107 [radioButtons selectCellAtRow:2 column:0];
108 [mappingPopup.window makeFirstResponder:mappingPopup];
112 - (void)mdirChanged:(NSView *)sender {
113 [radioButtons selectCellAtRow:3 column:0];
114 [sender.window makeFirstResponder:sender];
118 - (void)mouseSpeedChanged:(NSSlider *)sender {
119 [radioButtons selectCellAtRow:3 column:0];
120 [sender.window makeFirstResponder:sender];
124 - (void)mbtnChanged:(NSView *)sender {
125 [radioButtons selectCellAtRow:4 column:0];
126 [sender.window makeFirstResponder:sender];
130 - (void)sdirChanged:(NSView *)sender {
131 [radioButtons selectCellAtRow:5 column:0];
132 if (scrollDirSelect.selectedSegment >= 2)
133 scrollSpeedSlider.floatValue = 0;
134 [sender.window makeFirstResponder:sender];
138 - (void)scrollSpeedChanged:(NSSlider *)sender {
139 [radioButtons selectCellAtRow:5 column:0];
140 [sender.window makeFirstResponder:sender];
141 if (!sender.floatValue && scrollDirSelect.selectedSegment < 2)
142 scrollDirSelect.selectedSegment += 2;
143 else if (sender.floatValue && scrollDirSelect.selectedSegment >= 2)
144 scrollDirSelect.selectedSegment -= 2;
148 - (NJOutput *)currentOutput {
149 return mappingsController.currentMapping[inputController.selectedInput];
152 - (NJOutput *)makeOutput {
153 switch (radioButtons.selectedRow) {
157 if (keyInput.hasKeyCode) {
158 NJOutputKeyPress *k = [[NJOutputKeyPress alloc] init];
159 k.vk = keyInput.keyCode;
166 NJOutputMapping *c = [[NJOutputMapping alloc] init];
167 c.mapping = mappingsController[mappingPopup.indexOfSelectedItem];
171 NJOutputMouseMove *mm = [[NJOutputMouseMove alloc] init];
172 mm.axis = mouseDirSelect.selectedSegment;
173 mm.speed = mouseSpeedSlider.floatValue;
177 NJOutputMouseButton *mb = [[NJOutputMouseButton alloc] init];
178 mb.humanIndexedButton = mouseBtnSelect.selectedSegment;
182 NJOutputMouseScroll *ms = [[NJOutputMouseScroll alloc] init];
183 ms.direction = (scrollDirSelect.selectedSegment & 1) ? 1 : -1;
184 ms.speed = scrollDirSelect.selectedSegment < 2
185 ? scrollSpeedSlider.floatValue
195 [self cleanUpInterface];
196 mappingsController.currentMapping[inputController.selectedInput] = [self makeOutput];
197 [mappingsController save];
201 return [radioButtons isEnabled];
204 - (void)setEnabled:(BOOL)enabled {
205 [radioButtons setEnabled:enabled];
206 [keyInput setEnabled:enabled];
207 [mappingPopup setEnabled:enabled];
208 [mouseDirSelect setEnabled:enabled];
209 [mouseSpeedSlider setEnabled:enabled];
210 [mouseBtnSelect setEnabled:enabled];
211 [scrollDirSelect setEnabled:enabled];
212 [scrollSpeedSlider setEnabled:enabled];
215 - (void)loadOutput:(NJOutput *)output forInput:(NJInput *)input {
218 title.stringValue = @"";
221 NSString *inpFullName = input.name;
222 for (id <NJInputPathElement> cur = input.base; cur; cur = cur.base) {
223 inpFullName = [[NSString alloc] initWithFormat:@"%@ > %@", cur.name, inpFullName];
225 title.stringValue = inpFullName;
228 if ([output isKindOfClass:NJOutputKeyPress.class]) {
229 [radioButtons selectCellAtRow:1 column:0];
230 keyInput.keyCode = [(NJOutputKeyPress*)output vk];
231 } else if ([output isKindOfClass:NJOutputMapping.class]) {
232 [radioButtons selectCellAtRow:2 column:0];
233 NSMenuItem *item = [mappingPopup itemWithRepresentedObject:[(NJOutputMapping *)output mapping]];
234 [mappingPopup selectItem:item];
236 [radioButtons selectCellAtRow:self.enabled ? 0 : -1 column:0];
238 else if ([output isKindOfClass:NJOutputMouseMove.class]) {
239 [radioButtons selectCellAtRow:3 column:0];
240 mouseDirSelect.selectedSegment = [(NJOutputMouseMove *)output axis];
241 mouseSpeedSlider.floatValue = [(NJOutputMouseMove *)output speed];
243 else if ([output isKindOfClass:NJOutputMouseButton.class]) {
244 [radioButtons selectCellAtRow:4 column:0];
245 mouseBtnSelect.selectedSegment = [(NJOutputMouseButton *)output humanIndexedButton];
247 else if ([output isKindOfClass:NJOutputMouseScroll.class]) {
248 [radioButtons selectCellAtRow:5 column:0];
249 int direction = [(NJOutputMouseScroll *)output direction];
250 float speed = [(NJOutputMouseScroll *)output speed];
251 scrollDirSelect.selectedSegment = (direction > 0) + !speed * 2;
252 scrollSpeedSlider.floatValue = speed;
254 [radioButtons selectCellAtRow:self.enabled ? 0 : -1 column:0];
256 [self cleanUpInterface];
259 - (void)loadCurrent {
260 [self loadOutput:self.currentOutput forInput:inputController.selectedInput];
264 if (radioButtons.selectedRow <= 1)
265 [keyInput.window makeFirstResponder:keyInput];
267 [keyInput resignIfFirstResponder];
270 - (void)mappingListDidChange:(NSNotification *)note {
271 NSArray *mappings = note.object;
272 NJMapping *current = mappingPopup.selectedItem.representedObject;
273 [mappingPopup.menu removeAllItems];
274 for (NJMapping *mapping in mappings) {
275 NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:mapping.name
276 action:@selector(mappingChosen:)
279 item.representedObject = mapping;
280 [mappingPopup.menu addItem:item];
282 [mappingPopup selectItemWithRepresentedObject:current];