//********************************** Banshee Engine (www.banshee3d.com) **************************************************//
//**************** Copyright (c) 2016 Marko Pintera (marko.pintera@gmail.com). All rights reserved. **********************//
using bs;
namespace bs.Editor
{
/** @addtogroup Inspector
* @{
*/
///
/// Displays GUI for a serializable property containing a 2D vector.
///
public class InspectableVector2 : InspectableField
{
private GUIVector2Field guiField;
private InspectableState state;
///
/// Creates a new inspectable 2D vector GUI for the specified property.
///
/// Context shared by all inspectable fields created by the same parent.
/// Name of the property, or some other value to set as the title.
/// Full path to this property (includes name of this property and all parent properties).
/// Determines how deep within the inspector nesting hierarchy is this field. Some fields may
/// contain other fields, in which case you should increase this value by one.
/// Parent layout that all the field elements will be added to.
/// Serializable property referencing the field whose contents to display.
public InspectableVector2(InspectableContext context, string title, string path, int depth, InspectableFieldLayout layout,
SerializableProperty property)
: base(context, title, path, SerializableProperty.FieldType.Vector2, depth, layout, property)
{
}
///
protected internal override void Initialize(int layoutIndex)
{
if (property.Type == SerializableProperty.FieldType.Vector2)
{
guiField = new GUIVector2Field(new GUIContent(title));
guiField.OnComponentChanged += OnFieldValueChanged;
guiField.OnConfirm += x =>
{
OnFieldValueConfirm();
StartUndo(x.ToString());
};
guiField.OnComponentFocusChanged += (focus, comp) =>
{
if(focus)
StartUndo(comp.ToString());
else
OnFieldValueConfirm();
};
layout.AddElement(layoutIndex, guiField);
}
}
///
public override InspectableState Refresh(int layoutIndex)
{
if (guiField != null && !guiField.HasInputFocus)
guiField.Value = property.GetValue();
InspectableState oldState = state;
if (state.HasFlag(InspectableState.Modified))
state = InspectableState.NotModified;
return oldState;
}
///
public override void SetHasFocus(string subFieldName = null)
{
if(subFieldName == "X")
guiField.SetInputFocus(VectorComponent.X, true);
else if(subFieldName == "Y")
guiField.SetInputFocus(VectorComponent.Y, true);
else
guiField.SetInputFocus(VectorComponent.X, true);
}
///
/// Triggered when the user changes the field value of a single component.
///
/// New value of a single component in the 3D vector field.
/// Component that was changed.
private void OnFieldValueChanged(float newValue, VectorComponent component)
{
property.SetValue(guiField.Value);
state |= InspectableState.ModifyInProgress;
}
///
/// Triggered when the user confirms input in the 2D vector field.
///
private void OnFieldValueConfirm()
{
if (state.HasFlag(InspectableState.ModifyInProgress))
state |= InspectableState.Modified;
EndUndo();
}
}
/** @} */
}