----------------------------------
JUMPSTART BASIC: E-E-E-K, a Mouse!
By MARK SCULLY
----------------------------------

     (EDITOR'S NOTE -- The listings in this article are not included in the
Program Menu. However, they are on this disk in the same form given here.
Each listing has a RUN command, as in RUN "J:PROGRAMS/MOUSEGO". Type the
load command in Basic immediate mode, which is reached by loading Amiga
Basic, then clicking the mouse pointer at left of screen.)

     Clicking mouse buttons makes things happen. This much is obvious
the first time you hook up the system. Soon enough, you learn that clicking
the mouse buttons while the pointer is over certain areas -- pulldown menus,
boxes, even words -- causes certain results.
     You can use the mouse in many ways from Amiga Basic.
     I'll start with a very simple program, one that can be used as a little
routine that pauses a program, then continues it when the user presses the
left mouse button. Here it is, between the dashed lines:
RUN "J:PROGRAMS/MOUSEGO"
--------------------------------------------

' MOUSEGO
CLS: CLEAR
PRINT "CLick left mouse button to continue."

pause:
IF MOUSE(0)=0 THEN pause

PRINT "The program continues . . ."

--------------------------------------------
     The MOUSE(0) value is used to test whether the left mouse button has
been pressed. If it hasn't been pressed, its value is 0. Any other value
indicates it has been pressed. So here, the program loops back to pause:
repeatedly until the left button is pressed. Then the program continues.
     This program, though simple, gets a bit ahead of the way this material
should be presented. Let's fall back and dig in.
     To start, the Amiga Basic screen can be thought of as the southeast
quadrant of a field marked by Cartesian coordinates. Every pixel position on
the screen has an "address" marked by two numbers: an across number for the
X axis and a down number for the Y axis. At extreme northwest is position
0,0. The across and down numbers vary, according to the resolution of the
screen. In this article, you will be working only in the default state
screen, which registers from 0 to 636 on the X axis, 0 to 188 on the Y axis.
Therefore, the southeast extreme is position 636,188, the northeast 636,0,
and the southwest 0,188.
     To bring some sense to this numerical hodgepodge, try the following
brief program. The dashed lines should NOT be typed in:
RUN "J:PROGRAMS/MOUSEWHERE"
--------------------------------

' Mousewhere
CLS: CLEAR

test:
LOCATE 1,1
a=MOUSE(0)
PRINT MOUSE(1);MOUSE(2)
GOTO test
END

-------------------------------
     Run the program above, then move the mouse pointer around the screen.
All the program does is print the current location of the mouse pointer by
its two-number addresss. Only purpose here is to acquaint you with the
positions.

     The little program above makes references to MOUSE(0), MOUSE(1) and
MOUSE(2). There are, in fact, also MOUSE(3) through MOUSE(6) values to be
gleaned and worked.
     Now for some specifics:
     MOUSE(0) can return six values, based on pressing the left mouse
button. Though the values won't mean much at this point, allow me to list
them for later reference:
     0 -- The left mouse button is not being currently pressed and hasn't
been pressed since the last MOUSE(0) value was checked.
     1 -- The left mouse is not being currently pressed but has been pressed
once since the last MOUSE(0) check.
     2 -- Left mouse button not being pressed but has been pressed twice
since the last check.
     -1 -- Left mouse button is being pressed after being clicked once.
     -2 -- Left mouse button being pressed after being clicked twice.
     -3 -- Left mouse button pressed after being clicked three times.

     Now let's cover the MOUSE(1) to MOUSE(6) meanings:
     MOUSE(1) -- Current X (across) position of mouse pointer.
     MOUSE(2) -- Current Y (down) position of mouse pointer.
     MOUSE(3) -- Starting X position.
     MOUSE(4) -- Starting Y position.
     MOUSE(5) -- Ending X position.
     MOUSE(6) -- Ending Y position.

     If you'll take a bit of time to ruminate about the various MOUSE clicks
and positions Amiga Basic can register, you'll begin to see that some
sophisticated actions can be programmed. A program can be told to perform
certain tasks, according to how many times the left mouse button was pressed
and where the pointer was when it was pressed.
     The following program listing makes use of various MOUSE values to draw
a line between a starting and ending point. It also introduces a new
concept. Near the start are the lines
ON MOUSE GOSUB test
MOUSE ON
     The lines set up the heart of the program, the loop: block. When the
left mouse button is pressed, the program GOSUBs to the test: block;
otherwise, it loops through loop:.
     In the listing, MOUSE values are given mnenomic variable names for the
current X and Y positions, starting X and Y, and ending X and Y. Within the
loop: block, the program draws a line between start and end positions when
it finds the correct MOUSE(0) (variable click) value. In running it,
position the mouse pointer, hold down the left mouse button and move the
mouse, release and click again. A line is drawn between start and end.
RUN "J:PROGRAMS/MOUSELINER"
--------------------------------------------------

' Mouseliner
CLS: CLEAR
ON MOUSE GOSUB test
MOUSE ON

loop:
click=MOUSE(0)
IF click=3 THEN LINE(startx,starty)-(endx,endy),3
GOTO loop

test:
curx=MOUSE(1)
cury=MOUSE(2)
startx=MOUSE(3)
starty=MOUSE(4)
endx=MOUSE(5)
endy=MOUSE(6)
RETURN

--------------------------------------------------
     The next listing, Mousedraw, is set up to draw a line between the most
recent X,Y position and the current X,Y position. It lets you draw an
unbroken line as long as the left mouse button is held down. I won't make an
exhaustive explanation. What is interesting is that the program always
focuses on the lines
loop:
GOTO loop
until the left mouse button is pressed. Then it goes to subroutines test:
and action: to perform its little trick.
RUN "J:PROGRAMS/MOUSEDRAW"
------------------------------------------

' Mousedraw
CLS:CLEAR
ON MOUSE GOSUB test
MOUSE ON

loop:
GOTO loop

test:
click=MOUSE(0)
curx=MOUSE(1)
cury=MOUSE(2)
startx=MOUSE(3)
starty=MOUSE(4)
endx=MOUSE(5)
endy=MOUSE(6)
LOCATE 1,1:PRINT curx;cury
IF click=0 THEN a=curx: b=cury: GOTO test
GOSUB action
GOTO test

action:
x=x+1
IF x=1 THEN RETURN
LINE(a,b)-(curx,cury),3
a=curx: b=cury
RETURN

-----------------------------------------
     The next example, Mousebox, shows a way to prompt the user to take
specified action by clicking the left mouse button with the pointer within a
box containing a description of the action. The setup of this program draws
two boxes and puts a message within each: QUIT or RUN AGAIN. Due to the
ON MOUSE GOSUB test
command, the program won't go to the test: routine unless the left mouse
button is pressed. Once there, it tests to see if the mouse pointer is
within either of the boxes. If not, it returns to loop:.
     The tests are simply made. The program checks to see if curx and cury
variable values fall within the areas of either box. See the IF-THEN tests
in the loop for the form of these tests.
RUN "J:PROGRAMS/MOUSEBOX"
-----------------------------------------

' MOUSEBOX
CLS: CLEAR
ON MOUSE GOSUB test
MOUSE ON

LOCATE 5,5
PRINT "Click within box to indicate choice."
LOCATE 10,20
PRINT "QUIT"
LOCATE 10,27
PRINT "RUN AGAIN"
LINE(147,70)-(187,80),3,b
LINE(205,70)-(282,80),3,b

loop:
IF curx>147 AND curx<187 AND cury>70 AND cury<80 THEN quit
IF curx>205 AND curx<282 AND cury>70 AND cury<80 THEN again
GOTO loop

test:
click=MOUSE(0)
curx=MOUSE(1)
cury=MOUSE(2)
RETURN

again:
LOCATE 14,30 
PRINT "All right, let's run it again."
FOR x=1 TO 4000
NEXT x
RUN

quit:
LOCATE 14,30
PRINT "I quit!"
END
---------------------------------------------------------

     You can disable the mouse with a MOUSE OFF command, then reactivate it
later in the program.
     MOUSE STOP, conversely, lets the mouse's event trapping continue, but
it suspends the ON MOUSE GOSUB statement until a MOUSE ON statement is
executed.
     Like many Basic events, the strengths of the MOUSE are such that they
can be applied to many uses: drawing, event trapping, making screen choices
based on clicks.
     As always, this column is for beginners. I hope this helps you make
some new beginnings with MOUSE events.

END OF TEXT

