Using a Nasal file with a joystick Part 2: Difference between revisions

From FlightGear wiki
Jump to navigation Jump to search
mNo edit summary
Line 6: Line 6:
Backup your xml file.  
Backup your xml file.  
Print it, you need to know which button does what.
Print it, you need to know which button does what.
If you have a Nasal file, back it up too.  
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.
If you haven't read the first part, read it and do the basic implementation now.


Line 15: Line 17:


You need to use the labels of the buttons on your joystick.  
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.
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".  
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  
And remember, we are talking about the label printed on the joystick, not the button number  
assigned by the operating system.
assigned by the operating system.
<syntaxhighlight lang="xml">
<syntaxhighlight lang="xml">
Line 31: Line 36:
</syntaxhighlight>
</syntaxhighlight>
Of course, nasfilename is the name of your Nasal file.
Of course, nasfilename is the name of your Nasal file.


If you have a modifier button, named ''mymod'', then do this
If you have a modifier button, named ''mymod'', then do this
Line 43: Line 49:
   </button>
   </button>
</syntaxhighlight>
</syntaxhighlight>


If you use the keyboard Shift, Ctrl or Alt buttons as a modifier, you do this
If you use the keyboard Shift, Ctrl or Alt buttons as a modifier, you do this
Line 61: Line 68:
   </button>
   </button>
</syntaxhighlight>
</syntaxhighlight>


If you use 2 keyboard buttons then you would need another <mod-...> section, and pass the value 3.  
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.
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
If your button needs a mod-up, then do this
Line 83: Line 93:
</syntaxhighlight>
</syntaxhighlight>
but this is very seldom needed. Of course, if you use modifiers you need to pass the number  
but this is very seldom needed. Of course, if you use modifiers you need to pass the number  
of the modifier in the brackets.
of the modifier in the brackets.


You could use 2 modifiers simultaneously. The Saitek Pro Flight Yoke has a Mode Switch, which  
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  
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)  
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.
as well as a modifier button on the joystick.


In such a case do this
In such a case do this
Line 115: Line 131:
</syntaxhighlight>
</syntaxhighlight>
Here, saitekAlter is the value (0 or 1) of the modifier button, and the 1, 2 and 3  
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.
represent the position of the Mode Switch.




Now do this for all the buttons.
Now do this for all the buttons.


==  Contents of the Nasal file  ==
==  Contents of the Nasal file  ==
Line 129: Line 148:
   }
   }
</syntaxhighlight>
</syntaxhighlight>


If you pass the value of a modifier button then it is slightly different.  
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  
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
that in Nasal 0 represents false, and any other value represents true
<syntaxhighlight lang ="php">
<syntaxhighlight lang ="php">
Line 143: Line 165:
</syntaxhighlight>
</syntaxhighlight>


When have values other than just 0 and 1 then you need specific if clauses
<syntaxhighlight lang ="php">
  var button1 = func(mod) {
    if (mod == 1) {
      ... code for modifier value 1 goes here ...
    }
    if (mod == 2) {
      ... code for modifier value 2 goes here ...
    }
  }
</syntaxhighlight>
If you have 2 modifiers, as in the case of the Saitek Yoke with modes and a modifier button,
then you need to nest the if clauses
<syntaxhighlight lang ="php">
  var button1 = func(alter, mode) {
    if (mode == 1) {
      if (alter) {
        ... Mode 1, mod button pressed ...
      } else {
        ... Mode 1, mod button not pressed ...
      }
    }
    if (mode == 2) {
      if (alter) {
        ... Mode 2, mod button pressed ...
      } else {
        ... Mode 2, mod button not pressed ...
      }
    }
  }
    if (mode == 3) {
      if (alter) {
        ... Mode 3, mod button pressed ...
      } else {
        ... Mode 3, mod button not pressed ...
      }
    }
  }
</syntaxhighlight>





Revision as of 07:13, 4 February 2013

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

  <button n="6">
    <!-- Labled as C1 -->
    <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>

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 ...
    }
  }


When have values other than just 0 and 1 then you need specific if clauses

  var button1 = func(mod) {
    if (mod == 1) {
      ... code for modifier value 1 goes here ...
    } 
    if (mod == 2) {
      ... code for modifier value 2 goes here ...
    }
  }


If you have 2 modifiers, as in the case of the Saitek Yoke with modes and a modifier button,

then you need to nest the if clauses

  var button1 = func(alter, mode) {
    if (mode == 1) {
       if (alter) {
         ... Mode 1, mod button pressed ...
       } else {
         ... Mode 1, mod button not pressed ...
       }
    } 
    if (mode == 2) {
      if (alter) {
         ... Mode 2, mod button pressed ...
      } else {
         ... Mode 2, mod button not pressed ...
      }
    }
  }
    if (mode == 3) {
      if (alter) {
         ... Mode 3, mod button pressed ...
      } else {
         ... Mode 3, mod button not pressed ...
      }
    }
  }