Refactor VerticalDiceRollView layout computations
This commit is contained in:
parent
25238ac448
commit
632b554a64
|
@ -21,7 +21,7 @@ extension VerticalDiceRollView: View {
|
|||
var body: some View {
|
||||
GeometryReader { geometry in
|
||||
ZStack {
|
||||
ForEach(self.diceCollection.indexed(), id: \.element) { (index, dice) in
|
||||
ForEach(self.diceCollection.indexed(), id: \.0.self) { (index, dice) in
|
||||
DiceView(dice: dice)
|
||||
.frame(
|
||||
width: self.sideLength(forDiceIn: geometry),
|
||||
|
@ -88,14 +88,15 @@ private extension VerticalDiceRollView {
|
|||
let availableVerticalSpace = geometry.size.height - (rowSpacing * CGFloat(rowCount - 1))
|
||||
let availableHorizontalSpace = geometry.size.width - (columnSpacing * CGFloat(columnCount - 1))
|
||||
|
||||
if columnCount >= rowCount {
|
||||
return availableHorizontalSpace / CGFloat(columnCount)
|
||||
if availableHorizontalSpace < availableVerticalSpace {
|
||||
return availableHorizontalSpace / CGFloat(max(rowCount, columnCount))
|
||||
} else {
|
||||
return availableVerticalSpace / CGFloat(rowCount)
|
||||
return availableVerticalSpace / CGFloat(max(rowCount, columnCount))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
func position(forDiceAt index: Int, in geometry: GeometryProxy) -> CGPoint {
|
||||
let sideLength = self.sideLength(forDiceIn: geometry)
|
||||
let columnNumber: CGFloat = CGFloat(self.columnNumber(forDiceAt: index))
|
||||
|
@ -103,7 +104,7 @@ private extension VerticalDiceRollView {
|
|||
|
||||
let columnSpacingOffset = self.columnSpacingOffset(forDiceAt: index)
|
||||
let rowSpacingOffset = self.rowSpacingOffset(forDiceAt: index)
|
||||
|
||||
|
||||
|
||||
let centerX = (columnNumber * sideLength) + columnSpacingOffset
|
||||
+ (sideLength / 2)
|
||||
|
@ -124,11 +125,12 @@ struct VerticalDiceRollView_Previews: PreviewProvider {
|
|||
static var previews: some View {
|
||||
VStack(alignment: .center) {
|
||||
VerticalDiceRollView(
|
||||
diceCollection: [.one, .two, .three, .four]
|
||||
// diceCollection: [.one, .two, .three, .four]
|
||||
// diceCollection: [.one, .two, .three, .four, .five,]
|
||||
// diceCollection: [.one, .two, .three, .four, .five, .six]
|
||||
// diceCollection: [.one, .two,]
|
||||
// diceCollection: [.one]
|
||||
// diceCollection: [.one, .two, .three]
|
||||
diceCollection: [.one, .two, .three]
|
||||
)
|
||||
|
||||
Spacer()
|
||||
|
|
|
@ -0,0 +1,73 @@
|
|||
//
|
||||
// VerticalDiceSetView.swift
|
||||
// Dicey
|
||||
//
|
||||
// Created by CypherPoet on 2/2/20.
|
||||
// ✌️
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
|
||||
struct VerticalDiceSetView {
|
||||
var diceCount: Int = 1
|
||||
var diceSideLength: CGFloat = 40.0
|
||||
}
|
||||
|
||||
|
||||
// MARK: - View
|
||||
extension VerticalDiceSetView: View {
|
||||
|
||||
var body: some View {
|
||||
GeometryReader { geometry in
|
||||
Rectangle()
|
||||
.fill(Color.purple)
|
||||
.frame(
|
||||
width: (geometry.size.width / CGFloat(self.columnCount)) - (self.columnSpacing * (CGFloat(self.columnCount - 1))),
|
||||
height: (geometry.size.height / CGFloat(self.rowCount)) - (self.rowSpacing * CGFloat((self.rowCount - 1)))
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Computeds
|
||||
extension VerticalDiceSetView {
|
||||
var rowSpacing: CGFloat { 16.0 }
|
||||
var columnSpacing: CGFloat { 20.0 }
|
||||
|
||||
var rowCount: Int { ((diceCount - 1) / 2) + 1 }
|
||||
var columnCount: Int { (diceCount / 2) + 1 }
|
||||
// var rowCount: Int { 1 }
|
||||
// var columnCount: Int { 1 }
|
||||
}
|
||||
|
||||
|
||||
// MARK: - View Variables
|
||||
extension VerticalDiceSetView {
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Private Helpers
|
||||
private extension VerticalDiceSetView {
|
||||
|
||||
func diceSideLength(in geometry: GeometryProxy) {
|
||||
let maxDimension = max(geometry.size.width, geometry.size.height)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// MARK: - Preview
|
||||
struct DiceSetView_Previews: PreviewProvider {
|
||||
|
||||
static var previews: some View {
|
||||
VStack {
|
||||
VerticalDiceSetView(
|
||||
diceCount: 4
|
||||
)
|
||||
}
|
||||
// .environment(\.managedObjectContext, CurrentApp.coreDataManager.mainContext)
|
||||
}
|
||||
}
|
|
@ -13,7 +13,7 @@ struct DiceGeneratorView {
|
|||
@Environment(\.horizontalSizeClass) var horizontalSizeClass
|
||||
@Environment(\.verticalSizeClass) var verticalSizeClass
|
||||
|
||||
private let diceCountRange = 1...4
|
||||
private let diceCountRange = 1...6
|
||||
|
||||
@ObservedObject var viewModel: ViewModel
|
||||
let onDiceRolled: ((DiceRoll) -> Void)?
|
||||
|
@ -25,14 +25,12 @@ struct DiceGeneratorView {
|
|||
extension DiceGeneratorView: View {
|
||||
|
||||
var body: some View {
|
||||
Group {
|
||||
if isShowingHorizontalDiceLayout {
|
||||
HorizontalDiceRollView(diceCollection: viewModel.diceCollection)
|
||||
// .offset(offsetFromShake)
|
||||
} else {
|
||||
VerticalDiceRollView(diceCollection: viewModel.diceCollection)
|
||||
// .offset(offsetFromShake)
|
||||
}
|
||||
VStack {
|
||||
diceCollectionSection
|
||||
|
||||
rollButton
|
||||
|
||||
controlPanel
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -51,7 +49,20 @@ extension DiceGeneratorView {
|
|||
// MARK: - View Variables
|
||||
extension DiceGeneratorView {
|
||||
|
||||
private var roleButton: some View {
|
||||
private var diceCollectionSection: some View {
|
||||
Group {
|
||||
if isShowingHorizontalDiceLayout {
|
||||
HorizontalDiceRollView(diceCollection: viewModel.diceCollection)
|
||||
// .offset(offsetFromShake)
|
||||
} else {
|
||||
VerticalDiceRollView(diceCollection: viewModel.diceCollection)
|
||||
// .offset(offsetFromShake)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private var rollButton: some View {
|
||||
Button(action: {
|
||||
// self.onDiceRolled?(viewModel.diceRollFromForm)
|
||||
}) {
|
||||
|
@ -89,9 +100,7 @@ private extension DiceGeneratorView {
|
|||
struct DiceGeneratorView_Previews: PreviewProvider {
|
||||
|
||||
static var previews: some View {
|
||||
// let diceRoll = DiceRoll(context: CurrentApp.coreDataManager.mainContext)
|
||||
|
||||
return DiceGeneratorView(
|
||||
DiceGeneratorView(
|
||||
viewModel: .init(
|
||||
diceCount: .constant(2)
|
||||
// diceRoll: .constant(diceRoll)
|
||||
|
|
Loading…
Reference in New Issue