From a21058e927913b97e2089e3010161ccbb838064e Mon Sep 17 00:00:00 2001 From: Manuel Simon Hirsig Date: Tue, 23 Aug 2016 01:50:01 +0200 Subject: [PATCH] big refactoring --- aux-addon.lua | 140 ++++++++++++++++++++++++++++++-------------------- gui/core.lua | 14 +---- util/core.lua | 62 +++++++++++----------- 3 files changed, 119 insertions(+), 97 deletions(-) diff --git a/aux-addon.lua b/aux-addon.lua index 0da2112..7f78cb2 100644 --- a/aux-addon.lua +++ b/aux-addon.lua @@ -20,6 +20,7 @@ do t[k] = nil end table.setn(t, 0) + setmetatable(t, nil) end function public.recycle(t) @@ -29,7 +30,7 @@ do end function public.accessor.t() return - tremove(table_pool) or {} + tremove(table_pool) or {} end function public.accessor.tt() @@ -39,53 +40,80 @@ do end do - local mt = { - __call=function(self, arg) return self[1](arg) end, - __sub=function(self, arg) return self[1](arg) end, - } - function public.modifier(f) - local self = t - self[1] = f - return setmetatable(self, mt) + local T = t + local mt = {__unm=function(self) T = t; return setmetatable(self, nil) end} + function public.accessor.T() + wipe(T) + return setmetatable(T, mt) end end - public.temp = modifier(function(t) - temporary[t] = true - return t - end) - - public.perm = modifier(function(t) - temporary[t] = false - return t - end) - + function public.modifier_mt(f) + local function apply(_, value) return f(value) end + return { + __call=apply, __add=apply, __sub=apply, __mul=apply, __div=apply, __pow=apply, __concat=apply, __lt=apply, __le=apply, + __unm=function(self) return self end, + } + end do - public.set = modifier(function(t) - local self = _m.t - for _, v in t do self[v] = true end - recycle(t) - return self - end) + local mt = modifier_mt(function(t) temporary[t] = true; return t end) + function public.accessor.temp() + return setmetatable(T, mt) + end + end + do + local mt = modifier_mt(function(t) temporary[t] = false; return t end) + function public.accessor.perm() + return setmetatable(T, mt) + end end - do - local mt = { - __call=function(self, value) - tinsert(self, value) + function public.collector_mt(f) + return { + __unm=function(self) + setmetatable(self, nil) + end, + __index=function(self, key) + f(self, key) + return self + end, + __call=function(self, arg1, arg2) + f(self, arg2 or arg1) return self end, } - function public.accessor.from() - return setmetatable(tt, mt) + end + do + local mt = collector_mt(tinsert) + function public.accessor.list() + return setmetatable(T, mt) + end + end + do + local mt = collector_mt(function(self, value) + rawset(self, value, true) + end) + function public.accessor.set() + return setmetatable(T, mt) + end + end + do + local key + local mt = collector_mt(function(self, value) + if key ~= nil then + rawset(self, key, value) + else + key = value + end + end) + function public.accessor.map() + return setmetatable(T, mt) end end - - -- TODO map end local event_frame = CreateFrame 'Frame' -for event in temp-set-from 'ADDON_LOADED' 'VARIABLES_LOADED' 'PLAYER_LOGIN' 'AUCTION_HOUSE_SHOW' 'AUCTION_HOUSE_CLOSED' 'AUCTION_BIDDER_LIST_UPDATE' 'AUCTION_OWNED_LIST_UPDATE' do +for event in perm <- set 'ADDON_LOADED' 'VARIABLES_LOADED' 'PLAYER_LOGIN' 'AUCTION_HOUSE_SHOW' 'AUCTION_HOUSE_CLOSED' 'AUCTION_BIDDER_LIST_UPDATE' 'AUCTION_OWNED_LIST_UPDATE' do event_frame:RegisterEvent(event) end @@ -114,7 +142,11 @@ function public.log(...) DEFAULT_CHAT_FRAME:AddMessage(LIGHTYELLOW_FONT_COLOR_CODE..msg) end -tabs = {} +map :search_tab 'Search' :post_tab 'Post' :auctions_tab 'Auctions' :bids_tab 'Bids' +tabs = t +for _, name in from (from 'Search') 'Post' 'Auctions' 'Bids' do + tinsert(tabs) +end function public.tab(index, name) local module_env = getfenv(2) local tab = {name=name, env=module_env} @@ -126,9 +158,25 @@ do local active_tab_index function accessor.active_tab() return tabs[active_tab_index] end function on_tab_click(index) - call(active_tab_index and active_tab.env.CLOSE) + call(active_tab_index and active_tab.CLOSE) active_tab_index = index - call(active_tab_index and active_tab.env.OPEN) + call(active_tab_index and active_tab.OPEN) + end +end +function SetItemRef(...) + if arg[3] ~= 'RightButton' or not index(active_tab, 'env', 'CLICK_LINK') or not strfind(arg[1], '^item:%d+') then + return orig.SetItemRef(unpack(arg)) + end + for item_info in present(info.item(tonumber(select(3, strfind(arg[1], '^item:(%d+)'))))) do + return active_tab.CLICK_LINK(item_info) + end +end +function UseContainerItem(...) + if modified or not index(active_tab, 'env', 'USE_ITEM') then + return orig.UseContainerItem(unpack(arg)) + end + for item_info in present(info.container_item(arg[1], arg[2])) do + return active_tab.env.USE_ITEM(item_info) end end @@ -303,22 +351,4 @@ function AuctionFrameAuctions_OnEvent(...) if AuctionFrameAuctions:IsVisible() then return orig.AuctionFrameAuctions_OnEvent(unpack(arg)) end -end - -function SetItemRef(...) - if arg[3] ~= 'RightButton' or not index(active_tab, 'env', 'CLICK_LINK') or not strfind(arg[1], '^item:%d+') then - return orig.SetItemRef(unpack(arg)) - end - for item_info in present(info.item(tonumber(select(3, strfind(arg[1], '^item:(%d+)'))))) do - return active_tab.env.CLICK_LINK(item_info) - end -end - -function UseContainerItem(...) - if modified or not index(active_tab, 'env', 'USE_ITEM') then - return orig.UseContainerItem(unpack(arg)) - end - for item_info in present(info.container_item(arg[1], arg[2])) do - return active_tab.env.USE_ITEM(item_info) - end end \ No newline at end of file diff --git a/gui/core.lua b/gui/core.lua index 9ab4a70..2ff73b3 100644 --- a/gui/core.lua +++ b/gui/core.lua @@ -328,11 +328,7 @@ function public.editbox(parent) call(this.focus_loss) end) editbox:SetScript('OnTextChanged', function() - if this.focused then - colorize() - else - format() - end + if this.focused then colorize() else format() end call(this.change) end) editbox:SetScript('OnCursorChanged', function() @@ -387,11 +383,8 @@ end function public.status_bar(parent) local self = CreateFrame('Frame', nil, parent) - local level = parent:GetFrameLevel() - self:SetFrameLevel(level + 1) - do -- minor status bar (gray one) local status_bar = CreateFrame('STATUSBAR', nil, self, 'TextStatusBar') @@ -410,7 +403,6 @@ function public.status_bar(parent) end) self.minor_status_bar = status_bar end - do -- major status bar (main blue one) local status_bar = CreateFrame('STATUSBAR', nil, self, 'TextStatusBar') @@ -429,7 +421,6 @@ function public.status_bar(parent) end) self.major_status_bar = status_bar end - do local text_frame = CreateFrame('Frame', nil, self) text_frame:SetFrameLevel(level + 4) @@ -439,7 +430,6 @@ function public.status_bar(parent) text:SetPoint('CENTER', 0, 0) self.text = text end - function self:update_status(major_status, minor_status) if major_status then self.major_status_bar:SetValue(major_status) @@ -448,11 +438,9 @@ function public.status_bar(parent) self.minor_status_bar:SetValue(minor_status) end end - function self:set_text(text) self.text:SetText(text) end - return self end diff --git a/util/core.lua b/util/core.lua index 3e244d5..137bd2a 100644 --- a/util/core.lua +++ b/util/core.lua @@ -68,11 +68,13 @@ do end function public.call(f, ...) + temp(arg) if f then return f(unpack(arg)) end end function public.index(t, ...) for i=1,arg.n do t = t and t[arg[i]] end + recycle(arg) return t end @@ -107,19 +109,21 @@ end function public.expand(array, ...) local t = t for i=1,arg.n do t[arg[i]] = array[i] end + recycle(arg) return t end function public.copy(t) local copy = _m.t for k, v in t do copy[k] = v end - table.setn(t, getn(t)) - return copy + table.setn(copy, getn(t)) + return setmetatable(copy, getmetatable(t)) end function public.select(i, ...) + temp(arg) while i > 1 do i = i - 1; tremove(arg, i) end - return tremove(arg, 1), unpack(arg) + return tremove(arg, 1), unpack(temp) end function public.size(t) @@ -135,15 +139,15 @@ function public.key(value, t) end function public.keys(t) - local ks = _m.t - for k in t do tinsert(ks, k) end - return ks + local keys = _m.t + for k in t do tinsert(keys, k) end + return keys end function public.values(t) - local vs = _m.t - for _, v in t do tinsert(vs, v) end - return vs + local values = _m.t + for _, v in t do tinsert(values, v) end + return values end function public.eq(t1, t2) @@ -161,44 +165,44 @@ function public.eq(t1, t2) return true end -function public.any(xs, p) - for _, x in xs do +function public.any(t, p) + for _, v in t do if p then - if p(x) then return true end - elseif x then + if p(v) then return true end + elseif v then return true end end return false end -function public.all(xs, p) - for _, x in xs do +function public.all(t, p) + for _, v in t do if p then - if not p(x) then + if not p(v) then return false end - elseif not x then + elseif not v then return false end end return true end -function public.filter(t0, p) - local t = t - for k, x in t0 do - if p(x, k) then - t[k] = x +function public.filter(t, p) + local filtered = _m.t + for k, v in t do + if p(v, k) then + filtered[k] = v end end - return t + return filtered end -function public.map(t0, f) - local t = t - for k, x in t0 do t[k] = f(x, k) end - return t +function public.map(t, f) + local mapped = _m.t + for k, v in t do mapped[k] = f(v, k) end + return mapped end function public.trim(str) @@ -206,7 +210,7 @@ function public.trim(str) end function public.split(str, separator) - local parts = {} + local parts = t while true do local start_index, _ = strfind(str, separator, 1, true) if start_index then @@ -222,7 +226,7 @@ function public.split(str, separator) end function public.tokenize(str) - local tokens = {} + local tokens = t for token in string.gfind(str, '%S+') do tinsert(tokens, token) end return tokens end