Skip to content

Commit

Permalink
Fixed arrow shape
Browse files Browse the repository at this point in the history
  • Loading branch information
jean-bovet committed May 19, 2021
1 parent c181fc2 commit 86fd51b
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 8 deletions.
6 changes: 6 additions & 0 deletions BChess.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -220,6 +222,7 @@
A70A61D01FD4AA9600AFDF0E /* FEngineInfo.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = FEngineInfo.mm; sourceTree = "<group>"; };
A70B41A2264A527900A56E29 /* FullMoveView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FullMoveView.swift; sourceTree = "<group>"; };
A712C25226522F6000E05408 /* FullMove.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FullMove.swift; sourceTree = "<group>"; };
A712C255265377C000E05408 /* Arrow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Arrow.swift; sourceTree = "<group>"; };
A716976C262BFF8C00156BD6 /* SettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsView.swift; sourceTree = "<group>"; };
A72C3E012650D78100CB9DB7 /* VariationSelectionView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VariationSelectionView.swift; sourceTree = "<group>"; };
A72C3E042650D82B00CB9DB7 /* Variation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Variation.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -588,6 +591,7 @@
A77F66AE25A832240030E61D /* ContentView.swift */,
A7A30F2E25B2BAFC00729432 /* PromotionView.swift */,
A746997B2637CCF7007E0058 /* NavigationView.swift */,
A712C255265377C000E05408 /* Arrow.swift */,
);
path = Views;
sourceTree = "<group>";
Expand Down Expand Up @@ -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 */,
Expand Down Expand Up @@ -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 */,
Expand Down
1 change: 0 additions & 1 deletion Shared/Actions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
6 changes: 5 additions & 1 deletion Shared/ChessDocument.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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]()
}
Expand Down
54 changes: 54 additions & 0 deletions Shared/Views/Arrow.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
12 changes: 6 additions & 6 deletions Shared/Views/VariationSelectionView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down

0 comments on commit 86fd51b

Please sign in to comment.