ludic migrate state packages <every example program> packages/ludic.lab/example/plate.ludic 1804 vars into 126 states, 64 into lets; 23498 edits in 460 files Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
50 lines
2.9 KiB
Text
50 lines
2.9 KiB
Text
# 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 = "<ui>\n<component name='Price'><text>${cost}</text></component>\n<component name='Line' export='true'>\n <state picks='{0}'/>\n <row gap='8' w='fill' align='center'>\n <text color='{picked ? \"#ffcc00\" : \"#eeeeee\"}'>{item.name} -</text>\n <Price cost='{item.cost}'/>\n <spacer w='fill'/>\n <text>{picks}</text>\n <button enabled='{afford(i)}' on-press='buy(i); set picks = picks + 1; emit chose'>Buy</button>\n </row>\n</component>\n</ui>\n"
|
|
const PAGE: string = "<ui>\n<import src='kit.xml' as='kit'/>\n<screen name='shop' gap='4' pad='10'>\n <state pick='{-1}'/>\n <text size='24'>The purse: ${purse}</text>\n <each in='{stock}' as='item' index='i'>\n <kit:Line item='{item}' i='{i}' picked='{pick == i}' on-chose='set pick = i'/>\n </each>\n <if test='{owned == 0}'><text>Nothing bought yet</text></if>\n <else><text>{owned} bought, the last {pick + 1}</text></else>\n</screen>\n</ui>\n"
|
|
const PRIVATE: string = "<ui><import src='kit.xml' as='kit'/><screen name='peek'><kit:Price cost='{1}'/></screen></ui>"
|
|
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}`)
|
|
}
|
|
}
|