Skip to content

[swift-3.0-branch] SpriteKit overlay: add a better variant of the SKWarpGeometryGrid() initializer overlay #3984

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 53 additions & 3 deletions stdlib/public/SDK/SpriteKit/SpriteKit.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

@_exported import SpriteKit
import simd

Expand All @@ -22,13 +34,51 @@ public typealias SKColor = UIColor
@available(tvOS, introduced: 10.0)
@available(watchOS, introduced: 3.0)
extension SKWarpGeometryGrid {
/* init with the specified dimensions, source and dest positions. */
public convenience init(columns: Int, rows: Int, sourcePositions: [simd.float2]? = nil, destinationPositions: [simd.float2]? = nil) {
self.init(__columns: columns, rows: rows, sourcePositions: sourcePositions, destPositions:destinationPositions)
/// Create a grid of the specified dimensions, source and destination positions.
///
/// Grid dimensions (columns and rows) refer to the number of faces in each dimension. The
/// number of vertices required for a given dimension is equal to (cols + 1) * (rows + 1).
///
/// SourcePositions are normalized (0.0 - 1.0) coordinates to determine the source content.
///
/// DestinationPositions are normalized (0.0 - 1.0) positional coordinates with respect to
/// the node's native size. Values outside the (0.0-1.0) range are perfectly valid and
/// correspond to positions outside of the native undistorted bounds.
///
/// Source and destination positions are provided in row-major order staring from the top-left.
/// For example the indices for a 2x2 grid would be as follows:
///
/// [0]---[1]---[2]
/// | | |
/// [3]---[4]---[5]
/// | | |
/// [6]---[7]---[8]
///
/// - Parameter columns: the number of columns to initialize the SKWarpGeometryGrid with
/// - Parameter rows: the number of rows to initialize the SKWarpGeometryGrid with
/// - Parameter sourcePositions: the source positions for the SKWarpGeometryGrid to warp from
/// - Parameter destinationPositions: the destination positions for SKWarpGeometryGrid to warp to
public convenience init(columns: Int, rows: Int, sourcePositions: [simd.float2] = [float2](), destinationPositions: [simd.float2] = [float2]()) {
let requiredElementsCount = (columns + 1) * (rows + 1)
switch (destinationPositions.count, sourcePositions.count) {
case (0, 0):
self.init(__columns: columns, rows: rows, sourcePositions: nil, destPositions: nil)
case (let dests, 0):
_precondition(dests == requiredElementsCount, "Mismatch found between rows/columns and positions.")
self.init(__columns: columns, rows: rows, sourcePositions: nil, destPositions: destinationPositions)
case (0, let sources):
_precondition(sources == requiredElementsCount, "Mismatch found between rows/columns and positions.")
self.init(__columns: columns, rows: rows, sourcePositions: sourcePositions, destPositions: nil)
case (let dests, let sources):
_precondition(dests == requiredElementsCount && sources == requiredElementsCount, "Mismatch found between rows/columns and positions.")
self.init(__columns: columns, rows: rows, sourcePositions: sourcePositions, destPositions: destinationPositions)
}
}

public func replacingBySourcePositions(positions source: [simd.float2]) -> SKWarpGeometryGrid {
return self.__replacingSourcePositions(source)
}

public func replacingByDestinationPositions(positions destination: [simd.float2]) -> SKWarpGeometryGrid {
return self.__replacingDestPositions(destination)
}
Expand Down
50 changes: 26 additions & 24 deletions validation-test/stdlib/SpriteKit.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-run-simple-swift | FileCheck %s
// RUN: %target-run-simple-swift
// REQUIRES: executable_test

// REQUIRES: objc_interop
Expand All @@ -8,39 +8,41 @@ import StdlibUnittest
import Foundation
import SpriteKit

var SpriteKitTests = TestSuite("SpriteKit")

// Check that the subscript is there.
@available(OSX,introduced: 10.10)
@available(iOS,introduced: 8.0)
@available(tvOS,introduced: 8.0)
@available(watchOS,introduced: 2.0)
func testSubscript(_ node: SKNode) {
var _: [SKNode] = node["me"]
var result = node["me"]
expectType(Array<SKNode>.self, &result)
}

// SKColor is NSColor on OS X and UIColor on iOS.

var r = CGFloat(0)
var g = CGFloat(0)
var b = CGFloat(0)
var a = CGFloat(0)
var color = SKColor.red
color.getRed(&r, green:&g, blue:&b, alpha:&a)
print("color \(r) \(g) \(b) \(a)")
// CHECK: color 1.0 0.0 0.0 1.0

SpriteKitTests.test("SKColor/TypeEquivalence") {
// SKColor is NSColor on OS X and UIColor on iOS.
#if os(OSX)
func f(_ c: NSColor) {
print("colortastic")
}
expectEqualType(NSColor.self, SKColor.self)
#elseif os(iOS) || os(tvOS) || os(watchOS)
func f(_ c: UIColor) {
print("colortastic")
}
expectEqualType(UIColor.self, SKColor.self)
#else
_UnknownOSError()
#endif
f(color)
// CHECK: colortastic
}

var SpriteKitTests = TestSuite("SpriteKit")
SpriteKitTests.test("getRed(_:green:blue:alpha:)") {
var r: CGFloat = 0.0
var g: CGFloat = 0.0
var b: CGFloat = 0.0
var a: CGFloat = 0.0
let color = SKColor.red
color.getRed(&r, green: &g, blue: &b, alpha: &a)
expectEqual(1.0, r)
expectEqual(0.0, g)
expectEqual(0.0, b)
expectEqual(1.0, a)
}

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

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

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

Expand Down