View Single Post
11-05-16, 07:58 PM   #9
FreeXerxes
A Deviate Faerie Dragon
Join Date: Aug 2016
Posts: 10
Thanks for the nice Code. but that is not what i want for my Addon.
Here my complete Code that works fine, that you see what I mean, SDPhantom


There is a trader, wehre you can only buy one item (or the given amount) and you cannot use the SHIFT Click on this trader. I want to enabele this with my own input Window.


Lua Code:
  1. --[[
  2. Lua Fenster erstellen
  3. --]]
  4. -------------------
  5. -- Global Variables
  6. -------------------
  7. SLASH_BUYWIN1 = '/buywin'
  8. SLASH_BUYWIN2 = '/bw'
  9.  
  10. BuyWin = {}
  11. local traderopen=false -- Händler ist offen
  12. local tradeitem=nil    -- Laufende nummer vom Händler
  13. -- local tradeitemtexture = nil -- angeklicktes Item
  14. local enabled=true      -- Addon ist Aktiv
  15.  
  16.  
  17. function BuyWin:GetDisplay(text)
  18.     local displays=
  19.     {
  20.         ["deDE"] = {
  21.             ["ok"] = "OK",
  22.             ["cancel"] = "Abbrechen",
  23.             ["active"] = "BuyWin ist jetzt aktiv!",
  24.             ["inactive"] = "BuyWin ist jetzt inaktiv!",
  25.             ["ver"] = "Du benutzt BuyWin Version ",
  26.             ["help"] = "schaltet das AddOn aus oder an"    
  27.         },
  28.         ["enGB"] = {
  29.             ["ok"] = "OK",
  30.             ["cancel"] = "Cancel",
  31.             ["active"] = "BuyWin is now active!",
  32.             ["inactive"] = "BuyWin is now inactive!",
  33.             ["ver"] = "Your are using BuyWin Version ",
  34.             ["help"] = "switches the AddOn off or on"  
  35.         }
  36.     }
  37.     lang=GetLocale()
  38.     if displays[lang]==nil then
  39.         lang="enGB"
  40.     end
  41.     return  displays[lang][text]
  42. end
  43.  
  44.  
  45. ------------------
  46. -- Display Window
  47. ------------------
  48. function BuyWin:CloseWindow()
  49.     edt_amount:SetText("")
  50.     BuyFrame:Hide()
  51. end
  52.  
  53. function BuyWin:OpenWindow()
  54.     BuyFrame:SetFrameStrata("HIGH")
  55.    
  56.     local x, y = BuyWin:GetFramePosition();    
  57.     BuyFrame:SetPoint("BOTTOMLEFT",x+20,y-120)
  58.    
  59.     BuyFrame:Show()
  60.  
  61. end
  62.  
  63. function BuyWin:GetFramePosition()
  64.     local x, y = GetCursorPosition();      
  65.     x=  x*UIParent:GetWidth()/WorldFrame:GetWidth()
  66.     y=  y*UIParent:GetHeight()/WorldFrame:GetHeight()
  67.     return x,y
  68. end
  69.  
  70. ------------------
  71. -- Setup
  72. ------------------
  73. function BuyWin:Init(event, addon)     
  74.     if event == "ADDON_LOADED" and addon == "BuyWin" then
  75.         BuyWin:CreateGUI(BuyFrame)
  76.     end
  77.     if event == "MERCHANT_SHOW" then
  78.         edt_amount:SetText("")
  79.         traderopen=true
  80.     end
  81.     if event=="MERCHANT_CLOSED" then
  82.         BuyWin:CloseWindow()
  83.         traderopen=false;
  84.     end
  85.    
  86.     return 
  87. end
  88.  
  89.  
  90.  
  91.  
  92. ------------------
  93. -- Hook Merchant
  94. ------------------
  95.  
  96. local org_MerchantItemButton_OnModifiedClick = MerchantItemButton_OnModifiedClick;
  97. function MerchantItemButton_OnModifiedClick(self,button)  -- Buttonframe, used Button: LeftButton/RightButton
  98.     if ( MerchantFrame.selectedTab == 1 ) and IsShiftKeyDown() and enabled and GetCurrentKeyBoardFocus()==nil then 
  99.         local mouseFocus=GetMouseFocus()
  100.         tradeitem     = mouseFocus:GetID() --Laufende Nummer
  101.         name, texture, price, quantity, numAvailable, isUsable, extendedCost = GetMerchantItemInfo(tradeitem)
  102.         BuyWin:SetIcon(texture)
  103.         BuyWin:OpenWindow()
  104.  
  105.         return
  106.     end    
  107.     return org_MerchantItemButton_OnModifiedClick(self,button)
  108. end
  109.  
  110. ------------------
  111. -- Fenster
  112. ------------------
  113. function BuyWin:CreateGUI(f)
  114.     y=10
  115.     yd=y+30
  116.     local frame        = BuyWin:CreateWindow(f)
  117.  
  118.     local btn_OK       = BuyWin:CreateButton(frame, "btn_OK",BuyWin:GetDisplay("ok"),80,24,"BOTTOM",-50,y)
  119.     btn_OK:SetScript("OnClick",function(self) BuyWin:evt_BuyNow()  end)
  120.  
  121.     --local btn_break    = BuyWin:CreateButton(frame, "btn_break","Abbrechen",80,24,"BOTTOM",50,y)
  122.     local btn_break    = BuyWin:CreateButton(frame, "btn_break",BuyWin:GetDisplay("cancel"),80,24,"BOTTOM",50,y)
  123.     btn_break:SetScript("OnClick",function(self) BuyWin:CloseWindow() end)
  124.  
  125.     local btn_less     = BuyWin:CreateButton(frame, "btn_less","<",24,24,"BOTTOM",-80,yd)
  126.     btn_less:SetScript("OnClick",function(self) BuyWin:evt_ChangeAmount(-1) end)
  127.  
  128.     local btn_more     = BuyWin:CreateButton(frame, "btn_more",">",24,24,"BOTTOM",80,yd)
  129.     btn_more:SetScript("OnClick",function(self) BuyWin:evt_ChangeAmount(1) end)
  130.  
  131.     local edt_amount   = BuyWin:CreateEdit(frame, "edt_amount",100,24,"BOTTOM",0,yd)   
  132.    
  133.     local ico_item     = BuyWin:CreateIcon(frame, "ico_item","100",40,40,"TOP",0,y-20)
  134.     return frame
  135. end
  136.  
  137. function BuyWin:CreateWindow(frame)
  138.     frame:Hide()
  139.     frame:SetPoint("CENTER",0,0)    --Mittig setzen BOTTEMLEFT,TOPRIGHT / 0,0  = x und y verscheibung in negativ und Positiv richtung
  140.     frame:SetWidth(200)                   --Fenster breite
  141.     frame:SetHeight(120)                  --Fenster höhe
  142.     frame:SetBackdrop({
  143.         bgFile = "Interface/Tooltips/UI-Tooltip-Background",
  144.         edgeFile = "Interface/Tooltips/UI-Tooltip-Border",
  145.         tile = true, tileSize = 16, edgeSize = 16,
  146.         insets = { left = 4, right = 4, top = 4, bottom = 4 }
  147.     });
  148.     frame:SetBackdropColor(0,0,0,1);    -- r,g,b, alpha (0-1)
  149.     return(frame)
  150. end
  151.  
  152.  
  153. function BuyWin:CreateButton(frame,name,text,width,height,position,x,y,template)
  154.     if position == nil then
  155.         position="TOPLEFT"
  156.     end
  157.    
  158.     if template == nil then
  159.         template="OptionsButtonTemplate"
  160.     end
  161.     if x == nil and y == nil then
  162.         position="CENTER"
  163.         x=0
  164.         y=0
  165.     elseif x == nil then
  166.         position="TOP"
  167.     elseif y == nil then
  168.         position="LEFT"
  169.     end
  170.    
  171.    
  172.    
  173.     local button=CreateFrame("Button",name,frame,template)
  174.     button:SetPoint(position,x,y)    --Mittig setzen BOTTEMLEFT,TOPRIGHT / 0,0  = x und y verscheibung in negativ und Positiv richtung
  175.     button:SetWidth(width)                   --Fenster breite
  176.     button:SetHeight(height)                  --Fenster höhe
  177.     button:SetText(text)
  178.     return (button)
  179. end
  180.  
  181. function BuyWin:CreateEdit(frame,name,width,height,position,x,y)
  182.     if position == nil then
  183.         position="TOPLEFT"
  184.     end
  185.    
  186.     if x == nil and y == nil then
  187.         position="CENTER"
  188.         x=0
  189.         y=0
  190.     elseif x == nil then
  191.         position="TOP"
  192.     elseif y == nil then
  193.         position="LEFT"
  194.     end
  195.        
  196.     local template = "InputBoxTemplate"
  197.     local edit = CreateFrame("EditBox",name,frame,template)
  198.    
  199.     edit:SetPoint(position,x,y)    --Mittig setzen BOTTEMLEFT,TOPRIGHT / 0,0  = x und y verscheibung in negativ und Positiv richtung
  200.     edit:SetWidth(width)                   --Fenster breite
  201.     edit:SetHeight(height)                  --Fenster höhe
  202.     edit:SetAutoFocus(false)
  203.     edit:Show()
  204.     return (edit)
  205. end
  206.  
  207. function BuyWin:CreateIcon(frame,name,icon,width,height,position,x,y)
  208.     if position == nil then
  209.         position="TOPLEFT"
  210.     end
  211.    
  212.     if x == nil and y == nil then
  213.         position="CENTER"
  214.         x=0
  215.         y=0
  216.     elseif x == nil then
  217.         position="TOP"
  218.     elseif y == nil then
  219.         position="LEFT"
  220.     end
  221.  
  222.     local t = frame:CreateTexture(nil,"HIGH")
  223.     -- t:SetTexture("Interface\\Glues\\CharacterCreate\\UI-CharacterCreate-Factions.blp")
  224.     t:SetPoint(position,x,y)    --Mittig setzen BOTTEMLEFT,TOPRIGHT / 0,0  = x und y verscheibung in negativ und Positiv richtung
  225.     t:SetWidth(width)                   --Fenster breite
  226.     t:SetHeight(height)                  --Fenster höhe
  227.     frame.texture = t
  228.    
  229.     return (t)
  230. end
  231.  
  232. function BuyWin:SetIcon(texture)
  233.     BuyFrame.texture:SetTexture(texture)
  234. end
  235.  
  236. -- ---------
  237. -- Events
  238. -- ---------
  239. function BuyWin:evt_ChangeAmount(operator)
  240.     local n=edt_amount:GetText()
  241.     if not string.match(n,"^[0-9]+$") then
  242.         edt_amount:SetText(0)
  243.     end
  244.    
  245.     if (edt_amount:GetText()+operator)<0 then
  246.         operator=0;
  247.     end
  248.    
  249.     edt_amount:SetText(edt_amount:GetText()+operator)
  250. end
  251.    
  252. function BuyWin:evt_BuyNow()
  253.  
  254.     local amount=edt_amount:GetText()
  255.     if not string.match(amount,"^[0-9]+$") then
  256.         return
  257.     end
  258.     if amount == "0" then
  259.         return
  260.     end
  261.     if tradeitem == nil then
  262.         return
  263.     end
  264.    
  265.     -- confirm 
  266.     local name=GetMerchantItemInfo(tradeitem)  
  267.     BuyMerchantItem(tradeitem,amount)
  268.     BuyWin:CloseWindow()
  269.     return found
  270.    
  271. end
  272.  
  273. -- --------------
  274. -- Events an/aus
  275. -- --------------
  276. function BuyWin:Toggle(b)
  277. -- print ("b=" .. b)
  278.  
  279.     -- Toggle ohne Parameter
  280.     if b == nil then
  281.         if enabled then
  282.             b=false
  283.         else
  284.             b=true
  285.         end
  286.     end
  287.  
  288.     enabled=b
  289.    
  290.     if b == true then
  291.         --Toggle
  292.         BuyFrame:RegisterEvent("MERCHANT_SHOW")
  293.         BuyFrame:RegisterEvent("MERCHANT_CLOSED")
  294.         -- print("BuyWin ist nun aktiv!")
  295.        
  296.         print(BuyWin:GetDisplay("active"))
  297.     else
  298.         -- Unegister
  299.         BuyFrame:UnregisterEvent("MERCHANT_SHOW")
  300.         BuyFrame:UnregisterEvent("MERCHANT_CLOSED")
  301.  
  302.         print(BuyWin:GetDisplay("inactive"))
  303.     end
  304. end
  305.  
  306. function SlashCmdList.BUYWIN(msg, editbox)
  307.     local cmd, rest = msg:match("^(%S*)%s*(.-)$")
  308.    
  309.     if cmd == "toggle" then
  310.         BuyWin:Toggle()
  311.     elseif cmd == "on" then
  312.         BuyWin:Toggle(true)
  313.     elseif cmd == "off" then
  314.         BuyWin:Toggle(false)
  315.     elseif cmd == "ver" then
  316.         name, title, notes, enabled, loadable, reason, security = GetAddOnInfo("BuyWin")
  317.         version = GetAddOnMetadata("BuyWin", "Version")
  318.         print(BuyWin:GetDisplay("ver") .. version)
  319.     else
  320.         print(BuyWin:GetDisplay("help"))
  321.         print("Syntax:")
  322.         print("/BW [toggle|on|off|ver]")
  323.         print("/BUYWIN [toggle|on|off|ver]")
  324.     end
  325.    
  326. end
  327.  
  328.  
  329. -- ---------
  330. -- Main Part
  331. -- ---------
  332.  
  333. local frame = CreateFrame("Frame","BuyFrame",UIParent)
  334. frame:SetScript("OnEvent",BuyWin.Init)
  335. frame:RegisterEvent("ADDON_LOADED") -- Beim Starten geladen
  336. frame:RegisterEvent("MERCHANT_SHOW")
  337. frame:RegisterEvent("MERCHANT_CLOSED")
  Reply With Quote