From b6af3ced00202d9ec31ad62981792b048615a010 Mon Sep 17 00:00:00 2001 From: Kevin G Christiano Date: Thu, 27 Feb 2020 10:12:43 -0500 Subject: [PATCH] latest state --- MVMCoreUI/Atoms/Views/Arrow.swift | 66 +++++++++++++++++++ MVMCoreUI/Atoms/Views/ArrowModel.swift | 10 +-- .../OtherHandlers/MoleculeObjectMapping.swift | 1 + 3 files changed, 73 insertions(+), 4 deletions(-) diff --git a/MVMCoreUI/Atoms/Views/Arrow.swift b/MVMCoreUI/Atoms/Views/Arrow.swift index 96f2bb9a..59e6f6f9 100644 --- a/MVMCoreUI/Atoms/Views/Arrow.swift +++ b/MVMCoreUI/Atoms/Views/Arrow.swift @@ -13,12 +13,72 @@ open class Arrow: View { // MARK: - Properties //-------------------------------------------------- + var arrowLayer = CAShapeLayer() + var lineWidth: CGFloat = 1 + var color: UIColor = .black + //-------------------------------------------------- // MARK: - Lifecycle //-------------------------------------------------- open override func setupView() { super.setupView() + + heightAnchor.constraint(equalToConstant: 12).isActive = true + widthAnchor.constraint(equalToConstant: 12).isActive = true + + drawShapeLayer() + isOpaque = false + layer.addSublayer(arrowLayer) + arrowLayer.strokeEnd = 1 + } + + override open func layoutSubviews() { + super.layoutSubviews() + + drawShapeLayer() +// layer.cornerRadius = isRound ? cornerRadiusValue : 0 + } + + //-------------------------------------------------- + // MARK: - Drawing + //-------------------------------------------------- + + private func drawShapeLayer() { + + arrowLayer.frame = bounds + arrowLayer.strokeColor = color.cgColor + arrowLayer.fillColor = UIColor.clear.cgColor + arrowLayer.path = arrowPath() + arrowLayer.lineJoin = .miter + arrowLayer.lineCap = .butt + arrowLayer.lineWidth = lineWidth + } + + private func arrowPath() -> CGPath { + + let length = max(bounds.size.height, bounds.size.width) - 1 +// let xInsetLeft = length * 0.25 +// let yInsetTop = length * 0.3 +// let innerWidth = length - (xInsetLeft + length * 0.25) // + Right X Inset +// let innerHeight = length - (yInsetTop + length * 0.35) // + Bottom Y Inset + + var startPoint = CGPoint(x: length * 0.5, y: 1) + let pivotPoint = CGPoint(x: length, y: length * 0.5) + var endPoint = CGPoint(x: length * 0.5, y: length) + + let bezierPath = UIBezierPath() + bezierPath.move(to: startPoint) + bezierPath.addLine(to: pivotPoint) + bezierPath.addLine(to: endPoint) + + startPoint = CGPoint(x: 1, y: length * 0.5) + endPoint = CGPoint(x: length, y: length * 0.5) + + bezierPath.move(to: startPoint) + bezierPath.addLine(to: pivotPoint) + + return bezierPath.cgPath } //-------------------------------------------------- @@ -28,5 +88,11 @@ open class Arrow: View { public override func setWithModel(_ model: MoleculeModelProtocol?, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) { guard let model = model as? ArrowModel else { return } + arrowLayer.transform = CATransform3DIdentity + + if let degrees = model.direction { + let radians = CGFloat(degrees * Double.pi / 180) + arrowLayer.transform = CATransform3DMakeRotation(-radians, 0.0, 0.0, 1.0) + } } } diff --git a/MVMCoreUI/Atoms/Views/ArrowModel.swift b/MVMCoreUI/Atoms/Views/ArrowModel.swift index 00e85cfb..62bf5c05 100644 --- a/MVMCoreUI/Atoms/Views/ArrowModel.swift +++ b/MVMCoreUI/Atoms/Views/ArrowModel.swift @@ -16,8 +16,8 @@ open class ArrowModel: MoleculeModelProtocol { public static var identifier: String = "arrow" public var backgroundColor: Color? - public var color: Color = Color(uiColor: .mfLighterGray()) - public var direction: Bool? + public var color: Color = Color(uiColor: .mvmBlack) + public var direction: Double? //-------------------------------------------------- // MARK: - Keys @@ -28,6 +28,7 @@ open class ArrowModel: MoleculeModelProtocol { case backgroundColor case color case direction + case size } //-------------------------------------------------- @@ -36,12 +37,13 @@ open class ArrowModel: MoleculeModelProtocol { required public init(from decoder: Decoder) throws { let typeContainer = try decoder.container(keyedBy: CodingKeys.self) + if let color = try typeContainer.decodeIfPresent(Color.self, forKey: .color) { self.color = color } - backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor) - direction = try typeContainer.decodeIfPresent(Bool.self, forKey: .direction) + backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor) + direction = try typeContainer.decodeIfPresent(Double.self, forKey: .direction) } public func encode(to encoder: Encoder) throws { diff --git a/MVMCoreUI/OtherHandlers/MoleculeObjectMapping.swift b/MVMCoreUI/OtherHandlers/MoleculeObjectMapping.swift index ac7375d7..8cabfa3f 100644 --- a/MVMCoreUI/OtherHandlers/MoleculeObjectMapping.swift +++ b/MVMCoreUI/OtherHandlers/MoleculeObjectMapping.swift @@ -50,6 +50,7 @@ import Foundation MVMCoreUIMoleculeMappingObject.shared()?.register(viewClass: Toggle.self, viewModelClass: ToggleModel.self) MVMCoreUIMoleculeMappingObject.shared()?.register(viewClass: Checkbox.self, viewModelClass: CheckboxModel.self) MVMCoreUIMoleculeMappingObject.shared()?.register(viewClass: CheckboxLabel.self, viewModelClass: CheckboxLabelModel.self) + MVMCoreUIMoleculeMappingObject.shared()?.register(viewClass: Arrow.self, viewModelClass: ArrowModel.self) // Horizontal Combination Molecules MVMCoreUIMoleculeMappingObject.shared()?.register(viewClass: StringAndMoleculeView.self, viewModelClass: StringAndMoleculeModel.self)