Skip to content

Commit

Permalink
Update sample project
Browse files Browse the repository at this point in the history
  • Loading branch information
rudrankriyam committed Oct 19, 2024
1 parent ddb46bb commit 3436d7d
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 83 deletions.

This file was deleted.

166 changes: 89 additions & 77 deletions Sources/Meshin/Meshin/GradientSamplesView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,109 +2,121 @@ 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()
}
}

struct GradientSamplesView_Previews: PreviewProvider {
static var previews: some View {
GradientSamplesView()
}
}
}
18 changes: 16 additions & 2 deletions Sources/MeshingKit/AnimatedMeshGradientView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Float>]
var colors: [Color]
var background: Color

var body: some View {
public init(
gridSize: Int,
showAnimation: Binding<Bool>,
positions: [SIMD2<Float>],
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,
Expand Down

0 comments on commit 3436d7d

Please sign in to comment.