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 = 4;
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.f;
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 [sender.window makeFirstResponder:sender];
136 - (void)scrollSpeedChanged:(NSSlider *)sender {
137 [radioButtons selectCellAtRow:5 column:0];
138 [sender.window makeFirstResponder:sender];
139 if (!sender.floatValue && scrollDirSelect.selectedSegment < 2)
140 scrollDirSelect.selectedSegment += 2;
141 else if (sender.floatValue && scrollDirSelect.selectedSegment >= 2)
142 scrollDirSelect.selectedSegment -= 2;
146 - (NJOutput *)currentOutput {
147 return mappingsController.currentMapping[inputController.selectedInput];
150 - (NJOutput *)makeOutput {
151 switch (radioButtons.selectedRow) {
155 if (keyInput.hasKeyCode) {
156 NJOutputKeyPress *k = [[NJOutputKeyPress alloc] init];
157 k.vk = keyInput.keyCode;
164 NJOutputMapping *c = [[NJOutputMapping alloc] init];
165 c.mapping = mappingsController[mappingPopup.indexOfSelectedItem];
169 NJOutputMouseMove *mm = [[NJOutputMouseMove alloc] init];
170 mm.axis = mouseDirSelect.selectedSegment;
171 mm.speed = mouseSpeedSlider.floatValue;
175 NJOutputMouseButton *mb = [[NJOutputMouseButton alloc] init];
176 mb.button = mouseBtnSelect.selectedSegment == 0 ? kCGMouseButtonLeft : kCGMouseButtonRight;
180 NJOutputMouseScroll *ms = [[NJOutputMouseScroll alloc] init];
181 ms.direction = (scrollDirSelect.selectedSegment & 1) ? 1 : -1;
182 ms.speed = scrollDirSelect.selectedSegment < 2
183 ? scrollSpeedSlider.floatValue
193 [self cleanUpInterface];
194 mappingsController.currentMapping[inputController.selectedInput] = [self makeOutput];
195 [mappingsController save];
199 return [radioButtons isEnabled];
202 - (void)setEnabled:(BOOL)enabled {
203 [radioButtons setEnabled:enabled];
204 [keyInput setEnabled:enabled];
205 [mappingPopup setEnabled:enabled];
206 [mouseDirSelect setEnabled:enabled];
207 [mouseSpeedSlider setEnabled:enabled];
208 [mouseBtnSelect setEnabled:enabled];
209 [scrollDirSelect setEnabled:enabled];
212 - (void)loadOutput:(NJOutput *)output forInput:(NJInput *)input {
215 title.stringValue = @"";
218 NSString *inpFullName = input.name;
219 for (id <NJInputPathElement> cur = input.base; cur; cur = cur.base) {
220 inpFullName = [[NSString alloc] initWithFormat:@"%@ > %@", cur.name, inpFullName];
222 title.stringValue = inpFullName;
225 if ([output isKindOfClass:NJOutputKeyPress.class]) {
226 [radioButtons selectCellAtRow:1 column:0];
227 keyInput.keyCode = [(NJOutputKeyPress*)output vk];
228 } else if ([output isKindOfClass:NJOutputMapping.class]) {
229 [radioButtons selectCellAtRow:2 column:0];
230 NSMenuItem *item = [mappingPopup itemWithRepresentedObject:[(NJOutputMapping *)output mapping]];
231 [mappingPopup selectItem:item];
233 [radioButtons selectCellAtRow:self.enabled ? 0 : -1 column:0];
235 else if ([output isKindOfClass:NJOutputMouseMove.class]) {
236 [radioButtons selectCellAtRow:3 column:0];
237 mouseDirSelect.selectedSegment = [(NJOutputMouseMove *)output axis];
238 mouseSpeedSlider.floatValue = [(NJOutputMouseMove *)output speed];
240 else if ([output isKindOfClass:NJOutputMouseButton.class]) {
241 [radioButtons selectCellAtRow:4 column:0];
242 mouseBtnSelect.selectedSegment = [(NJOutputMouseButton *)output button] == kCGMouseButtonLeft ? 0 : 1;
244 else if ([output isKindOfClass:NJOutputMouseScroll.class]) {
245 [radioButtons selectCellAtRow:5 column:0];
246 int direction = [(NJOutputMouseScroll *)output direction];
247 float speed = [(NJOutputMouseScroll *)output speed];
248 scrollDirSelect.selectedSegment = (direction > 0) + !speed * 2;
249 scrollSpeedSlider.floatValue = speed;
251 [radioButtons selectCellAtRow:self.enabled ? 0 : -1 column:0];
253 [self cleanUpInterface];
256 - (void)loadCurrent {
257 [self loadOutput:self.currentOutput forInput:inputController.selectedInput];
261 if (radioButtons.selectedRow <= 1)
262 [keyInput.window makeFirstResponder:keyInput];
264 [keyInput resignIfFirstResponder];
267 - (void)mappingListDidChange:(NSNotification *)note {
268 NSArray *mappings = note.object;
269 NJMapping *current = mappingPopup.selectedItem.representedObject;
270 [mappingPopup.menu removeAllItems];
271 for (NJMapping *mapping in mappings) {
272 NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:mapping.name
273 action:@selector(mappingChosen:)
276 item.representedObject = mapping;
277 [mappingPopup.menu addItem:item];
279 [mappingPopup selectItemWithRepresentedObject:current];