/// <summary> /// Return the given pen button. /// </summary> /// <param name="button">Pen button to return.</param> /// <exception cref="InvalidEnumArgumentException"><paramref name="button"/> is not a valid pen button.</exception> public ButtonControl this[PenButton button] { get { switch (button) { case PenButton.Tip: return(tip); case PenButton.Eraser: return(eraser); case PenButton.BarrelFirst: return(firstBarrelButton); case PenButton.BarrelSecond: return(secondBarrelButton); case PenButton.BarrelThird: return(thirdBarrelButton); case PenButton.BarrelFourth: return(fourthBarrelButton); case PenButton.InRange: return(inRange); default: throw new InvalidEnumArgumentException(nameof(button), (int)button, typeof(PenButton)); } } }
/// <summary> /// Set or unset the bit in <see cref="buttons"/> for the given <paramref name="button"/>. /// </summary> /// <param name="button">Button whose state to set.</param> /// <param name="state">Whether the button is on or off.</param> /// <returns>Same PenState with an updated <see cref="buttons"/> mask.</returns> public PenState WithButton(PenButton button, bool state = true) { if (state) { buttons |= (ushort)(1 << (int)button); } else { buttons &= (ushort)~(1 << (int)button); } return(this); }
/// <summary> /// Set or unset the bit in <see cref="buttons"/> for the given <paramref name="button"/>. /// </summary> /// <param name="button">Button whose state to set.</param> /// <param name="state">Whether the button is on or off.</param> /// <returns>Same PenState with an updated <see cref="buttons"/> mask.</returns> public PenState WithButton(PenButton button, bool state = true) { Debug.Assert((int)button < 16, $"Expected button < 16, so we fit into the 16 bit wide bitmask"); var bit = 1U << (int)button; if (state) { buttons |= (ushort)bit; } else { buttons &= (ushort)~bit; } return(this); }