From 088bb54cc6efe95e0701dac2ba94471842952daf Mon Sep 17 00:00:00 2001 From: Brandon Talbot Date: Wed, 29 Jun 2016 09:16:22 +0200 Subject: [PATCH] Create a template class creator --- src/lua/base/djclass.lua | 69 ++++++++++++++++++++++++++++++++++++++++++++++ src/lua/core.lua | 1 + src/manifest.xml | 33 ++++++++++++++++++++++ 3 files changed, 103 insertions(+) create mode 100644 src/lua/base/djclass.lua create mode 100644 src/lua/core.lua create mode 100644 src/manifest.xml diff --git a/src/lua/base/djclass.lua b/src/lua/base/djclass.lua new file mode 100644 index 0000000..157a374 --- /dev/null +++ b/src/lua/base/djclass.lua @@ -0,0 +1,69 @@ +-- local NAME, ADDON = ... + +function class(...) + local arg1 = ... + local base + if type(arg1) == 'table' and arg1.__djclass then + base = arg1 + end + + local c = {} + c.__index = c + c.__djclass = true + c.type = select(base and 2 or 1, ...) or base and base.type + c.inherits = select(base and 3 or 2, ...) or base and base.inherits + + if base then + for k, v in pairs(base) do + c[k] = v + end + end + + setmetatable(c, { + __call = function(table, ...) + local name + if table.type then + name = ... + assert(name and type(name) == 'string', 'DJClass needs a name (first argument) when creating an object for WoWs frame content') + end + local obj + if name then + obj = {} -- CreateFrame(name, table.type, UIParent, table.inherits) + else + obj = {} + end + + for k, v in pairs(table) do + obj[k] = v + end + + if obj.init and type(obj.init) == 'function' then + obj:init(select(name and 2 or 1, ...)) + end + + return obj + end + }) + + return c +end + +local A = class('FRAME') +function A:init(string) + self.name = string +end + +local B = class(A) +function B:init(name, text) + A.init(self, name) + self.text = text +end + +function B:print() + print(self.name, self.text) +end + +print("\nstart\n\n") + +local b = B('Namerize', 'Text', 'Face') +b:print() \ No newline at end of file diff --git a/src/lua/core.lua b/src/lua/core.lua new file mode 100644 index 0000000..4f05f6d --- /dev/null +++ b/src/lua/core.lua @@ -0,0 +1 @@ +local NAME, ADDON = ... \ No newline at end of file diff --git a/src/manifest.xml b/src/manifest.xml new file mode 100644 index 0000000..5e4e862 --- /dev/null +++ b/src/manifest.xml @@ -0,0 +1,33 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +