-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathNodeValue.swift
106 lines (85 loc) · 2.62 KB
/
NodeValue.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
//
// NodeValue.swift
// VPL
//
// Created by Nathan Flurry on 3/28/18.
// Copyright © 2018 Nathan Flurry. All rights reserved.
//
public final class InputValue {
/// The node that owns this value.
public internal(set) weak var owner: Node!
/// An identifier for this value.
public let id: String
/// Name for this value.
public let name: String
/// The type of value this holds.
public let type: ValueType
/// The connected value.
public private(set) var target: OutputValue?
public init(id: String, name: String, type: ValueType) {
self.id = id
self.name = name
self.type = type
}
/// Determines if two values can be connected.
public func canConnect(to newTarget: OutputValue) -> Bool {
return newTarget.canConnect(to: self)
}
/// Connects this value to another value.
public func connect(to newTarget: OutputValue) {
// Set the new target
target = newTarget
// Connect the other node
if newTarget.target !== self {
newTarget.connect(to: self)
}
}
/// Disconnects any targets this is connected to.
public func reset() {
// Remove the target
let tmpTarget = target
target = nil
// Remove other target if needed
if tmpTarget?.target != nil {
tmpTarget?.reset()
}
}
/// Assembles the code.
public func assemble() -> String {
return target?.owner.assemble() ?? ""
}
}
public final class OutputValue {
/// The node that owns this value
public internal(set) weak var owner: Node!
/// The type of value this holds.
public let type: ValueType
/// The connected value.
public private(set) var target: InputValue?
public init(type: ValueType) {
self.type = type
}
/// Determines if two values can be connected.
public func canConnect(to newTarget: InputValue) -> Bool {
return type.canCast(to: newTarget.type) && owner !== newTarget.owner && target == nil && newTarget.target == nil
}
/// Connects this value to another value.
public func connect(to newTarget: InputValue) {
// Set the new target
target = newTarget
// Connect the other node
if newTarget.target !== self {
newTarget.connect(to: self)
}
}
/// Disconnects any targets this is connected to.
public func reset() {
// Remove the target
let tmpTarget = target
target = nil
// Remove other target if needed
if tmpTarget?.target != nil {
tmpTarget?.reset()
}
}
}