Skip to main content

Dropdown

dropdown

See the DropdownDemo.cs.

using Gridrand.RimGui.Manual;
using System;
using System.Collections.Generic;

namespace Gridrand.RimGui.Extensions.Manual
{
/// <summary>
/// Demos such as
/// <see cref="GuiUtil.Dropdown(Gui, IReadOnlyList{string}, ISelectableItems{int}, bool, int)"/> and
/// <see cref="GuiUtil.InputDropdown(Gui, IReadOnlyList{string}, ISelectableItems{int}, bool, int)"/>
/// </summary>
class DropdownDemo : ManualBase, IManual
{
SampleType type;
SampleType inputType;
SampleTypeFlags typeFlags;

int selectedIndex;
int inputSelectedIndex0;
int inputSelectedIndex1;

readonly DropdownItems dropdownItems = new();
readonly DropdownItems inputDropdownItems = new();

readonly string[] labels0 = new string[3]
{
"AAA",
"BBB",
"CCC",
};

readonly string[] lables1 = new string[9]
{
"012",
"123",
"234",
"345",
"456",
"567",
"678",
"789",
"890",
};

public DropdownDemo(ManualBaseResource p) : base(p)
{
dropdownItems.Select(0);
inputDropdownItems.Select(0);
}

public void Draw()
{
Gui.Heading("Dropdown");
Gui.Dropdown(labels0, ref selectedIndex);

Gui.SmallSpacing();

Gui.Text("Multiple select (Ctrl + Click)");
Gui.Dropdown(labels0, dropdownItems, true);

Gui.Heading("Dropdown(Enum)");
// Pass the selected enum as the argument.
Gui.Dropdown(ref type);


Gui.Text("Multiple select (Ctrl + Click)");
Gui.Dropdown(ref typeFlags);


Gui.Heading("InputDropdown");
Gui.InputDropdown(labels0, ref inputSelectedIndex0);

// The items will be scrolled because their total height is greater than MaxHeights.
using (ExStyle.Dropdown.MaxHeights.Begin(100))
Gui.InputDropdown(lables1, ref inputSelectedIndex1);

Gui.SmallSpacing();

Gui.Text("Multiple select (Ctrl + Click)");
Gui.InputDropdown(labels0, inputDropdownItems, true);

Gui.Heading("InputDropdown(Enum)");
Gui.InputDropdown(ref inputType);

}

enum SampleType
{
Element0,
Element1,
Element2
}

// If FlagsAttribute is attached, multiple selection is possible.
[Flags]
enum SampleTypeFlags
{
None = 0,
Element1 = 1 << 0,
Element2 = 1 << 1,
Element3 = 1 << 2,
Element4 = 1 << 3,
}

class DropdownItems : ISelectableItems<int>
{
readonly HashSet<int> values = new();

public IEnumerable<int> Items => values;

public void Deselect(int item) => values.Remove(item);
public void DeselectAll() => values.Clear();
public bool IsSelected(int item) => values.Contains(item);
public void Select(int item) => values.Add(item);
}
}
}