-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Swift-y
ViewOption
enum & SCNView/Controller convenience inits
`SCNView` still hasn't gotten a modern Swift associated-type-enum type for its options init param (still using unchecked Obj-C-ish [String:Any]), so I'm just adding my own `ViewOption` enum with conversions to/from ObjC-ish dictionaries. * Created `SCNViewController.ViewOption` enum with associated types for each case. * Gave `SCNViewController` a convenience init that takes a `[ViewOption]`. * Gave `SCNView` a convenience init that takes a `[ViewOption]`. * Updated tests to use `[ViewOption]` init variant.
- Loading branch information
Showing
5 changed files
with
112 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// SCNViewExtensions | ||
// @author: Slipp Douglas Thompson | ||
// @license: Public Domain per The Unlicense. See accompanying LICENSE file or <http://unlicense.org/>. | ||
|
||
import SceneKit | ||
|
||
|
||
|
||
public extension SCNView | ||
{ | ||
convenience init(frame: CGRect, options: [SCNViewController.ViewOption]? = nil) | ||
{ | ||
self.init(frame: frame, options: options?.asObjCStyleOptions) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// ViewOptions | ||
// @author: Slipp Douglas Thompson | ||
// @license: Public Domain per The Unlicense. See accompanying LICENSE file or <http://unlicense.org/>. | ||
|
||
import SceneKit | ||
|
||
|
||
|
||
public extension SCNViewController | ||
{ | ||
enum ViewOption : Equatable | ||
{ | ||
case preferLowPowerDevice(Bool) | ||
case preferredDevice(MTLDevice) | ||
case preferredRenderingAPI(SCNRenderingAPI) | ||
|
||
|
||
public static func == (lhs: ViewOption, rhs: ViewOption) -> Bool { | ||
switch (lhs, rhs) { | ||
case (.preferLowPowerDevice(let lhsValue), .preferLowPowerDevice(let rhsValue)): | ||
return lhsValue == rhsValue | ||
case (.preferredDevice(let lhsValue), .preferredDevice(let rhsValue)): | ||
if #available(iOS 11.0, macOS 10.13, macCatalyst 13.0, tvOS 11.0, *) { | ||
return lhsValue.registryID == rhsValue.registryID | ||
} else { | ||
return lhsValue.name == rhsValue.name | ||
} | ||
case (.preferredRenderingAPI(let lhsValue), .preferredRenderingAPI(let rhsValue)): | ||
return lhsValue == rhsValue | ||
default: | ||
return false | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
extension Array where Element == SCNViewController.ViewOption | ||
{ | ||
public init(objcStyleOptions: [SCNView.Option.RawValue:Any]) | ||
{ | ||
self = [] | ||
if let objcValue = objcStyleOptions[SCNView.Option.preferLowPowerDevice.rawValue] { | ||
append(.preferLowPowerDevice((objcValue as! NSNumber).boolValue)) | ||
} | ||
if let objcValue = objcStyleOptions[SCNView.Option.preferredDevice.rawValue] { | ||
append(.preferredDevice(objcValue as! MTLDevice)) | ||
} | ||
if let objcValue = objcStyleOptions[SCNView.Option.preferredRenderingAPI.rawValue] { | ||
append(.preferredRenderingAPI(SCNRenderingAPI(rawValue: (objcValue as! NSNumber).uintValue)!)) | ||
} | ||
} | ||
|
||
public var asObjCStyleOptions: [SCNView.Option.RawValue:Any] { | ||
var objcStyleOptions: [SCNView.Option.RawValue:Any] = [:] | ||
|
||
for element in self { | ||
switch element { | ||
case .preferLowPowerDevice(let value): | ||
objcStyleOptions[SCNView.Option.preferLowPowerDevice.rawValue] = NSNumber(value: value) | ||
case .preferredDevice(let value): | ||
objcStyleOptions[SCNView.Option.preferredDevice.rawValue] = value | ||
case .preferredRenderingAPI(let value): | ||
objcStyleOptions[SCNView.Option.preferredRenderingAPI.rawValue] = NSNumber(value: value.rawValue) | ||
} | ||
} | ||
|
||
return objcStyleOptions | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters