// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved.
using FlaxEditor.GUI;
using FlaxEngine;
namespace FlaxEditor.Surface.Elements
{
///
/// Combo box element.
///
///
///
[HideInEditor]
public class ComboBoxElement : ComboBox, ISurfaceNodeElement
{
///
/// True if automatic value selecting is active.
///
protected bool _isAutoSelect;
///
public SurfaceNode ParentNode { get; }
///
public NodeElementArchetype Archetype { get; }
///
/// Gets the surface.
///
public VisjectSurface Surface => ParentNode.Surface;
///
public ComboBoxElement(SurfaceNode parentNode, NodeElementArchetype archetype)
: base(archetype.ActualPositionX, archetype.ActualPositionY, archetype.Size.X)
{
ParentNode = parentNode;
Archetype = archetype;
// Check if combo box will use auto select feature
// Note: used provided items we should auto update saved node value on closed
_isAutoSelect = Archetype.Text != null;
if (Archetype.Text != null)
{
var items = Archetype.Text.Split('\n');
AddItems(items);
OnNodeValuesChanged();
ParentNode.ValuesChanged += OnNodeValuesChanged;
}
}
private void OnNodeValuesChanged()
{
_selectedIndices.Clear();
_selectedIndices.Add((int)ParentNode.Values[Archetype.ValueIndex]);
OnSelectedIndexChanged();
}
///
protected override void OnSelectedIndexChanged()
{
if (_isAutoSelect && (int)ParentNode.Values[Archetype.ValueIndex] != SelectedIndex)
{
// Edit value
ParentNode.SetValue(Archetype.ValueIndex, SelectedIndex);
}
base.OnSelectedIndexChanged();
}
}
}