-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPieceShelfView.swift
More file actions
99 lines (90 loc) · 3.26 KB
/
PieceShelfView.swift
File metadata and controls
99 lines (90 loc) · 3.26 KB
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
/*
See the LICENSE.txt file for this sample’s licensing information.
Abstract:
The main SwiftUI view when building the ride.
*/
import CoreGraphics
import SwiftUI
import SwiftSplashTrackPieces
/// A UI element containing the various pieces the player can add to the track.
struct PieceShelfView: View {
@Environment(AppState.self) var appState
@Environment(\.dismiss) private var dismiss
@State private var showStartOverConfirmation = false
@State private var animateIn = true
@State private var canStartRide = false
let timer = Timer.publish(every: 0.01, on: .main, in: .common).autoconnect()
static var displayedOnce = false
var body: some View {
@Bindable var appState = appState
VStack {
Picker("Material", selection: $appState.selectedMaterialIndex) {
Text("Metal")
.tag(MaterialType.metal.rawValue)
Text("Wood")
.tag(MaterialType.wood.rawValue)
Text("Plastic")
.tag(MaterialType.plastic.rawValue)
}
.pickerStyle(.segmented)
.padding(.horizontal, 25)
.padding(.bottom)
.accessibilityAction(named: Text("Select metallic material for next piece added.")) {
appState.selectedMaterialType = .metal
}
.accessibilityAction(named: Text("Select wooden material for next piece added.")) {
appState.selectedMaterialType = .wood
}
.accessibilityAction(named: Text("Select plastic material for next piece added.")) {
appState.selectedMaterialType = .plastic
}
PieceShelfTrackButtonsView()
Button {
appState.startRide()
appState.presentedRide = [.init()]
} label: {
Label("Start Ride", systemImage: "play.fill")
}
.disabled(!canStartRide)
.controlSize(.large)
.padding(.top)
.onChange(of: appState.phase) { _, newPhase in
if newPhase != .buildingTrack && newPhase != .rideRunning {
dismiss()
}
}
.accessibilityElement()
}
.opacity(animateIn ? 0 : 1.0)
.frame(width: 460, height: 420, alignment: .center)
.onAppear {
if Self.displayedOnce {
Timer.scheduledTimer(withTimeInterval: 1.0, repeats: false) { _ in
withAnimation(.easeIn(duration: 0.7)) {
animateIn = false
}
}
} else {
animateIn = false
}
Self.displayedOnce = true
Timer.scheduledTimer(withTimeInterval: 0.2, repeats: true) { _ in
canStartRide = appState.canStartRide
}
}
.onDisappear {
animateIn = true
}
}
}
struct TranslucentGroupBox: GroupBoxStyle {
func makeBody(configuration: Configuration) -> some View {
configuration.content
.background(.regularMaterial)
}
}
#Preview {
PieceShelfView()
.glassBackgroundEffect()
.environment(AppState())
}