Advertisement
tobast

slot_sig

Jun 15th, 2014
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.72 KB | None | 0 0
  1. --[[
  2.   API slot_sig by Théophile ``Tobast'' Bastian.
  3.   Completely changes the way to program with a slot/signal
  4.   way, Qt-like.
  5.  
  6.   Copyright (C) 2014  BASTIAN Theophile
  7.  
  8.   This program is free software: you can redistribute it and/or modify
  9.   it under the terms of the GNU General Public License as published by
  10.   the Free Software Foundation, either version 3 of the License, or
  11.   at your option) any later version.
  12.  
  13.   This program is distributed in the hope that it will be useful,
  14.   but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.   GNU General Public License for more details.
  17.  
  18.   You should have received a copy of the GNU General Public License
  19.   along with this program.  If not, see <http://www.gnu.org/licenses/gpl.txt>.
  20. --]]
  21.  
  22.  
  23. local slots = {}
  24. local quitRequired = false
  25.  
  26. --- Connects a new slot.
  27. -- @param id signal identifier
  28. -- @param callback slot function to call on signal
  29. function connectSlot(id, callback, obj)
  30.     if (slots[id] == nil) then
  31.         slots[id] = {}
  32.     end
  33.     if obj == nil then
  34.         table.insert(slots[id], callback)
  35.     else
  36.         table.insert(slots[id], function (arg) callback(obj,arg) end)
  37.     end
  38. end
  39.  
  40. --- Fires a signal.
  41. -- @param id signal identifier
  42. -- @param params parameters for the callbacks
  43. function fireSignal(id, params)
  44.     if (slots[id] ~= nil) then
  45.         for _,slot in pairs(slots[id]) do
  46.             slot(params)
  47.         end
  48.     end
  49. end
  50.  
  51. --- Runs the main loop of the program.
  52. function run()
  53.     while not quitRequired do
  54.         a = {}
  55.         evt,a[1],a[2],a[3],a[4],a[5] = os.pullEvent()
  56.         fireSignal(evt, a)
  57.     end
  58. end
  59.  
  60. --- Quits the program as soon as it have processed the current event
  61. function quit()
  62.     quitRequired = true
  63. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement