2021-03-30 14:23:41 +08:00
using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Text ;
using System.Threading.Tasks ;
using UnityEngine ;
using UnityEngine.UI ;
using UnityEngine.EventSystems ;
2021-04-09 09:44:06 +08:00
using UnityEngine.Events ;
2021-03-30 14:23:41 +08:00
namespace Assets.Scripts.UI.Control
{
public class PFUIDropdown : PFUIComponentBase
{
2021-04-09 09:44:06 +08:00
private int mSelectedIndex = 0 ;
2021-03-30 14:23:41 +08:00
public int SelectedIndex
{
get
{
return this . mSelectedIndex ;
}
}
2021-04-09 09:44:06 +08:00
//public int Value
//{
// get
// {
// return this.GetComponent<Dropdown>().value;
// }
// set
// {
// this.GetComponent<Dropdown>().value = value;
// }
//}
public string SelectedItem
2021-03-30 14:23:41 +08:00
{
get
{
2021-04-09 09:44:06 +08:00
if ( this . mSelectedIndex > = 0 )
{
return this . GetComponent < Dropdown > ( ) . options [ this . mSelectedIndex ] . text ;
}
return null ;
}
}
private UnityAction < int > mOnValueChange ;
public UnityAction < int > OnValueChange
{
get
{
return mOnValueChange ;
2021-03-30 14:23:41 +08:00
}
set
{
2021-04-09 09:44:06 +08:00
mOnValueChange = value ;
2021-03-30 14:23:41 +08:00
}
}
2021-04-21 16:16:14 +08:00
private Outline outline ;
2021-03-30 14:23:41 +08:00
void Awake ( )
{
//this.mSelectedIndex =
//UIManager.AddEvent(this.transform.GetComponent<Image>().gameObject, EventTriggerType.Select, new UnityEngine.Events.UnityAction<BaseEventData>(OnSelect));
//UIManager.AddEvent(this.transform.GetComponent<Image>().gameObject, EventTriggerType.Deselect, new UnityEngine.Events.UnityAction<BaseEventData>(OnDeselect));
2021-04-09 09:44:06 +08:00
this . GetComponent < Dropdown > ( ) . onValueChanged . AddListener ( i = > {
mSelectedIndex = i ;
if ( OnValueChange ! = null )
{
mOnValueChange ( i ) ;
}
2021-04-21 16:16:14 +08:00
outline . enabled = false ;
2021-04-09 09:44:06 +08:00
} ) ;
2021-04-21 16:16:14 +08:00
outline = transform . GetComponent < Outline > ( ) ;
2021-03-30 14:23:41 +08:00
}
2021-04-21 16:16:14 +08:00
public void SetValidate ( )
{
outline . enabled = true ;
}
//public void SetValidate(string s)
//{
// outline.gameObject.SetActive(true);
//}
2021-03-30 14:23:41 +08:00
public void OnSelect ( BaseEventData eventData )
{
var image = this . transform . GetComponent < Image > ( ) ;
2021-04-01 11:01:05 +08:00
var png = Resources . Load < Sprite > ( "Images/ipt-1" ) ;
2021-03-30 14:23:41 +08:00
image . sprite = png ;
}
public void OnDeselect ( BaseEventData eventData )
{
var image = this . transform . GetComponent < Image > ( ) ;
2021-04-01 11:01:05 +08:00
var png1 = Resources . Load < Sprite > ( "Images/ipt-0" ) ;
2021-03-30 14:23:41 +08:00
image . sprite = png1 ;
}
public void AddOptions ( List < Dropdown . OptionData > options )
2021-04-09 09:44:06 +08:00
{
this . GetComponent < Dropdown > ( ) . AddOptions ( options ) ;
}
public void AddOptions ( List < string > options )
2021-03-30 14:23:41 +08:00
{
this . GetComponent < Dropdown > ( ) . AddOptions ( options ) ;
2021-04-09 09:44:06 +08:00
}
public void ClearOptions ( )
{
this . GetComponent < Dropdown > ( ) . ClearOptions ( ) ;
}
public void SelectIndex ( int index )
{
this . GetComponent < Dropdown > ( ) . value = index ;
this . mSelectedIndex = index ;
}
public void SelectValue ( string value )
{
var options = this . GetComponent < Dropdown > ( ) . options ;
for ( int i = 0 ; i < options . Count ; i + + )
{
if ( options [ i ] . text = = value )
{
SelectIndex ( i ) ;
return ;
}
}
2021-03-30 14:23:41 +08:00
}
}
}