Advertisement
Guest User

Spells.Lua

a guest
Sep 29th, 2015
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 30.21 KB | None | 0 0
  1. -- Spells.lua
  2.  
  3. -- Adds a spell predicter to aceconfig.  This work is from AceGUI-3.0-Spell-EditBox
  4. -- changed to a non lib to work in my addon.
  5. -- Further modifications were done.
  6. -- This version returns the Name and SpellID
  7. -------------------------------------------------------------------------------
  8. -- GUB   shared data table between all parts of the addon
  9. -------------------------------------------------------------------------------
  10. local MyAddon, GUB = ...
  11. local Main = GUB.Main
  12.  
  13. local AceGUI = LibStub('AceGUI-3.0')
  14.  
  15. -- localize some globals.
  16. local GetSpellInfo, SPELL_PASSIVE, ipairs, pairs, type, CreateFrame, select, floor, strlower, strfind, format, tinsert, print, sort =
  17.       GetSpellInfo, SPELL_PASSIVE, ipairs, pairs, type, CreateFrame, select, floor, strlower, strfind, format, tinsert, print, sort
  18. local table, GameTooltip, ClearOverrideBindings, SetOverrideBindingClick, GetCursorInfo, GetSpellBookItemName =
  19.       table, GameTooltip, ClearOverrideBindings, SetOverrideBindingClick, GetCursorInfo, GetSpellBookItemName
  20. local ClearCursor, GameTooltip, UIParent, GameFontHighlight, GameFontNormal, ChatFontNormal, OKAY =
  21.       ClearCursor, GameTooltip, UIParent, GameFontHighlight, GameFontNormal, ChatFontNormal, OKAY
  22.  
  23. -------------------------------------------------------------------------------
  24. -- Locals
  25. --
  26. -- SpellList         Contains a list of loaded spells used in the editbox.
  27. -- SpellsLoaded      if true then spells are already loaded.
  28. -- SpellsPerRun      Amount of spells to load at one time.
  29. -- Predictorlines    Amount of lines the predictor uses.
  30. -- Predictors        Table that keeps track of predictor frames.
  31. --                   The keyname is the Frame and the value is true or nil
  32. -------------------------------------------------------------------------------
  33. local SpellsTPS = 0.10 -- 10 times per second.
  34. local SpellsPerRun = 1000
  35. local SpellsLoaded = false
  36. local Tooltip = nil
  37. local HyperLinkSt = 'spell:%s'
  38.  
  39. local PredictorLines = 100
  40. local MenuLines = 10
  41. local MenuFrameWidth = 200
  42. local EditBoxWidgetVersion = 1
  43. local AuraEditBoxWidgetVersion = 1
  44.  
  45. local EditBoxWidgetType = 'GUB_Predictor_Base'
  46. local AuraEditBoxWidgetType = 'GUB_Aura_EditBox'
  47.  
  48. local SpellsTimer = {}
  49. local SpellList = {}
  50. local Predictors = {}
  51. local SpellFilterCache = {}
  52.  
  53. local PredictorBackdrop = {
  54.   bgFile   = [[Interface\ChatFrame\ChatFrameBackground]],
  55.   edgeFile = [[Interface\DialogFrame\UI-DialogBox-Border]],
  56.   edgeSize = 26,
  57.   insets = {
  58.     left = 9 ,
  59.     right = 9,
  60.     top = 9,
  61.     bottom = 9,
  62.   },
  63. }
  64.  
  65. local SliderBackdrop = {
  66.   bgFile = [[Interface\Buttons\UI-SliderBar-Background]],
  67.   edgeFile = [[Interface\Buttons\UI-SliderBar-Border]],
  68.   tile = true,
  69.   edgeSize = 8,
  70.   tileSize = 8,
  71.   insets = {
  72.     left = 3,
  73.     right = 3,
  74.     top = 3,
  75.     bottom = 3,
  76.   },
  77. }
  78.  
  79. --*****************************************************************************
  80. --
  81. -- Spell utility
  82. --
  83. --*****************************************************************************
  84.  
  85. -------------------------------------------------------------------------------
  86. -- RegisterSpellPredictor
  87. --
  88. -- Frame    Frame that will contain the predicted spells
  89. -------------------------------------------------------------------------------
  90. local function RegisterSpellPredictor(Frame)
  91.   Predictors[Frame] = true
  92. end
  93.  
  94. -------------------------------------------------------------------------------
  95. -- UnegisterSpellPredictor
  96. --
  97. -- Frame    Frame that will no longer contain the predicted spells
  98. -------------------------------------------------------------------------------
  99. local function UnregisterSpellPredictor(Frame)
  100.   Predictors[Frame] = nil
  101. end
  102.  
  103. -------------------------------------------------------------------------------
  104. -- LoadSpells
  105. --
  106. -- Loads spells just once.  This is used for the predictor.
  107. -------------------------------------------------------------------------------
  108. local function LoadSpells()
  109.   if not SpellsLoaded then
  110.     local TotalInvalid = 0
  111.     local CurrentIndex = 0
  112.     local NumSpells = 0
  113.  
  114.     local Exclude = {
  115.       ['interface\\icons\\trade_alchemy'] = true,
  116.       ['interface\\icons\\trade_blacksmithing'] = true,
  117.       ['interface\\icons\\trade_brewpoison'] = true,
  118.       ['interface\\icons\\trade_engineering'] = true,
  119.       ['interface\\icons\\trade_engraving'] = true,
  120.       ['interface\\icons\\trade_fishing'] = true,
  121.       ['interface\\icons\\trade_herbalism'] = true,
  122.       ['interface\\icons\\trade_leatherworking'] = true,
  123.       ['interface\\icons\\trade_mining'] = true,
  124.       ['interface\\icons\\trade_tailoring'] = true,
  125.       ['interface\\icons\\temp'] = true,
  126.     }
  127.  
  128.     local ScanTooltip = CreateFrame('GameTooltip')
  129.  
  130.     ScanTooltip:SetOwner(UIParent, 'ANCHOR_NONE')
  131.     for i = 1, 6 do
  132.       ScanTooltip['TextLeft' .. i] = ScanTooltip:CreateFontString()
  133.       ScanTooltip['TextRight' .. i] = ScanTooltip:CreateFontString()
  134.       ScanTooltip:AddFontStrings(ScanTooltip['TextLeft' .. i], ScanTooltip['TextRight' .. i])
  135.     end
  136.  
  137.     local function LoadSpells(self)
  138.  
  139.       -- 5,000 invalid spells in a row means it's a safe assumption that there are no more spells to query
  140.       if TotalInvalid >= 5000 then
  141.         Main:SetTimer(self, nil)
  142.         return
  143.       end
  144.  
  145.       -- Load as many spells in
  146.       for SpellID = CurrentIndex + 1, CurrentIndex + SpellsPerRun do
  147.         local Name, SubName, Icon = GetSpellInfo(SpellID)
  148.         local IsAura = false
  149.  
  150.         -- Pretty much every profession spell uses Trade_* and 99% of the random spells use the Trade_Engineering icon
  151.         -- we can safely exclude any of these spells as they are not needed. Can get away with this because things like
  152.         -- Alchemy use two icons, the Trade_* for the actual crafted spell and a different icon for the actual buff
  153.         -- Passive spells have no use as well, since they are well passive and can't actually be used
  154.         if Name and Name ~= '' and Exclude[strlower(Icon)] == nil and SubName ~= SPELL_PASSIVE then
  155.  
  156.           -- Scan tooltip for debuff/buff
  157.           ScanTooltip:SetHyperlink(format(HyperLinkSt, SpellID))
  158.  
  159.           IsAura = true
  160.           for i = 1, ScanTooltip:NumLines() do
  161.             local Text = ScanTooltip['TextLeft' .. i]
  162.  
  163.             if Text and false then
  164.               local r, g, b = Text:GetTextColor()
  165.  
  166.               r = floor(r + 0.10)
  167.               g = floor(g + 0.10)
  168.               b = floor(b + 0.10)
  169.  
  170.               -- Gold first text, it's a profession link
  171.               -- If first line is not white then reject it.
  172.               if i == 1 and (r ~= 1 or g ~= 1 or b ~= 1) then
  173.                 break
  174.  
  175.               -- Gold for anything else and it should be a valid aura
  176.               -- line 2 or after is not white except it.
  177.               elseif r ~= 1 or g ~= 1 or b ~= 1 then
  178.                 IsAura = true
  179.                 break
  180.               end
  181.             end
  182.           end
  183.         end
  184.  
  185.         if IsAura then
  186.           NumSpells = NumSpells + 1
  187.           SpellList[SpellID] = strlower(Name)
  188.  
  189.           TotalInvalid = 0
  190.         else
  191.           TotalInvalid = TotalInvalid + 1
  192.         end
  193.       end
  194.  
  195.       -- Every ~1 second it will update any visible predictors to make up for the fact that the data is delay loaded
  196.       if CurrentIndex % 5000 == 0 then
  197.         for Frame in pairs(Predictors) do
  198.           if Frame:IsVisible() then
  199.             Frame:PopulatePredictor()
  200.           end
  201.         end
  202.       end
  203.  
  204.       -- Increment and do it all over!
  205.       CurrentIndex = CurrentIndex + SpellsPerRun
  206.     end
  207.  
  208.     Main:SetTimer(SpellsTimer, LoadSpells, SpellsTPS)
  209.     SpellsLoaded = true
  210.   end
  211. end
  212.  
  213. --*****************************************************************************
  214. --
  215. -- Editbox for the predictor
  216. --
  217. --*****************************************************************************
  218. local function HideScroller(PredictFrame, Hide)
  219.   local ScrollFrame = PredictFrame.ScrollFrame
  220.   local Scroller = PredictFrame.Scroller
  221.  
  222.   if Hide then
  223.     Scroller:SetValue(0)
  224.     Scroller:Hide()
  225.     ScrollFrame:SetPoint('BOTTOMRIGHT', -9, 6)
  226.   else
  227.     Scroller:Show()
  228.     ScrollFrame:SetPoint('TOPLEFT', 0, -10)
  229.     ScrollFrame:SetPoint('BOTTOMRIGHT', -28, 10)
  230.   end
  231. end
  232.  
  233. ------------------------------------------------------------------------------
  234. -- OnAcquire
  235. --
  236. -- Gets called after a new widget is created or reused.
  237. ------------------------------------------------------------------------------
  238. local function OnAcquire(self)
  239.   self:SetHeight(26)
  240.   self:SetWidth(200)
  241.   self:SetDisabled(false)
  242.   self:SetLabel()
  243.   self.showButton = true
  244.  
  245.   RegisterSpellPredictor(self.PredictFrame)
  246.   LoadSpells()
  247. end
  248.  
  249. ------------------------------------------------------------------------------
  250. -- OnRelease
  251. --
  252. -- Gets called when the widget is released
  253. ------------------------------------------------------------------------------
  254. local function OnRelease(self)
  255.   local Frame = self.frame
  256.  
  257.   Frame:ClearAllPoints()
  258.   Frame:Hide()
  259.   self.PredictFrame.MenuFrame:Hide()
  260.   self.SpellFilter = nil
  261.  
  262.   self:SetDisabled(false)
  263.  
  264.   UnregisterSpellPredictor(self.PredictFrame)
  265. end
  266.  
  267. -------------------------------------------------------------------------------
  268. -- EditBoxOnEnter
  269. --
  270. -- Gets called when the mouse enters the edit box.
  271. -------------------------------------------------------------------------------
  272. local function EditBoxOnEnter(self)
  273.   self.Widget:Fire('OnEnter')
  274. end
  275.  
  276. -------------------------------------------------------------------------------
  277. -- EditBoxOnLeave
  278. --
  279. -- Gets called when the mouse enters the edit box.
  280. -------------------------------------------------------------------------------
  281. local function EditBoxOnLeave(self)
  282.   self.Widget:Fire('OnLeave')
  283. end
  284.  
  285. -------------------------------------------------------------------------------
  286. -- PopulatePredictorFrame
  287. --
  288. -- Populates the predictorframe using a SpellList table and SearchSt
  289. --
  290. -- Type    Type of spelllist
  291. --            'spells'   Came from SpellList
  292. --            'auras'  Came from TrackedAurasList
  293. -------------------------------------------------------------------------------
  294.  
  295. -------------------------------------------------------------------------------
  296. -- AddPredictorButton
  297. --
  298. -- Adds a button to the predictor frame
  299. --
  300. -- ActiveButton    Button position to add one at.
  301. -- FormatText      Format string
  302. -- SpellID         SpellID to add to button
  303. -------------------------------------------------------------------------------
  304. local function AddPredictorButton(self, ActiveButton, FormatText, SpellID)
  305.  
  306.   -- Ran out of text to suggest :<
  307.   local Button = self.Buttons[ActiveButton]
  308.   local Name, _, Icon = GetSpellInfo(SpellID)
  309.  
  310.   Button:SetFormattedText(FormatText, Icon, Name)
  311.   Button.SpellID = SpellID
  312.   Button:Show()
  313.  
  314.   -- Highlight if needed
  315.   if ActiveButton ~= self.SelectedButton then
  316.     Button:UnlockHighlight()
  317.  
  318.     if GameTooltip:IsOwned(Button) then
  319.       GameTooltip:Hide()
  320.     end
  321.   end
  322. end
  323.  
  324. -------------------------------------------------------------------------------
  325. -- PopulatePredictor
  326. --
  327. -- Populates the predictor with a list of spells matching the spell name entered.
  328. -------------------------------------------------------------------------------
  329. local function SortMatches(a, b)
  330.    return SpellList[a] < SpellList[b]
  331. end
  332.  
  333. local function PopulatePredictor(self)
  334.   local Widget = self.Widget
  335.   local SearchSt = strlower(Widget.EditBox:GetText())
  336.   local TrackedAurasList = Main.TrackedAurasList
  337.   local Matches = {}
  338.   local ActiveButtons = 0
  339.  
  340.   for _, Button in pairs(self.Buttons) do
  341.     Button:Hide()
  342.   end
  343.  
  344.   -- Do auras
  345.   if TrackedAurasList then
  346.     for SpellID, Aura in pairs(TrackedAurasList.All) do
  347.       local Name = strlower(GetSpellInfo(SpellID))
  348.  
  349.       if strfind(Name, SearchSt, 1) == 1 then
  350.         if ActiveButtons < PredictorLines then
  351.           ActiveButtons = ActiveButtons + 1
  352.           AddPredictorButton(self, ActiveButtons, '|T%s:15:15:2:11|t |cFFFFFFFF%s|r', SpellID)
  353.         else
  354.           break
  355.         end
  356.       end
  357.     end
  358.   end
  359.  
  360.   -- Do SpellList
  361.   for SpellID, Name in pairs(SpellList) do
  362.     if strfind(Name, SearchSt, 1) == 1 then
  363.       local Found = TrackedAurasList and TrackedAurasList[SpellID] or nil
  364.  
  365.       Matches[#Matches + 1] = SpellID
  366.     end
  367.   end
  368.  
  369.   -- Sort only the spells from the SpellList
  370.   sort(Matches, SortMatches)
  371.  
  372.   for _, SpellID in ipairs(Matches) do
  373.     if ActiveButtons < PredictorLines then
  374.       ActiveButtons = ActiveButtons + 1
  375.       AddPredictorButton(self, ActiveButtons, '|T%s:15:15:2:11|t %s', SpellID)
  376.     else
  377.       break
  378.     end
  379.   end
  380.  
  381.   -- Set the size of the menu.
  382.   if ActiveButtons > 0 then
  383.     if ActiveButtons <= MenuLines then
  384.       self.MenuFrame:SetHeight(19 + ActiveButtons * 17)
  385.       HideScroller(self, true)
  386.     else
  387.       self.MenuFrame:SetHeight(19 + MenuLines * 17)
  388.       self.Scroller:SetMinMaxValues(1, 18 + (ActiveButtons - MenuLines - 1) * 17)
  389.       HideScroller(self, false)
  390.     end
  391.     self.MenuFrame:Show()
  392.   else
  393.     self.MenuFrame:Hide()
  394.   end
  395.  
  396.   self.ActiveButtons = ActiveButtons
  397. end
  398.  
  399. -------------------------------------------------------------------------------
  400. -- PredictorShowButton
  401. --
  402. -- Shows the okay button in the editbox selector
  403. -------------------------------------------------------------------------------
  404. local function PredictorShowButton(self)
  405.   if self.LastText ~= '' then
  406.     self.PredictFrame.SelectedButton = nil
  407.     PopulatePredictor(self.PredictFrame)
  408.   else
  409.     self.PredictFrame.MenuFrame:Hide()
  410.   end
  411.  
  412.   if self.showButton then
  413.     self.Button:Show()
  414.     self.EditBox:SetTextInsets(0, 20, 3, 3)
  415.   end
  416. end
  417.  
  418. -------------------------------------------------------------------------------
  419. -- PredictorHideButton
  420. --
  421. -- Hides the okay button in the editbox selector
  422. -------------------------------------------------------------------------------
  423. local function PredictorHideButton(self)
  424.   self.Button:Hide()
  425.   self.EditBox:SetTextInsets(0, 0, 3, 3)
  426.  
  427.   self.PredictFrame.SelectedButton = nil
  428.   self.PredictFrame.MenuFrame:Hide()
  429. end
  430.  
  431. -------------------------------------------------------------------------------
  432. -- PredictorOnShow
  433. --
  434. -- Hides the predictor editbox and restores binds, tooltips
  435. -------------------------------------------------------------------------------
  436. local function PredictorOnShow(self)
  437.   if self.EditBox:GetText() ~= '' then
  438.     self.MenuFrame:Show()
  439.   end
  440. end
  441.  
  442. -------------------------------------------------------------------------------
  443. -- PredictorOnHide
  444. --
  445. -- Hides the predictor editbox and restores binds, tooltips
  446. -------------------------------------------------------------------------------
  447. local function PredictorOnHide(self)
  448.  
  449.   -- Allow users to use arrows to go back and forth again without the fix
  450.   self.Widget.EditBox:SetAltArrowKeyMode(false)
  451.  
  452.   -- Make sure the tooltip isn't kept open if one of the buttons was using it
  453.   for _, Button in pairs(self.Buttons) do
  454.     if GameTooltip:IsOwned(Button) then
  455.       GameTooltip:Hide()
  456.     end
  457.   end
  458.  
  459.   self.SelectedButton = nil
  460.   self.MenuFrame:Hide()
  461.  
  462.  
  463.   -- Reset all bindings set on this predictor
  464.   ClearOverrideBindings(self)
  465. end
  466.  
  467. -------------------------------------------------------------------------------
  468. -- EditBoxOnEnterPressed
  469. --
  470. -- Gets called when something is entered into the edit box
  471. -------------------------------------------------------------------------------
  472. local function EditBoxOnEnterPressed(self)
  473.   local Widget = self.Widget
  474.   local PredictFrame = Widget.PredictFrame
  475.  
  476.   -- Something is selected in the predictor, use that value instead of whatever is in the input box
  477.   if PredictFrame.SelectedButton then
  478.     PredictFrame.Buttons[Widget.PredictFrame.SelectedButton]:Click()
  479.     return
  480.   end
  481.  
  482.   local cancel = Widget:Fire('OnEnterPressed', self:GetText())
  483.   if not cancel then
  484.     PredictorHideButton(Widget)
  485.   end
  486.  
  487.   -- Reactive the cursor, odds are if someone is adding spells they are adding more than one
  488.   -- and if they aren't, it can't hurt anyway.
  489.   -- Widget.EditBox:SetFocus()
  490. end
  491.  
  492. -------------------------------------------------------------------------------
  493. -- EditBoxOnEscapePressed
  494. --
  495. -- Gets called when esckey is pressed which clears the focus
  496. -------------------------------------------------------------------------------
  497. local function EditBoxOnEscapePressed(self)
  498.   self:ClearFocus()
  499. end
  500.  
  501. -------------------------------------------------------------------------------
  502. -- EditBoxFixCursorPosition
  503. --
  504. -- When using SetAltArrowKeyMode the ability to move the cursor with left and right arrows is disabled
  505. -- this reenables that so the user doesn't notice anything wrong
  506. -------------------------------------------------------------------------------
  507. local function EditBoxFixCursorPosition(self, Direction)
  508.   self:SetCursorPosition(self:GetCursorPosition() + (Direction == 'RIGHT' and 1 or -1))
  509. end
  510.  
  511. -------------------------------------------------------------------------------
  512. -- EditBoxOnReceiveDrag
  513. --
  514. -- Gets called when a button is selected.
  515. -------------------------------------------------------------------------------
  516. local function EditBoxOnReceiveDrag(self)
  517.   local Widget = self.Widget
  518.   local Type, ID, Info = GetCursorInfo()
  519.  
  520.   ClearCursor()
  521.  
  522.   PredictorHideButton(Widget)
  523.   AceGUI:ClearFocus()
  524. end
  525.  
  526. -------------------------------------------------------------------------------
  527. -- EditBoxOnTextChanged
  528. --
  529. -- Gets called when the text changes in the edit box.
  530. -------------------------------------------------------------------------------
  531. local function EditBoxOnTextChanged(self)
  532.   local Widget = self.Widget
  533.   local Value = self:GetText()
  534.  
  535.   if Value ~= Widget.LastText then
  536.     Widget:Fire('OnTextChanged', Value)
  537.     Widget.LastText = Value
  538.  
  539.     PredictorShowButton(Widget)
  540.   end
  541. end
  542.  
  543. -------------------------------------------------------------------------------
  544. -- EditBoxOnFocusGained
  545. --
  546. -- Gets called when the edit box loses focus
  547. -------------------------------------------------------------------------------
  548. local function EditBoxOnEditFocusGained(self)
  549.   PredictorOnShow(self.Widget.PredictFrame)
  550. end
  551.  
  552. -------------------------------------------------------------------------------
  553. -- EditBoxOnFocusLost
  554. --
  555. -- Gets called when the edit box loses focus
  556. -------------------------------------------------------------------------------
  557. local function EditBoxOnEditFocusLost(self)
  558.   PredictorOnHide(self.Widget.PredictFrame)
  559. end
  560.  
  561. -------------------------------------------------------------------------------
  562. -- EditBoxButtonOnclick
  563. --
  564. -- called when the 'edit' button in the edit box is clicked
  565. -------------------------------------------------------------------------------
  566. local function EditBoxButtonOnClick(self)
  567.   EditBoxOnEnterPressed(self.Widget.EditBox)
  568. end
  569.  
  570. --*****************************************************************************
  571. --
  572. -- Editbox for the predictor
  573. -- API calls
  574. --
  575. --*****************************************************************************
  576.  
  577. -------------------------------------------------------------------------------
  578. -- EditBoxSetDisabled
  579. --
  580. -- Disables the edit box
  581. -------------------------------------------------------------------------------
  582. local function EditBoxSetDisabled(self, Disabled)
  583.   local EditBox = self.EditBox
  584.  
  585.   self.disabled = Disabled
  586.  
  587.   if Disabled then
  588.     EditBox:EnableMouse(false)
  589.     EditBox:ClearFocus()
  590.     EditBox:SetTextColor(0.5, 0.5, 0.5)
  591.     self.Label:SetTextColor(0.5, 0.5, 0.5)
  592.   else
  593.     EditBox:EnableMouse(true)
  594.     EditBox:SetTextColor(1, 1, 1)
  595.     self.Label:SetTextColor(1, 0.82, 0)
  596.   end
  597. end
  598.  
  599. -------------------------------------------------------------------------------
  600. -- EditBoxSetText
  601. --
  602. -- Changes the text in the edit box
  603. -------------------------------------------------------------------------------
  604. local function EditBoxSetText(self, Text, Cursor)
  605.   local EditBox = self.EditBox
  606.  
  607.   self.LastText = Text or ''
  608.   EditBox:SetText(self.LastText)
  609.   EditBox:SetCursorPosition(Cursor or 0)
  610.  
  611.   PredictorHideButton(self)
  612. end
  613.  
  614. -------------------------------------------------------------------------------
  615. -- EditBoxSetLabel
  616. --
  617. -- Sets the label on the edit box.
  618. -------------------------------------------------------------------------------
  619. local function EditBoxSetLabel(self, Text)
  620.   local Label = self.Label
  621.  
  622.   if Text and Text ~= '' then
  623.     Label:SetText(Text)
  624.     Label:Show()
  625.     self.EditBox:SetPoint('TOPLEFT', self.frame, 'TOPLEFT', 7, -18)
  626.     self:SetHeight(44)
  627.     self.alignoffset = 30
  628.   else
  629.     Label:SetText('')
  630.     Label:Hide()
  631.     self.EditBox:SetPoint('TOPLEFT', self.frame, 'TOPLEFT', 7, 0)
  632.     self:SetHeight(26)
  633.     self.alignoffset = 12
  634.   end
  635. end
  636.  
  637. -------------------------------------------------------------------------------
  638. -- PredictorOnMouseDown
  639. --
  640. -- Gets called when the mouse is clicked on the predictor.
  641. -------------------------------------------------------------------------------
  642. local function PredictorOnMouseDown(self, Direction)
  643.  
  644.   -- Fix the cursor positioning if left or right arrow key was used
  645.   if Direction == 'LEFT' or Direction == 'RIGHT' then
  646.     EditBoxFixCursorPosition(self.EditBox, Direction)
  647.   end
  648. end
  649.  
  650. -------------------------------------------------------------------------------
  651. -- PredictorButtonOnClick
  652. --
  653. -- Sets the editbox to the button that was clicked in the selector
  654. -------------------------------------------------------------------------------
  655. local function PredictorButtonOnClick(self)
  656.   local Name = GetSpellInfo(self.SpellID)
  657.   local Parent = self.parent
  658.  
  659.   EditBoxSetText(self.parent.Widget, Name, #Name)
  660.  
  661.   Parent.SelectedButton = nil
  662.   Parent.Widget:Fire('OnEnterPressed', Name, self.SpellID)
  663. end
  664.  
  665. -------------------------------------------------------------------------------
  666. -- PredictorButtonOnEnter
  667. --
  668. -- Highlights the predictor button when the mouse enters the button area
  669. -------------------------------------------------------------------------------
  670. local function PredictorButtonOnEnter(self)
  671.   self.parent.SelectedButton = nil
  672.   self:LockHighlight()
  673.   local SpellID = self.SpellID
  674.  
  675.   GameTooltip:SetOwner(self, 'ANCHOR_BOTTOMRIGHT', 3)
  676.   GameTooltip:SetHyperlink(format(HyperLinkSt, SpellID))
  677.   GameTooltip:AddLine(format('|cFFFFFF00SpellID:|r|cFF00FF00%s|r', SpellID))
  678.  
  679.   -- Need to show to make sure the tooltip surrounds the AddLine text
  680.   -- after SetHyperlink
  681.   GameTooltip:Show()
  682. end
  683.  
  684. -------------------------------------------------------------------------------
  685. -- PredictorButtonOnLeave
  686. --
  687. -- Highlights the predictor button when the mouse enters the button area
  688. -------------------------------------------------------------------------------
  689. local function PredictorButtonOnLeave(self)
  690.   self:UnlockHighlight()
  691.   GameTooltip:Hide()
  692. end
  693.  
  694. -------------------------------------------------------------------------------
  695. -- CreateButton
  696. --
  697. -- Creates a button for the predictor frame.
  698. --
  699. -- PredictFrame       Frame the will contain the buttons
  700. -- EditBox            Reference to the EditBox
  701. -- Index              Button Index, needed for setpoint
  702. --
  703. -- Returns
  704. --   Button           Created buttom.
  705. -------------------------------------------------------------------------------
  706. local function CreateButton(PredictFrame, EditBox, Index)
  707.   local Buttons = PredictFrame.Buttons
  708.   local Button = CreateFrame('Button', nil, PredictFrame)
  709.  
  710.   Button:SetHeight(17)
  711.   Button:SetWidth(1)
  712.   Button:SetPushedTextOffset(-2, 0)
  713.   Button:SetScript('OnClick', PredictorButtonOnClick)
  714.   Button:SetScript('OnEnter', PredictorButtonOnEnter)
  715.   Button:SetScript('OnLeave', PredictorButtonOnLeave)
  716.   Button.parent = PredictFrame
  717.   Button.EditBox = EditBox
  718.   Button:Hide()
  719.  
  720.   if Index > 1 then
  721.     Button:SetPoint('TOPLEFT', Buttons[Index - 1], 'BOTTOMLEFT', 0, 0)
  722.     Button:SetPoint('TOPRIGHT', Buttons[Index - 1], 'BOTTOMRIGHT', 0, 0)
  723.   else
  724.     Button:SetPoint('TOPLEFT', PredictFrame, 12, 0)
  725.     Button:SetPoint('TOPRIGHT', PredictFrame, -7, 0)
  726.   end
  727.  
  728.   -- Create the actual text
  729.   local Text = Button:CreateFontString(nil, 'ARTWORK', 'GameFontNormal')
  730.   Text:SetHeight(1)
  731.   Text:SetWidth(1)
  732.   Text:SetJustifyH('LEFT')
  733.   Text:SetAllPoints(Button)
  734.   Button:SetFontString(Text)
  735.  
  736.   -- Setup the highlighting
  737.   local Texture = Button:CreateTexture(nil, 'ARTWORK')
  738.   Texture:SetTexture([[Interface\QuestFrame\UI-QuestTitleHighlight]])
  739.   Texture:ClearAllPoints()
  740.   Texture:SetPoint('TOPLEFT', Button, 0, -2)
  741.   Texture:SetPoint('BOTTOMRIGHT', Button, 5, 2)
  742.   Texture:SetAlpha(0.70)
  743.  
  744.   Button:SetHighlightTexture(Texture)
  745.   Button:SetHighlightFontObject(GameFontHighlight)
  746.   Button:SetNormalFontObject(GameFontNormal)
  747.  
  748.   return Button
  749. end
  750.  
  751. -------------------------------------------------------------------------------
  752. -- PredictorConstructor
  753. --
  754. -- Creates the widget for the edit box and predictor
  755. -------------------------------------------------------------------------------
  756. local function ScrollerOnValueChanged(self, Value)
  757.   self:GetParent():SetVerticalScroll(Value)
  758. end
  759.  
  760. local function PredictorConstructor()
  761.   local Frame = CreateFrame('Frame', nil, UIParent)
  762.   local EditBox = CreateFrame('EditBox', nil, Frame, 'InputBoxTemplate')
  763.  
  764.   -- Don't feel like looking up the specific callbacks for when a widget resizes, so going to be creative with SetPoint instead!
  765.   local MenuFrame = CreateFrame('Frame', nil, UIParent)
  766.   MenuFrame:SetBackdrop(PredictorBackdrop)
  767.   MenuFrame:SetBackdropColor(0, 0, 0, 0.85)
  768.   MenuFrame:SetWidth(1)
  769.   MenuFrame:SetHeight(150)
  770.   MenuFrame:SetPoint('TOPLEFT', EditBox, 'BOTTOMLEFT', -6, 0)
  771.   MenuFrame:SetWidth(MenuFrameWidth)
  772.   MenuFrame:SetFrameStrata('TOOLTIP')
  773.   MenuFrame:SetClampedToScreen(true)
  774.   MenuFrame:Hide()
  775.  
  776.   -- Create the scroll frame
  777.   local ScrollFrame = CreateFrame('ScrollFrame', nil, MenuFrame)
  778.   ScrollFrame:SetPoint('TOPLEFT', 0, -6)
  779.   ScrollFrame:SetPoint('BOTTOMRIGHT', -28, 6)
  780.  
  781.     local PredictFrame = CreateFrame('Frame', nil, ScrollFrame)
  782.     local Buttons = {}
  783.  
  784.     PredictFrame:SetSize(MenuFrameWidth, 2000)
  785.     PredictFrame.PopulatePredictor = PopulatePredictor
  786.     PredictFrame.EditBox = EditBox
  787.     PredictFrame.Buttons = Buttons
  788.     PredictFrame.MenuFrame = MenuFrame
  789.     PredictFrame.ScrollFrame = ScrollFrame
  790.  
  791.   ScrollFrame:SetScrollChild(PredictFrame)
  792.  
  793.   -- Create the scroller
  794.   local Scroller = CreateFrame('slider', nil, ScrollFrame)
  795.   Scroller:SetOrientation('VERTICAL')
  796.   Scroller:SetPoint('TOPRIGHT', MenuFrame, 'TOPRIGHT', -12, -7)
  797.   Scroller:SetPoint('BOTTOMRIGHT', MenuFrame, 'BOTTOMRIGHT', -12, 7)
  798.   Scroller:SetBackdrop(SliderBackdrop)
  799.   Scroller:SetThumbTexture( [[Interface\Buttons\UI-SliderBar-Button-Vertical]] )
  800.   Scroller:SetMinMaxValues(0, 1)
  801.   Scroller:SetWidth(12)
  802.   Scroller:SetValueStep(1)
  803.   Scroller:SetValue(0)
  804.   Scroller:SetScript('OnValueChanged', ScrollerOnValueChanged)
  805.  
  806.   PredictFrame.Scroller = Scroller
  807.  
  808.  
  809.   -- Create the mass of predictor rows
  810.   for Index = 1, PredictorLines + 1 do
  811.     Buttons[Index] = CreateButton(PredictFrame, EditBox, Index)
  812.   end
  813.  
  814.   -- Set the main info things for this thingy
  815.   local Widget = {}
  816.  
  817.   Widget.type = EditBoxWidgetType
  818.   Widget.frame = Frame
  819.  
  820.   Widget.OnRelease = OnRelease
  821.   Widget.OnAcquire = OnAcquire
  822.  
  823.   Widget.SetDisabled = EditBoxSetDisabled
  824.   Widget.SetText = EditBoxSetText
  825.   Widget.SetLabel = EditBoxSetLabel
  826.  
  827.   Widget.PredictFrame = PredictFrame
  828.   Widget.EditBox = EditBox
  829.  
  830.   Widget.alignoffset = 30
  831.  
  832.   Frame:SetHeight(44)
  833.   Frame:SetWidth(200)
  834.  
  835.   Frame.Widget = Widget
  836.   EditBox.Widget = Widget
  837.   PredictFrame.Widget = Widget
  838.  
  839.   -- EditBoxes override the OnKeyUp/OnKeyDown events so that they can function, meaning in order to make up and down
  840.   -- arrow navigation of the menu work, I have to do some trickery with temporary bindings.
  841.   -- This is currently taken out since no one uses a keyboard for dropdowns.
  842.   PredictFrame:SetScript('OnMouseDown', PredictorOnMouseDown)
  843.   PredictFrame:SetScript('OnHide', PredictorOnHide)
  844.   --PredictFrame:SetScript('OnShow', PredictorOnShow)
  845.  
  846.   EditBox:SetScript('OnEnter', EditBoxOnEnter)
  847.   EditBox:SetScript('OnLeave', EditBoxOnLeave)
  848.  
  849.   EditBox:SetAutoFocus(false)
  850.   EditBox:SetFontObject(ChatFontNormal)
  851.   EditBox:SetScript('OnEscapePressed', EditBoxOnEscapePressed)
  852.   EditBox:SetScript('OnEnterPressed', EditBoxOnEnterPressed)
  853.   EditBox:SetScript('OnTextChanged', EditBoxOnTextChanged)
  854.   EditBox:SetScript('OnReceiveDrag', EditBoxOnReceiveDrag)
  855.   EditBox:SetScript('OnMouseDown', EditBoxOnReceiveDrag)
  856.   EditBox:SetScript('OnEditFocusGained', EditBoxOnEditFocusGained)
  857.   EditBox:SetScript('OnEditFocusLost', EditBoxOnEditFocusLost)
  858.  
  859.   EditBox:SetTextInsets(0, 0, 3, 3)
  860.   EditBox:SetMaxLetters(256)
  861.  
  862.   EditBox:SetPoint('BOTTOMLEFT', Frame, 'BOTTOMLEFT', 6, 0)
  863.   EditBox:SetPoint('BOTTOMRIGHT', Frame, 'BOTTOMRIGHT', 0, 0)
  864.   EditBox:SetHeight(19)
  865.  
  866.   local Label = Frame:CreateFontString(nil, 'OVERLAY', 'GameFontNormalSmall')
  867.   Label:SetPoint('TOPLEFT', Frame, 'TOPLEFT', 0, -2)
  868.   Label:SetPoint('TOPRIGHT', Frame, 'TOPRIGHT', 0, -2)
  869.   Label:SetJustifyH('LEFT')
  870.   Label:SetHeight(18)
  871.  
  872.   Widget.Label = Label
  873.  
  874.   local Button = CreateFrame('Button', nil, EditBox, 'UIPanelButtonTemplate')
  875.   Button:SetPoint('RIGHT', EditBox, 'RIGHT', -2, 0)
  876.   Button:SetScript('OnClick', EditBoxButtonOnClick)
  877.   Button:SetWidth(40)
  878.   Button:SetHeight(20)
  879.   Button:SetText(OKAY)
  880.   Button:Hide()
  881.  
  882.   Widget.Button = Button
  883.   Button.Widget = Widget
  884.  
  885.   AceGUI:RegisterAsWidget(Widget)
  886.   return Widget
  887. end
  888.  
  889. --*****************************************************************************
  890. --
  891. -- Aura_EditBox dialog control
  892. --
  893. --*****************************************************************************
  894.  
  895. -------------------------------------------------------------------------------
  896. -- AuraEditBoxConstructor
  897. --
  898. -- Creates the widget for the Aura_EditBox
  899. --
  900. -- I know theres a better way of doing this than this, but not sure for the time being, works fine though!
  901. -------------------------------------------------------------------------------
  902. local function AuraEditBoxConstructor()
  903.   return AceGUI:Create(EditBoxWidgetType)
  904. end
  905.  
  906. AceGUI:RegisterWidgetType(EditBoxWidgetType, PredictorConstructor, EditBoxWidgetVersion)
  907. AceGUI:RegisterWidgetType(AuraEditBoxWidgetType, AuraEditBoxConstructor, AuraEditBoxWidgetVersion)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement