mirror of
https://github.com/Insality/druid.git
synced 2025-06-27 10:27:47 +02:00
Get rid of middleclass
This commit is contained in:
parent
54345b924b
commit
067b650838
@ -19,10 +19,9 @@
|
||||
-- @alias druid.base_component
|
||||
|
||||
local const = require("druid.const")
|
||||
local class = require("druid.system.middleclass")
|
||||
local helper = require("druid.helper")
|
||||
|
||||
local BaseComponent = class("druid.component")
|
||||
local BaseComponent = {}
|
||||
|
||||
local INTERESTS = {} -- Cache interests per component class in runtime
|
||||
local IS_AUTO_TEMPLATE = not (sys.get_config_int("druid.no_auto_template", 0) == "1")
|
||||
@ -355,24 +354,6 @@ function BaseComponent.setup_component(self, druid_instance, context, style, ins
|
||||
end
|
||||
|
||||
|
||||
--- Basic constructor of component. It will call automaticaly
|
||||
-- by `BaseComponent.create`
|
||||
-- @tparam BaseComponent self @{BaseComponent}
|
||||
-- @tparam string name BaseComponent name
|
||||
-- @tparam number|nil input_priority The input priority. The bigger number processed first
|
||||
-- @local
|
||||
function BaseComponent.initialize(self, name, input_priority)
|
||||
self._component = {
|
||||
name = name,
|
||||
input_priority = input_priority or const.PRIORITY_INPUT,
|
||||
default_input_priority = input_priority or const.PRIORITY_INPUT,
|
||||
is_debug = false,
|
||||
_is_input_priority_changed = true, -- Default true for sort once time after GUI init
|
||||
_uid = BaseComponent.create_uid()
|
||||
}
|
||||
end
|
||||
|
||||
|
||||
--- Print log information if debug mode is enabled
|
||||
-- @tparam BaseComponent self @{BaseComponent}
|
||||
-- @tparam string message
|
||||
@ -499,12 +480,24 @@ end
|
||||
-- @tparam number|nil input_priority The input priority. The bigger number processed first
|
||||
-- @local
|
||||
function BaseComponent.create(name, input_priority)
|
||||
-- Yea, inheritance here
|
||||
local new_class = class(name, BaseComponent)
|
||||
|
||||
new_class.initialize = function(self)
|
||||
BaseComponent.initialize(self, name, input_priority)
|
||||
local new_class = setmetatable({}, {
|
||||
__index = BaseComponent,
|
||||
__call = function(cls, ...)
|
||||
local self = setmetatable({
|
||||
_component = {
|
||||
name = name,
|
||||
input_priority = input_priority or const.PRIORITY_INPUT,
|
||||
default_input_priority = input_priority or const.PRIORITY_INPUT,
|
||||
is_debug = false,
|
||||
_is_input_priority_changed = true, -- Default true for sort once time after GUI init
|
||||
_uid = BaseComponent.create_uid()
|
||||
}
|
||||
}, {
|
||||
__index = cls
|
||||
})
|
||||
return self
|
||||
end
|
||||
})
|
||||
|
||||
return new_class
|
||||
end
|
||||
|
@ -107,7 +107,9 @@ function M.new(context, style)
|
||||
M.set_default_style(default_style)
|
||||
end
|
||||
|
||||
local new_instance = druid_instance(context, style)
|
||||
local new_instance = setmetatable({}, { __index = druid_instance })
|
||||
new_instance:initialize(context, style)
|
||||
|
||||
table.insert(_instances, new_instance)
|
||||
return new_instance
|
||||
end
|
||||
|
@ -7,8 +7,11 @@
|
||||
-- @within BaseComponent
|
||||
-- @alias druid.hotkey
|
||||
|
||||
--- On change state callback(self, state)
|
||||
-- @tfield DruidEvent on_change_state @{DruidEvent}
|
||||
--- On hotkey released callback(self, argument)
|
||||
-- @tfield DruidEvent on_hotkey_pressed @{DruidEvent}
|
||||
|
||||
--- On hotkey released callback(self, argument)
|
||||
-- @tfield DruidEvent on_hotkey_released @{DruidEvent}
|
||||
|
||||
--- Visual node
|
||||
-- @tfield node node
|
||||
|
@ -68,7 +68,6 @@
|
||||
-- @see Timer
|
||||
|
||||
local helper = require("druid.helper")
|
||||
local class = require("druid.system.middleclass")
|
||||
local settings = require("druid.system.settings")
|
||||
local base_component = require("druid.component")
|
||||
|
||||
@ -94,7 +93,7 @@ local back_handler = require("druid.base.back_handler")
|
||||
-- local dynamic_grid = require("druid.extended.dynamic_grid")
|
||||
-- local checkbox_group = require("druid.extended.checkbox_group")
|
||||
|
||||
local DruidInstance = class("druid.druid_instance")
|
||||
local DruidInstance = {}
|
||||
|
||||
local MSG_ADD_FOCUS = hash("acquire_input_focus")
|
||||
local MSG_REMOVE_FOCUS = hash("release_input_focus")
|
||||
@ -479,7 +478,7 @@ end
|
||||
-- @tparam table|BaseComponent|nil whitelist_components The array of component to whitelist
|
||||
-- @treturn self @{DruidInstance}
|
||||
function DruidInstance.set_whitelist(self, whitelist_components)
|
||||
if whitelist_components and whitelist_components.isInstanceOf then
|
||||
if whitelist_components and whitelist_components._component then
|
||||
whitelist_components = { whitelist_components }
|
||||
end
|
||||
|
||||
@ -501,7 +500,7 @@ end
|
||||
-- @tparam table|BaseComponent|nil blacklist_components The array of component to blacklist
|
||||
-- @treturn self @{DruidInstance}
|
||||
function DruidInstance.set_blacklist(self, blacklist_components)
|
||||
if blacklist_components and blacklist_components.isInstanceOf then
|
||||
if blacklist_components and blacklist_components._component then
|
||||
blacklist_components = { blacklist_components }
|
||||
end
|
||||
|
||||
|
@ -1,156 +0,0 @@
|
||||
-- Source: https://github.com/kikito/middleclass
|
||||
local middleclass = {}
|
||||
|
||||
local function _createIndexWrapper(aClass, f)
|
||||
if f == nil then
|
||||
return aClass.__instanceDict
|
||||
else
|
||||
return function(self, name)
|
||||
local value = aClass.__instanceDict[name]
|
||||
|
||||
if value ~= nil then
|
||||
return value
|
||||
elseif type(f) == "function" then
|
||||
return (f(self, name))
|
||||
else
|
||||
return f[name]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function _propagateInstanceMethod(aClass, name, f)
|
||||
f = name == "__index" and _createIndexWrapper(aClass, f) or f
|
||||
aClass.__instanceDict[name] = f
|
||||
|
||||
for subclass in pairs(aClass.subclasses) do
|
||||
if rawget(subclass.__declaredMethods, name) == nil then
|
||||
_propagateInstanceMethod(subclass, name, f)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function _declareInstanceMethod(aClass, name, f)
|
||||
aClass.__declaredMethods[name] = f
|
||||
|
||||
if f == nil and aClass.super then
|
||||
f = aClass.super.__instanceDict[name]
|
||||
end
|
||||
|
||||
_propagateInstanceMethod(aClass, name, f)
|
||||
end
|
||||
|
||||
local function _tostring(self) return "class " .. self.name end
|
||||
local function _call(self, ...) return self:instantiate(...) end
|
||||
|
||||
local function _createClass(name, super)
|
||||
local dict = {}
|
||||
dict.__index = dict
|
||||
|
||||
local aClass = { name = name, super = super, static = {},
|
||||
__instanceDict = dict, __declaredMethods = {},
|
||||
subclasses = setmetatable({}, {__mode='k'}) }
|
||||
|
||||
if super then
|
||||
setmetatable(aClass.static, {
|
||||
__index = function(_,k)
|
||||
local result = rawget(dict,k)
|
||||
if result == nil then
|
||||
return super.static[k]
|
||||
end
|
||||
return result
|
||||
end
|
||||
})
|
||||
else
|
||||
setmetatable(aClass.static, { __index = function(_,k) return rawget(dict,k) end })
|
||||
end
|
||||
|
||||
setmetatable(aClass, { __index = aClass.static, __tostring = _tostring,
|
||||
__call = _call, __newindex = _declareInstanceMethod })
|
||||
|
||||
return aClass
|
||||
end
|
||||
|
||||
local function _includeMixin(aClass, mixin)
|
||||
assert(type(mixin) == 'table', "mixin must be a table")
|
||||
|
||||
for name,method in pairs(mixin) do
|
||||
if name ~= "included" and name ~= "static" then aClass[name] = method end
|
||||
end
|
||||
|
||||
for name,method in pairs(mixin.static or {}) do
|
||||
aClass.static[name] = method
|
||||
end
|
||||
|
||||
if type(mixin.included)=="function" then mixin:included(aClass) end
|
||||
return aClass
|
||||
end
|
||||
|
||||
local DefaultMixin = {
|
||||
__tostring = function(self) return "instance of " .. tostring(self.class) end,
|
||||
|
||||
initialize = function(self, ...) end,
|
||||
|
||||
isInstanceOf = function(self, aClass)
|
||||
return type(aClass) == 'table'
|
||||
and type(self) == 'table'
|
||||
and (self.class == aClass
|
||||
or type(self.class) == 'table'
|
||||
and type(self.class.isSubclassOf) == 'function'
|
||||
and self.class:isSubclassOf(aClass))
|
||||
end,
|
||||
|
||||
static = {
|
||||
allocate = function(self)
|
||||
assert(type(self) == 'table', "Make sure that you are using 'Class:allocate' instead of 'Class.allocate'")
|
||||
return setmetatable({ class = self }, self.__instanceDict)
|
||||
end,
|
||||
|
||||
instantiate = function(self, ...)
|
||||
assert(type(self) == 'table', "Make sure that you are using 'Class:instantiate' instead of 'Class.instantiate'")
|
||||
local instance = self:allocate()
|
||||
instance:initialize(...)
|
||||
return instance
|
||||
end,
|
||||
|
||||
subclass = function(self, name)
|
||||
assert(type(self) == 'table', "Make sure that you are using 'Class:subclass' instead of 'Class.subclass'")
|
||||
assert(type(name) == "string", "You must provide a name(string) for your class")
|
||||
|
||||
local subclass = _createClass(name, self)
|
||||
|
||||
for methodName, f in pairs(self.__instanceDict) do
|
||||
_propagateInstanceMethod(subclass, methodName, f)
|
||||
end
|
||||
subclass.initialize = function(instance, ...) return self.initialize(instance, ...) end
|
||||
|
||||
self.subclasses[subclass] = true
|
||||
self:subclassed(subclass)
|
||||
|
||||
return subclass
|
||||
end,
|
||||
|
||||
subclassed = function(self, other) end,
|
||||
|
||||
isSubclassOf = function(self, other)
|
||||
return type(other) == 'table' and
|
||||
type(self.super) == 'table' and
|
||||
( self.super == other or self.super:isSubclassOf(other) )
|
||||
end,
|
||||
|
||||
include = function(self, ...)
|
||||
assert(type(self) == 'table', "Make sure you that you are using 'Class:include' instead of 'Class.include'")
|
||||
for _,mixin in ipairs({...}) do _includeMixin(self, mixin) end
|
||||
return self
|
||||
end
|
||||
}
|
||||
}
|
||||
|
||||
function middleclass.class(name, super)
|
||||
assert(type(name) == 'string', "A name (string) is needed for the new class")
|
||||
return super and super:subclass(name) or _includeMixin(_createClass(name), DefaultMixin)
|
||||
end
|
||||
|
||||
setmetatable(middleclass, { __call = function(_, ...) return middleclass.class(...) end })
|
||||
|
||||
return middleclass
|
Loading…
x
Reference in New Issue
Block a user