5 // Created by Sam McCall on 5/05/09.
8 #import "TargetController.h"
10 #import "NJMappingsController.h"
13 #import "NJInputController.h"
14 #import "NJKeyInputField.h"
15 #import "TargetConfig.h"
16 #import "TargetController.h"
17 #import "TargetKeyboard.h"
18 #import "TargetMouseBtn.h"
19 #import "TargetMouseMove.h"
20 #import "TargetMouseScroll.h"
21 #import "TargetToggleMouseScope.h"
23 @implementation TargetController
25 - (void)cleanUpInterface {
26 NSInteger row = radioButtons.selectedRow;
29 keyInput.keyCode = -1;
30 [keyInput resignIfFirstResponder];
34 [mappingPopup selectItemAtIndex:-1];
35 [mappingPopup resignIfFirstResponder];
36 } else if (!mappingPopup.selectedItem)
37 [mappingPopup selectItemAtIndex:0];
40 mouseDirSelect.selectedSegment = -1;
41 [mouseDirSelect resignIfFirstResponder];
42 } else if (mouseDirSelect.selectedSegment == -1)
43 mouseDirSelect.selectedSegment = 0;
46 mouseBtnSelect.selectedSegment = -1;
47 [mouseBtnSelect resignIfFirstResponder];
48 } else if (mouseBtnSelect.selectedSegment == -1)
49 mouseBtnSelect.selectedSegment = 0;
52 scrollDirSelect.selectedSegment = -1;
53 [scrollDirSelect resignIfFirstResponder];
54 } else if (scrollDirSelect.selectedSegment == -1)
55 scrollDirSelect.selectedSegment = 0;
58 - (IBAction)radioChanged:(NSView *)sender {
59 [sender.window makeFirstResponder:sender];
60 if (radioButtons.selectedRow == 1)
61 [keyInput.window makeFirstResponder:keyInput];
65 - (void)keyInputField:(NJKeyInputField *)keyInput didChangeKey:(CGKeyCode)keyCode {
66 [radioButtons selectCellAtRow:1 column:0];
67 [radioButtons.window makeFirstResponder:radioButtons];
71 - (void)keyInputFieldDidClear:(NJKeyInputField *)keyInput {
72 [radioButtons selectCellAtRow:0 column:0];
76 - (void)mappingChosen:(id)sender {
77 [radioButtons selectCellAtRow:2 column:0];
78 [mappingPopup.window makeFirstResponder:mappingPopup];
82 - (void)mdirChanged:(NSView *)sender {
83 [radioButtons selectCellAtRow:3 column:0];
84 [sender.window makeFirstResponder:sender];
88 - (void)mbtnChanged:(NSView *)sender {
89 [radioButtons selectCellAtRow:4 column:0];
90 [sender.window makeFirstResponder:sender];
94 - (void)sdirChanged:(NSView *)sender {
95 [radioButtons selectCellAtRow:5 column:0];
96 [sender.window makeFirstResponder:sender];
100 - (Target *)currentTarget {
101 return mappingsController.currentMapping[joystickController.selectedInput];
104 - (Target *)makeTarget {
105 switch (radioButtons.selectedRow) {
109 if (keyInput.hasKeyCode) {
110 TargetKeyboard *k = [[TargetKeyboard alloc] init];
111 k.vk = keyInput.keyCode;
118 TargetConfig *c = [[TargetConfig alloc] init];
119 c.mapping = mappingsController.mappings[mappingPopup.indexOfSelectedItem];
123 TargetMouseMove *mm = [[TargetMouseMove alloc] init];
124 mm.axis = mouseDirSelect.selectedSegment;
128 TargetMouseBtn *mb = [[TargetMouseBtn alloc] init];
129 mb.button = mouseBtnSelect.selectedSegment == 0 ? kCGMouseButtonLeft : kCGMouseButtonRight;
133 TargetMouseScroll *ms = [[TargetMouseScroll alloc] init];
134 ms.amount = scrollDirSelect.selectedSegment ? 1 : -1;
138 TargetToggleMouseScope *tms = [[TargetToggleMouseScope alloc] init];
147 [self cleanUpInterface];
148 mappingsController.currentMapping[joystickController.selectedInput] = [self makeTarget];
149 [mappingsController save];
153 return [radioButtons isEnabled];
156 - (void)setEnabled:(BOOL)enabled {
157 [radioButtons setEnabled:enabled];
158 [keyInput setEnabled:enabled];
159 [mappingPopup setEnabled:enabled];
160 [mouseDirSelect setEnabled:enabled];
161 [mouseBtnSelect setEnabled:enabled];
162 [scrollDirSelect setEnabled:enabled];
165 - (void)loadTarget:(Target *)target forInput:(NJInput *)input {
168 title.stringValue = @"";
171 NSString *inpFullName = input.name;
172 for (id <NJInputPathElement> cur = input.base; cur; cur = cur.base) {
173 inpFullName = [[NSString alloc] initWithFormat:@"%@ > %@", cur.name, inpFullName];
175 title.stringValue = [[NSString alloc] initWithFormat:@"%@ > %@", mappingsController.currentMapping.name, inpFullName];
178 if ([target isKindOfClass:TargetKeyboard.class]) {
179 [radioButtons selectCellAtRow:1 column:0];
180 keyInput.keyCode = [(TargetKeyboard*)target vk];
181 } else if ([target isKindOfClass:TargetConfig.class]) {
182 [radioButtons selectCellAtRow:2 column:0];
183 NSUInteger idx = [mappingsController.mappings
184 indexOfObject:[(TargetConfig *)target mapping]];
185 if (idx == NSNotFound) {
186 [radioButtons selectCellAtRow:self.enabled ? 0 : -1 column:0];
187 [mappingPopup selectItemAtIndex:-1];
189 [mappingPopup selectItemAtIndex:idx];
191 else if ([target isKindOfClass:TargetMouseMove.class]) {
192 [radioButtons selectCellAtRow:3 column:0];
193 [mouseDirSelect setSelectedSegment:[(TargetMouseMove *)target axis]];
195 else if ([target isKindOfClass:TargetMouseBtn.class]) {
196 [radioButtons selectCellAtRow:4 column:0];
197 mouseBtnSelect.selectedSegment = [(TargetMouseBtn *)target button] == kCGMouseButtonLeft ? 0 : 1;
199 else if ([target isKindOfClass:TargetMouseScroll.class]) {
200 [radioButtons selectCellAtRow:5 column:0];
201 scrollDirSelect.selectedSegment = [(TargetMouseScroll *)target amount] > 0;
203 else if ([target isKindOfClass:TargetToggleMouseScope.class]) {
204 [radioButtons selectCellAtRow:6 column:0];
206 [radioButtons selectCellAtRow:self.enabled ? 0 : -1 column:0];
208 [self cleanUpInterface];
211 - (void)loadCurrent {
212 [self loadTarget:self.currentTarget forInput:joystickController.selectedInput];
216 if (radioButtons.selectedRow <= 1)
217 [keyInput.window makeFirstResponder:keyInput];
219 [keyInput resignIfFirstResponder];
222 - (void)refreshMappings {
223 NSInteger initialIndex = mappingPopup.indexOfSelectedItem;
224 [mappingPopup.menu removeAllItems];
225 for (NJMapping *mapping in mappingsController.mappings) {
226 NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:mapping.name
227 action:@selector(mappingChosen:)
230 [mappingPopup.menu addItem:item];
232 [mappingPopup selectItemAtIndex:initialIndex];