123 lines
3.6 KiB
C#
Raw Normal View History

using Assets.Scripts;
using System;
using System.Collections;
2021-04-21 11:19:31 +08:00
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
2021-04-21 11:19:31 +08:00
public class PFUISlider : MonoBehaviour
{
private Text text;
private Slider slider;
public System.Func<float, float> valueHandler = (a) => a * 100;
private List<Color> colorGradientList;
2021-04-21 11:19:31 +08:00
private void Awake()
{
text = transform.Find("Text").GetComponent<Text>();
slider = transform.GetComponent<Slider>();
//text.text = $"{(slider.value * 100).ToString("#0")}";
//slider.onValueChanged.AddListener((f) =>
//{
// text.text = $"{valueHandler(f).ToString("#0")}";
//});
colorGradientList = SetColorGradient(new List<Color>
{
Utils.HexToColorHtml("#ffffff"),
Utils.HexToColorHtml("#27DFE3"),
Utils.HexToColorHtml("#27E363"),
Utils.HexToColorHtml("#E3D427"),
Utils.HexToColorHtml("#E33B27"),
},
new List<int>
2021-04-21 11:19:31 +08:00
{
50,50,100,100
2021-04-21 11:19:31 +08:00
});
}
public bool runCallback = false;
2022-02-10 12:57:43 +08:00
public void SetValueChanged(System.Action<float> a)
{
2022-06-14 18:37:58 +08:00
this.ValueChangedHandler = a;
2022-02-10 12:57:43 +08:00
slider = transform.GetComponent<Slider>();
slider.onValueChanged.RemoveAllListeners();
2022-06-14 18:37:58 +08:00
UIManager.AddEvent(slider.gameObject, EventTriggerType.EndDrag, (e) =>
{
OnValueChanged();
});
2022-02-10 12:57:43 +08:00
slider.onValueChanged.AddListener((f) =>
{
2022-06-14 18:37:58 +08:00
current = f;
2022-02-10 12:57:43 +08:00
});
}
2022-06-14 18:37:58 +08:00
float current;
System.Action<float> ValueChangedHandler;
private void OnValueChanged()
{
float f = current;
var step = 1f / (colorGradientList.Count - 1);
var index = (int)Math.Round(f / step, 0);
slider.targetGraphic.color = colorGradientList[index];
text.text = $"{valueHandler(f):#0}";
this.ValueChangedHandler?.Invoke(f);
}
public void SetValue(float a)
{
2022-06-13 18:59:02 +08:00
a = a > 1 ? 1 : a;
a = a < 0 ? 0 : a;
slider.value = a;
2022-06-14 18:37:58 +08:00
OnValueChanged();
2022-06-10 19:31:07 +08:00
}
public float GetValue()
{
return slider.value;
}
public List<Color> SetColorGradient(List<Color> list, int divideCount)
{
var res = new List<Color>();
for (int i = 0; i < list.Count - 1; i++)
{
Color c = list[i], nc = list[i + 1];
var diff = new Tuple<float, float, float>((nc.r - c.r) / divideCount, (nc.g - c.g) / divideCount, (nc.b - c.b) / divideCount);
for (int j = 0; j < divideCount; j++)
{
res.Add(new Color(c.r + diff.Item1 * j, c.g + diff.Item2 * j, c.b + diff.Item3 * j));
}
}
return res;
}
public List<Color> SetColorGradient(List<Color> list, List<int> divideList)
{
if (list.Count != divideList.Count+1) return null;
var res = new List<Color>();
for (int i = 0; i < list.Count - 1; i++)
{
Color c = list[i], nc = list[i + 1];
var divideCount = divideList[i];
var diff = new Tuple<float, float, float>((nc.r - c.r) / divideCount, (nc.g - c.g) / divideCount, (nc.b - c.b) / divideCount);
for (int j = 0; j < divideCount; j++)
{
res.Add(new Color(c.r + diff.Item1 * j, c.g + diff.Item2 * j, c.b + diff.Item3 * j));
}
}
return res;
}
2021-04-21 11:19:31 +08:00
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}