Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
359 changes: 194 additions & 165 deletions Examples/Menu/Sources/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,181 +76,210 @@ class ContentViewModel {
// -----------------------------------------------------------------------------
// MARK: - Sample Usage: ContentView and App Launch
// This sample content view is written using the new flexible layout system.
struct ContentView: BrewView {
let viewModel = ContentViewModel()
// Declare the UI in a computed property, similar to SwiftUI.
var body: some BrewView {
VStack(spacing: 7, alignment: .center) {
if viewModel.showCalculator {
// Calculator UI
AnyFramedView(Text(" ", frame: Frame(width: 200, height: 10)))
AnyFramedView(Text(viewModel.calculatorDisplay,
frame: Frame(width: 200, height: 25)))
// struct ContentView: BrewView {
// let viewModel = ContentViewModel()
// // Declare the UI in a computed property, similar to SwiftUI.
// var body: some BrewView {
// VStack(spacing: 7, alignment: .center) {
// if viewModel.showCalculator {
// // Calculator UI
// AnyFramedView(Text(" ", frame: Frame(width: 200, height: 10)))
// AnyFramedView(Text(viewModel.calculatorDisplay,
// frame: Frame(width: 200, height: 25)))

// Row 1: 7, 8, 9, +
AnyFramedView(HStack(spacing: 5) {
AnyFramedView(Button(text: "7",
frame: Frame(width: 45, height: 30)) {
print("Pressed 7")
viewModel.appendDigit(7)
})
AnyFramedView(Button(text: "8",
frame: Frame(width: 45, height: 30)) {
print("Pressed 8")
viewModel.appendDigit(8)
})
AnyFramedView(Button(text: "9",
frame: Frame(width: 45, height: 30)) {
print("Pressed 9")
viewModel.appendDigit(9)
})
AnyFramedView(Button(text: "+",
frame: Frame(width: 45, height: 30)) {
print("Pressed +")
viewModel.setOperation("+")
})
})
// // Row 1: 7, 8, 9, +
// AnyFramedView(HStack(spacing: 5) {
// AnyFramedView(Button(text: "7",
// frame: Frame(width: 45, height: 30)) {
// print("Pressed 7")
// viewModel.appendDigit(7)
// })
// AnyFramedView(Button(text: "8",
// frame: Frame(width: 45, height: 30)) {
// print("Pressed 8")
// viewModel.appendDigit(8)
// })
// AnyFramedView(Button(text: "9",
// frame: Frame(width: 45, height: 30)) {
// print("Pressed 9")
// viewModel.appendDigit(9)
// })
// AnyFramedView(Button(text: "+",
// frame: Frame(width: 45, height: 30)) {
// print("Pressed +")
// viewModel.setOperation("+")
// })
// })

// Row 2: 4, 5, 6, -
AnyFramedView(HStack(spacing: 5) {
AnyFramedView(Button(text: "4",
frame: Frame(width: 45, height: 30)) {
print("Pressed 4")
viewModel.appendDigit(4)
})
AnyFramedView(Button(text: "5",
frame: Frame(width: 45, height: 30)) {
print("Pressed 5")
viewModel.appendDigit(5)
})
AnyFramedView(Button(text: "6",
frame: Frame(width: 45, height: 30)) {
print("Pressed 6")
viewModel.appendDigit(6)
})
AnyFramedView(Button(text: "-",
frame: Frame(width: 45, height: 30)) {
print("Pressed -")
viewModel.setOperation("-")
})
})
// // Row 2: 4, 5, 6, -
// AnyFramedView(HStack(spacing: 5) {
// AnyFramedView(Button(text: "4",
// frame: Frame(width: 45, height: 30)) {
// print("Pressed 4")
// viewModel.appendDigit(4)
// })
// AnyFramedView(Button(text: "5",
// frame: Frame(width: 45, height: 30)) {
// print("Pressed 5")
// viewModel.appendDigit(5)
// })
// AnyFramedView(Button(text: "6",
// frame: Frame(width: 45, height: 30)) {
// print("Pressed 6")
// viewModel.appendDigit(6)
// })
// AnyFramedView(Button(text: "-",
// frame: Frame(width: 45, height: 30)) {
// print("Pressed -")
// viewModel.setOperation("-")
// })
// })

// Row 3: 1, 2, 3, *
AnyFramedView(HStack(spacing: 5) {
AnyFramedView(Button(text: "1",
frame: Frame(width: 45, height: 30)) {
print("Pressed 1")
viewModel.appendDigit(1)
})
AnyFramedView(Button(text: "2",
frame: Frame(width: 45, height: 30)) {
print("Pressed 2")
viewModel.appendDigit(2)
})
AnyFramedView(Button(text: "3",
frame: Frame(width: 45, height: 30)) {
print("Pressed 3")
viewModel.appendDigit(3)
})
AnyFramedView(Button(text: "*",
frame: Frame(width: 45, height: 30)) {
print("Pressed *")
viewModel.setOperation("*")
})
})
// // Row 3: 1, 2, 3, *
// AnyFramedView(HStack(spacing: 5) {
// AnyFramedView(Button(text: "1",
// frame: Frame(width: 45, height: 30)) {
// print("Pressed 1")
// viewModel.appendDigit(1)
// })
// AnyFramedView(Button(text: "2",
// frame: Frame(width: 45, height: 30)) {
// print("Pressed 2")
// viewModel.appendDigit(2)
// })
// AnyFramedView(Button(text: "3",
// frame: Frame(width: 45, height: 30)) {
// print("Pressed 3")
// viewModel.appendDigit(3)
// })
// AnyFramedView(Button(text: "*",
// frame: Frame(width: 45, height: 30)) {
// print("Pressed *")
// viewModel.setOperation("*")
// })
// })

// Row 4: 0, C, =, /
AnyFramedView(HStack(spacing: 5) {
AnyFramedView(Button(text: "0",
frame: Frame(width: 45, height: 30)) {
print("Pressed 0")
viewModel.appendDigit(0)
})
AnyFramedView(Button(text: "C",
frame: Frame(width: 45, height: 30)) {
print("Pressed C")
viewModel.clear()
})
AnyFramedView(Button(text: "=",
frame: Frame(width: 45, height: 30)) {
print("Pressed =")
viewModel.calculate()
})
AnyFramedView(Button(text: "/",
frame: Frame(width: 45, height: 30)) {
print("Pressed /")
viewModel.setOperation("/")
})
})
// // Row 4: 0, C, =, /
// AnyFramedView(HStack(spacing: 5) {
// AnyFramedView(Button(text: "0",
// frame: Frame(width: 45, height: 30)) {
// print("Pressed 0")
// viewModel.appendDigit(0)
// })
// AnyFramedView(Button(text: "C",
// frame: Frame(width: 45, height: 30)) {
// print("Pressed C")
// viewModel.clear()
// })
// AnyFramedView(Button(text: "=",
// frame: Frame(width: 45, height: 30)) {
// print("Pressed =")
// viewModel.calculate()
// })
// AnyFramedView(Button(text: "/",
// frame: Frame(width: 45, height: 30)) {
// print("Pressed /")
// viewModel.setOperation("/")
// })
// })

// Back button
AnyFramedView(Button(text: "BACK",
frame: Frame(width: 200, height: 30)) {
print("Exiting calculator")
viewModel.showCalculator = false
})
} else {
// Main menu UI
AnyFramedView(Text(" ",
frame: Frame(width: 200, height: 10)))
AnyFramedView(Text("EARBII",
frame: Frame(width: 200, height: 25)))
// // Back button
// AnyFramedView(Button(text: "BACK",
// frame: Frame(width: 200, height: 30)) {
// print("Exiting calculator")
// viewModel.showCalculator = false
// })
// } else {
// // Main menu UI
// AnyFramedView(Text(" ",
// frame: Frame(width: 200, height: 10)))
// AnyFramedView(Text("EARBII",
// frame: Frame(width: 200, height: 25)))

AnyFramedView(Button(text: "CALCULATOR",
frame: Frame(width: 200, height: 30)) {
print("Calculator selected")
viewModel.showCalculator = true
})
AnyFramedView(Button(text: "OPTION B",
frame: Frame(width: 200, height: 30)) {
print("Option B selected")
})
if !viewModel.showExtraOption {
AnyFramedView(Button(text: "MORE",
frame: Frame(width: 200, height: 30)) {
print("More selected")
viewModel.showExtraOption.toggle()
})
} else {
AnyFramedView(Button(text: "COLLAPSE",
frame: Frame(width: 200, height: 30)) {
print("Back selected")
viewModel.showExtraOption.toggle()
})
}
// AnyFramedView(Button(text: "CALCULATOR",
// frame: Frame(width: 200, height: 30)) {
// print("Calculator selected")
// viewModel.showCalculator = true
// })
// AnyFramedView(Button(text: "OPTION B",
// frame: Frame(width: 200, height: 30)) {
// print("Option B selected")
// })
// if !viewModel.showExtraOption {
// AnyFramedView(Button(text: "MORE",
// frame: Frame(width: 200, height: 30)) {
// print("More selected")
// viewModel.showExtraOption.toggle()
// })
// } else {
// AnyFramedView(Button(text: "COLLAPSE",
// frame: Frame(width: 200, height: 30)) {
// print("Back selected")
// viewModel.showExtraOption.toggle()
// })
// }

if viewModel.showExtraOption {
AnyFramedView(Button(text: "EXTRA",
frame: Frame(width: 200, height: 30)) {
print("Extra Option selected")
})
} else {
AnyFramedView(EmptyView())
}
// if viewModel.showExtraOption {
// AnyFramedView(Button(text: "EXTRA",
// frame: Frame(width: 200, height: 30)) {
// print("Extra Option selected")
// })
// } else {
// AnyFramedView(EmptyView())
// }

if !viewModel.showExtraOption {
AnyFramedView(HStack(spacing: 10) {
AnyFramedView(Button(text: "X",
frame: Frame(width: 60, height: 30)) {
print("Option C selected")
})
AnyFramedView(Button(text: "Y",
frame: Frame(width: 60, height: 30)) {
print("Option D selected")
})
AnyFramedView(Button(text: "Z",
frame: Frame(width: 60, height: 30)) {
print("Option E selected")
})
})
} else {
AnyFramedView(EmptyView())
}
}
// if !viewModel.showExtraOption {
// AnyFramedView(HStack(spacing: 10) {
// AnyFramedView(Button(text: "X",
// frame: Frame(width: 60, height: 30)) {
// print("Option C selected")
// })
// AnyFramedView(Button(text: "Y",
// frame: Frame(width: 60, height: 30)) {
// print("Option D selected")
// })
// AnyFramedView(Button(text: "Z",
// frame: Frame(width: 60, height: 30)) {
// print("Option E selected")
// })
// })
// } else {
// AnyFramedView(EmptyView())
// }
// }
// }
// }

// // Render the computed body.
// func render(in context: inout BrewUIContext) {
// body.render(in: &context)
// }
// }

struct ContentView: BrewView {
let viewModel = ContentViewModel()
let pixelCat = PixelCat(x: 100, y: 100)

var body: some BrewView {
VStack(spacing: 7, alignment: .center) {
// Your existing UI code

// To start the cat walking:
AnyFramedView(Button(text: "WALK CAT",
frame: Frame(width: 200, height: 30)) {
pixelCat.walk(direction: -1.0) // No mutating needed
})

// To stop the cat:
AnyFramedView(Button(text: "STOP CAT",
frame: Frame(width: 200, height: 30)) {
pixelCat.sit() // No mutating needed
})

AnyFramedView(pixelCat)
}
}

// Render the computed body.
func render(in context: inout BrewUIContext) {
body.render(in: &context)
}
Expand All @@ -277,7 +306,7 @@ buzzer.suspend() // Buzzer off initially
let contentView = ContentView()

let myFontConfig = FontConfiguration(
defaultFontPath: "/SD:/AveriaSansLibre-Bold.ttf",
defaultFontPath: "/lfs/Resources/Fonts/Roboto-Regular.ttf",
defaultPointSize: 10,
defaultDPI: 240
)
Expand Down
Loading