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-08-26 15:08:02 +08:00
using DG.Tweening ;
2021-03-30 14:23:41 +08:00
namespace Assets.Scripts.UI.Control
{
2021-04-23 15:36:18 +08:00
public class PFUIDropdown : PFUIComponentBase , IPointerClickHandler
2021-03-30 14:23:41 +08:00
{
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 =
2021-04-28 15:21:06 +08:00
UIManager . AddEvent ( this . transform . GetComponent < Image > ( ) . gameObject , EventTriggerType . Select , ( b ) = >
{
App . isChanged = true ;
} ) ;
2021-03-30 14:23:41 +08:00
//UIManager.AddEvent(this.transform.GetComponent<Image>().gameObject, EventTriggerType.Deselect, new UnityEngine.Events.UnityAction<BaseEventData>(OnDeselect));
2021-04-28 15:21:06 +08:00
2021-04-23 15:36:18 +08:00
//this.GetComponent<Dropdown>().itemText.color = Utils.HexToColorHtml("#ff2742");
2021-04-09 09:44:06 +08:00
this . GetComponent < Dropdown > ( ) . onValueChanged . AddListener ( i = > {
mSelectedIndex = i ;
if ( OnValueChange ! = null )
{
mOnValueChange ( i ) ;
}
2021-04-27 18:26:30 +08:00
SetWhite ( i ) ;
2021-04-21 16:16:14 +08:00
outline . enabled = false ;
2021-04-23 15:36:18 +08:00
2021-04-09 09:44:06 +08:00
} ) ;
2021-04-27 18:26:30 +08:00
SetWhite ( this . GetComponent < Dropdown > ( ) . value ) ;
2021-04-21 16:16:14 +08:00
outline = transform . GetComponent < Outline > ( ) ;
2021-03-30 14:23:41 +08:00
}
2021-04-27 18:26:30 +08:00
public void SetWhite ( int index )
{
if ( index ! = - 1 & & this . GetComponent < Dropdown > ( ) . interactable )
{
this . GetComponent < Dropdown > ( ) . captionText . color = Utils . HexToColorHtml ( "#ffffff" ) ;
}
2021-04-29 11:36:30 +08:00
if ( ! GetComponent < Dropdown > ( ) . interactable )
{
this . GetComponent < Dropdown > ( ) . captionText . color = Utils . HexToColorHtml ( "#5c5c6e" ) ;
}
2021-04-27 18:26:30 +08:00
}
2021-04-21 16:16:14 +08:00
public void SetValidate ( )
{
outline . enabled = true ;
2021-08-26 15:08:02 +08:00
outline . DOFade ( 1 , 1f ) . onComplete = ( ) = >
{
outline . DOFade ( 0 , 1f ) ;
} ;
2021-04-21 16:16:14 +08:00
}
//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 ;
2021-04-28 15:21:06 +08:00
App . isChanged = true ;
2021-03-30 14:23:41 +08:00
}
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
}
2021-04-23 15:36:18 +08:00
public void OnPointerClick ( PointerEventData eventData )
{
2021-04-27 18:26:30 +08:00
if ( GetComponent < Dropdown > ( ) . value = = - 1 | | ! GetComponent < Dropdown > ( ) . interactable ) return ;
2021-04-23 15:36:18 +08:00
var x = transform . Find ( "Dropdown List" ) . GetComponent < ScrollRect > ( ) . content ;
x . GetChild ( SelectedIndex + 1 ) . Find ( "Item Label" ) . GetComponent < Text > ( ) . color = Utils . HexToColorHtml ( "#f93086" ) ;
}
2021-03-30 14:23:41 +08:00
}
}