Vga pull down menus 1/2

 BBS: Inland Empire Archive
Date: 06-02-92 (23:33)             Number: 172
From: MATT PRITCHARD @ 930/21      Refer#: NONE
  To: MIKE THAYER                   Recvd: NO  
Subj: Vga pull down menus   1/2      Conf: (2) Quik_Bas
MT>        I'm writing an Icon based program for EGA/VGA. In order to get
MT>my mouse to work with it I've been forced to write something like the
MT>following:

MT>        (Given: X1Cord, Y1Cord represent the upper left corner and
MT>        X2Cord, Y2Cord represent the lower right corner and last but
MT>        not least MouseValx, MouseValy represent the coordinates
MT>        returned by the mouse.)

MT>IF MouseValx>=X1Cord and MouseValx<=X2Cord and MouseValy>=Y1Cord and
MT>MouseValy<=Y1Cord THEN CALL SomeRoutine (MouseValx, MouseValy)

   Oh lordy; you are about where I was 2 years ago...

MT>        The subroutine further divides the box defined in the above
MT>        IF THEN statement into Icon sized blocks which in turn call
MT>        the intended routine.

       Aiiiiiiiiiiieeeeeeeeee!!!!!!

MT>        There has got to be an easier way! The way I'm doing it, it
MT>        sucks up a lot of room! Is there a way to use arrays in doing
MT>        this, or am I totally off base in my concept with this method?

yes, yes, yes,  use arrays, and generic routines......

(Dropping into improvizational mode)

Make a type:


TYPE MouseRegion
    Left    AS INTEGER
    Top     AS INTEGER
    Right   AS INTEGER
    Bottom  AS INTEGER
    RegionType  AS INTEGER
    RegionItem  AS INTEGER
    Used        AS INTEGER
END TYPE

The RegionType is used to identify the type of object in
the region; I.e a Button, an icon, a text String, etc...

The RegionItem is used to tell 2 or more items of the same type apart.

Now for some constants:

CONST  r.Icon = 1, r.Button = 2, r.TextWindow = 3, etc...
CONST  True = -1, False = 0 , MaxRegions = 100, MaxIcons =
8 CONST  IconWidth = 16, IconHeight = 16

and make an array:

DIM MouseRegions(MaxRegions) AS MouseRegion COMMON SHARED MouseRegions()

Now, whenever you draw anything that can be clicked upon,
make a routine to add it to the array:

  ' Draw all of the icons on the screen

  FOR X = 1 TO MaxIcons
     Xpos = XBase = X * IconWidth
     Ypos = YBase
     CALL DRAW.ICON ( GraphicImage(X), Xpos, Ypos )
     CALL ADD.MOUSE.REGION (r.Icon, X, Xpos, Ypos, Xpos + IconWidth -1,_
                           Ypos + IconHeight -1)
  NEXT X

  ....

  'Add a global mouse region

SUB ADD.MOUSE.REGION( rType, rItem, Top, Left, Right, Bottom ) STATIC

   Region = 0
   FOR X = 1 TO MaxRegions
       IF MouseRegions(X).Used = False THEN Region = X: EXIT FOR
   NEXT X

   IF NOT Region THEN STOP    ' Error, out of free regions

   MouseRegion(Region).RegionType = rType
   MouseRegion(Region).RegionItem = rItem
   MouseRegion(Region).Top = Top
   MouseRegion(Region).Left = Left
   MouseRegion(Region).Right = Right
   MouseRegion(Region).Bottom = Bottom
   MouseRegion(Region).Used = True

END SUB


Now... In your main Program, you have an event loop.



GET.NEXT.EVENT:
     xKey$=INKEY$
     IF LEN(xKey$) THEN GOTO PROCESS.KEYSTROKE

     CALL GET.MOUSE.COORS (MouseX, MouseY, MouseButton)
     IF MouseButton THEN
         CALL WHAT.REGION.IS.MOUSE.IN ( MouseX, MouseY, rType, rItem )
         IF rType = r.Icon THEN GOTO ICON.EVENT
         IF rType = r.Button THEN GOTO BUTTON.EVENT
         ...
     END IF

     GOTO GET.NEXT.EVENT
     .....

     'The Mouse event has been isolated down to clicking on an icon...

ICON.EVENT:
     IconNumPressed = rItem
     CALL PRINT.MESSAGE ("You pressed Icon # ", IconNumPressed)
     IF IconNumPressed = IconDoThis THEN ........
     ...
     ...

     'Figgure out what the mouse clicked on...

SUB WHAT.REGION.IS.MOUSE.IN (MouseX, MouseY, rType, rItem) STATIC

    rType = 0 : rItem = 0

    FOR X = 1 TO MaxRegions
        IF MouseRegion(X).Used THEN
            IF (MouseX >= MouseRegion(X).Left) AND _
               (MouseX <= MouseRegion(X).Right) THEN
                 IF (MouseY >= MouseRegion(X).Top) AND _
                    (MouseY <= MouseRegion(X).Bottom) THEN
                       rType = MouseRegion(X).RegionType
                       rItem = MouseRegion(X).RegionItem
                       EDIT FOR
                 END IF
             END IF
         END IF
    NEXT X

END SUB

>>> Continued to next message
===
 * SLMR 2.1a * Corporations don't care about you

--- InterPCB 1.50
 # Origin: CENTRAL BBS -Texas' BEST BBS! 214-393-7090 HST  3+GIGs (8:930/21)
 * Origin: Gateway System to/from RBBS-NET (RBBS-PC 1:10/8)
Outer Court
Echo Basic Postings

Books at Amazon:

Back to BASIC: The History, Corruption, and Future of the Language

Hackers: Heroes of the Computer Revolution (including Tiny BASIC)

Go to: The Story of the Math Majors, Bridge Players, Engineers, Chess Wizards, Scientists and Iconoclasts who were the Hero Programmers of the Software Revolution

The Advent of the Algorithm: The Idea that Rules the World

Moths in the Machine: The Power and Perils of Programming

Mastering Visual Basic .NET