1
1
mirror of https://github.com/exyte/Macaw.git synced 2024-09-11 13:15:35 +03:00

add examples + fix BezierPath

This commit is contained in:
manindaniil 2017-08-15 15:42:05 +07:00
parent d71442b4a6
commit 83421226b5
12 changed files with 669 additions and 91 deletions

View File

@ -13,6 +13,12 @@
A753C9EC1F3DB18A006615A4 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = A753C9EB1F3DB18A006615A4 /* Assets.xcassets */; };
A753C9EF1F3DB18A006615A4 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = A753C9ED1F3DB18A006615A4 /* Main.storyboard */; };
A753C9F71F3DB615006615A4 /* tiger.svg in Resources */ = {isa = PBXBuildFile; fileRef = A753C9F61F3DB615006615A4 /* tiger.svg */; };
A75939A21F41C54E000CE329 /* ShapesExampleView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A75939A11F41C54E000CE329 /* ShapesExampleView.swift */; };
A75939A51F41C571000CE329 /* MorphingView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A75939A41F41C571000CE329 /* MorphingView.swift */; };
A75939A81F41C5A8000CE329 /* TransformExampleView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A75939A71F41C5A8000CE329 /* TransformExampleView.swift */; };
A768EAD81F42C98E00F22A17 /* EventsExampleController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A768EAD71F42C98E00F22A17 /* EventsExampleController.swift */; };
A768EADC1F42CC7C00F22A17 /* EasingView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A768EADB1F42CC7C00F22A17 /* EasingView.swift */; };
A768EADE1F42CCBC00F22A17 /* AnimationsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A768EADD1F42CCBC00F22A17 /* AnimationsView.swift */; };
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
@ -24,6 +30,12 @@
A753C9EE1F3DB18A006615A4 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
A753C9F01F3DB18A006615A4 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
A753C9F61F3DB615006615A4 /* tiger.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = tiger.svg; sourceTree = "<group>"; };
A75939A11F41C54E000CE329 /* ShapesExampleView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ShapesExampleView.swift; path = Examples/Shapes/ShapesExampleView.swift; sourceTree = "<group>"; };
A75939A41F41C571000CE329 /* MorphingView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = MorphingView.swift; path = Examples/Morphing/MorphingView.swift; sourceTree = "<group>"; };
A75939A71F41C5A8000CE329 /* TransformExampleView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = TransformExampleView.swift; path = Examples/Transform/TransformExampleView.swift; sourceTree = "<group>"; };
A768EAD71F42C98E00F22A17 /* EventsExampleController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = EventsExampleController.swift; path = Examples/Events/EventsExampleController.swift; sourceTree = "<group>"; };
A768EADB1F42CC7C00F22A17 /* EasingView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = EasingView.swift; path = Examples/Easing/EasingView.swift; sourceTree = "<group>"; };
A768EADD1F42CCBC00F22A17 /* AnimationsView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = AnimationsView.swift; path = Examples/Animations/AnimationsView.swift; sourceTree = "<group>"; };
B752613023F25BBB88A17503 /* Pods-Example-macOS.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Example-macOS.debug.xcconfig"; path = "Pods/Target Support Files/Pods-Example-macOS/Pods-Example-macOS.debug.xcconfig"; sourceTree = "<group>"; };
BEA3A0212E620A41C8362253 /* Pods-Example-macOS.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Example-macOS.release.xcconfig"; path = "Pods/Target Support Files/Pods-Example-macOS/Pods-Example-macOS.release.xcconfig"; sourceTree = "<group>"; };
/* End PBXFileReference section */
@ -78,6 +90,7 @@
A753C9E61F3DB18A006615A4 /* Example-macOS */ = {
isa = PBXGroup;
children = (
A759399F1F41C51A000CE329 /* Examples */,
A753C9E71F3DB18A006615A4 /* AppDelegate.swift */,
A753C9E91F3DB18A006615A4 /* ViewController.swift */,
A753C9EB1F3DB18A006615A4 /* Assets.xcassets */,
@ -88,6 +101,75 @@
path = "Example-macOS";
sourceTree = "<group>";
};
A759399F1F41C51A000CE329 /* Examples */ = {
isa = PBXGroup;
children = (
A75939A01F41C544000CE329 /* Shapes */,
A75939A61F41C58D000CE329 /* Transform */,
A768EADA1F42CBF600F22A17 /* Animations */,
A768EAD91F42CBDC00F22A17 /* SVG */,
A75939A91F41CE45000CE329 /* Easing */,
A75939A31F41C566000CE329 /* Morphing */,
A768EAD61F42C97900F22A17 /* Events */,
);
name = Examples;
sourceTree = "<group>";
};
A75939A01F41C544000CE329 /* Shapes */ = {
isa = PBXGroup;
children = (
A75939A11F41C54E000CE329 /* ShapesExampleView.swift */,
);
name = Shapes;
sourceTree = "<group>";
};
A75939A31F41C566000CE329 /* Morphing */ = {
isa = PBXGroup;
children = (
A75939A41F41C571000CE329 /* MorphingView.swift */,
);
name = Morphing;
sourceTree = "<group>";
};
A75939A61F41C58D000CE329 /* Transform */ = {
isa = PBXGroup;
children = (
A75939A71F41C5A8000CE329 /* TransformExampleView.swift */,
);
name = Transform;
sourceTree = "<group>";
};
A75939A91F41CE45000CE329 /* Easing */ = {
isa = PBXGroup;
children = (
A768EADB1F42CC7C00F22A17 /* EasingView.swift */,
);
name = Easing;
sourceTree = "<group>";
};
A768EAD61F42C97900F22A17 /* Events */ = {
isa = PBXGroup;
children = (
A768EAD71F42C98E00F22A17 /* EventsExampleController.swift */,
);
name = Events;
sourceTree = "<group>";
};
A768EAD91F42CBDC00F22A17 /* SVG */ = {
isa = PBXGroup;
children = (
);
name = SVG;
sourceTree = "<group>";
};
A768EADA1F42CBF600F22A17 /* Animations */ = {
isa = PBXGroup;
children = (
A768EADD1F42CCBC00F22A17 /* AnimationsView.swift */,
);
name = Animations;
sourceTree = "<group>";
};
/* End PBXGroup section */
/* Begin PBXNativeTarget section */
@ -210,8 +292,14 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
A75939A51F41C571000CE329 /* MorphingView.swift in Sources */,
A753C9EA1F3DB18A006615A4 /* ViewController.swift in Sources */,
A75939A21F41C54E000CE329 /* ShapesExampleView.swift in Sources */,
A753C9E81F3DB18A006615A4 /* AppDelegate.swift in Sources */,
A768EAD81F42C98E00F22A17 /* EventsExampleController.swift in Sources */,
A768EADE1F42CCBC00F22A17 /* AnimationsView.swift in Sources */,
A75939A81F41C5A8000CE329 /* TransformExampleView.swift in Sources */,
A768EADC1F42CC7C00F22A17 /* EasingView.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

View File

@ -658,7 +658,7 @@
<customObject id="Voe-Tx-rLC" customClass="AppDelegate" customModule="Example_macOS" customModuleProvider="target"/>
<customObject id="Ady-hI-5gd" userLabel="First Responder" customClass="NSResponder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="75" y="0.0"/>
<point key="canvasLocation" x="58" y="-207"/>
</scene>
<!--Window Controller-->
<scene sceneID="R2V-B0-nI4">
@ -686,22 +686,19 @@
<rect key="frame" x="0.0" y="0.0" width="480" height="480"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<customView translatesAutoresizingMaskIntoConstraints="NO" id="YcN-xy-Wad" customClass="SVGView" customModule="Macaw">
<customView translatesAutoresizingMaskIntoConstraints="NO" id="05R-gU-uKp" customClass="ShapesExampleView" customModule="Example_macOS" customModuleProvider="target">
<rect key="frame" x="0.0" y="0.0" width="480" height="480"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="string" keyPath="fileName" value="tiger"/>
</userDefinedRuntimeAttributes>
</customView>
</subviews>
<constraints>
<constraint firstItem="YcN-xy-Wad" firstAttribute="leading" secondItem="m2S-Jp-Qdl" secondAttribute="leading" id="CbQ-P7-CcT"/>
<constraint firstAttribute="trailing" secondItem="YcN-xy-Wad" secondAttribute="trailing" id="Fvg-ou-jXz"/>
<constraint firstItem="YcN-xy-Wad" firstAttribute="top" secondItem="m2S-Jp-Qdl" secondAttribute="top" id="TwW-QB-xiV"/>
<constraint firstAttribute="bottom" secondItem="YcN-xy-Wad" secondAttribute="bottom" id="cSa-WP-uaE"/>
<constraint firstAttribute="bottom" secondItem="05R-gU-uKp" secondAttribute="bottom" id="3ff-8i-gMj"/>
<constraint firstAttribute="trailing" secondItem="05R-gU-uKp" secondAttribute="trailing" id="GGY-rK-xao"/>
<constraint firstItem="05R-gU-uKp" firstAttribute="top" secondItem="m2S-Jp-Qdl" secondAttribute="top" id="Yxg-Lv-nJ1"/>
<constraint firstItem="05R-gU-uKp" firstAttribute="leading" secondItem="m2S-Jp-Qdl" secondAttribute="leading" id="dVI-sb-Nhb"/>
</constraints>
</view>
<connections>
<outlet property="svgView" destination="YcN-xy-Wad" id="SzW-iC-Kic"/>
<outlet property="mainView" destination="05R-gU-uKp" id="IHx-rY-SpS"/>
</connections>
</viewController>
<customObject id="rPt-NT-nkU" userLabel="First Responder" customClass="NSResponder" sceneMemberID="firstResponder"/>

View File

@ -0,0 +1,119 @@
import Macaw
/*
class AnimationsView: MacawView {
var animation: Animation?
var ballNodes = [Group]()
var onComplete: (() -> ()) = {}
let n = 100
let speed = 20.0
let r = 10.0
let ballColors = [
Color(val: 0x1abc9c),
Color(val: 0x2ecc71),
Color(val: 0x3498db),
Color(val: 0x9b59b6),
Color(val: 0xf1c40f),
Color(val: 0xe67e22),
Color(val: 0xe67e22),
Color(val: 0xe74c3c)
]
required init?(coder aDecoder: NSCoder) {
super.init(node: Group(), coder: aDecoder)
}
func startAnimation() {
if let animation = self.animation {
animation.play()
}
}
func prepareAnimation() {
ballNodes.removeAll()
var animations = [Animation]()
let screenBounds = UIScreen.main.bounds
let startPos = Transform.move(
dx: Double(screenBounds.width / 2) - r,
dy: Double(screenBounds.height / 2) - r
)
var velocities = [Point]()
var positions = [Point]()
func posForTime(_ t: Double, index: Int) -> Point {
let prevPos = positions[index]
var velocity = velocities[index]
var pos = prevPos.add(velocity)
// Borders
if pos.x < Double(self.bounds.width) / -2.0 || pos.x > Double(self.bounds.width) / 2.0 {
velocity = Point(x: -1.0 * velocity.x, y: velocity.y)
velocities[index] = velocity
pos = prevPos.add(velocity)
}
if pos.y < Double(self.bounds.height) / -2.0 || pos.y > Double(self.bounds.height) / 2.0 {
velocity = Point(x: velocity.x, y: -1.0 * velocity.y)
velocities[index] = velocity
pos = prevPos.add(velocity)
}
return pos
}
for i in 0 ... (n - 1) {
// Node
let circle = Circle(cx: r, cy: r, r: r)
let shape = Shape(
form: circle,
fill: ballColors[Int(arc4random() % 7)]
)
let ballGroup = Group(contents: [shape])
ballNodes.append(ballGroup)
// Animation
let velocity = Point(
x: -0.5 * speed + speed * Double(arc4random() % 1000) / 1000.0,
y: -0.5 * speed + speed * Double(arc4random() % 1000) / 1000.0)
velocities.append(velocity)
positions.append(Point(x: 0.0, y: 0.0))
let anim = ballGroup.placeVar.animation({ (t) -> Transform in
let pos = posForTime(t, index: i)
positions[i] = pos
return Transform().move(
dx: pos.x,
dy: pos.y)
}, during: 3.0)
animations.append([
anim,
ballGroup.opacityVar.animation((0.1 >> 1.0).t(3.0))
].combine())
}
animation = animations.combine().autoreversed().onComplete {
self.completeAnimation()
}
let node = Group(contents: ballNodes)
node.place = Transform().move(dx: startPos.dx, dy: startPos.dy)
self.node = node
}
func completeAnimation() {
self.node = Group()
self.prepareAnimation()
self.onComplete()
}
}
*/

View File

@ -0,0 +1,44 @@
import Macaw
class EasingView: MacawView {
var anims = [Animation]()
var circlesNodes = [Group]()
var animation: Animation!
required init?(coder aDecoder: NSCoder) {
let screenSize: CGRect = NSScreen.main()!.frame
let centerX = Double(screenSize.width / 2)
let fromStroke = Stroke(fill: Color.black, width: 3)
let all = [Easing.ease, Easing.linear, Easing.easeIn, Easing.easeOut, Easing.easeInOut]
for (i, easing) in all.enumerated() {
let y = Double(150 + i * 120)
let title = EasingView.title(easing: easing)
let titleText = Text(text: title, align: .mid, place: .move(dx: centerX, dy: y - 70))
let fromCircle = Circle(cx: centerX - 100, cy: y, r: 25).stroke(with: fromStroke)
let toPlace = fromCircle.place.move(dx: 200, dy: 0)
let toAnimation = fromCircle.placeVar.animation(to: toPlace).easing(easing)
anims.append([toAnimation.autoreversed()].sequence())
circlesNodes.append(Group(contents: [fromCircle, titleText]))
}
animation = anims.combine().cycle()
super.init(node: circlesNodes.group(), coder: aDecoder)
}
fileprivate static func title(easing: Easing) -> String {
switch easing {
case .ease: return "Ease"
case .linear: return "Linear"
case .easeIn: return "Ease In"
case .easeOut: return "Ease Out"
case .easeInOut: return "Ease InOut"
}
}
}

View File

@ -0,0 +1,150 @@
//
// EventsExampleController.swift
// Example
//
// Created by Victor Sukochev on 02/03/2017.
// Copyright © 2017 Exyte. All rights reserved.
//
import Macaw
/*
class EventsExampleController: UIViewController {
@IBOutlet weak var macawView: MacawView?
override func viewDidLoad() {
super.viewDidLoad()
macawView?.node = loadScene()
}
enum PanelTool {
case ellipse
case rectangle
}
var selectedTool: PanelTool?
private func loadScene() -> Node {
return [
createCanvas(),
createPanel(),
].group()
}
private func createPanel() -> Node {
let panel = Shape(form: Rect(x: 10.0, y: 10.0, w: 80.0, h: 120.0), fill: Color.white, stroke: Stroke(fill: Color.black, width: 1.0))
let panelGroup = [
panel,
createTools()
].group()
panelGroup.onPan { event in
panelGroup.place = panelGroup.place.move(dx: event.dx, dy: event.dy)
}
return panelGroup
}
private func createCanvas() -> Node {
let canvas = Shape(form: Rect(x: 0.0, y: 0.0,
w: Double(macawView!.bounds.width),
h: Double(macawView!.bounds.height)),
fill: Color.white)
let objectsGroup = Group(contents:[])
var startPoint = Point()
var currentFigure: Shape?
canvas.onTouchPressed { event in
guard let tool = self.selectedTool else {
return
}
guard let loc = event.points.first?.location else {
return
}
startPoint = loc
switch tool {
case .ellipse:
currentFigure = Shape(form: Ellipse(cx: startPoint.x, cy: startPoint.y, rx: 0.0, ry: 0.0))
break
case .rectangle:
currentFigure = Shape(form: Rect(x: startPoint.x, y: startPoint.y, w: 0.0, h: 0.0))
break
}
var updatedContents = objectsGroup.contents
updatedContents.append(currentFigure!)
objectsGroup.contents = updatedContents
}
canvas.onTouchMoved { event in
guard let tool = self.selectedTool else {
return
}
guard let loc = event.points.first?.location else {
return
}
let width = loc.x - startPoint.x
let height = loc.y - startPoint.y
switch tool {
case .ellipse:
currentFigure?.form = Ellipse(
cx: startPoint.x + width / 2.0,
cy: startPoint.y + height / 2.0,
rx: width / 2.0,
ry: height / 2.0)
break
case .rectangle:
currentFigure?.form = Rect(x: startPoint.x, y: startPoint.y, w: width, h: height)
break
}
}
return [
canvas,
objectsGroup
].group()
}
private func createTools() -> Node {
let ellipseTool = Shape(form: Ellipse(cx: 50.0, cy: 50.0, rx: 25, ry: 15),
fill: Color.white,
stroke: Stroke(fill: Color.black, width: 1.0))
let rectTool = Shape(form: Rect(x: 25.0, y: 75.0, w: 50.0, h: 30.0),
fill: Color.white,
stroke: Stroke(fill: Color.black, width: 1.0))
ellipseTool.onTap { _ in
self.selectedTool = .ellipse
ellipseTool.stroke = Stroke(fill: Color.blue, width: 2.0)
rectTool.stroke = Stroke(fill: Color.black, width: 1.0)
}
rectTool.onTap { _ in
self.selectedTool = .rectangle
rectTool.stroke = Stroke(fill: Color.blue, width: 2.0)
ellipseTool.stroke = Stroke(fill: Color.black, width: 1.0)
}
return [
ellipseTool,
rectTool
].group()
}
}
*/

View File

@ -0,0 +1,40 @@
//
// MorphingView.swift
// Example
//
// Created by Victor Sukochev on 25/01/2017.
// Copyright © 2017 Exyte. All rights reserved.
//
import Macaw
class MorphingView: MacawView {
required init?(coder aDecoder: NSCoder) {
super.init(node: MorphingView.newScene(), coder: aDecoder)
}
class func newScene() -> Node {
let stroke = Stroke(width: 15.0, cap: .round)
let contents1 = [
Shape(form: Line(x1: 150.0, y1: 150.0, x2: 175.0, y2: 125.0), stroke: stroke),
Shape(form: Line(x1: 150.0, y1: 150.0, x2: 225.0, y2: 150.0), stroke: stroke),
Shape(form: Line( x1: 150.0, y1: 150.0, x2: 175.0, y2: 175.0), stroke: stroke),
]
let contents2 = [
Shape(form: Line(x1: 130.0, y1: 110.0, x2: 245.0, y2: 110.0), stroke: stroke),
Shape(form: Line(x1: 130.0, y1: 150.0, x2: 245.0, y2: 150.0), stroke: stroke),
Shape(form: Line(x1: 130.0, y1: 190.0, x2: 245.0, y2: 190.0), stroke: stroke),
]
var switched = false
let group = contents1.group()
group.onTap { e in
group.contentsVar.animate(to: switched ? contents1 : contents2)
switched = !switched
}
return group
}
}

View File

@ -0,0 +1,10 @@
import UIKit
import Macaw
class SVGExampleView: MacawView {
required init?(coder aDecoder: NSCoder) {
super.init(node: SVGParser.parse(path: "tiger"), coder: aDecoder)
}
}

View File

@ -0,0 +1,79 @@
//
// CustomView.swift
// Example
//
// Created by Yuri Strot on 12/19/15.
// Copyright © 2015 Exyte. All rights reserved.
//
import Macaw
class ShapesExampleView: MacawView {
required init?(coder aDecoder: NSCoder) {
let text1 = ShapesExampleView.newText("Point", .move(dx: 100, dy: 40))
let point = Point(x: 100, y: 50).stroke(fill: Color.black, width: 7, cap: .round)
let text2 = ShapesExampleView.newText("Line", .move(dx: 250, dy: 40))
let line = Line(x1: 200, y1: 50, x2: 300, y2: 50).stroke(fill: Color.black, width: 4)
let text3 = ShapesExampleView.newText("Polyline", .move(dx: 100, dy: 90))
let polyline = Polyline(points: [100, 100, 150, 150, 50, 150]).stroke(fill: Color.navy, dashes: [3, 3])
let text4 = ShapesExampleView.newText("Polygon", .move(dx: 250, dy: 90))
let polygon = Polygon(points: [250, 100, 300, 150, 200, 150]).fill(with: Color.blue)
let text5 = ShapesExampleView.newText("Rect", .move(dx: 100, dy: 200))
let rect = Rect(x: 50, y: 200, w: 100, h: 50).fill(with: LinearGradient(
x2: 1, y2: 1,
stops: [
Stop(offset: 0, color: Color(val: 0xFFAE27)),
Stop(offset: 1, color: Color(val: 0xDE496D))
]
))
let text6 = ShapesExampleView.newText("RoundRect", .move(dx: 250, dy: 200))
let round = Rect(x: 200, y: 200, w: 100, h: 50).round(rx: 10, ry: 10).fill(with: LinearGradient(
x2: 1, y2: 1,
stops: [
Stop(offset: 0, color: Color(val: 0xDE496D)),
Stop(offset: 0.5, color: Color(val: 0xAB49DE)),
Stop(offset: 1, color: Color(val: 0x4954DE))
]
))
let text7 = ShapesExampleView.newText("Circle", .move(dx: 75, dy: 300))
let circle = Circle(cx: 75, cy: 325, r: 25).fill(with: RadialGradient(
stops: [
Stop(offset: 0, color: Color(val: 0xF5027C)),
Stop(offset: 1, color: Color(val: 0x850143))
]
))
let text8 = ShapesExampleView.newText("Ellipse", .move(dx: 175, dy: 300))
let ellipse = Ellipse(cx: 175, cy: 325, rx: 50, ry: 25).fill(with: RadialGradient(
fx: 0.05, fy: 0.05, r: 0.65,
stops: [
Stop(offset: 0, color: Color(val: 0x00ee00)),
Stop(offset: 1, color: Color(val: 0x006600))
]
))
let text9 = ShapesExampleView.newText("Arc", .move(dx: 275, dy: 300))
let arc = Circle(cx: 250, cy: 300, r: 50).arc(shift: 0, extent: Double.pi / 2.0).stroke(fill: Color.green)
let group = Group(
contents: [
point, line, polyline, polygon, rect, round, circle, ellipse, arc,
text1, text2, text3, text4, text5, text6, text7, text8, text9
]
)
super.init(node: group, coder: aDecoder)
}
fileprivate static func newText(_ text: String, _ place: Transform, baseline: Baseline = .bottom) -> Text {
return Text(text: text, fill: Color.black, align: .mid, baseline: baseline, place: place)
}
}

View File

@ -0,0 +1,69 @@
//
// TransformExampleView.swift
// Example
//
// Created by Yuri Strot on 9/10/16.
// Copyright © 2016 Exyte. All rights reserved.
//
import Foundation
import Macaw
class TransformExampleView: MacawView {
fileprivate static let transforms = [Transform.scale(sx: 2, sy: 2),
Transform.move(dx: 100, dy: 30),
Transform.rotate(angle: Double.pi / 4.0, x: 150, y: 80),
Transform.rotate(angle: Double.pi / 4.0)]
fileprivate static let titles = ["Transform.scale(sx: 2, sy: 2)",
"Transform.move(dx: 100, dy: 30)",
"Transform.rotate(angle: M_PI_4, x: 150, y: 80)",
"Transform.rotate(angle: M_PI_4)"]
required init?(coder aDecoder: NSCoder) {
super.init(node: TransformExampleView.newScene(), coder: aDecoder)
}
fileprivate static func newScene() -> Node {
let shape = Shape(form: Rect(x: 0, y: 0, w: 50, h: 50), fill: Color.blue)
let textes = Group(place: .move(dx: 50, dy: 275))
for (i, item) in titles.enumerated() {
let place = Transform.move(dx: 0, dy: Double(i * 25))
textes.contents.append(Text(text: item, baseline: .bottom, place: place, opacity: 0))
}
var combines: [Transform] = [Transform.identity]
for transform in transforms {
combines.append(GeomUtils.concat(t1: transform, t2: combines.last!))
}
var state = 0
shape.onTouchPressed { _ in
if (state < textes.contents.count) {
textes.contents[state].opacityVar.animate(from: 0.0, to: 1.0, during: 0.6)
} else {
for item in textes.contents {
item.opacityVar.animate(from: 1.0, to: 0.0, during: 0.6)
}
}
state = (state + 1) % 5;
shape.placeVar.animate(to: combines[state], during: 0.6)
}
return Group(contents: [newAxes(), shape, textes], place: .move(dx: 10, dy: 10))
}
fileprivate static func newAxes() -> Node {
var items: [Node] = []
let gray = Color(val: 0xF0F0F0)
for i in 1...20 {
let shift = Double(i) * 50.0
items.append(Line(x1: -50, y1: shift, x2: 1000, y2: shift).stroke(fill: gray))
items.append(Line(x1: shift, y1: -50, x2: shift, y2: 1000).stroke(fill: gray))
}
items.append(Line(x1: -10, y1: 0, x2: 1000, y2: 0).stroke())
items.append(Line(x1: 0, y1: -10, x2: 0, y2: 1000).stroke())
return Group(contents: items)
}
}

View File

@ -11,27 +11,12 @@ import Macaw
class ViewController: NSViewController {
@IBOutlet weak var svgView: SVGView!
@IBOutlet weak var mainView: EasingView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
override func viewDidAppear() {
super.viewDidAppear()
svgView.updateLayer()
svgView.contentMode = .scaleAspectFit
svgView.fileName = "tiger"
//mainView.animation.play()
}
}

View File

@ -112,6 +112,19 @@ import Foundation
}
open class MView: NSView {
public override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
self.wantsLayer = true
}
public required init?(coder: NSCoder) {
super.init(coder: coder)
self.wantsLayer = true
}
open override var isFlipped: Bool {
return true
}
@ -126,7 +139,6 @@ import Foundation
}
set {
self.wantsLayer = true
self.layer?.backgroundColor = newValue == nil ? nil : newValue!.cgColor
}
}
@ -416,79 +428,62 @@ import Foundation
public convenience init(roundedRect rect: NSRect, byRoundingCorners corners: MRectCorner, cornerRadii: NSSize) {
self.init()
let kappa: CGFloat = 0.552228474
let opKappa = 1 - kappa
let topLeft = rect.origin
let topRight = NSPoint(x: rect.maxX, y: rect.minY);
let bottomRight = NSPoint(x: rect.maxX, y: rect.maxY);
let bottomLeft = NSPoint(x: rect.minX, y: rect.maxY);
if corners.contains(.topLeft) {
move(to: CGPoint(
x: topLeft.x + cornerRadii.width,
y: topLeft.y))
move(to: CGPoint(x: topLeft.x + cornerRadii.width, y: topLeft.y))
} else {
move(to: topLeft)
}
if corners.contains(.topRight) {
line(to: CGPoint(
x: topRight.x - cornerRadii.width,
y: topRight.y))
curve(
to: topRight,
controlPoint1: CGPoint(
x: topRight.x,
y: topRight.y + cornerRadii.height),
controlPoint2: CGPoint(
x: topRight.x,
y: topRight.y + cornerRadii.height))
line(to: CGPoint(x: topRight.x - cornerRadii.width, y: topRight.y))
curve(to: CGPoint(x: topRight.x, y: topRight.y + cornerRadii.height),
controlPoint1: CGPoint(x: topRight.x - cornerRadii.width * opKappa, y: topRight.y),
controlPoint2: CGPoint(x: topRight.x, y: topRight.y + cornerRadii.height * opKappa))
} else {
line(to: topRight)
}
if corners.contains(.bottomRight) {
line(to: CGPoint(
x: bottomRight.x,
y: bottomRight.y - cornerRadii.height))
curve(
to: bottomRight,
controlPoint1: CGPoint(
x: bottomRight.x - cornerRadii.width,
y: bottomRight.y),
controlPoint2: CGPoint(
x: bottomRight.x - cornerRadii.width,
y: bottomRight.y))
line(to: CGPoint(x: bottomRight.x, y: bottomRight.y - cornerRadii.height))
curve(to: CGPoint(x: bottomRight.x - cornerRadii.width, y: bottomRight.y),
controlPoint1: CGPoint(x: bottomRight.x, y: bottomRight.y - cornerRadii.height * opKappa),
controlPoint2: CGPoint(x: bottomRight.x - cornerRadii.width * opKappa, y: bottomRight.y))
} else {
line(to: bottomRight)
}
if corners.contains(.bottomLeft) {
line(to: CGPoint(
x: bottomLeft.x + cornerRadii.width,
y: bottomLeft.y))
curve(
to: bottomLeft,
controlPoint1: CGPoint(
x: bottomLeft.x,
y: bottomLeft.y - cornerRadii.height),
controlPoint2: CGPoint(
x: bottomLeft.x,
y: bottomLeft.y - cornerRadii.height))
line(to: CGPoint(x: bottomLeft.x + cornerRadii.width, y: bottomLeft.y))
curve(to: CGPoint(x: bottomLeft.x, y: bottomLeft.y - cornerRadii.height),
controlPoint1: CGPoint(x: bottomLeft.x + cornerRadii.width * opKappa, y: bottomLeft.y),
controlPoint2: CGPoint(x: bottomLeft.x, y: bottomLeft.y - cornerRadii.height * opKappa))
} else {
line(to: bottomLeft)
}
if corners.contains(.topLeft) {
line(to: CGPoint(
x: topLeft.x,
y: topLeft.y + cornerRadii.height))
curve(
to: topLeft,
controlPoint1: CGPoint(
x: topLeft.x + cornerRadii.width,
y: topLeft.y),
controlPoint2: CGPoint(
x: topLeft.x + cornerRadii.width,
y: topLeft.y))
line(to: CGPoint(x: topLeft.x, y: topLeft.y + cornerRadii.height))
curve(to: CGPoint(x: topLeft.x + cornerRadii.width, y: topLeft.y),
controlPoint1: CGPoint(x: topLeft.x, y: topLeft.y + cornerRadii.height * opKappa),
controlPoint2: CGPoint(x: topLeft.x + cornerRadii.width * opKappa, y: topLeft.y))
} else {
line(to: topLeft)
}

View File

@ -68,6 +68,21 @@ open class MacawView: MView, MGestureRecognizerDelegate {
internal var animationCache: AnimationCache?
#if os(OSX)
open override var layer: CALayer? {
didSet {
guard self.layer != nil else {
return
}
initializeView()
if let cache = self.animationCache {
self.renderer = RenderUtils.createNodeRenderer(node, context: context, animationCache: cache)
}
}
}
#endif
public init?(node: Node, coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
@ -100,20 +115,6 @@ open class MacawView: MView, MGestureRecognizerDelegate {
self.init(node: Group(), coder: aDecoder)
}
#if os(OSX)
open override func updateLayer() {
super.updateLayer()
initializeView()
}
open override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
initializeView()
}
#endif
fileprivate func initializeView() {
self.context = RenderContext(view: self)
@ -252,7 +253,7 @@ open class MacawView: MView, MGestureRecognizerDelegate {
for touch in touches {
let location = touch.location(in: self)
NSLog("\(location)")
var foundNode: Node? = .none
localContext { ctx in
foundNode = renderer.findNodeAt(location: location, ctx: ctx)
@ -281,7 +282,6 @@ open class MacawView: MView, MGestureRecognizerDelegate {
touchesOfNode[currentNode]?.append(touch)
parent!.handleTouchPressed(touchEvent)
parent = nodesMap.parents(parent!).first
}
}
@ -359,9 +359,11 @@ open class MacawView: MView, MGestureRecognizerDelegate {
// MARK: - Tap
func handleTap(recognizer: MTapGestureRecognizer) {
#if os(iOS)
if !self.node.shouldCheckForTap() {
return
}
#endif
guard let renderer = renderer else {
return