Mouse improvements. Segment and snap the mouse move and scroll speed for easier match...
[enjoyable.git] / Classes / NJOutputController.m
1 //
2 // NJOutputController.m
3 // Enjoy
4 //
5 // Created by Sam McCall on 5/05/09.
6 //
7
8 #import "NJOutputController.h"
9
10 #import "NJMappingsController.h"
11 #import "NJMapping.h"
12 #import "NJInput.h"
13 #import "NJEvents.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
23 @implementation NJOutputController
24
25 - (id)init {
26 if ((self = [super init])) {
27 [NSNotificationCenter.defaultCenter
28 addObserver:self
29 selector:@selector(mappingListDidChange:)
30 name:NJEventMappingListChanged
31 object:nil];
32 }
33 return self;
34 }
35
36 - (void)dealloc {
37 [NSNotificationCenter.defaultCenter removeObserver:self];
38 }
39
40 - (void)cleanUpInterface {
41 NSInteger row = radioButtons.selectedRow;
42
43 if (row != 1) {
44 keyInput.keyCode = NJKeyInputFieldEmpty;
45 [keyInput resignIfFirstResponder];
46 }
47
48 if (row != 2) {
49 [mappingPopup selectItemAtIndex:-1];
50 [mappingPopup resignIfFirstResponder];
51 } else if (!mappingPopup.selectedItem)
52 [mappingPopup selectItemAtIndex:0];
53
54 if (row != 3) {
55 mouseDirSelect.selectedSegment = -1;
56 mouseSpeedSlider.floatValue = mouseSpeedSlider.minValue;
57 [mouseDirSelect resignIfFirstResponder];
58 } else {
59 if (mouseDirSelect.selectedSegment == -1)
60 mouseDirSelect.selectedSegment = 0;
61 if (!mouseSpeedSlider.floatValue)
62 mouseSpeedSlider.floatValue = 10;
63 }
64
65 if (row != 4) {
66 mouseBtnSelect.selectedSegment = -1;
67 [mouseBtnSelect resignIfFirstResponder];
68 } else if (mouseBtnSelect.selectedSegment == -1)
69 mouseBtnSelect.selectedSegment = 0;
70
71 if (row != 5) {
72 scrollDirSelect.selectedSegment = -1;
73 scrollSpeedSlider.floatValue = scrollSpeedSlider.minValue;
74 [scrollDirSelect resignIfFirstResponder];
75 } else {
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;
84 }
85
86 }
87
88 - (IBAction)radioChanged:(NSView *)sender {
89 [sender.window makeFirstResponder:sender];
90 if (radioButtons.selectedRow == 1)
91 [keyInput.window makeFirstResponder:keyInput];
92 [self commit];
93 }
94
95 - (void)keyInputField:(NJKeyInputField *)keyInput didChangeKey:(CGKeyCode)keyCode {
96 [radioButtons selectCellAtRow:1 column:0];
97 [radioButtons.window makeFirstResponder:radioButtons];
98 [self commit];
99 }
100
101 - (void)keyInputFieldDidClear:(NJKeyInputField *)keyInput {
102 [radioButtons selectCellAtRow:0 column:0];
103 [self commit];
104 }
105
106 - (void)mappingChosen:(id)sender {
107 [radioButtons selectCellAtRow:2 column:0];
108 [mappingPopup.window makeFirstResponder:mappingPopup];
109 [self commit];
110 }
111
112 - (void)mdirChanged:(NSView *)sender {
113 [radioButtons selectCellAtRow:3 column:0];
114 [sender.window makeFirstResponder:sender];
115 [self commit];
116 }
117
118 - (void)mouseSpeedChanged:(NSSlider *)sender {
119 [radioButtons selectCellAtRow:3 column:0];
120 [sender.window makeFirstResponder:sender];
121 [self commit];
122 }
123
124 - (void)mbtnChanged:(NSView *)sender {
125 [radioButtons selectCellAtRow:4 column:0];
126 [sender.window makeFirstResponder:sender];
127 [self commit];
128 }
129
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];
135 [self commit];
136 }
137
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;
145 [self commit];
146 }
147
148 - (NJOutput *)currentOutput {
149 return mappingsController.currentMapping[inputController.selectedInput];
150 }
151
152 - (NJOutput *)makeOutput {
153 switch (radioButtons.selectedRow) {
154 case 0:
155 return nil;
156 case 1:
157 if (keyInput.hasKeyCode) {
158 NJOutputKeyPress *k = [[NJOutputKeyPress alloc] init];
159 k.vk = keyInput.keyCode;
160 return k;
161 } else {
162 return nil;
163 }
164 break;
165 case 2: {
166 NJOutputMapping *c = [[NJOutputMapping alloc] init];
167 c.mapping = mappingsController[mappingPopup.indexOfSelectedItem];
168 return c;
169 }
170 case 3: {
171 NJOutputMouseMove *mm = [[NJOutputMouseMove alloc] init];
172 mm.axis = mouseDirSelect.selectedSegment;
173 mm.speed = mouseSpeedSlider.floatValue;
174 return mm;
175 }
176 case 4: {
177 NJOutputMouseButton *mb = [[NJOutputMouseButton alloc] init];
178 mb.humanIndexedButton = mouseBtnSelect.selectedSegment;
179 return mb;
180 }
181 case 5: {
182 NJOutputMouseScroll *ms = [[NJOutputMouseScroll alloc] init];
183 ms.direction = (scrollDirSelect.selectedSegment & 1) ? 1 : -1;
184 ms.speed = scrollDirSelect.selectedSegment < 2
185 ? scrollSpeedSlider.floatValue
186 : 0.f;
187 return ms;
188 }
189 default:
190 return nil;
191 }
192 }
193
194 - (void)commit {
195 [self cleanUpInterface];
196 mappingsController.currentMapping[inputController.selectedInput] = [self makeOutput];
197 [mappingsController save];
198 }
199
200 - (BOOL)enabled {
201 return [radioButtons isEnabled];
202 }
203
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];
213 }
214
215 - (void)loadOutput:(NJOutput *)output forInput:(NJInput *)input {
216 if (!input) {
217 self.enabled = NO;
218 title.stringValue = @"";
219 } else {
220 self.enabled = YES;
221 NSString *inpFullName = input.name;
222 for (id <NJInputPathElement> cur = input.base; cur; cur = cur.base) {
223 inpFullName = [[NSString alloc] initWithFormat:@"%@ > %@", cur.name, inpFullName];
224 }
225 title.stringValue = inpFullName;
226 }
227
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];
235 if (!item)
236 [radioButtons selectCellAtRow:self.enabled ? 0 : -1 column:0];
237 }
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];
242 }
243 else if ([output isKindOfClass:NJOutputMouseButton.class]) {
244 [radioButtons selectCellAtRow:4 column:0];
245 mouseBtnSelect.selectedSegment = [(NJOutputMouseButton *)output humanIndexedButton];
246 }
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;
253 } else {
254 [radioButtons selectCellAtRow:self.enabled ? 0 : -1 column:0];
255 }
256 [self cleanUpInterface];
257 }
258
259 - (void)loadCurrent {
260 [self loadOutput:self.currentOutput forInput:inputController.selectedInput];
261 }
262
263 - (void)focusKey {
264 if (radioButtons.selectedRow <= 1)
265 [keyInput.window makeFirstResponder:keyInput];
266 else
267 [keyInput resignIfFirstResponder];
268 }
269
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:)
277 keyEquivalent:@""];
278 item.target = self;
279 item.representedObject = mapping;
280 [mappingPopup.menu addItem:item];
281 }
282 [mappingPopup selectItemWithRepresentedObject:current];
283 }
284
285 @end