Skip to content

Game.UI.Editor.DirectoryAdapter

Assembly:
Game

Namespace:
Game.UI.Editor

Type:
public class DirectoryAdapter : ItemPicker.IAdapter, SearchField.IAdapter

Base:
Implements: ItemPicker.IAdapter, SearchField.IAdapter

Summary:
Adapter used by DirectoryPanelBase to provide and manage a list of file/directory items for a directory picker UI. Handles selection, filtering by directory and search query, favorites state (persisted via SharedSettings.instance.editor.directoryPickerFavorites), and notifies the panel on selection changes. Designed to be consumed via the ItemPicker.IAdapter and SearchField.IAdapter interfaces.


Fields

  • private DirectoryPanelBase m_Panel
    Reference to the owning DirectoryPanelBase instance. Used to notify the panel when selection changes (calls m_Panel.OnSelect).

  • private Item m_SelectedItem
    Backer for the selectedItem property. Holds the currently selected Item.

  • private List<Item> m_Items
    Backer for the items collection. Stores the full list of items; the public getter returns a filtered view based on directoryPath and searchQuery.

  • private bool m_Dirty
    Flag indicating that the adapter's data changed and needs refreshing. Returned by ItemPicker.IAdapter.Update and cleared by that method.

  • private HashSet<string> m_FavoriteIds = new HashSet<string>()
    Set of relative paths marked as favorites. Used to quickly check and update Item.favorite and persisted to editor settings.

  • public string searchQuery = string.Empty
    Public searchQuery field (note: the SearchField.IAdapter implementation uses a separate private m_SearchQuery). This field is used by the items getter in filtering (case-insensitive substring match on item.name).

  • private string m_SearchQuery = string.Empty
    Backer for the explicit SearchField.IAdapter.searchQuery property (exposed through the interface).

Properties

  • public string directoryPath { get; set; }
    Directory path used by the items getter to filter items to the current directory scope. When null, some search behavior in the getter differs (it allows prefix matching on fullName).

  • public Item selectedItem { get; set; }
    Getter returns m_SelectedItem. Setter updates m_SelectedItem and, if changed, calls m_Panel.OnSelect(value) to notify the panel about the selection change.

  • public List<Item> items { get; set; }
    Getter: returns a filtered list based on the current directoryPath and searchQuery. Filtering logic:

  • If searchQuery is empty: item.parentDir must equal directoryPath.
  • If searchQuery is non-empty: allows items whose fullName starts with directoryPath (when directoryPath is not null) and ensures item.name + "/" != directoryPath (exclusion check).
  • Also requires item.name to contain searchQuery (case-insensitive).
  • The result is returned as a new List. Setter: assigns m_Items, rebuilds m_FavoriteIds from SharedSettings.instance?.editor.directoryPickerFavorites (if present), updates each item.favorite based on m_FavoriteIds, sorts m_Items (using Item.CompareTo or IComparable implemented by Item), and sets m_Dirty = true.

  • int ItemPicker<Item>.IAdapter.columnCount => 1
    Explicit interface property returning the number of columns used by the picker (fixed to 1).

  • string SearchField.IAdapter.searchQuery { get; set; }
    Explicit interface property mapped to m_SearchQuery. Setter only updates the internal value when changed.

Constructors

  • public DirectoryAdapter(DirectoryPanelBase panel)
    Constructs a DirectoryAdapter bound to the given DirectoryPanelBase. Stores the reference in m_Panel.

Methods

  • bool ItemPicker<Item>.IAdapter.Update()
    Explicit interface method. Returns the current m_Dirty value and resets m_Dirty to false. Used by the UI to know when to refresh.

  • void ItemPicker<Item>.IAdapter.SetFavorite(int index, bool favorite)
    Explicit interface method. Toggles favorite state for the item at the given index in the filtered items list:

  • Retrieves item = items[index].
  • Adds or removes item.relativePath from m_FavoriteIds based on favorite argument.
  • Persists m_FavoriteIds to SharedSettings.instance?.editor.directoryPickerFavorites (as an array) if editor settings exist.
  • Updates item.favorite to the new state, re-sorts m_Items, reassigns items = m_Items (to refresh favorites/ordering) and sets m_Dirty = true.

Notes: - The items getter allocates and returns a new List each time; callers should avoid repeatedly enumerating it unless necessary. - Persistence uses SharedSettings.instance?.editor.directoryPickerFavorites — if SharedSettings or editor is null, favorites are not persisted. - Filter logic mixes searchQuery (public field) and SearchField.IAdapter.searchQuery (m_SearchQuery). The items getter reads the public searchQuery field.

Usage Example

// Assume 'panel' is an existing DirectoryPanelBase instance
var adapter = new DirectoryAdapter(panel);

// Set the backing items list (populates favorites from editor settings)
adapter.items = loadedItems; // loadedItems: List<Item>

// Set the directory to show
adapter.directoryPath = "Assets/Models/Buildings/";

// Set a search term (the adapter's getter uses the public searchQuery field)
adapter.searchQuery = "warehouse";

// Notify UI to refresh: the picker will call ((ItemPicker<Item>.IAdapter)adapter).Update()
// To mark an item as favorite via the adapter interface:
((ItemPicker<Item>.IAdapter)adapter).SetFavorite(0, true);