﻿B4A=true
Group=Default Group
ModulesStructureVersion=1
Type=Class
Version=6.8
@EndOfDesignText@
'#Event: Clicked (item As String)

Private Sub Class_Globals
	Type item(index As Int, text As String)
	Private inActivity As Activity
	Private acT As Object
	Private eSub As String
	Private Base As Panel
	Private maxlblwidth As Float
	Private itemHeight As Float
	Private itemlist As List 
	Private godown, yAnim, goright As Boolean
	Private vSender As View
	Private abslutedifference As Float
End Sub

'Target = is the Callback Activity that handle the _Clicked Event
'EventName = set the EventName for catching the item clicks
'Height = set the Height of all Menu items
'Right = if TRUE Menu will slide to the right (default is left)
'Down = if TRUE Menu will slide down (default is up)
'yAnim = if TRUE Menu Animation will be also in the y axis
'v = set the Target View (like a button, panel, label,...)
'Activity = set a reference to the Activity
Public Sub Initialize(Target As Object, EventName As String, Height As Float, Right As Boolean, Down As Boolean, yAnimation As Boolean,V As View, Activity As Activity)
	acT = Target
	eSub = EventName
	itemlist.Initialize
	Base.Initialize("base")
	Base.Color = Colors.ARGB(80,0,0,0)
	Base.Visible = False
	Base.SetElevationAnimated(100,5dip)
	maxlblwidth = 0
	itemHeight = Height
	inActivity = Activity
	inActivity.AddView(Base,0,0,inActivity.Width,inActivity.Height)
	goright = Right
	godown = Down
	yAnim = yAnimation
	vSender = v
	abslutedifference = GetDeviceLayoutValues.Height-inActivity.Height
End Sub

Private Sub base_Click
	dismiss
End Sub

'Return True if Menu is Visible
Public Sub isOpen As Boolean
	Return Base.Visible
End Sub

Private Sub getLocation(v As View) As Int()
	Dim leftTop(2) As Int
	Dim JO As JavaObject = v
	JO.RunMethod("getLocationOnScreen", Array As Object(leftTop))
	Return leftTop
End Sub


'Show the Menu
Public Sub show
	If itemlist.Size = 0 Then Return
	If Base.Visible Then 
		dismiss
		Return
	End If
	
	Base.RemoveAllViews
	Base.SetVisibleAnimated(300,True)
	Base.BringToFront
	
	getmaxwidth 'calculate maxwidth
	
	Dim fromleft, toleft, fromtop, pnlwidth, pnlheight, pnlheight2 As Float
	pnlwidth = itemHeight+18%x+maxlblwidth
	pnlheight = itemHeight+(itemHeight*0.2)
	
	Dim absolutelocation() As Int = getLocation(vSender)
	
	If goright = True Then 
		fromleft = absolutelocation(0) 'vSender.Left
		toleft = absolutelocation(0) 'vSender.Left
	Else 
		fromleft = absolutelocation(0)+vSender.Width
		toleft = (absolutelocation(0)+vSender.Width) - (itemHeight+18%x+maxlblwidth)	
	End If
	
	If godown Then 
		fromtop = absolutelocation(1)+vSender.Height+(itemHeight*0.2)-abslutedifference
		pnlheight2 = itemHeight+(itemHeight*0.1)
	Else 
		fromtop = absolutelocation(1)-pnlheight-abslutedifference
		pnlheight2 = (itemHeight+(itemHeight*0.1)) *-1
	End If
	
	For i = 0 To itemlist.Size-1
		Dim pnl As Panel = itemlist.Get(i)
		If yAnim Then 
			Base.AddView(pnl,fromleft,fromtop,0,itemHeight)
			pnl.SetLayoutAnimated(150+(i*100),toleft,fromtop+(pnlheight2*i),pnlwidth,pnl.Height)	
		Else 
			Base.AddView(pnl,fromleft,fromtop+(pnlheight2*i),0,itemHeight)
			pnl.SetLayoutAnimated(150+(i*100),toleft,pnl.Top,pnlwidth,pnl.Height)	
		End If	
		Dim lbl As Label = pnl.GetView(0)
		lbl.Width = maxlblwidth
		
		Dim lbl2 As Label = pnl.GetView(2)
		lbl2.Left = pnl.Width-itemHeight
	Next
End Sub

'Close the Menu if it's open
Public Sub dismiss
	If Base.Visible Then Base.SetVisibleAnimated(300, False)
End Sub

'Get a list of all items (each item is a Panel that contains a label and an imageview)
Public Sub itemsList As List
	Return itemlist
End Sub

Public Sub ItemPanel(index As Int) As Panel
	If itemlist.Size < index Then
		 Log("Index is wrong.")
		 Return Null
	End If
	Dim p As Panel = itemlist.Get(index)
	Return p
End Sub

'Get the Label of the Menuitem
Public Sub ItemLabel(index As Int) As Label
	If itemlist.Size < index Then
		 Log("Index is wrong.")
		 Return Null
	End If
	Dim p As Panel = itemlist.Get(index)
	Dim lbl As Label = p.GetView(0)
	Return lbl
End Sub

'Get the Imageview (icon) of the Menuitem
Public Sub ItemIcon(index As Int) As Button
	If itemlist.Size < index Then
		 Log("Index is wrong.")
		 Return Null
	End If
	Dim p As Panel = itemlist.Get(index)
	Dim v As View = p.GetView(1)
	If v Is Button Then 
		Dim btn As Button = v
		Return btn
	Else 
		Log("No icon was set when Menuitem was intialized.")
		Return Null	
	End If
End Sub

Public Sub getItemHeight As Int
	If itemlist.Size < 0 Then
		 Log("Index is wrong.")
		 Return 0
	End If
	Dim p As Panel = itemlist.Get(0)
	Return (p.Height/2)-1
End Sub

'Text = Set the Text of the Menuitem
'txtColor = Set the Textcolor
'txtsize = Set the Textsize
'bmp = set the icon bitmap
Public Sub additem(Text As String, txtColor As Int, txtsize As Int, bmp As Bitmap)
	Dim dr As ColorDrawable
	dr.Initialize(Colors.White,(itemHeight/2)-1)
  
	Dim ItemPnl As Panel
	ItemPnl.Initialize("itempnl")
	ItemPnl.Tag = itemlist.Size
	ItemPnl.Background = dr
 
	Dim itemlbl As Label
	itemlbl.Initialize("itemlbl")
	itemlbl.Text = Text
	itemlbl.TextColor = txtColor
	itemlbl.Gravity = Gravity.CENTER_VERTICAL
	itemlbl.Typeface = Typeface.DEFAULT
	itemlbl.TextSize = txtsize
   
	Dim itemlbl2 As Label
	itemlbl2.Initialize("itemlbl2")
	'   itemlbl2.Text = "="
	itemlbl2.TextColor = Colors.RGB(43,43,43)
	itemlbl2.TextSize = 24
	itemlbl2.Gravity = Gravity.CENTER
	itemlbl2.Typeface = Typeface.DEFAULT
	itemlbl2.Tag = "lbl"
   
	Dim itemimgbtn As Button
	itemimgbtn.Initialize("")
	itemimgbtn.Tag = "btn"
	itemimgbtn.Color = Colors.Transparent
     
	If bmp <> Null And bmp.IsInitialized Then
		itemimgbtn.SetBackgroundImage(updatebmp(bmp))
		itemimgbtn.Gravity = Gravity.FILL
	End If

	'add items to list
	ItemPnl.AddView(itemlbl,		itemHeight+2%x,	0,	0,	itemHeight)
	ItemPnl.AddView(itemimgbtn,	itemHeight*0.1,	itemHeight*0.1,	itemHeight*0.8,	itemHeight*0.8)
	ItemPnl.AddView(itemlbl2,	0,	0,	itemHeight,	itemHeight)
	itemlist.Add(ItemPnl)
End Sub

Private Sub getmaxwidth
	 Dim measuretxt As Float = 0
	 For Each pnl As Panel In itemlist
	 	Dim lbl As Label = pnl.GetView(0)
		Dim maxw As Float = measuretext(lbl.Text,lbl.Typeface,lbl.TextSize)
		If measuretxt < maxw Then measuretxt = maxw
	Next
	maxlblwidth = measuretxt
End Sub

'Return a round bitmap
Public Sub updatebmp(bmp As Bitmap) As Bitmap
     Private cvs As Canvas = CreateBitmap
	 DrawRoundBitmap(cvs, bmp)
  	 Return cvs.Bitmap
End Sub

Private Sub measuretext(text As String, tf As Typeface, txtsize As Int) As Float
   Private bmp As Bitmap
   bmp.InitializeMutable(200dip, 200dip)
   Private cvs As Canvas
   cvs.Initialize2(bmp)
   Return cvs.MeasureStringWidth(text,tf,txtsize)
End Sub

Sub itempnl_Touch (Action As Int, X As Float, Y As Float)
	Dim pnl As Panel = Sender
	Select Action
		Case 0 'down
'			pnl.Color = Colors.RGB(220,220,220)
		Case 1 'up
			CallSub2(acT, eSub & "_Click", pnl.Tag)
			dismiss 'hide menu			
		Case 2	'move
			'do nothing	
	End Select
End Sub

Private Sub CreateBitmap As Canvas
   Private bmp As Bitmap
   bmp.InitializeMutable(50dip, 50dip)
   Private cvs As Canvas
   cvs.Initialize2(bmp)
   Private r As Rect
   r.Initialize(0, 0, bmp.Width, bmp.Height)
   cvs.DrawRect(r, Colors.Transparent, True, 0)
   Private p As Path
   p.Initialize(0, 0)
   Private jo As JavaObject = p
   Private x = 25dip, y = 25dip, radius = 25dip As Float
   jo.RunMethod("addCircle", Array As Object(x, y, radius, "CW"))
   cvs.ClipPath(p)
   Return cvs
End Sub

Private Sub DrawRoundBitmap (cvs As Canvas, bmp As Bitmap)
   Private r As Rect
   r.Initialize(0, 0, cvs.Bitmap.Width, cvs.Bitmap.Height)
   cvs.DrawBitmap(bmp, Null, r)
End Sub