# input_pad.ludic - the first gamepad, and a held direction: the d-pad and the left stick move the # focus as the arrows do, A is Enter and B is Escape; any direction held presses once, then again # after 0.42 s and every 0.11 s after that, on the ui clock const RP_FIRST: float = 0.42 const RP_EVERY: float = 0.11 const PAD_STICK: float = 0.5 # what is held this frame: the arrow keys, the d-pad (SDL's order: 11 up, 12 down, 13 left, 14 # right) and the stick past half way along its larger axis function in_held(i: UiInput) -> void { i.held_up = Input.key_down(Key.Up) i.held_down = Input.key_down(Key.Down) i.held_left = Input.key_down(Key.Left) i.held_right = Input.key_down(Key.Right) i.held_enter = Input.key_down(Key.Enter) if not Input.pad_connected(0) { return } let ax = float(Input.pad_axis(0, 0)) let ay = float(Input.pad_axis(0, 1)) let flat = Math.abs(ax) >= Math.abs(ay) i.held_up = i.held_up or Input.pad_button(0, 11) or (not flat and ay < 0.0 - PAD_STICK) i.held_down = i.held_down or Input.pad_button(0, 12) or (not flat and ay > PAD_STICK) i.held_left = i.held_left or Input.pad_button(0, 13) or (flat and ax < 0.0 - PAD_STICK) i.held_right = i.held_right or Input.pad_button(0, 14) or (flat and ax > PAD_STICK) i.pad_a = Input.pad_button(0, 0) i.pad_b = Input.pad_button(0, 1) } # the held directions and buttons as this frame's presses function in_repeat(ui_st: mut UiState, i: UiInput) -> void { let now = an_now(ui_st) if rp_fire(ui_st, 0, i.held_up, now) { i.up = true } if rp_fire(ui_st, 1, i.held_down, now) { i.down_key = true } if rp_fire(ui_st, 2, i.held_left, now) { i.left = true } if rp_fire(ui_st, 3, i.held_right, now) { i.right = true } if i.pad_a and not ui_st.rp_a { i.enter = true } if i.pad_b and not ui_st.rp_b { i.escape = true } ui_st.rp_a = i.pad_a ui_st.rp_b = i.pad_b let act = i.pad_a or i.held_enter ui_st.in_act_down = act and not ui_st.rp_act ui_st.in_act_up = ui_st.rp_act and not act ui_st.rp_act = act } function rp_fire(ui_st: mut UiState, k: int, held: bool, now: float) -> bool { if not held { ui_st.rp_next[k] = -1.0 return false } if ui_st.rp_next[k] < 0.0 { ui_st.rp_next[k] = now + RP_FIRST return true } if now + 0.000001 < ui_st.rp_next[k] { return false } ui_st.rp_next[k] = ui_st.rp_next[k] + RP_EVERY if ui_st.rp_next[k] <= now { ui_st.rp_next[k] = now + RP_EVERY } return true }