ludic/packages/ludic.ui/input.ludic

113 lines
3.7 KiB
Text

# input.ludic - what the player did this frame, read once from the runtime's Input: the pointer, its
# button, the wheel, the keys a UI answers, the first pad and what was typed. A test or a host can set
# it instead (input_pad.ludic makes a held direction repeat).
export property UiInput {
x: float = -1.0
y: float = -1.0
down: bool = false
wheel: float = 0.0
tab: bool = false
shift: bool = false
up: bool = false
down_key: bool = false
left: bool = false
right: bool = false
enter: bool = false
space: bool = false
escape: bool = false
backspace: bool = false
typed: string = ""
key: int = 0 # any key pressed this frame, for a key-capture field
held_up: bool = false # a direction held - an arrow key, the d-pad or the left stick -
held_down: bool = false # which presses its arrow now and repeats while it is held
held_left: bool = false
held_right: bool = false
pad_a: bool = false # the pad's south button held: Enter
pad_b: bool = false # its east button held: Escape
held_enter: bool = false # Enter held (with pad_a: a button's on-down, then its on-up)
down_right: bool = false # the other mouse buttons held
down_middle: bool = false
}
function in_read(ui_st: mut UiState) -> void {
if ui_st.in_given {
ui_st.in_given = false
return
}
ui_st.in_was_down = ui_st.in_now.down
var i = ui_st.in_rec_a # the package's own two records, in turn (never a host's)
if ui_st.in_now == ui_st.in_rec_a { i = ui_st.in_rec_b }
ui_st.in_last = ui_st.in_now
in_reset(i)
if ui_st.ui_be != null and ui_st.ui_be.input {
let ps = in_pointer_scale(ui_st)
i.x = float(Input.mouse_x()) * ps
i.y = float(Input.mouse_y()) * ps
i.down = Input.mouse_down(0)
i.down_right = Input.mouse_down(1)
i.down_middle = Input.mouse_down(2)
i.wheel = float(Input.wheel())
i.tab = Input.key_pressed(Key.Tab)
i.shift = Input.key_down(Key.Shift)
i.up = Input.key_pressed(Key.Up)
i.down_key = Input.key_pressed(Key.Down)
i.left = Input.key_pressed(Key.Left)
i.right = Input.key_pressed(Key.Right)
i.enter = Input.key_pressed(Key.Enter)
i.space = Input.key_pressed(Key.Space)
i.escape = Input.key_pressed(Key.Escape)
i.backspace = Input.key_pressed(Key.Backspace)
i.typed = Input.text()
i.key = in_any_key()
in_held(i)
}
in_repeat(ui_st, i)
ui_st.in_now = i
}
# the platform reports the pointer in window points and a renderer draws in pixels: on Retina the
# layout is twice the pointer's numbers, and every hit landed up and to the left of the cursor
function in_pointer_scale(ui_st: UiState) -> float {
if ui_st.ui_be.pointer_scale == null { return 1.0 }
return ui_st.ui_be.pointer_scale()
}
function in_any_key() -> int {
for k in 8 .. 167 {
if Input.key_pressed(k) { return k }
}
return 0
}
# this frame's input, given by a test or a host that reads its own
export function ui_input(ui_st: mut UiState, i: UiInput) -> void {
ui_st.in_was_down = ui_st.in_now.down
ui_st.in_last = ui_st.in_now
in_repeat(ui_st, i)
ui_st.in_now = i
ui_st.in_given = true
}
# every field back to its default (generated from UiInput)
function in_reset(i: UiInput) -> void {
i.x = -1.0
i.y = -1.0
i.down = false
i.wheel = 0.0
i.tab = false
i.shift = false
i.up = false
i.down_key = false
i.left = false
i.right = false
i.enter = false
i.space = false
i.escape = false
i.backspace = false
i.typed = ""
i.key = 0
i.held_up = false
i.held_down = false
i.held_left = false
i.held_right = false
i.pad_a = false
i.pad_b = false
i.held_enter = false
i.down_right = false
i.down_middle = false
}