Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.25 KB | None | 0 0
  1. _VARS = class()
  2. _VARS.storage = {}
  3.  
  4. function _VARS:reg(var)
  5.     assert(is_a(var, _VAR), "Cannot register variable because wrong class was provided.")
  6.     self.storage[var.name] = var
  7.     self:__defineGetter(var.name, function (self, var) return var.curv end, var)
  8.     self:__defineSetter(var.name, function (self, var, val)
  9.         if var:isInReach(val) then
  10.             local oval = var.curv
  11.             var.curv = val
  12.             if var.chng and type(var.chng) == "function" then
  13.                 var:chng(oval, val)
  14.             end
  15.         end
  16.     end, var)
  17. end
  18.  
  19. function _VARS:r(vtype, name, ...)
  20.     self:reg(_G[vtype](name, ...))
  21. end
  22.  
  23. EV = _VARS()
  24.  
  25. _VAR = class()
  26. function _VAR:__init(name, minv, curv, maxv, persist, onchange)
  27.     assert(name, "Cannot register variable: name is missing.")
  28.     assert(curv, "Cannot register variable: no value set.")
  29.     self.name = name
  30.     self.minv = minv
  31.     self.maxv = maxv
  32.     self.curv = curv
  33.     self.pers = persist
  34.     self.chng = onchange
  35. end
  36.  
  37. IVAR = class(_VAR)
  38. function IVAR:__tostring() return "IVAR" end
  39. function IVAR:__init(name, minv, curv, maxv)
  40.     assert(type(minv) == "number" and type(curv) == "number" and type(maxv) == "number", "Wrong value type provided to IVAR.")
  41.     self[_VAR].__user_init(self, name, minv, curv, maxv, false, nil)
  42. end
  43. function IVAR:isInReach(v)
  44.     if type(v) ~= "number" then
  45.         log(ERROR, "Wrong value type passed to variable.")
  46.     end
  47.     if v < self.minv or v > self.maxv then
  48.         log(ERROR, "Variable accepts only values of range " .. self.minv .. " to " .. self.maxv)
  49.         return false
  50.     end
  51.     return true
  52. end
  53.  
  54. IVARP = class(IVAR)
  55. function IVARP:__tostring() return "IVARP" end
  56. function IVARP:__init(name, minv, curv, maxv)
  57.     assert(type(minv) == "number" and type(curv) == "number" and type(maxv) == "number", "Wrong value type provided to IVARP.")
  58.     self[_VAR].__user_init(self, name, minv, curv, maxv, true, nil)
  59. end
  60.  
  61. IVARF = class(IVAR)
  62. function IVARF:__tostring() return "IVARF" end
  63. function IVARF:__init(name, minv, curv, maxv, onchange)
  64.     assert(type(minv) == "number" and type(curv) == "number" and type(maxv) == "number", "Wrong value type provided to IVARF.")
  65.     assert(onchange and type(onchange) == "function", "Wrong type of onchange callback to IVARF.")
  66.     self[_VAR].__user_init(self, name, minv, curv, maxv, false, onchange)
  67. end
  68.  
  69. IVARFP = class(IVARF)
  70. function IVARFP:__tostring() return "IVARFP" end
  71. function IVARFP:__init(name, minv, curv, maxv, onchange)
  72.     assert(type(minv) == "number" and type(curv) == "number" and type(maxv) == "number", "Wrong value type provided to IVARFP.")
  73.     assert(onchange and type(onchange) == "function", "Wrong type of onchange callback to IVARFP.")
  74.     self[_VAR].__user_init(self, name, minv, curv, maxv, true, onchange)
  75. end
  76.  
  77. FVAR = class(IVAR)
  78. function FVAR:__tostring() return "FVAR" end
  79. FVARP = class(IVARP)
  80. function FVARP:__tostring() return "FVARP" end
  81. FVARF = class(IVARF)
  82. function FVARF:__tostring() return "FVARF" end
  83. FVARFP = class(IVARFP)
  84. function FVARFP:__tostring() return "FVARFP" end
  85.  
  86. SVAR = class(_VAR)
  87. function SVAR:__tostring() return "SVAR" end
  88. function SVAR:__init(name, curv)
  89.     assert(type(curv) == "string", "Wrong value type provided to SVAR.")
  90.     self[_VAR].__user_init(self, name, nil, curv, nil, false, nil)
  91. end
  92. function SVAR:isInReach(v)
  93.     if type(v) ~= "string" then
  94.         log(ERROR, "Wrong value type passed to variable.")
  95.         return false
  96.     end
  97.     return true
  98. end
  99.  
  100. SVARP = class(SVAR)
  101. function SVARP:__tostring() return "SVARP" end
  102. function SVARP:__init(name, curv)
  103.     assert(type(curv) == "string", "Wrong value type provided to SVARP.")
  104.     self[_VAR].__user_init(self, name, nil, curv, nil, true, nil)
  105. end
  106.  
  107. SVARF = class(SVAR)
  108. function SVARF:__tostring() return "SVARF" end
  109. function SVARF:__init(name, curv, onchange)
  110.     assert(type(curv) == "string", "Wrong value type provided to SVARF.")
  111.     assert(onchange and type(onchange) == "function", "Wrong type of onchange callback to SVARF.")
  112.     self[_VAR].__user_init(self, name, nil, curv, nil, false, onchange)
  113. end
  114.  
  115. SVARFP = class(SVARF)
  116. function SVARFP:__tostring() return "SVARFP" end
  117. function SVARFP:__init(name, curv, onchange)
  118.     assert(type(curv) == "string", "Wrong value type provided to SVARFP.")
  119.     assert(onchange and type(onchange) == "function", "Wrong type of onchange callback to SVARFP.")
  120.     self[_VAR].__user_init(self, name, nil, curv, nil, true, onchange)
  121. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement