Finished supporting mouse buttons
[enjoyable.git] / TargetController.m
1 //
2 // TargetController.m
3 // Enjoy
4 //
5 // Created by Sam McCall on 5/05/09.
6 //
7
8 @implementation TargetController
9
10 -(void) keyChanged {
11 [radioButtons setState: 1 atRow: 1 column: 0 ];
12 [self commit];
13 }
14 -(IBAction)radioChanged:(id)sender {
15 [[[NSApplication sharedApplication] mainWindow] makeFirstResponder: sender];
16 [self commit];
17 }
18 -(IBAction)mbtnChanged:(id)sender {
19 [radioButtons setState: 1 atRow: 5 column: 0];
20 [[[NSApplication sharedApplication] mainWindow] makeFirstResponder: sender];
21 [self commit];
22 }
23
24
25 -(Target*) state {
26 switch([radioButtons selectedRow]) {
27 case 0: // none
28 return NULL;
29 case 1: // key
30 if([keyInput hasKey]) {
31 TargetKeyboard* k = [[TargetKeyboard alloc] init];
32 [k setVk: [keyInput vk]];
33 [k setDescr: [keyInput descr]];
34 return k;
35 }
36 break;
37 case 2:
38 {
39 TargetConfig* c = [[TargetConfig alloc] init];
40 [c setConfig: [[configsController configs] objectAtIndex: [configPopup indexOfSelectedItem]]];
41 return c;
42 }
43 case 3: {
44 // mouse X
45 TargetMouseMove *mm = [[TargetMouseMove alloc] init];
46 [mm setDir: 0];
47 return mm;
48 }
49 case 4: {
50 // mouse Y
51 TargetMouseMove *mm = [[TargetMouseMove alloc] init];
52 [mm setDir: 1];
53 return mm;
54 }
55 case 5: {
56 // mouse button
57 TargetMouseBtn *mb = [[TargetMouseBtn alloc] init];
58 if ([mouseBtnSelect selectedSegment] == 0) {
59 [mb setWhich: kCGMouseButtonLeft];
60 }
61 else {
62 [mb setWhich: kCGMouseButtonRight];
63 }
64 return mb;
65 }
66 }
67 return NULL;
68 }
69
70 -(void)configChosen:(id)sender {
71 [radioButtons setState: 1 atRow: 2 column: 0];
72 [self commit];
73 }
74
75 -(void) commit {
76 id action = [joystickController selectedAction];
77 if(action) {
78 Target* target = [self state];
79 [[configsController currentConfig] setTarget: target forAction: action];
80 }
81 }
82
83 -(void) reset {
84 [keyInput clear];
85 [radioButtons setState: 1 atRow: 0 column: 0];
86 [mouseBtnSelect setSelectedSegment: 0];
87 [self refreshConfigsPreservingSelection: NO];
88 }
89
90 -(void) setEnabled: (BOOL) enabled {
91 [radioButtons setEnabled: enabled];
92 [keyInput setEnabled: enabled];
93 [configPopup setEnabled: enabled];
94 [mouseBtnSelect setEnabled: enabled];
95 }
96 -(BOOL) enabled {
97 return [radioButtons isEnabled];
98 }
99
100 -(void) load {
101 id jsaction = [joystickController selectedAction];
102 currentJsaction = jsaction;
103 if(!jsaction) {
104 [self setEnabled: NO];
105 [title setStringValue: @""];
106 return;
107 } else {
108 [self setEnabled: YES];
109 }
110 Target* target = [[configsController currentConfig] getTargetForAction: jsaction];
111
112 id act = jsaction;
113 NSString* actFullName = [act name];
114 while([act base]) {
115 act = [act base];
116 actFullName = [[NSString alloc] initWithFormat: @"%@ > %@", [act name], actFullName];
117 }
118 [title setStringValue: [[NSString alloc] initWithFormat: @"%@ > %@", [[configsController currentConfig] name], actFullName]];
119
120 if(!target) {
121 // already reset
122 } else if([target isKindOfClass: [TargetKeyboard class]]) {
123 [radioButtons setState:1 atRow: 1 column: 0];
124 [keyInput setVk: [(TargetKeyboard*)target vk]];
125 } else if([target isKindOfClass: [TargetConfig class]]) {
126 [radioButtons setState:1 atRow: 2 column: 0];
127 [configPopup selectItemAtIndex: [[configsController configs] indexOfObject: [(TargetConfig*)target config]]];
128 }
129 else if ([target isKindOfClass: [TargetMouseMove class]]) {
130 if ([(TargetMouseMove *)target dir] == 0)
131 [radioButtons setState:1 atRow: 3 column: 0];
132 else
133 [radioButtons setState:1 atRow: 4 column: 0];
134 }
135 else if ([target isKindOfClass: [TargetMouseBtn class]]) {
136 [radioButtons setState: 1 atRow: 5 column: 0];
137 if ([(TargetMouseBtn *)target which] == kCGMouseButtonLeft)
138 [mouseBtnSelect setSelectedSegment: 0];
139 else
140 [mouseBtnSelect setSelectedSegment: 1];
141 } else {
142 [NSException raise:@"Unknown target subclass" format:@"Unknown target subclass"];
143 }
144 }
145
146 -(void) focusKey {
147 [[[NSApplication sharedApplication] mainWindow] makeFirstResponder: keyInput];
148 }
149
150 -(void) refreshConfigsPreservingSelection: (BOOL) preserve {
151 int initialIndex = [configPopup indexOfSelectedItem];
152
153 NSArray* configs = [configsController configs];
154 [configPopup removeAllItems];
155 for(int i=0; i<[configs count]; i++) {
156 [configPopup addItemWithTitle: [[configs objectAtIndex:i]name]];
157 }
158 if(preserve)
159 [configPopup selectItemAtIndex:initialIndex];
160
161 }
162
163 @end