RadialMenu
RadialMenu in UIToolbox is a ring of wedges, as a weapon wheel or a quick menu of a game. Each RadialMenuItem has a wedge. The first item is at the top, and the others follow clockwise. The center shows the text of the highlighted item. An item can have sub-items, which fold out in a second ring. The RadialMenuDemo example scene has a weapon wheel, a build wheel and a command wheel.
<tb:RadialMenu name="wheel" inner-radius="0.45" cancel-text="Cancel">
<tb:RadialMenuItem text="Sword" />
<tb:RadialMenuItem text="Bow">
<tb:RadialMenuItem text="Fire" />
<tb:RadialMenuItem text="Ice" />
</tb:RadialMenuItem>
</tb:RadialMenu>
var potion = wheel.AddItem("Potion");
potion.AddItem("Health", () => player.Drink(Potion.Health));
potion.AddItem("Mana", () => player.Drink(Potion.Mana));
wheel.ItemChosen += item => equip(item.Text);
wheel.Cancelled += closeWheel;
// A stick that points the wheel. A Value action is canceled, not performed, when the stick goes back.
stick.performed += context => wheel.Point(context.ReadValue<Vector2>());
stick.canceled += _ => wheel.Point(Vector2.zero);
| Member | Description |
|---|---|
AddItem(text, action), Items |
The items, clockwise from the top. A child RadialMenuItem in UXML is an item too. |
HighlightedIndex, Highlighted, HighlightChanged |
The highlighted item. A disabled item cannot be highlighted. A removed or disabled item loses the highlight. HighlightedIndex is -1 while a sub-item is highlighted; Highlighted is the sub-item. |
Point(direction) |
Highlights the item in the direction of a stick, with y up. A direction shorter than 0.5 keeps the highlight. |
Choose(), ItemChosen |
Chooses the highlighted item. RadialMenuItem.Chosen occurs first, then ItemChosen. For an item with sub-items, Choose() goes into the sub-items and raises no event. |
Cancel(), Cancelled |
Cancel() goes back from a sub-item to its parent. With no sub-item highlighted, it raises Cancelled. A click in the center hole always raises Cancelled. |
CancelText (cancel-text) |
A text in the center hole, under the text of the highlighted item, that shows where a click cancels. Empty (default) shows none. |
OpenItem |
The item that shows its sub-items, or null. |
SubRingSize (sub-ring-size) |
The depth of the ring of the sub-items as a share of the radius of the menu, from 0.1 to 0.5. Default 0.28. |
ItemAt(localPosition) |
The item or sub-item at a local position, or null in the center hole. |
InnerRadius |
The size of the center hole as a share of the radius, from 0 to 0.9. Default 0.45. 0 draws pie wedges. |
Animate |
True (default) fades the highlight between the wedges. |
RadialMenuItem.Text, Icon, Enabled |
The label, an optional image over the label, and the enabled state. |
RadialMenuItem.AddItem(text, action), SubItems, ParentItem |
The sub-items of an item, and the parent of a sub-item. A child RadialMenuItem of an item in UXML is a sub-item too. |
Input:
- The pointer highlights the wedge in its direction, and a click chooses it. A click in the center hole cancels.
- Left and Right (and Tab) step counterclockwise and clockwise over the enabled items. Up and Down go to the enabled item nearest the top or the bottom. The menu keeps these moves, so the focus stays on it.
- Submit chooses. The cancel event cancels.
- A click on a disabled wedge does nothing.
Sub-items:
An item with sub-items shows a thin band outside its wedge. It is open while it or one of its sub-items is highlighted: the sub-items then show outside its wedge, in wedges of the same shape.
With the pointer: point at the item, then move out to a sub-item and click.
With the keys or a gamepad: submit on the item goes to the sub-item in the middle. Left and Right step over the sub-items. Submit chooses, and cancel goes back to the item.
With a stick: after the submit,
Pointhighlights the sub-item in the direction of the stick. A direction out of the section of the sub-items goes back to the ring.When an item has sub-items, the ring is smaller: the menu keeps the space of the second ring free. A sub-wedge is as wide as a wedge, and 30 degrees at most.
The menu shows one level of sub-items.
The UI navigation also reads the sticks. While a
Pointdirection is out of the dead zone, the menu ignores the navigation moves, so the same stick does not step the highlight away. CallPoint(Vector2.zero)when the stick goes back, or the arrow keys stay ignored.The menu is square: the smaller side of the content sets the radius.
The menu draws the wedges with
Painter2Dand places the items withleftandtop.
Styling
| Class | Element |
|---|---|
tb-radial-menu |
The menu: 320 px. Its variables set the look of the wedges (see below). |
tb-radial-menu--outline |
A style: wedges with a line and no fill. |
tb-radial-menu--solid |
A style: a disc with no space between the wedges, and a hub in the hole. |
tb-radial-menu--over-center |
Set while the pointer is in the center hole. The cancel text is then --tb-color-danger. |
tb-radial-menu__items |
The layer of the items. It fills the menu and has no look of its own. |
tb-radial-menu__item |
An item, centered in the middle of its wedge with translate: -50% -50%. |
tb-radial-menu__item--highlighted |
The highlighted item: scale 1.15 with a 0.15 s transition, and the label and icon in --tb-color-accent-text. |
tb-radial-menu__item--open |
The item that shows its sub-items. It has no scale. |
tb-radial-menu__item-sub-items |
The layer of the sub-items of an item. It fades in while the item is open. |
tb-radial-menu__item-icon |
The icon: 32 px, tinted --tb-color-text. It shows only when the item has an icon. |
tb-radial-menu__item-label |
The label, in --tb-color-text. A disabled item has opacity 0.5. |
tb-radial-menu__hub |
The layer of the two texts in the center hole. |
tb-radial-menu__center |
The text of the highlighted item in the center hole: bold, --tb-font-size-lg. |
tb-radial-menu__cancel |
The cancel text: --tb-font-size-sm, --tb-color-text-muted. |
The variables of tb-radial-menu:
| Variable | Description |
|---|---|
--tb-radial-menu-wedge |
The wedge color. Default --tb-color-surface-hover. A disabled wedge draws at 40 percent of its alpha. |
--tb-radial-menu-sub-wedge |
The color of the wedge of a sub-item, and of the band of an item with sub-items. |
--tb-radial-menu-highlight |
The color of the highlighted wedge. Default --tb-color-accent. |
--tb-radial-menu-center |
The color of a disc in the center hole. Default: no disc. |
--tb-radial-menu-line |
The color of a line of 2 px around each wedge. Default: no line. |
--tb-radial-menu-gap |
The space between two wedges in degrees. Default 1.5. |
To make a style of your own, set the variables in a class and add the class to the menu:
.ember-wheel {
--tb-radial-menu-wedge: rgba(120, 40, 20, 0.85);
--tb-radial-menu-sub-wedge: rgba(90, 30, 15, 0.85);
--tb-radial-menu-highlight: #ffb020;
--tb-radial-menu-center: rgba(40, 14, 8, 0.9);
--tb-radial-menu-line: #ff7a30;
--tb-radial-menu-gap: 3;
}