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"
22 #import "NJOutputSwitchMouseMode.h"
24 @implementation NJOutputController
27 if ((self = [super init])) {
28 [NSNotificationCenter.defaultCenter
30 selector:@selector(mappingListDidChange:)
31 name:NJEventMappingListChanged
38 [NSNotificationCenter.defaultCenter removeObserver:self];
41 - (void)cleanUpInterface {
42 NSInteger row = radioButtons.selectedRow;
45 keyInput.keyCode = NJKeyInputFieldEmpty;
46 [keyInput resignIfFirstResponder];
50 [mappingPopup selectItemAtIndex:-1];
51 [mappingPopup resignIfFirstResponder];
52 } else if (!mappingPopup.selectedItem)
53 [mappingPopup selectItemAtIndex:0];
56 mouseDirSelect.selectedSegment = -1;
57 [mouseDirSelect resignIfFirstResponder];
58 } else if (mouseDirSelect.selectedSegment == -1)
59 mouseDirSelect.selectedSegment = 0;
62 mouseBtnSelect.selectedSegment = -1;
63 [mouseBtnSelect resignIfFirstResponder];
64 } else if (mouseBtnSelect.selectedSegment == -1)
65 mouseBtnSelect.selectedSegment = 0;
68 scrollDirSelect.selectedSegment = -1;
69 [scrollDirSelect resignIfFirstResponder];
70 } else if (scrollDirSelect.selectedSegment == -1)
71 scrollDirSelect.selectedSegment = 0;
74 - (IBAction)radioChanged:(NSView *)sender {
75 [sender.window makeFirstResponder:sender];
76 if (radioButtons.selectedRow == 1)
77 [keyInput.window makeFirstResponder:keyInput];
81 - (void)keyInputField:(NJKeyInputField *)keyInput didChangeKey:(CGKeyCode)keyCode {
82 [radioButtons selectCellAtRow:1 column:0];
83 [radioButtons.window makeFirstResponder:radioButtons];
87 - (void)keyInputFieldDidClear:(NJKeyInputField *)keyInput {
88 [radioButtons selectCellAtRow:0 column:0];
92 - (void)mappingChosen:(id)sender {
93 [radioButtons selectCellAtRow:2 column:0];
94 [mappingPopup.window makeFirstResponder:mappingPopup];
98 - (void)mdirChanged:(NSView *)sender {
99 [radioButtons selectCellAtRow:3 column:0];
100 [sender.window makeFirstResponder:sender];
104 - (void)mbtnChanged:(NSView *)sender {
105 [radioButtons selectCellAtRow:4 column:0];
106 [sender.window makeFirstResponder:sender];
110 - (void)sdirChanged:(NSView *)sender {
111 [radioButtons selectCellAtRow:5 column:0];
112 [sender.window makeFirstResponder:sender];
116 - (NJOutput *)currentOutput {
117 return mappingsController.currentMapping[inputController.selectedInput];
120 - (NJOutput *)makeOutput {
121 switch (radioButtons.selectedRow) {
125 if (keyInput.hasKeyCode) {
126 NJOutputKeyPress *k = [[NJOutputKeyPress alloc] init];
127 k.vk = keyInput.keyCode;
134 NJOutputMapping *c = [[NJOutputMapping alloc] init];
135 c.mapping = mappingsController[mappingPopup.indexOfSelectedItem];
139 NJOutputMouseMove *mm = [[NJOutputMouseMove alloc] init];
140 mm.axis = mouseDirSelect.selectedSegment;
144 NJOutputMouseButton *mb = [[NJOutputMouseButton alloc] init];
145 mb.button = mouseBtnSelect.selectedSegment == 0 ? kCGMouseButtonLeft : kCGMouseButtonRight;
149 NJOutputMouseScroll *ms = [[NJOutputMouseScroll alloc] init];
150 ms.amount = scrollDirSelect.selectedSegment ? 1 : -1;
154 NJOutputSwitchMouseMode *tms = [[NJOutputSwitchMouseMode alloc] init];
163 [self cleanUpInterface];
164 mappingsController.currentMapping[inputController.selectedInput] = [self makeOutput];
165 [mappingsController save];
169 return [radioButtons isEnabled];
172 - (void)setEnabled:(BOOL)enabled {
173 [radioButtons setEnabled:enabled];
174 [keyInput setEnabled:enabled];
175 [mappingPopup setEnabled:enabled];
176 [mouseDirSelect setEnabled:enabled];
177 [mouseBtnSelect setEnabled:enabled];
178 [scrollDirSelect setEnabled:enabled];
181 - (void)loadOutput:(NJOutput *)output forInput:(NJInput *)input {
184 title.stringValue = @"";
187 NSString *inpFullName = input.name;
188 for (id <NJInputPathElement> cur = input.base; cur; cur = cur.base) {
189 inpFullName = [[NSString alloc] initWithFormat:@"%@ > %@", cur.name, inpFullName];
191 title.stringValue = inpFullName;
194 if ([output isKindOfClass:NJOutputKeyPress.class]) {
195 [radioButtons selectCellAtRow:1 column:0];
196 keyInput.keyCode = [(NJOutputKeyPress*)output vk];
197 } else if ([output isKindOfClass:NJOutputMapping.class]) {
198 [radioButtons selectCellAtRow:2 column:0];
199 NSMenuItem *item = [mappingPopup itemWithRepresentedObject:[(NJOutputMapping *)output mapping]];
200 [mappingPopup selectItem:item];
202 [radioButtons selectCellAtRow:self.enabled ? 0 : -1 column:0];
204 else if ([output isKindOfClass:NJOutputMouseMove.class]) {
205 [radioButtons selectCellAtRow:3 column:0];
206 [mouseDirSelect setSelectedSegment:[(NJOutputMouseMove *)output axis]];
208 else if ([output isKindOfClass:NJOutputMouseButton.class]) {
209 [radioButtons selectCellAtRow:4 column:0];
210 mouseBtnSelect.selectedSegment = [(NJOutputMouseButton *)output button] == kCGMouseButtonLeft ? 0 : 1;
212 else if ([output isKindOfClass:NJOutputMouseScroll.class]) {
213 [radioButtons selectCellAtRow:5 column:0];
214 scrollDirSelect.selectedSegment = [(NJOutputMouseScroll *)output amount] > 0;
216 else if ([output isKindOfClass:NJOutputSwitchMouseMode.class]) {
217 [radioButtons selectCellAtRow:6 column:0];
219 [radioButtons selectCellAtRow:self.enabled ? 0 : -1 column:0];
221 [self cleanUpInterface];
224 - (void)loadCurrent {
225 [self loadOutput:self.currentOutput forInput:inputController.selectedInput];
229 if (radioButtons.selectedRow <= 1)
230 [keyInput.window makeFirstResponder:keyInput];
232 [keyInput resignIfFirstResponder];
235 - (void)mappingListDidChange:(NSNotification *)note {
236 NSArray *mappings = note.object;
237 NJMapping *current = mappingPopup.selectedItem.representedObject;
238 [mappingPopup.menu removeAllItems];
239 for (NJMapping *mapping in mappings) {
240 NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:mapping.name
241 action:@selector(mappingChosen:)
244 item.representedObject = mapping;
245 [mappingPopup.menu addItem:item];
247 [mappingPopup selectItemWithRepresentedObject:current];