mirror of
https://github.com/drewcassidy/KSP-Conformal-Decals.git
synced 2024-06-11 05:54:07 +00:00
81 lines
2.3 KiB
C#
81 lines
2.3 KiB
C#
|
using ConformalDecals.Util;
|
||
|
using TMPro;
|
||
|
using UnityEngine;
|
||
|
using UnityEngine.UI;
|
||
|
|
||
|
namespace ConformalDecals.UI {
|
||
|
public class ColorChannelSlider : MonoBehaviour {
|
||
|
[SerializeField] private ColorPickerController.ChannelUpdateEvent _onChannelChanged = new ColorPickerController.ChannelUpdateEvent();
|
||
|
|
||
|
[SerializeField] private float _value;
|
||
|
[SerializeField] private int _channel;
|
||
|
[SerializeField] private bool _hsl;
|
||
|
|
||
|
[SerializeField] private Selectable _textBox;
|
||
|
[SerializeField] private Slider _slider;
|
||
|
[SerializeField] private Image _image;
|
||
|
|
||
|
private Material _imageMaterial;
|
||
|
|
||
|
private bool _ignoreUpdates;
|
||
|
|
||
|
public float Value {
|
||
|
get => _value;
|
||
|
set {
|
||
|
_value = Mathf.Clamp01(value);
|
||
|
UpdateSlider();
|
||
|
UpdateTextbox();
|
||
|
OnChannelUpdate();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void Awake() {
|
||
|
_imageMaterial = _image.material;
|
||
|
}
|
||
|
|
||
|
public void OnTextBoxUpdate(string text) {
|
||
|
if (_ignoreUpdates) return;
|
||
|
|
||
|
if (byte.TryParse(text, out byte byteValue)) {
|
||
|
_value = (float) byteValue / 255;
|
||
|
UpdateSlider();
|
||
|
OnChannelUpdate();
|
||
|
}
|
||
|
else {
|
||
|
// value is invalid, reset value
|
||
|
UpdateTextbox();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void OnSliderUpdate(float value) {
|
||
|
if (_ignoreUpdates) return;
|
||
|
|
||
|
_value = value;
|
||
|
UpdateTextbox();
|
||
|
OnChannelUpdate();
|
||
|
}
|
||
|
|
||
|
public void OnChannelUpdate() {
|
||
|
_onChannelChanged.Invoke(_value, _channel, _hsl);
|
||
|
}
|
||
|
|
||
|
public void OnColorUpdate(Color rgb, ColorHSL hsl) {
|
||
|
_imageMaterial.SetColor(PropertyIDs._Color, rgb);
|
||
|
Value = _hsl ? hsl[_channel] : rgb[_channel];
|
||
|
}
|
||
|
|
||
|
public void UpdateSlider() {
|
||
|
_ignoreUpdates = true;
|
||
|
_slider.value = _value;
|
||
|
_ignoreUpdates = false;
|
||
|
}
|
||
|
|
||
|
public void UpdateTextbox() {
|
||
|
if (_textBox == null) return;
|
||
|
|
||
|
_ignoreUpdates = true;
|
||
|
((TMP_InputField) _textBox).text = ((byte) (255 * _value)).ToString();
|
||
|
_ignoreUpdates = false;
|
||
|
}
|
||
|
}
|
||
|
}
|