From 3436d7d5652803103519897c5914e5a37007575a Mon Sep 17 00:00:00 2001 From: Rudrank Riyam Date: Sat, 19 Oct 2024 16:59:36 +0530 Subject: [PATCH] Update sample project --- .../contents.xcworkspacedata | 4 - .../Meshin/Meshin/GradientSamplesView.swift | 166 ++++++++++-------- .../MeshingKit/AnimatedMeshGradientView.swift | 18 +- 3 files changed, 105 insertions(+), 83 deletions(-) delete mode 100644 Meshin/Meshin.xcodeproj/project.xcworkspace/contents.xcworkspacedata diff --git a/Meshin/Meshin.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/Meshin/Meshin.xcodeproj/project.xcworkspace/contents.xcworkspacedata deleted file mode 100644 index 94b2795..0000000 --- a/Meshin/Meshin.xcodeproj/project.xcworkspace/contents.xcworkspacedata +++ /dev/null @@ -1,4 +0,0 @@ - - - diff --git a/Sources/Meshin/Meshin/GradientSamplesView.swift b/Sources/Meshin/Meshin/GradientSamplesView.swift index 6378702..23c6801 100644 --- a/Sources/Meshin/Meshin/GradientSamplesView.swift +++ b/Sources/Meshin/Meshin/GradientSamplesView.swift @@ -2,104 +2,116 @@ import SwiftUI import MeshingKit import Inject -/// A view that displays various gradient samples using MeshingKit. -/// -/// This view showcases different gradient sizes and an animated gradient, -/// demonstrating the capabilities of the MeshingKit framework. +/// A view that displays a list of gradient templates and allows full-screen viewing. struct GradientSamplesView: View { @ObserveInjection var inject - @State private var showAnimation = true + @State private var selectedTemplate: GradientTemplate? var body: some View { - ScrollView { - VStack(spacing: 20) { - Text("MeshingKit Samples") - .font(.largeTitle) - .padding() - - GradientSize2View() - - GradientSize3View() - - GradientSize4View() - - AnimatedGradientView(showAnimation: $showAnimation) + NavigationStack { + List { + Section(header: Text("Size 2 Templates")) { + ForEach(GradientTemplateSize2.allCases, id: \.self) { template in + Button(template.name) { + selectedTemplate = .size2(template) + } + } + } + + Section(header: Text("Size 3 Templates")) { + ForEach(GradientTemplateSize3.allCases, id: \.self) { template in + Button(template.name) { + selectedTemplate = .size3(template) + } + } + } + + Section(header: Text("Size 4 Templates")) { + ForEach(GradientTemplateSize4.allCases, id: \.self) { template in + Button(template.name) { + selectedTemplate = .size4(template) + } + } + } } - .padding() + .navigationTitle("Gradient Templates") } - .enableInjection() - } -} - -/// A view that displays a size 2 gradient sample. -struct GradientSize2View: View { - @ObserveInjection var inject - - var body: some View { - VStack { - Text("Gradient Size 2") - .font(.headline) - - MeshingKit.gradientSize2(template: .mysticTwilight) - .frame(width: 200, height: 200) - .cornerRadius(20) + .sheet(item: $selectedTemplate) { template in + FullScreenGradientView(template: template) } .enableInjection() } } -/// A view that displays a size 3 gradient sample. -struct GradientSize3View: View { - @ObserveInjection var inject - - var body: some View { - VStack { - Text("Gradient Size 3") - .font(.headline) - - MeshingKit.gradientSize3(template: .cosmicAurora) - .frame(width: 200, height: 200) - .cornerRadius(20) +/// An enumeration representing the different types of gradient templates. +enum GradientTemplate: Identifiable { + case size2(GradientTemplateSize2) + case size3(GradientTemplateSize3) + case size4(GradientTemplateSize4) + + var id: String { + switch self { + case .size2(let template): return "size2_\(template.rawValue)" + case .size3(let template): return "size3_\(template.rawValue)" + case .size4(let template): return "size4_\(template.rawValue)" } - .enableInjection() } } -/// A view that displays a size 4 gradient sample. -struct GradientSize4View: View { - @ObserveInjection var inject +/// A view that displays a full-screen version of a selected gradient template. +struct FullScreenGradientView: View { + let template: GradientTemplate + @Environment(\.presentationMode) var presentationMode + @State private var showAnimation: Bool = false var body: some View { - VStack { - Text("Gradient Size 4") - .font(.headline) + ZStack { + gradientView - MeshingKit.gradientSize4(template: .auroraBorealis) - .frame(width: 200, height: 200) - .cornerRadius(20) + VStack { + Spacer() + Toggle("Animate", isOn: $showAnimation) + .padding() + .background(Color.white.opacity(0.7)) + .cornerRadius(8) + Button("Close") { + presentationMode.wrappedValue.dismiss() + } + .padding(.bottom) + .buttonStyle(.borderedProminent) + } } - .enableInjection() + .edgesIgnoringSafeArea(.all) } -} -/// A view that displays an animated gradient sample. -struct AnimatedGradientView: View { - @ObserveInjection var inject - @Binding var showAnimation: Bool - - var body: some View { - VStack { - Text("Animated Gradient") - .font(.headline) - - MeshingKit.animatedGradientSize3(template: .intelligence, showAnimation: $showAnimation) - .frame(width: 200, height: 200) - .cornerRadius(20) - - Toggle("Show Animation", isOn: $showAnimation) - .padding() + @ViewBuilder + var gradientView: some View { + switch template { + case .size2(let size2Template): + AnimatedMeshGradientView( + gridSize: 2, + showAnimation: $showAnimation, + positions: size2Template.positions, + colors: size2Template.colors, + background: size2Template.background + ) + case .size3(let size3Template): + AnimatedMeshGradientView( + gridSize: 3, + showAnimation: $showAnimation, + positions: size3Template.positions, + colors: size3Template.colors, + background: size3Template.background + ) + case .size4(let size4Template): + AnimatedMeshGradientView( + gridSize: 4, + showAnimation: $showAnimation, + positions: size4Template.positions, + colors: size4Template.colors, + background: size4Template.background + ) } - .enableInjection() } } @@ -107,4 +119,4 @@ struct GradientSamplesView_Previews: PreviewProvider { static var previews: some View { GradientSamplesView() } -} +} \ No newline at end of file diff --git a/Sources/MeshingKit/AnimatedMeshGradientView.swift b/Sources/MeshingKit/AnimatedMeshGradientView.swift index 0915699..bf5409d 100644 --- a/Sources/MeshingKit/AnimatedMeshGradientView.swift +++ b/Sources/MeshingKit/AnimatedMeshGradientView.swift @@ -8,14 +8,28 @@ import SwiftUI /// A view that displays an animated mesh gradient. -struct AnimatedMeshGradientView: View { +public struct AnimatedMeshGradientView: View { var gridSize: Int @Binding var showAnimation: Bool var positions: [SIMD2] var colors: [Color] var background: Color - var body: some View { + public init( + gridSize: Int, + showAnimation: Binding, + positions: [SIMD2], + colors: [Color], + background: Color + ) { + self.gridSize = gridSize + self._showAnimation = showAnimation + self.positions = positions + self.colors = colors + self.background = background + } + + public var body: some View { TimelineView(.animation(minimumInterval: 1/120, paused: !showAnimation)) { phase in MeshGradient( width: gridSize,