REM ***********************************************
REM Title: Menus
REM Author: HeavyAmp
REM Downloaded from: http://dbcc.zimnox.com/
REM ***********************************************
 
REM =======================
REM written by: HeavyAmp
REM =======================
 
`Set up Game Requirements
Set Display Mode 1280,1024,32
Sync On `: Sync Rate 60
 
#Constant True  1
#Constant False 0
 
Type Window
   Name        as String  `Window Name
   X           as Integer `X cord of Window
   Y           as Integer `Y Cord of Window
   Width       as Integer `Width of the Window
   Height      as Integer `Height of the Window Body
   Hidden      as Boolean `Is the Window Hidden
   Minimized   as Boolean `Is the Window Minimized
   Bar         as Integer `Sprite/Image Number for Window Bar
   Body        as Integer `Sprite/Image Number for Window Body
   CloseButton as Integer `Sprite/Image Number for Close Button
   MinButton   as Integer `Sprite/Image Number for Minimize Button
   LockButton  as Integer `Sprite/Image Number for Lock Button
   Locked      as Boolean `Stores wether the Window is locked/UnLocked
   Dynamic     as Boolean `Sets if the Window is able to be Moved etc by the Player
EndType : Dim Window(-1) as Window
 
`Make Sphere
AutoCam On
Make Object Sphere 1,5
 
CreateWindow("Techniques",400,600,0,0, True)
CreateWindow("Items",300,500,0,0, True)
CreateWindow("Character Profile",500,300,400,400, True)
CreateWindow("Map",500,300,0,800,True)
CreateWindow("Attacks",Screen Width(),200,0,800,False)
 
 
Do
   UpdateMouse()
   UpdateWindows()
   Set Cursor 0,0 : Print "FPS:",Screen FPS()
   Sync
Loop
 
Function CreateWindow(WindowName as String, Width as Integer, Height as Integer, X as Integer , Y as Integer , Dynamic as Boolean)
 
   `Create a New Window in the Array
   Array Insert at Bottom Window()
   aWin as Integer : aWin=Array Count(Window())
 
   `Initialize Window Variables
   Window(aWin).Name=WindowName
   Window(aWin).Hidden=0 : Window(aWin).Minimized=0
   Window(aWin).X=X : Window(aWin).Y=Y
   Window(aWin).Height=Height : Window(aWin).Width=Width
   Window(aWin).Dynamic=Dynamic
 
   `Get Image Numbers For Parts of our Window
   Window(aWin).Bar=GetIDNumber()   : Window(aWin).Body=GetIDNumber()
   Window(aWin).CloseButton=GetIDNumber() : Window(aWin).MinButton=GetIDNumber()
   Window(aWin).LockButton=GetIDNumber()
 
   `Draw Our Window
   Create Bitmap 1, Screen Width(), Screen Height()
   Set Current Bitmap 1
   Set Text Font "Copperplate Gothic Light" : Set Text to Bold : Set Text Size 20
 
      Ink RGB(2,2,2),1        : Box 0,0,Width,Height+30
      Ink RGB(128,128,128),1  : Box 1,30+2,Width-1,Height+30-1
      Ink RGB(1,1,1),1        : Box 2,30+3,Width-2,Height+30-2
 
      Ink RGB(128,128,128),1  : Box 1,1,Width-1,30
      Ink RGB(2,2,2),1        : Box 2,2,Width-2,30-1
 
      Ink RGB(128,128,128),1  : Box (Width-67),6,(Width-51),20
      Ink RGB(2,2,2),1        : Box (Width-66),7,(Width-52),19
      Ink RGB(128,128,128),1  : Box (Width-47),6,(Width-31),20
      Ink RGB(2,2,2),1        : Box (Width-46),7,(Width-32),19
      Ink RGB(128,128,128),1  : Box (Width-27),6,(Width-11),20
      Ink RGB(2,2,2),1        : Box (Width-26),7,(Width-12),19
      Set Text Font "Copperplate Gothic Light" : Set Text to Bold : Set Text Size 12
      Ink RGB(156,156,156),1 : Center Text (Width-20),6,"x"
      Ink RGB(156,156,156),1 : Center Text (Width-39),6,"v"
 
      circle (Width-60),11,2 : Box (Width-63),12,(Width-56),17
 
      Set Text Font "Copperplate Gothic Light" : Set Text to Bold : Set Text Size 14
      Ink RGB(156,156,156),1 : Center Text (Width/2),6, WindowName
 
      `Convert The Parts of our Windows to Sprites
      Get Image Window(aWin).Body,0,30,Width,Height+30,1
      SetColorAlpha(Window(aWin).Body,"1,1,1",150)
      Sprite Window(aWin).Body,X,Y+27,Window(aWin).Body
 
         Get Image Window(aWin).Bar,0,0,Width,30,1
         Sprite Window(aWin).Bar,X,Y,Window(aWin).Bar
 
         Get Image Window(aWin).LockButton, (Width-67),6,(Width-51),20,1
         Sprite Window(aWin).LockButton,X+(Width-67),Y+6,Window(aWin).LockButton
 
         Get Image Window(aWin).MinButton,(Width-47),6,(Width-31),20,1
         Sprite Window(aWin).MinButton,X+(Width-47),Y+6,Window(aWin).MinButton
 
         Get Image Window(aWin).CloseButton,(Width-27),6,(Width-11),20,1
         Sprite Window(aWin).CloseButton,X+(Width-27),Y+6,Window(aWin).CloseButton
 
   Set Current Bitmap 0
 
   If Window(aWin).Dynamic=0
 
      Hide Sprite Window(aWin).Bar
      Hide Sprite Window(aWin).MinButton
      Hide Sprite Window(aWin).CloseButton
      Hide Sprite Window(aWin).LockButton
 
   EndIf
 
EndFunction
 
 
Function UpdateWindows()
 
      `Change Window Priority If New WindowBar Is Pressed
      For aWin=Array Count(Window()) to 0 step -1
         If Sprite Collision(Window(aWin).Bar,SPMouse)=1 and ButtonState="LeftPressed" and Window(aWin).Dynamic=1
            For bWin=aWin to Array Count(Window())
               If aWin<>bWin
                  If Sprite Collision(Window(bWin).Body,SPMouse)=1  and Sprite Visible(Window(bWin).Body)=1
                        goto ExitLoop
                  EndIF
               EndIf
            Next bWin
            SetWindowtoTop(aWin) : goto ExitLoop
         EndIf
      Next aWin
      ExitLoop:
 
   `Scroll Through Windows using Tab Key
   if KeyState(15) = 1 then CurrentTab=1
   if KeyState(15) = 0 then CurrentTab=0
   Set Cursor 0,20 : print KeyState(15)
   If LastTab=1 and CurrentTab=0 then SetWindowtoTop(0)
   LastTab=CurrentTab
 
 
   `Get the Top Window
   Local TopWindow as Integer : TopWindow = Array Count(Window())
 
      If Window(TopWindow).Dynamic=1
 
      `Move Top WindowBar when Dragged
      If Sprite Collision(Window(TopWindow).Bar,SPMouse)=1 and Window(TopWindow).Locked = 0
 
         If ButtonState="LeftPressed"
 
            Global DistanceX as Integer : DistanceX=Window(TopWindow).X-Mousex()
            Global DistanceY as Integer : DistanceY=Window(TopWindow).Y-Mousey()
 
         EndIf
 
         If ButtonState="LeftHeld"
 
            Window(TopWindow).X = Mousex()+ DistanceX
            Window(TopWindow).Y = Mousey()+ DistanceY
 
         EndIf
 
         `Keep Window Bar On Screen
         If Window(TopWindow).X<0 then Window(TopWindow).X=0
         If Window(TopWindow).Y<0 then Window(TopWindow).Y=0
         If Window(TopWindow).X+Window(TopWindow).Width >Screen Width() then Window(TopWindow).X=Screen Width()-Window(TopWindow).Width
         If Window(TopWindow).Y+30>Screen Height() then Window(TopWindow).Y=Screen Height()-30
 
         `Update Window Sprites
         Sprite Window(TopWindow).Bar,Window(TopWindow).X,Window(TopWindow).Y,Window(TopWindow).Bar
         Sprite Window(TopWindow).Body,Window(TopWindow).X,Window(TopWindow).Y+27,Window(TopWindow).Body
         Sprite Window(TopWindow).MinButton,Window(TopWindow).X+(Window(TopWindow).Width-47),Window(TopWindow).Y+6,Window(TopWindow).MinButton
         Sprite Window(TopWindow).CloseButton,Window(TopWindow).X+(Window(TopWindow).Width-27),Window(TopWindow).Y+6,Window(TopWindow).CloseButton
         Sprite Window(TopWindow).LockButton,Window(TopWindow).X+(Window(TopWindow).Width-67),Window(TopWindow).Y+6,Window(TopWindow).LockButton
      EndIf
 
      If Window(TopWindow).Locked=0
         `Detect the Minimize Button
         If Sprite Collision(Window(TopWindow).MinButton,SPMouse) and ButtonState="LeftPressed"
            If Sprite Visible(Window(TopWindow).Body)=1
               Hide Sprite Window(TopWindow).Body
            Else
               Show Sprite Window(TopWindow).Body
            EndIf
         EndIf
 
         `Detect the Close Button
         If Sprite Collision(Window(TopWindow).CloseButton,SPMouse) and ButtonState="LeftPressed"
            Hide Sprite Window(TopWindow).Bar
            Hide Sprite Window(TopWindow).Body
            Hide Sprite Window(TopWindow).MinButton
            Hide Sprite Window(TopWindow).CloseButton
            Hide Sprite Window(TopWindow).LockButton
         EndIf
 
      EndIf
 
      `Detect the lock Button
      If Sprite Collision(Window(TopWindow).LockButton,SPMouse) and ButtonState="LeftPressed"
         If Window(TopWindow).Locked=1
            Window(TopWindow).Locked=0
            DistanceX=Window(TopWindow).X-Mousex()
            DistanceY=Window(TopWindow).Y-Mousey()
            Set Sprite Diffuse Window(TopWindow).LockButton, 255,255,255
         Else
            Window(TopWindow).Locked=1
            Set Sprite Diffuse Window(TopWindow).LockButton, 255, 0, 0
         EndIf
      EndIf
      EndIf
 
EndFunction
 
 
Function SetWindowtoTop(aWindow as Integer)
 
   `Change Old Selected Window to Grey
   Set Sprite Diffuse Window(Array Count(Window())).Bar, 255,255, 255
   Set Sprite Diffuse Window(Array Count(Window())).Body, 255,255, 255
 
   Local TempWin as Window
   TempWin=Window(aWindow)
   Array Delete Element Window(),aWindow
   Array Insert At Bottom Window()
   BottomWin=Array Count(Window())
   Window(BottomWin)=TempWin
 
   `Change New Selected Window to Green
   Set Sprite Diffuse Window(Array Count(Window())).Bar,0, 255,0
   Set Sprite Diffuse Window(Array Count(Window())).Body, 0, 255, 0
 
   `Set Window Priorities Based on Array Position
   For aWin=0 to Array Count(Window())
 
       Set Sprite Priority Window(aWin).Bar         ,(aWin+1)*100
       Set Sprite Priority Window(aWin).Body        ,(aWin+1)*100
       Set Sprite Priority Window(aWin).MinButton   ,(aWin+1.1)*100
       Set Sprite Priority Window(aWin).CloseButton ,(aWin+1.1)*100
       Set Sprite Priority Window(aWin).LockButton  ,(aWin+1.1)*100
 
   Next aWin
 
EndFunction
 
Function GetIDNumber()
 
   ` Intialize GetIDNumber()
   If GetIDInit=0
      Dim ID(0) as Integer
      Global GetIDInit as Boolean : GetIDInit=1
   EndIf
 
   `Safety Check
   If Array Count(ID())>10000
      Exit Prompt "You have reached The maximum amount of ID's allowed", "GetIDNumber Error" : End
   EndIf
 
   `Pick a Random ID Number
   Local IDNum  as Integer : IDNum =(Rnd(10000)+1)
   Local Unique as Boolean
 
   `Check if the ID number is being used if so pick another new ID
   Repeat
      If Unique=0
         Unique=1
         For aID=1 to Array Count(ID())
            If ID(aID) = IDNum then IDNum=(Rnd(10000)+1) : Unique=0
         Next aID
      EndIf
   Until Unique=1
 
   `If the ID Number is free to use then assign it to the ID Array
   Array Insert at Bottom ID()
   ID(Array Count(ID())) = IDNum
 
EndFunction IDNum
 
 
Function UpdateMouse()
 
   `Initialize Mouse
   If MouseInit=0
 
      `Set Up Mouse Detection
      Global SPMouse as Integer : SPMouse = GetIDNumber()
      Get Image SPMouse,0,0,1,1 : Sprite SPMouse,Mousex(),Mousey(),SPMouse : Hide Sprite SPMouse
 
      Global LastTab     as Integer
      Global LastMClick  as Integer
      Global MClick      as Integer
      Global ButtonState as String
      Global MousePosX   as Integer
      Global MousePosY   as Integer
      Global MouseInit   as Boolean : MouseInit=1
   EndIf
 
   LastMClick=MClick
   MClick=MouseClick()
 
   `Mouse is not held Down
   If MClick=0 and LastMClick=0 then ButtonState="NotPressed"
   `LeftMouse is held Down
   If MClick=1 and LastMClick=0 then ButtonState="LeftPressed"
   `LeftMouse has just Been Pressed
   If MClick=1 and LastMClick=1 then ButtonState="LeftHeld"
   `LeftMouse has just been Lifted
   If MClick=0 and LastMClick=1 then ButtonState="LeftLifted"
   `RightMouse has just been Pressed
   If MClick=2 and LastMClick=0 then ButtonState="RightPressed"
   `RightMouse is held Down
   If MClick=2 and LastMClick=2 then ButtonState="RightHeld"
   `RightMouse has just been Lifted
   If MClick=0 and LastMClick=2 then ButtonState="RightLifted"
 
   `Update Mouse Position
   Sprite SPMouse,MousePosX,MousePosY,SPMouse
   MousePosX = Mousex()
   MousePosY = Mousey()
 
Endfunction
 
Function SetColorAlpha(ImageNumber as Integer, Color as String, Transparency as Byte)
 
   Make Memblock from Image 1,ImageNumber
 
   Local Width  as Dword : Width  = Memblock Dword(1,0)
   Local Height as Dword : Height = Memblock Dword(1,4)
   Local Depth  as Dword : Depth  = Memblock Dword(1,8)
   Local PixelPointer as Integer PixelPointer=12
 
   For Y=1 to Height
      For X=1 to Width
 
         PixelColor as String
         PixelColor=Str$(Memblock Byte(1 , PixelPointer+2)) + "," + Str$(Memblock Byte(1 , PixelPointer+1)) + "," + Str$(Memblock Byte(1 , PixelPointer))
 
         If PixelColor=Color
            Write Memblock Byte 1, PixelPointer+3,Transparency
         EndIf
 
         PixelPointer=PixelPointer+4
      Next X
   Next Y
 
   Delete Image ImageNumber
   Make Image From Memblock ImageNumber,1
 
EndFunction