mirror of
https://github.com/bryanthaboi/gen1recomp.git
synced 2026-08-18 11:44:42 +02:00
bzinga
This commit is contained in:
+6
-5
@@ -349,17 +349,16 @@ function Game:gamepadaxis(joystick, axis, value)
|
||||
Input:gamepadaxis(joystick, axis, value)
|
||||
end
|
||||
|
||||
-- Window lost focus or got minimized: any release event due while it was
|
||||
-- unfocused/hidden can be swallowed by the OS instead of delivered here,
|
||||
-- which would otherwise leave a held direction stuck on.
|
||||
-- Window focus/visibility flips: a release due while unfocused/hidden can
|
||||
-- be swallowed by the OS. Reset on both edges -- gaining focus with a
|
||||
-- physically held key won't re-fire keypressed, so trusting leftover
|
||||
-- state is worse than asking the player to re-press.
|
||||
function Game:focus(f)
|
||||
if f then return end
|
||||
Input:reset()
|
||||
TouchInput:reset()
|
||||
end
|
||||
|
||||
function Game:visible(v)
|
||||
if v then return end
|
||||
Input:reset()
|
||||
TouchInput:reset()
|
||||
end
|
||||
@@ -369,6 +368,7 @@ end
|
||||
-- flags it owned.
|
||||
function Game:joystickremoved(joystick)
|
||||
Input:reset()
|
||||
TouchInput:reset()
|
||||
end
|
||||
|
||||
function Game:touchpressed(id, x, y)
|
||||
@@ -433,6 +433,7 @@ function Game:applyOptions(opts)
|
||||
require("src.render.Tilt").applyOptions(opts)
|
||||
require("src.render.GBCFX").applyOptions(opts)
|
||||
require("src.core.VideoMode").applyOptions(opts)
|
||||
Input:applyBindings(opts.bindings)
|
||||
end
|
||||
|
||||
function Game:restoreSave(loaded, recovered)
|
||||
|
||||
+93
-16
@@ -3,7 +3,7 @@
|
||||
|
||||
local Input = {}
|
||||
|
||||
local BINDINGS = {
|
||||
local DEFAULT_BINDINGS = {
|
||||
up = "up", w = "up",
|
||||
down = "down", s = "down",
|
||||
left = "left", a = "left",
|
||||
@@ -18,8 +18,11 @@ local BINDINGS = {
|
||||
-- Escape = start for desktop friendliness.
|
||||
|
||||
-- LÖVE's standard gamepad mapping (SDL game controller DB), consistent
|
||||
-- across Xbox/PlayStation/generic controllers on desktop and mobile.
|
||||
local GAMEPAD_BINDINGS = {
|
||||
-- across Xbox/PlayStation/generic controllers on desktop and mobile. Some
|
||||
-- third-party pads report their own SDL mapping for a given physical
|
||||
-- button (e.g. Select/Back/View on off-brand XInput pads), which is what
|
||||
-- src/ui/BindingsMenu.lua's rebinding is for -- see applyBindings below.
|
||||
local DEFAULT_GAMEPAD_BINDINGS = {
|
||||
dpup = "up", dpdown = "down", dpleft = "left", dpright = "right",
|
||||
a = "a", b = "b",
|
||||
start = "start", back = "select",
|
||||
@@ -32,9 +35,33 @@ local STICK_ON = 0.5
|
||||
local STICK_OFF = 0.3
|
||||
|
||||
function Input:init()
|
||||
self:applyBindings(nil)
|
||||
self:reset()
|
||||
end
|
||||
|
||||
-- Layers a player's rebind choices (save.options.bindings, written by
|
||||
-- src/ui/BindingsMenu.lua) on top of the defaults above. A rebind adds an
|
||||
-- extra way to trigger that action instead of replacing the default key,
|
||||
-- so e.g. Z/Enter/Space all still press A even after binding a 4th key to
|
||||
-- it. Call whenever options load or change (see Game:applyOptions and
|
||||
-- BindingsMenu:storeBinding) -- without this the menu records a choice
|
||||
-- that never actually reaches gameplay.
|
||||
function Input:applyBindings(overlay)
|
||||
local keys, pads = {}, {}
|
||||
for key, action in pairs(DEFAULT_BINDINGS) do keys[key] = action end
|
||||
for button, action in pairs(DEFAULT_GAMEPAD_BINDINGS) do pads[button] = action end
|
||||
for actionId, binding in pairs(overlay or {}) do
|
||||
if type(binding) == "table" then
|
||||
if binding.key then keys[binding.key] = actionId end
|
||||
if binding.pad then pads[binding.pad] = actionId end
|
||||
elseif type(binding) == "string" then
|
||||
keys[binding] = actionId
|
||||
end
|
||||
end
|
||||
self.keyBindings = keys
|
||||
self.padBindings = pads
|
||||
end
|
||||
|
||||
-- Purely event-driven state (press sets true, release sets false) has no
|
||||
-- fallback if a release event never arrives -- focus loss, a minimized
|
||||
-- window, or a disconnected gamepad can all swallow the key-up/button-up
|
||||
@@ -44,45 +71,95 @@ function Input:reset()
|
||||
self.state = {}
|
||||
self.pressQueue = {}
|
||||
self.pressed = {}
|
||||
self.sources = {}
|
||||
self.stickAxis = { x = 0, y = 0 }
|
||||
self.stickDir = nil
|
||||
end
|
||||
|
||||
function Input:keypressed(key)
|
||||
local btn = BINDINGS[key]
|
||||
if btn then
|
||||
-- Multiple physical sources (W + Up, d-pad + stick, etc.) can claim the
|
||||
-- same GB button. Track them individually so releasing one doesn't clear
|
||||
-- a hold another source still owns, and so a press+release that both land
|
||||
-- before the next FixedStep can't be revived when step() drains the queue.
|
||||
local function press(self, btn, source)
|
||||
local sources = self.sources[btn]
|
||||
if not sources then
|
||||
sources = {}
|
||||
self.sources[btn] = sources
|
||||
end
|
||||
if not sources[source] then
|
||||
sources[source] = true
|
||||
table.insert(self.pressQueue, btn)
|
||||
end
|
||||
self.state[btn] = true
|
||||
end
|
||||
|
||||
function Input:keyreleased(key)
|
||||
local btn = BINDINGS[key]
|
||||
if btn then
|
||||
local function release(self, btn, source)
|
||||
local sources = self.sources[btn]
|
||||
if sources then
|
||||
sources[source] = nil
|
||||
if next(sources) == nil then
|
||||
-- Leave an empty table (not nil) so step() can tell a real
|
||||
-- source was released before the queue drained, versus a
|
||||
-- synthetic pressQueue inject that never had sources at all.
|
||||
self.state[btn] = false
|
||||
end
|
||||
else
|
||||
self.state[btn] = false
|
||||
end
|
||||
end
|
||||
|
||||
function Input:keypressed(key)
|
||||
local btn = self.keyBindings[key]
|
||||
if btn then
|
||||
press(self, btn, "key:" .. key)
|
||||
end
|
||||
end
|
||||
|
||||
function Input:keyreleased(key)
|
||||
local btn = self.keyBindings[key]
|
||||
if btn then
|
||||
release(self, btn, "key:" .. key)
|
||||
end
|
||||
end
|
||||
|
||||
-- Called once per fixed step: promote queued presses to this step's edges.
|
||||
-- Hold state is owned by live sources (updated in press/release), not
|
||||
-- re-asserted here -- otherwise a same-frame press→release leaves the
|
||||
-- button stuck on after the queue drains.
|
||||
-- Synthetic injects (tests/drivers writing pressQueue directly, with no
|
||||
-- source entry) still set state so scripted holds keep working.
|
||||
function Input:step()
|
||||
self.pressed = {}
|
||||
for _, btn in ipairs(self.pressQueue) do
|
||||
self.pressed[btn] = true
|
||||
self.state[btn] = true
|
||||
local sources = self.sources[btn]
|
||||
if sources == nil then
|
||||
-- synthetic pressQueue inject (tests/drivers): no live source map
|
||||
self.state[btn] = true
|
||||
elseif next(sources) ~= nil then
|
||||
self.state[btn] = true
|
||||
end
|
||||
-- sources == {}: real press fully released before this step — keep up
|
||||
end
|
||||
for btn, sources in pairs(self.sources) do
|
||||
if next(sources) == nil then
|
||||
self.sources[btn] = nil
|
||||
end
|
||||
end
|
||||
self.pressQueue = {}
|
||||
end
|
||||
|
||||
function Input:gamepadpressed(joystick, button)
|
||||
local btn = GAMEPAD_BINDINGS[button]
|
||||
local btn = self.padBindings[button]
|
||||
if btn then
|
||||
table.insert(self.pressQueue, btn)
|
||||
press(self, btn, "pad:" .. button)
|
||||
end
|
||||
end
|
||||
|
||||
function Input:gamepadreleased(joystick, button)
|
||||
local btn = GAMEPAD_BINDINGS[button]
|
||||
local btn = self.padBindings[button]
|
||||
if btn then
|
||||
self.state[btn] = false
|
||||
release(self, btn, "pad:" .. button)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -112,10 +189,10 @@ function Input:gamepadaxis(joystick, axis, value)
|
||||
|
||||
if newDir ~= self.stickDir then
|
||||
if self.stickDir then
|
||||
self.state[self.stickDir] = false
|
||||
release(self, self.stickDir, "stick")
|
||||
end
|
||||
if newDir then
|
||||
table.insert(self.pressQueue, newDir)
|
||||
press(self, newDir, "stick")
|
||||
end
|
||||
self.stickDir = newDir
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user