'===========================================================================
' Subject: COMPLETE MOUSE LIBRARY FOR QB      Date: 10-02-99 (21:10)       
'  Author: Aaron Severn                       Code: QB, PDS                
'  Origin: rlsevern@idirect.com             Packet: MOUSE.ABC
'===========================================================================
'****************************************************************************
' MOUSE.BAS
' Programmed by Aaron Severn (bob_severn@msn.com)
' Nov. 4/97
'
' This is a more complete library of mouse functions than others I've seen
' for QB.  These functions were designed specifically for use with mode 13
' and SVGA modes.  Since they do not use the DEF SEG command it is still
' possible to draw graphics very quickly with POKE in the screen modes
' mensioned without having to repeatedly use DEF SEG thus slowing the
' program down tremendously.  This program does use CALL INTERRUPT so it
' requires QuickBasic with the QB.QLB library loaded.
'
' This code is freeware, do what you want to it.  If you use any of this or
' code based on this in your own programs, give me the credit I'm due, that's
' not too much to ask, right.  If, however, by some mysterious occurence,
' this code should crash your computer, or blow it up and then you get burnt,
' or something like that, I'm not responsible.  With that understood, here's
' a list of functions and how to use them.
'
'
' ResetMouse            - Details - Resets the mouse to it's defaults, should
'                                   be called before using the mouse.
'                       - Returns - -1 if successful, 0 if failed.
'
' ShowMouse             - Details - Makes the mouse pointer visible.
'
' HideMouse             - Details - Makes the mouse pointer invisible.
'
' GetMouse              - Details - Gets the status of the mouse buttons and
'                                   the location of the mouse.
'                       - Params  - lB: status of the left mouse button, 1 =
'                                   pushed, 0 = released; rB: same as lB but
'                                   for the right mouse button; mB: same as
'                                   lB but for the middle mouse button (if
'                                   the mouse has a middle button); xPos: the
'                                   x coords of the mouse; yPos: the y coords
'                                   of the mouse.
'
' SetMouseRange         - Details - Sets a square defined by startX, startY,
'                                   endX, and endY that the mouse can move
'                                   within.  The mouse pointer will not leave
'                                   the boundaries of this box.
'                       - Params  - startX and startY: the upper left coords
'                                   of the box; endX and endY: the lower right
'                                   coords of the box.
'
' MoveMouse             - Details - Moves the mouse pointer to the given
'                                   location.
'                       - Params  - newXPos and newYPos: the new x and y
'                                   coords of the mouse pointer.
'
' SetSensitivity        - Details - Sets how fast the mouse moves on the
'                                   screen.
'                       - Params  - xRatio: the horizontal speed of the mouse;
'                                   yRatio: the vertical speed of the mouse.
'                                   NOTE: mouse moves faster as these values
'                                   are decreased, slower as they are
'                                   increased.
'****************************************************************************

DEFINT A-Z

DECLARE SUB GetMouse (lB, rB, mB, xPos, yPos)
DECLARE SUB HideMouse ()
DECLARE SUB MoveMouse (newXPos, newYPos)
DECLARE FUNCTION ResetMouse ()
DECLARE SUB SetMouseRange (startX, startY, endX, endY)
DECLARE SUB SetSensitivity (xRatio, yRatio)
DECLARE SUB ShowMouse ()

TYPE RegType                            'DOS Registers for BIOS calls.
    AX AS INTEGER
    BX AS INTEGER
    CX AS INTEGER
    DX AS INTEGER
    BP AS INTEGER
    SI AS INTEGER
    DI AS INTEGER
    FLAGS AS INTEGER
    DS AS INTEGER
    ES AS INTEGER
END TYPE

' These constants define the box which the mouse will be limited to in this
' demo.  Feel free to play around with the numbers.
CONST MINX = 60, MINY = 60, MAXX = 580, MAXY = 420

'$DYNAMIC

DIM SHARED Regs AS RegType

SCREEN 12
CLS

xSensitivity = 8                        'These are the default values for
ySensitivity = 16                       'mouse sensitivity.

IF ResetMouse = 0 THEN END              'Reset the mouse.
ShowMouse                               'Show the mouse.

SetMouseRange MINX, MINY, MAXX, MAXY    'Limit the mouse to this box.

PRINT "Left button status:"
PRINT "Right button status:"
PRINT "Middle button status:"
PRINT "X location:"
PRINT "Y location:"
PRINT "X sensitivity:"
PRINT "Y sensitivity:"
PRINT "Minimum X:"; MINX
PRINT "Minimum Y:"; MINY
PRINT "Maximum X:"; MAXX
PRINT "Maximum Y:"; MAXY

DO
  GetMouse lB, rB, mB, xPos, yPos       'Update the mouse.
 
  a$ = INKEY$
  DO: LOOP UNTIL INKEY$ = ""

  SELECT CASE a$
         CASE CHR$(0) + CHR$(72)
             xSensitivity = (xSensitivity + 1) MOD 41
         CASE CHR$(0) + CHR$(75)
             ySensitivity = (ySensitivity + 80) MOD 82
         CASE CHR$(0) + CHR$(77)
             ySensitivity = (ySensitivity + 2) MOD 82
         CASE CHR$(0) + CHR$(80)
             xSensitivity = (xSensitivity + 40) MOD 41
  END SELECT

  ' Adjust sensitivity if necessary.
  IF a$ <> "" THEN SetSensitivity xSensitivity, ySensitivity

  LOCATE 1, 21: PRINT lB
  LOCATE 2, 22: PRINT rB
  LOCATE 3, 23: PRINT mB
  LOCATE 4, 13: PRINT xPos
  LOCATE 5, 13: PRINT yPos
  LOCATE 6, 16: PRINT xSensitivity
  LOCATE 7, 16: PRINT ySensitivity
LOOP UNTIL a$ = CHR$(27)

HideMouse                               'Hide the mouse.


REM $STATIC
' Returns button status and location of the mouse.
SUB GetMouse (lB, rB, mB, xPos, yPos)

Regs.AX = 3
CALL interrupt(&H33, Regs, Regs)

lB = Regs.BX AND 1
rB = (Regs.BX AND 2) \ 2
mB = (Regs.BX AND 4) \ 4
xPos = Regs.DX
yPos = Regs.CX

END SUB

' Hides the mouse.
SUB HideMouse

Regs.AX = 2
CALL interrupt(&H33, Regs, Regs)

END SUB

' Moves the mouse to a new location.
SUB MoveMouse (newXPos, newYPos)

Regs.AX = 4
Regs.CX = newYPos
Regs.DX = newXPos
CALL interrupt(&H33, Regs, Regs)

END SUB

' Resets the mouse.
FUNCTION ResetMouse

Regs.AX = 0
CALL interrupt(&H33, Regs, Regs)

IF Regs.AX = 0 THEN ResetMouse = 0 ELSE ResetMouse = 1

END FUNCTION

' Defines a box that the mouse can move in.
SUB SetMouseRange (startX, startY, endX, endY)

Regs.AX = 7
Regs.CX = startX
Regs.DX = endX
CALL interrupt(&H33, Regs, Regs)

Regs.AX = 8
Regs.CX = startY
Regs.DX = endY
CALL interrupt(&H33, Regs, Regs)

END SUB

' Sets the sensitivity of the mouse (how fast it moves).
SUB SetSensitivity (xRatio, yRatio)

Regs.AX = 15
Regs.CX = xRatio
Regs.DX = yRatio
CALL interrupt(&H33, Regs, Regs)

END SUB

' Shows the mouse.
SUB ShowMouse

Regs.AX = 1
CALL interrupt(&H33, Regs, Regs)

END SUB
