Update
Rewritten back to Lua. Using more beautiful classes. Add Arabic (lol idk why).
This commit is contained in:
parent
5a54bc1032
commit
ed6598b145
41 changed files with 762 additions and 505 deletions
2
etc/api
2
etc/api
|
@ -1 +1 @@
|
|||
Subproject commit da0a266b72f0919c5ed1bfa3fbc75e7d2f398229
|
||||
Subproject commit faab5a7e6d0dc684d04cf5121b9735b786716aca
|
116
etc/class.lua
Normal file
116
etc/class.lua
Normal file
|
@ -0,0 +1,116 @@
|
|||
-- Class library
|
||||
-- (c) Er2 2022 <er2@dismail.de>
|
||||
-- Zlib License
|
||||
|
||||
-- (Not) Virtual classes table
|
||||
local vtable = {}
|
||||
|
||||
-- Metamethods
|
||||
local cls = {}
|
||||
cls.__index = cls
|
||||
|
||||
-- :__tostring() - Calls when using Lua tostring() or error occurs.
|
||||
function cls:__tostring()
|
||||
local str = 'Class <'.. self.__name ..'>'
|
||||
while self.__super do
|
||||
str = str.. ' inherits '.. self.__super.__name
|
||||
self = self.__super
|
||||
end
|
||||
return str
|
||||
end
|
||||
|
||||
-- :__call(table) - Calls when defining class body
|
||||
function cls:__call(t)
|
||||
for k, v in pairs(t) do
|
||||
self[k] = v
|
||||
end
|
||||
if self.__init
|
||||
then self:__init()
|
||||
end
|
||||
end
|
||||
|
||||
-- :new(...) - Creates new instance of class. Please use new(...) function instead.
|
||||
-- Can throw error if no constructor defined in class or subclasses.
|
||||
function cls:new(...)
|
||||
local cl = self
|
||||
local cons
|
||||
repeat cons = rawget(cl, 1) or rawget(cl, 'new')
|
||||
cl = cl.__super
|
||||
until cons or not cl
|
||||
assert(cons, 'No constructor found in class')
|
||||
|
||||
--self.__index = self.__index or cls.__index
|
||||
self = setmetatable({}, self)
|
||||
cons(self, ...)
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
-- :super(...) - Call parent constructor.
|
||||
function cls:super(...)
|
||||
return cls.superM(self, 1, ...)
|
||||
or cls.superM(self, 'new', ...)
|
||||
end
|
||||
|
||||
-- :superM(method, ...) - Call method from parent class.
|
||||
-- Can throw error if class is not inherited.
|
||||
function cls:superM(meth, ...)
|
||||
local cl = self.__super
|
||||
local fn
|
||||
while not fn and cl do
|
||||
fn = cl[meth]
|
||||
if cl.__sup then fn = nil end
|
||||
cl = cl.__super
|
||||
end
|
||||
if not fn then return nil end
|
||||
|
||||
if cl then cl.__sup = true end
|
||||
local v = {fn(self, ...)}
|
||||
if cl then cl.__sup = nil end
|
||||
return table.unpack(v)
|
||||
end
|
||||
|
||||
-- :clone() - Make clone of class.
|
||||
function cls:clone()
|
||||
local cl = {}
|
||||
for k, v in pairs(cls)
|
||||
do cl[k] = v end
|
||||
for k, v in pairs(self)
|
||||
do cl[k] = v end
|
||||
cl.__index = cl
|
||||
cl.__super = self
|
||||
return setmetatable(cl, self)
|
||||
end
|
||||
|
||||
-- :inherits(className) - Make this class inherits from another.
|
||||
-- Can throw error if class is already inherited.
|
||||
function cls:inherits(name)
|
||||
-- assert(self.__super, 'no')
|
||||
local cl = vtable[name]
|
||||
assert(cl, 'Class is not exists')
|
||||
return setmetatable(self, cl:clone())
|
||||
end
|
||||
|
||||
-- :extends(className) - Same as :inherits(className).
|
||||
cls.extends = cls.inherits
|
||||
|
||||
-- class(className) - Make new class.
|
||||
-- Can throw error if class already exists with this name.
|
||||
function class(name)
|
||||
assert(not vtable[name], 'Cannot override defined class')
|
||||
local cl = setmetatable({__name = name, __tostring = cls.__tostring}, cls)
|
||||
cl.__index = cl
|
||||
vtable[name] = cl
|
||||
return cl
|
||||
end
|
||||
|
||||
-- new(className) - Make new instance of class. Use this instead of :new(class).
|
||||
-- Can throw error if class by name was not found.
|
||||
function new(name)
|
||||
local cl = vtable[name]
|
||||
assert(cl, 'No class found')
|
||||
return function(...)
|
||||
return cls.new(cl, ...)
|
||||
end
|
||||
end
|
||||
|
109
etc/events.lua
109
etc/events.lua
|
@ -1,51 +1,74 @@
|
|||
--[[ Events library
|
||||
-- (c) Er2 2021 <er2@dismail.de>
|
||||
-- Zlib License
|
||||
--]]
|
||||
-- Events library
|
||||
-- (c) Er2 2022 <er2@dismail.de>
|
||||
-- Zlib License
|
||||
|
||||
local events = {}
|
||||
events.__index = events
|
||||
require 'class'
|
||||
|
||||
function events:_add(t, n, f)
|
||||
table.insert(self._ev_, {
|
||||
type = t,
|
||||
name = n,
|
||||
fn = f,
|
||||
})
|
||||
end
|
||||
class 'Events' {
|
||||
function(this)
|
||||
this._ev_ = {}
|
||||
end,
|
||||
|
||||
function events:on(n,f) self:_add('on', n,f) end
|
||||
function events:once(n,f) self:_add('once', n,f) end
|
||||
_add = function(this, type, name, func)
|
||||
table.insert(this._ev_, {
|
||||
type = tostring(type),
|
||||
name = tostring(name),
|
||||
func = func,
|
||||
})
|
||||
end,
|
||||
|
||||
function events:off(f)
|
||||
for k, v in pairs(self._ev_) do
|
||||
if v.fn == f then
|
||||
table.remove(self._ev_, k)
|
||||
on = function(this, name, func)
|
||||
this:_add('on', name, func)
|
||||
end,
|
||||
|
||||
once = function(this, name, func)
|
||||
this:_add('once', name, func)
|
||||
end,
|
||||
|
||||
off = function(this, func)
|
||||
for k, v in pairs(this._ev_) do
|
||||
if v.func == func
|
||||
then table.remove(this._ev_, k)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end,
|
||||
|
||||
function events:_ev(t, i, name, ...)
|
||||
local v = t[i]
|
||||
if v.name == name then
|
||||
v.fn(...)
|
||||
if v.type == 'once' then table.remove(t, i) end
|
||||
end
|
||||
end
|
||||
_ev = function(this, t, i, name, ...)
|
||||
local v = t[i]
|
||||
if v.name == name then
|
||||
v.func(...)
|
||||
if v.type == 'once'
|
||||
then table.remove(t, i)
|
||||
end
|
||||
end
|
||||
end,
|
||||
|
||||
function events:emit(name, ...)
|
||||
local t = self._ev_
|
||||
for i = 1, #t do
|
||||
local v = t[i] or {}
|
||||
if type(v) == 'table'
|
||||
and type(v.name) == 'string'
|
||||
and type(v.type) == 'string'
|
||||
and type(v.fn) == 'function'
|
||||
then self:_ev(t, i, name, ...) end
|
||||
end
|
||||
end
|
||||
emit = function(this, name, ...)
|
||||
local t = this._ev_
|
||||
for i = 1, #t do
|
||||
local v = t[i]
|
||||
if type(v) == 'table'
|
||||
and type(v.name) == 'string'
|
||||
and type(v.type) == 'string'
|
||||
and type(v.func) == 'function'
|
||||
then this:_ev(t, i, name, ...)
|
||||
else print 'Invalid event'
|
||||
if v then print(v, v.name, v.type, v.func)
|
||||
else print 'nil' end
|
||||
end
|
||||
end
|
||||
end,
|
||||
}
|
||||
|
||||
class 'EventsThis' : inherits 'Events' {
|
||||
_ev = function(this, t, i, name, ...)
|
||||
local v = t[i]
|
||||
if v.name == name then
|
||||
v.func(this, ...)
|
||||
if v.type == 'once'
|
||||
then table.remove(t, i)
|
||||
end
|
||||
end
|
||||
end,
|
||||
}
|
||||
|
||||
return function(t)
|
||||
t._ev_ = {}
|
||||
return setmetatable(t, events)
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue