# ui_templates.ludic — ludic.ui without a renderer: a shop written as a template, reading a view
# of the program's state and answering with its events. A press is made as a pointer would make
# it; it runs the view's event once the frame is built, and a component's own state keeps. The
# row comes from a component library, whose unexported Price no other file can use.
import "ludic.ui"
program UiTemplates {
numbers float
property Item {
name: string = ""
cost: int = 0
}
var stock: []Item = new []Item
var purse: int = 100
var bought: []int = new []int
function add(name: string, cost: int) -> void {
let it = new Item
it.name = name
it.cost = cost
push(stock, it)
}
view Shop {
purse = purse
stock = stock
owned: int = len(bought)
function afford(i: int) -> bool { return purse >= stock[i].cost }
on buy(i: int) {
purse -= stock[i].cost
push(bought, i)
}
}
const KIT: string = "\n${cost}\n\n \n \n {item.name} -\n \n \n {picks}\n \n \n\n\n"
const PAGE: string = "\n\n\n \n The purse: ${purse}\n \n \n \n Nothing bought yet\n {owned} bought, the last {pick + 1}\n\n\n"
const PRIVATE: string = ""
entry {
add("Rope", 30)
add("Lantern", 80)
ui_load_text(KIT, "ui/kit.xml")
ui_load_text(PAGE, "ui/shop.xml")
ui_load_text(PRIVATE, "ui/peek.xml")
var root: UiNode = ui_nodes("shop", view_shop())
ui_place(root, 0.0, 0.0, 600.0, 400.0)
let press = root.children[1].children[4]
ui_press(ui_hit(root, press.x + 1.0, press.y + 1.0))
root = ui_nodes("shop", view_shop())
ui_place(root, 0.0, 0.0, 600.0, 400.0)
print(`purse {purse}, bought {len(bought)}; the lantern is enabled: {root.children[2].children[4].enabled}`)
}
}