Writing Joystick Code: Part 1: Difference between revisions

Jump to navigation Jump to search
no edit summary
m (Use Geshi line numbering and styling)
No edit summary
Line 113: Line 113:
There is almost no end to what we can do with buttons, limited usually by the number of buttons on the joystick, what we need, and what we can remember of the allocation of button actions.
There is almost no end to what we can do with buttons, limited usually by the number of buttons on the joystick, what we need, and what we can remember of the allocation of button actions.


There are two categories of buttons, repeatable and non-repeatable. With non-repeatable buttons, when we push it, a single action is carried out, and nothing else happens. An example would be a button to lower the landing-gear. We press the button, the gear goes down. With repeatable buttons, when we hold the button in, the same action is carried out over and over, until we release the button. An example is zoom-in. We press the button, and the view keeps zooming in. When we have the view we want, we release the button, and zooming stops.
=== Repeatable/Non-repeatable ===


There are two categories of buttons, repeatable and non-repeatable. With non-repeatable buttons, when we push it, a single action is carried out, and nothing else happens. An example of its use would be a button to lower the landing-gear. We press the button, the gear goes down. With repeatable buttons, when we hold the button in, the same action is carried out over and over, until we release the button. An example is zoom-in. We press the button, and the view keeps zooming in. When we have the view we want, we release the button, and zooming stops.
An important point to remeber is that if a button is repeatable, even if you just give it the briefest of touches, the instruction will be carried out a few times, thanks to the repeat-rate of the system.
Setting a button to repeatable or non-repeatable is part of the basic definition of the button (like its number), it cannot be changed willy-nilly. So when you decide on the use for joystick buttons, you decide ''which will be repeatable and which not''.
Setting a button to repeatable or non-repeatable is part of the basic definition of the button (like its number), it cannot be changed willy-nilly. So when you decide on the use for joystick buttons, you decide ''which will be repeatable and which not''.


Line 135: Line 137:
   <repeatable>false</repeatable>
   <repeatable>false</repeatable>


For a repeatable button, replace false with true.
To make a button repeatable, replace false with true.


You can use this shell for all your buttons. Just use the correct button number, give the correct <desc>, decide repeatable or non-repeatable, and then type the correct code in place of ''Your code goes here'' and ''Code for what happens when the button is released goes here''. Not all actions need a <mod-up> section, but you will soon get the hang of what does and what doesn't.
You can use this shell for all your buttons. Just use the correct button number, give the correct <desc>, decide repeatable or non-repeatable, and then type the correct code in place of ''Your code goes here'' and ''Code for what happens when the button is released goes here''. Not all actions need a <mod-up> section, but you will soon get the hang of what does and what doesn't.
Line 147: Line 149:


Note that all lines except the last have a ; at the end. This lets the interpreter know that you haven't finished yet.
Note that all lines except the last have a ; at the end. This lets the interpreter know that you haven't finished yet.
=== A note on <mod-up> ===
I have been having a good look at the controls.nas file that comes with FG. With most of them, where you would use
  controls.someAction(1)
to do something, you are allowed to use
  controls.someAction(0)
in <mod-up>, but it does nothing. Xml files are full of such code, which is totally, unnecessary. It does no harm, just wastes time, albeit very little time. In fact, the only time a <mod-up> is needed is when a non-repeatable button starts an ongoing process, and we want to stop that process when we release the button. In Part2 and Part 3 you will see almost no <mod-up> code at all.
One advantage  of leaving out unnecessary <mod-up> code is that it makes the code easier to read. (And saves a couple of trees when the files are printed.)




Line 153: Line 166:
There is a limit to the number of buttons on the joystick and lots of things we'd like to do with them. Fortunately, FG makes it easy for us. It lets us know if any or more than one of the Shift, Alt and Ctrl buttons are pressed. We can thus modify the action of a button with the <mod-shift>, <mod-ctrl> and <mod-alt> tags.
There is a limit to the number of buttons on the joystick and lots of things we'd like to do with them. Fortunately, FG makes it easy for us. It lets us know if any or more than one of the Shift, Alt and Ctrl buttons are pressed. We can thus modify the action of a button with the <mod-shift>, <mod-ctrl> and <mod-alt> tags.


Here is an example of what you need to modify the action of a button when the Shift key is held in when you press the button.
Here is an example of what you need to modify the action of a button when the Shift key is held in when you press the button. I have deliberately included <mod-up> tags so that you can see where they must go, should they be necessary.
   <button n="0">
   <button n="0">
     <desc>''Normal action/Shift action''</desc>
     <desc>''Normal action/Shift action''</desc>
224

edits

Navigation menu