Refactor value editors to simplify code after many new features added
This commit is contained in:
@@ -4,7 +4,6 @@ using System;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using FlaxEditor.CustomEditors.Elements;
|
using FlaxEditor.CustomEditors.Elements;
|
||||||
using FlaxEngine;
|
using FlaxEngine;
|
||||||
using Utils = FlaxEngine.Utils;
|
|
||||||
|
|
||||||
namespace FlaxEditor.CustomEditors.Editors
|
namespace FlaxEditor.CustomEditors.Editors
|
||||||
{
|
{
|
||||||
@@ -22,50 +21,28 @@ namespace FlaxEditor.CustomEditors.Editors
|
|||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override void Initialize(LayoutElementsContainer layout)
|
public override void Initialize(LayoutElementsContainer layout)
|
||||||
{
|
{
|
||||||
_element = null;
|
var doubleValue = layout.DoubleValue();
|
||||||
|
doubleValue.ValueBox.ValueChanged += OnValueChanged;
|
||||||
// Try get limit attribute for value min/max range setting and slider speed
|
doubleValue.ValueBox.SlidingEnd += ClearToken;
|
||||||
|
_element = doubleValue;
|
||||||
var attributes = Values.GetAttributes();
|
var attributes = Values.GetAttributes();
|
||||||
var categoryAttribute = attributes.FirstOrDefault(x => x is ValueCategoryAttribute);
|
|
||||||
var valueCategory = ((ValueCategoryAttribute)categoryAttribute)?.Category ?? Utils.ValueCategory.None;
|
|
||||||
if (attributes != null)
|
if (attributes != null)
|
||||||
{
|
{
|
||||||
var limit = attributes.FirstOrDefault(x => x is LimitAttribute);
|
var limit = (LimitAttribute)attributes.FirstOrDefault(x => x is LimitAttribute);
|
||||||
if (limit != null)
|
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.SetCategory(valueCategory);
|
||||||
doubleValue.SetLimits((LimitAttribute)limit);
|
|
||||||
doubleValue.ValueBox.ValueChanged += OnValueChanged;
|
|
||||||
doubleValue.ValueBox.SlidingEnd += ClearToken;
|
|
||||||
_element = doubleValue;
|
|
||||||
LinkedLabel.SetupContextMenu += (label, menu, editor) =>
|
LinkedLabel.SetupContextMenu += (label, menu, editor) =>
|
||||||
{
|
{
|
||||||
menu.AddSeparator();
|
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.AutoCheck = true;
|
||||||
mb.Checked = doubleValue.ValueBox.Category != Utils.ValueCategory.None;
|
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()
|
private void OnValueChanged()
|
||||||
|
|||||||
@@ -3,7 +3,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using FlaxEditor.CustomEditors.Elements;
|
using FlaxEditor.CustomEditors.Elements;
|
||||||
using FlaxEditor.GUI.ContextMenu;
|
|
||||||
using FlaxEngine;
|
using FlaxEngine;
|
||||||
using Utils = FlaxEngine.Utils;
|
using Utils = FlaxEngine.Utils;
|
||||||
|
|
||||||
@@ -29,60 +28,40 @@ namespace FlaxEditor.CustomEditors.Editors
|
|||||||
public override void Initialize(LayoutElementsContainer layout)
|
public override void Initialize(LayoutElementsContainer layout)
|
||||||
{
|
{
|
||||||
_element = null;
|
_element = null;
|
||||||
|
|
||||||
// Try get limit attribute for value min/max range setting and slider speed
|
|
||||||
var attributes = Values.GetAttributes();
|
var attributes = Values.GetAttributes();
|
||||||
var categoryAttribute = attributes.FirstOrDefault(x => x is ValueCategoryAttribute);
|
var range = (RangeAttribute)attributes?.FirstOrDefault(x => x is RangeAttribute);
|
||||||
var valueCategory = ((ValueCategoryAttribute)categoryAttribute)?.Category ?? Utils.ValueCategory.None;
|
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)
|
if (attributes != null)
|
||||||
{
|
{
|
||||||
var range = attributes.FirstOrDefault(x => x is RangeAttribute);
|
var limit = (LimitAttribute)attributes.FirstOrDefault(x => x is LimitAttribute);
|
||||||
if (range != null)
|
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.SetCategory(valueCategory);
|
||||||
floatValue.ValueBox.ValueChanged += OnValueChanged;
|
|
||||||
floatValue.ValueBox.SlidingEnd += ClearToken;
|
|
||||||
_element = floatValue;
|
|
||||||
LinkedLabel.SetupContextMenu += (label, menu, editor) =>
|
LinkedLabel.SetupContextMenu += (label, menu, editor) =>
|
||||||
{
|
{
|
||||||
menu.AddSeparator();
|
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.AutoCheck = true;
|
||||||
mb.Checked = floatValue.ValueBox.Category != Utils.ValueCategory.None;
|
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()
|
private void OnValueChanged()
|
||||||
|
|||||||
Reference in New Issue
Block a user