XboxTools: GameControllerDetector + improved XboxKeyControl

XboxTools: GameControllerDetector + improved XboxKeyControl

I recently released GameControllerDetector (here), a tool allowing developers to detect automatically if a user uses a Xbox gamepad or a media remote on Xbox. I also released XboxKeyControl (v1 here and v2) some months ago, a control allowing developers to display the different keys available on a xbox gamepad or a media remote.

Now that we have a way to detect the input device used by a user, we can improve XboxKeyControl to automatically change the glyph displayed when the user switches from one input device type to an other.

Here is a demo showing how it works:

Improve XboxKeyControl to support alternative glyph

The current implementation of XboxKeyControl allows you to write:

<control:XboxKeyControl Symbol="A" />

We will add a new property named SymbolMedia, so the control will be able to automatically switch between Symbol and SymbolMedia when the user starts using a different input device. Of course, if SymbolMedia is not set, we will do nothing.

Let’s add the new property:

#region SymbolMedia

public Key SymbolMedia
{
	get => (Key)GetValue(SymbolMediaProperty);
	set => SetValue(SymbolMediaProperty, value);
}

public static readonly DependencyProperty SymbolMediaProperty =
	DependencyProperty.Register("SymbolMedia", typeof(Key), typeof(XboxKeyControl), new PropertyMetadata(Key.Unknown, SymbolMediaCallback));

private static void SymbolMediaCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
	((XboxKeyControl)d).SymbolMediaCallback(e.NewValue);

}

private void SymbolMediaCallback(object newValue)
{
    ...
}

For SymbolMediaCallback, we will first update the glyph and then listen GameControllerDetector.GameControllerChanged event in order to update the glyph when necessary.

private void SymbolMediaCallback(object newValue)
{
    UpdateGlyph();
    GameControllerDetector.Instance.GameControllerChanged -= XboxKeyControl_GameControllerChanged;
    if (newValue != null)
        GameControllerDetector.Instance.GameControllerChanged += XboxKeyControl_GameControllerChanged;
}

private void XboxKeyControl_GameControllerChanged(object sender, GameControllerDetector.GameControllerType e)
{
    UpdateGlyph();
}

Last but not least, we update UpdateGlyph to use the Symbol or SymbolMedia depends of the input device used.

var symbol = Symbol;
if (GameControllerDetector.Instance.CurrentControllerType == GameControllerDetector.GameControllerType.MEDIAREMOTE && SymbolMedia != Key.Unknown)
     symbol = SymbolMedia;

Source code

These 2 components are now available on github and grouped in a library named Huyn.XboxTools, don’t hesitate to use it for your Xbox app!

Github repository available here.

Comments are closed.