Skip to content
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
6 changes: 3 additions & 3 deletions Source/animation/AnimationUtils.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import Foundation

class AnimationUtils {
class func absolutePosition(_ node: Node) -> Transform {
return AnimationUtils.absoluteTransform(node, pos: node.place)
class func absolutePosition(_ node: Node, view: MacawView? = nil) -> Transform {
return AnimationUtils.absoluteTransform(node, pos: node.place, view: view)
}

class func absoluteTransform(_ node: Node, pos: Transform) -> Transform {
class func absoluteTransform(_ node: Node, pos: Transform, view: MacawView? = nil) -> Transform {
var transform = pos
var parent = nodesMap.parents(node).first
while parent != .none {
Expand Down
11 changes: 1 addition & 10 deletions Source/animation/types/ShapeAnimation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,7 @@ class ShapeAnimation: AnimationImpl<Shape> {

let interpolationFunc = { (t: Double) -> Shape in
if t == 0 {
return Shape(form: animatedNode.form,
fill: animatedNode.fill,
stroke: animatedNode.stroke,
place: animatedNode.place,
opaque: animatedNode.opaque,
opacity: animatedNode.opacity,
clip: animatedNode.clip,
effect: animatedNode.effect,
visible: animatedNode.visible,
tag: animatedNode.tag)
return animatedNode
}

return finalValue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func addShapeAnimation(_ animation: BasicAnimation, sceneLayer: CALayer, animati

// Creating proper animation
let generatedAnim = generateShapeAnimation(
from: fromShape,
from: mutatingShape,
to: toShape,
duration: duration,
renderTransform: layer.renderTransform!)
Expand Down Expand Up @@ -78,6 +78,12 @@ func addShapeAnimation(_ animation: BasicAnimation, sceneLayer: CALayer, animati

if !animation.autoreverses {
let currentShape = shapeAnimation.getVFunc()(t)
mutatingShape.place = currentShape.place
mutatingShape.opaque = currentShape.opaque
mutatingShape.opacity = currentShape.opacity
mutatingShape.clip = currentShape.clip
mutatingShape.mask = currentShape.mask
mutatingShape.effect = currentShape.effect
mutatingShape.form = currentShape.form
mutatingShape.stroke = currentShape.stroke
mutatingShape.fill = currentShape.fill
Expand Down Expand Up @@ -137,6 +143,15 @@ fileprivate func generateShapeAnimation(from: Shape, to: Shape, duration: Double

group.animations = [pathAnimation]

// Transform
let scaleAnimation = CABasicAnimation(keyPath: "transform")
scaleAnimation.duration = duration
let view = nodesMap.getView(from)
scaleAnimation.fromValue = CATransform3DMakeAffineTransform(AnimationUtils.absolutePosition(from, view: view).toCG())
scaleAnimation.toValue = CATransform3DMakeAffineTransform(AnimationUtils.absolutePosition(to, view: view).toCG())

group.animations?.append(scaleAnimation)

// Fill
let fromFillColor = from.fill as? Color ?? Color.clear
let toFillColor = to.fill as? Color ?? Color.clear
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ func transformAnimationByFunc(_ node: Node, valueFunc: (Double) -> Transform, du
}

timeValues.append(dt)
let value = AnimationUtils.absoluteTransform(node, pos: valueFunc(offset + dt))

let view = nodesMap.getView(node)
let value = AnimationUtils.absoluteTransform(node, pos: valueFunc(offset + dt), view: view)
let cgValue = CATransform3DMakeAffineTransform(value.toCG())
transformValues.append(cgValue)
}
Expand Down
13 changes: 2 additions & 11 deletions Source/model/scene/Group.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ open class Group: Node {
tag: tag
)

self.contents = contents
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You change both contentsVar and contents which is not necessary, because contentsVar will be updated automatically.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

node property for contentsVar is being set up here, not the contents which indeed will be updated automatically

self.contentsVar.node = self
}

Expand Down Expand Up @@ -64,17 +65,7 @@ open class Group: Node {
}

override open var bounds: Rect? {
var union: Rect?

contents.forEach { node in
guard let nodeBounds = node.bounds?.applying(node.place) else {
return
}

union = union?.union(rect: nodeBounds) ?? nodeBounds
}

return union
return BoundsUtils.getNodesBounds(contents)
}

override func shouldCheckForPressed() -> Bool {
Expand Down
11 changes: 11 additions & 0 deletions Source/utils/BoundsUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,15 @@ final internal class BoundsUtils {
return CGRect(x: destX, y: destY, width: newSize.w, height: newSize.h)
}
}

class func getNodesBounds(_ nodes: [Node]) -> Rect? {
var union: Rect?
nodes.forEach { node in
guard let nodeBounds = node.bounds?.applying(node.place) else {
return
}
union = union?.union(rect: nodeBounds) ?? nodeBounds
}
return union
}
}
13 changes: 11 additions & 2 deletions Source/views/MacawView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -592,12 +592,21 @@ private class LayoutHelper {

public func getTransform(_ node: Node, _ layout: ContentLayout, _ size: Size) -> CGAffineTransform {
setSize(size: size)
if let rect = getNodeBounds(node: node) {
var rect = node.bounds
if let canvas = node as? SVGCanvas {
if let view = nodesMap.getView(canvas) {
rect = canvas.layout(size: view.bounds.size.toMacaw()).rect()
} else {
rect = BoundsUtils.getNodesBounds(canvas.contents)
}
}

if let rect = rect {
setRect(rect: rect)
if let transform = prevTransform {
return transform
}
return setTransform(transform: layout.layout(rect: prevRect!, into: size).toCG())
return setTransform(transform: layout.layout(rect: prevRect!, into: size).move(dx: rect.x, dy: rect.y).toCG())
}
return CGAffineTransform.identity
}
Expand Down