# 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
}
state UiTemplatesState {
stock: []Item = new []Item
purse: int = 100
bought: []int = new []int
}
function add(ui_templates_st: mut UiTemplatesState, name: string, cost: int) -> void {
let it = new Item
it.name = name
it.cost = cost
push(ui_templates_st.stock, it)
}
view Shop (ui_templates_st: mut UiTemplatesState) {
purse = ui_templates_st.purse
stock = ui_templates_st.stock
owned: int = len(ui_templates_st.bought)
function afford(i: int) -> bool { return ui_templates_st.purse >= ui_templates_st.stock[i].cost }
on buy(i: int) {
ui_templates_st.purse -= ui_templates_st.stock[i].cost
push(ui_templates_st.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 (ui_st: mut UiState, ui_templates_st: mut UiTemplatesState) {
add(ui_templates_st, "Rope", 30)
add(ui_templates_st, "Lantern", 80)
ui_load_text(ui_st, KIT, "ui/kit.xml")
ui_load_text(ui_st, PAGE, "ui/shop.xml")
ui_load_text(ui_st, PRIVATE, "ui/peek.xml")
var root: UiNode = ui_nodes(ui_st, "shop", view_shop())
ui_place(ui_st, root, 0.0, 0.0, 600.0, 400.0)
let press = root.children[1].children[4]
ui_press(ui_st, ui_hit(root, press.x + 1.0, press.y + 1.0))
root = ui_nodes(ui_st, "shop", view_shop())
ui_place(ui_st, root, 0.0, 0.0, 600.0, 400.0)
print(`purse {ui_templates_st.purse}, bought {len(ui_templates_st.bought)}; the lantern is enabled: {root.children[2].children[4].enabled}`)
}
}