CrabUI
|
CUIComponents can be attached to barotrauma items to intercept their OnSelect, OnDeselect, UpdateHUD events
It's done via AttachedItemHandle
AttachedItemHandle contains links to connected Item and CUIComponent, and events:
public event Action<Item, CUIComponent> OnUpdate; public event Action<Item, CUIComponent> OnSelect; public event Action<Item, CUIComponent> OnDeselect;
You can also set them in initializer with:
public Action<Item, CUIComponent> AddOnUpdate { set => OnUpdate += value; } public Action<Item, CUIComponent> AddOnSelect { set => OnSelect += value; } public Action<Item, CUIComponent> AddOnDeselect { set => OnDeselect += value; }
There are 2 ways to attach item: connect item to component and ItemPrefab to CUIComponent type
public void CUIComponent.AttachTo(Item item, Action<Item, CUIComponent> callback = null) public static void AttachedItems.Connect(Item item, CUIComponent component, Action<Item, CUIComponent> callback = null)
public static void AttachedItems.ConnectPrefabs(ItemPrefab prefab, Type CUIType) public static void AttachedItems.ConnectPrefabs(string prefabName, Type CUIType) public static void AttachedItems.ConnectPrefabs(Identifier prefabId, Type CUIType)
This way when Item with mathing prefab is created CUI will create new CUIComponent of CUIType and attach it to that item
Also Item.ItemList will be scanned for matches
And this new CUIComponent must create its AttachedItemHandle and set callbacks for its events
Check CrabUITest.RandomTest.AttachedItemTest for example
There's basically Dictionary<Item.ID, AttachedItemHandle> which is contains all connections
It uses WeakReference<Item> and is also shaked every roundstart so it shouldn't hold dead items
And SelectedItem of Character.Controlled is constantly checked in update loop (it's done the same way in game), and if it matches something in the dict events are called