Skip to main content

InteractiveItem

See the InteractiveItemDemo class.

public void Draw()
{
// Displays a heading for interactive items
Gui.Heading("InteractiveItem");
DrawInteractiveItem();

// Displays a heading for selection handling using ItemSelectUpdater
Gui.Heading("Using ItemSelectUpdater");
DrawUsingItemSelectUpdater();

}
void DrawInteractiveItem()
{
// Draws an interactive item and updates the selection state
Gui.InteractiveItem(isSelectedItem);
isSelectedItem = Ctx.IsLastSelected();
}

void DrawUsingItemSelectUpdater()
{
using var s = Style.SpacingYs.Begin(0f);
var itemSelectUpdater = new ItemSelectUpdater<int>(selection, Gui.Input, true);

// Iterates through the selection items and renders interactive elements
for (int i = 0; i < selection.Length; i++)
{
using var s2 = Ctx.PushId(i);

var rect = Ctx.AllocateRect();

// Draws an interactive item and checks if it's selected
Gui.NextRect(rect).InteractiveItem(selection.IsSelected(i));

// Updates selection state based on user interaction
itemSelectUpdater.Update(i, Ctx.IsLastPressedThisFrame());

// Logs when an item is hovered
if (Ctx.IsLastHovered())
{
Logger.Debug($"Hovered:{i}");
}

Gui.NextRect(rect).Text($"{i}");
}
}

/// <summary>
/// Implements item selection logic.
/// </summary>
class Selection : IItemSelection<int>
{
readonly bool[] bools;

public int Length => bools.Length;

public Selection(int length)
{
bools = new bool[length];
}

public void Deselect(int item)
{
bools[item] = false;
}

public void DeselectAll()
{
for (int i = 0; i < bools.Length; i++)
bools[i] = false;
}

public bool IsSelected(int item)
{
return bools[item];
}

public void Select(int item)
{
bools[item] = true;
}
}