ludic/examples/library/ui_pointer.ludic

62 lines
3.1 KiB
Text

# ui_pointer.ludic — pointer events: a native map takes presses, drags (still its own when the
# pointer leaves it, until let go), the wheel and the release, but not a press on the button over it;
# a pad takes the same in markup; a hold button says on-down and on-up, and is :active while held.
import "ludic.ui"
import "ui_pointer_parts/Chart.ludic"
program UiPointer {
numbers float
state UiPointerState {
log: string = ""
dragged: float = 0.0
}
function map_measure(n: UiNode, avail: float) -> void { }
function map_draw(n: UiNode) -> void { }
function map_input(ui_pointer_st: mut UiPointerState, n: UiNode, e: UiPointer) -> bool {
if e.kind == "drag" { ui_pointer_st.dragged = ui_pointer_st.dragged + e.dx } else if e.kind != "pointermove" { ui_pointer_st.log = ui_pointer_st.log + `{e.kind} {int(e.x)},{int(e.y)} {e.button} {int(e.wheel)}; ` }
return true
}
function frame(ui_st: mut UiState, x: float, y: float, down: bool, wheel: float, enter: bool) -> UiNode {
let i = new UiInput
i.x = x
i.y = y
i.down = down
i.wheel = wheel
i.held_enter = enter
ui_input(ui_st, i)
ui_show(ui_st, "Chart", null, 0.0, 0.0, 400.0, 400.0)
let root: UiNode = ui_nodes(ui_st, "Chart", null)
ui_place(ui_st, root, 0.0, 0.0, 400.0, 400.0)
return root.children[0]
}
function said(page: UiNode) -> string { return page.children[len(page.children) - 1].text }
entry (ui_pointer_st: UiPointerState, ui_st: mut UiState) {
ui_backend(ui_st, new UiBackend)
ui_native(ui_st, "chartmap", fn map_measure, fn map_draw)
ui_native_input(ui_st, "chartmap", fn map_input)
let page = frame(ui_st, -1.0, -1.0, false, 0.0, false)
let box = page.children[0]
let pad = page.children[1]
let hold = page.children[2]
frame(ui_st, box.x + 20.0, box.y + 20.0, true, 0.0, false)
frame(ui_st, box.x + 20.0, box.y + 20.0, false, 0.0, false)
frame(ui_st, box.x + 100.0, box.y + 80.0, true, 0.0, false)
frame(ui_st, box.x + 130.0, box.y + 60.0, true, 0.0, false)
frame(ui_st, box.x + 300.0, box.y + 300.0, true, 0.0, false)
frame(ui_st, box.x + 300.0, box.y + 300.0, false, 0.0, false)
frame(ui_st, box.x + 50.0, box.y + 50.0, false, 1.0, false)
frame(ui_st, pad.x + 7.0, pad.y + 9.0, true, 0.0, false)
frame(ui_st, pad.x + 17.0, pad.y + 9.0, true, 0.0, false)
frame(ui_st, pad.x + 17.0, pad.y + 9.0, false, 0.0, false)
frame(ui_st, pad.x + 17.0, pad.y + 9.0, false, 2.0, false)
frame(ui_st, hold.x + 5.0, hold.y + 5.0, true, 0.0, false)
let down = frame(ui_st, hold.x + 5.0, hold.y + 5.0, true, 0.0, false)
let active = down.children[2].bg
let mid = said(down)
frame(ui_st, hold.x + 5.0, hold.y + 90.0, false, 0.0, false)
let off = said(frame(ui_st, -1.0, -1.0, false, 0.0, false))
frame(ui_st, -1.0, -1.0, false, 0.0, true)
let keyed = said(frame(ui_st, -1.0, -1.0, false, 0.0, true))
let up = frame(ui_st, -1.0, -1.0, false, 0.0, false)
print(`{ui_pointer_st.log}dragged {int(ui_pointer_st.dragged)} | {mid} active {active} | {off} | {keyed} | {said(up)} {up.children[2].bg}`)
}
}