Skip to content

Commit

Permalink
remove haptic engine from spray
Browse files Browse the repository at this point in the history
  • Loading branch information
sakofchit committed Nov 2, 2024
1 parent 4f47e33 commit a563e89
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 61 deletions.
7 changes: 7 additions & 0 deletions .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>Pow.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
</dict>
</plist>
23 changes: 23 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"pins" : [
{
"identity" : "flyingfox",
"kind" : "remoteSourceControl",
"location" : "https://github.com/swhitty/FlyingFox.git",
"state" : {
"revision" : "ed86fc6d68ec1467aaab3e494b581e66dd7a4512",
"version" : "0.12.2"
}
},
{
"identity" : "snapshotpreviews-ios",
"kind" : "remoteSourceControl",
"location" : "https://github.com/EmergeTools/SnapshotPreviews-iOS",
"state" : {
"revision" : "d6629ac56ad8c4cd2bb2aa11b6bdcc435f92cfc9",
"version" : "0.7.6"
}
}
],
"version" : 2
}
63 changes: 2 additions & 61 deletions Sources/Pow/Effects/SprayEffect.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import SwiftUI
import simd

#if os(iOS)
import CoreHaptics
#endif

public extension AnyChangeEffect {
/// An effect that emits multiple particles in different shades and sizes moving up from the origin point.
///
Expand All @@ -22,13 +18,9 @@ public extension AnyChangeEffect {

internal struct SpraySimulation<ParticleView: View>: ViewModifier, Simulative {
var particle: ParticleView

var impulseCount: Int = 0

var initialVelocity: CGFloat = 0.0

var origin: UnitPoint

private let spring = Spring(zeta: 1, stiffness: 30)

private struct Ping: Identifiable {
Expand All @@ -40,9 +32,7 @@ internal struct SpraySimulation<ParticleView: View>: ViewModifier, Simulative {

@State
private var pings: [Ping] = []

private let layer: ParticleLayer

@Environment(\.particleLayerNames)
var particleLayerNames

Expand Down Expand Up @@ -80,7 +70,6 @@ internal struct SpraySimulation<ParticleView: View>: ViewModifier, Simulative {

Canvas { context, size in
var symbols: [GraphicsContext.ResolvedSymbol] = []

var i = 0
var nextSymbol: GraphicsContext.ResolvedSymbol? = context.resolveSymbol(id: i)
while let symbol = nextSymbol {
Expand All @@ -102,7 +91,7 @@ internal struct SpraySimulation<ParticleView: View>: ViewModifier, Simulative {
})
let value: SIMD16<Float> = indices / 10

/// To simply the expression :rolleyes:
/// To simplify the expression :rolleyes:
let adjustedValue: SIMD16<Float> = (value - 0.5)

// in degrees
Expand All @@ -112,38 +101,27 @@ internal struct SpraySimulation<ParticleView: View>: ViewModifier, Simulative {
var rng = SeededRandomNumberGenerator(seed: ping.id)

let symbolOffset = (0...10).randomElement(using: &rng) ?? 0

let value2: SIMD16<Float> = SIMD16<Float>.random(in: 0.0 ... 1.0, using: &rng) + scaleFactors

let insetAmount: Float = cos(ping.progress) * pow(ping.progress, 1) * -Float(symbolHeight) * 2.5

let phases: SIMD16<Float> = (ping.progress * 0.75) + value2
let sineScales: SIMD16<Float> = simd_abs(sin(phases * SIMD16(repeating: .pi)))
let scales: SIMD16<Float> = sineScales * (1.0 - pow(ping.progress, 8.0)) * pow(ping.progress, 0.25)

let brightness: SIMD16<Float> = .random(in: -0.1 ... 0.1, using: &rng)

let x: SIMD16<Float> = adjustedValue * (sin(ping.progress * Float.pi) * Float(symbolWidth) * -2)
let y: SIMD16<Float> = insetAmount - (value2 * ping.progress) * Float(symbolHeight) * 2.5

for i in 0...10 {
let point = CGPoint(x: x[i], y: y[i])

let angle = Angle(degrees: angles[i])
let scale = Double(scales[i])

let symbol = symbols[(i + symbolOffset) % symbols.count]

// If we're drawing in the particle group, fade in the
// the particles as we're no longer drawing behind the
// view.
if hasParticleLayer {
context.opacity = clamp(Double(ping.progress) * 4)
}

context.drawLayer { context in
context.addFilter(.brightness(Double(brightness[i])))

context.rotate(by: .degrees(Double(ping.progress) * -angle.degrees + -angle.degrees * 0.25))
context.translateBy(x: point.x, y: point.y)
context.scaleBy(x: scale, y: scale)
Expand Down Expand Up @@ -172,7 +150,6 @@ internal struct SpraySimulation<ParticleView: View>: ViewModifier, Simulative {
.particleLayerBackground(layer: layer, isEnabled: !isSimulationPaused) {
overlay
}
.usesCustomHaptics()
.onChange(of: impulseCount) { newValue in
let ping = Ping(
id: UUID(),
Expand All @@ -184,12 +161,6 @@ internal struct SpraySimulation<ParticleView: View>: ViewModifier, Simulative {
withAnimation(nil) {
pings.append(ping)
}

#if os(iOS)
if let hapticPattern {
Haptics.play(hapticPattern)
}
#endif
}
}

Expand Down Expand Up @@ -218,37 +189,6 @@ internal struct SpraySimulation<ParticleView: View>: ViewModifier, Simulative {
abs(ping.progress - ping.target) < 0.04 && ping.velocity < 0.04
}
}

#if os(iOS)
private var hapticPattern: CHHapticPattern? {
var rng = SeededRandomNumberGenerator(seed: 123)

return try? CHHapticPattern(
events: (0 ..< 5).map { i in
let i = Float(i)

let relativeTime: TimeInterval

if i == 0 {
relativeTime = 0
} else {
relativeTime = Double(i * 0.03) + .random(in: -0.005 ... 0.005, using: &rng)
}

return CHHapticEvent(
eventType: .hapticContinuous,
parameters: [
CHHapticEventParameter(parameterID: .hapticIntensity, value: 0.6 * (i / 5) + .random(in: -0.2 ... 0.2, using: &rng)),
CHHapticEventParameter(parameterID: .hapticSharpness, value: 0.2)
],
relativeTime: relativeTime,
duration: 0.05
)
},
parameterCurves: []
)
}
#endif
}

private struct RelativeOffsetModifier: GeometryEffect {
Expand Down Expand Up @@ -276,6 +216,7 @@ private extension Angle {
}
}


#if os(iOS) && DEBUG
struct SprayChangeEffect_Previews: PreviewProvider {
struct Preview: View {
Expand Down

0 comments on commit a563e89

Please sign in to comment.