2022-04-02 18:15:09 +08:00

350 lines
12 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Assets.Scripts;
using Assets.Scripts.UI.Control;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
public class RowerTaskPanel : PFUIPanel
{
public class RowerType
{
/// <summary>
/// 1 里程 2 时间
/// </summary>
public int type { get; set; }
/// <summary>
/// 里程m 时间s
/// </summary>
public float value { get; set; }
}
Transform selector, distance, time;
public RowerType rowerType;
public Action<RowerType> callBack { get; set; }
protected override void Awake()
{
distance = transform.Find("Main/Distance");
time = transform.Find("Main/Time");
rowerType = new RowerType();
UIManager.AddEvent(transform.Find("Main/BtnGo").gameObject, UnityEngine.EventSystems.EventTriggerType.PointerClick, b =>
{
Go();
});
UIManager.AddEvent(transform.Find("Main/BtnCancel").gameObject, UnityEngine.EventSystems.EventTriggerType.PointerClick, b =>
{
Close();
});
UIManager.AddEvent(transform.Find("Main/BtnFreeMode").gameObject, UnityEngine.EventSystems.EventTriggerType.PointerClick, b =>
{
rowerType.type = 0;
rowerType.value = 0;
if (callBack != null)
{
callBack.Invoke(rowerType);
}
Close();
});
//var bg = transform.Find("Main/Time/Inputbg").gameObject;
//UIManager.AddEvent(bg, UnityEngine.EventSystems.EventTriggerType.PointerEnter, b =>
// {
// bg.GetComponent<Outline>().enabled = true;
// bg.GetComponent<Outline>().effectColor = Utils.HexToColorHtml("#353543");
// });
//UIManager.AddEvent(bg, UnityEngine.EventSystems.EventTriggerType.Select, b =>
//{
// bg.GetComponent<Outline>().enabled = true;
// bg.GetComponent<Outline>().effectColor = Utils.HexToColorHtml("#f93086");
//});
//UIManager.AddEvent(bg, UnityEngine.EventSystems.EventTriggerType.Deselect, b =>
//{
// bg.GetComponent<Outline>().enabled = false;
//});
formList = new List<object>
{
distance.Find("Btn500").GetComponent<Button>(),
distance.Find("Btn1000").GetComponent<Button>(),
distance.Find("Btn2000").GetComponent<Button>(),
distance.Find("Btn5000").GetComponent<Button>(),
distance.Find("Btn10000").GetComponent<Button>(),
distance.Find("Customize").GetComponent<PFUIInputField>(),
time.Find("Btn30").GetComponent<Button>(),
time.Find("CustomizeMin").GetComponent<PFUIInputField>(),
time.Find("CustomizeS").GetComponent<PFUIInputField>(),
};
InitDistance();
}
public override void Show()
{
base.Show();
InitAll();
InitValue();
}
private void Go()
{
if (rowerType.type == 0 || rowerType.value == 0)
{
Utils.showToast(null, "Please select a course.", isOnly:true);
return;
}
if (rowerType.type == 1 && rowerType.value < 100)
{
Utils.showToast(null, "Minimum mileage is 100M.", isOnly: true);
return;
}
if (rowerType.type == 2 && rowerType.value < 20)
{
Utils.showToast(null, "Minimum time is 20s.", isOnly: true);
return;
}
if (callBack != null)
{
callBack.Invoke(rowerType);
}
Close();
}
List<object> formList = null;
private void InitDistance()
{
Func<Transform, float, float, float> calc = (t, a, b) => t.parent.name == "Distance" ? a : a * 60 + b;
AddListener(formList, (t, v) =>
{
if (t.parent.name == "Distance")
{
rowerType.type = 1;
if (float.TryParse(v, out float f))
{
rowerType.value = f;
}
else
{
rowerType.value = 0;
}
}
else
{
rowerType.type = 2;
rowerType.value = 0;
var min = time.Find("CustomizeMin").GetComponent<InputField>().text;
var s = time.Find("CustomizeS").GetComponent<InputField>().text;
if (float.TryParse(min, out float a))
{
rowerType.value += 60 * a;
}
if (float.TryParse(s, out float b))
{
if (b < 0 || b > 59)
{
Utils.showToast(null, "Please enter an integer between 0-59.");
time.Find("CustomizeS").GetComponent<InputField>().text = "";
return;
}
rowerType.value += b;
}
}
}, calc);
}
private void AddListener(IEnumerable<object> list, UnityAction<Transform,string> inputCustomAction, Func<Transform, float,float,float> calcButton = null)
{
var btns = list.Where(x => x is Button).Select(x => x as Button);
foreach (var item in list)
{
if (item is Button)
{
var btn = item as Button;
btn.onClick.RemoveAllListeners();
btn.onClick.AddListener(() =>
{
InitAll();
rowerType.type = btn.transform.parent.name == "Distance" ? 1 : 2;
if (calcButton == null)
{
rowerType.value = float.Parse(btn.name.Replace("Btn", ""));
}
else
{
rowerType.value = calcButton.Invoke(btn.transform ,float.Parse(btn.name.Replace("Btn", "")), 0);
}
ClickDistanceBtn(btn, btns);
});
}
else if (item is PFUIInputField)
{
var input = item as PFUIInputField;
input.CustomSelect = () =>
{
string ignoreName = null;
if (input.name == "CustomizeMin")
{
ignoreName = "CustomizeS";
}
else if (input.name == "CustomizeS")
{
ignoreName = "CustomizeMin";
}
InitAll(ignoreName);
ClickDistanceBtn(null, btns);
rowerType.type = input.transform.parent.name == "Distance" ? 1 : 2;
SetTextColor(input.gameObject, true);
SetBgColor(input,2);
};
input.CustomDeSelect = () =>
{
SetTextColor(input.gameObject, false);
SetBgColor(input, 3);
};
input.CustomEnter = () =>
{
SetBgColor(input, 1);
};
if (inputCustomAction != null)
{
input.GetComponent<InputField>().onEndEdit.RemoveAllListeners();
input.GetComponent<InputField>().onEndEdit.AddListener((v) => inputCustomAction(input.transform, v));
}
}
}
}
Dictionary<string, int> inputStatusDict = null;
private void SetBgColor(PFUIInputField input, int v)
{
if (inputStatusDict == null)
{
inputStatusDict = new Dictionary<string, int>
{
{"CustomizeMin",3 },{"CustomizeS",3 }
};
}
if (!inputStatusDict.ContainsKey(input.name)) return;
//只要有一个selectenter 常亮 两个都exit,deselect 关掉
inputStatusDict[input.name] = v;
var bg = transform.Find("Main/Time/Inputbg").gameObject;
if (inputStatusDict.Count(x=>x.Value == 1) > 0)
{
bg.GetComponent<Outline>().enabled = true;
bg.GetComponent<Outline>().effectColor = Utils.HexToColorHtml("#6E6E7D");
}
else if (inputStatusDict.Count(x => x.Value == 2) > 0)
{
bg.GetComponent<Outline>().enabled = true;
bg.GetComponent<Outline>().effectColor = Utils.HexToColorHtml("#f93086");
}
else if(inputStatusDict.Count(x => x.Value == 3) == 2)
{
bg.GetComponent<Outline>().enabled = false;
}
}
public readonly Dictionary<int, List<double>> stdList = new Dictionary<int, List<double>>
{
{ 1,new List<double>() { 500,1000,2000,5000,10000 }},
{ 2,new List<double>() { 1800 }}
};
private void InitAll(string igName = null)
{
foreach (var item in formList)
{
if (item is Button)
{
var btn = item as Button;
if (btn.name == igName) continue;
SetButtonColor(btn, false);
}
else
{
var i = item as PFUIInputField;
if (i.name == igName) continue;
i.Text = "";
}
}
}
private void InitValue()
{
foreach (var item in formList)
{
if (item is Button)
{
var btn = item as Button;
var flag = (rowerType.type == 1 && string.Equals(rowerType.value.ToString(), btn.name.Replace("Btn", ""))) ||
(rowerType.type == 2 && rowerType.value == 1800 && btn.name == "Btn30");
SetButtonColor(btn, flag);
}
else
{
var i = item as PFUIInputField;
i.Text = "";
if (stdList.ContainsKey(rowerType.type) && !stdList[rowerType.type].Contains(rowerType.value))
{
if (rowerType.type == 1 && i.name == "Customize")
{
i.Text = rowerType.value.ToString();
}
else if (rowerType.type == 2)
{
var ts = TimeSpan.FromSeconds(rowerType.value);
if (i.name == "CustomizeMin")
{
i.Text = ts.Minutes.ToString();
}
else if (i.name == "CustomizeS")
{
i.Text = ts.Seconds.ToString();
}
}
}
}
}
}
private void ClickDistanceBtn(Button btn, IEnumerable<Button> list)
{
foreach (var item in list)
{
SetButtonColor(item, false);
}
if (btn)
{
SetButtonColor(btn, true);
}
}
private void SetButtonColor(Button btn, bool flag)
{
SetTextColor(btn.gameObject, flag);
btn.gameObject.GetComponent<Image>().color = flag ? Utils.HexToColorHtml("#f93086") : Utils.HexToColorHtml("#23232d");
}
private void SetTextColor(GameObject btn, bool flag)
{
if (btn.transform.Find("t1"))
{
btn.transform.Find("t1").GetComponent<Text>().color = flag ? Utils.HexToColorHtml("#ffffff") : Utils.HexToColorHtml("#9E9EAD");
}
if (btn.transform.Find("t2"))
{
btn.transform.Find("t2").GetComponent<Text>().color = flag ? Utils.HexToColorHtml("#ffffff") : Utils.HexToColorHtml("#9E9EAD");
}
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}