Skip to content

Instantly share code, notes, and snippets.

@kyzentun
Created September 3, 2015 23:36
Show Gist options
  • Save kyzentun/1a55689d3d193443b06f to your computer and use it in GitHub Desktop.
Save kyzentun/1a55689d3d193443b06f to your computer and use it in GitHub Desktop.
-- This simple example looks at input events to set text on the screen
-- showing one button that is being pressed. Because it doesn't use a list
-- of text actors, if multiple buttons are pressed it will only show info for
-- the last one.
-- The Config Key/Joy screen lists all buttons in the current game mode.
-- button_text and game_button_text will be set to actors when those actors
-- are created. These local variables exist to skip the hassle of calling
-- GetChild.
local button_text= false
local game_button_text= false
-- button_text will be used to show the button field from the input event,
-- and game_button_text will be used for the GameButton field. These two
-- fields can be different because Stepmania has the OnlyDedicatedMenuButtons
-- preference. That preference is intended to make it so that only the menu
-- buttons (MenuLeft, MenuRight, MenuUp, MenuDown, commonly only MenuLeft and
-- MenuRight on cabinets) can be used in menus. If OnlyDedicatedMenuButtons
-- is false, then Left will be translated to MenuLeft, and the GameButton
-- field will be set to MenuLeft when Left on the pad is pressed.
-- This digression about menu button translation really only matters to
-- themers trying to make a good menu control scheme. For a simfile played
-- on a cabinet, GameButton will usually be nil, because most cabinets are
-- set not to map the pads to the menu buttons.
-- Since both text actors will have their text formatted the same way, it's
-- useful to make a wrapper function to avoid duplicating the formatting.
-- When the formatting changes, say changing spaces to colons, only
-- set_button_text needs to change.
local function set_button_text(text_actor, pn, button, press_type)
text_actor:settext(
ToEnumShortString(pn) .. " " .. tostring(button) .. " " .. press_type)
end
-- Lua.xml lists all the parts of the event table.
local function input(event)
-- If a button that is not mapped to any player is pressed, the
-- PlayerNumber field will be nil. Otherwise, it will be PLAYER_1 or
-- PLAYER_2.
if not event.PlayerNumber then return end
-- The delay between FirstPress and Repeat is set by the RepeatDelay metric for the screen.
-- The number of repeats per second is set by the RepeatRate metric.
local press_type= ToEnumShortString(event.type)
set_button_text(button_text, event.PlayerNumber, event.button, press_type)
set_button_text(game_button_text, event.PlayerNumber, event.GameButton, press_type)
end
return Def.ActorFrame{
-- The sleeper actor so this can be used in a bgchange lua file and the
-- file won't be unloaded. Don't do this in a screen file in a theme.
Def.Actor{InitCommand= function(self) self:hibernate(1000) end},
-- AddInputCallback cannot be in an InitCommand. InitCommands happen
-- before the screen is the top screen, so calling GetTopScreen during an
-- InitCommand would fetch the screen that is about to be destroyed.
OnCommand= function(self)
SCREENMAN:GetTopScreen():AddInputCallback(input)
end,
Def.BitmapText{
Font= "Common Normal", InitCommand= function(self)
-- Since button_text is set inside an InitCommand, it occurs before the
-- OnCommand that adds the input callback, so the input callback
-- doesn't need to worry about whether button_text is initialized.
button_text= self
-- Lua is whitespace insensitive, so sticking a line break before a
-- colon in the middle of a chain of functions is valid.
self:xy(_screen.cx, _screen.cy-12)
-- Colors are just tables of four numbers. Using html strings and the
-- color function would also work.
:DiffuseAndStroke({.75, .75, 0, 1}, {0, 0, 0, 1})
end
},
Def.BitmapText{
Font= "Common Normal", InitCommand= function(self)
game_button_text= self
self:xy(_screen.cx, _screen.cy+12)
:DiffuseAndStroke({.75, 0, .75, 1}, {0, 0, 0, 1})
end
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment