Advertisement
Bolodefchoco_LUAXML

[Class] Callback

Oct 4th, 2018
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.00 KB | None | 0 0
  1. local callback
  2. do
  3.     local callback_id = 0
  4.     callback = setmetatable({ }, {
  5.         __call = function(this, eventName, x, y, w, h, hidden)
  6.             local id = callback_id
  7.             local action
  8.             local hasClickableRange, range = false, 0
  9.            
  10.             local fixed = false
  11.             local textarea = function(playerName)
  12.                 ui.addTextArea(id, "<textformat leftmargin='1' rightmargin='1'><a href='event:callback." .. eventName .. "'>" .. string.rep('\n', h / 10), playerName, x - 5, y - 5, w + 5, h + 5, 1, 1, 0, fixed)
  13.             end
  14.  
  15.             local self = {
  16.                 callback = eventName,
  17.                 x = x,
  18.                 y = y,
  19.                 w = w,
  20.                 h = h
  21.             }
  22.  
  23.             self.displayCallback = function(self, f)
  24.                 if type(f) == "string" then
  25.                     return textarea(f)
  26.                 end
  27.  
  28.                 for k, v in next, tfm.get.room.playerList do
  29.                     if not f or f(k, v) then
  30.                         textarea(k)
  31.                     end
  32.                 end
  33.             end
  34.  
  35.             self.fixed = function(self)
  36.                 fixed = true
  37.                 return self
  38.             end
  39.  
  40.             self.inClickableRange = function(self, playerName)
  41.                 if hasClickableRange then
  42.                     local x, y = tfm.get.room.playerList[playerName].x, tfm.get.room.playerList[playerName].y
  43.                     return ((x >= (self.x - range) and x <= (self.x + self.w + range)) and (y >= (self.y - range) and y <= (self.y + self.h + range)))
  44.                 end
  45.                 return true
  46.             end
  47.  
  48.             self.performAction = function(self, ...)
  49.                 if action then
  50.                     action(self, ...)
  51.                     return self, true
  52.                 end
  53.                 return self, false
  54.             end
  55.  
  56.             self.remove = function(self, playerName)
  57.                 ui.removeTextArea(id, playerName)
  58.  
  59.                 if not playerName then
  60.                     this[eventName] = nil
  61.                 end
  62.                 return true
  63.             end
  64.  
  65.             self.setAction = function(self, f)
  66.                 if type(f) == "function" then
  67.                     action = f
  68.                     this[eventName] = self
  69.                     return true
  70.                 end
  71.                 return false
  72.             end
  73.  
  74.             self.setClickableRange = function(self, complement)
  75.                 hasClickableRange, range = true, (complement or 0)
  76.             end
  77.  
  78.             if not hidden then
  79.                 self.displayCallback(self)
  80.             end
  81.  
  82.             callback_id = callback_id + 1
  83.             return self
  84.         end
  85.     })
  86. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement