From 149332e6e6a9d2f3f30f6030f7a1a064a2c6b29d Mon Sep 17 00:00:00 2001 From: Alisa Mylnikova Date: Fri, 2 Nov 2018 13:58:40 +0700 Subject: [PATCH] Fix z positions test --- .../Animation/AnimationUtilsTests.swift | 45 ++++++++++++------- .../Cache/AnimationCache.swift | 4 +- Source/render/NodeRenderer.swift | 4 +- Source/views/MacawView.swift | 9 ++-- 4 files changed, 38 insertions(+), 24 deletions(-) diff --git a/MacawTests/Animation/AnimationUtilsTests.swift b/MacawTests/Animation/AnimationUtilsTests.swift index 307e4e41..b776b2c6 100644 --- a/MacawTests/Animation/AnimationUtilsTests.swift +++ b/MacawTests/Animation/AnimationUtilsTests.swift @@ -13,27 +13,38 @@ class AnimationUtilsTests: XCTestCase { func testIndex() { let rootGroup = Group() - let a = Node() + let a = Shape(form: Locus()) rootGroup.contents.append(a) - let aGroup = Group() - let b = Node() - let c = Node() - aGroup.contents.append(b) - aGroup.contents.append(c) - rootGroup.contents.append(aGroup) + let bGroup = Group() + let c = Shape(form: Locus()) + let d = Shape(form: Locus()) + bGroup.contents.append(c) + bGroup.contents.append(d) + rootGroup.contents.append(bGroup) - let d = Node() - let e = Node() - rootGroup.contents.append(d) + let e = Shape(form: Locus()) + let f = Shape(form: Locus()) rootGroup.contents.append(e) + rootGroup.contents.append(f) + + let view = MacawView() + view.node = rootGroup + view.draw(CGRect(x: 0, y: 0, width: 100, height: 100)) + let rootRenderer = view.renderer as? GroupRenderer + let aRenderer = rootRenderer?.renderers[0] + let bRenderer = rootRenderer?.renderers[1] as? GroupRenderer + let cRenderer = bRenderer?.renderers[0] + let dRenderer = bRenderer?.renderers[1] + let eRenderer = rootRenderer?.renderers[2] + let fRenderer = rootRenderer?.renderers[3] - XCTAssert(AnimationUtils.absoluteIndex(rootGroup) == 0) - XCTAssert(AnimationUtils.absoluteIndex(a) == 1) - XCTAssert(AnimationUtils.absoluteIndex(aGroup) == 2) - XCTAssert(AnimationUtils.absoluteIndex(b) == 3) - XCTAssert(AnimationUtils.absoluteIndex(c) == 4 ) - XCTAssert(AnimationUtils.absoluteIndex(d) == 5 ) - XCTAssert(AnimationUtils.absoluteIndex(e) == 6 ) + XCTAssert(rootRenderer?.zPosition == 0) + XCTAssert(aRenderer?.zPosition == 1) + XCTAssert(bRenderer?.zPosition == 2) + XCTAssert(cRenderer?.zPosition == 3) + XCTAssert(dRenderer?.zPosition == 4 ) + XCTAssert(eRenderer?.zPosition == 5 ) + XCTAssert(fRenderer?.zPosition == 6 ) } } diff --git a/Source/animation/types/animation_generators/Cache/AnimationCache.swift b/Source/animation/types/animation_generators/Cache/AnimationCache.swift index adc8ce89..b000f92b 100644 --- a/Source/animation/types/animation_generators/Cache/AnimationCache.swift +++ b/Source/animation/types/animation_generators/Cache/AnimationCache.swift @@ -32,7 +32,7 @@ class AnimationCache { return ShapeLayer() } - if let cachedLayer = layerCache[renderer] { + if let cachedLayer = layerCache[renderer] { cachedLayer.linksCounter += 1 return cachedLayer.layer } @@ -148,7 +148,7 @@ class AnimationCache { func isAnimating(_ node: Node) -> Bool { - let renderer = layerCache.keys.filter{ $0.node() === node }.first + let renderer = layerCache.keys.filter { $0.node() === node }.first if let renderer = renderer, let _ = layerCache[renderer] { return true } diff --git a/Source/render/NodeRenderer.swift b/Source/render/NodeRenderer.swift index c824bfc2..edaa3de3 100644 --- a/Source/render/NodeRenderer.swift +++ b/Source/render/NodeRenderer.swift @@ -10,7 +10,7 @@ enum ColoringMode { case rgb, greyscale, alphaOnly } -class NodeRenderer{ +class NodeRenderer { weak var view: MView? weak var parentRenderer: NodeRenderer? @@ -72,7 +72,7 @@ class NodeRenderer{ open func dispose() { removeObservers() - node()?.animationObservers = node()?.animationObservers.filter{ !($0 as? NodeRenderer === self) } ?? [] + node()?.animationObservers = node()?.animationObservers.filter { !($0 as? NodeRenderer === self) } ?? [] } open func node() -> Node? { diff --git a/Source/views/MacawView.swift b/Source/views/MacawView.swift index a5a47fe5..3eef807a 100644 --- a/Source/views/MacawView.swift +++ b/Source/views/MacawView.swift @@ -196,13 +196,16 @@ open class MacawView: MView, MGestureRecognizerDelegate { renderer.render(in: ctx, force: false, opacity: node.opacity) } - private func calculateZPosition(_ nodeRenderer: NodeRenderer?, currentIndex: Int = 0) { + private func calculateZPosition(_ nodeRenderer: NodeRenderer?, currentIndex: Int = 0) -> Int { nodeRenderer?.zPosition = currentIndex if let groupRenderer = nodeRenderer as? GroupRenderer { - for (i, child) in groupRenderer.renderers.enumerated() { - calculateZPosition(child, currentIndex: currentIndex + i + 1) + var i = currentIndex + 1 + for child in groupRenderer.renderers { + i = calculateZPosition(child, currentIndex: i) } + return i } + return currentIndex + 1 } public final func findNodeAt(location: CGPoint) -> Node? {