Skip to content

Instantly share code, notes, and snippets.

@joshaber
Last active August 29, 2015 14:05
Show Gist options
  • Save joshaber/cecd44aab46f6f40ed0e to your computer and use it in GitHub Desktop.
Save joshaber/cecd44aab46f6f40ed0e to your computer and use it in GitHub Desktop.
func renderForm(state: State) -> Element<State> {
let incButton = Button(title: "Increment", fn: { mapCount($0, inc) })
|> sizeToFit
|> offset(0, 40)
let decButton = Button(title: "Decrement", fn: { mapCount($0, dec) })
|> sizeToFit
let count = Label<State>(text: "\(state.count)")
|> sizeToFit
|> offset(0, 20)
return offset(incButton + count + decButton, 200, 200)
}
func renderBackground(state: State) -> Element<State> {
var element: Element<State> = empty()
if state.count < 0 {
element = rect(NSColor.redColor().colorWithAlphaComponent(0.5))
} else if state.count > 0 {
element = rect(NSColor.greenColor().colorWithAlphaComponent(0.5))
}
return absolute(element, CGSize(width: 1000, height: 1000))
}
func renderLost() -> Element<State> {
return Label(text: "Y O U L O S E")
|> sizeToFit
|> absolute(CGPoint(x: 200, y: 225))
}
func renderWon() -> Element<State> {
return Label(text: "Y O U W I N")
|> sizeToFit
|> absolute(CGPoint(x: 200, y: 225))
}
func renderReset() -> Element<State> {
return Button(title: "Reset", fn: const(State(count: 0)))
|> sizeToFit
|> absolute(CGPoint(x: 2, y: 300))
}
let scoreLimit = 5
func render(state: State) -> Element<State> {
let bg = renderBackground(state)
if state.count <= -scoreLimit {
return bg + renderLost() + renderReset()
} else if state.count >= scoreLimit {
return bg + renderReset() + renderWon()
} else {
return bg + renderForm(state)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment