AkiraPixelWind/Assets/Scripts/Main/UI/UIYYControl/UIInputNumChange.cs
2022-12-29 18:20:40 +08:00

283 lines
6.3 KiB
C#

/*-------------------------------------------------------------------------------------
--- 创建人:
--- 描 述:
--- 创建时间: 2022年8月10日
-------------------------------------------------------------------------------------*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Game;
using Axibug.Event;
using System;
using TMPro;
/// <summary>
/// xx 功能界面
/// </summary>
public class UIInputNumChange : MonoBehaviour
{
//UIAutoBuild_CtrlDefine
//public InputField iptNumShow;
//public Button btnAdd;
//public Button btnRedece;
//UIAutoBuild_CtrlDefine
public int mNum { get; private set; } = 0;
public int mMaxNum = -1;
public int mMinNum = -1;
private bool mStateAdd = false;
private bool mStateReduce = false;
private float mBeginExp;
private float mEndExp;
private float mTickTimeSpeed;
private float mBeginExp2;
private float mEndExp2;
private float mTickTimeSpeed2;
public TMP_InputField IptNumShow;
private UILongClickButton mAddBtn;
private UILongClickButton mReduceBtn;
/// <summary>
/// 抛出数值修改事件,父级界面联动用
/// </summary>
[HideInInspector]
public Action ChangeEvent;
bool bAwake = false;
public void Awake()
{
////UIAutoBuild_CtrlBind
//Transform tf = transform;
//iptNumShow = tf.Find("IptNumShow").GetComponent<InputField>();
//btnAdd = tf.Find("btnAdd").GetComponent<Button>();
//btnRedece = tf.Find("btnRedece").GetComponent<Button>();
//UIAutoBuild_CtrlBind
Transform tf = transform;
IptNumShow = tf.Find("TIptNumShow").GetComponent<TMP_InputField>();
mAddBtn = tf.Find("BtnAdd").GetComponent<UILongClickButton>();
mReduceBtn = tf.Find("BtnRedece").GetComponent<UILongClickButton>();
bAwake = true;
}
void OnEnable()
{
//UIAutoBuild_EventReg
//btnAdd.onClick.AddListener(OnBtnAddClick);
//btnRedece.onClick.AddListener(OnBtnRedeceClick);
//UIAutoBuild_EventReg
mAddBtn.onClick.AddListener(OnBtnAddClick);
mReduceBtn.onClick.AddListener(OnBtnRedeceClick);
IptNumShow.onValueChanged.AddListener((param) => { OniptNumShowChange(param); });
mAddBtn.NumberChange += (() => { AddNum(); });
mAddBtn.NumberChangeCancel += (() => { AddNumCanel(); });
mReduceBtn.NumberChange += (() => { ReduceNum(); });
mReduceBtn.NumberChangeCancel += (() => { ReduceNumCanel(); });
/*
AppEntry.Event.Subscribe(NumChangeAddEvent.EventId, AddNum);
AppEntry.Event.Subscribe(NumChangeReduceEvent.EventId, ReduceNum);
AppEntry.Event.Subscribe(NumChangeAddCancelEvent.EventId, AddNumCanel);
AppEntry.Event.Subscribe(NumChangeReduceCancelEvent.EventId, ReduceNumCanel);
*/
}
void OnDisable()
{
//UIAutoBuild_EventUnReg
//btnAdd.onClick.RemoveAllListeners();
//btnRedece.onClick.RemoveAllListeners();
//UIAutoBuild_EventUnReg
mAddBtn.onClick.RemoveAllListeners();
mReduceBtn.onClick.RemoveAllListeners();
IptNumShow.onValueChanged.RemoveAllListeners();
mAddBtn.NumberChange = null;
mAddBtn.NumberChangeCancel = null;
mReduceBtn.NumberChange = null;
mReduceBtn.NumberChangeCancel = null;
/*
AppEntry.Event.Unsubscribe(NumChangeAddEvent.EventId, AddNum);
AppEntry.Event.Unsubscribe(NumChangeReduceEvent.EventId, ReduceNum);
AppEntry.Event.Unsubscribe(NumChangeAddCancelEvent.EventId, AddNumCanel);
AppEntry.Event.Unsubscribe(NumChangeReduceCancelEvent.EventId, ReduceNumCanel);
*/
}
void Start()
{
}
// 每次显示都执行, 可定义参数
public void Show()
{
}
public void ClearNum()
{
mNum = 0;
if (!bAwake) return;
IptNumShow.text = mNum.ToString();
ChangeEvent?.Invoke();
}
public void SetNum(int tNum)
{
if (mMaxNum != -1)
mNum = Mathf.Min(mMaxNum, tNum);
if (mMinNum != -1)
mNum = Mathf.Max(mMinNum, mNum);
if (!bAwake) return;
IptNumShow.text = mNum.ToString();
ChangeEvent?.Invoke();
}
private void OniptNumShowChange(string pValue)
{
if (int.TryParse(pValue, out int tNum))
{
mNum = tNum;
if(mMaxNum != -1)
mNum = Mathf.Min(mMaxNum, tNum);
if(mMinNum != -1)
mNum = Mathf.Max(mMinNum, mNum);
}
IptNumShow.text = mNum.ToString();
ChangeEvent?.Invoke();
}
private void OnBtnAddClick()
{
if (int.TryParse(IptNumShow.text, out int tNum))
{
++tNum;
if (mMaxNum != -1)
{
tNum = Mathf.Min(tNum, mMaxNum);
}
IptNumShow.text = tNum.ToString();
ChangeEvent?.Invoke();
}
else
{
Debug.LogError("IptNumShow.text 数据错误: " + IptNumShow.text);
}
}
private void OnBtnRedeceClick()
{
if (int.TryParse(IptNumShow.text, out int tNum))
{
--tNum;
if (mMinNum != -1)
{
tNum = Mathf.Max(tNum, mMinNum);
}
IptNumShow.text = tNum.ToString();
ChangeEvent?.Invoke();
}
else
{
Debug.LogError("IptNumShow.text 数据错误: " + IptNumShow.text);
}
}
public void SetMaxNum(int pMaxNum)
{
mMaxNum = pMaxNum;
}
public void SetMinNum(int pMinNum)
{
mMinNum = pMinNum;
}
public void SetMaxNumAndMinNum(int pMaxNum, int pMinNum)
{
mMaxNum = pMaxNum;
mMinNum = pMinNum;
}
private void AddNum()
{
if (mMaxNum == -1) return;
mNum = int.Parse(IptNumShow.text);
mStateAdd = true;
}
private void ReduceNum()
{
if (mMinNum == -1) return;
mNum = int.Parse(IptNumShow.text);
mStateReduce = true;
}
private void AddNumCanel()
{
mStateAdd = false;
mTickTimeSpeed = 0;
}
private void ReduceNumCanel()
{
mStateReduce = false;
mTickTimeSpeed2 = 0;
}
private void Update()
{
if(mStateAdd)
{
int change = mMaxNum - mNum;
mBeginExp = 0;
mEndExp = Mathf.Log(change);
mTickTimeSpeed += Time.deltaTime;
//Debug.Log(mTickTimeSpeed);
float currExp = Mathf.Lerp(mBeginExp, mEndExp, mTickTimeSpeed * 0.2f);
mNum += (int)Mathf.Exp(currExp);
IptNumShow.text = mNum.ToString();
ChangeEvent?.Invoke();
}
if(mStateReduce)
{
int change = mNum - mMinNum;
mBeginExp2 = 0;
mEndExp2 = Mathf.Log(change);
mTickTimeSpeed2 += Time.deltaTime;
//Debug.Log(mTickTimeSpeed);
float currExp = Mathf.Lerp(mBeginExp2, mEndExp2, mTickTimeSpeed2 * 0.2f);
mNum -= (int)Mathf.Exp(currExp);
IptNumShow.text = mNum.ToString();
ChangeEvent?.Invoke();
}
}
}