Refactor value editors to simplify code after many new features added

This commit is contained in:
Wojtek Figat
2024-03-19 20:04:00 +01:00
parent 35d1d97840
commit bfbabbc395
2 changed files with 30 additions and 74 deletions

View File

@@ -4,7 +4,6 @@ using System;
using System.Linq;
using FlaxEditor.CustomEditors.Elements;
using FlaxEngine;
using Utils = FlaxEngine.Utils;
namespace FlaxEditor.CustomEditors.Editors
{
@@ -22,50 +21,28 @@ namespace FlaxEditor.CustomEditors.Editors
/// <inheritdoc />
public override void Initialize(LayoutElementsContainer layout)
{
_element = null;
// Try get limit attribute for value min/max range setting and slider speed
var doubleValue = layout.DoubleValue();
doubleValue.ValueBox.ValueChanged += OnValueChanged;
doubleValue.ValueBox.SlidingEnd += ClearToken;
_element = doubleValue;
var attributes = Values.GetAttributes();
var categoryAttribute = attributes.FirstOrDefault(x => x is ValueCategoryAttribute);
var valueCategory = ((ValueCategoryAttribute)categoryAttribute)?.Category ?? Utils.ValueCategory.None;
if (attributes != null)
{
var limit = attributes.FirstOrDefault(x => x is LimitAttribute);
if (limit != null)
var limit = (LimitAttribute)attributes.FirstOrDefault(x => x is LimitAttribute);
doubleValue.SetLimits(limit);
var valueCategory = ((ValueCategoryAttribute)attributes.FirstOrDefault(x => x is ValueCategoryAttribute))?.Category ?? Utils.ValueCategory.None;
if (valueCategory != Utils.ValueCategory.None)
{
// Use double value editor with limit
var doubleValue = layout.DoubleValue();
doubleValue.SetCategory(valueCategory);
doubleValue.SetLimits((LimitAttribute)limit);
doubleValue.ValueBox.ValueChanged += OnValueChanged;
doubleValue.ValueBox.SlidingEnd += ClearToken;
_element = doubleValue;
LinkedLabel.SetupContextMenu += (label, menu, editor) =>
{
menu.AddSeparator();
var mb = menu.AddButton("Show formatted", bt => { doubleValue.SetCategory(bt.Checked ? valueCategory : Utils.ValueCategory.None);});
var mb = menu.AddButton("Show formatted", bt => { doubleValue.SetCategory(bt.Checked ? valueCategory : Utils.ValueCategory.None); });
mb.AutoCheck = true;
mb.Checked = doubleValue.ValueBox.Category != Utils.ValueCategory.None;
};
return;
}
}
if (_element == null)
{
// Use double value editor
var doubleValue = layout.DoubleValue();
doubleValue.SetCategory(valueCategory);
doubleValue.ValueBox.ValueChanged += OnValueChanged;
doubleValue.ValueBox.SlidingEnd += ClearToken;
LinkedLabel.SetupContextMenu += (label, menu, editor) =>
{
menu.AddSeparator();
var mb = menu.AddButton("Show formatted", bt => { doubleValue.SetCategory(bt.Checked ? valueCategory : Utils.ValueCategory.None);});
mb.AutoCheck = true;
mb.Checked = doubleValue.ValueBox.Category != Utils.ValueCategory.None;
};
_element = doubleValue;
}
}
private void OnValueChanged()

View File

@@ -3,7 +3,6 @@
using System;
using System.Linq;
using FlaxEditor.CustomEditors.Elements;
using FlaxEditor.GUI.ContextMenu;
using FlaxEngine;
using Utils = FlaxEngine.Utils;
@@ -29,60 +28,40 @@ namespace FlaxEditor.CustomEditors.Editors
public override void Initialize(LayoutElementsContainer layout)
{
_element = null;
// Try get limit attribute for value min/max range setting and slider speed
var attributes = Values.GetAttributes();
var categoryAttribute = attributes.FirstOrDefault(x => x is ValueCategoryAttribute);
var valueCategory = ((ValueCategoryAttribute)categoryAttribute)?.Category ?? Utils.ValueCategory.None;
var range = (RangeAttribute)attributes?.FirstOrDefault(x => x is RangeAttribute);
if (range != null)
{
// Use slider
var slider = layout.Slider();
slider.Slider.SetLimits(range);
slider.Slider.ValueChanged += OnValueChanged;
slider.Slider.SlidingEnd += ClearToken;
_element = slider;
return;
}
var floatValue = layout.FloatValue();
floatValue.ValueBox.ValueChanged += OnValueChanged;
floatValue.ValueBox.SlidingEnd += ClearToken;
_element = floatValue;
if (attributes != null)
{
var range = attributes.FirstOrDefault(x => x is RangeAttribute);
if (range != null)
var limit = (LimitAttribute)attributes.FirstOrDefault(x => x is LimitAttribute);
floatValue.SetLimits(limit);
var valueCategory = ((ValueCategoryAttribute)attributes.FirstOrDefault(x => x is ValueCategoryAttribute))?.Category ?? Utils.ValueCategory.None;
if (valueCategory != Utils.ValueCategory.None)
{
// Use slider
var slider = layout.Slider();
slider.SetLimits((RangeAttribute)range);
slider.Slider.ValueChanged += OnValueChanged;
slider.Slider.SlidingEnd += ClearToken;
_element = slider;
return;
}
var limit = attributes.FirstOrDefault(x => x is LimitAttribute);
if (limit != null)
{
// Use float value editor with limit
var floatValue = layout.FloatValue();
floatValue.SetLimits((LimitAttribute)limit);
floatValue.SetCategory(valueCategory);
floatValue.ValueBox.ValueChanged += OnValueChanged;
floatValue.ValueBox.SlidingEnd += ClearToken;
_element = floatValue;
LinkedLabel.SetupContextMenu += (label, menu, editor) =>
{
menu.AddSeparator();
var mb = menu.AddButton("Show formatted", bt => { floatValue.SetCategory(bt.Checked ? valueCategory : Utils.ValueCategory.None);});
var mb = menu.AddButton("Show formatted", bt => { floatValue.SetCategory(bt.Checked ? valueCategory : Utils.ValueCategory.None); });
mb.AutoCheck = true;
mb.Checked = floatValue.ValueBox.Category != Utils.ValueCategory.None;
};
return;
}
}
if (_element == null)
{
// Use float value editor
var floatValue = layout.FloatValue();
floatValue.SetCategory(valueCategory);
floatValue.ValueBox.ValueChanged += OnValueChanged;
floatValue.ValueBox.SlidingEnd += ClearToken;
_element = floatValue;
LinkedLabel.SetupContextMenu += (label, menu, editor) =>
{
menu.AddSeparator();
var mb = menu.AddButton("Show formatted", bt => { floatValue.SetCategory(bt.Checked ? valueCategory : Utils.ValueCategory.None);});
mb.AutoCheck = true;
mb.Checked = floatValue.ValueBox.Category != Utils.ValueCategory.None;
};
}
}
private void OnValueChanged()