Skip to content

Commit

Permalink
Add slime showcase
Browse files Browse the repository at this point in the history
  • Loading branch information
aheze committed Sep 27, 2022
1 parent fbdbc8b commit ec68474
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 13 deletions.
14 changes: 11 additions & 3 deletions Example/PrismExample/PrismExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
3C35DD3328E27E19006B7A47 /* Gradient.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C35DD3228E27E19006B7A47 /* Gradient.swift */; };
3C35DD3528E27FD5006B7A47 /* Image.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C35DD3428E27FD5006B7A47 /* Image.swift */; };
3C35DD3728E28269006B7A47 /* Glass.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C35DD3628E28269006B7A47 /* Glass.swift */; };
3C35DD3A28E2846D006B7A47 /* Slime.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C35DD3928E2846D006B7A47 /* Slime.swift */; };
3C35DD3C28E286A4006B7A47 /* Utilities.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C35DD3B28E286A4006B7A47 /* Utilities.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -37,6 +39,8 @@
3C35DD3228E27E19006B7A47 /* Gradient.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Gradient.swift; sourceTree = "<group>"; };
3C35DD3428E27FD5006B7A47 /* Image.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Image.swift; sourceTree = "<group>"; };
3C35DD3628E28269006B7A47 /* Glass.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Glass.swift; sourceTree = "<group>"; };
3C35DD3928E2846D006B7A47 /* Slime.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Slime.swift; sourceTree = "<group>"; };
3C35DD3B28E286A4006B7A47 /* Utilities.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Utilities.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -74,8 +78,9 @@
3C35DD1328E232E0006B7A47 /* PrismExampleApp.swift */,
3C35DD2D28E261DE006B7A47 /* ViewModel.swift */,
3C35DD1528E232E0006B7A47 /* ContentView.swift */,
3C35DD3B28E286A4006B7A47 /* Utilities.swift */,
3C35DD2F28E27CE0006B7A47 /* Gallery */,
3C35DD3828E283FA006B7A47 /* Examples */,
3C35DD3828E283FA006B7A47 /* Showcase */,
3C35DD2A28E25D56006B7A47 /* Templates */,
3C35DD1728E232E1006B7A47 /* Assets.xcassets */,
3C35DD1928E232E1006B7A47 /* Preview Content */,
Expand Down Expand Up @@ -112,11 +117,12 @@
path = Gallery;
sourceTree = "<group>";
};
3C35DD3828E283FA006B7A47 /* Examples */ = {
3C35DD3828E283FA006B7A47 /* Showcase */ = {
isa = PBXGroup;
children = (
3C35DD3928E2846D006B7A47 /* Slime.swift */,
);
path = Examples;
path = Showcase;
sourceTree = "<group>";
};
/* End PBXGroup section */
Expand Down Expand Up @@ -196,10 +202,12 @@
buildActionMask = 2147483647;
files = (
3C35DD2928E25D53006B7A47 /* Color.swift in Sources */,
3C35DD3A28E2846D006B7A47 /* Slime.swift in Sources */,
3C35DD3528E27FD5006B7A47 /* Image.swift in Sources */,
3C35DD1628E232E0006B7A47 /* ContentView.swift in Sources */,
3C35DD2728E25C79006B7A47 /* DetailView.swift in Sources */,
3C35DD1428E232E0006B7A47 /* PrismExampleApp.swift in Sources */,
3C35DD3C28E286A4006B7A47 /* Utilities.swift in Sources */,
3C35DD2E28E261DE006B7A47 /* ViewModel.swift in Sources */,
3C35DD3128E27CEE006B7A47 /* GalleryCardView.swift in Sources */,
3C35DD2C28E25DFD006B7A47 /* GalleryView.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import SwiftUI

struct GalleryCardView<Content: View>: View {
@ObservedObject var model: ViewModel
var kind: TemplateKind
var kind: ExampleKind
@ViewBuilder var content: Content

var body: some View {
Expand Down
14 changes: 9 additions & 5 deletions Example/PrismExample/PrismExample/Gallery/GalleryView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import Prism
import SwiftUI

enum TemplateKind: String {
enum ExampleKind: String {
case slime = "Slime"

case color = "Color"
case gradient = "Gradient"
case image = "Image"
Expand All @@ -26,11 +28,10 @@ struct GalleryView: View {
var body: some View {
LazyVGrid(columns: columns, spacing: 8) {
Section {
ColorGalleryView(model: model)
SlimeGalleryView(model: model)

GradientGalleryView(model: model)
} header: {
Text("Examples")
Text("Showcase")
.foregroundColor(UIColor.secondaryLabel.color)
.textCase(.uppercase)
.frame(maxWidth: .infinity, alignment: .leading)
Expand All @@ -53,8 +54,11 @@ struct GalleryView: View {
}
}
.padding(.horizontal, 20)
.navigationDestination(for: TemplateKind.self) { kind in
.navigationDestination(for: ExampleKind.self) { kind in
switch kind {
case .slime:
SlimeDetailView()

case .color:
ColorDetailView()
case .gradient:
Expand Down
152 changes: 152 additions & 0 deletions Example/PrismExample/PrismExample/Showcase/Slime.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
//
// Slime.swift
// PrismExample
//
// Created by A. Zheng (github.com/aheze) on 9/26/22.
// Copyright © 2022 A. Zheng. All rights reserved.
//

import Prism
import SwiftUI

struct DisplayedPrismConfiguration: Identifiable {
let id = UUID()
var configuration: PrismConfiguration
}

struct SlimeGalleryView: View {
@ObservedObject var model: ViewModel

var body: some View {
GalleryCardView(model: model, kind: .slime) {
PrismView(
tilt: 0.3,
size: .init(width: 100, height: 100),
extrusion: 60
) {
Color.green
.opacity(0.75)
} left: {
Color.green.brightness(-0.1)
.opacity(0.5)
} right: {
Color.green.brightness(-0.3)
.opacity(0.5)
}
.frame(minHeight: 220)
.offset(y: 20)
}
}
}

struct SlimeDetailView: View {
static let defaultLength = CGFloat(100)
static let defaultExtrusion = CGFloat(60)

@State var displayedConfigurations: [DisplayedPrismConfiguration] = [
.init(
configuration: .init(
tilt: 0.3,
size: .init(width: defaultLength, height: defaultLength),
extrusion: defaultExtrusion
)
),
.init(
configuration: .init(
tilt: 0.3,
size: .init(width: defaultLength, height: defaultLength),
extrusion: defaultExtrusion
)
),
.init(
configuration: .init(
tilt: 0.3,
size: .init(width: defaultLength, height: defaultLength),
extrusion: defaultExtrusion
)
),
]

var body: some View {
PrismCanvas(tilt: 0.3) {
HStack(spacing: 20) {
ForEach($displayedConfigurations) { $displayedConfiguration in
let configuration = Binding {
$displayedConfiguration.wrappedValue.configuration
} set: { newValue in
$displayedConfiguration.wrappedValue.configuration = newValue
}

SlimePrismView(configuration: configuration)
}
}
}
.navigationTitle("Color")
}
}

struct SlimePrismView: View {
@Binding var configuration: PrismConfiguration
@State var isPressed = false

var body: some View {
Button {} label: {
PrismView(configuration: configuration) {
Color.green
.opacity(0.75)
} left: {
Color.green.brightness(-0.1)
.opacity(0.5)
} right: {
Color.green.brightness(-0.3)
.opacity(0.5)
}
}
.buttonStyle(PressedButtonStyle(isPressed: $isPressed))
.onChange(of: isPressed) { newValue in
if newValue {
withAnimation(
.spring(response: 0.21, dampingFraction: 0.5, blendDuration: 1)
) {
configuration.extrusion *= 0.2
}
} else {
withAnimation(
.spring(response: 0.4, dampingFraction: 0.7, blendDuration: 1)
) {
configuration.extrusion = SlimeDetailView.defaultLength * 3 / 2
configuration.size = .init(
width: SlimeDetailView.defaultLength * 2 / 5,
height: SlimeDetailView.defaultLength * 2 / 5
)
}

DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
withAnimation(
.spring(response: 0.3, dampingFraction: 0.7, blendDuration: 1)
) {
configuration.levitation = SlimeDetailView.defaultLength * 3 / 2
configuration.extrusion = 10
}
}

DispatchQueue.main.asyncAfter(deadline: .now() + 0.293) {
withAnimation(
.spring(response: 0.7, dampingFraction: 0.7, blendDuration: 1)
) {
configuration.size = .init(width: SlimeDetailView.defaultLength, height: SlimeDetailView.defaultLength)
configuration.levitation = 0
}
}

DispatchQueue.main.asyncAfter(deadline: .now() + 0.66) {
withAnimation(
.spring(response: 0.6, dampingFraction: 0.7, blendDuration: 1)
) {
configuration.extrusion = SlimeDetailView.defaultExtrusion
}
}
}
}
}
}
43 changes: 43 additions & 0 deletions Example/PrismExample/PrismExample/Utilities.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// Utilities.swift
// PrismExample
//
// Created by A. Zheng (github.com/aheze) on 9/26/22.
// Copyright © 2022 A. Zheng. All rights reserved.
//

import SwiftUI

struct PressedButtonStyle: ButtonStyle {
@Binding var isPressed: Bool

func makeBody(configuration: Configuration) -> some View {
var animation: Animation?

/// only change when it's different
if isPressed != configuration.isPressed {
if configuration.isPressed {
animation = .spring(
response: 0.1,
dampingFraction: 0.6,
blendDuration: 1
)
} else {
animation = .spring(
response: 0.4,
dampingFraction: 0.4,
blendDuration: 1
)
}

DispatchQueue.main.async {
withAnimation(animation) {
isPressed = configuration.isPressed
}
}
}

return configuration.label
.opacity(1) /// needs a modifier to actually be called
}
}
8 changes: 4 additions & 4 deletions Sources/PrismView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import SwiftUI

struct PrismView<Content: View, Left: View, Right: View>: View {
public struct PrismView<Content: View, Left: View, Right: View>: View {
var configuration: PrismConfiguration
@ViewBuilder var content: Content
@ViewBuilder var left: Left
Expand All @@ -26,7 +26,7 @@ struct PrismView<Content: View, Left: View, Right: View>: View {
self.right = right()
}

var body: some View {
public var body: some View {
let topRightOffset = configuration.tilt * configuration.size.width
let topAngle = atan2(topRightOffset, configuration.size.width)

Expand Down Expand Up @@ -77,8 +77,8 @@ struct PrismView<Content: View, Left: View, Right: View>: View {
}
}

extension PrismView {
public init(
public extension PrismView {
init(
tilt: CGFloat,
size: CGSize,
extrusion: CGFloat,
Expand Down

0 comments on commit ec68474

Please sign in to comment.