TGC Codebase Backup



Card Index File by Coin

15th Mar 2005 18:46
Summary

Create your own set of tabbed index cards.



Description

This is a pair of functions; the first sets up set of tabbed index cards (such as that which appears when you edit your profile on this site) and the second function draws the index cards with your chosen card on top ready for you to draw or print on it. An included routine shows you how to monitor mouse activity to identify when a different tab has been selected.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    
SET DISPLAY MODE 1024,768,16

CLS RGB(100,100,200)

` Prepare the array which stores all the index card information
type IndexType
  x1 AS INTEGER
  y1 AS INTEGER
  x2 AS INTEGER
  y2 AS INTEGER
  Txt AS STRING
  Font AS STRING
  FontSize AS BYTE
  ThreeD AS BYTE
  TabHeight AS BYTE
  CurrentTab AS BYTE
  CardCol AS INTEGER
  TxtCol AS INTEGER
  ThreeDCol AS INTEGER
  HiCol AS INTEGER
  ShadCol AS INTEGER
  NoOfTabs AS INTEGER
endtype

` You may have more than one set of index cards identified by the 'Number' variable
Number=1
DIM IndexCardData(Number) AS IndexType

` At the beginning of the programme define how the tabs will appear
` See the TabSetup function for description of the parameters
TabSetup(1,200,100,800,500,"TAB 1|TAB 2|TAB 3|TAB 4|TAB 5","Arial",15,2,20,RGB(125,125,125),RGB(0,0,0),RGB(150,150,150),RGB(255,255,255),RGB(0,0,0))

` Later in the programme display the index cards
IndexCard(Number,1) : rem This time card 1 will be on top

` Show some hints
SET CURSOR 50,325
PRINT "Click the tabs."
SET CURSOR 50,340
PRINT "Press Esc to quit."

` This is the main loop which monitors the pointer and clicking

Number=1 : rem This variable must be set to the value which identifies the index card set you are checking
do
	if MOUSECLICK()=1
	  MX=MOUSEX()	
	  MY=MOUSEY()
` Check all tabs in turn
    CardWidth=(IndexCardData(Number).x2-IndexCardData(Number).x1)/IndexCardData(Number).NoOfTabs
	  for Tab=1 TO IndexCardData(Number).NoOfTabs
      TabStart=IndexCardData(Number).x1+(Tab-1)*CardWidth
` Was this tab clicked?      
      if MX>TabStart AND MX<TabStart+CardWidth AND MY>IndexCardData(Number).y1-IndexCardData(Number).TabHeight AND MY<IndexCardData(Number).y1
` This tab was clicked so redraw the index cards with this one on top
				IndexCard(Number,Tab)
				
` Place here the gosubs or functions which draw what you require on the topmost
` index card and any other code to be executed after the card has been drawn
` Eg
`       IF Tab=1
`         GOSUB Routine_That_Draws_What_Is_On_Index_Card_One
`       ENDIF
`       IF Tab=2
`         GOSUB Routine_That_Draws_What_Is_On_Index_Card_Two
`       ENDIF

      endif
	  next Tab
	endif
` Test for key press
  x$=INKEY$()
  if x$<>"" then EXIT
loop

END

` Sets up set of index cards # Number for use later
function TabSetup(Number,x1,y1,x2,y2,TabText$,Font$,FontSize,ThreeD,TabHeight,CardCol,TxtCol,ThreeDCol,HiCol,ShadCol)
` Number   = number allocated to this index card set
` x1, y1   = top left coordinates of the index cards
` x2, y2   = bottom right coordinates of the index cards
` TabText$     = word(s) that appear on the tabs separated with "|" character
` ThreeD   = appearance of TabText$ on the tabs:
`              normal if ThreeD=0
`              embossed if ThreeD=1
`              etched if ThreeD=2
` TabHeight= Height of all tabs
` CardCol  = index card colour
` TextCol  = Text$ colour
` ThreeDCol= ThreeD colour (used with embossing or etching)
` HiCol    = highlight colour (brighter edges of the index card)
` ShadCol  = shadow colour (also colour of 'shading' on inactive tab)
` NoOfTabs = No of tabs in this card set

` set (or reset) and store the index card set's data
  IndexCardData(Number).x1=x1
  IndexCardData(Number).y1=y1
  IndexCardData(Number).x2=x2
  IndexCardData(Number).y2=y2
  IndexCardData(Number).Txt=TabText$
  IndexCardData(Number).Font=Font$
  IndexCardData(Number).FontSize=FontSize
  IndexCardData(Number).ThreeD=ThreeD
  IndexCardData(Number).TabHeight=TabHeight
  IndexCardData(Number).CurrentTab=0 : rem default start value
  IndexCardData(Number).CardCol=CardCol
  IndexCardData(Number).TxtCol=TxtCol
  IndexCardData(Number).ThreeDCol=ThreeDCol
  IndexCardData(Number).HiCol=HiCol
  IndexCardData(Number).ShadCol=ShadCol
  NoOfTabs=0
  for Pointer=1 TO LEN(TabText$)
   	if MID$(TabText$,Pointer)="|"then INC NoOfTabs
  next Pointer
  INC NoOfTabs
  IndexCardData(Number).NoOfTabs=NoOfTabs
endfunction

` Displays index cards or changes existing set's appearance
function IndexCard(Number,CurrentTab)
` Number     = Number allocated to this index card set
` CurrentTab = Number of the card to be drawn on top of the others

` Ensure that the tab's new state is different to its existing state
` and that it is therefore due to be drawn

` Ensure that the chosen card is not already on top
  if IndexCardData(Number).CurrentTab<>CurrentTab
` Don't allow a zero value  	
    if CurrentTab=0 then CurrentTab=Number    
`	Calculate card width
		CardWidth=IndexCardData(Number).x2-IndexCardData(Number).x1+1
` Calculate tab width
		TabWidth=CardWidth/IndexCardData(Number).NoOfTabs
` Draw the basic card
		INK IndexCardData(Number).CardCol,0
		BOX IndexCardData(Number).x1,IndexCardData(Number).y1,IndexCardData(Number).x2,IndexCardData(Number).y2
` Draw card set's shadowed edges
		INK IndexCardData(Number).ShadCol,0
		LINE IndexCardData(Number).x1,IndexCardData(Number).y2,IndexCardData(Number).x2,IndexCardData(Number).y2
		LINE IndexCardData(Number).x2,IndexCardData(Number).y1,IndexCardData(Number).x2,IndexCardData(Number).y2
` Draw card set's highlighted edges
		INK IndexCardData(Number).HiCol,0
		LINE IndexCardData(Number).x1,IndexCardData(Number).y1,IndexCardData(Number).x1,IndexCardData(Number).y2
` Calculate position of top edge of tabs
		TopEdge=IndexCardData(Number).y1-IndexCardData(Number).TabHeight
` Calculate position of the first card's tab's front edge
		TabStart=IndexCardData(Number).x1
` Calculate position of the first card's tab's back edge
		TabEnd=IndexCardData(Number).x1+TabWidth-1
		TextPointer=1
    for T=1 TO IndexCardData(Number).NoOfTabs
      if T=IndexCardData(Number).NoOfTabs
` Adjust last card's tab's back edge if necessary to keep it in line with right hand edge of card
      	TabEnd=IndexCardData(Number).x2
      endif
` Draw next tab's highlighted edges
		  INK IndexCardData(Number).HiCol,0
    	LINE TabStart,IndexCardData(Number).y1,TabStart,TopEdge+3
    	LINE TabStart,TopEdge+3,TabStart+3,TopEdge
    	LINE TabStart+3,TopEdge,TabEnd-3,TopEdge
    	if T<>CurrentTab then LINE TabStart,IndexCardData(Number).y1,TabEnd,IndexCardData(Number).y1
` Draw tab's shadowed edges    	
 	    INK IndexCardData(Number).ShadCol,0
    	LINE TabEnd-3,TopEdge,TabEnd,TopEdge+3
      LINE TabEnd,TopEdge+3,TabEnd,IndexCardData(Number).y1 	
` Flood fill the tab
      INK IndexCardData(Number).CardCol,0
      P1=TabStart+1
      P2=P1+TabWidth-2
      PT=POINT(P1,L)
      for L=IndexCardData(Number).y1-1 TO TopEdge+1 STEP -1
      	if POINT(P1,L)<>PT
      		INC P1
      		DEC P2
      	endif
      	LINE P1,L,P2,L
      next L    
` Prepare for the text
	    SET TEXT TRANSPARENT
  	  SET TEXT FONT IndexCardData(Number).Font
    	SET TEXT SIZE IndexCardData(Number).FontSize
	    FontHeight=IndexCardData(Number).FontSize
	    TabText$=IndexCardData(Number).Txt
` The variable where the tab's text will be stored temporarily	    
	    T$=""
` Seek out the relevant text for this tab
	    for Pointer=TextPointer TO LEN(TabText$)   
	    	if MID$(TabText$,Pointer)<>"|"
	    		T$=T$+MID$(TabText$,Pointer)
	    	else
	    		INC TextPointer
	    		Pointer=LEN(TabText$)+1
	    	endif
	    next Pointer
	    INC TextPointer,LEN(T$)
  	  Width=TEXT WIDTH(T$)
` Find the tab's horizontal mid point
	    MidTabX=TabStart+TabWidth/2
` Find the tab's vertical mid point - may need adjustment for different fonts
  	  MidTabY=TopEdge+TabHeight/2
` Is the text embossed?
    	if IndexCardData(Number).ThreeD=1
      	INK IndexCardData(Number).ThreeDCol,0
` Draw the 3D effect for the text
  	    TEXT MidTabX-Width/2-1,MidTabY+FontHeight/2-4,T$
  	  endif
` Is the text etched?
 		  if IndexCardData(Number).ThreeD=2
    	  INK IndexCardData(Number).ThreeDCol,0
` Draw the 3D effect for the text
      	TEXT MidTabX-Width/2+1,MidTabY+FontHeight/2-4,T$
 	    endif
   	  INK IndexCardData(Number).TxtCol,0
` Draw the text to overlap the 3D effect
 	    TEXT MidTabX-Width/2,MidTabY+FontHeight/2-4,T$
` Store the number of the index card that is now on top
      IndexCardData(Number).CurrentTab=CurrentTab
` Adjust pointers for next tab
      INC TabStart,TabWidth
    	INC TabEnd,TabWidth  
  	next T
  endif      
endfunction