/
SNRelationshipCurvesLayer.swift
179 lines (139 loc) · 7.15 KB
/
SNRelationshipCurvesLayer.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//
// SNRelationshipCurvesLayer.swift
// ShinpuruNodeUI
//
// Created by Simon Gladman on 02/09/2015.
// Copyright © 2015 Simon Gladman. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>
import UIKit
class SNRelationshipCurvesLayer: CALayer
{
var relationshipLayersDictionary = [SNNodePair: CAShapeLayer]()
func deleteSpecificRelationship(sourceNode sourceNode: SNNode, targetNode: SNNode, targetNodeInputIndex: Int)
{
let nodePair = SNNodePair(sourceNode: targetNode, targetNode: sourceNode, targetIndex: targetNodeInputIndex)
if let relationshipLayer = relationshipLayersDictionary[nodePair]
{
relationshipLayer.removeFromSuperlayer()
relationshipLayersDictionary.removeValueForKey(nodePair)
}
}
func deleteNodeRelationships(deletedNode: SNNode)
{
for (key, value) in relationshipLayersDictionary where key.sourceNode == deletedNode || key.targetNode == deletedNode
{
value.removeFromSuperlayer()
relationshipLayersDictionary.removeValueForKey(key)
}
}
func renderRelationships(nodes: [SNNode], widgetsDictionary: [SNNode: SNNodeWidget], focussedNode: SNNode? = nil)
{
drawsAsynchronously = true
CATransaction.begin()
CATransaction.disableActions()
CATransaction.setAnimationDuration(0)
let sourceNodeTargets: [SNNode]
if focussedNode != nil
{
sourceNodeTargets = nodes.filter
{
$0.inputs != nil && $0.inputs!.contains({ $0 == focussedNode })
}
}
else
{
sourceNodeTargets = [SNNode]()
}
for sourceNode in nodes where focussedNode == nil || sourceNode == focussedNode || sourceNodeTargets.contains(sourceNode)
{
guard let sourceWidget = widgetsDictionary[sourceNode] else
{
continue
}
if let inputs = sourceNode.inputs
{
var inputRowsHeight: CGFloat = 0
// draw relationships...
for (idx, targetNode) in inputs.enumerate()
{
guard let targetNode = targetNode,
targetWidget = widgetsDictionary[targetNode],
targetOutputRow = targetWidget.outputRenderer,
sourceItemRendererHeight = sourceWidget.itemRenderer?.intrinsicContentSize().height
else
{
if idx < sourceWidget.inputRowRenderers.count
{
inputRowsHeight += sourceWidget.inputRowRenderers[idx].intrinsicContentSize().height
}
continue
}
if idx < sourceWidget.inputRowRenderers.count
{
let targetWidgetHeight = targetWidget.intrinsicContentSize().height - targetOutputRow.intrinsicContentSize().height
let targetWidgetWidth = targetWidget.intrinsicContentSize().width
let rowHeight = sourceWidget.inputRowRenderers[idx].intrinsicContentSize().height
let inputPosition = CGPoint(x: targetNode.position.x + targetWidgetWidth,
y: targetNode.position.y + CGFloat(targetWidgetHeight) + (targetOutputRow.intrinsicContentSize().height / 2))
let targetY = sourceNode.position.y + inputRowsHeight + CGFloat(rowHeight / 2) + sourceItemRendererHeight + SNNodeWidget.titleBarHeight
let targetPosition = CGPoint(x: sourceNode.position.x, y: targetY)
let controlPointHorizontalOffset = max(abs(targetPosition.x - inputPosition.x), 40) * 0.75
let controlPointOne = CGPoint(x: targetPosition.x - controlPointHorizontalOffset, y: targetY)
let controlPointTwo = CGPoint(x: inputPosition.x + controlPointHorizontalOffset, y: inputPosition.y)
let relationshipCurvesPath = UIBezierPath()
drawTerminal(relationshipCurvesPath, position: inputPosition.offset(4, dy: 0))
drawTerminal(relationshipCurvesPath, position: targetPosition.offset(-4, dy: 0))
relationshipCurvesPath.moveToPoint(targetPosition.offset(-4, dy: 0))
relationshipCurvesPath.addCurveToPoint(inputPosition.offset(4, dy: 0), controlPoint1: controlPointOne, controlPoint2: controlPointTwo)
inputRowsHeight += rowHeight
let nodePair = SNNodePair(sourceNode: sourceNode, targetNode: targetNode, targetIndex: idx)
let layer = layerForNodePair(nodePair)
layer.path = relationshipCurvesPath.CGPath
}
}
}
}
CATransaction.commit()
}
func layerForNodePair(nodePair: SNNodePair) -> CAShapeLayer
{
if relationshipLayersDictionary[nodePair] == nil
{
let layer = CAShapeLayer()
layer.strokeColor = UIColor.whiteColor().CGColor
layer.lineWidth = 4
layer.fillColor = nil
layer.lineCap = kCALineCapSquare
layer.shadowColor = UIColor.blackColor().CGColor
layer.shadowOffset = CGSizeZero
layer.shadowRadius = 2
layer.shadowOpacity = 1
relationshipLayersDictionary[nodePair] = layer
addSublayer(layer)
}
return relationshipLayersDictionary[nodePair]!
}
func drawTerminal(relationshipCurvesPath: UIBezierPath, position: CGPoint)
{
let rect = UIBezierPath(roundedRect: CGRect(origin: CGPoint(x: position.x - 2, y: position.y - 2), size: CGSize(width: 4, height: 4)), cornerRadius: 0)
relationshipCurvesPath.appendPath(rect)
}
}
extension CGPoint
{
func offset(dx: CGFloat, dy: CGFloat) -> CGPoint
{
return CGPoint(x: x + dx, y: y + dy)
}
}