diff --git a/BChess.xcodeproj/project.pbxproj b/BChess.xcodeproj/project.pbxproj index b9ecc25..46cd5d4 100644 --- a/BChess.xcodeproj/project.pbxproj +++ b/BChess.xcodeproj/project.pbxproj @@ -23,6 +23,8 @@ A70B41A4264A527900A56E29 /* FullMoveView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A70B41A2264A527900A56E29 /* FullMoveView.swift */; }; A712C25326522F6000E05408 /* FullMove.swift in Sources */ = {isa = PBXBuildFile; fileRef = A712C25226522F6000E05408 /* FullMove.swift */; }; A712C25426522F6000E05408 /* FullMove.swift in Sources */ = {isa = PBXBuildFile; fileRef = A712C25226522F6000E05408 /* FullMove.swift */; }; + A712C256265377C000E05408 /* Arrow.swift in Sources */ = {isa = PBXBuildFile; fileRef = A712C255265377C000E05408 /* Arrow.swift */; }; + A712C257265377C000E05408 /* Arrow.swift in Sources */ = {isa = PBXBuildFile; fileRef = A712C255265377C000E05408 /* Arrow.swift */; }; A716976D262BFF8C00156BD6 /* SettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A716976C262BFF8C00156BD6 /* SettingsView.swift */; }; A72C3E022650D78100CB9DB7 /* VariationSelectionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A72C3E012650D78100CB9DB7 /* VariationSelectionView.swift */; }; A72C3E032650D78100CB9DB7 /* VariationSelectionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A72C3E012650D78100CB9DB7 /* VariationSelectionView.swift */; }; @@ -220,6 +222,7 @@ A70A61D01FD4AA9600AFDF0E /* FEngineInfo.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = FEngineInfo.mm; sourceTree = ""; }; A70B41A2264A527900A56E29 /* FullMoveView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FullMoveView.swift; sourceTree = ""; }; A712C25226522F6000E05408 /* FullMove.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FullMove.swift; sourceTree = ""; }; + A712C255265377C000E05408 /* Arrow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Arrow.swift; sourceTree = ""; }; A716976C262BFF8C00156BD6 /* SettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsView.swift; sourceTree = ""; }; A72C3E012650D78100CB9DB7 /* VariationSelectionView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VariationSelectionView.swift; sourceTree = ""; }; A72C3E042650D82B00CB9DB7 /* Variation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Variation.swift; sourceTree = ""; }; @@ -588,6 +591,7 @@ A77F66AE25A832240030E61D /* ContentView.swift */, A7A30F2E25B2BAFC00729432 /* PromotionView.swift */, A746997B2637CCF7007E0058 /* NavigationView.swift */, + A712C255265377C000E05408 /* Arrow.swift */, ); path = Views; sourceTree = ""; @@ -949,6 +953,7 @@ A7FE322C25AA96D100A75936 /* magicmoves.c in Sources */, A77F66D925A832270030E61D /* ContentView.swift in Sources */, A757614F264A50E8006242F9 /* FEngineMoveNode.mm in Sources */, + A712C256265377C000E05408 /* Arrow.swift in Sources */, A79515D525ABE3A400AEA95F /* SelectionModifier.swift in Sources */, A775237625AD68FA009BFFD1 /* Animation.swift in Sources */, A795161725ABE57C00AEA95F /* ConditionalView.swift in Sources */, @@ -1006,6 +1011,7 @@ A7FE31FB25AA96C500A75936 /* ChessGame.cpp in Sources */, A7FE31FC25AA96C500A75936 /* ChessOpenings.cpp in Sources */, A7576150264A50E8006242F9 /* FEngineMoveNode.mm in Sources */, + A712C257265377C000E05408 /* Arrow.swift in Sources */, A79515D625ABE3A400AEA95F /* SelectionModifier.swift in Sources */, A775237725AD68FA009BFFD1 /* Animation.swift in Sources */, A795161825ABE57C00AEA95F /* ConditionalView.swift in Sources */, diff --git a/Shared/Actions.swift b/Shared/Actions.swift index 907226c..1868f84 100644 --- a/Shared/Actions.swift +++ b/Shared/Actions.swift @@ -80,7 +80,6 @@ struct Actions { // Hide the variations document.variations.show = false - document.variations.selectedVariationIndex = 0 } else { // Get the next move UUID let nextMoveUUID = UInt(engine.moveUUID(to)) diff --git a/Shared/ChessDocument.swift b/Shared/ChessDocument.swift index f33c109..18720c8 100644 --- a/Shared/ChessDocument.swift +++ b/Shared/ChessDocument.swift @@ -53,7 +53,11 @@ struct GameMode { // move forward in a game and a choice must be made because more than // one move is available as the next move. struct Variations { - var show = false + var show = false { + didSet { + selectedVariationIndex = 0 + } + } var selectedVariationIndex = 0 var variations = [FEngineMoveNode]() } diff --git a/Shared/Views/Arrow.swift b/Shared/Views/Arrow.swift new file mode 100644 index 0000000..55f4887 --- /dev/null +++ b/Shared/Views/Arrow.swift @@ -0,0 +1,54 @@ +// +// Arrow.swift +// BChess +// +// Created by Jean Bovet on 5/17/21. +// Copyright © 2021 Jean Bovet. All rights reserved. +// + +import SwiftUI + +struct Arrow { + + var path = Path() + + init(start a: CGPoint, end b: CGPoint, length: CGFloat) { + let arrowLength = sqrt(pow(b.x-a.x, 2)+pow(b.y-a.y, 2)) + + let x = -(b.x - a.x)/arrowLength * length + let y = -(b.y - a.y)/arrowLength * length + + let alpha = CGFloat(deg2rad(25)) + + let c1x = b.x + (x*cos(alpha)-y*sin(alpha)) + let c1y = b.y + (x*sin(alpha)+y*cos(alpha)) + + let c2x = b.x + (x*cos(-alpha)-y*sin(-alpha)) + let c2y = b.y + (x*sin(-alpha)+y*cos(-alpha)) + + path.move(to: b) + path.addLine(to: CGPoint(x: c1x, y: c1y)) + path.addLine(to: CGPoint(x: c2x, y: c2y)) + path.addLine(to: b) + path.closeSubpath() + path.move(to: a) + path.addLine(to: b) + } + + private func deg2rad(_ number: Double) -> Double { + return number * .pi / 180 + } + +} + +struct Arrow_Previews: PreviewProvider { + static var previews: some View { + GeometryReader { geometry in + let p = Arrow(start: CGPoint(x: 10, y: 10), + end: CGPoint(x: geometry.size.width - 20, y: geometry.size.height - 20), + length: 20).path + p.fill(Color.blue) + p.stroke(Color.blue, lineWidth: 10) + } + } +} diff --git a/Shared/Views/VariationSelectionView.swift b/Shared/Views/VariationSelectionView.swift index 21fb710..4c0632e 100644 --- a/Shared/Views/VariationSelectionView.swift +++ b/Shared/Views/VariationSelectionView.swift @@ -46,12 +46,12 @@ struct VariationSelectionView: View { document.variations.selectedVariationIndex = v.index Actions(document: $document).move(to: .forward) } - - Path { path in - path.move(to: CGPoint(x: x1, y: y1)) - path.addLine(to: CGPoint(x: x2, y: y2)) - } - .stroke(Color.blue, lineWidth: 10) + + let p = Arrow(start: CGPoint(x: x1, y: y1), + end: CGPoint(x: x2, y: y2), + length: squareSize*0.4).path + p.fill(Color.blue) + p.stroke(Color.blue, lineWidth: 10) } } }