UsoUIElements 0.1.0
Data Centric implimentation of Unity's UI Elements (UI Toolkit)
Loading...
Searching...
No Matches
GWG.UsoUIElements.Editor.UsoObjectField Class Reference

A custom UI element that wraps Unity's ObjectField with enhanced functionality for the Uso UI system. This element provides field validation, data binding support, and standardized behavior consistent with other Uso UI elements. More...

Inheritance diagram for GWG.UsoUIElements.Editor.UsoObjectField:

Public Member Functions

 UsoObjectField ()
 Initializes a new instance of the UsoObjectField with default configuration. Creates an ObjectField with no initial label or binding configuration.
 UsoObjectField (string fieldName, string labelText)
 Initializes a new instance of the UsoObjectField with a field name and label text. Creates an ObjectField with both an element name for identification and a user-visible label.
 UsoObjectField (string fieldName, string labelText, out string newFieldName)
 Initializes a new instance of the UsoObjectField with a field name and label text, returning the assigned name. Creates an ObjectField and provides the actual assigned field name through an out parameter.
 UsoObjectField (string fieldName, string labelText, string bindingPath, BindingMode bindingMode, out string newFieldName)
 Initializes a new instance of the UsoObjectField with full configuration including name verification and data binding. Creates an ObjectField with complete setup and returns the assigned field name for confirmation.
 UsoObjectField (string fieldName, string labelText, string bindingPath, BindingMode bindingMode=BindingMode.ToTarget)
 Initializes a new instance of the UsoObjectField with field name, label, and data binding configuration. Creates an ObjectField with full configuration including automatic data synchronization.
 UsoObjectField (string labelText)
 Initializes a new instance of the UsoObjectField with a specified label text. Creates an ObjectField with the provided label displayed to the user.
void ApplyBinding (string fieldBindingProp, string fieldBindingPath, BindingMode fieldBindingMode)
 Applies data binding to the internal ObjectField using the specified property name, path, and mode. This method enables two-way data binding between the ObjectField and a data source.
UsoLineItem GetParentLineItem ()
 Retrieves the first ancestor UsoLineItem element in the visual hierarchy. This method enables integration with parent line item containers for coordinated behavior.
void InitElement (string fieldName=null)
 Initializes the internal ObjectField and configures the element with optional field name and styling. This method sets up the core functionality and event handling for the ObjectField wrapper.
void SetFieldStatus (FieldStatusTypes fieldStatus)
 Sets the field validation status to the specified value. This method provides external access to update the validation state of the element.
void SetValueWithoutNotify (Object newValue)
 Sets the value of the ObjectField without triggering change notifications or events. Updates the internal ObjectField value directly without propagating change events.
void ShowFieldStatus (bool status)
 Shows or hides the field validation status display based on the specified flag. This method controls whether validation visual feedback is active for the element.
Public Member Functions inherited from GWG.UsoUIElements.IUsoUiElement
void AddToClassList (string className)
 Adds the specified CSS class name to this element's class list. This method is inherited from Unity's VisualElement and enables dynamic styling modifications.
void ClearBindings ()
 Removes all data binding configurations from this element. This method is inherited from Unity's VisualElement and provides cleanup functionality for data bindings.
void RemoveFromClassList (string className)
 Removes the specified CSS class name from this element's class list. This method is inherited from Unity's VisualElement and provides dynamic styling capabilities.

Properties

bool allowSceneObjects [get, set]
 Gets or sets whether scene objects can be assigned to this ObjectField. When false, only asset objects from the project can be assigned.
FieldStatusTypes FieldStatus [get, private set]
 Gets or sets the current field validation status for visual feedback. This property controls the validation state styling of the element.
bool FieldStatusEnabled [get, private set]
 Gets or sets whether field validation status display is enabled for this element. When enabled, the element can show validation states through CSS class changes.
Type objectType [get, set]
 Gets or sets the type of objects that can be assigned to this ObjectField. This property determines what types of Unity objects are valid for selection.
Object value [get, set]
 Gets or sets the current Object value of the ObjectField. Setting this property triggers change events and updates the field display.

Private Member Functions

 ~UsoObjectField ()
 Finalizer that ensures proper cleanup of event callbacks when the element is destroyed. Unregisters the value change callback from the internal ObjectField to prevent memory leaks.
void OnObjectFieldValueChanged (ChangeEvent< Object > evt)
 Handles value changes from the internal ObjectField and updates the wrapper element's value accordingly. This method is called when the user selects a different object through the ObjectField interface.

Private Attributes

FieldStatusTypes _fieldStatus
 The internal storage for the field status value.
bool _fieldStatusEnabled = true
 The internal storage for the field status enabled flag.
ObjectField _objectField
 The internal ObjectField that handles the actual object selection functionality.
Object _value
 The internal storage for the selected object value.

Static Private Attributes

const string DefaultBindProp = "value"
 The default property name used for data binding operations.
const string ElementStylesheet = "uso-object-field"
 The CSS class name used for general Uso UI element styling.
const string ElementValidationClass = "uso-field-validation"
 The CSS class name applied when field validation is enabled.

Detailed Description

A custom UI element that wraps Unity's ObjectField with enhanced functionality for the Uso UI system. This element provides field validation, data binding support, and standardized behavior consistent with other Uso UI elements.

The UsoObjectField extends Unity's standard ObjectField with additional features required by the Uso UI framework:

  • Integrated field validation with visual status indicators
  • Data binding capabilities for automatic synchronization with data sources
  • Consistent API with other Uso UI elements through the IUsoUiElement interface
  • Multiple constructor overloads for different initialization scenarios
  • Support for both scene and asset object selection

This element is designed for use in Unity Editor contexts where object reference selection is required with enhanced validation and binding capabilities beyond the standard ObjectField. The element maintains full compatibility with Unity's serialization and Inspector systems.

Constructor & Destructor Documentation

◆ UsoObjectField() [1/6]

GWG.UsoUIElements.Editor.UsoObjectField.UsoObjectField ( )

Initializes a new instance of the UsoObjectField with default configuration. Creates an ObjectField with no initial label or binding configuration.

This parameterless constructor creates the most basic ObjectField configuration. Additional properties like objectType, label, and bindings must be set after construction. This constructor is suitable for programmatic creation where configuration will be applied separately.

◆ UsoObjectField() [2/6]

GWG.UsoUIElements.Editor.UsoObjectField.UsoObjectField ( string labelText)

Initializes a new instance of the UsoObjectField with a specified label text. Creates an ObjectField with the provided label displayed to the user.

Parameters
labelTextThe text to display as the field label.

This constructor is convenient for creating labeled ObjectFields in programmatic UI construction. The label helps users understand what type of object should be assigned to the field.

◆ UsoObjectField() [3/6]

GWG.UsoUIElements.Editor.UsoObjectField.UsoObjectField ( string fieldName,
string labelText )

Initializes a new instance of the UsoObjectField with a field name and label text. Creates an ObjectField with both an element name for identification and a user-visible label.

Parameters
fieldNameThe name to assign to this element for identification purposes.
labelTextThe text to display as the field label.

This constructor is useful when you need both programmatic identification (through the name) and user-friendly labeling. The field name can be used for element queries and identification while the label text provides clear user guidance.

◆ UsoObjectField() [4/6]

GWG.UsoUIElements.Editor.UsoObjectField.UsoObjectField ( string fieldName,
string labelText,
out string newFieldName )

Initializes a new instance of the UsoObjectField with a field name and label text, returning the assigned name. Creates an ObjectField and provides the actual assigned field name through an out parameter.

Parameters
fieldNameThe desired name to assign to this element for identification purposes.
labelTextThe text to display as the field label.
newFieldNameReturns the actual field name that was assigned to the element.

This constructor variant allows the caller to verify the actual field name that was assigned, which is useful in scenarios where name conflicts might occur or where the assigned name needs to be stored for later reference. The out parameter provides the confirmed element name.

◆ UsoObjectField() [5/6]

GWG.UsoUIElements.Editor.UsoObjectField.UsoObjectField ( string fieldName,
string labelText,
string bindingPath,
BindingMode bindingMode = BindingMode::ToTarget )

Initializes a new instance of the UsoObjectField with field name, label, and data binding configuration. Creates an ObjectField with full configuration including automatic data synchronization.

Parameters
fieldNameThe name to assign to this element for identification purposes.
labelTextThe text to display as the field label.
bindingPathThe path to the property in the data source for binding.
bindingModeThe binding mode controlling data synchronization behavior. Default is ToTarget.

This constructor provides complete ObjectField setup in a single call, including data binding. It's ideal for creating ObjectFields that need to automatically synchronize with data sources without requiring separate binding configuration. The default binding mode of ToTarget provides one-way data flow from the data source to the UI element.

◆ UsoObjectField() [6/6]

GWG.UsoUIElements.Editor.UsoObjectField.UsoObjectField ( string fieldName,
string labelText,
string bindingPath,
BindingMode bindingMode,
out string newFieldName )

Initializes a new instance of the UsoObjectField with full configuration including name verification and data binding. Creates an ObjectField with complete setup and returns the assigned field name for confirmation.

Parameters
fieldNameThe desired name to assign to this element for identification purposes.
labelTextThe text to display as the field label.
bindingPathThe path to the property in the data source for binding.
bindingModeThe binding mode controlling data synchronization behavior.
newFieldNameReturns the actual field name that was assigned to the element.

This constructor provides the most comprehensive ObjectField initialization, including full configuration with data binding and field name confirmation. It's suitable for scenarios where you need complete control over the ObjectField setup and want to verify the assigned element name for later reference.

◆ ~UsoObjectField()

GWG.UsoUIElements.Editor.UsoObjectField.~UsoObjectField ( )
private

Finalizer that ensures proper cleanup of event callbacks when the element is destroyed. Unregisters the value change callback from the internal ObjectField to prevent memory leaks.

This finalizer is important for preventing memory leaks by ensuring that event callbacks are properly unregistered when the element is garbage collected. It specifically removes the OnObjectFieldValueChanged callback that was registered during initialization.

Member Function Documentation

◆ ApplyBinding()

void GWG.UsoUIElements.Editor.UsoObjectField.ApplyBinding ( string fieldBindingProp,
string fieldBindingPath,
BindingMode fieldBindingMode )

Applies data binding to the internal ObjectField using the specified property name, path, and mode. This method enables two-way data binding between the ObjectField and a data source.

Parameters
fieldBindingPropThe name of the property to bind to (typically "value").
fieldBindingPathThe path to the property in the data source.
fieldBindingModeThe binding mode (OneWay, TwoWay, etc.).
Exceptions
ExceptionThrown when binding setup fails, with the original exception preserved.

This method implements the IUsoUiElement interface requirement for binding support. It applies the binding to the internal ObjectField rather than the wrapper element to ensure the binding works with the actual input control. Any exceptions during binding setup are caught, logged to console, and re-thrown for proper error handling.

Implements GWG.UsoUIElements.IUsoUiElement.

◆ GetParentLineItem()

UsoLineItem GWG.UsoUIElements.Editor.UsoObjectField.GetParentLineItem ( )

Retrieves the first ancestor UsoLineItem element in the visual hierarchy. This method enables integration with parent line item containers for coordinated behavior.

Returns
The first UsoLineItem ancestor found, or null if none exists.

This method implements the IUsoUiElement interface requirement for parent line item access. It searches up the visual tree to find the containing line item, enabling coordinated validation and behavior.

◆ InitElement()

void GWG.UsoUIElements.Editor.UsoObjectField.InitElement ( string fieldName = null)

Initializes the internal ObjectField and configures the element with optional field name and styling. This method sets up the core functionality and event handling for the ObjectField wrapper.

Parameters
fieldNameOptional name to assign to this element for identification purposes.

This method creates the internal ObjectField, registers the value change callback, adds it to the element hierarchy, applies the base CSS class for styling, and ensures field status validation is properly configured. The method is called by all constructors to ensure consistent initialization regardless of construction parameters.

Implements GWG.UsoUIElements.IUsoUiElement.

◆ OnObjectFieldValueChanged()

void GWG.UsoUIElements.Editor.UsoObjectField.OnObjectFieldValueChanged ( ChangeEvent< Object > evt)
private

Handles value changes from the internal ObjectField and updates the wrapper element's value accordingly. This method is called when the user selects a different object through the ObjectField interface.

Parameters
evtThe change event containing the old and new object values from the internal ObjectField.

This callback ensures that changes in the internal ObjectField are properly propagated to the wrapper element's value property, which will trigger the INotifyValueChanged implementation and send appropriate change events. It acts as a bridge between the internal ObjectField's change events and the wrapper's change notification system.

◆ SetFieldStatus()

void GWG.UsoUIElements.Editor.UsoObjectField.SetFieldStatus ( FieldStatusTypes fieldStatus)

Sets the field validation status to the specified value. This method provides external access to update the validation state of the element.

Parameters
fieldStatusThe new field status to apply.

This method implements the IUsoUiElement interface requirement for status management. It updates the FieldStatus property, which automatically triggers visual styling changes.

Implements GWG.UsoUIElements.IUsoUiElement.

◆ SetValueWithoutNotify()

void GWG.UsoUIElements.Editor.UsoObjectField.SetValueWithoutNotify ( Object newValue)

Sets the value of the ObjectField without triggering change notifications or events. Updates the internal ObjectField value directly without propagating change events.

Parameters
newValueThe new Object value to set, or null to clear the selection.

This method is used internally to update the ObjectField's state without triggering change events, which is important for avoiding infinite loops during value synchronization and data binding operations. It delegates directly to the internal ObjectField's SetValueWithoutNotify method.

◆ ShowFieldStatus()

void GWG.UsoUIElements.Editor.UsoObjectField.ShowFieldStatus ( bool status)

Shows or hides the field validation status display based on the specified flag. This method controls whether validation visual feedback is active for the element.

Parameters
statusTrue to show field status validation; false to hide it.

This method implements the IUsoUiElement interface requirement for status visibility control. It updates the FieldStatusEnabled property, which automatically manages the validation CSS class.

Implements GWG.UsoUIElements.IUsoUiElement.

Member Data Documentation

◆ _fieldStatus

FieldStatusTypes GWG.UsoUIElements.Editor.UsoObjectField._fieldStatus
private

The internal storage for the field status value.

This field stores the actual validation status and is used by the FieldStatus property.

◆ _fieldStatusEnabled

bool GWG.UsoUIElements.Editor.UsoObjectField._fieldStatusEnabled = true
private

The internal storage for the field status enabled flag.

This field stores whether validation status display is enabled and defaults to true.

◆ _objectField

ObjectField GWG.UsoUIElements.Editor.UsoObjectField._objectField
private

The internal ObjectField that handles the actual object selection functionality.

This field wraps Unity's standard ObjectField and provides the core object selection behavior. All ObjectField-specific functionality is delegated to this internal instance.

◆ _value

Object GWG.UsoUIElements.Editor.UsoObjectField._value
private

The internal storage for the selected object value.

This field stores the currently selected Object and is used by the value property.

◆ DefaultBindProp

const string GWG.UsoUIElements.Editor.UsoObjectField.DefaultBindProp = "value"
staticprivate

The default property name used for data binding operations.

This constant defines the default binding property name when no specific property is specified for binding.

◆ ElementStylesheet

const string GWG.UsoUIElements.Editor.UsoObjectField.ElementStylesheet = "uso-object-field"
staticprivate

The CSS class name used for general Uso UI element styling.

This constant defines the base stylesheet class for Uso object field elements.

◆ ElementValidationClass

const string GWG.UsoUIElements.Editor.UsoObjectField.ElementValidationClass = "uso-field-validation"
staticprivate

The CSS class name applied when field validation is enabled.

This class is conditionally applied based on the FieldStatusEnabled property to enable validation styling.

Property Documentation

◆ allowSceneObjects

bool GWG.UsoUIElements.Editor.UsoObjectField.allowSceneObjects
getset

Gets or sets whether scene objects can be assigned to this ObjectField. When false, only asset objects from the project can be assigned.

True if scene objects are allowed; otherwise, false to restrict to project assets only.

This property is exposed to UXML through the UxmlAttribute for declarative configuration. When set to false, the ObjectField will only accept objects that are stored as assets in the project, preventing assignment of objects that exist only in the current scene. This is useful for creating references that remain valid across different scenes and can be safely serialized in assets.

◆ FieldStatus

FieldStatusTypes GWG.UsoUIElements.Editor.UsoObjectField.FieldStatus
getprivate set

Gets or sets the current field validation status for visual feedback. This property controls the validation state styling of the element.

A FieldStatusTypes value indicating the current validation status of the field.

Setting this property automatically updates the visual styling through the UsoUiHelper.SetFieldStatus method. The status affects how the element appears to indicate validation states like error, warning, or success. This property is exposed to UXML through the UxmlAttribute for declarative configuration.

Implements GWG.UsoUIElements.IUsoUiElement.

◆ FieldStatusEnabled

bool GWG.UsoUIElements.Editor.UsoObjectField.FieldStatusEnabled
getprivate set

Gets or sets whether field validation status display is enabled for this element. When enabled, the element can show validation states through CSS class changes.

True if field status validation is enabled; otherwise, false. Default is true.

Setting this property to true adds the ElementValidationClass to enable validation styling. Setting it to false removes the validation class, disabling status-based visual feedback. This property is exposed to UXML through the UxmlAttribute for declarative configuration.

Implements GWG.UsoUIElements.IUsoUiElement.

◆ objectType

Type GWG.UsoUIElements.Editor.UsoObjectField.objectType
getset

Gets or sets the type of objects that can be assigned to this ObjectField. This property determines what types of Unity objects are valid for selection.

A Type that represents the allowed object type. Must be derived from UnityEngine.Object.

This property is exposed to UXML through the UxmlAttribute, allowing declarative specification of the allowed object type in UI layout files. The type restricts what objects can be assigned through drag-and-drop or the object picker dialog. Common values include typeof(GameObject), typeof(Texture2D), typeof(Material), etc.

◆ value

Object GWG.UsoUIElements.Editor.UsoObjectField.value
getset

Gets or sets the current Object value of the ObjectField. Setting this property triggers change events and updates the field display.

The currently selected Object, or null if no object is selected.

The setter implements the INotifyValueChanged pattern by creating and sending a ChangeEvent when the value actually changes. It uses SetValueWithoutNotify internally to update the display and then sends the appropriate change notification to any listeners.

The getter returns the currently stored object value. This property provides the main interface for getting and setting the ObjectField's value programmatically and through data binding.