Browse Source

Added better offset/constant maths

Robert Payne 10 years ago
parent
commit
03ba3d603e
1 changed files with 82 additions and 34 deletions
  1. 82 34
      Source/Constraint.swift

+ 82 - 34
Source/Constraint.swift

@@ -586,27 +586,51 @@ private extension NSLayoutAttribute {
         }
         // CGPoint
         else if let point = value as? CGPoint {
-            if self == .Left || self == .CenterX {
-                return point.x
-            } else if self == .Top || self == .CenterY {
-                return point.y
-            } else if self == .Right {
-                return -point.x
-            } else if self == .Bottom {
-                return -point.y
+            #if os(iOS)
+            switch self {
+            case .Left, .CenterX, .LeftMargin, .CenterXWithinMargins: return point.x
+            case .Top, .CenterY, .TopMargin, .CenterYWithinMargins, .Baseline, .FirstBaseline: return point.y
+            case .Right, .RightMargin: return -point.x
+            case .Bottom, .BottomMargin: return -point.y
+            case .Leading, .LeadingMargin: return (Config.interfaceLayoutDirection == .LeftToRight) ? point.x : -point.x
+            case .Trailing, .TrailingMargin: return (Config.interfaceLayoutDirection == .LeftToRight) ? -point.x : point.x
+            case .Width, .Height, .NotAnAttribute: return CGFloat(0)
             }
+            #else
+            switch self {
+            case .Left, .CenterX: return point.x
+            case .Top, .CenterY, .Baseline: return point.y
+            case .Right: return -point.x
+            case .Bottom: return -point.y
+            case .Leading: return (Config.interfaceLayoutDirection == .LeftToRight) ? point.x : -point.x
+            case .Trailing: return (Config.interfaceLayoutDirection == .LeftToRight) ? -point.x : point.x
+            case .Width, .Height, .NotAnAttribute: return CGFloat(0)
+            }
+            #endif
         }
         // EdgeInsets
         else if let insets = value as? EdgeInsets {
-            if self == .Left {
-                return insets.left
-            } else if self == .Top {
-                return insets.top
-            } else if self == .Right {
-                return -insets.right
-            } else if self == .Bottom {
-                return -insets.bottom
+            #if os(iOS)
+            switch self {
+            case .Left, .CenterX, .LeftMargin, .CenterXWithinMargins: return insets.left
+            case .Top, .CenterY, .TopMargin, .CenterYWithinMargins, .Baseline, .FirstBaseline: return insets.top
+            case .Right, .RightMargin: return -insets.right
+            case .Bottom, .BottomMargin: return -insets.bottom
+            case .Leading, .LeadingMargin: return (Config.interfaceLayoutDirection == .LeftToRight) ? insets.left : -insets.right
+            case .Trailing, .TrailingMargin: return (Config.interfaceLayoutDirection == .LeftToRight) ? -insets.right : insets.left
+            case .Width, .Height, .NotAnAttribute: return CGFloat(0)
+            }
+            #else
+            switch self {
+            case .Left, .CenterX: return insets.left
+            case .Top, .CenterY, .Baseline: return insets.top
+            case .Right: return -insets.right
+            case .Bottom: return -insets.bottom
+            case .Leading: return (Config.interfaceLayoutDirection == .LeftToRight) ? insets.left : -insets.right
+            case .Trailing: return (Config.interfaceLayoutDirection == .LeftToRight) ? -insets.right : insets.left
+            case .Width, .Height, .NotAnAttribute: return CGFloat(0)
             }
+            #endif
         }
         
         return CGFloat(0)
@@ -641,29 +665,53 @@ private extension NSLayoutAttribute {
                 return size.height
             }
         }
-        // CGPoint
+            // CGPoint
         else if let point = value as? CGPoint {
-            if self == .Left || self == .CenterX {
-                return point.x
-            } else if self == .Top || self == .CenterY {
-                return point.y
-            } else if self == .Right {
-                return point.x
-            } else if self == .Bottom {
-                return point.y
+            #if os(iOS)
+            switch self {
+            case .Left, .CenterX, .LeftMargin, .CenterXWithinMargins: return point.x
+            case .Top, .CenterY, .TopMargin, .CenterYWithinMargins, .Baseline, .FirstBaseline: return point.y
+            case .Right, .RightMargin: return -point.x
+            case .Bottom, .BottomMargin: return -point.y
+            case .Leading, .LeadingMargin: return (Config.interfaceLayoutDirection == .LeftToRight) ? point.x : -point.x
+            case .Trailing, .TrailingMargin: return (Config.interfaceLayoutDirection == .LeftToRight) ? -point.x : point.x
+            case .Width, .Height, .NotAnAttribute: return CGFloat(0)
             }
+            #else
+            switch self {
+            case .Left, .CenterX: return point.x
+            case .Top, .CenterY, .Baseline: return point.y
+            case .Right: return -point.x
+            case .Bottom: return -point.y
+            case .Leading: return (Config.interfaceLayoutDirection == .LeftToRight) ? point.x : -point.x
+            case .Trailing: return (Config.interfaceLayoutDirection == .LeftToRight) ? -point.x : point.x
+            case .Width, .Height, .NotAnAttribute: return CGFloat(0)
+            }
+            #endif
         }
-        // EdgeInsets
+            // EdgeInsets
         else if let insets = value as? EdgeInsets {
-            if self == .Left {
-                return insets.left
-            } else if self == .Top {
-                return insets.top
-            } else if self == .Right {
-                return -insets.right
-            } else if self == .Bottom {
-                return -insets.bottom
+            #if os(iOS)
+            switch self {
+            case .Left, .CenterX, .LeftMargin, .CenterXWithinMargins: return insets.left
+            case .Top, .CenterY, .TopMargin, .CenterYWithinMargins, .Baseline, .FirstBaseline: return insets.top
+            case .Right, .RightMargin: return -insets.right
+            case .Bottom, .BottomMargin: return -insets.bottom
+            case .Leading, .LeadingMargin: return (Config.interfaceLayoutDirection == .LeftToRight) ? insets.left : -insets.right
+            case .Trailing, .TrailingMargin: return (Config.interfaceLayoutDirection == .LeftToRight) ? -insets.right : insets.left
+            case .Width, .Height, .NotAnAttribute: return CGFloat(0)
+            }
+            #else
+            switch self {
+            case .Left, .CenterX: return insets.left
+            case .Top, .CenterY, .Baseline: return insets.top
+            case .Right: return -insets.right
+            case .Bottom: return -insets.bottom
+            case .Leading: return (Config.interfaceLayoutDirection == .LeftToRight) ? insets.left : -insets.right
+            case .Trailing: return (Config.interfaceLayoutDirection == .LeftToRight) ? -insets.right : insets.left
+            case .Width, .Height, .NotAnAttribute: return CGFloat(0)
             }
+            #endif
         }
         
         return CGFloat(0);