2020-07-21 07:07:18 +00:00
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
|
|
|
namespace ConformalDecals.UI {
|
|
|
|
public class ColorBoxSlider : MonoBehaviour {
|
2020-07-23 05:37:16 +00:00
|
|
|
[SerializeField] public ColorPickerController.SVUpdateEvent onValueChanged = new ColorPickerController.SVUpdateEvent();
|
2020-07-21 07:07:18 +00:00
|
|
|
|
|
|
|
[SerializeField] private Vector2 _value;
|
|
|
|
|
|
|
|
[SerializeField] private BoxSlider _slider;
|
|
|
|
[SerializeField] private Image _image;
|
2020-07-23 05:37:16 +00:00
|
|
|
|
|
|
|
private bool _ignoreUpdates;
|
2020-07-21 07:07:18 +00:00
|
|
|
|
|
|
|
public Vector2 Value {
|
|
|
|
get => _value;
|
|
|
|
set {
|
|
|
|
_value.x = Mathf.Clamp01(value.x);
|
|
|
|
_value.y = Mathf.Clamp01(value.y);
|
|
|
|
UpdateSlider();
|
2020-07-23 05:37:16 +00:00
|
|
|
UpdateChannels();
|
2020-07-21 07:07:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-23 05:37:16 +00:00
|
|
|
public void OnColorUpdate(Color rgb, Util.ColorHSV hsv) {
|
|
|
|
if (_ignoreUpdates) return;
|
|
|
|
|
|
|
|
_image.material.SetColor(PropertyIDs._Color, (Vector4) hsv);
|
|
|
|
|
|
|
|
_value.x = hsv.s;
|
|
|
|
_value.y = hsv.v;
|
|
|
|
UpdateSlider();
|
|
|
|
}
|
|
|
|
|
2020-07-21 07:07:18 +00:00
|
|
|
public void OnSliderUpdate(Vector2 value) {
|
2020-07-23 05:37:16 +00:00
|
|
|
if (_ignoreUpdates) return;
|
|
|
|
|
2020-07-21 07:07:18 +00:00
|
|
|
_value = value;
|
2020-07-23 05:37:16 +00:00
|
|
|
UpdateChannels();
|
2020-07-21 07:07:18 +00:00
|
|
|
}
|
|
|
|
|
2020-07-23 05:37:16 +00:00
|
|
|
private void Awake() {
|
|
|
|
var boxSlider = gameObject.GetComponentInChildren<BoxSlider>();
|
|
|
|
boxSlider.OnValueChanged.AddListener(OnSliderUpdate);
|
2020-07-21 07:07:18 +00:00
|
|
|
}
|
|
|
|
|
2020-07-23 05:37:16 +00:00
|
|
|
private void UpdateChannels() {
|
|
|
|
_ignoreUpdates = true;
|
2020-07-21 07:52:23 +00:00
|
|
|
|
2020-07-23 05:37:16 +00:00
|
|
|
onValueChanged.Invoke(_value);
|
|
|
|
|
|
|
|
_ignoreUpdates = false;
|
2020-07-21 07:07:18 +00:00
|
|
|
}
|
2020-07-23 05:37:16 +00:00
|
|
|
|
|
|
|
private void UpdateSlider() {
|
|
|
|
_ignoreUpdates = true;
|
|
|
|
|
2020-07-21 07:07:18 +00:00
|
|
|
_slider.Value = _value;
|
2020-07-23 05:37:16 +00:00
|
|
|
|
|
|
|
_ignoreUpdates = false;
|
2020-07-21 07:07:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|