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 mouseSpeedSlider.floatValue = mouseSpeedSlider.minValue;
58 [mouseDirSelect resignIfFirstResponder];
60 if (mouseDirSelect.selectedSegment == -1)
61 mouseDirSelect.selectedSegment = 0;
62 if (!mouseSpeedSlider.floatValue)
63 mouseSpeedSlider.floatValue = 4;
67 mouseBtnSelect.selectedSegment = -1;
68 [mouseBtnSelect resignIfFirstResponder];
69 } else if (mouseBtnSelect.selectedSegment == -1)
70 mouseBtnSelect.selectedSegment = 0;
73 scrollDirSelect.selectedSegment = -1;
74 [scrollDirSelect resignIfFirstResponder];
75 } else if (scrollDirSelect.selectedSegment == -1)
76 scrollDirSelect.selectedSegment = 0;
79 - (IBAction)radioChanged:(NSView *)sender {
80 [sender.window makeFirstResponder:sender];
81 if (radioButtons.selectedRow == 1)
82 [keyInput.window makeFirstResponder:keyInput];
86 - (void)keyInputField:(NJKeyInputField *)keyInput didChangeKey:(CGKeyCode)keyCode {
87 [radioButtons selectCellAtRow:1 column:0];
88 [radioButtons.window makeFirstResponder:radioButtons];
92 - (void)keyInputFieldDidClear:(NJKeyInputField *)keyInput {
93 [radioButtons selectCellAtRow:0 column:0];
97 - (void)mappingChosen:(id)sender {
98 [radioButtons selectCellAtRow:2 column:0];
99 [mappingPopup.window makeFirstResponder:mappingPopup];
103 - (void)mdirChanged:(NSView *)sender {
104 [radioButtons selectCellAtRow:3 column:0];
105 [sender.window makeFirstResponder:sender];
109 - (void)mouseSpeedChanged:(NSSlider *)sender {
110 [radioButtons selectCellAtRow:3 column:0];
111 [sender.window makeFirstResponder:sender];
115 - (void)mbtnChanged:(NSView *)sender {
116 [radioButtons selectCellAtRow:4 column:0];
117 [sender.window makeFirstResponder:sender];
121 - (void)sdirChanged:(NSView *)sender {
122 [radioButtons selectCellAtRow:5 column:0];
123 [sender.window makeFirstResponder:sender];
127 - (NJOutput *)currentOutput {
128 return mappingsController.currentMapping[inputController.selectedInput];
131 - (NJOutput *)makeOutput {
132 switch (radioButtons.selectedRow) {
136 if (keyInput.hasKeyCode) {
137 NJOutputKeyPress *k = [[NJOutputKeyPress alloc] init];
138 k.vk = keyInput.keyCode;
145 NJOutputMapping *c = [[NJOutputMapping alloc] init];
146 c.mapping = mappingsController[mappingPopup.indexOfSelectedItem];
150 NJOutputMouseMove *mm = [[NJOutputMouseMove alloc] init];
151 mm.axis = mouseDirSelect.selectedSegment;
152 mm.speed = mouseSpeedSlider.floatValue;
156 NJOutputMouseButton *mb = [[NJOutputMouseButton alloc] init];
157 mb.button = mouseBtnSelect.selectedSegment == 0 ? kCGMouseButtonLeft : kCGMouseButtonRight;
161 NJOutputMouseScroll *ms = [[NJOutputMouseScroll alloc] init];
162 ms.amount = scrollDirSelect.selectedSegment ? 1 : -1;
166 NJOutputSwitchMouseMode *tms = [[NJOutputSwitchMouseMode alloc] init];
175 [self cleanUpInterface];
176 mappingsController.currentMapping[inputController.selectedInput] = [self makeOutput];
177 [mappingsController save];
181 return [radioButtons isEnabled];
184 - (void)setEnabled:(BOOL)enabled {
185 [radioButtons setEnabled:enabled];
186 [keyInput setEnabled:enabled];
187 [mappingPopup setEnabled:enabled];
188 [mouseDirSelect setEnabled:enabled];
189 [mouseSpeedSlider setEnabled:enabled];
190 [mouseBtnSelect setEnabled:enabled];
191 [scrollDirSelect setEnabled:enabled];
194 - (void)loadOutput:(NJOutput *)output forInput:(NJInput *)input {
197 title.stringValue = @"";
200 NSString *inpFullName = input.name;
201 for (id <NJInputPathElement> cur = input.base; cur; cur = cur.base) {
202 inpFullName = [[NSString alloc] initWithFormat:@"%@ > %@", cur.name, inpFullName];
204 title.stringValue = inpFullName;
207 if ([output isKindOfClass:NJOutputKeyPress.class]) {
208 [radioButtons selectCellAtRow:1 column:0];
209 keyInput.keyCode = [(NJOutputKeyPress*)output vk];
210 } else if ([output isKindOfClass:NJOutputMapping.class]) {
211 [radioButtons selectCellAtRow:2 column:0];
212 NSMenuItem *item = [mappingPopup itemWithRepresentedObject:[(NJOutputMapping *)output mapping]];
213 [mappingPopup selectItem:item];
215 [radioButtons selectCellAtRow:self.enabled ? 0 : -1 column:0];
217 else if ([output isKindOfClass:NJOutputMouseMove.class]) {
218 [radioButtons selectCellAtRow:3 column:0];
219 mouseDirSelect.selectedSegment = [(NJOutputMouseMove *)output axis];
220 mouseSpeedSlider.floatValue = [(NJOutputMouseMove *)output speed];
222 else if ([output isKindOfClass:NJOutputMouseButton.class]) {
223 [radioButtons selectCellAtRow:4 column:0];
224 mouseBtnSelect.selectedSegment = [(NJOutputMouseButton *)output button] == kCGMouseButtonLeft ? 0 : 1;
226 else if ([output isKindOfClass:NJOutputMouseScroll.class]) {
227 [radioButtons selectCellAtRow:5 column:0];
228 scrollDirSelect.selectedSegment = [(NJOutputMouseScroll *)output amount] > 0;
230 else if ([output isKindOfClass:NJOutputSwitchMouseMode.class]) {
231 [radioButtons selectCellAtRow:6 column:0];
233 [radioButtons selectCellAtRow:self.enabled ? 0 : -1 column:0];
235 [self cleanUpInterface];
238 - (void)loadCurrent {
239 [self loadOutput:self.currentOutput forInput:inputController.selectedInput];
243 if (radioButtons.selectedRow <= 1)
244 [keyInput.window makeFirstResponder:keyInput];
246 [keyInput resignIfFirstResponder];
249 - (void)mappingListDidChange:(NSNotification *)note {
250 NSArray *mappings = note.object;
251 NJMapping *current = mappingPopup.selectedItem.representedObject;
252 [mappingPopup.menu removeAllItems];
253 for (NJMapping *mapping in mappings) {
254 NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:mapping.name
255 action:@selector(mappingChosen:)
258 item.representedObject = mapping;
259 [mappingPopup.menu addItem:item];
261 [mappingPopup selectItemWithRepresentedObject:current];