Axis.swift 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /// An axis in a 2D coordinate system.
  2. public enum Axis: Sendable, CaseIterable {
  3. /// The horizontal axis.
  4. case horizontal
  5. /// The vertical axis.
  6. case vertical
  7. /// Gets the orientation with this axis as its main axis.
  8. var orientation: Orientation {
  9. switch self {
  10. case .horizontal:
  11. .horizontal
  12. case .vertical:
  13. .vertical
  14. }
  15. }
  16. /// A set of axes represented as an efficient bit field.
  17. public struct Set: OptionSet, Sendable {
  18. // Required to satisfy older compilers (5.10) due to our custom
  19. // `contains(_:)` overload below which confuses the inference
  20. // of SetAlgebra's Element associated type.
  21. public typealias Element = Self
  22. /// The horizontal axis.
  23. public static let horizontal = Set(rawValue: 1)
  24. /// The vertical axis.
  25. public static let vertical = Set(rawValue: 2)
  26. public var rawValue: UInt8
  27. public init(rawValue: UInt8) {
  28. self.rawValue = rawValue
  29. }
  30. /// Gets whether a given member is a member of the option set.
  31. public func contains(_ member: Axis) -> Bool {
  32. switch member {
  33. case .horizontal:
  34. contains(Axis.Set.horizontal)
  35. case .vertical:
  36. contains(Axis.Set.vertical)
  37. }
  38. }
  39. }
  40. }