﻿B4J=true
Group=Default Group
ModulesStructureVersion=1
Type=Class
Version=9.3
@EndOfDesignText@

#DesignerProperty: Key: BackgroundColor, DisplayName: Background Color, FieldType: Color, DefaultValue: 0xFFFFFFFF
#DesignerProperty: Key: ButtonColor, DisplayName: Button Color, FieldType: Color, DefaultValue: 0xFF000000
#DesignerProperty: Key: SlideColor, DisplayName: Slide Color, FieldType: Color, DefaultValue: 0xFF000000
#DesignerProperty: Key: SliderTextColor, DisplayName: Slider Text Color, FieldType: Color, DefaultValue: 0xFF000000
#DesignerProperty: Key: CornerRadius, DisplayName: Corner Radius, FieldType: Int, DefaultValue: 10, MinRange: 0

#DesignerProperty: Key: StartDirection, DisplayName: Start Direction, FieldType: String, DefaultValue: LeftToRight, List: LeftToRight|RightToLeft
#DesignerProperty: Key: SliderText, DisplayName: Slider Text, FieldType: String, DefaultValue: slide to unlock
#DesignerProperty: Key: SlideBackDuration, DisplayName: Slide Back Duration, FieldType: Int, DefaultValue: 250, MinRange: 0

#DesignerProperty: Key: SmoothChangeDirectionAfterReachedEnd, DisplayName: Change direction after reached end, FieldType: Boolean, DefaultValue: False, Description: Smooth change direction after reached end

#Event: ReachedEnd

Sub Class_Globals
	Private mEventName As String 'ignore
	Private mCallBack As Object 'ignore
	Public mBase As B4XView
	Private xui As XUI 'ignore
	Public Tag As Object
	
	Private xlbl_SliderText As B4XView
	
	Private xpnl_Slide As B4XView
	Private xpnl_Button As B4XView
	Private xiv_ButtonIcon As B4XView
	
	Private m_BackgroundColor As Int
	Private m_SliderTextColor As Int
	Private m_ButtonColor As Int
	Private m_SlideColor As Int
	Private m_CornerRadius As Float
	Private m_SliderText As String
	Private m_SmoothChangeDirectionAfterReachedEnd As Boolean
	Private m_StartDirection As String
	
	Private mButtonGap As Float = 0
	Private m_ButtonCornerRadius As Float = 0
	
	Private donwx As Int
	Private ReachEnd As Boolean = False
	
	Private ButtonIconImage As B4XBitmap
	
	
End Sub

Public Sub Initialize (Callback As Object, EventName As String)
	mEventName = EventName
	mCallBack = Callback
End Sub

'Base type must be Object
Public Sub DesignerCreateView (Base As Object, Lbl As Label, Props As Map)
	mBase = Base
    Tag = mBase.Tag
    mBase.Tag = Me 
	
	xpnl_Button = xui.CreatePanel("xpnl_Button")
	xpnl_Slide = xui.CreatePanel("")
	xlbl_SliderText = CreateLabel("")
	mBase.AddView(xlbl_SliderText,0,0,0,0)
	mBase.AddView(xpnl_Slide,0,0,0,0)
	mBase.AddView(xpnl_Button,0,0,0,0)
	
	xiv_ButtonIcon = CreateImageView("")
	xpnl_Button.AddView(xiv_ButtonIcon,0,0,0,0)
	
	IniProps(Props)
	
	#If B4A
	Base_Resize(mBase.Width,mBase.Height)
	#End If
	
End Sub

Private Sub IniProps(Props As Map)
	m_BackgroundColor = xui.PaintOrColorToColor(Props.Get("BackgroundColor"))
	m_SliderTextColor = xui.PaintOrColorToColor(Props.Get("SliderTextColor"))
	m_ButtonColor = xui.PaintOrColorToColor(Props.Get("ButtonColor"))
	m_SlideColor = xui.PaintOrColorToColor(Props.Get("SlideColor"))
	m_CornerRadius = Props.Get("CornerRadius")
	m_SliderText = Props.Get("SliderText")
	m_SmoothChangeDirectionAfterReachedEnd = Props.Get("SmoothChangeDirectionAfterReachedEnd")
	m_StartDirection = Props.Get("StartDirection")
	
	mBase.Color = m_BackgroundColor
	
	mBase.SetColorAndBorder(mBase.Color,0,0,m_CornerRadius)
	
	xlbl_SliderText.SetTextAlignment("CENTER","CENTER")
	xlbl_SliderText.TextColor = xui.Color_Black
	xlbl_SliderText.Text = m_SliderText
	xlbl_SliderText.TextColor = m_SliderTextColor
	
	xpnl_Button.Color = m_ButtonColor
End Sub

Private Sub Base_Resize (Width As Double, Height As Double)
  
	'mBase.SetLayoutAnimated(0,0,0,Width,Height)
	
	If m_StartDirection = "LeftToRight" Then
	xpnl_Button.SetLayoutAnimated(0,mButtonGap,mButtonGap,Height - mButtonGap*2,Height - mButtonGap*2)
	Else
		xpnl_Button.SetLayoutAnimated(0,Width - Height - mButtonGap*2 - mButtonGap,mButtonGap,Height - mButtonGap*2,Height - mButtonGap*2)
	End If
	xlbl_SliderText.SetLayoutAnimated(0,0,0,Width,Height)
  
  
	xiv_ButtonIcon.SetLayoutAnimated(0,0,0,xpnl_Button.Width,xpnl_Button.Height)
	If ButtonIconImage.IsInitialized = True Then
	xiv_ButtonIcon.SetBitmap(ButtonIconImage.Resize(Max(1dip,xiv_ButtonIcon.Width),Max(1dip,xiv_ButtonIcon.Height),True))
	End If
End Sub


Public Sub setButtonCornerRadius(CornerRadius As Float)
	
	m_ButtonCornerRadius = CornerRadius
	xpnl_Button.SetColorAndBorder(m_ButtonColor,0,0,CornerRadius)
	
End Sub

Public Sub setCornerRadius(CornerRadius As Float)
	
	m_CornerRadius = CornerRadius
	mBase.SetColorAndBorder(mBase.Color,0,0,m_CornerRadius)
	
End Sub

Public Sub setButtonIcon(Icon As B4XBitmap)
	ButtonIconImage = Icon
	xiv_ButtonIcon.SetBitmap(Icon.Resize(Max(1dip,xiv_ButtonIcon.Width),Max(1dip,xiv_ButtonIcon.Height),True))
End Sub

Public Sub setSliderText(Text As String)
	m_SliderText = Text
	xlbl_SliderText.Text = Text
End Sub

Public Sub getStartDirection As String
	Return m_StartDirection
End Sub

Private Sub xpnl_Button_Touch (Action As Int, X As Float, Y As Float)
	
	If Action = xpnl_Button.TOUCH_ACTION_DOWN Then
		donwx = x
	else If Action = xpnl_Button.TOUCH_ACTION_MOVE Then
	
		If xpnl_Button.Left + x - donwx + xpnl_Button.Width < mBase.Width Then
			xpnl_Button.Left = Max(0 + mButtonGap,xpnl_Button.Left + x - donwx)
		Else
			xpnl_Button.Left = Min(mBase.Width - xpnl_Button.Width - mButtonGap,xpnl_Button.Left + x - donwx + xpnl_Button.Width)
		End If
	
		
		xpnl_Slide.Color = m_SlideColor
		xpnl_Slide.Visible = True
		If m_StartDirection = "LeftToRight" Then
			xpnl_Slide.SetLayoutAnimated(0,0,0,xpnl_Button.Left + xpnl_Button.Width/2,mBase.Height)
		Else
			xpnl_Slide.SetLayoutAnimated(0,xpnl_Button.Left + xpnl_Button.Width/2,0,mBase.Width - xpnl_Button.Left + xpnl_Button.Width/2,mBase.Height)
		End If
	
		If m_StartDirection = "LeftToRight" Then
			If xpnl_Button.Left + xpnl_Button.Width = mBase.Width Then
				ReachEnd = True
				Else
				ReachEnd = False
			End If
		Else
			If xpnl_Button.Left = 0 Then
				ReachEnd = True
				Else
				ReachEnd = False
			End If
		End If
	
	else If Action = xpnl_Button.TOUCH_ACTION_UP Then
		If ReachEnd = True Then
			ReachedEnd
			'xpnl_Slide.SetColorAnimated(250,xui.Color_ARGB(152,clr_argb(1),clr_argb(2),clr_argb(3)),xui.Color_ARGB(255,clr_argb(1),clr_argb(2),clr_argb(3)))
			Sleep(250)
			If m_StartDirection = "LeftToRight" Then
				If m_SmoothChangeDirectionAfterReachedEnd = False Then
					xpnl_Button.SetLayoutAnimated(250,mButtonGap,xpnl_Button.Top,xpnl_Button.Width,xpnl_Button.Height)
					xpnl_Slide.SetLayoutAnimated(250,xpnl_Slide.Left,xpnl_Slide.Top,2dip,mBase.Height)
				Else
					m_StartDirection = "RightToLeft"
					xpnl_Slide.SetLayoutAnimated(250,mBase.Width,xpnl_Slide.Top,2dip,mBase.Height)
				End If
			Else
				If m_SmoothChangeDirectionAfterReachedEnd = False Then
					xpnl_Button.SetLayoutAnimated(250,mBase.Width - xpnl_Button.Width - mButtonGap,xpnl_Button.Top,xpnl_Button.Width,xpnl_Button.Height)
					xpnl_Slide.SetLayoutAnimated(250,mBase.Width,xpnl_Slide.Top,2dip,mBase.Height)

				Else
					m_StartDirection = "LeftToRight"
					xpnl_Slide.SetLayoutAnimated(250,mButtonGap,xpnl_Slide.Top,2dip,mBase.Height)
					End If
			End If
			Sleep(250)
			xpnl_Slide.Visible = False
		Else
			If m_StartDirection = "LeftToRight" Then
				xpnl_Button.SetLayoutAnimated(250,mButtonGap,xpnl_Button.Top,xpnl_Button.Width,xpnl_Button.Height)
				xpnl_Slide.SetLayoutAnimated(250,xpnl_Slide.Left,xpnl_Slide.Top,2dip,mBase.Height)
			Else
				xpnl_Button.SetLayoutAnimated(250,mBase.Width - xpnl_Button.Width - mButtonGap,xpnl_Button.Top,xpnl_Button.Width,xpnl_Button.Height)
				xpnl_Slide.SetLayoutAnimated(250,mBase.Width,xpnl_Slide.Top,2dip,mBase.Height)
			End If
			Sleep(250)
			xpnl_Slide.Visible = False
		End If
		ReachEnd = False
	End If
End Sub

Private Sub CreateLabel(EventName As String) As B4XView
	Dim lbl As Label
	lbl.Initialize(EventName)
	Return lbl
End Sub

Private Sub CreateImageView(EventName As String) As B4XView
	Dim iv As ImageView
	iv.Initialize(EventName)
	Return iv
End Sub

Private Sub ReachedEnd
	If xui.SubExists(mCallBack,mEventName & "_ReachedEnd",0) Then
		CallSub(mCallBack,mEventName & "_ReachedEnd")
	End If
End Sub