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 scrollSpeedSlider.floatValue = scrollSpeedSlider.minValue;
75 [scrollDirSelect resignIfFirstResponder];
77 if (scrollDirSelect.selectedSegment == -1)
78 scrollDirSelect.selectedSegment = 0;
79 if (scrollDirSelect.selectedSegment < 2
80 && !scrollSpeedSlider.floatValue)
81 scrollSpeedSlider.floatValue = 15.f;
82 else if (scrollDirSelect.selectedSegment >= 2
83 && scrollSpeedSlider.floatValue)
84 scrollSpeedSlider.floatValue = scrollSpeedSlider.minValue;
89 - (IBAction)radioChanged:(NSView *)sender {
90 [sender.window makeFirstResponder:sender];
91 if (radioButtons.selectedRow == 1)
92 [keyInput.window makeFirstResponder:keyInput];
96 - (void)keyInputField:(NJKeyInputField *)keyInput didChangeKey:(CGKeyCode)keyCode {
97 [radioButtons selectCellAtRow:1 column:0];
98 [radioButtons.window makeFirstResponder:radioButtons];
102 - (void)keyInputFieldDidClear:(NJKeyInputField *)keyInput {
103 [radioButtons selectCellAtRow:0 column:0];
107 - (void)mappingChosen:(id)sender {
108 [radioButtons selectCellAtRow:2 column:0];
109 [mappingPopup.window makeFirstResponder:mappingPopup];
113 - (void)mdirChanged:(NSView *)sender {
114 [radioButtons selectCellAtRow:3 column:0];
115 [sender.window makeFirstResponder:sender];
119 - (void)mouseSpeedChanged:(NSSlider *)sender {
120 [radioButtons selectCellAtRow:3 column:0];
121 [sender.window makeFirstResponder:sender];
125 - (void)mbtnChanged:(NSView *)sender {
126 [radioButtons selectCellAtRow:4 column:0];
127 [sender.window makeFirstResponder:sender];
131 - (void)sdirChanged:(NSView *)sender {
132 [radioButtons selectCellAtRow:5 column:0];
133 [sender.window makeFirstResponder:sender];
137 - (void)scrollSpeedChanged:(NSSlider *)sender {
138 [radioButtons selectCellAtRow:5 column:0];
139 [sender.window makeFirstResponder:sender];
140 if (!sender.floatValue && scrollDirSelect.selectedSegment < 2)
141 scrollDirSelect.selectedSegment += 2;
142 else if (sender.floatValue && scrollDirSelect.selectedSegment >= 2)
143 scrollDirSelect.selectedSegment -= 2;
147 - (NJOutput *)currentOutput {
148 return mappingsController.currentMapping[inputController.selectedInput];
151 - (NJOutput *)makeOutput {
152 switch (radioButtons.selectedRow) {
156 if (keyInput.hasKeyCode) {
157 NJOutputKeyPress *k = [[NJOutputKeyPress alloc] init];
158 k.vk = keyInput.keyCode;
165 NJOutputMapping *c = [[NJOutputMapping alloc] init];
166 c.mapping = mappingsController[mappingPopup.indexOfSelectedItem];
170 NJOutputMouseMove *mm = [[NJOutputMouseMove alloc] init];
171 mm.axis = mouseDirSelect.selectedSegment;
172 mm.speed = mouseSpeedSlider.floatValue;
176 NJOutputMouseButton *mb = [[NJOutputMouseButton alloc] init];
177 mb.button = mouseBtnSelect.selectedSegment == 0 ? kCGMouseButtonLeft : kCGMouseButtonRight;
181 NJOutputMouseScroll *ms = [[NJOutputMouseScroll alloc] init];
182 ms.direction = (scrollDirSelect.selectedSegment & 1) ? 1 : -1;
183 ms.speed = scrollDirSelect.selectedSegment < 2
184 ? scrollSpeedSlider.floatValue
189 NJOutputSwitchMouseMode *tms = [[NJOutputSwitchMouseMode alloc] init];
198 [self cleanUpInterface];
199 mappingsController.currentMapping[inputController.selectedInput] = [self makeOutput];
200 [mappingsController save];
204 return [radioButtons isEnabled];
207 - (void)setEnabled:(BOOL)enabled {
208 [radioButtons setEnabled:enabled];
209 [keyInput setEnabled:enabled];
210 [mappingPopup setEnabled:enabled];
211 [mouseDirSelect setEnabled:enabled];
212 [mouseSpeedSlider setEnabled:enabled];
213 [mouseBtnSelect setEnabled:enabled];
214 [scrollDirSelect setEnabled:enabled];
217 - (void)loadOutput:(NJOutput *)output forInput:(NJInput *)input {
220 title.stringValue = @"";
223 NSString *inpFullName = input.name;
224 for (id <NJInputPathElement> cur = input.base; cur; cur = cur.base) {
225 inpFullName = [[NSString alloc] initWithFormat:@"%@ > %@", cur.name, inpFullName];
227 title.stringValue = inpFullName;
230 if ([output isKindOfClass:NJOutputKeyPress.class]) {
231 [radioButtons selectCellAtRow:1 column:0];
232 keyInput.keyCode = [(NJOutputKeyPress*)output vk];
233 } else if ([output isKindOfClass:NJOutputMapping.class]) {
234 [radioButtons selectCellAtRow:2 column:0];
235 NSMenuItem *item = [mappingPopup itemWithRepresentedObject:[(NJOutputMapping *)output mapping]];
236 [mappingPopup selectItem:item];
238 [radioButtons selectCellAtRow:self.enabled ? 0 : -1 column:0];
240 else if ([output isKindOfClass:NJOutputMouseMove.class]) {
241 [radioButtons selectCellAtRow:3 column:0];
242 mouseDirSelect.selectedSegment = [(NJOutputMouseMove *)output axis];
243 mouseSpeedSlider.floatValue = [(NJOutputMouseMove *)output speed];
245 else if ([output isKindOfClass:NJOutputMouseButton.class]) {
246 [radioButtons selectCellAtRow:4 column:0];
247 mouseBtnSelect.selectedSegment = [(NJOutputMouseButton *)output button] == kCGMouseButtonLeft ? 0 : 1;
249 else if ([output isKindOfClass:NJOutputMouseScroll.class]) {
250 [radioButtons selectCellAtRow:5 column:0];
251 int direction = [(NJOutputMouseScroll *)output direction];
252 float speed = [(NJOutputMouseScroll *)output speed];
253 scrollDirSelect.selectedSegment = (direction > 0) + !speed * 2;
254 scrollSpeedSlider.floatValue = speed;
256 else if ([output isKindOfClass:NJOutputSwitchMouseMode.class]) {
257 [radioButtons selectCellAtRow:6 column:0];
259 [radioButtons selectCellAtRow:self.enabled ? 0 : -1 column:0];
261 [self cleanUpInterface];
264 - (void)loadCurrent {
265 [self loadOutput:self.currentOutput forInput:inputController.selectedInput];
269 if (radioButtons.selectedRow <= 1)
270 [keyInput.window makeFirstResponder:keyInput];
272 [keyInput resignIfFirstResponder];
275 - (void)mappingListDidChange:(NSNotification *)note {
276 NSArray *mappings = note.object;
277 NJMapping *current = mappingPopup.selectedItem.representedObject;
278 [mappingPopup.menu removeAllItems];
279 for (NJMapping *mapping in mappings) {
280 NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:mapping.name
281 action:@selector(mappingChosen:)
284 item.representedObject = mapping;
285 [mappingPopup.menu addItem:item];
287 [mappingPopup selectItemWithRepresentedObject:current];