--[[
Lua Fenster erstellen
--]]
-------------------
-- Global Variables
-------------------
SLASH_BUYWIN1 = '/buywin'
SLASH_BUYWIN2 = '/bw'
BuyWin = {}
local traderopen=false -- Händler ist offen
local tradeitem=nil -- Laufende nummer vom Händler
-- local tradeitemtexture = nil -- angeklicktes Item
local enabled=true -- Addon ist Aktiv
function BuyWin:GetDisplay(text)
local displays=
{
["deDE"] = {
["ok"] = "OK",
["cancel"] = "Abbrechen",
["active"] = "BuyWin ist jetzt aktiv!",
["inactive"] = "BuyWin ist jetzt inaktiv!",
["ver"] = "Du benutzt BuyWin Version ",
["help"] = "schaltet das AddOn aus oder an"
},
["enGB"] = {
["ok"] = "OK",
["cancel"] = "Cancel",
["active"] = "BuyWin is now active!",
["inactive"] = "BuyWin is now inactive!",
["ver"] = "Your are using BuyWin Version ",
["help"] = "switches the AddOn off or on"
}
}
lang=GetLocale()
if displays[lang]==nil then
lang="enGB"
end
return displays[lang][text]
end
------------------
-- Display Window
------------------
function BuyWin:CloseWindow()
edt_amount:SetText("")
BuyFrame:Hide()
end
function BuyWin:OpenWindow()
BuyFrame:SetFrameStrata("HIGH")
local x, y = BuyWin:GetFramePosition();
BuyFrame:SetPoint("BOTTOMLEFT",x+20,y-120)
BuyFrame:Show()
end
function BuyWin:GetFramePosition()
local x, y = GetCursorPosition();
x= x*UIParent:GetWidth()/WorldFrame:GetWidth()
y= y*UIParent:GetHeight()/WorldFrame:GetHeight()
return x,y
end
------------------
-- Setup
------------------
function BuyWin:Init(event, addon)
if event == "ADDON_LOADED" and addon == "BuyWin" then
BuyWin:CreateGUI(BuyFrame)
end
if event == "MERCHANT_SHOW" then
edt_amount:SetText("")
traderopen=true
end
if event=="MERCHANT_CLOSED" then
BuyWin:CloseWindow()
traderopen=false;
end
return
end
------------------
-- Hook Merchant
------------------
local org_MerchantItemButton_OnModifiedClick = MerchantItemButton_OnModifiedClick;
function MerchantItemButton_OnModifiedClick(self,button) -- Buttonframe, used Button: LeftButton/RightButton
if ( MerchantFrame.selectedTab == 1 ) and IsShiftKeyDown() and enabled and GetCurrentKeyBoardFocus()==nil then
local mouseFocus=GetMouseFocus()
tradeitem = mouseFocus:GetID() --Laufende Nummer
name, texture, price, quantity, numAvailable, isUsable, extendedCost = GetMerchantItemInfo(tradeitem)
BuyWin:SetIcon(texture)
BuyWin:OpenWindow()
return
end
return org_MerchantItemButton_OnModifiedClick(self,button)
end
------------------
-- Fenster
------------------
function BuyWin:CreateGUI(f)
y=10
yd=y+30
local frame = BuyWin:CreateWindow(f)
local btn_OK = BuyWin:CreateButton(frame, "btn_OK",BuyWin:GetDisplay("ok"),80,24,"BOTTOM",-50,y)
btn_OK:SetScript("OnClick",function(self) BuyWin:evt_BuyNow() end)
--local btn_break = BuyWin:CreateButton(frame, "btn_break","Abbrechen",80,24,"BOTTOM",50,y)
local btn_break = BuyWin:CreateButton(frame, "btn_break",BuyWin:GetDisplay("cancel"),80,24,"BOTTOM",50,y)
btn_break:SetScript("OnClick",function(self) BuyWin:CloseWindow() end)
local btn_less = BuyWin:CreateButton(frame, "btn_less","<",24,24,"BOTTOM",-80,yd)
btn_less:SetScript("OnClick",function(self) BuyWin:evt_ChangeAmount(-1) end)
local btn_more = BuyWin:CreateButton(frame, "btn_more",">",24,24,"BOTTOM",80,yd)
btn_more:SetScript("OnClick",function(self) BuyWin:evt_ChangeAmount(1) end)
local edt_amount = BuyWin:CreateEdit(frame, "edt_amount",100,24,"BOTTOM",0,yd)
local ico_item = BuyWin:CreateIcon(frame, "ico_item","100",40,40,"TOP",0,y-20)
return frame
end
function BuyWin:CreateWindow(frame)
frame:Hide()
frame:SetPoint("CENTER",0,0) --Mittig setzen BOTTEMLEFT,TOPRIGHT / 0,0 = x und y verscheibung in negativ und Positiv richtung
frame:SetWidth(200) --Fenster breite
frame:SetHeight(120) --Fenster höhe
frame:SetBackdrop({
bgFile = "Interface/Tooltips/UI-Tooltip-Background",
edgeFile = "Interface/Tooltips/UI-Tooltip-Border",
tile = true, tileSize = 16, edgeSize = 16,
insets = { left = 4, right = 4, top = 4, bottom = 4 }
});
frame:SetBackdropColor(0,0,0,1); -- r,g,b, alpha (0-1)
return(frame)
end
function BuyWin:CreateButton(frame,name,text,width,height,position,x,y,template)
if position == nil then
position="TOPLEFT"
end
if template == nil then
template="OptionsButtonTemplate"
end
if x == nil and y == nil then
position="CENTER"
x=0
y=0
elseif x == nil then
position="TOP"
elseif y == nil then
position="LEFT"
end
local button=CreateFrame("Button",name,frame,template)
button:SetPoint(position,x,y) --Mittig setzen BOTTEMLEFT,TOPRIGHT / 0,0 = x und y verscheibung in negativ und Positiv richtung
button:SetWidth(width) --Fenster breite
button:SetHeight(height) --Fenster höhe
button:SetText(text)
return (button)
end
function BuyWin:CreateEdit(frame,name,width,height,position,x,y)
if position == nil then
position="TOPLEFT"
end
if x == nil and y == nil then
position="CENTER"
x=0
y=0
elseif x == nil then
position="TOP"
elseif y == nil then
position="LEFT"
end
local template = "InputBoxTemplate"
local edit = CreateFrame("EditBox",name,frame,template)
edit:SetPoint(position,x,y) --Mittig setzen BOTTEMLEFT,TOPRIGHT / 0,0 = x und y verscheibung in negativ und Positiv richtung
edit:SetWidth(width) --Fenster breite
edit:SetHeight(height) --Fenster höhe
edit:SetAutoFocus(false)
edit:Show()
return (edit)
end
function BuyWin:CreateIcon(frame,name,icon,width,height,position,x,y)
if position == nil then
position="TOPLEFT"
end
if x == nil and y == nil then
position="CENTER"
x=0
y=0
elseif x == nil then
position="TOP"
elseif y == nil then
position="LEFT"
end
local t = frame:CreateTexture(nil,"HIGH")
-- t:SetTexture("Interface\\Glues\\CharacterCreate\\UI-CharacterCreate-Factions.blp")
t:SetPoint(position,x,y) --Mittig setzen BOTTEMLEFT,TOPRIGHT / 0,0 = x und y verscheibung in negativ und Positiv richtung
t:SetWidth(width) --Fenster breite
t:SetHeight(height) --Fenster höhe
frame.texture = t
return (t)
end
function BuyWin:SetIcon(texture)
BuyFrame.texture:SetTexture(texture)
end
-- ---------
-- Events
-- ---------
function BuyWin:evt_ChangeAmount(operator)
local n=edt_amount:GetText()
if not string.match(n,"^[0-9]+$") then
edt_amount:SetText(0)
end
if (edt_amount:GetText()+operator)<0 then
operator=0;
end
edt_amount:SetText(edt_amount:GetText()+operator)
end
function BuyWin:evt_BuyNow()
local amount=edt_amount:GetText()
if not string.match(amount,"^[0-9]+$") then
return
end
if amount == "0" then
return
end
if tradeitem == nil then
return
end
-- confirm
local name=GetMerchantItemInfo(tradeitem)
BuyMerchantItem(tradeitem,amount)
BuyWin:CloseWindow()
return found
end
-- --------------
-- Events an/aus
-- --------------
function BuyWin:Toggle(b)
-- print ("b=" .. b)
-- Toggle ohne Parameter
if b == nil then
if enabled then
b=false
else
b=true
end
end
enabled=b
if b == true then
--Toggle
BuyFrame:RegisterEvent("MERCHANT_SHOW")
BuyFrame:RegisterEvent("MERCHANT_CLOSED")
-- print("BuyWin ist nun aktiv!")
print(BuyWin:GetDisplay("active"))
else
-- Unegister
BuyFrame:UnregisterEvent("MERCHANT_SHOW")
BuyFrame:UnregisterEvent("MERCHANT_CLOSED")
print(BuyWin:GetDisplay("inactive"))
end
end
function SlashCmdList.BUYWIN(msg, editbox)
local cmd, rest = msg:match("^(%S*)%s*(.-)$")
if cmd == "toggle" then
BuyWin:Toggle()
elseif cmd == "on" then
BuyWin:Toggle(true)
elseif cmd == "off" then
BuyWin:Toggle(false)
elseif cmd == "ver" then
name, title, notes, enabled, loadable, reason, security = GetAddOnInfo("BuyWin")
version = GetAddOnMetadata("BuyWin", "Version")
print(BuyWin:GetDisplay("ver") .. version)
else
print(BuyWin:GetDisplay("help"))
print("Syntax:")
print("/BW [toggle|on|off|ver]")
print("/BUYWIN [toggle|on|off|ver]")
end
end
-- ---------
-- Main Part
-- ---------
local frame = CreateFrame("Frame","BuyFrame",UIParent)
frame:SetScript("OnEvent",BuyWin.Init)
frame:RegisterEvent("ADDON_LOADED") -- Beim Starten geladen
frame:RegisterEvent("MERCHANT_SHOW")
frame:RegisterEvent("MERCHANT_CLOSED")