Merge branch 'master' of git://cams.pavlovian.net/questhelper
[QuestHelper.git] / tooltip.lua
blob6efe71a736bad5a3841057c2e49f4daa8f61ae6f
1 QuestHelper_File["tooltip.lua"] = "Development Version"
2 QuestHelper_Loadtime["tooltip.lua"] = GetTime()
4 if QuestHelper_File["tooltip.lua"] == "Development Version" then
5 qh_hackery_nosuppress = true
6 end
8 local function DoTooltip(self, data, lines, prefix)
9 local indent = 1
11 if prefix then
12 self:AddLine((" "):rep(indent) .. prefix, 1, 1, 1)
13 indent = indent + 1
14 end
16 --QuestHelper:TextOut(QuestHelper:StringizeTable(data))
17 --QuestHelper:TextOut(QuestHelper:StringizeTable(lines))
18 for _, v in ipairs(lines) do
19 self:AddLine((" "):rep(indent) .. v, 1, 1, 1)
20 indent = indent + 1
21 end
22 self:AddLine((" "):rep(indent) .. data.desc, 1, 1, 1)
23 QuestHelper:AppendObjectiveProgressToTooltip(data, self, nil, indent + 1)
24 end
26 local function DoTooltipDefault(self, qname, text)
27 self:AddLine(" " .. QHFormat("TOOLTIP_SLAY", text), 1, 1, 1)
28 self:AddLine(" " .. QHFormat("TOOLTIP_QUEST", qname), 1, 1, 1)
29 end
31 local ctts = {}
33 -- Format:
34 -- { ["monster@@1234"] = {{"Slay for blah blah blah"}, (Objective)} }
35 -- ("Slay for" is frequently an empty table)
36 function QH_Tooltip_Canned_Add(tooltips)
37 for k, v in pairs(tooltips) do
38 local typ, id = k:match("([^@]+)@@([^@]+)")
39 --[[print(k)
40 for tk, tv in pairs(v[1]) do
41 print(" ", 1, tk, tv)
42 end
43 for tk, tv in pairs(v[2]) do
44 print(" ", 2, tk, tv)
45 end]]
46 QuestHelper: Assert(typ and id, k)
47 if not ctts[typ] then ctts[typ] = {} end
48 if not ctts[typ][id] then ctts[typ][id] = {} end
49 QuestHelper: Assert(not ctts[typ][id][v[2]])
50 ctts[typ][id][v[2]] = v[1]
51 end
52 end
53 function QH_Tooltip_Canned_Remove(tooltips)
54 for k, v in pairs(tooltips) do
55 local typ, id = k:match("([^@]+)@@([^@]+)")
56 QuestHelper: Assert(typ and id, k)
57 QuestHelper: Assert(ctts[typ][id][v[2]])
58 ctts[typ][id][v[2]] = nil
60 local cleanup = true
61 for _, _ in pairs(ctts[typ][id]) do
62 cleanup = false
63 end
65 if cleanup then
66 ctts[typ][id] = nil
67 end
68 end
69 end
71 local deferences = {}
72 local deference_default = {} -- this is just a unique value that we can use to lookup
74 -- think about what we want out of this
75 -- If it matches quest/objective, we suppress it and show our canned text
76 -- If it matches quest, but has unknown objectives, we suppress it and show some synthesized "Canned thing, for Quest Blahblahblah"
78 -- tooltips is the same slay/objective pair in the above thing
79 function QH_Tooltip_Defer_Add(questname, objective, tooltips)
80 --print("defer add", questname, objective)
81 if not objective then objective = deference_default end
83 if not deferences[questname] then deferences[questname] = {} end
84 QuestHelper: Assert(not deferences[questname][objective], string.format("%s %s", tostring(questname), tostring(objective)))
85 deferences[questname][objective] = tooltips
86 if not deferences[questname][objective] then deferences[questname] = true end
88 --print("adding", questname, objective)
89 end
90 function QH_Tooltip_Defer_Remove(questname, objective)
91 if not objective then objective = deference_default end
93 --print("remove", questname, objective)
94 --print("removing", questname, objective, deferences[questname][objective])
95 QuestHelper: Assert(deferences[questname][objective], string.format("%s %s", tostring(questname), tostring(objective)))
96 deferences[questname][objective] = nil
98 local cleanup = true
99 for _ in pairs(deferences[questname]) do
100 cleanup = false
103 if cleanup then
104 deferences[questname] = nil
108 -- TODO: move this into some common file, I hate that I'm duplicating them but I just want this to work. entire codebase will need a going-over soon
109 local function IsMonsterGUID(guid)
110 QuestHelper: Assert(#guid == 18, "guid len " .. guid) -- 64 bits, plus the 0x prefix
111 QuestHelper: Assert(guid:sub(1, 2) == "0x", "guid 0x-prefix " .. guid)
112 return guid:sub(5, 5) == "3" or guid:sub(5, 5) == "5"
115 local function GetMonsterType(guid)
116 QuestHelper: Assert(#guid == 18, "guid len " .. guid) -- 64 bits, plus the 0x prefix
117 QuestHelper: Assert(guid:sub(1, 2) == "0x", "guid 0x-prefix " .. guid)
118 QuestHelper: Assert(guid:sub(5, 5) == "3" or guid:sub(5, 5) == "5", "guid 3-prefix " .. guid) -- It *shouldn't* be a player or a pet by the time we've gotten here. If so, something's gone wrong.
119 return tonumber(guid:sub(9, 12), 16) -- here's our actual identifier
122 local function GetItemType(link, vague)
123 return tonumber(string.match(link,
124 (vague and "" or "^") .. "|cff%x%x%x%x%x%x|Hitem:(%d+):[%d:-]+|h%[[^%]]*%]|h|r".. (vague and "" or "$")
128 local function CopyOver(to, from)
129 to:SetFont(from:GetFont())
130 to:SetFontObject(from:GetFontObject())
131 to:SetText(from:GetText())
132 to:SetTextColor(from:GetTextColor())
133 to:SetSpacing(from:GetSpacing())
134 to:SetShadowOffset(from:GetShadowOffset())
135 to:SetShadowColor(from:GetShadowColor())
136 to:Show()
139 local function StripBlizzQHTooltipClone(ttp)
140 --do return end
141 if not UnitExists("mouseover") then return end
143 local line = 2
144 local wpos = line
146 local changed = false
147 local removed = 0
149 if qh_tooltip_print_a_lot then print(line, _G["GameTooltipTextLeft" .. line], _G["GameTooltipTextLeft" .. line]:IsShown()) end
151 local qobj = nil
152 local qobj_name = nil
154 while _G["GameTooltipTextLeft" .. line] and _G["GameTooltipTextLeft" .. line]:IsShown() do
155 local r, g, b, a = _G["GameTooltipTextLeft" .. line]:GetTextColor()
156 r, g, b, a = math.floor(r * 255 + 0.5), math.floor(g * 255 + 0.5), math.floor(b * 255 + 0.5), math.floor(a * 255 + 0.5)
158 if qh_tooltip_print_a_lot then print(r, g, b, a) end
160 if r == 255 and g == 210 and b == 0 and a == 255 and not qh_tooltip_do_not_hide_things then
161 if qh_tooltip_print_a_lot then print("hiding") end
163 local thistext = _G["GameTooltipTextLeft" .. line]:GetText()
165 if deferences[thistext] then
166 qobj = deferences[thistext]
167 qobj_name = thistext
168 elseif qobj then
169 --print(qobj, thistext, qobj[thistext], qobj[deference_default])
170 if qobj[thistext] then
171 local ttsplat = thistext:match("(.*): ([0-9]+)/([0-9]+)")
172 if ttsplat == ttp:GetUnit() then
173 ttsplat = nil
175 DoTooltip(ttp, qobj[thistext][2], qobj[thistext][1], ttsplat and QHFormat("TOOLTIP_SLAY", ttsplat))
176 elseif qobj[deference_default] and not thistext:find(":") then -- Blizzard cleverly does not suppress tooltips when the user has finished getting certain items, so we do instead
177 DoTooltipDefault(ttp, qobj_name, thistext)
181 if not qh_hackery_nosuppress then
182 _G["GameTooltipTextLeft" .. line]:SetText(nil)
183 _G["GameTooltipTextLeft" .. line]:SetHeight(0)
184 _G["GameTooltipTextLeft" .. line]:ClearAllPoints()
185 _G["GameTooltipTextLeft" .. line]:SetPoint("TOPLEFT", _G["GameTooltipTextLeft" .. (line - 1)], "BOTTOMLEFT", 0, 1)
186 changed = true
187 removed = removed + 1
191 line = line + 1
194 if changed then
195 ttp:Show()
198 return removed
201 local glob_strip = 0
202 function CreateTooltip(self)
203 glob_strip = 0
205 if QuestHelper_Pref.tooltip then
206 local inu, ilink = self:GetItem()
207 local un, ulink = self:GetUnit()
208 if ulink then ulink = UnitGUID(ulink) end
210 if ilink then
211 local ite = tostring(GetItemType(ilink))
213 if ctts["item"] and ctts["item"][ite] then
214 DoTooltip(self, ctts["item"][ite])
217 self:Show()
220 if qh_tooltip_print_a_lot then print("wut", ulink, IsMonsterGUID(ulink)) print(ulink) print(IsMonsterGUID(ulink)) end
221 if ulink and IsMonsterGUID(ulink) then
222 if qh_tooltip_print_a_lot then print("huhwuzat") print(QH_filter_hints) end
224 glob_strip = StripBlizzQHTooltipClone(self)
226 local ite = tostring(GetMonsterType(ulink))
228 if ctts["monster"] and ctts["monster"][ite] then
229 for data, lines in pairs(ctts["monster"][ite]) do
230 DoTooltip(self, data, lines)
234 self:Show()
239 local unit_to_adjust = nil
241 local ottsu = GameTooltip:GetScript("OnTooltipSetUnit")
242 QH_Hook(GameTooltip, "OnTooltipSetUnit", function (self, ...)
243 if qh_tooltip_print_a_lot then print("lol") end
244 CreateTooltip(self)
245 if ottsu then ottsu(self, ...) end
246 unit_to_adjust = self:GetUnit()
247 end, "tooltip OnTooltipSetUnit")
249 local ottsi = GameTooltip:GetScript("OnTooltipSetItem")
250 QH_Hook(GameTooltip, "OnTooltipSetItem", function (self, ...)
251 QH_Hook_NotMyFault(CreateTooltip, self)
252 if ottsi then return QH_Hook_NotMyFault(ottsi, self, ...) end
253 end, "tooltip OnTooltipSetItem")
255 local ttsx = GameTooltip:GetScript("OnUpdate")
256 QH_Hook(GameTooltip, "OnUpdate", function (self, ...)
257 if ttsx then QH_Hook_NotMyFault(ttsx, self, ...) end
258 if glob_strip and unit_to_adjust and unit_to_adjust == self:GetUnit() then
259 self:SetHeight(self:GetHeight() - glob_strip * 3) -- maaaaaagic
260 unit_to_adjust = nil
262 end, "tooltip OnUpdate")