-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05-08-the-joystick-and-mouse.html
406 lines (321 loc) · 15.9 KB
/
05-08-the-joystick-and-mouse.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
<!doctype html>
<html lang="en">
<head>
<!--
This Amos Professional Manual is written by asymetrix for the Amiga community and should stay completely FREE FOREVER.
Created 2008. :)
It was created from the original AMOS Professional Manual by Europress Software Ltd.
It has been updated by Fredrik Rambris.
-->
<title>The Joystick and Mouse - AMOS Professional Manual</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=yes, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta name="keywords" content="Amos Professional, Amiga, Programming, Basic, Francois Lionet, Europress Software Ltd, Amos, computing, code, AmigaDOS">
<meta name="author" content="asymetrix,Fredrik Rambris">
<link rel="GitHub" href="https://github.com/fredrik-rambris/amospromanual">
<meta property="og:site_name" content="AMOS Professional Manual">
<meta property="og:image" content="https://amospromanual.dev/images/cover.jpg">
<link rel="stylesheet" type="text/css" href="styles.css">
<link rel="canonical" href="https://amospromanual.dev/05-08-the-joystick-and-mouse.html">
</head>
<body>
<section>
<h1>The Joystick and Mouse</h1>
This Chapter clarifies all aspects of controlling and exploiting the joystick and mouse in your
programs.
</section>
<section id="01-joysticks">
<h2>Joysticks</h2>
<p>A joystick can be used to control movement around the screen by pushing its handle in the
desired direction, and to trigger all sorts of actions by pressing one or more buttons built in to its
mechanism. Either of the two joystick sockets at the back or side of your Amiga will happily
accept a joystick plug. If two users want to control one joystick each for specially written
programs, both ports can be used. To make a joystick interact with your programs, the computer
!Weds to be able to read its movements and actions. AMOS Professional offers a number of
useful functions to do just that.</p>
<h3 class="command" id="fn-joy">JOY</h3>
<p><i>function: read status of joystick</i><br>
status=<b>Joy</b>(port number)</p>
<p>This inspects what is happening with the joystick and makes a report. If the joystick you are
interested in is plugged into the joystick port, the computer must be told to look at port number
(1). If you are using the mouse port call that port number (0). For example:</p>
<code class="prefix edit">Do
J=Joy(1)
Print Bin$(J,5),J
Loop
</code>
<p>When you run that routine, reports are given about the movements of the joystick and the
status of the fire-button in the form of binary numbers. The pattern of ones and zeros in the
report can then be inspected. Binary bits shown as zero indicate that nothing is happening,
whereas if any of the bits in the report is shown as a one, it means that the joystick has been
moved in the direction that relates to that bit. Here is a list of those bits along with their
meanings.</p>
<pre>
<b>Bit number Meaning</b>
0 Joystick has been moved Up
1 Joystick has been moved Down
2 Joystick has been moved Left
3 Joystick has been moved Right
4 Fire-button has been pressed
</pre>
<p>
Each of those aspects of the joystick status can be accessed individually, using the following
functions:</p>
<h3 class="command" id="fn-jleft">JLEFT</h3>
<p><i>function: test for joystick movement towards the left</i><br>
x=<b>Jleft</b>(port number)</p>
<p>This returns a value of -1 (meaning True) if the joystick connected to the given port number has
been pushed to the left, otherwise a value of 0 is returned (meaning False).</p>
<p>The three other function in this family are self-evident, as follows:</p>
<h3 class="command" id="fn-jright">JRIGHT</h3>
<p><i>function: test for joystick movement towards the right</i><br>
x=<b>Jright</b>(port number)</p>
<h3 class="command" id="fn-jup">JUP</h3>
<p><i>function: test for joystick movement upwards</i><br>
x=<b>Jup</b>(port number)</p>
<h3 class="command" id="fn-jdown">JDOWN</h3>
<p><i>function: test for joystick movement downwards</i><br>
x=<b>Jdown</b>(port number)</p>
<p>These functions can be demonstrated by the following example:</p>
<code class="prefix edit">Do
If Jleft(1) Then Print "WEST"
If Jright(1) Then Print "EAST"
If Jup(1) Then Print "NORTH"
If Jdown(1) Then Print "SOUTH"
Loop
</code>
<h3 class="command" id="fn-fire">FIRE</h3>
<p><i>function: test status of fire-button</i><br>
x=<b>Fire</b>(port number)</p>
<p>To set up a routine for testing to see if the fire-button has been pressed, use the FIRE function
followed by the joystick port number. A value of -1 will be given only if the fire-button on the
relevant joystick has been pressed.</p>
<code class="prefix edit">Do
F=Fire(1)
If F=-1 Then Centre "BANG!": Shoot
Print
Loop
</code>
</section>
<section id="02-the-mouse-pointer">
<h2>The mouse pointer</h2>
<p>The mouse is often used in practical programming whereas joysticks have become associated
with playing computer games, but they both do much the same thing. They can both control
moving objects on screen and be used to select from a range of on-screen options, using a
cursor.</p>
<p>The mouse cursor has been pre-programmed to look like a pointer arrow, along with two
additional standard shapes that can be selected at any time. The standard shapes have been
assigned the numbers one to three, as follows:</p>
<pre>
<b>Number Shape of mouse cursor</b>
1 Arrow pointer (default shape)
2 Cross-hair
3 Clock
</pre>
<h3 class="command" id="i-change-mouse">CHANGE MOUSE</h3>
<p><i>instruction: change the shape of the mouse pointer</i><br>
<b>Change Mouse</b> number</p>
<p>
To change the shape of the pointer arrow, use this command followed by the number of the
required shape listed above. For example:</p>
<code class="prefix edit">Do
For N=1 To 3
Change Mouse N
Wait 25
Next N
Loop
</code>
<p>There is no need to restrict your choice to these three shapes. If you select an image number
greater than three, AMOS Professional will look at an image stored in the sprite bank, and install
it as the mouse pointer. The first image in the bank may be called up by using Change Mouse 4,
the second by specifying number 5, and so on. To make use of this option, sprites can feature no
more than four colours, and they must be exactly 16 pixels wide, although any height is allowed.
For such oversized sprites, the SET SPRITE BUFFER command should be used, which is
explained in <a href="07-01-hardware-sprites.html">Chapter 7.1</a>.</p>
<h3 class="command" id="i-hide">HIDE</h3>
<p><i>instruction: remove the mouse pointer from the screen</i><br>
<b>Hide</b><br>
<b>Hide</b> On</p>
<p>This instruction hides the mouse pointer by making it invisible. Although it cannot be seen, it is
still active and sending back reports, and the position of the mouse pointer co-ordinates can still
be read. AMOS Professional will automatically count the number of times that the HIDE
instruction is used, and employ this number to SHOW the mouse pointer once again at your
command. If you prefer to keep the mouse pointer invisible all the time and ignore the counting
system, use the special ON version of the instruction, like this:</p>
<code class="prefix ex">Hide On</code>
<h3 class="command" id="i-show">SHOW</h3>
<p><i>instruction: reveal the mouse pointer back on screen</i><br>
<b>Show</b><br>
<b>Show</b> On</p>
<p>This makes the mouse pointer visible again after a HIDE instruction.</p>
<p>
As a default, the system counts the number of times that the HIDE command has been used,
then reveals the pointer on screen when the number of SHOWs equals the number of HIDEs. To
bypass this counting system and reveal the mouse pointer immediately, use SHOW ON.</p>
<code class="prefix edit">Do
For N=1 To 10
Hide : Wait N : Show
Next N
Loop
</code>
</section>
<section id="03-reading-the-status-of-the-mouse">
<h2>Reading the status of the mouse</h2>
<p>Whether or not the mouse pointer is visible, the computer must know two things in order to
make any use of the mouse. It needs to recognise where the mouse pointer is as well as if any of
the mouse buttons have been pressed.</p>
<h3 class="command" id="resv-x-mouse">X MOUSE</h3>
<p><i>reserved variable: report or set the x-co-ordinate of the mouse pointer</i><br>
<b>X Mouse</b><br>
x=<b>X Mouse</b></p>
<p>X MOUSE reports the current location of the x-coordinate of the mouse pointer. Because
movement is controlled by the mouse rather than by software, coordinates are given in
hardware notation, which is demonstrated by the following example:</p>
<code class="prefix edit">Do
Print X Mouse
Loop
</code>
<p>
This can also be used to set a new coordinate position for the mouse pointer and move it to a
specific position on the screen. This is done by assigning a value to X MOUSE as if it was a Basic
variable. For example:</p>
<code class="prefix edit">For N=200 To 350
X Mouse=N
Print X Mouse
Next N
</code>
<h3 class="command" id="resv-y-mouse">Y MOUSE</h3>
<p><i>reserved variable: report or set the y-coordinate of the mouse pointer</i><br>
<b>Y Mouse</b><br>
y=<b>Y Mouse</b></p>
<p>
Y MOUSE is used to give the y-coordinate of the mouse pointer in hardware co-ordinates, or to
reposition the mouse pointer on screen, and it is employed in exactly the same way as X
MOUSE.</p>
<code class="prefix edit">For N=150 To 300
X Mouse=N : Y Mouse=N/2
Print X Mouse : Print Y Mouse
Next N
</code>
<h3 class="command" id="fn-mouse-key">MOUSE KEY</h3>
<p><i>function: read status of mouse buttons</i><br>
k=<b>Mouse Key</b></p>
<p>
The MOUSE KEY function checks whether one of the mouse buttons has been pressed and
makes a report in the form of a binary pattern made up of these elements:</p>
<pre>
<b>Pattern Report</b>
Bit 0 Left mouse button
Bit 1 Right mouse button
Bit 2 Third mouse button if it exists
</pre>
<p>
As usual, the numbers zero and one make up the report, with a one displayed when the
relevant button is held down, otherwise a zero is shown. Try this routine:</p>
<code class="prefix edit">Curs Off
Do
Locate 0,0
M= Mouse Key : Print "Bit Pattern ";Bin$(M,8);" Number ";M
Loop
</code>
<h3 class="command" id="fn-mouse-click">MOUSE CLICK</h3>
<p><i>function: check for click of mouse button</i><br>
c=<b>Mouse Click</b></p>
<p>
This is similar to MOUSE KEY, but instead of checking to see whether or not a mouse button is
held down, MOUSE CLICK is only interested in whether the user has just made a single click on
a mouse button. It returns the familiar bit pattern of these elements:</p>
<pre>
<b>Pattern Report</b>
Bit 1 Single test for left mouse button
Bit 2 Single test for right mouse button
Bit 3 Single test for third mouse button, if available
</pre>
<p>
These bits are automatically re-set to zero after one test has been made, so they will only check
for a single key press at a time. Here is an example:</p>
<code class="prefix edit">Curs Off
Do
M=Mouse Click
If M<>0 Then Print "Bit Pattern ";Bin$(M,8);" Number";M
Loop
</code>
</section>
<section id="04-limiting-the-mouse-pointer">
<h2>Limiting the mouse pointer</h2>
<p>One of the commonest screen conventions for both leisure and serious programs is the use of
control panels. AMOS Professional relies on them extensively for ease of use and clarity.
Supposing you need to set up a control panel on your screen, but you want to prevent the
mouse pointer from wandering outside the area of that panel.</p>
<h3 class="command" id="i-limit-mouse">LIMIT MOUSE</h3>
<p><i>instruction: limit mouse pointer to part of the screen</i><br>
<b>Limit Mouse</b> x1 ,y1 To x2,y2<br>
<b>Limit Mouse</b></p>
<p>
This command sets up a rectangular area for the mouse pointer to move around, and traps it
inside the boundaries ,set by hardware coordinates, from the rectangle's top-left TO bottom
right-hand corner. For example:</p>
<code class="prefix edit">Limit Mouse 300,100 To 350,150</code>
<p>
If you need to restore freedom to the mouse pointer and allow it to move around the entire
screen, use the LIMIT MOUSE instruction on its own, without any coordinates after it. Note that
SCREEN OPEN must be followed by a WAIT VBL command before LIMIT MOUSE can be used,
otherwise no screen will be set up for screen limits to be set.</p>
</section>
<section id="05-finding-the-mouse-pointer">
<h2>Finding the mouse pointer</h2>
<p>If you already understand the concept of different screens and screen zone numbers, you will
appreciate that it is not difficult to lose track of the mouse pointer.</p>
<p>
You may need to keep a check on various screens and screen zones in order to keep in control of
the mouse pointer. If you do not already understand the concept of different screens and screen
zone numbers, you will need to become familiar with the various SCREEN commands and
ZONE functions.</p>
<p>
<b>MOUSE ZONE</b><br>
<i>function: check if the mouse pointer is in a zone</i><br>
zone number=<b>Mouse Zone</b></p><br>
<p>
The MOUSE ZONE function checks to see where the mouse pointer is currently located, and if
it has entered a screen zone, the number of that zone is returned. It is equivalent to the
following line:</p>
<code class="prefix ex">X=Hzone(X Mouse,Y Mouse)</code>
<h3 class="command" id="fn-mouse-screen">MOUSE SCREEN</h3>
<p><i>function: check which screen the mouse pointer is occupying</i><br>
screen number=<b>Mouse Screen</b></p>
<p>
Use MOUSE SCREEN to return the number of the screen where the mouse pointer is currently
located, like this:</p>
<code class="prefix edit">X=Mouse Screen
Print X
</code>
</section>
<section id="06-displaying-menus-with-the-mouse-pointer">
<h2>Displaying menus with the mouse pointer</h2>
<p>Finally, as an AMOS Professional programmer, you will want to make use of the automatic
facility for displaying all the menus whose root starts from the current position of the mouse
pointer.</p>
<p>
<b>MENU MOUSE</b><br>
<i>instruction: display menu under current mouse pointer location</i><br>
<b>Menu Mouse On</b><br>
<b>Menu Mouse Off</b></p>
<p>
When this facility is turned ON, any menus that have been set up at the current location of the
mouse pointer are instantly displayed on screen. The mouse coordinates are added to the
MENU BASE in order to calculate the position where the menus are displayed, so you are able
to place a menu at a pre-set distance away from the mouse pointer if you like. To stop this
automatic process, simply use MENU MOUSE OFF.</p>
<p>
Please see <a href="06-05-menus.html">Chapter 6.5</a> which deals with all aspects of AMOS Professional menus, if you are not
experienced in their use.</p>
</section>
<footer>
<a href="05-07-windows.html" rel="prev">Windows</a>
<a href="./">Contents</a>
<a href="14-appendix-g-command-index.html">Index</a>
<a href="05-09-memory-banks.html" rel="next">Memory Banks</a>
</footer>
</body>
</html>