You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

355 lines
8.4 KiB
C#

3 weeks ago
using System;
using System.Runtime.InteropServices;
using Unity.Collections.LowLevel.Unsafe;
public struct OfflineButtons : IEquatable<OfflineButtons>
{
// [FieldOffset(0)]
private int _bits;
//
// Summary:
// Gets the bits representing the state of the buttons.
public int Bits => _bits;
//
// Summary:
// Initializes a new instance of the NetworkButtons struct with the specified buttons
// state.
//
// Parameters:
// buttons:
// The integer representing the state of the buttons.
public OfflineButtons(int buttons)
{
_bits = buttons;
}
//
// Summary:
// Checks if the button at the specified index is set.
//
// Parameters:
// button:
// The index of the button to check.
//
// Returns:
// true if the button is set; otherwise, false.
public bool IsSet(int button)
{
return (_bits & (1 << button)) != 0;
}
//
// Summary:
// Sets the button at the specified index to down.
//
// Parameters:
// button:
// The index of the button to set to down.
public void SetDown(int button)
{
_bits |= 1 << button;
}
//
// Summary:
// Sets the button at the specified index to up.
//
// Parameters:
// button:
// The index of the button to set to up.
public void SetUp(int button)
{
_bits &= ~(1 << button);
}
//
// Summary:
// Sets the button at the specified index to the specified state.
//
// Parameters:
// button:
// The index of the button to set.
//
// state:
// The state to set the button to.
public void Set(int button, bool state)
{
if (state)
{
SetDown(button);
}
else
{
SetUp(button);
}
}
//
// Summary:
// Sets all buttons to up.
public void SetAllUp()
{
_bits = 0;
}
//
// Summary:
// Sets all buttons to down.
public void SetAllDown()
{
_bits = -1;
}
//
// Summary:
// Checks if the button of the specified enum type is set.
//
// Parameters:
// button:
// The button of type T to check.
//
// Type parameters:
// T:
// The enum type of the button.
//
// Returns:
// true if the button is set; otherwise, false.
public bool IsSet<T>(T button) where T : unmanaged, Enum
{
return IsSet(UnsafeUtility.EnumToInt(button));
}
//
// Summary:
// Sets the button of the specified enum type to down.
//
// Parameters:
// button:
// The button of type T to set to down.
//
// Type parameters:
// T:
// The enum type of the button.
public void SetDown<T>(T button) where T : unmanaged, Enum
{
SetDown(UnsafeUtility.EnumToInt(button));
}
//
// Summary:
// Sets the button of the specified enum type to up.
//
// Parameters:
// button:
// The button of type T to set to up.
//
// Type parameters:
// T:
// The enum type of the button.
public void SetUp<T>(T button) where T : unmanaged, Enum
{
SetDown(UnsafeUtility.EnumToInt(button));
}
//
// Summary:
// Sets the button of the specified enum type to the specified state.
//
// Parameters:
// button:
// The button of type T to set.
//
// state:
// The state to set the button to.
//
// Type parameters:
// T:
// The enum type of the button.
public void Set<T>(T button, bool state) where T : unmanaged, Enum
{
Set(UnsafeUtility.EnumToInt(button), state);
}
//
// Summary:
// Gets the buttons that were pressed or released since the previous state.
//
// Parameters:
// previous:
// The previous state of the buttons.
//
// Returns:
// A tuple containing the buttons that were pressed and the buttons that were released.
public (OfflineButtons, OfflineButtons) GetPressedOrReleased(OfflineButtons previous)
{
return (GetPressed(previous), GetReleased(previous));
}
//
// Summary:
// Gets the buttons that were pressed since the previous state.
//
// Parameters:
// previous:
// The previous state of the buttons.
//
// Returns:
// The buttons that were pressed.
public OfflineButtons GetPressed(OfflineButtons previous)
{
previous._bits = (previous._bits ^ _bits) & _bits;
return previous;
}
//
// Summary:
// Gets the buttons that were released since the previous state.
//
// Parameters:
// previous:
// The previous state of the buttons.
//
// Returns:
// The buttons that were released.
public OfflineButtons GetReleased(OfflineButtons previous)
{
previous._bits = (previous._bits ^ _bits) & previous._bits;
return previous;
}
//
// Summary:
// Checks if the button at the specified index was pressed since the previous state.
//
//
// Parameters:
// previous:
// The previous state of the buttons.
//
// button:
// The index of the button to check.
//
// Returns:
// true if the button was pressed; otherwise, false.
public bool WasPressed(OfflineButtons previous, int button)
{
return GetPressed(previous).IsSet(button);
}
//
// Summary:
// Checks if the button at the specified index was released since the previous state.
//
//
// Parameters:
// previous:
// The previous state of the buttons.
//
// button:
// The index of the button to check.
//
// Returns:
// true if the button was released; otherwise, false.
public bool WasReleased(OfflineButtons previous, int button)
{
return GetReleased(previous).IsSet(button);
}
//
// Summary:
// Checks if the button of the specified enum type was pressed since the previous
// state.
//
// Parameters:
// previous:
// The previous state of the buttons.
//
// button:
// The button of type T to check.
//
// Type parameters:
// T:
// The enum type of the button.
//
// Returns:
// true if the button was pressed; otherwise, false.
public bool WasPressed<T>(OfflineButtons previous, T button) where T : unmanaged, Enum
{
return GetPressed(previous).IsSet(button);
}
//
// Summary:
// Checks if the button of the specified enum type was released since the previous
// state.
//
// Parameters:
// previous:
// The previous state of the buttons.
//
// button:
// The button of type T to check.
//
// Type parameters:
// T:
// The enum type of the button.
//
// Returns:
// true if the button was released; otherwise, false.
public bool WasReleased<T>(OfflineButtons previous, T button) where T : unmanaged, Enum
{
return GetReleased(previous).IsSet(button);
}
//
// Summary:
// Determines whether the specified NetworkButtons is equal to the current NetworkButtons.
//
//
// Parameters:
// other:
// The NetworkButtons to compare with the current NetworkButtons.
//
// Returns:
// true if the specified NetworkButtons is equal to the current NetworkButtons;
// otherwise, false.
public bool Equals(OfflineButtons other)
{
return _bits == other._bits;
}
//
// Summary:
// Determines whether the specified object is equal to the current NetworkButtons.
//
//
// Parameters:
// obj:
// The object to compare with the current NetworkButtons.
//
// Returns:
// true if the specified object is equal to the current NetworkButtons; otherwise,
// false.
public override bool Equals(object obj)
{
if (obj is OfflineButtons other)
{
return Equals(other);
}
return false;
}
//
// Summary:
// Serves as the default hash function.
//
// Returns:
// A hash code for the current NetworkButtons.
public override int GetHashCode()
{
return _bits;
}
}