From abc163d7f3e739cdb4f6e3ff4d0ef3f075061e93 Mon Sep 17 00:00:00 2001 From: Manuel Simon Hirsig Date: Sun, 7 Aug 2016 23:06:31 +0200 Subject: [PATCH] refactoring, change of pickup/use behavior --- components/post.lua | 2 +- components/stack.lua | 2 +- core.lua | 150 +++++++++++++++++------------------------ gui.lua | 21 +++--- tabs/auctions/core.lua | 4 +- tabs/bids/core.lua | 4 +- tabs/post/core.lua | 14 +++- tabs/post/frames.lua | 27 +------- tabs/search/core.lua | 19 +++++- tabs/search/frames.lua | 8 --- 10 files changed, 108 insertions(+), 143 deletions(-) diff --git a/components/post.lua b/components/post.lua index 9d519bf..3a185ba 100644 --- a/components/post.lua +++ b/components/post.lua @@ -30,7 +30,7 @@ function private.post_auction(slot, k) ClearCursor() ClickAuctionSellItemButton() ClearCursor() - PickupContainerItem(unpack(slot)) + aux.orig.PickupContainerItem(unpack(slot)) ClickAuctionSellItemButton() ClearCursor() diff --git a/components/stack.lua b/components/stack.lua index 6188dcf..46a922b 100644 --- a/components/stack.lua +++ b/components/stack.lua @@ -61,7 +61,7 @@ function private.move_item(from_slot, to_slot, amount, k) ClearCursor() SplitContainerItem(from_slot[1], from_slot[2], amount) - PickupContainerItem(unpack(to_slot)) + aux.orig.PickupContainerItem(unpack(to_slot)) return aux.control.when(function() return m.stack_size(to_slot) == expected_size end, k) end diff --git a/core.lua b/core.lua index 76d065f..8af598e 100644 --- a/core.lua +++ b/core.lua @@ -2,6 +2,37 @@ local addon = aux_module() aux = tremove(addon, 1) local m, public, private = unpack(addon) +do + local mt = { + __newindex = function(self, key, value) + self._f()[key] = value + end, + __index = function(self, key) + return self._f()[key] + end, + __call = function(self) + return self._f() + end, + } + function public.dynamic_table(f) + return setmetatable({_f=f}, mt) + end +end + +do + local mt = { + __index = function(self, key) + return self:_cb(key) + end, + __call = function(self) + return self:_cb() + end, + } + function public.index_function(cb) + return setmetatable({_cb = cb}, mt) + end +end + private.modules = {} function private.initialize_module(public_declarator) public_declarator.LOAD = nil @@ -17,20 +48,21 @@ end private.tabs = {} function public.tab(index, name) - local ret = { m.module(name) } - ret[2].ACTIVE = function() + local tab = {m.module(name)} + tab[2].ACTIVE = function() return m[name] == m.active_tab() end + for _, handler in {'OPEN', 'CLOSE', 'CLICK_LINK', 'PICKUP_ITEM', 'USE_ITEM'} do + tab[2][handler] = nil + end m.tabs[index] = m[name] - return unpack(ret) + return unpack(tab) end do local active_tab_index - function private.active_tab() - if active_tab_index then - return m.tabs[active_tab_index] - end - end + private.active_tab = m.dynamic_table(function() + return m.tabs[active_tab_index] + end) function private.on_tab_click(index) if m.active_tab() then m.active_tab().CLOSE() @@ -104,37 +136,6 @@ do end end -do - local mt = { - __newindex = function(self, key, value) - self._f()[key] = value - end, - __index = function(self, key) - return self._f()[key] - end, - __call = function(self) - return self._f() - end, - } - function public.dynamic_table(f) - return setmetatable({_f=f}, mt) - end -end - -do - local mt = { - __index = function(self, key) - return self:_cb(key) - end, - __call = function(self) - return self:_cb() - end, - } - function public.index_function(cb) - return setmetatable({_cb = cb}, mt) - end -end - function public.call(f, ...) if f then return f(unpack(arg)) @@ -162,8 +163,6 @@ function public.on_load() aux.gui.set_window_style(AuxFrame) tinsert(UISpecialFrames, 'AuxFrame') - aux.control.event_listener('CURSOR_UPDATE', m.CURSOR_UPDATE) - do local tab_group = m.gui.tab_group(AuxFrame, 'DOWN') tab_group:create_tab('Search') @@ -427,63 +426,34 @@ function public.price_level_color(pct) end end -do -- TODO make it work for other ways to pick up things - local last_picked_up - function private.CURSOR_UPDATE() - last_picked_up = nil - end - function private.PickupContainerItem(...) - local bag, slot = unpack(arg) - aux.control.thread(function() - last_picked_up = {bag, slot} - end) - return m.orig.PickupContainerItem(unpack(arg)) - end - function public.cursor_item() - if last_picked_up and CursorHasItem() then - return m.info.container_item(unpack(last_picked_up)) - end - end +function private.SetItemRef(...) + if not arg[3] == 'RightButton' or not m.active_tab.CLICK_LINK or not strfind(arg[1], '^item:%d+') then + return m.orig.SetItemRef(unpack(arg)) + end + local item_info = m.info.item(tonumber(({strfind(arg[1], '^item:(%d+)')})[3])) + if item_info then + return m.active_tab.CLICK_LINK(item_info) + end end -function private.SetItemRef(...) - local itemstring, text, button = unpack(arg) - if m.search_tab.ACTIVE() and button == 'RightButton' then - local item_info = m.info.item(tonumber(({strfind(itemstring, '^item:(%d+)')})[3])) - if item_info then - m.search_tab.set_filter(strlower(item_info.name)..'/exact') - m.search_tab.execute(nil, false) - return - end - end - return m.orig.SetItemRef(unpack(arg)) +function private.PickupContainerItem(...) + if IsShiftKeyDown() or IsControlKeyDown() or IsAltKeyDown() or not m.active_tab.PICKUP_ITEM then + return m.orig.PickupContainerItem(unpack(arg)) + end + local item_info = m.info.container_item(arg[1], arg[2]) + if item_info then + return m.active_tab.PICKUP_ITEM(item_info) + end end function private.UseContainerItem(...) - local bag, slot = unpack(arg) - if IsShiftKeyDown() or IsControlKeyDown() or IsAltKeyDown() then + if IsShiftKeyDown() or IsControlKeyDown() or IsAltKeyDown() or not m.active_tab.USE_ITEM then return m.orig.UseContainerItem(unpack(arg)) end - - if m.search_tab.ACTIVE() then - local item_info = m.info.container_item(bag, slot) - item_info = item_info and m.info.item(item_info.item_id) - if item_info then - m.search_tab.set_filter(strlower(item_info.name)..'/exact') - m.search_tab.execute(nil, false) - end - return + local item_info = m.info.container_item(arg[1], arg[2]) + if item_info then + return m.active_tab.USE_ITEM(item_info) end - - if m.post_tab.ACTIVE() then - local item_info = m.info.container_item(bag, slot) - if item_info then - m.post_tab.select_item(item_info.item_key) - end - return - end - - return m.orig.UseContainerItem(unpack(arg)) end function public.is_player(name, current) diff --git a/gui.lua b/gui.lua index d8532f7..6d45eb3 100644 --- a/gui.lua +++ b/gui.lua @@ -410,21 +410,22 @@ function public.status_bar(parent) end function public.item(parent) - local item = CreateFrame('Button', nil, parent) + local item = CreateFrame('Frame', nil, parent) item:SetWidth(260) item:SetHeight(40) - local icon = CreateFrame('CheckButton', 'aux_frame'..aux.id(), item, 'ActionButtonTemplate') - icon:SetPoint('LEFT', 2, 0.5) - icon:SetHighlightTexture(nil) - icon:RegisterForClicks() - icon:EnableMouse(nil) - item.texture = getglobal(icon:GetName()..'Icon') + local btn = CreateFrame('CheckButton', 'aux_frame'..aux.id(), item, 'ActionButtonTemplate') + item.button = btn + btn:SetPoint('LEFT', 2, 0.5) + btn:SetHighlightTexture(nil) + btn:RegisterForClicks() + btn:EnableMouse(nil) + item.texture = getglobal(btn:GetName()..'Icon') item.texture:SetTexCoord(0.06,0.94,0.06,0.94) - item.name = aux.gui.label(icon, 15) + item.name = aux.gui.label(btn, 15) item.name:SetJustifyH('LEFT') - item.name:SetPoint('LEFT', icon, 'RIGHT', 10, 0) + item.name:SetPoint('LEFT', btn, 'RIGHT', 10, 0) item.name:SetPoint('RIGHT', item, 'RIGHT', -10, 0.5) - item.count = getglobal(icon:GetName()..'Count') + item.count = getglobal(btn:GetName()..'Count') item.count:SetTextHeight(17) return item end diff --git a/tabs/auctions/core.lua b/tabs/auctions/core.lua index 008222b..985974b 100644 --- a/tabs/auctions/core.lua +++ b/tabs/auctions/core.lua @@ -10,12 +10,12 @@ function m.LOAD() m.create_frames(m, public, private) end -function public.OPEN() +function m.OPEN() m.frame:Show() m.scan_auctions() end -function public.CLOSE() +function m.CLOSE() m.frame:Hide() end diff --git a/tabs/bids/core.lua b/tabs/bids/core.lua index 011cfc8..2c21441 100644 --- a/tabs/bids/core.lua +++ b/tabs/bids/core.lua @@ -10,12 +10,12 @@ function m.LOAD() m.create_frames(m, public, private) end -function public.OPEN() +function m.OPEN() m.frame:Show() m.scan_bids() end -function public.CLOSE() +function m.CLOSE() m.frame:Hide() end diff --git a/tabs/post/core.lua b/tabs/post/core.lua index 616680f..bd7d313 100644 --- a/tabs/post/core.lua +++ b/tabs/post/core.lua @@ -17,7 +17,7 @@ function m.LOAD() m.create_frames(m, public, private) end -function public.OPEN() +function m.OPEN() m.frame:Show() m.deposit:SetText('Deposit: '..aux.money.to_string(0, nil, nil, nil, aux.gui.inline_color.text.enabled)) @@ -30,11 +30,19 @@ function public.OPEN() m.refresh = true end -function public.CLOSE() +function m.CLOSE() m.selected_item = nil m.frame:Hide() end +function m.PICKUP_ITEM(item_info) + m.select_item(item_info.item_key) +end + +function m.USE_ITEM(item_info) + m.select_item(item_info.item_key) +end + function private.default_settings() return { duration = DURATION_8, @@ -358,7 +366,7 @@ function private.unit_vendor_price(item_key) if aux.info.auctionable(item_info.tooltip, nil, item_info.lootable) then ClearCursor() - PickupContainerItem(unpack(slot)) + aux.orig.PickupContainerItem(unpack(slot)) ClickAuctionSellItemButton() local auction_sell_item = aux.info.auction_sell_item() ClearCursor() diff --git a/tabs/post/frames.lua b/tabs/post/frames.lua index d66a494..1ec862b 100644 --- a/tabs/post/frames.lua +++ b/tabs/post/frames.lua @@ -118,34 +118,13 @@ aux.post_tab.FRAMES(function(m, public, private) do local item = aux.gui.item(m.frame.parameters) item:SetPoint('TOPLEFT', 6, -6) - item:EnableMouse() - item:SetScript('OnReceiveDrag', function() - local item_info = aux.cursor_item() - if item_info then - m.select_item(item_info.item_key) - end - ClearCursor() - end) - item:SetScript('OnClick', function() - local item_info = aux.cursor_item() - if item_info then - m.select_item(item_info.item_key) - end - ClearCursor() - end) - local highlight = item:CreateTexture() - highlight:SetPoint('TOPLEFT', -2.5, 2) - highlight:SetPoint('BOTTOMRIGHT', 2.5, -2) - highlight:Hide() - highlight:SetTexture(1, .9, .9, .1) - item:SetScript('OnEnter', function() - highlight:Show() + item.button:EnableMouse(true) + item.button:SetScript('OnEnter', function() if m.selected_item then aux.info.set_tooltip(m.selected_item.itemstring, this, 'ANCHOR_RIGHT') end end) - item:SetScript('OnLeave', function() - highlight:Hide() + item.button:SetScript('OnLeave', function() GameTooltip:Hide() end) private.item = item diff --git a/tabs/search/core.lua b/tabs/search/core.lua index ef267d8..c291e35 100644 --- a/tabs/search/core.lua +++ b/tabs/search/core.lua @@ -144,17 +144,32 @@ function m.LOAD() m.current_search().placeholder = true end -function public.OPEN() +function m.OPEN() m.frame:Show() m.update_search_listings() end -function public.CLOSE() +function m.CLOSE() m.close_settings() m.current_search().table:SetSelectedRecord() m.frame:Hide() end +function m.CLICK_LINK(item_info) + m.set_filter(strlower(item_info.name)..'/exact') + m.execute(nil, false) +end + +function m.PICKUP_ITEM(item_info) + m.set_filter(strlower(item_info.name)..'/exact') + m.execute(nil, false) +end + +function m.USE_ITEM(item_info) + m.set_filter(strlower(item_info.name)..'/exact') + m.execute(nil, false) +end + function private.update_search_listings() local favorite_search_rows = {} for i, favorite_search in aux_favorite_searches do diff --git a/tabs/search/frames.lua b/tabs/search/frames.lua index 80a480b..0dd7451 100644 --- a/tabs/search/frames.lua +++ b/tabs/search/frames.lua @@ -243,14 +243,6 @@ aux.search_tab.FRAMES(function(m, public, private) this:HighlightText(0, 0) end) editbox:SetScript('OnEnterPressed', m.execute) - editbox:SetScript('OnReceiveDrag', function() - local item_info = aux.cursor_item() and aux.info.item(aux.cursor_item().item_id) - if item_info then - m.set_filter(strlower(item_info.name)..'/exact') - m.execute(nil, false) - end - ClearCursor() - end) private.search_box = editbox end do