From 11db1dc49f469c4544c5c17fd4abaa6a6861aeb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?tekkub=20=CA=95=20=C2=B4=E1=B4=A5=60=20=CA=94?= Date: Fri, 9 Sep 2016 04:11:16 -0600 Subject: [PATCH] Pull item row generator into its own file --- GnomishVendorShrinker.lua | 176 +++---------------------------------- GnomishVendorShrinker.toc | 1 + frames/MerchantItem.lua | 179 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 190 insertions(+), 166 deletions(-) create mode 100644 frames/MerchantItem.lua diff --git a/GnomishVendorShrinker.lua b/GnomishVendorShrinker.lua index b07e0d9..23604fb 100644 --- a/GnomishVendorShrinker.lua +++ b/GnomishVendorShrinker.lua @@ -4,7 +4,7 @@ local myname, ns = ... local ItemSearch = LibStub('LibItemSearch-1.0') -local NUMROWS, ICONSIZE, GAP, SCROLLSTEP = 14, 17, 4, 5 +local NUMROWS, SCROLLSTEP = 14, 5 for _,f in pairs{MerchantNextPageButton, MerchantPrevPageButton, MerchantPageText} do @@ -21,178 +21,22 @@ GVS:SetScript("OnEvent", function(self, event, ...) if self[event] then return s GVS:Hide() -local function OnClick(self, button) - if IsAltKeyDown() and not self.AltCurrency:IsShown() then self:BuyItem(true) - elseif IsModifiedClick() then HandleModifiedItemClick(GetMerchantItemLink(self:GetID())) - elseif self.AltCurrency:IsShown() then - local id = self:GetID() - local link = GetMerchantItemLink(id) - self.link, self.texture = GetMerchantItemLink(id), self.icon:GetTexture() - MerchantFrame_ConfirmExtendedItemCost(self) - else self:BuyItem() end -end - - -local function PopoutOnClick(self, button) - local id = self:GetParent():GetID() - local link = GetMerchantItemLink(id) - if not link then return end - - local _, _, _, vendorStackSize, numAvailable = GetMerchantItemInfo(id) - local maxPurchase = GetMerchantItemMaxStack(id) - local _, _, _, _, _, _, _, itemStackSize = GetItemInfo(link) - - local size = numAvailable > 0 and numAvailable or itemStackSize - OpenStackSplitFrame(250, self, "LEFT", "RIGHT") -end - - -local function Purchase(id, quantity) - local _, _, _, vendorStackSize, numAvailable = GetMerchantItemInfo(id) - local maxPurchase = GetMerchantItemMaxStack(id) - - if numAvailable > 0 and numAvailable < quantity then quantity = numAvailable end - local purchased = 0 - while purchased < quantity do - local buyamount = math.min(maxPurchase, quantity - purchased) - purchased = purchased + buyamount - BuyMerchantItem(id, buyamount) - end -end - - -local function BuyItem(self, fullstack) - local id = self:GetID() - local link = GetMerchantItemLink(id) - if not link then return end - - local _, _, _, vendorStackSize = GetMerchantItemInfo(id) - local _, _, _, _, _, _, _, itemStackSize = GetItemInfo(link) - Purchase(id, fullstack and itemStackSize or vendorStackSize or 1) -end - - -local function PopoutSplitStack(self, qty) - Purchase(self:GetParent():GetID(), qty) -end - - -local ROWHEIGHT = 21 local rows = {} for i=1,NUMROWS do - local row = CreateFrame('Button', nil, GVS) -- base frame - row:SetHeight(ROWHEIGHT) - row:SetPoint("TOP", i == 1 and GVS or rows[i-1], i == 1 and "TOP" or "BOTTOM") - row:SetPoint("LEFT") - row:SetPoint("RIGHT", -19, 0) + local row = ns.NewMerchantItemFrame(GVS) - row.BuyItem = BuyItem - - row:SetHighlightTexture("Interface\\HelpFrame\\HelpFrameButton-Highlight") - row:GetHighlightTexture():SetTexCoord(0, 1, 0, 0.578125) - - row:RegisterForClicks("AnyUp") - row:SetScript('OnClick', OnClick) - row:RegisterForDrag("LeftButton") - row:SetScript('OnDragStart', function(self, button) - MerchantFrame.extendedCost = nil - PickupMerchantItem(self:GetID()) - if self.extendedCost then MerchantFrame.extendedCost = self end - end) - - local backdrop = row:CreateTexture(nil, "BACKGROUND") - backdrop:SetAllPoints() - backdrop:SetTexture("Interface\\Tooltips\\UI-Tooltip-Background") - row.backdrop = backdrop - - local icon = CreateFrame('Frame', nil, row) - icon:SetHeight(ICONSIZE) - icon:SetWidth(ICONSIZE) - icon:SetPoint('LEFT', 2, 0) - - row.icon = icon:CreateTexture(nil, "BORDER") - row.icon:SetAllPoints() - - local popout = CreateFrame("Button", nil, row) - popout:SetPoint("RIGHT") - popout:SetWidth(ROWHEIGHT/2) popout:SetHeight(ROWHEIGHT) - popout:SetNormalTexture("Interface\\PaperDollInfoFrame\\UI-GearManager-FlyoutButton") - popout:SetHighlightTexture("Interface\\PaperDollInfoFrame\\UI-GearManager-FlyoutButton") - popout:GetNormalTexture():SetTexCoord(0.15625, 0.5, 0.84375, 0.5, 0.15625, 0, 0.84375, 0) - popout:GetHighlightTexture():SetTexCoord(0.15625, 1, 0.84375, 1, 0.15625, 0.5, 0.84375, 0.5) - popout:SetScript("OnClick", PopoutOnClick) - popout.SplitStack = PopoutSplitStack - row.popout = popout - - local ItemPrice = row:CreateFontString(nil, nil, "NumberFontNormal") - ItemPrice:SetPoint('RIGHT', popout, "LEFT", -2, 0) - row.ItemPrice = ItemPrice - - local AltCurrency = ns.NewAltCurrencyFrame(row) - AltCurrency:SetPoint("RIGHT", ItemPrice, "LEFT") - row.AltCurrency = AltCurrency - - local ItemName = row:CreateFontString(nil, nil, "GameFontNormalSmall") - ItemName:SetPoint("LEFT", icon, "RIGHT", GAP, 0) - ItemName:SetPoint("RIGHT", AltCurrency, "LEFT", -GAP, 0) - ItemName:SetJustifyH("LEFT") - row.ItemName = ItemName - - row:SetScript('OnEnter', function(self) - GameTooltip:SetOwner(self, "ANCHOR_RIGHT") - GameTooltip:SetMerchantItem(self:GetID()) - GameTooltip_ShowCompareItem() - MerchantFrame.itemHover = self:GetID() - if IsModifiedClick("DRESSUP") then ShowInspectCursor() else ResetCursor() end - end) - row:SetScript('OnLeave', function() - GameTooltip:Hide() - ResetCursor() - MerchantFrame.itemHover = nil - end) + if i == 1 then + row:SetPoint("TOPLEFT") + row:SetPoint("RIGHT", -19, 0) + else + row:SetPoint("TOPLEFT", rows[i-1], "BOTTOMLEFT") + row:SetPoint("RIGHT", rows[i-1]) + end rows[i] = row end -local function ShowMerchantItem(row, i) - row:SetID(i) - row:Show() - - local name, itemTexture, itemPrice, itemStackCount, numAvailable, isUsable, extendedCost = GetMerchantItemInfo(i) - local link = GetMerchantItemLink(i) - - local gradient, shown = ns.GetRowGradient(i) - row.backdrop:SetGradientAlpha("HORIZONTAL", unpack(gradient)) - row.backdrop:SetShown(shown) - - row.icon:SetTexture(itemTexture) - - local textcolor = ns.GetRowTextColor(i) - row.ItemName:SetText((numAvailable > -1 and ("["..numAvailable.."] ") or "").. textcolor.. (name or "").. (itemStackCount > 1 and ("|r x"..itemStackCount) or "")) - - row.AltCurrency:SetValue(i) - - if extendedCost then - row.link, row.texture, row.extendedCost = link, itemTexture, true - end - if itemPrice > 0 then - row.ItemPrice:SetText(ns.GSC(itemPrice)) - row.Price = itemPrice - end - if extendedCost and (itemPrice <= 0) then - row.ItemPrice:SetText("") - row.Price = 0 - elseif extendedCost and (itemPrice > 0) then - row.ItemPrice:SetText(ns.GSC(itemPrice)) - else - row.extendedCost = nil - end - - row.icon:SetVertexColor(ns.GetRowVertexColor(i)) -end - - local scrollbar = LibStub("tekKonfig-Scroll").new(GVS, 0, SCROLLSTEP) local offset = 0 local function Refresh() @@ -202,7 +46,7 @@ local function Refresh() local link = GetMerchantItemLink(i) if ItemSearch:Find(link, GVS.searchstring) then if n_searchmatch >= offset and n_searchmatch < offset + NUMROWS then - ShowMerchantItem(rows[row], i) + rows[row]:SetValue(i) row = row + 1 end n_searchmatch = n_searchmatch + 1 diff --git a/GnomishVendorShrinker.toc b/GnomishVendorShrinker.toc index c312515..5816cd3 100644 --- a/GnomishVendorShrinker.toc +++ b/GnomishVendorShrinker.toc @@ -21,6 +21,7 @@ LibItemSearch-1.0.lua frames\AltCurrency.lua frames\AltCurrencyItem.lua +frames\MerchantItem.lua frames\RowShader.lua frames\SearchField.lua diff --git a/frames/MerchantItem.lua b/frames/MerchantItem.lua new file mode 100644 index 0000000..3a49bbf --- /dev/null +++ b/frames/MerchantItem.lua @@ -0,0 +1,179 @@ + +local myname, ns = ... + + +local GAP = 4 +local HEIGHT = 21 +local ICONSIZE = 17 + + +local function OnClick(self, button) + if IsAltKeyDown() and not self.AltCurrency:IsShown() then + self:BuyItem(true) + elseif IsModifiedClick() then + HandleModifiedItemClick(GetMerchantItemLink(self:GetID())) + elseif self.AltCurrency:IsShown() then + local id = self:GetID() + local link = GetMerchantItemLink(id) + self.link, self.texture = GetMerchantItemLink(id), self.icon:GetTexture() + MerchantFrame_ConfirmExtendedItemCost(self) + else self:BuyItem() end +end + + +local function PopoutOnClick(self, button) + local id = self:GetParent():GetID() + local link = GetMerchantItemLink(id) + if not link then return end + + local _, _, _, vendorStackSize, numAvailable = GetMerchantItemInfo(id) + local maxPurchase = GetMerchantItemMaxStack(id) + local _, _, _, _, _, _, _, itemStackSize = GetItemInfo(link) + + local size = numAvailable > 0 and numAvailable or itemStackSize + OpenStackSplitFrame(250, self, "LEFT", "RIGHT") +end + + +local function Purchase(id, quantity) + local _, _, _, vendorStackSize, numAvailable = GetMerchantItemInfo(id) + local maxPurchase = GetMerchantItemMaxStack(id) + + if numAvailable > 0 and numAvailable < quantity then quantity = numAvailable end + local purchased = 0 + while purchased < quantity do + local buyamount = math.min(maxPurchase, quantity - purchased) + purchased = purchased + buyamount + BuyMerchantItem(id, buyamount) + end +end + + +local function BuyItem(self, fullstack) + local id = self:GetID() + local link = GetMerchantItemLink(id) + if not link then return end + + local _, _, _, vendorStackSize = GetMerchantItemInfo(id) + local _, _, _, _, _, _, _, itemStackSize = GetItemInfo(link) + Purchase(id, fullstack and itemStackSize or vendorStackSize or 1) +end + + +local function PopoutSplitStack(self, qty) + Purchase(self:GetParent():GetID(), qty) +end + + +local function SetValue(self, i) + self:SetID(i) + self:Show() + + local name, itemTexture, itemPrice, itemStackCount, numAvailable, isUsable, extendedCost = GetMerchantItemInfo(i) + local link = GetMerchantItemLink(i) + + local gradient, shown = ns.GetRowGradient(i) + self.backdrop:SetGradientAlpha("HORIZONTAL", unpack(gradient)) + self.backdrop:SetShown(shown) + + self.icon:SetTexture(itemTexture) + + local textcolor = ns.GetRowTextColor(i) + self.ItemName:SetText((numAvailable > -1 and ("["..numAvailable.."] ") or "").. textcolor.. (name or "").. (itemStackCount > 1 and ("|r x"..itemStackCount) or "")) + + self.AltCurrency:SetValue(i) + + if extendedCost then + self.link, self.texture, self.extendedCost = link, itemTexture, true + end + if itemPrice > 0 then + self.ItemPrice:SetText(ns.GSC(itemPrice)) + self.Price = itemPrice + end + if extendedCost and (itemPrice <= 0) then + self.ItemPrice:SetText("") + self.Price = 0 + elseif extendedCost and (itemPrice > 0) then + self.ItemPrice:SetText(ns.GSC(itemPrice)) + else + self.extendedCost = nil + end + + self.icon:SetVertexColor(ns.GetRowVertexColor(i)) +end + + +function ns.NewMerchantItemFrame(parent) + local frame = CreateFrame("Button", nil, parent) + frame:SetHeight(HEIGHT) + + frame.BuyItem = BuyItem + + frame:SetHighlightTexture("Interface\\HelpFrame\\HelpFrameButton-Highlight") + frame:GetHighlightTexture():SetTexCoord(0, 1, 0, 0.578125) + + frame:RegisterForClicks("AnyUp") + frame:SetScript('OnClick', OnClick) + frame:RegisterForDrag("LeftButton") + frame:SetScript('OnDragStart', function(self, button) + MerchantFrame.extendedCost = nil + PickupMerchantItem(self:GetID()) + if self.extendedCost then MerchantFrame.extendedCost = self end + end) + + local backdrop = frame:CreateTexture(nil, "BACKGROUND") + backdrop:SetAllPoints() + backdrop:SetTexture("Interface\\Tooltips\\UI-Tooltip-Background") + frame.backdrop = backdrop + + local icon = CreateFrame('Frame', nil, frame) + icon:SetHeight(ICONSIZE) + icon:SetWidth(ICONSIZE) + icon:SetPoint('LEFT', 2, 0) + + frame.icon = icon:CreateTexture(nil, "BORDER") + frame.icon:SetAllPoints() + + local popout = CreateFrame("Button", nil, frame) + popout:SetPoint("RIGHT") + popout:SetWidth(HEIGHT/2) popout:SetHeight(HEIGHT) + popout:SetNormalTexture("Interface\\PaperDollInfoFrame\\UI-GearManager-FlyoutButton") + popout:SetHighlightTexture("Interface\\PaperDollInfoFrame\\UI-GearManager-FlyoutButton") + popout:GetNormalTexture():SetTexCoord(0.15625, 0.5, 0.84375, 0.5, 0.15625, 0, 0.84375, 0) + popout:GetHighlightTexture():SetTexCoord(0.15625, 1, 0.84375, 1, 0.15625, 0.5, 0.84375, 0.5) + popout:SetScript("OnClick", PopoutOnClick) + popout.SplitStack = PopoutSplitStack + frame.popout = popout + + local ItemPrice = frame:CreateFontString(nil, nil, "NumberFontNormal") + ItemPrice:SetPoint('RIGHT', popout, "LEFT", -2, 0) + frame.ItemPrice = ItemPrice + + local AltCurrency = ns.NewAltCurrencyFrame(frame) + AltCurrency:SetPoint("RIGHT", ItemPrice, "LEFT") + frame.AltCurrency = AltCurrency + + local ItemName = frame:CreateFontString(nil, nil, "GameFontNormalSmall") + ItemName:SetPoint("LEFT", icon, "RIGHT", GAP, 0) + ItemName:SetPoint("RIGHT", AltCurrency, "LEFT", -GAP, 0) + ItemName:SetJustifyH("LEFT") + frame.ItemName = ItemName + + frame:SetScript('OnEnter', function(self) + GameTooltip:SetOwner(self, "ANCHOR_RIGHT") + GameTooltip:SetMerchantItem(self:GetID()) + GameTooltip_ShowCompareItem() + MerchantFrame.itemHover = self:GetID() + if IsModifiedClick("DRESSUP") then ShowInspectCursor() else ResetCursor() end + end) + + frame:SetScript('OnLeave', function() + GameTooltip:Hide() + ResetCursor() + MerchantFrame.itemHover = nil + end) + + frame.SetValue = SetValue + + return frame +end