' In its JUMPDISK appearance, this file is crunched. It is the source for
' a program that is presented in running form. To decrunch this file, see
' the Articles Menu item on DECRUNCHING.

' **********************************************************************

' MouseTrap

' MOUSE() Function Demo Program
'      by Tom Beale

' **********************************************************************

REM $OPTION Y+

WINDOW 1,"MouseTrap"

DEFINT a-w


M0$(0) = " Number of Clicks Since Last MOUSE(0) (Down)  "
M0$(1) = " Double-Click - Left Button Still Down        "
M0$(2) = " Button is Down - Mouse Being Moved           "
M0$(3) = " Left Button Not Pressed Since Last MOUSE(0)  "
M0$(4) = " Single Click Since Last Mouse(0)             "
M0$(5) = " Button Down and Double-Clicked since MOUSE(0)"
M0$(6) = " Number of Clicks Since Last MOUSE(0) (UP)    "

MV$(1) = "MOUSE(1) =      Current Mouse X Position"
MV$(2) = "MOUSE(2) =      Current Mouse Y Position"
MV$(3) = "MOUSE(3) =      Mouse X of Last Click (Before MOUSE(0))"
MV$(4) = "MOUSE(4) =      Mouse Y of Last Click (Before MOUSE(0))"
MV$(5) = "MOUSE(5) =      Mouse X When Button Was Released"
MV$(6) = "MOUSE(6) =      Mouse Y When Button Was Released"


DIM pat%(3),solid%(3)

pat%(0) = &hAAAA
pat%(1) = &h5555
pat%(2) = &hAAAA
pat%(3) = &h5555

solid%(0) = &hFFFF
solid%(1) = &hFFFF
solid%(2) = &hFFFF
solid%(3) = &hFFFF

GOSUB getbuttons

Buttons = 4
DIM gx1(buttons), gx2(buttons), gy1(buttons), gy2(buttons), gsl(buttons)

DIM b$(buttons,2)

gw = nosel%(0)
gh = nosel%(1) 

COMMON SHARED gx1(), gx2(), gy1(), gy2(), gsl(), mx, my, result

gx    = 40
start = 118
inc   = 16

x1 = 103    ' Corners of Black Box Around Mouse Output
y1 = 15
x2 = 617
y2 = 95

lm = 16    ' Left Margin of Mouse Output
tm = 4     ' Top Margin

  
FOR a = 0 TO buttons-1
    gx1(a+1) = gx
    gy1(a+1) = a*inc+start
NEXT

slp = 1

' ----------------------------------------- MOUSE ON/OFF
gx2(1) = gx1(1)+gw  ' Bottom left x
gy2(1) = gy1(1)+gh  ' Bottom left y
gsl(1) = 0          ' Selected (0 or 1)
b$(1,0)  = "MOUSE OFF - The automatic Mouse trapping is turned off."
b$(1,1)  = "MOUSE ON  - Clicking the mouse will auto-call MOUSE(0)."

' ----------------------------------------- MOUSE Checking
gx2(2) = gx1(2)+gw  ' Bottom left x
gy2(2) = gy1(2)+gh  ' Bottom left y
gsl(2) = 0          ' Selected (0 or 1)
b$(2,0)  = "MOUSE(0) is called as part of the main loop."
b$(2,1)  = "PRESS ANY KEY to call MOUSE(0).             "
' ----------------------------------------- Sleep ON/OFF
gx2(3) = gx1(3)+gw  ' Bottom left x
gy2(3) = gy1(3)+gh  ' Bottom left y
gsl(3) = 0          ' Selected (0 or 1)
b$(3,0)  = "LOOP - The main loop will run uninterrupted.             "
b$(3,1)  = "SLEEP - The main loop will sleep if there is no activity."
' ----------------------------------------- Sleep ON/OFF
gx2(4) = gx1(4)+gw  ' Bottom left x
gy2(4) = gy1(4)+gh  ' Bottom left y
gsl(4) = 0          ' Selected (0 or 1)
b$(4,0)  = "DO NOT TRAP MOUSE - Return to main loop if button is down.  "
b$(4,1)  = "TRAP THE MOUSE - Inside subroutine until button is released."


MOUSE ON
ON MOUSE GOSUB getmouse
MOUSE OFF

GOSUB drawmousescreen

DO ' ------------------------------------- Main Loop
   if gsl(2) <> 1  then GOSUB getmouse
   if inkey$ <> "" then gosub getmouse
   GOSUB showmouse 
   if gsl(3) then SLEEP
LOOP

' ---------------------------------------- Mouse Screen Setup

drawmousescreen:

    PUT (5,20), logo%,PSET
    
    COLOR ,,0
    PATTERN ,pat%
    LINE (x1-10,y1+10)-(x2-10,y2+10),2,bf
    
    COLOR ,,1
    PATTERN ,solid%
    LINE (x1,y1)-(x2,y2),2,bf
    LINE (x1,y1)-(x2,y2),1,b
        
    GOSUB drawbuttons
    COLOR 3,2
    FOR a = 1 TO 6
       LOCATE tm+1+a,lm
       PRINT MV$(a)
    NEXT
RETURN
   
getmouse:
    if gsl(4) = 0 then gosub noholdmouse
    if gsl(4) = 1 then gosub holdmouse
return

' --------------------------------- Hold Mouse Until Button is Released

holdmouse:
    while mouse(0)
        gosub noholdmouse
        gosub showmouse
    wend
return
   
' ---------------------------------------- Get Mouse Status

noholdmouse:
    M0 = MOUSE(0)
    if M0 <> 0 then
        mx = MOUSE(1)
        my = MOUSE(2)
        sel = 0
        for a = 1 to buttons
            checkgadg a
            if result = 1 then sel = a
        next
        if sel then gosub dogadget
    end if
RETURN

' ---------------------------------------- Deal With Gadget Selections
DoGadget:
    if gsl(sel) = 1 then
        gsl(sel) = 0
    else
        gsl(sel) = 1
    end if
    if gsl(1) = 0 then MOUSE OFF ELSE MOUSE ON
    gosub drawbuttons
    while MOUSE(0): WEND
RETURN

' ---------------------------------------- Display Mouse Function Values

showmouse:
    MOUSE STOP
    M1 = M0+3
    COLOR 3,2
    if M1 < 0 then M1 = 0
    if M1 > 5 THEN M1 = 6
    LOCATE tm,lm: PRINT "Mouse(0) = "; 
    COLOR 1,2
    PRINT M0; M0$(M1);
    FOR a = 1 TO 6
       LOCATE tm+1+a,lm+10
       PRINT MOUSE(a);" "
    NEXT
    IF gsl(1) = 1 THEN MOUSE ON
RETURN

' ---------------------------------------- Load Button Image Data

GetButtons:

    file$ = "Clips/MT1.clp"
    OPEN file$ FOR INPUT AS #1
    DIM nosel%(LOF(1)/2)
    CLOSE #1
    BLOAD file$, VARPTR(NoSel%(0))


    file$ = "Clips/MT2.clp"
    OPEN file$ FOR INPUT AS #1
    DIM sel%(LOF(1)/2)
    CLOSE #1
    BLOAD file$, VARPTR(Sel%(0))
    
    file$ = "Clips/Mouse.clp"
    OPEN file$ FOR INPUT AS #1
    DIM logo%(LOF(1)/2)
    CLOSE #1
    BLOAD file$, VARPTR(logo%(0))
    
RETURN
    
' ----------------------------------------- Draw Buttons
DrawButtons:
    COLOR 1,0
    FOR a = 1 TO buttons
        drawbutton a
        LOCATE gy2(a)/8-1,gx2(a)/8+2
        PRINT b$(a,gsl(a));
    NEXT
RETURN

' ----------------------------------------- Refresh Buttons
RefButtons:
    FOR a = 1 TO buttons
        drawbutton a        
    NEXT
RETURN
    
    
' ***************************************** Begin Subroutines

' ----------------------------------------- Draw Button

SUB DrawButton(a)
    IF gsl(a) = 0 THEN PUT (gx1(a),gy1(a)),NoSel%,PSET
    IF gsl(a) = 1 THEN PUT (gx1(a),gy1(a)),Sel%  ,PSET
END SUB

' ----------------------------------------- Is Button Down?
    
SUB CheckGadg(num)
    result = 1
    locate num + 17,20
    IF (mx>gx2(num)) OR (mx<gx1(num)) THEN result = 0
    IF (my>gy2(num)) OR (my<gy1(num)) THEN result = 0
END SUB
    
    
