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

A custom UI element that provides texture selection and preview functionality with detailed texture information display. This element combines an ObjectField for texture selection with an automatic preview image and comprehensive metadata display including texture properties and file information. More...

Inheritance diagram for GWG.UsoUIElements.Editor.UsoTexturePreviewElement:

Public Member Functions

 UsoTexturePreviewElement ()
 Initializes a new instance of the UsoTexturePreviewElement, setting up the complete UI structure and event handlers. Creates all child elements including the ObjectField, preview image, detail labels, and configures the layout and styling.
void ApplyBinding (string fieldBindingProp, string fieldBindingPath, BindingMode fieldBindingMode)
 Applies data binding to this element using the specified property name, path, and mode. This method enables two-way data binding between the element 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 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 element without triggering change notifications or events. Updates the internal texture reference, preview image, detail labels, and ObjectField display.
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 InitElement (string fieldName)
 Initializes the USO UI element with the specified field name and applies framework-specific configuration. Sets up the element for integration with the USO framework including styling, validation, and identification.
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.

Static Public Attributes

static readonly string USSClassName = "texture-preview-element"
 The primary CSS class name applied to the texture preview element.
static readonly string USSStylesheetPath = "texture_preview_element"
 The path to the USS stylesheet resource that styles this texture preview element.

Properties

FieldStatusTypes FieldStatus [get, private set]
 Gets or sets the current field status for validation display. This property controls the visual validation state 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.
Object value [get, set]
 Gets or sets the current Texture2D value of the element. Setting this property triggers change events and updates the preview display.

Private Member Functions

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

Private Attributes

readonly VisualElement _detailsRow
 Container element that holds the preview image and texture details in a horizontal layout.
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 ObjectField that allows users to select Texture2D assets.
readonly Image _previewImage
 The Image element that displays the texture preview.
readonly Label _selectItemLabel
 Label displayed when no texture is selected, prompting the user to select one.
Texture2D _texture
 The currently selected Texture2D asset.
readonly Label _textureFileSize
 Label that displays the calculated file size of the selected texture.
readonly Label _textureName
 Label that displays the name of the selected texture.
readonly Label _textureSize
 Label that displays the dimensions of the selected texture.
Label _textureTags
 Label that would display texture tags information.
Object _value
 The internal storage for the selected texture 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 provides texture selection and preview functionality with detailed texture information display. This element combines an ObjectField for texture selection with an automatic preview image and comprehensive metadata display including texture properties and file information.

This element is specifically designed for editor use and integrates with Unity's asset system to display texture previews and information. It implements the IUsoUiElement interface for consistent field validation and binding support across the Uso UI system. The element displays texture name, dimensions, and calculated file size information alongside a 64x64 pixel preview image. Field validation status can be enabled/disabled and various status types are supported for form validation scenarios. The element automatically handles showing/hiding details based on whether a valid texture is selected.

Constructor & Destructor Documentation

◆ UsoTexturePreviewElement()

GWG.UsoUIElements.Editor.UsoTexturePreviewElement.UsoTexturePreviewElement ( )

Initializes a new instance of the UsoTexturePreviewElement, setting up the complete UI structure and event handlers. Creates all child elements including the ObjectField, preview image, detail labels, and configures the layout and styling.

The constructor loads the associated stylesheet and creates a complex UI hierarchy including:

  • A prompt label for when no texture is selected
  • An ObjectField configured for Texture2D selection
  • A details row containing a preview image and information labels
  • Multiple labels for displaying texture name, size, and file size information

The ObjectField is configured with a value change callback, and the preview image is sized to 64x64 pixels. The details row uses horizontal layout and is initially hidden until a texture is selected. The element starts with field validation enabled and applies appropriate CSS classes.

◆ ~UsoTexturePreviewElement()

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

Finalizer that ensures proper cleanup of event callbacks when the element is destroyed. Unregisters the value change callback from the 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 construction.

Member Function Documentation

◆ ApplyBinding()

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

Applies data binding to this element using the specified property name, path, and mode. This method enables two-way data binding between the element 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 creates a DataBinding with the specified parameters and applies it to the element. 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.UsoTexturePreviewElement.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.

◆ OnObjectFieldValueChanged()

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

Handles value changes from the ObjectField and updates the element's value accordingly. This method is called when the user selects a different texture in the ObjectField.

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

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

◆ SetFieldStatus()

void GWG.UsoUIElements.Editor.UsoTexturePreviewElement.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.UsoTexturePreviewElement.SetValueWithoutNotify ( Object newValue)

Sets the value of the element without triggering change notifications or events. Updates the internal texture reference, preview image, detail labels, and ObjectField display.

Parameters
newValueThe new Texture2D value to set, or null to clear the selection.
Exceptions
ArgumentExceptionThrown when the newValue is not null and not a Texture2D.

This method is used internally to update the element's state without triggering change events, which is important for avoiding infinite loops during value synchronization. The method validates that the provided value is either null or a Texture2D before proceeding.

When a valid texture is set, the method:

  • Updates the preview image with the texture
  • Populates the detail labels with texture information
  • Shows the details row and hides the selection prompt
  • Calculates an estimated file size based on dimensions and RGBA format

When null is set, it hides the details and shows the selection prompt label.

◆ ShowFieldStatus()

void GWG.UsoUIElements.Editor.UsoTexturePreviewElement.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

◆ _detailsRow

readonly VisualElement GWG.UsoUIElements.Editor.UsoTexturePreviewElement._detailsRow
private

Container element that holds the preview image and texture details in a horizontal layout.

This element uses horizontal flex direction to arrange the preview image alongside the texture information. It is hidden when no texture is selected and becomes visible when a valid texture is chosen.

◆ _fieldStatus

FieldStatusTypes GWG.UsoUIElements.Editor.UsoTexturePreviewElement._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.UsoTexturePreviewElement._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.UsoTexturePreviewElement._objectField
private

The ObjectField that allows users to select Texture2D assets.

This field is configured to only accept Texture2D objects and triggers preview updates when its value changes. It provides the standard Unity asset selection interface including drag-and-drop support.

◆ _previewImage

readonly Image GWG.UsoUIElements.Editor.UsoTexturePreviewElement._previewImage
private

The Image element that displays the texture preview.

This image shows a visual representation of the selected texture and is sized to 64x64 pixels with fixed dimensions. The image is automatically updated when a new texture is selected through the ObjectField.

◆ _selectItemLabel

readonly Label GWG.UsoUIElements.Editor.UsoTexturePreviewElement._selectItemLabel
private

Label displayed when no texture is selected, prompting the user to select one.

This label provides user guidance when the element is in an empty state. It is hidden when a valid texture is selected and shown when the selection is cleared.

◆ _texture

Texture2D GWG.UsoUIElements.Editor.UsoTexturePreviewElement._texture
private

The currently selected Texture2D asset.

This field stores the actual texture reference and is synchronized with the ObjectField selection. It is used to populate the preview image and metadata displays.

◆ _textureFileSize

readonly Label GWG.UsoUIElements.Editor.UsoTexturePreviewElement._textureFileSize
private

Label that displays the calculated file size of the selected texture.

Shows an estimated file size calculation based on width × height × 4 bytes (assuming RGBA format). This provides an approximate memory footprint rather than actual disk file size.

◆ _textureName

readonly Label GWG.UsoUIElements.Editor.UsoTexturePreviewElement._textureName
private

Label that displays the name of the selected texture.

Shows the texture asset's name with HTML bold formatting for emphasis in the details display.

◆ _textureSize

readonly Label GWG.UsoUIElements.Editor.UsoTexturePreviewElement._textureSize
private

Label that displays the dimensions of the selected texture.

Shows the texture's width and height in pixels with HTML bold formatting for the label. Displays as "Viewable Size: WIDTHxHEIGHT" format.

◆ _textureTags

Label GWG.UsoUIElements.Editor.UsoTexturePreviewElement._textureTags
private

Label that would display texture tags information.

This field is declared but not currently used in the implementation. Reserved for potential future functionality to display texture asset tags.

◆ _value

Object GWG.UsoUIElements.Editor.UsoTexturePreviewElement._value
private

The internal storage for the selected texture object value.

This field stores the currently selected Object (specifically Texture2D) and is used by the value property.

◆ DefaultBindProp

const string GWG.UsoUIElements.Editor.UsoTexturePreviewElement.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.UsoTexturePreviewElement.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.UsoTexturePreviewElement.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.

◆ USSClassName

readonly string GWG.UsoUIElements.Editor.UsoTexturePreviewElement.USSClassName = "texture-preview-element"
static

The primary CSS class name applied to the texture preview element.

This class name is used for styling the main container of the texture preview element.

◆ USSStylesheetPath

readonly string GWG.UsoUIElements.Editor.UsoTexturePreviewElement.USSStylesheetPath = "texture_preview_element"
static

The path to the USS stylesheet resource that styles this texture preview element.

This stylesheet should be placed in the TexturePreviewElement Resources folder and contains the visual styling for the texture preview element.

Property Documentation

◆ FieldStatus

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

Gets or sets the current field status for validation display. This property controls the visual validation state 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.UsoTexturePreviewElement.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.

◆ value

Object GWG.UsoUIElements.Editor.UsoTexturePreviewElement.value
getset

Gets or sets the current Texture2D value of the element. Setting this property triggers change events and updates the preview display.

The currently selected Texture2D asset, or null if no texture 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 texture reference. The property provides type-safe access to the texture while maintaining compatibility with the generic Object interface.