' "GadgetSubs"
'
'    The following subprograms were written for
'           The AmigaWorld TechJournal
'              by  Bryan D. Catley.
'        Copyright (c) 1990 by FelineSystems
'              All Rights Reserved
'
' This subprogram reads DATA statements and builds arrays
' containing information about the gadgets.  The DATA statements
' should be of the form:
'   DATA x,y,l,h,lp,mp,dp,"text"
'      x      -upper left corner x coordinate
'      y      -upper left corner y coordinate
'      l      -length of gadget in pixels
'      h      -height of gadget in pixels
'      lp     -number of palette containing light shade
'      mp     -number of palette containing medium shade
'      dp     -number of palette containing dark shade
'      "text" -any text which is to appear within the gadget
' Input parameter:
'      NumGadgets  -total number of gadgets; equal to the number
'                   of entries in the arrays.
' 
SUB StoreGadgetInfo (NumGdgts) STATIC
SHARED GdgtDef1(),GdgtDef2$()
FOR n=0 TO NumGdgts-1
  FOR M=0 TO 6
    READ GdgtDef1(n,M)
  NEXT
  READ GdgtDef2$(n)
NEXT
END SUB

' Draw the specified range of gadgets.
' Input parameters:
'   Gstart%  -the number of the entry in the arrays containing
'             information about the first in the range of gadgets
'             to be drawn. 
'   Gend%    -the number of the entry in the arrays containing
'             information about the last in the range of gadgets
'             to be drawn.
' 
SUB DisplayGadgets (Gstart%,Gend%) STATIC
SHARED GdgtDef1(),GdgtDef2$()
FOR n=Gstart% TO Gend%
  x=GdgtDef1(n,0):y=GdgtDef1(n,1)
  ln=GdgtDef1(n,2):ht=GdgtDef1(n,3)
  L=GdgtDef1(n,4):M=GdgtDef1(n,5):D=GdgtDef1(n,6)
  LINE(x,y)-STEP(ln,ht),M,bf
  LINE(x-4,y-2)-STEP(3,ht+4),L,bf
  LINE(x+ln,y-2)-STEP(3,ht+3),D,bf
  LINE(x,y-2)-STEP(ln+3,0),L
  LINE(x,y-1)-STEP(ln+1,0),L
  LINE(x-1,y+ht+1)-STEP(ln+2,0),D
  LINE(x-3,y+ht+2)-STEP(ln+6,0),D
  x&=x:y&=y+6:COLOR D,M
  CALL Move&(WINDOW(8),x&,y&)
  CALL Text&(WINDOW(8),SADD(GdgtDef2$(n)),LEN(GdgtDef2$(n)))
NEXT
END SUB

' Determine which of a specified range of gadgets was selected.
' Input parameters:
'   Gstart%  -the number of the entry in the arrays containing
'             information about the first in the range of gadgets
'             to be checked. 
'   Gend%    -the number of the entry in the arrays containing
'             information about the last in the range of gadgets
'             to be checked.
'   selected -the number of the gadget selected, relative to
'             Gstart%, will be returned in this parameter.  Zero
'             indicates that no gadget was selected.
' The shared variables MouseX%, MouseY%, and MousePress are for
' the benefit of the caller and other subprograms.
' 
SUB CheckOnGadgets (Gstart%,Gend%,selected) STATIC
SHARED GdgtDef1(),MouseX%,MouseY%,MousePress
WHILE MOUSE(0)=0:WEND
mx=MOUSE(1):my=MOUSE(2)
MouseX%=mx:MouseY%=my:MousePress=0:selected=0
FOR n=Gstart% TO Gend%
  IF mx>GdgtDef1(n,0) AND mx<GdgtDef1(n,0)+GdgtDef1(n,2) THEN
    IF my>GdgtDef1(n,1) AND my<GdgtDef1(n,1)+GdgtDef1(n,3) THEN
      x=GdgtDef1(n,0):y=GdgtDef1(n,1)
      ln=GdgtDef1(n,2):ht=GdgtDef1(n,3)
      L=GdgtDef1(n,4):M=GdgtDef1(n,5):D=GdgtDef1(n,6)     
      LINE(x-4,y-2)-STEP(3,ht+4),D,bf
      LINE(x+ln,y-2)-STEP(3,ht+3),L,bf
      LINE(x,y-2)-STEP(ln+3,0),D
      LINE(x,y-1)-STEP(ln+1,0),D
      LINE(x-1,y+ht+1)-STEP(ln+2,0),L
      LINE(x-3,y+ht+2)-STEP(ln+6,0),L
      selected=n-Gstart%+1:n=Gend%:MousePress=1
    END IF
  END IF
NEXT
WHILE MOUSE(0)<>0:WEND
IF selected>0 THEN
  LINE(x-4,y-2)-STEP(3,ht+4),L,bf
  LINE(x+ln,y-2)-STEP(3,ht+3),D,bf
  LINE(x,y-2)-STEP(ln+3,0),L
  LINE(x,y-1)-STEP(ln+1,0),L
  LINE(x-1,y+ht+1)-STEP(ln+2,0),D
  LINE(x-3,y+ht+2)-STEP(ln+6,0),D
END IF        
END SUB

