Using a Nasal file with a joystick Part 2

From FlightGear wiki
Revision as of 06:59, 4 February 2013 by Macnab (talk | contribs)
Jump to navigation Jump to search

In Using a Nasal file with a joystick you saw how to get started. This article goes into it in more depth.


First Steps

Backup your xml file. Print it, you need to know which button does what. If you have a Nasal file, back it up too. If you haven't read the first part, read it and do the basic implementation now.


Modifying your xml file

This seems like a lot of work, but it is worth it in the end.

You need to use the labels of the buttons on your joystick. You will use these labels for the name of the Nasal function to call in your nasal file.

For each button in your xml file, change your code. Here, the button is labelled "1". And remember, we are talking about the label printed on the joystick, not the button number assigned by the operating system.

  <button n="xxx">
    <!-- Labled as 1 -->
    <desc>Whatever it does</desc>
    <binding>
      <command>nasal</command>
      <script>nasfilename.button1()</script>
    </binding>
  </button>

Of course, nasfilename is the name of your Nasal file.

If you have a modifier button, named mymod, then do this

  <button n="xxx">
    <!-- Labled as 1 -->
    <desc>Whatever it does</desc>
    <binding>
      <command>nasal</command>
      <script>nasfilename.button1(mymod)</script>
    </binding>
  </button>

If you use the keyboard Shift, Ctrl or Alt buttons as a modifier, you do this

  <button n="xxx">
    <!-- Labled as 1 -->
    <desc>Whatever it does</desc>
    <binding>
      <command>nasal</command>
      <script>nasfilename.button1(1)</script>
    </binding>
    <mod-shift>
      <binding>
      <command>nasal</command>
      <script>nasfilename.button1(2)</script>
      </binding>
    </mod-shift>
  </button>

If you use 2 keyboard buttons then you would need another <mod-...> section, and pass the value 3. To help you remember, add a comment at the top of your Nasal file to remind you what the values mean.

If your button needs a mod-up, then do this

  <button n="xxx">
    <!-- Labled as 1 -->
    <desc>Whatever it does</desc>
    <binding>
      <command>nasal</command>
      <script>nasfilename.button1()</script>
    </binding>
    <mod-up>
      <binding>
        <command>nasal</command>
        <script>nasfilename.button1Up()</script>
      </binding>
    </mod-up>
  </button>

but this is very seldom needed. Of course, if you use modifiers you need to pass the number of the modifier in the brackets.

You could use 2 modifiers simultaneously. The Saitek Pro Flight Yoke has a Mode Switch, which needs special methods to access it - see Using Saitek Pro Flight Yoke Mode Switch, then could have a case where you have these modes (which simulate using the keyboard Shift, Ctrl and Alt keys) as well as a modifier button on the joystick.

In such a case do this <syntaxhightlight land="xml">

 <button n="6">
   <desc>Whatever</desc>
   <repeatable>true</repeatable>
   <binding>
     <command>nasal</command>
     <script>saitekyoke.buttonC1(saitekAlter, 1)</script>
   </binding>
   <mod-shift>
     <binding>
       <command>nasal</command>
       <script>saitekyoke.buttonC2(saitekAlter, 2)</script>
     </binding>
   </mod-shift>
   <mod-ctrl>
     <binding>
       <command>nasal</command>
       <script>saitekyoke.buttonC1(saitekAlter, 3)</script>
     </binding>
   </mod-ctrl>
 </button>

</syntaxhightlight> Here, saitekAlter is the value (0 or 1) of the modifier button, and the 1, 2 and 3 represent the position of the Mode Switch.


Now do this for all the buttons.


Contents of the Nasal file

The blank file

For each button you need a function defined. In our simplest example above you need

  var button1 = func {
        ... code goes here ...
  }

If you pass the value of a modifier button then it is slightly different. Assuming that your modifier button is 0 for not pressed and 1 for pressed, we can make use of the fact that in Nasal 0 represents false, and any other value represents true

  var button1 = func(mod) {
    if (mod) {
      ... code for modifier button pressed goes here ...
    } else {
      ... code for modifier not pressed goes here ...
    }
  }