// Copyright (c) Wojciech Figat. All rights reserved.
using FlaxEditor.GUI;
using FlaxEngine;
using FlaxEngine.GUI;
namespace FlaxEditor.CustomEditors.Dedicated
{
///
/// Custom editor for .
///
class BezierCurveObjectEditor : CustomEditor where T : struct
{
private bool _isSetting;
private int _firstTimeShow;
private BezierCurveEditor _curve;
private Splitter _splitter;
private string _heightCachedPath;
///
public override void Initialize(LayoutElementsContainer layout)
{
var item = layout.CustomContainer>();
_curve = item.CustomControl;
var height = 120.0f;
var presenter = Presenter;
if (presenter != null && (presenter.Features & FeatureFlags.CacheExpandedGroups) != 0)
{
// Try to restore curve height
_heightCachedPath = layout.GetLayoutCachePath("Height");
if (Editor.Instance.ProjectCache.TryGetCustomData(_heightCachedPath, out float cachedHeight) && cachedHeight > 10.0f)
height = cachedHeight;
}
_curve.Height = height;
_curve.Edited += OnCurveEdited;
_firstTimeShow = 4; // For some weird reason it needs several frames of warmup (probably due to sliders smoothing)
_splitter = new Splitter
{
Moved = OnSplitterMoved,
Parent = _curve,
AnchorPreset = AnchorPresets.HorizontalStretchBottom,
Bounds = new Rectangle(0, _curve.Height - Splitter.DefaultHeight, _curve.Width, Splitter.DefaultHeight),
};
}
private void OnCurveEdited()
{
if (_isSetting)
return;
_isSetting = true;
SetValue(new BezierCurve(_curve.Keyframes));
_isSetting = false;
}
private void OnSplitterMoved(Float2 location)
{
_curve.Height = Mathf.Clamp(_splitter.PointToParent(location).Y, 50.0f, 1000.0f);
// Cache curve height
if (_heightCachedPath != null)
Editor.Instance.ProjectCache.SetCustomData(_heightCachedPath, _curve.Height);
}
///
public override void Refresh()
{
base.Refresh();
var value = (BezierCurve)Values[0];
if (value != null && !_curve.IsUserEditing && !Utils.ArraysEqual(value.Keyframes, _curve.Keyframes))
{
_isSetting = true;
_curve.SetKeyframes(value.Keyframes);
_isSetting = false;
}
if (_firstTimeShow-- > 0)
_curve.ShowWholeCurve();
}
///
protected override void Deinitialize()
{
_curve = null;
_splitter = null;
base.Deinitialize();
}
}
[CustomEditor(typeof(BezierCurve)), DefaultEditor]
sealed class IntBezierCurveObjectEditor : BezierCurveObjectEditor
{
}
[CustomEditor(typeof(BezierCurve)), DefaultEditor]
sealed class FloatBezierCurveObjectEditor : BezierCurveObjectEditor
{
}
[CustomEditor(typeof(BezierCurve)), DefaultEditor]
sealed class Vector2BezierCurveObjectEditor : BezierCurveObjectEditor
{
}
[CustomEditor(typeof(BezierCurve)), DefaultEditor]
sealed class Vector3BezierCurveObjectEditor : BezierCurveObjectEditor
{
}
[CustomEditor(typeof(BezierCurve)), DefaultEditor]
sealed class Vector4BezierCurveObjectEditor : BezierCurveObjectEditor
{
}
[CustomEditor(typeof(BezierCurve)), DefaultEditor]
sealed class Float2BezierCurveObjectEditor : BezierCurveObjectEditor
{
}
[CustomEditor(typeof(BezierCurve)), DefaultEditor]
sealed class Float3BezierCurveObjectEditor : BezierCurveObjectEditor
{
}
[CustomEditor(typeof(BezierCurve)), DefaultEditor]
sealed class Float4BezierCurveObjectEditor : BezierCurveObjectEditor
{
}
[CustomEditor(typeof(BezierCurve)), DefaultEditor]
sealed class QuaternionBezierCurveObjectEditor : BezierCurveObjectEditor
{
}
[CustomEditor(typeof(BezierCurve)), DefaultEditor]
sealed class ColorBezierCurveObjectEditor : BezierCurveObjectEditor
{
}
///
/// Custom editor for .
///
class LinearCurveObjectEditor : CustomEditor where T : struct
{
private bool _isSetting;
private int _firstTimeShow;
private LinearCurveEditor _curve;
private Splitter _splitter;
private string _heightCachedPath;
///
public override void Initialize(LayoutElementsContainer layout)
{
var item = layout.CustomContainer>();
_curve = item.CustomControl;
var height = 120.0f;
var presenter = Presenter;
if (presenter != null && (presenter.Features & FeatureFlags.CacheExpandedGroups) != 0)
{
// Try to restore curve height
_heightCachedPath = layout.GetLayoutCachePath("Height");
if (Editor.Instance.ProjectCache.TryGetCustomData(_heightCachedPath, out float cachedHeight) && cachedHeight > 10.0f)
height = cachedHeight;
}
_curve.Height = height;
_curve.Edited += OnCurveEdited;
_firstTimeShow = 4; // For some weird reason it needs several frames of warmup (probably due to sliders smoothing)
_splitter = new Splitter
{
Moved = OnSplitterMoved,
Parent = _curve,
AnchorPreset = AnchorPresets.HorizontalStretchBottom,
Bounds = new Rectangle(0, _curve.Height - Splitter.DefaultHeight, _curve.Width, Splitter.DefaultHeight),
};
}
private void OnCurveEdited()
{
if (_isSetting)
return;
_isSetting = true;
SetValue(new LinearCurve(_curve.Keyframes));
_isSetting = false;
}
private void OnSplitterMoved(Float2 location)
{
_curve.Height = Mathf.Clamp(_splitter.PointToParent(location).Y, 50.0f, 1000.0f);
// Cache curve height
if (_heightCachedPath != null)
Editor.Instance.ProjectCache.SetCustomData(_heightCachedPath, _curve.Height);
}
///
public override void Refresh()
{
base.Refresh();
var value = (LinearCurve)Values[0];
if (value != null && !_curve.IsUserEditing && !Utils.ArraysEqual(value.Keyframes, _curve.Keyframes))
{
_isSetting = true;
_curve.SetKeyframes(value.Keyframes);
_isSetting = false;
}
if (_firstTimeShow-- > 0)
_curve.ShowWholeCurve();
}
///
protected override void Deinitialize()
{
_curve = null;
_splitter = null;
base.Deinitialize();
}
}
[CustomEditor(typeof(LinearCurve)), DefaultEditor]
sealed class IntLinearCurveObjectEditor : LinearCurveObjectEditor
{
}
[CustomEditor(typeof(LinearCurve)), DefaultEditor]
sealed class FloatLinearCurveObjectEditor : LinearCurveObjectEditor
{
}
[CustomEditor(typeof(LinearCurve)), DefaultEditor]
sealed class Vector2LinearCurveObjectEditor : LinearCurveObjectEditor
{
}
[CustomEditor(typeof(LinearCurve)), DefaultEditor]
sealed class Vector3LinearCurveObjectEditor : LinearCurveObjectEditor
{
}
[CustomEditor(typeof(LinearCurve)), DefaultEditor]
sealed class Vector4LinearCurveObjectEditor : LinearCurveObjectEditor
{
}
[CustomEditor(typeof(LinearCurve)), DefaultEditor]
sealed class Float2LinearCurveObjectEditor : LinearCurveObjectEditor
{
}
[CustomEditor(typeof(LinearCurve)), DefaultEditor]
sealed class Float3LinearCurveObjectEditor : LinearCurveObjectEditor
{
}
[CustomEditor(typeof(LinearCurve)), DefaultEditor]
sealed class Float4LinearCurveObjectEditor : LinearCurveObjectEditor
{
}
[CustomEditor(typeof(LinearCurve)), DefaultEditor]
sealed class QuaternionLinearCurveObjectEditor : LinearCurveObjectEditor
{
}
[CustomEditor(typeof(LinearCurve)), DefaultEditor]
sealed class ColorLinearCurveObjectEditor : LinearCurveObjectEditor
{
}
}