Skip to content

Instantly share code, notes, and snippets.

@sverrirs
Created November 11, 2015 23:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sverrirs/0a0181fb17b29d070003 to your computer and use it in GitHub Desktop.
Save sverrirs/0a0181fb17b29d070003 to your computer and use it in GitHub Desktop.
Unity3d component that allows a very basic usage of the Xbox 360 Big Button Controllers. Exposes main static functions, GetButton and GetAxis.
using System;
using UnityEngine;
using XboxBigButton;
public class XboxBigButtonJoystick : MonoBehaviour
{
private XboxBigButtonDevice _device;
private Buttons[] _state = new Buttons[4];
private static XboxBigButtonJoystick _instance;
public static bool GetButton(Controller playerColor, Buttons button)
{
bool success = (_instance._state[(int)playerColor] & button ) == button;
_instance._state[(int)playerColor] &= ~button; // Consume the button state
return success;
}
public static bool GetPlayer(Controller playerColor)
{
return _instance._state[(int) playerColor] > 0;
}
private float _hor = 0f;
private float _easeStrength = 0.5f;
public static float GetAxis(Controller playerColor, string axisName )
{
int amount = 0;
if (axisName == "Horizontal")
{
if ((_instance._state[(int) playerColor] & Buttons.Right) == Buttons.Right)
{
amount += 1;
_instance._state[(int)playerColor] &= ~Buttons.Right;
}
if ((_instance._state[(int) playerColor] & Buttons.Left) == Buttons.Left)
{
amount += -1;
_instance._state[(int)playerColor] &= ~Buttons.Left;
}
}
else if( axisName == "Vertical")
{
if ((_instance._state[(int)playerColor] & Buttons.Up) == Buttons.Up)
{
amount += 1;
_instance._state[(int)playerColor] &= ~Buttons.Up;
}
if ((_instance._state[(int)playerColor] & Buttons.Down) == Buttons.Down)
{
amount += -1;
_instance._state[(int)playerColor] &= ~Buttons.Down;
}
}
if (amount == 0)
return 0.0f;
// Attempt to do some easing of the movement, yeah this doesn't really work that well
float val = 1.0f * amount;
_instance._hor = Mathf.Lerp(_instance._hor, val, _instance._easeStrength);
return _instance._hor;
}
// Use this for initialization
void Start ()
{
// Must have a dummy line to instantiate Loom correctly (sets up the dummy game object that we will invoke everuthing on)
var tmp = Loom.Current.GetComponent<Loom>();
// Store the singleton
_instance = this;
if (_device == null)
{
_state = new Buttons[4];
_device = new XboxBigButtonDevice();
_device.ButtonStateChanged += _device_ButtonStateChanged;
_device.Connect();
}
}
private void CleanUpJoystickDevice()
{
if (_device != null)
{
_device.ButtonStateChanged -= _device_ButtonStateChanged;
_device.Disconnect();
_device.Dispose();
_device = null;
_state = null;
}
}
void OnApplicationQuit()
{
CleanUpJoystickDevice();
}
void OnDestroy()
{
CleanUpJoystickDevice();
}
private void _device_ButtonStateChanged(object sender, XboxBigButtonDeviceEventArgs e)
{
var controller = e.Controller;
var buttonState = e.ButtonState;
Loom.QueueOnMainThread(() =>
{
UpdateButtonStateDictionary(controller, buttonState);
});
}
private void UpdateButtonStateDictionary(Controller c, Buttons b)
{
_instance._state[(int) c] |= b;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment