Page 1 of 1

Action Handler

Posted: 13 Mar 2007, 00:21
by trepan
Before there are too many widgets written with hardcoded keys (a bad thing),
I'll be committing my action handler. This makes it much easier for widgets to
register actions that they wish to process. Widget's can also register new bindings
by using the SendCommands({"bind ..."}) interface.

Here's some example code (that would go into a widget).

Code: Select all

-- test function to show action callback data

local function actionPrint(cmd, optLine, optWords, data, isRepeat, release)
  local msg = ''
  if (release == nil) then
    msg = "TEXT:    "   
  elseif (release) then 
    msg = "RELEASE: "   
  elseif (isRepeat) then
    msg = "REPEAT:  "
  else
    msg = "PRESS:   "
  end  
  msg = msg .. cmd .. " " .. optLine .. " (" .. tostring(data) .. ")"
  print(msg)
  return true  --  take the event
end

-- NOTE: the action hooks can only be installed after the widget is fully initialized
-- it's easy to wrap all of the bindings into a call like this and call it in widget:Initialize()

function InstallActions()
  -- type codes:
  -- t: text   (/luaui ...)
  -- p: key press
  -- R: key repeat
  -- r: key release

  -- default types for AddAction() are "tp"
  -- default types for RemoveAction() are "tpRr"  (all)

  -- the 3rd parameter to AddAction() is a userData that is passed to the callback

  -- AddAction() and RemoveAction() return 4 bools:
  --   text, keyPress, keyRepeat, keyRelease    (set it successful)

  print(widgetHandler:AddAction("specteam", actionPrint, 123, "t"))   
  print(widgetHandler:AddAction("specteam", actionPrint, 234, "p"))   
  print(widgetHandler:AddAction("specteam", actionPrint, 345, "r"))   
  print(widgetHandler:AddAction("specteam", actionPrint, 456, "R"))   
  print(widgetHandler:AddAction("attack",   actionPrint, 999, "tp"))   
  print(widgetHandler:AddAction("buildunit_armaap", actionPrint, 567))
  print(widgetHandler:RemoveAction("specteam", "tR"))
end
P.S. This is based on the 0.74b3 LuaUI API, which has all the necessary calls.