Debian Codegraph @

Nasal/joystick.nas

raw
1 # Joystick configuration library.
2 var DIALOGROOT = "/sim/gui/dialogs/joystick-config";
3 var MAX_AXES = 8;
4 var MAX_NASALS = 8;
5 # Buttons 0..31 seem to work, higher numbers wrap around, i.e. 32 -> 0, 33 -> 1
6 var MAX_BUTTONS = 32;
7
8 # Hash of the custom axis/buttons
9 var custom_bindings = {};
10
11 # Class for an individual joystick axis binding
12 var Axis = {
13 new: func(name, prop, invertable) {
14 var m = { parents: [Axis] };
15 m.name = name;
16 m.prop = prop;
17 m.invertable = invertable;
18 m.inverted = 0;
19 m.power = 1.0;
20 return m;
21 },
22
23 clone: func() {
24 var m = { parents: [Axis] };
25 m.name = me.name;
26 m.prop = me.prop;
27 m.invertable = me.invertable;
28 m.inverted = me.inverted;
29 m.power = me.power;
30 return m;
31 },
32
33 match: func(prop) {
34 return 0;
35 },
36
37 parse: func(prop) { },
38
39 readProps: func(props) {},
40
41 getName: func() { return me.name; },
42 getBinding: func(axis) { return props.Node.new(); },
43
44 isInvertable: func() { return me.invertable; },
45 isInverted: func() { return me.inverted; },
46 getPower: func() { return me.power; },
47
48 setPower: func(v) { me.power = v; },
49 setInverted: func(b) { if (me.invertable) me.inverted = b; },
50 };
51
52 var CustomAxis = {
53 new: func() {
54 var m = { parents: [CustomAxis, Axis.new("Custom", "", 0) ] };
55 me.custom_binding = nil;
56 return m;
57 },
58
59 clone: func() {
60 var m = { parents: [CustomAxis, Axis.new("Custom", "", 0) ] };
61 m.custom_binding = me.custom_binding;
62 return m;
63 },
64
65 match: func(prop) {
66 var p = props.Node.new();
67
68 if (prop.getNode("binding") != nil) {
69 props.copy(prop.getNode("binding"), p);
70 me.custom_binding = p;
71 return 1;
72 } else {
73 return 0;
74 }
75 },
76
77 getBinding: func(axis) {
78 var p = props.Node.new().getNode("axis[" ~ axis ~ "]", 1);
79 p.getNode("desc", 1).setValue(me.name);
80 props.copy(me.custom_binding, p.getNode("binding", 1));
81 return p;
82 },
83 };
84
85 var UnboundAxis = {
86 new: func() {
87 var m = { parents: [UnboundAxis, Axis.new("None", "", 0) ] };
88 return m;
89 },
90
91 clone: func() {
92 var m = { parents: [UnboundAxis, Axis.new("None", "", 0) ] };
93 return m;
94 },
95
96 match: func(prop) {
97 return 1;
98 },
99
100 getBinding: func(axis) {
101 return nil;
102 },
103 };
104
105
106 var PropertyScaleAxis = {
107 new: func(name, prop, factor=1, offset=0, power=1) {
108 var m = { parents: [PropertyScaleAxis, Axis.new(name, prop, 1) ] };
109 m.prop=prop;
110 m.factor = factor;
111 m.offset = offset;
112 m.power = power;
113
114 return m;
115 },
116
117 clone: func() {
118 var m = { parents: [PropertyScaleAxis, Axis.new(me.name, me.prop, 1) ] };
119
120 m.inverted = me.inverted;
121
122 m.prop= me.prop;
123 m.factor = me.factor;
124 m.offset = me.offset;
125 m.power = me.power;
126 return m;
127 },
128
129
130 match: func(prop) {
131 var cmd = prop.getNode("binding", 1).getNode("command", 1).getValue();
132 var p = prop.getNode("binding", 1).getNode("property", 1).getValue();
133 return ((cmd == "property-scale") and (p == me.prop));
134 },
135
136 parse: func(p) {
137 bindingNode = p.getNode("binding", 1);
138 me.prop = bindingNode.getNode("property", 1).getValue();
139
140 # Don't create an empty 'factor' node if it doesn't exist!
141 # (value 0 wouldn't be appropriate for a default factor)
142 factorNode = bindingNode.getNode("factor", 0);
143 me.factor = (factorNode != nil) ? factorNode.getValue() : 1.0;
144 me.inverted = (me.factor < 0);
145 me.offset = bindingNode.getNode("offset", 1).getValue();
146
147 powerNode = bindingNode.getNode("power", 0);
148 me.power = (powerNode != nil) ? powerNode.getValue() : 1.0;
149 },
150
151 getBinding: func(axis) {
152 var p = props.Node.new();
153 p = p.getNode("axis[" ~ axis ~ "]", 1);
154 p.getNode("desc", 1).setValue(me.name);
155 p.getNode("binding", 1).getNode("command", 1).setValue("property-scale");
156 p.getNode("binding", 1).getNode("property", 1).setValue(me.prop);
157 if (me.inverted) {
158 p.getNode("binding", 1).getNode("factor", 1).setValue(0 - me.factor);
159 } else {
160 p.getNode("binding", 1).getNode("factor", 1).setValue(me.factor);
161 }
162 p.getNode("binding", 1).getNode("offset", 1).setValue(me.offset);
163 p.getNode("binding", 1).getNode("power",1 ).setValue(me.power);
164 return p;
165 },
166
167 };
168
169 var NasalScaleAxis = {
170 new: func(name, script, prop) {
171 var m = { parents: [NasalScaleAxis, Axis.new(name, prop, 0) ] };
172 m.script = script;
173 m.prop = prop;
174 return m;
175 },
176
177 clone: func() {
178 var m = { parents: [NasalScaleAxis, Axis.new(me.name, me.prop, 0) ] };
179
180 m.script = me.script;
181 m.prop = me.prop;
182 return m;
183 },
184
185 match: func(prop) {
186 var cmd = prop.getNode("binding", 1).getNode("command", 1).getValue();
187 var p = prop.getNode("binding", 1).getNode("script", 1).getValue();
188 if ((p != nil) and (cmd == "nasal")) {
189 p = string.trim(p);
190 p = string.replace(p, ";", "");
191 p = p ~ ";";
192 return (p == me.script);
193 } else {
194 return 0;
195 }
196 },
197
198 getBinding: func(axis) {
199 var p = props.Node.new().getNode("axis[" ~ axis ~ "]", 1);
200 p.getNode("desc", 1).setValue(me.name);
201 p.getNode("binding", 1).getNode("command", 1).setValue("nasal");
202 p.getNode("binding", 1).getNode("script", 1).setValue(me.script);
203 return p;
204 },
205 };
206
207 var NasalLowHighAxis = {
208 new: func(name, lowscript, highscript, prop, repeatable) {
209 var m = { parents: [NasalLowHighAxis, Axis.new(name, prop, 1) ] };
210 m.lowscript = lowscript;
211 m.highscript = highscript;
212 m.repeatable = repeatable;
213 return m;
214 },
215
216 clone: func() {
217 var m = { parents: [NasalLowHighAxis, Axis.new(me.name, me.prop, 1) ] };
218
219 m.inverted = me.inverted;
220
221 m.lowscript = me.lowscript;
222 m.highscript = me.highscript;
223 m.repeatable = me.repeatable;
224 return m;
225 },
226
227 match: func(prop) {
228 var cmd = prop.getNode("low", 1).getNode("binding", 1).getNode("command", 1).getValue();
229 var p = prop.getNode("low", 1).getNode("binding", 1).getNode("script", 1).getValue();
230
231 if ((p == nil) or (cmd != "nasal")) return 0;
232 p = string.trim(p);
233 p = string.replace(p, ";", "");
234 p = p ~ ";";
235
236 if (p == me.lowscript) {
237 return 1;
238 }
239
240 if (p == me.highscript) {
241 me.inverted = 1;
242 return 1;
243 }
244
245 return 0;
246 },
247
248 getBinding: func(axis) {
249 var p = props.Node.new().getNode("axis[" ~ axis ~ "]", 1);
250 p.getNode("desc", 1).setValue(me.name);
251
252 p.getNode("low", 1).getNode("binding", 1).getNode("command", 1).setValue("nasal");
253 p.getNode("low", 1).getNode("repeatable", 1).setBoolValue(me.repeatable);
254 p.getNode("high", 1).getNode("binding", 1).getNode("command", 1).setValue("nasal");
255 p.getNode("high", 1).getNode("repeatable", 1).setBoolValue(me.repeatable);
256
257 if (me.inverted) {
258 p.getNode("low", 1).getNode("binding", 1).getNode("script", 1).setValue(me.highscript);
259 p.getNode("high", 1).getNode("binding", 1).getNode("script", 1).setValue(me.lowscript);
260 } else {
261 p.getNode("low", 1).getNode("binding", 1).getNode("script", 1).setValue(me.lowscript);
262 p.getNode("high", 1).getNode("binding", 1).getNode("script", 1).setValue(me.highscript);
263 }
264
265 return p;
266 },
267 };
268
269
270 var axisBindings = [
271 PropertyScaleAxis.new("Aileron", "/controls/flight/aileron"),
272 PropertyScaleAxis.new("Elevator", "/controls/flight/elevator"),
273 PropertyScaleAxis.new("Rudder", "/controls/flight/rudder"),
274 NasalScaleAxis.new("Throttle (all)", "controls.throttleAxis();", "/controls/engines/engine[0]/throttle") ,
275 NasalScaleAxis.new("Mixture (all)", "controls.mixtureAxis();", "/controls/engines/engine[0]/mixture") ,
276 NasalScaleAxis.new("Propeller (all)", "controls.propellerAxis();", "/controls/engines/engine[0]/propeller-pitch") ,
277
278 # Controls of individual throttles, mixtures, propellers
279 NasalScaleAxis.new("Throttle 1", "controls.perEngineSelectedAxisHandler(0)([0]);", "/controls/engines/engine[0]/throttle") ,
280 NasalScaleAxis.new("Mixture 1", "controls.perEngineSelectedAxisHandler(1)([0]);", "/controls/engines/engine[0]/mixture") ,
281 NasalScaleAxis.new("Propeller 1", "controls.perEngineSelectedAxisHandler(2)([0]);", "/controls/engines/engine[0]/propeller-pitch") ,
282 NasalScaleAxis.new("Throttle 2", "controls.perEngineSelectedAxisHandler(0)([1]);", "/controls/engines/engine[1]/throttle") ,
283 NasalScaleAxis.new("Mixture 2", "controls.perEngineSelectedAxisHandler(1)([1]);", "/controls/engines/engine[1]/mixture") ,
284 NasalScaleAxis.new("Propeller 2", "controls.perEngineSelectedAxisHandler(2)([1]);", "/controls/engines/engine[1]/propeller-pitch") ,
285
286 # 2018.2 (RJH); change engine bindings to use the scaled axis configuration; the -all property
287 # that is used has a listener in controls.nas that will propogate the value to all configured engines.
288 # - mainly to allow the use of the "invert" option.
289 PropertyScaleAxis.new("Throttle All Engines", "/controls/engines/throttle-all") ,
290 PropertyScaleAxis.new("Mixture All Engines", "/controls/engines/mixture-all") ,
291 PropertyScaleAxis.new("Propeller All Engines", "/controls/engines/propeller-pitch-all") ,
292 PropertyScaleAxis.new("Throttle Engine 0", "/controls/engines/engine[0]/throttle") ,
293 PropertyScaleAxis.new("Mixture Engine 0", "/controls/engines/engine[0]/mixture") ,
294 PropertyScaleAxis.new("Propeller Pitch Engine 0", "/controls/engines/engine[0]/propeller-pitch") ,
295 PropertyScaleAxis.new("Throttle Engine 1", "/controls/engines/engine[1]/throttle") ,
296 PropertyScaleAxis.new("Mixture Engine 1", "/controls/engines/engine[1]/mixture") ,
297 PropertyScaleAxis.new("Propeller Pitch Engine 1", "/controls/engines/engine[1]/propeller-pitch"),
298 PropertyScaleAxis.new("Reverser All Engines", "/controls/engines/reverser-all") ,
299
300 NasalLowHighAxis.new("View (horizontal)",
301 "setprop(\"/sim/current-view/goal-heading-offset-deg\", getprop(\"/sim/current-view/goal-heading-offset-deg\") + 2);",
302 "setprop(\"/sim/current-view/goal-heading-offset-deg\", getprop(\"/sim/current-view/goal-heading-offset-deg\") - 2);",
303 "/sim/current-view/goal-heading-offset-deg",1),
304 NasalLowHighAxis.new("View (vertical)",
305 "setprop(\"/sim/current-view/goal-pitch-offset-deg\", getprop(\"/sim/current-view/goal-pitch-offset-deg\") - 1);",
306 "setprop(\"/sim/current-view/goal-pitch-offset-deg\", getprop(\"/sim/current-view/goal-pitch-offset-deg\") + 1);",
307 "/sim/current-view/goal-heading-offset-deg",1),
308 PropertyScaleAxis.new("Aileron Trim", "/controls/flight/aileron-trim"),
309 PropertyScaleAxis.new("Elevator Trim", "/controls/flight/elevator-trim"),
310 PropertyScaleAxis.new("Rudder Trim", "/controls/flight/rudder-trim"),
311 PropertyScaleAxis.new("Brake Left", "/controls/gear/brake-left", 0.5, 1.0),
312 PropertyScaleAxis.new("Brake Right", "/controls/gear/brake-right", 0.5, 1.0),
313 PropertyScaleAxis.new("Flaps", "/controls/flight/flaps", 0.5, 1.0),
314 PropertyScaleAxis.new("Wings", "/controls/flight/wings", 0.5, 1.0),
315 NasalLowHighAxis.new("Aileron Trim inc.", "controls.aileronTrim(-1);", "controls.aileronTrim(1);", "/controls/flight/aileron-trim-delta", 1),
316 NasalLowHighAxis.new("Elevator Trim inc.", "controls.elevatorTrim(-1);", "controls.elevatorTrim(1);", "/controls/flight/elevator-trim-delta", 1),
317 NasalLowHighAxis.new("Rudder Trim inc.", "controls.rudderTrim(-1);", "controls.rudderTrim(1);", "/controls/flight/rudder-trim-delta", 1),
318
319 PropertyScaleAxis.new("View Horizontal Axis", "/sim/current-view/goal-heading-offset-deg", 180, 0),
320 PropertyScaleAxis.new("View Vertical Axis", "/sim/current-view/goal-pitch-offset-deg", 180, 0),
321 CustomAxis.new(),
322 UnboundAxis.new(),
323 ];
324
325 # Button bindings
326 var ButtonBinding = {
327 new: func(name, binding, repeatable) {
328 var m = { parents: [ButtonBinding] };
329 m.name = name;
330 m.binding = binding;
331 m.repeatable = repeatable;
332 return m;
333 },
334
335 clone: func() {
336 var m = { parents: [ButtonBinding] };
337 m.name = me.name;
338 m.binding= me.binding;
339 m.repeatable = me.repeatable;
340 return m;
341 },
342
343 match: func(prop) {
344 return 0;
345 },
346
347 getName: func() { return me.name; },
348 getBinding: func(button) { return nil; },
349 isRepeatable: func() { return me.repeatable; }
350 };
351
352 var CustomButton = {
353 new: func() {
354 var m = { parents: [CustomButton, ButtonBinding.new("Custom", "", 0) ] };
355 m.custom_binding = nil;
356 return m;
357 },
358
359 clone: func() {
360 var m = { parents: [CustomButton, ButtonBinding.new("Custom", "", 0) ] };
361 m.custom_binding = me.custom_binding;
362 return m;
363 },
364
365 match: func(prop) {
366 if (prop.getNode("binding") != nil) {
367 var p = props.Node.new();
368 props.copy(prop.getNode("binding"), p);
369 me.custom_binding = p;
370 return 1;
371 } else {
372 return 0;
373 }
374 },
375
376 getBinding: func(button) {
377 var p = props.Node.new().getNode("button[" ~ button ~ "]", 1);
378 p.getNode("desc", 1).setValue(me.name);
379 props.copy(me.custom_binding, p.getNode("binding", 1));
380 return p;
381 },
382 };
383
384 var UnboundButton = {
385 new: func() {
386 var m = { parents: [UnboundButton, ButtonBinding.new("None", "", 0) ] };
387 return m;
388 },
389
390 clone: func() {
391 var m = { parents: [UnboundButton, ButtonBinding.new("None", "", 0) ] };
392 return m;
393 },
394
395 match: func(prop) {
396 return (prop.getNode("binding") != nil);
397 },
398
399 getBinding: func(button) {
400 var p = props.Node.new().getNode("button[" ~ button ~ "]", 1);
401 p.getNode("desc", 1).setValue(me.name);
402 return p;
403 },
404 };
405
406
407 var PropertyToggleButton = {
408 new: func(name, prop) {
409 var m = { parents: [PropertyToggleButton, ButtonBinding.new(name, prop, 0) ] };
410 return m;
411 },
412
413 clone: func() {
414 var m = { parents: [PropertyToggleButton, ButtonBinding.new(me.name, me.binding, 0) ] };
415 return m;
416 },
417
418 match: func(prop) {
419
420 var c = prop.getNode("binding", 1).getNode("command", 1).getValue();
421 var p = prop.getNode("binding", 1).getNode("property", 1).getValue();
422 return ((c == "property-toggle") and (p == me.binding));
423 },
424
425 getBinding: func(button) {
426 var p = props.Node.new().getNode("button[" ~ button ~ "]", 1);
427 p.getNode("desc", 1).setValue(me.name);
428 p.getNode("binding", 1).getNode("command", 1).setValue("property-toggle");
429 p.getNode("binding", 1).getNode("property", 1).setValue(me.binding);
430 return p;
431 },
432 };
433
434 var PropertyAdjustButton = {
435 new: func(name, prop, step) {
436 var m = { parents: [PropertyAdjustButton, ButtonBinding.new(name, prop, 0) ] };
437 m.step = step;
438 return m;
439 },
440
441 clone: func() {
442 var m = { parents: [PropertyAdjustButton, ButtonBinding.new(me.name, me.binding, 0) ] };
443 m.step = me.step;
444 return m;
445 },
446
447 match: func(prop) {
448 var c = prop.getNode("binding", 1).getNode("command", 1).getValue();
449 var p = prop.getNode("binding", 1).getNode("property", 1).getValue();
450 var s = prop.getNode("binding", 1).getNode("step", 1).getValue();
451 return ((c == "property-adjust") and (p == me.binding) and (s == me.step));
452 },
453
454 getBinding: func(button) {
455 var p = props.Node.new().getNode("button[" ~ button ~ "]", 1);
456 p.getNode("desc", 1).setValue(me.name);
457 p.getNode("binding", 1).getNode("command", 1).setValue("property-adjust");
458 p.getNode("binding", 1).getNode("property", 1).setValue(me.binding);
459 p.getNode("binding", 1).getNode("step", 1).setValue(me.step);
460 return p;
461 },
462 };
463
464 var NasalButton = {
465 new: func(name, script, repeatable) {
466 var m = { parents: [NasalButton, ButtonBinding.new(name, script, repeatable) ] };
467 return m;
468 },
469
470 clone: func() {
471 var m = { parents: [NasalButton, ButtonBinding.new(me.name, me.binding, me.repeatable) ] };
472 return m;
473 },
474
475 match: func(prop) {
476 var c = prop.getNode("binding", 1).getNode("command", 1).getValue();
477 var p = prop.getNode("binding", 1).getNode("script", 1).getValue();
478
479 if (p == nil) { return 0; }
480
481 p = string.trim(p);
482 p = string.replace(p, ";", "");
483 p = p ~ ";";
484
485 return ((c == "nasal") and (p == me.binding));
486 },
487
488 getBinding: func(button) {
489 var p = props.Node.new().getNode("button[" ~ button ~ "]", 1);
490 p.getNode("desc", 1).setValue(me.name);
491 p.getNode("binding", 1).getNode("command", 1).setValue("nasal");
492 p.getNode("binding", 1).getNode("script", 1).setValue(me.binding);
493 p.getNode("repeatable", 1).setValue(me.repeatable);
494 return p;
495 },
496 };
497
498
499 var NasalHoldButton = {
500 new: func(name, script, scriptUp) {
501 var m = { parents: [NasalHoldButton, ButtonBinding.new(name, script, 0) ] };
502 m.scriptUp = scriptUp;
503 return m;
504 },
505
506 clone: func() {
507 var m = { parents: [NasalHoldButton, ButtonBinding.new(me.name, me.binding, 0) ] };
508 m.scriptUp = me.scriptUp;
509 return m;
510 },
511
512 match: func(prop) {
513
514 var c = prop.getNode("mod-up", 1).getNode("binding", 1).getNode("command", 1).getValue();
515 var p1 = prop.getNode("binding", 1).getNode("script", 1).getValue();
516 var p2 = prop.getNode("mod-up", 1).getNode("binding", 1).getNode("script", 1).getValue();
517
518 if (p2 == nil) { return 0; }
519
520 p1 = string.trim(p1);
521 p1 = string.replace(p1, ";", "");
522 p1 = p1 ~ ";";
523
524 return ((c == "nasal") and (p1 == me.binding));
525 },
526
527 getBinding: func(button) {
528 var p = props.Node.new().getNode("button[" ~ button ~ "]", 1);
529 p.getNode("desc", 1).setValue(me.name);
530 p.getNode("repeatable", 1).setValue("false");
531 p.getNode("binding", 1).getNode("command", 1).setValue("nasal");
532 p.getNode("binding", 1).getNode("script", 1).setValue(me.binding);
533 p.getNode("mod-up", 1).getNode("binding", 1).getNode("command", 1).setValue("nasal");
534 p.getNode("mod-up", 1).getNode("binding", 1).getNode("script", 1).setValue(me.scriptUp);
535 return p;
536 },
537 };
538
539 var buttonBindings = [
540 NasalButton.new("Throttle Up", "controls.incThrottle(0.01, 1.0);", 1),
541 NasalButton.new("Throttle Down", "controls.incThrottle(-0.01, -1.0);", 1),
542 NasalButton.new("Mixture Rich", "controls.adjMixture(1);", 1),
543 NasalButton.new("Mixture Lean", "controls.adjMixture(-1);", 1),
544 NasalButton.new("Propeller Fine", "controls.adjPropeller(1);", 1),
545 NasalButton.new("Propeller Coarse", "controls.adjPropeller(-1);", 1),
546
547 NasalButton.new("Elevator Trim Up", "controls.elevatorTrim(-1);", 1),
548 NasalButton.new("Elevator Trim Down", "controls.elevatorTrim(1);", 1),
549 NasalButton.new("Elevator Trim Pos", "controls.setElevatorTrimToPosition();", 1),
550 NasalButton.new("Rudder Trim Left", "controls.rudderTrim(-1);", 1),
551 NasalButton.new("Rudder Trim Right", "controls.rudderTrim(1);", 1),
552 NasalButton.new("Rudder Trim Pos", "controls.setRudderTrimToPosition();", 1),
553 NasalButton.new("Aileron Trim Left", "controls.aileronTrim(-1);", 1),
554 NasalButton.new("Aileron Trim Right", "controls.aileronTrim(1);", 1),
555 NasalButton.new("Aileron Trim Pos", "controls.setAileronToPosition();", 1),
556 NasalButton.new("Trim to Positions", "controls.setElevatorTrimToPosition();controls.setAileronTrimToPosition();controls.setRudderTrimToPosition();", 1),
557 NasalHoldButton.new("FGCom PTT", "controls.ptt(1);", "controls.ptt(0);"),
558 NasalHoldButton.new("FGCom PTT(2)", "controls.ptt(2);", "controls.ptt(0);"),
559 NasalHoldButton.new("Trigger", "controls.trigger(1);", "controls.trigger(0);"),
560 NasalHoldButton.new("Pickle", "controls.applyPickle(1);","controls.applyPickle(0);"),
561 NasalButton.new("Flaps Up", "controls.flapsDown(-1);",0),
562 NasalButton.new("Flaps Down", "controls.flapsDown(1);",0),
563 NasalButton.new("Gear Up", "controls.gearDown(-1);",0),
564 NasalButton.new("Gear Down", "controls.gearDown(1);",0),
565 NasalButton.new("Gear Toggle", "controls.gearTogglePosition(1);",0),
566 NasalButton.new("Reverser Toggle", "controls.reverserTogglePosition();",0),
567 NasalHoldButton.new("Spoilers Retract", "controls.stepSpoilers(-1);", "controls.stepSpoilers(0);"),
568 NasalHoldButton.new("Spoilers Deploy", "controls.stepSpoilers(1);", "controls.stepSpoilers(0);"),
569 NasalHoldButton.new("Brakes", "controls.applyBrakes(1);", "controls.applyBrakes(0);"),
570 NasalHoldButton.new("Brakes (air/wheel)", "controls.applyApplicableBrakes(1);", "controls.applyApplicableBrakes(0);"),
571 NasalHoldButton.new("Parking brakes", "controls.parkingBrakeToggle(0);", "controls.parkingBrakeToggle(1);"),
572 NasalHoldButton.new("NWS toggle", "controls.toggleNWS(0);", "controls.toggleNWS(1);"),
573 NasalButton.new("Autopilot disconnect", "controls.autopilotDisconnect();",0),
574
575 PropertyToggleButton.new("Total Freeze", "/sim/freeze/clock"),
576
577 # with all of these it is expected the the armament system in the selected aircraft
578 # will manage the wrap around and or reset to zero.
579 PropertyAdjustButton.new("Target next", "/controls/armament/target-selected", "1"),
580 PropertyAdjustButton.new("Target previous", "/controls/armament/target-selected", "-1"),
581 PropertyAdjustButton.new("Weapon next", "/controls/armament/weapon-selected", "1"),
582 PropertyAdjustButton.new("Weapon previous", "/controls/armament/weapon-selected", "-1"),
583 PropertyAdjustButton.new("Azimuth left", "/controls/radar/azimuth-deg", "-5"),
584 PropertyAdjustButton.new("Azimuth right", "/controls/radar/azimuth-deg", "5"),
585 PropertyAdjustButton.new("Elevation up", "/controls/radar/elevation-deg", "5"),
586 PropertyAdjustButton.new("Elevation down", "/controls/radar/elevation-deg", "-5"),
587 PropertyAdjustButton.new("Missile Reject", "/controls/armament/missile-reject", "1"),
588
589 NasalButton.new("View Decrease", "view.decrease(0.75);", 1),
590 NasalButton.new("View Increase", "view.increase(0.75);", 1),
591 NasalButton.new("View Cycle Forwards", "view.stepView(1);", 0),
592 NasalButton.new("View Cycle Backwards", "view.stepView(-1);", 0),
593 PropertyAdjustButton.new("View Left", "/sim/current-view/goal-heading-offset-deg", "30.0"),
594 PropertyAdjustButton.new("View Right", "/sim/current-view/goal-heading-offset-deg", "-30.0"),
595 PropertyAdjustButton.new("View Up", "/sim/current-view/goal-pitch-offset-deg", "20.0"),
596 PropertyAdjustButton.new("View Down", "/sim/current-view/goal-pitch-offset-deg", "-20.0"),
597 CustomButton.new(),
598 ];
599
600 # Parse config from the /input tree and write it to the
601 # dialog_root.
602 var readConfig = func(dialog_root="/sim/gui/dialogs/joystick-config") {
603
604 var js_name = getprop(dialog_root ~ "/selected-joystick");
605 var joysticks = props.globals.getNode("/input/joysticks").getChildren("js");
606
607 if (size(joysticks) == 0) { return 0; }
608
609 if (js_name == nil) {
610 js_name = joysticks[0].getNode("id").getValue();
611 }
612
613 var js = nil;
614
615 forindex (var i; joysticks) {
616 if ((joysticks[i].getNode("id") != nil) and
617 (joysticks[i].getNode("id").getValue() == js_name))
618 {
619 js = joysticks[i];
620 setprop(dialog_root ~ "/selected-joystick", js_name);
621 setprop(dialog_root ~ "/selected-joystick-index", i);
622 setprop(dialog_root ~ "/selected-joystick-config", joysticks[i].getNode("source").getValue());
623 }
624 }
625
626 if (js == nil) {
627 # We didn't find the joystick we expected - default to the first
628 setprop(dialog_root ~ "/selected-joystick", joysticks[0].getNode("id").getValue());
629 setprop(dialog_root ~ "/selected-joystick-index", 0);
630 setprop(dialog_root ~ "/selected-joystick-config", joysticks[0].getNode("source").getValue());
631 }
632
633 # Set up the axes assignments
634 var axes = js.getChildren("axis");
635
636 for (var axis = 0; axis < MAX_AXES; axis = axis +1) {
637
638 var p = props.globals.getNode(dialog_root ~ "/axis[" ~ axis ~ "]", 1);
639 p.remove();
640 p = props.globals.getNode(dialog_root ~ "/axis[" ~ axis ~ "]", 1);
641
642 # Note that we can't simply use an index into the axes array
643 # as that doesn't work for a sparsley populated set of axes.
644 # E.g. one with n="3"
645 var a = js.getNode("axis[" ~ axis ~ "]");
646
647 if (a != nil) {
648 # Read properties from bindings
649 props.copy(a, p.getNode("original_binding", 1));
650
651 var binding = nil;
652 foreach (var b; joystick.axisBindings) {
653 if ((binding == nil) and (a != nil) and b.match(a)) {
654 binding = b.clone();
655 binding.parse(a);
656 p.getNode("binding", 1).setValue(binding.getName());
657 p.getNode("invertable", 1).setValue(binding.isInvertable());
658 p.getNode("inverted", 1).setValue(binding.isInverted());
659 p.getNode("power", 1).setValue(binding.getPower());
660 }
661 }
662
663 if (binding == nil) {
664 # No binding for this axis
665 p.getNode("binding", 1).setValue("None");
666 p.getNode("invertable", 1).setValue(0);
667 p.getNode("inverted", 1).setValue(0);
668 p.getNode("power", 1).setValue(1.0);
669 p.removeChild("original_binding");
670 }
671 } else {
672 p.getNode("binding", 1).setValue("None");
673 p.getNode("invertable", 1).setValue(0);
674 p.getNode("inverted", 1).setValue(0);
675 p.getNode("power", 1).setValue(1.0);
676 p.removeChild("original_binding");
677 }
678 }
679
680 # Set up button assignment.
681 var buttons = js.getChildren("button");
682
683 for (var button = 0; button < MAX_BUTTONS; button = button + 1) {
684 var btn = props.globals.getNode(dialog_root ~ "/button[" ~ button ~ "]", 1);
685 btn.remove();
686 btn = props.globals.getNode(dialog_root ~ "/button[" ~ button ~ "]", 1);
687
688 # Note that we can't simply use an index into the buttons array
689 # as that doesn't work for a sparsley populated set of buttons.
690 # E.g. one with n="3"
691 var a = js.getNode("button[" ~ button ~ "]");
692
693 if (a != nil) {
694 # Read properties from bindings
695 props.copy(a, btn.getNode("original_binding", 1));
696 var binding = nil;
697 foreach (var b; joystick.buttonBindings) {
698 if ((binding == nil) and (a != nil) and b.match(a)) {
699 binding = b.clone();
700 btn.getNode("binding", 1).setValue(binding.getName());
701 props.copy(b.getBinding(button), btn.getNode("original_binding", 1));
702 }
703 }
704
705 if (b == nil) {
706 btn.getNode("binding", 1).setValue("None");
707 btn.removeChild("original_binding");
708 }
709 } else {
710 btn.getNode("binding", 1).setValue("None");
711 btn.removeChild("original_binding");
712 }
713 }
714
715 # Set up Nasal code.
716 var nasals = js.getChildren("nasal");
717
718 for (var nasal = 0; nasal < MAX_NASALS; nasal = nasal + 1) {
719 var nas = props.globals.getNode(dialog_root ~ "/nasal[" ~ nasal ~ "]", 1);
720 nas.remove();
721 nas = props.globals.getNode(dialog_root ~ "/nasal[" ~ nasal ~ "]", 1);
722
723 # Note that we can't simply use an index into the buttons array
724 # as that doesn't work for a sparsley populated set of buttons.
725 # E.g. one with n="3"
726 var a = js.getNode("nasal[" ~ nasal ~ "]");
727 if (a != nil) {
728 props.copy(a, nas.getNode("original_script", 1));
729 }
730 }
731 }
732
733 var writeConfig = func(dialog_root="/sim/gui/dialogs/joystick-config", reset=0) {
734
735 # Write out the joystick file.
736 var config = props.Node.new();
737 var id = getprop(dialog_root ~ "/selected-joystick");
738
739 if (reset == 1) {
740 # We've been asked to reset the joystick config to the default. As we can't
741 # delete the configuration file, we achieve this by setting an invalid name
742 # tag that won't match.
743 config.getNode("name", 1).setValue("UNUSED INVALID CONFIG");
744 } else {
745 config.getNode("name", 1).setValue(id);
746 }
747
748 var nasals = props.globals.getNode(dialog_root).getChildren("nasal");
749 forindex (var nas; nasals) {
750 var nasalscript = config.getNode("nasal[" ~ nas ~ "]", 1);
751 props.copy(props.globals.getNode(dialog_root ~ "/nasal[" ~ nas ~ "]/original_script", 1), nasalscript);
752 }
753
754 var axes = props.globals.getNode(dialog_root).getChildren("axis");
755 forindex (var axis; axes) {
756 var name = getprop(dialog_root ~ "/axis[" ~ axis ~ "]/binding");
757
758 if (name != "None") {
759 foreach (var binding; axisBindings) {
760 if (binding.getName() == name) {
761 var b = binding.clone();
762 b.setInverted(getprop(dialog_root ~ "/axis[" ~ axis ~ "]/inverted"));
763 b.setPower(getprop(dialog_root ~ "/axis[" ~ axis ~ "]/power"));
764
765 # Generate the axis and binding
766 var axisnode = config.getNode("axis[" ~ axis ~ "]", 1);
767 if (name == "Custom") {
768 props.copy(props.globals.getNode(dialog_root ~ "/axis[" ~ axis ~ "]/original_binding", 1), axisnode);
769 } else {
770 props.copy(b.getBinding(axis), axisnode);
771 }
772 }
773 }
774 }
775 }
776
777 var buttons = props.globals.getNode(dialog_root).getChildren("button");
778 forindex (var btn; buttons) {
779 var name = getprop(dialog_root ~ "/button[" ~ btn ~ "]/binding");
780
781 if (name != "None") {
782 foreach (var binding; buttonBindings) {
783 if (binding.getName() == name) {
784 var b = binding.clone();
785 # Generate the axis and binding
786 var buttonprop = config.getNode("button[" ~ btn ~ "]", 1);
787 if (name == "Custom") {
788 props.copy(props.globals.getNode(dialog_root ~ "/button[" ~ btn ~ "]/original_binding", 1), buttonprop);
789 } else {
790 props.copy(b.getBinding(btn), buttonprop);
791 }
792 }
793 }
794 }
795 }
796
797 var filename = id;
798 filename = string.replace(filename, " ", "-");
799 filename = string.replace(filename, ".", "");
800 filename = string.replace(filename, "/", "");
801
802 # Write out the file
803 io.write_properties(getprop("/sim/fg-home") ~ "/Input/Joysticks/" ~ filename ~ ".xml", config);
804 }
805
806 var resetConfig = func(dialog_root="/sim/gui/dialogs/joystick-config") {
807 writeConfig(dialog_root, 1);
808 }