Context Menu - Pop-Up

Blitz3D Forums/Blitz3D Programming/Context Menu - Pop-Up

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

OK, here it is:
SetBuffer BackBuffer()
Local items$[50]
items[0]="Exit"
items[1]="Return"
items[2]="Start"
items[3]="Wombatify"
items[4]="Forty-two"
items[5]="Shrubbery"

While action<>1
Cls

If MouseHit(2)
action=PopupMenu(MouseX(),MouseY(),items,6)
EndIf
Delay 10
Flip
Wend


;*******************************
;**	Function PopupMenu()	****

;**Parameters:
;x: the x coordinate of the upper left corner of the menu
;y: the y coordinate of the upper left corner of the menu
;item$[50]: the text of the menuitems. Must be a 50-member blitzarray
;num: the number of menuitems. can be up to 50

;return value: the index of the menuitem selected, starting from 1
;if no item is selected, it returns 0

Function PopupMenu%(x,y,item$[50],num)
Local width=0,temp
Local gw=GraphicsWidth(),gh=GraphicsHeight()
If num>50 Return 0

For i=0 To num-1
temp=StringWidth(item[i])
If temp>width width=temp
Next



temp=CreateImage(gw,gh)
CopyRect 0,0,gw,gh,0,0,GraphicsBuffer(),ImageBuffer(temp)

Repeat
Cls

DrawImage temp,0,0

my=(MouseY()-y)/FontHeight()
If my<num And (MouseX()>x And MouseX()<x+width)
pos=my
Else
pos=-1
EndIf


For i=0 To num-1
Color 255,255,255
Rect x-1,y+(i*FontHeight())-1,width+5,FontHeight()+2,1
Color 0,0,0
Rect x-1,y+(i*FontHeight())-1,width+5,FontHeight()+2,0
Text x,y+(i*FontHeight()),item[i]
Next

Flip
Delay 10

Until MouseHit(1)

Cls
DrawImage temp,0,0
FreeImage temp
Return pos+1
End Function


Far from optimized, let me know of any bugs or features you want :)