Skip to content

Commit 9ad7cbd

Browse files
committed
SpriteKit overlay: add a better variant of the SKWarpGeometryGrid() initializer overlay
1 parent 03415be commit 9ad7cbd

File tree

2 files changed

+79
-27
lines changed

2 files changed

+79
-27
lines changed

stdlib/public/SDK/SpriteKit/SpriteKit.swift

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
113
@_exported import SpriteKit
214
import simd
315

@@ -22,13 +34,51 @@ public typealias SKColor = UIColor
2234
@available(tvOS, introduced: 10.0)
2335
@available(watchOS, introduced: 3.0)
2436
extension SKWarpGeometryGrid {
25-
/* init with the specified dimensions, source and dest positions. */
26-
public convenience init(columns: Int, rows: Int, sourcePositions: [simd.float2]? = nil, destinationPositions: [simd.float2]? = nil) {
27-
self.init(__columns: columns, rows: rows, sourcePositions: sourcePositions, destPositions:destinationPositions)
37+
/// Create a grid of the specified dimensions, source and destination positions.
38+
///
39+
/// Grid dimensions (columns and rows) refer to the number of faces in each dimension. The
40+
/// number of vertices required for a given dimension is equal to (cols + 1) * (rows + 1).
41+
///
42+
/// SourcePositions are normalized (0.0 - 1.0) coordinates to determine the source content.
43+
///
44+
/// DestinationPositions are normalized (0.0 - 1.0) positional coordinates with respect to
45+
/// the node's native size. Values outside the (0.0-1.0) range are perfectly valid and
46+
/// correspond to positions outside of the native undistorted bounds.
47+
///
48+
/// Source and destination positions are provided in row-major order staring from the top-left.
49+
/// For example the indices for a 2x2 grid would be as follows:
50+
///
51+
/// [0]---[1]---[2]
52+
/// | | |
53+
/// [3]---[4]---[5]
54+
/// | | |
55+
/// [6]---[7]---[8]
56+
///
57+
/// - Parameter columns: the number of columns to initialize the SKWarpGeometryGrid with
58+
/// - Parameter rows: the number of rows to initialize the SKWarpGeometryGrid with
59+
/// - Paremeter sourcePositions: the source positions for the SKWarpGeometryGrid to warp from
60+
/// - Parameter destinationPositions: the destination positions for SKWarpGeometryGrid to warp to
61+
public convenience init(columns: Int, rows: Int, sourcePositions: [simd.float2] = [float2](), destinationPositions: [simd.float2] = [float2]()) {
62+
let requiredElementsCount = (columns + 1) * (rows + 1)
63+
switch (destinationPositions.count, sourcePositions.count) {
64+
case (0, 0):
65+
self.init(__columns: columns, rows: rows, sourcePositions: nil, destPositions: nil)
66+
case (let dests, 0):
67+
_precondition(dests == requiredElementsCount, "Mismatch found between rows/columns and positions.")
68+
self.init(__columns: columns, rows: rows, sourcePositions: nil, destPositions: destinationPositions)
69+
case (0, let sources):
70+
_precondition(sources == requiredElementsCount, "Mismatch found between rows/columns and positions.")
71+
self.init(__columns: columns, rows: rows, sourcePositions: sourcePositions, destPositions: nil)
72+
case (let dests, let sources):
73+
_precondition(dests == requiredElementsCount && sources == requiredElementsCount, "Mismatch found between rows/columns and positions.")
74+
self.init(__columns: columns, rows: rows, sourcePositions: sourcePositions, destPositions: destinationPositions)
75+
}
2876
}
77+
2978
public func replacingBySourcePositions(positions source: [simd.float2]) -> SKWarpGeometryGrid {
3079
return self.__replacingSourcePositions(source)
3180
}
81+
3282
public func replacingByDestinationPositions(positions destination: [simd.float2]) -> SKWarpGeometryGrid {
3383
return self.__replacingDestPositions(destination)
3484
}

validation-test/stdlib/SpriteKit.swift

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-run-simple-swift | FileCheck %s
1+
// RUN: %target-run-simple-swift
22
// REQUIRES: executable_test
33

44
// REQUIRES: objc_interop
@@ -8,39 +8,41 @@ import StdlibUnittest
88
import Foundation
99
import SpriteKit
1010

11+
var SpriteKitTests = TestSuite("SpriteKit")
12+
1113
// Check that the subscript is there.
1214
@available(OSX,introduced: 10.10)
1315
@available(iOS,introduced: 8.0)
1416
@available(tvOS,introduced: 8.0)
1517
@available(watchOS,introduced: 2.0)
1618
func testSubscript(_ node: SKNode) {
17-
var _: [SKNode] = node["me"]
19+
var result = node["me"]
20+
expectType(Array<SKNode>.self, &result)
1821
}
1922

20-
// SKColor is NSColor on OS X and UIColor on iOS.
21-
22-
var r = CGFloat(0)
23-
var g = CGFloat(0)
24-
var b = CGFloat(0)
25-
var a = CGFloat(0)
26-
var color = SKColor.red
27-
color.getRed(&r, green:&g, blue:&b, alpha:&a)
28-
print("color \(r) \(g) \(b) \(a)")
29-
// CHECK: color 1.0 0.0 0.0 1.0
30-
23+
SpriteKitTests.test("SKColor/TypeEquivalence") {
24+
// SKColor is NSColor on OS X and UIColor on iOS.
3125
#if os(OSX)
32-
func f(_ c: NSColor) {
33-
print("colortastic")
34-
}
26+
expectEqualType(NSColor.self, SKColor.self)
3527
#elseif os(iOS) || os(tvOS) || os(watchOS)
36-
func f(_ c: UIColor) {
37-
print("colortastic")
38-
}
28+
expectEqualType(UIColor.self, SKColor.self)
29+
#else
30+
_UnknownOSError()
3931
#endif
40-
f(color)
41-
// CHECK: colortastic
32+
}
4233

43-
var SpriteKitTests = TestSuite("SpriteKit")
34+
SpriteKitTests.test("getRed(_:green:blue:alpha:)") {
35+
var r: CGFloat = 0.0
36+
var g: CGFloat = 0.0
37+
var b: CGFloat = 0.0
38+
var a: CGFloat = 0.0
39+
let color = SKColor.red
40+
color.getRed(&r, green: &g, blue: &b, alpha: &a)
41+
expectEqual(1.0, r)
42+
expectEqual(0.0, g)
43+
expectEqual(0.0, b)
44+
expectEqual(1.0, a)
45+
}
4446

4547
if #available(OSX 10.12, iOS 10.0, tvOS 10.0, watchOS 3.0, *) {
4648
SpriteKitTests.test("SKNode.setValue(_:forAttribute:)") {
@@ -62,11 +64,11 @@ if #available(OSX 10.12, iOS 10.0, tvOS 10.0, watchOS 3.0, *) {
6264
warpGrid = warpGrid.replacingByDestinationPositions(positions: [float2(1.0), float2(2.0), float2(3.0), float2(4.0)])
6365
expectEqual(warpGrid.destPosition(at: 0).x, 1.0)
6466

65-
warpGrid = SKWarpGeometryGrid(columns: 1, rows: 1, sourcePositions: nil, destinationPositions: [float2(1.0), float2(2.0), float2(3.0), float2(4.0)])
67+
warpGrid = SKWarpGeometryGrid(columns: 1, rows: 1, destinationPositions: [float2(1.0), float2(2.0), float2(3.0), float2(4.0)])
6668
expectEqual(warpGrid.destPosition(at: 0).x, 1.0)
6769
expectEqual(warpGrid.sourcePosition(at: 0).x, 0.0)
6870

69-
warpGrid = SKWarpGeometryGrid(columns: 1, rows: 1, sourcePositions: [float2(1.0), float2(2.0), float2(3.0), float2(4.0)], destinationPositions: nil)
71+
warpGrid = SKWarpGeometryGrid(columns: 1, rows: 1, sourcePositions: [float2(1.0), float2(2.0), float2(3.0), float2(4.0)])
7072
expectEqual(warpGrid.destPosition(at: 0).x, 0.0)
7173
expectEqual(warpGrid.sourcePosition(at: 0).x, 1.0)
7274

0 commit comments

Comments
 (0)