Code archives/User Input/DropDown Menus!

This code has been declared by its author to be Public Domain code.

Download source code

DropDown Menus! by Yahfree2007
Hey, first of all i'd like to thank b32, for helping me with many problems i encountered making this..

UPDATE: Better ID system, DeleteDropDown function, Visual Arrow, width support.


Ok, this dropdown system works with types, so you will need the functions, and the types above the main program (see code)

you will also need this before everything:

"Global CursorX, CursorY, CursorHit"

and this in your main loop:

"CursorX=MouseX()
CursorY=MouseY()
CursorHit=MouseHit(1)"

to create a dropdown simply use the following

DDname.DropDown=CreateDropDown(X,Y,Width,AcceptsID)

DDname.DropDown = DDname is the handle of your dropdown basicly.. later you can use this to access stuff like the current selected item (DDname\words)

X=X of dropdown
Y= y of dropdown
Width = width of dropdown *MUST be 25 or higher
AcceptsID = ID that the dropdown accepts more later

To add stuff to your dropdown you must have the following after your graphics setup..

"Type list
Field name$
Field ID
End Type"

ID is the dropdown this goes into (see createdropdown's AcceptsID)

the so if you have a dropdown that accepts ID 5 and you want this item to go into that dropdown, you delclare that items ID=5.

name$ is what the user sees in the dropdown,

to create an item do the following:

"myitem.list = New list
myitem\name="Pick ME"
myitem\ID=1"

you can add more fields to the List type if you want to know more about what the user picked, for example:

"Type list
Field name$
Field ID
Field number
End Type"

you access these attributes like so:

"For checklist.list = Each list
If checklist\name=DDname\words
print checklist\number
end if
next"


as shown above i accessed the current "words" that the dropdown is displaying uptop.

you can access all the attributes of a dropdown by a simple: DDname\attrubute

replace ddname with your dropdown handle and atributes with the attribute you want to access.

here is the dropdown type this goes above all the functions. and below that global call of Cursor stats(see above)

"Type DropDown
Field drawdrop
Field scrolly
Field shown
Field x,y
Field words$
Field Accepts
Field DropDown_ID
End Type"

other stuff:

DrawDropDowns(handle)

you could draw all dropdowns with a for-next loop like in the code example below, or you could draw them indivisualy by feeding a dropdowns handle into it.

DeleteDropDown(handle)

This deletes a dropdown specified by the handle.



Here is an example of all of the above put into one:
Global CursorX, CursorY, CursorHit

Type DropDown
Field width
Field drawdrop
Field scrolly
Field shown
Field x,y
Field words$
Field Accepts
End Type

;-----------------------------------------------------------------
;                       CreateDropDown()
;-----------------------------------------------------------------




Function CreateDropDown.DropDown(x,y,dd_width,Accepted)



NewDD.DropDown = New DropDown
NewDD\x=x
NewDD\y=y
NewDD\drawdrop=False
NewDD\scrolly=y+20
NewDD\shown=y
NewDD\Accepts=Accepted
NewDD\width=dd_width


Return NewDD
End Function

;----------------------------------------------------------------
;              DrawDropDowns()
;----------------------------------------------------------------

Function DrawDropDowns(Creation_Info.DropDown)



drawdrop = Creation_Info\drawdrop
scrolly = Creation_Info\scrolly
shown = Creation_Info\shown
x = Creation_Info\x
y = Creation_Info\y
words$ = Creation_Info\words$
number = Creation_Info\Accepts
width = Creation_Info\width

;If you click on the button, dropdown=True
If RectsOverlap(x+width-25,y,25,20,CursorX,CursorY,5,5) And CursorHit
  If drawdrop=False
     drawdrop=True
Else
   drawdrop=False
  End If
End If



If drawdrop=True
Rect x,y,width,200,0
Line x+width,y+20,x+width-12.5,y+10
Line x+width-25,y+20,x+width-12.5,y+10


;For...Each ... should get the name of all the objects.
i=0
For checklist.list = Each list

If checklist\ID=number
i=i+1

;Made this For i...2 loop so the Text can loop itself...
Viewport x,y+20,125,180
   Text x,shown+(i*20),checklist\name
Viewport 0,0,GraphicsWidth(),GraphicsHeight()

;If you click on one of the names it will put it up top.
    If RectsOverlap(x,shown+(i*20),width-25,20,CursorX,CursorY,5,5) And CursorX > x And CursorX< x+125 And CursorY > y+20 And CursorY < y+200 And MouseDown(1)
        words$=checklist\name
        drawdrop=False
        shown=y
        scrolly=y+20
     End If


End If
Next
Rect x+width-25,y+20,25,180,False
If i>9 

 


   Rect x+width-25,scrolly,25,20,True
  If RectsOverlap(x+width-25,y+20,25,180,CursorX,CursorY,5,5) And MouseDown(1)
     scrolly=MouseY()
End If




If scrolly<y+20 scrolly=y+20
If scrolly>y+180 scrolly=y+180

;The more things in the Text the faster it scrolls To compensate..
ScrollbarPos = ScrollY - y-20
ScrollbarMax = y+180- y-20
ListSize = (i - 9) * 20 
shown = y - (scrollbarpos * listsize / scrollbarmax)


End If

Else

Line x+width-25,y,x+width-12.5,y+10
Line x+width,y,x+width-12.5,y+10

End If



;Draws the 3 main parts of the dropdown, with all the math from above: Main box, DD button, 
Rect x,y,width,20,0
Rect x+width-25,y,25,20,0
Text x,y,words


;Takes the Creation info and Equals it to the messed with dropdown info.
Creation_Info\scrolly = scrolly
Creation_Info\drawdrop = drawdrop
Creation_Info\shown = shown
Creation_Info\words$ = words$

End Function

;-------------------------------------------------------------------------
;                          DeleteDropDown(DropDown_ID)
;-------------------------------------------------------------------------

Function DeleteDropDown(ID.Dropdown)
Delete ID
End Function

;----------------------------------------------------------------------
;             Example program
;----------------------------------------------------------------------



Graphics 300,500,16,2

SetBuffer BackBuffer()

;---------------------

Type list
   Field name$
   Field ID
End Type

For i=1 To 200

list1.list = New list
list1\name="bah"+i
list1\ID=1

Next

For i=1 To 400

list1.list = New list
list1\ID=2
list1\name="test"+i

Next

;---------------------------

dd1.DropDown=CreateDropDown(50,0,100,1) ; ID=1
dd2.DropDown=CreateDropDown(180,0,100,1) ; ID=2
dd3.DropDown=CreateDropDown(50,250,100,2) ; ID=3
dd4.DropDown=CreateDropDown(180,250,100,2) ; ID=4

;--------------------------------------
While Not KeyHit(1)
Cls

CursorX = MouseX()
CursorY = MouseY()
CursorHit = MouseHit(1)


For CheckDD.DropDown = Each DropDown
DrawDropDowns(CheckDD.DropDown)
Next

Text 0,460,"Press 2-5 to delete Dropdowns"
If KeyHit(3) DeleteDropDown(dd1)
If KeyHit(4) DeleteDropDown(dd2)
If KeyHit(5) DeleteDropDown(dd3)
If KeyHit(6) DeleteDropDown(dd4)




Delay 5
Flip
Wend

Comments

Yo! Wazzup?2007
b23? I think you mean b32.
Nice Dropdowns!


Yahfree2007
Haha, your right, Sorry >>b32<< :D


Code Archives Forum