UI Toolbox logoUI Toolbox
Support

DeviceSwap

A DeviceSwap shows one layer for the keyboard and the mouse and another for a gamepad, and swaps them when the player changes the device, with a fade, a slide or a scale.

A hotbar of keys 1 to 8 and a gamepad crossbar of two diamondsA hotbar of keys 1 to 8 and a gamepad crossbar of two diamonds

<tb:DeviceSwap transition="Slide" slide-direction="Up">
    <tb:DeviceLayer device="Keyboard">
        <!-- the hotbar: keys 1 to 8 -->
    </tb:DeviceLayer>
    <tb:DeviceLayer device="Gamepad">
        <!-- the crossbar: d-pad and face buttons -->
    </tb:DeviceLayer>
</tb:DeviceSwap>

The swap shows the layer of the device that the player used last, which LastInputDevice tracks. The keyboard layer shows before the first press. You can fill the layers from code too:

var swap = new DeviceSwap { Transition = SwapTransition.Fade };
swap.KeyboardLayer.Add(hotbar);
swap.GamepadLayer.Add(crossbar);
swap.Swapped += device => PlayClick();

KeyboardLayer and GamepadLayer make the layer when the swap has none.

Member Description
Transition (transition) A SwapTransition: Fade (default), Slide, Scale or None. None shows the new layer at once.
Duration (duration) The time of a swap in milliseconds (250). Zero swaps at once.
SlideDirection (slide-direction) A SwapDirection: Up (default), Down, Left or Right. The old layer leaves this way and the new layer comes from the other side.
Device (device) A DeviceSelect: Auto (default) follows the player, Keyboard and Gamepad fix the layer for a preview or the UI Builder.
ActiveDevice The DeviceKind of the layer that shows now.
KeyboardLayer, GamepadLayer The two DeviceLayers.
Swapped An event with the DeviceKind of the new layer. It fires after a swap, not for the first layer.

A DeviceLayer has one attribute, device (Keyboard or Gamepad).

How a swap runs

In the examples

The DeviceSwapDemo scene swaps a hotbar for the keys 1 to 8 with a crossbar of two diamonds of AbilitySlots with GamepadGlyphs: the left diamond for the d-pad, the right for the face buttons, with LT and RT above. Buttons switch the device by hand with LastInputDevice.Set, and a list picks the transition.

Styling

Class or property Element
tb-device-swap The control. It has position: relative.
tb-device-swap--fade, --slide, --scale, --none The transition.
tb-device-swap--slide--up, --down, --left, --right The direction of the slide.
tb-device-layer A layer. It has position: absolute at the top left corner and the transition properties.
tb-device-layer--enter A layer that is hidden or starts to show: the first look of the transition.
tb-device-layer--exit A layer that leaves: the last look of the transition.

See also