Files
esp32-lua-api/native/src/bindings/features/buttons.cpp
T
evan b9f7c9347c refactor(api)!: declare callbacks as classes an app composes
The runtime has always called fields on the table main.lua returns, but
@lua-global declared them as loose functions, so the stubs type-checked
something that does not exist and read as "define a global".

Callbacks are now @lua-app blocks that generate a class: App for the core
contract, TouchHandlers and ButtonHandlers beside the namespaces they belong
to. An app composes what it implements:

  ---@class PaintApp : App, TouchHandlers

Names follow the rest of the surface: onTouchDown rather than on_touch_down,
with the field names the runtime looks up renamed to match. @lua-field carries
the plain fields (home, data) that were prose in a preamble before.
2026-08-05 10:42:01 -04:00

89 lines
2.5 KiB
C++

#include "../helpers.h"
namespace esp32lua {
namespace bindings {
namespace {
int getButtons(lua_State* state) {
pushStrings(state, Runtime::from(state)->buttons().buttons());
return 1;
}
int isAnyPressed(lua_State* state) {
lua_pushboolean(state, Runtime::from(state)->buttons().isAnyPressed());
return 1;
}
int isPressed(lua_State* state) {
const std::string button = checkString(state, 1);
lua_pushboolean(state, Runtime::from(state)->buttons().isPressed(button));
return 1;
}
int wasPressed(lua_State* state) {
const std::string button = checkString(state, 1);
lua_pushboolean(state, Runtime::from(state)->buttons().wasPressed(button));
return 1;
}
int wasReleased(lua_State* state) {
const std::string button = checkString(state, 1);
lua_pushboolean(state, Runtime::from(state)->buttons().wasReleased(button));
return 1;
}
// @lua-module buttons ButtonsLib
// @lua-preamble ---@alias Button "up"|"down"|"left"|"right"|"confirm"|"back"
// @lua-preamble
// @lua-preamble -- Roles, not physical buttons: a device maps whatever hardware
// it has onto them, and
// @lua-preamble -- up/down/left/right are the directions tree.moveFocus already
// takes.
const luaL_Reg FUNCTIONS[] = {
// ---Returns the roles this device reports, so an app can label only the
// actions it has.
// @return Button[]
{"getAll", getButtons},
// ---Whether any button is held.
// @return boolean
{"isAnyPressed", isAnyPressed},
// ---Whether a button is held.
// @param button Button
// @return boolean
{"isPressed", isPressed},
// ---Whether a button went down since the last poll.
// @param button Button
// @return boolean
{"wasPressed", wasPressed},
// ---Whether a button came up since the last poll.
// @param button Button
// @return boolean
{"wasReleased", wasReleased},
{nullptr, nullptr},
};
} // namespace
void registerButtons(lua_State* state) {
luaL_newlib(state, FUNCTIONS);
lua_setglobal(state, "buttons");
}
} // namespace bindings
} // namespace esp32lua
// @lua-app ButtonHandlers
// @lua-preamble -- What an app implements to see buttons, composed into its own
// class:
// @lua-preamble --
// @lua-preamble -- ---@class MenuApp : App, ButtonHandlers
// ---Fired when a button goes down.
// @param button Button
// @lua-fn onButtonDown?
// ---Fired when a button comes up.
// @param button Button
// @lua-fn onButtonUp?
// ---Tap alias, fired on release like a click, after onButtonUp.
// @param button Button
// @lua-fn onButton?