Skip to content

Commit

Permalink
Use buttons for other parts of the UI
Browse files Browse the repository at this point in the history
  • Loading branch information
mfikes committed Jan 28, 2024
1 parent b45db6e commit 3526023
Show file tree
Hide file tree
Showing 6 changed files with 229 additions and 142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
EDBBC7652B4A24B4004B844C /* Settings.bundle in Resources */ = {isa = PBXBuildFile; fileRef = EDBBC7642B4A24B3004B844C /* Settings.bundle */; };
EDC389FA2B6144BF00E61C79 /* CursorsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = EDC389F92B6144BF00E61C79 /* CursorsView.swift */; };
EDD36F182B443CB00032B9BC /* LengthView.swift in Sources */ = {isa = PBXBuildFile; fileRef = EDD36F172B443CB00032B9BC /* LengthView.swift */; };
EDD95F8E2B65B80C00482CD8 /* ButtonView.swift in Sources */ = {isa = PBXBuildFile; fileRef = EDD95F8D2B65B80C00482CD8 /* ButtonView.swift */; };
EDDCB6292B4E29DC008A0E28 /* ComplexModule in Frameworks */ = {isa = PBXBuildFile; productRef = EDDCB6282B4E29DC008A0E28 /* ComplexModule */; };
EDDCB62B2B4E29DC008A0E28 /* Numerics in Frameworks */ = {isa = PBXBuildFile; productRef = EDDCB62A2B4E29DC008A0E28 /* Numerics */; };
EDDCB62D2B4E29DC008A0E28 /* RealModule in Frameworks */ = {isa = PBXBuildFile; productRef = EDDCB62C2B4E29DC008A0E28 /* RealModule */; };
Expand Down Expand Up @@ -76,6 +77,7 @@
EDBBC7642B4A24B3004B844C /* Settings.bundle */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.plug-in"; path = Settings.bundle; sourceTree = "<group>"; };
EDC389F92B6144BF00E61C79 /* CursorsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CursorsView.swift; sourceTree = "<group>"; };
EDD36F172B443CB00032B9BC /* LengthView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LengthView.swift; sourceTree = "<group>"; };
EDD95F8D2B65B80C00482CD8 /* ButtonView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ButtonView.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -144,6 +146,7 @@
ED737AB12B3F570900926575 /* Units.swift */,
ED16E7702B44C8FC00F1A9BC /* SmoothAnimation.swift */,
EDBBC7602B48ACC1004B844C /* ShakeDetectorView.swift */,
EDD95F8D2B65B80C00482CD8 /* ButtonView.swift */,
ED24868C2B3727B0008820C5 /* Assets.xcassets */,
EDBBC7642B4A24B3004B844C /* Settings.bundle */,
ED24868E2B3727B0008820C5 /* Preview Content */,
Expand Down Expand Up @@ -300,6 +303,7 @@
EDBBC7612B48ACC1004B844C /* ShakeDetectorView.swift in Sources */,
ED24868B2B3727AE008820C5 /* ContentView.swift in Sources */,
EDD36F182B443CB00032B9BC /* LengthView.swift in Sources */,
EDD95F8E2B65B80C00482CD8 /* ButtonView.swift in Sources */,
ED737AAC2B3F4C6100926575 /* Colors.swift in Sources */,
ED737AB22B3F570900926575 /* Units.swift in Sources */,
ED737AB02B3F568200926575 /* SmithChartView.swift in Sources */,
Expand Down
145 changes: 145 additions & 0 deletions Impedance Converter/Impedance Converter/ButtonView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import SwiftUI

import SwiftUI

struct BaseButtonView<Content: View>: View {
@Binding var isPressed: Bool
var rectColor: Color
var pressedRectColor: Color { rectColor.adjusted(brightness: 0.95) }
var panelGapColor: Color
let content: Content

init(isPressed: Binding<Bool>, rectColor: Color, panelGapColor: Color, @ViewBuilder content: () -> Content) {
self._isPressed = isPressed
self.rectColor = rectColor
self.panelGapColor = panelGapColor
self.content = content()
}

var body: some View {
ZStack {
let padding: CGFloat = 5
let offset: CGFloat = padding / 2

GeometryReader { geometry in
RadialGradient(gradient: Gradient(colors: [Color.gray.opacity(0.95), Color.gray.opacity(0)]),
center: .center,
startRadius: 1,
endRadius: 20)
.scaleEffect(x: 1.5, y: 0.5, anchor: .bottom) // Scale in y-direction to create an ellipse
.frame(width: geometry.size.width, height: geometry.size.height + (isPressed ? offset : padding))
}
.blendMode(.normal)
.allowsHitTesting(false)

Canvas { context, size in
let panelGapWidth: CGFloat = min(size.width, size.height) * 0.11
let cornerRadius: CGFloat = 10
let buttonSize = size.width - padding

// Drawing the panel gap
let outerRect = CGRect(x: panelGapWidth / 2 + offset,
y: panelGapWidth / 2 + offset,
width: buttonSize - panelGapWidth,
height: buttonSize - panelGapWidth)
context.fill(Path(roundedRect: outerRect, cornerRadius: cornerRadius*1.2), with: .color(panelGapColor))

// Drawing the inner rectangle (button)
let innerRectSize: CGFloat = buttonSize - 2 * panelGapWidth
let innerRect = CGRect(x: panelGapWidth + offset,
y: panelGapWidth + offset,
width: innerRectSize,
height: innerRectSize)
context.fill(Path(roundedRect: innerRect, cornerRadius: cornerRadius), with: .color(isPressed ? pressedRectColor : rectColor))
// Shiny edge
context.stroke(Path(roundedRect: innerRect, cornerRadius: cornerRadius), with: .color(isPressed ? pressedRectColor : rectColor.adjusted(brightness: 1.2)), lineWidth: 0.5)
}
.frame(width: 50, height: 50)
.aspectRatio(contentMode: .fit)
// Phong in button
GeometryReader { geometry in
RadialGradient(gradient: Gradient(colors: [Color.white.opacity(0.2), Color.white.opacity(0)]),
center: .center,
startRadius: 1,
endRadius: 20)
.scaleEffect(x: 1, y: 0.5, anchor: .bottom) // Scale in y-direction to create an ellipse
.frame(width: geometry.size.width - padding , height: geometry.size.height - padding)
}
.blendMode(.normal)
.allowsHitTesting(false)

content
}
}
}


struct ButtonView: View {
@State private var isPressed = false
var rectColor: Color = .gray
var panelGapColor: Color = .black
var action: () -> Void

var body: some View {
BaseButtonView(isPressed: $isPressed, rectColor: rectColor, panelGapColor: panelGapColor) {
// Empty for now as no specific UI elements are needed
}
.gesture(
DragGesture(minimumDistance: 0)
.onChanged({ _ in
if (!self.isPressed) {
self.isPressed = true
Haptics.shared.playHapticFeedback(for: self.isPressed)
}
})
.onEnded({ _ in
self.isPressed = false
Haptics.shared.playHapticFeedback(for: self.isPressed)
action()
})
)
}
}

struct ToggleButtonView: View {
@Binding var isOn: Bool
@State private var isPressed = false

var rectColor: Color = .gray
var offCircleColor: Color = .black
var onCircleColor: Color = .green
var panelGapColor: Color = .black

var body: some View {
BaseButtonView(isPressed: $isPressed, rectColor: rectColor, panelGapColor: panelGapColor) {
let circleDiameter: CGFloat = 10
let circleColor = isOn ? onCircleColor : offCircleColor

ZStack {
Circle()
.fill(circleColor)
.frame(width: circleDiameter, height: circleDiameter)
if isOn {
Circle()
.fill(circleColor)
.frame(width: circleDiameter, height: circleDiameter)
.blur(radius: 4)
}
}
}
.gesture(
DragGesture(minimumDistance: 0)
.onChanged({ _ in
if (!self.isPressed) {
self.isPressed = true
Haptics.shared.playHapticFeedback(for: self.isPressed)
}
})
.onEnded({ _ in
self.isPressed = false
Haptics.shared.playHapticFeedback(for: self.isPressed)
self.isOn.toggle()
})
)
}
}
24 changes: 17 additions & 7 deletions Impedance Converter/Impedance Converter/CircuitView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,28 @@ import SwiftUI

struct CircuitPickerView: View {
@ObservedObject var viewModel: ViewModel

var body: some View {
HStack {
Spacer()
Picker("Mode", selection: $viewModel.circuitMode) {
Image("Series").tag(CircuitMode.series)
Image("Parallel").tag(CircuitMode.parallel)
Spacer(minLength: 15)
HStack(spacing: -10) {
Image("Series")
ToggleButtonView(isOn: Binding(
get: { viewModel.circuitMode == .series },
set: { if $0 { viewModel.circuitMode = .series }}
))
}
.pickerStyle(SegmentedPickerStyle())
.frame(maxWidth: 200)
Spacer()
HStack(spacing: -15) {
Image("Parallel")
ToggleButtonView(isOn: Binding(
get: { viewModel.circuitMode == .parallel },
set: { if $0 { viewModel.circuitMode = .parallel }}
))
}
}
.padding(10)
.padding(.vertical, -2)
.frame(maxWidth: 250, maxHeight: 50)
}
}

Expand Down
37 changes: 29 additions & 8 deletions Impedance Converter/Impedance Converter/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -132,19 +132,40 @@ struct ContentView: View {

struct ModePickerView: View {
@ObservedObject var viewModel: ViewModel

var body: some View {
HStack {
Picker("Mode", selection: $viewModel.displayMode) {
Text("Impedance Z").tag(DisplayMode.impedance)
Text("Admittance Y").tag(DisplayMode.admittance)
Text("Refl. Coeff. Γ").tag(DisplayMode.reflectionCoefficient)
HStack {
Spacer(minLength: 15)
HStack(spacing: 2) {
Text("Z")
ToggleButtonView(isOn: Binding(
get: { viewModel.displayMode == .impedance },
set: { if $0 { viewModel.displayMode = .impedance }}
))
}
Spacer()
HStack(spacing: 2) {
Text("Y")
ToggleButtonView(isOn: Binding(
get: { viewModel.displayMode == .admittance },
set: { if $0 { viewModel.displayMode = .admittance }}
))
}
Spacer()
HStack(spacing: 2) {
Text("Γ")
ToggleButtonView(isOn: Binding(
get: { viewModel.displayMode == .reflectionCoefficient },
set: { if $0 { viewModel.displayMode = .reflectionCoefficient }}
))
}
Spacer()
}
.pickerStyle(SegmentedPickerStyle())
.padding(.vertical, -2)
.frame(maxWidth: 250, maxHeight: 50)
SettingsButtonView()
}
.padding([.horizontal], 10)
.padding([.top], 10)
.frame(maxWidth: 500)
}
}

Expand Down
Loading

0 comments on commit 3526023

Please # to comment.