Mercurial > wow > wowdb-profiler
comparison Comments.lua @ 270:e1566483082c
Added visual menu for selecting a comment subject via minimap icon (defaults to off), LDB icon, or the /comment command without an argument.
| author | James D. Callahan III <jcallahan@curse.com> |
|---|---|
| date | Thu, 21 Mar 2013 15:11:34 -0500 |
| parents | 463b0f117b1b |
| children | fbd94a0be29c |
comparison
equal
deleted
inserted
replaced
| 269:463b0f117b1b | 270:e1566483082c |
|---|---|
| 3 local _G = getfenv(0) | 3 local _G = getfenv(0) |
| 4 | 4 |
| 5 local table = _G.table | 5 local table = _G.table |
| 6 | 6 |
| 7 local next = _G.next | 7 local next = _G.next |
| 8 local pairs = _G.pairs | |
| 8 | 9 |
| 9 -- ADDON NAMESPACE ---------------------------------------------------- | 10 -- ADDON NAMESPACE ---------------------------------------------------- |
| 10 | 11 |
| 11 local ADDON_NAME, private = ... | 12 local ADDON_NAME, private = ... |
| 12 | 13 |
| 13 local LibStub = _G.LibStub | 14 local LibStub = _G.LibStub |
| 14 local WDP = LibStub("AceAddon-3.0"):GetAddon(ADDON_NAME) | 15 local WDP = LibStub("AceAddon-3.0"):GetAddon(ADDON_NAME) |
| 15 local Dialog = LibStub("LibDialog-1.0") | 16 local Dialog = LibStub("LibDialog-1.0") |
| 17 local LQT = LibStub("LibQTip-1.0") | |
| 16 | 18 |
| 17 local ParseGUID = private.ParseGUID | 19 local ParseGUID = private.ParseGUID |
| 18 local ItemLinkToID = private.ItemLinkToID | 20 local ItemLinkToID = private.ItemLinkToID |
| 19 local DBEntry = private.DBEntry | 21 local DBEntry = private.DBEntry |
| 20 | 22 |
| 68 self.text:SetJustifyH("LEFT") | 70 self.text:SetJustifyH("LEFT") |
| 69 self.text:SetFormattedText(LINK_EDITBOX_DESC_FORMAT:format(data.label)) | 71 self.text:SetFormattedText(LINK_EDITBOX_DESC_FORMAT:format(data.label)) |
| 70 end, | 72 end, |
| 71 }) | 73 }) |
| 72 | 74 |
| 75 -- VARIABLES ---------------------------------------------------------- | |
| 76 | |
| 73 local comment_subject = {} | 77 local comment_subject = {} |
| 78 local comment_frame | |
| 74 | 79 |
| 75 -- HELPERS ------------------------------------------------------------ | 80 -- HELPERS ------------------------------------------------------------ |
| 76 | 81 |
| 77 local comment_frame | 82 local function NewComment(type_name, label, id) |
| 83 comment_subject.id = id | |
| 84 comment_subject.label = label | |
| 85 comment_subject.type_name = type_name | |
| 86 | |
| 87 comment_frame.subject_name:SetText(label) | |
| 88 comment_frame.subject_data:SetFormattedText("(%s #%d)", type_name, id) | |
| 89 comment_frame.scroll_frame.edit_box:SetText("") | |
| 90 _G.ShowUIPanel(comment_frame) | |
| 91 end | |
| 92 | |
| 93 local function CreateUnitComment(unit_id) | |
| 94 if not _G.UnitExists(unit_id) then | |
| 95 WDP:Printf("Unit '%s' does not exist.", unit_id) | |
| 96 return | |
| 97 end | |
| 98 local unit_type, unit_idnum = ParseGUID(_G.UnitGUID(unit_id)) | |
| 99 | |
| 100 if not unit_idnum then | |
| 101 WDP:Printf("Unable to determine unit from '%s'", unit_id) | |
| 102 return | |
| 103 end | |
| 104 local type_name = private.UNIT_TYPE_NAMES[unit_type + 1] | |
| 105 local unit_name = _G.UnitName(unit_id) | |
| 106 NewComment(type_name, unit_name, unit_idnum) | |
| 107 end | |
| 108 | |
| 109 local DATA_TYPE_MAPPING = { | |
| 110 merchant = "ITEM", | |
| 111 } | |
| 112 | |
| 113 local CreateCursorComment | |
| 78 do | 114 do |
| 115 local CURSOR_DATA_FUNCS = { | |
| 116 item = function(type_name, id_num, data_subtype) | |
| 117 local item_name = _G.GetItemInfo(id_num) | |
| 118 NewComment(type_name, item_name, id_num) | |
| 119 end, | |
| 120 merchant = function(type_name, item_index) | |
| 121 local item_link = _G.GetMerchantItemLink(item_index) | |
| 122 local item_name = _G.GetItemInfo(item_link) | |
| 123 NewComment(type_name, item_name, ItemLinkToID(item_link)) | |
| 124 end, | |
| 125 spell = function(type_name, data, data_subtype, spell_id) | |
| 126 local spell_name = _G.GetSpellInfo(spell_id) | |
| 127 NewComment(type_name, spell_name, spell_id) | |
| 128 end, | |
| 129 } | |
| 130 | |
| 131 function CreateCursorComment() | |
| 132 local data_type, data, data_subtype, subdata = _G.GetCursorInfo() | |
| 133 local comment_func = CURSOR_DATA_FUNCS[data_type] | |
| 134 | |
| 135 if not comment_func then | |
| 136 WDP:Print("Unable to determine comment subject from cursor.") | |
| 137 return | |
| 138 end | |
| 139 comment_func(DATA_TYPE_MAPPING[data_type] or data_type:upper(), data, data_subtype, subdata) | |
| 140 end | |
| 141 end -- do-block | |
| 142 | |
| 143 local function CreateQuestComment() | |
| 144 local index = _G.GetQuestLogSelection() | |
| 145 | |
| 146 if not index or not _G.QuestLogFrame:IsShown() then | |
| 147 WDP:Print("You must select a quest from the Quest frame.") | |
| 148 return | |
| 149 end | |
| 150 local title, _, tag, _, is_header, _, _, _, idnum = _G.GetQuestLogTitle(index) | |
| 151 | |
| 152 if is_header then | |
| 153 WDP:Print("You must select a quest from the Quest frame.") | |
| 154 return | |
| 155 end | |
| 156 NewComment("QUEST", title, idnum) | |
| 157 end | |
| 158 | |
| 159 local function CreateAchievementComment() | |
| 160 if not _G.AchievementFrame or not _G.AchievementFrame:IsShown() or not _G.AchievementFrameAchievements.selection then | |
| 161 WDP:Print("You must select an achievement from the Achievement frame.") | |
| 162 return | |
| 163 end | |
| 164 | |
| 165 for _, button in next, _G.AchievementFrameAchievementsContainer.buttons do | |
| 166 if button.selected then | |
| 167 NewComment("ACHIEVEMENT", button.label:GetText(), button.id) | |
| 168 break | |
| 169 end | |
| 170 end | |
| 171 end | |
| 172 | |
| 173 local ShowPossibleSubjects | |
| 174 do | |
| 175 local display | |
| 176 local old_x, old_y, click_time | |
| 177 | |
| 178 _G.WorldFrame:HookScript("OnMouseDown", function(frame, ...) | |
| 179 old_x, old_y = _G.GetCursorPosition() | |
| 180 click_time = _G.GetTime() | |
| 181 end) | |
| 182 | |
| 183 _G.WorldFrame:HookScript("OnMouseUp", function(frame, ...) | |
| 184 if not display then | |
| 185 return | |
| 186 end | |
| 187 local x, y = _G.GetCursorPosition() | |
| 188 | |
| 189 if not old_x or not old_y or not x or not y or not click_time then | |
| 190 display = display:Release() | |
| 191 return | |
| 192 end | |
| 193 | |
| 194 if (_G.math.abs(x - old_x) + _G.math.abs(y - old_y)) <= 5 and _G.GetTime() - click_time < 1 then | |
| 195 display = display:Release() | |
| 196 end | |
| 197 end) | |
| 198 | |
| 199 local function CreateComment(cell, func) | |
| 200 func() | |
| 201 display = display:Release() | |
| 202 end | |
| 203 | |
| 204 local CURSOR_NAME_FUNCS = { | |
| 205 item = function(id_num) | |
| 206 return _G.GetItemInfo(id_num) | |
| 207 end, | |
| 208 merchant = function(item_index) | |
| 209 return _G.GetItemInfo(_G.GetMerchantItemLink(item_index)) | |
| 210 end, | |
| 211 spell = function(data, data_subtype, spell_id) | |
| 212 return _G.GetSpellInfo(spell_id) | |
| 213 end, | |
| 214 } | |
| 215 | |
| 216 local VALID_UNITS = { | |
| 217 boss1 = true, | |
| 218 boss2 = true, | |
| 219 boss3 = true, | |
| 220 boss4 = true, | |
| 221 focus = true, | |
| 222 mouseover = true, | |
| 223 npc = true, | |
| 224 target = true, | |
| 225 } | |
| 226 | |
| 227 function ShowPossibleSubjects(anchor) | |
| 228 if not display then | |
| 229 display = LQT:Acquire(ADDON_NAME, 1, "LEFT") | |
| 230 display:EnableMouse(true) | |
| 231 end | |
| 232 | |
| 233 if anchor then | |
| 234 display:SmartAnchorTo(anchor) | |
| 235 display:SetAutoHideDelay(0.2, anchor) | |
| 236 else | |
| 237 display:SetPoint("CENTER", _G.UIParent, "CENTER", 0, 0) | |
| 238 end | |
| 239 display:Clear() | |
| 240 display:AddHeader("Choose comment subject:", "CENTER") | |
| 241 display:AddSeparator() | |
| 242 display:AddSeparator() | |
| 243 | |
| 244 local line | |
| 245 | |
| 246 for unit_id in pairs(VALID_UNITS) do | |
| 247 if _G.UnitExists(unit_id) then | |
| 248 local unit_type, unit_idnum = ParseGUID(_G.UnitGUID(unit_id)) | |
| 249 | |
| 250 if unit_idnum then | |
| 251 line = display:AddLine(("%s: %s"):format(unit_id:gsub("^%l", _G.string.upper), _G.UnitName(unit_id))) | |
| 252 display:SetLineScript(line, "OnMouseUp", CreateComment, CreateUnitComment) | |
| 253 end | |
| 254 end | |
| 255 end | |
| 256 | |
| 257 if _G.AchievementFrame and _G.AchievementFrame:IsShown() and _G.AchievementFrameAchievements.selection then | |
| 258 for _, button in next, _G.AchievementFrameAchievementsContainer.buttons do | |
| 259 if button.selected then | |
| 260 line = display:AddLine(("Achievement: %s"):format(button.label:GetText())) | |
| 261 display:SetLineScript(line, "OnMouseUp", CreateComment, CreateAchievementComment) | |
| 262 break | |
| 263 end | |
| 264 end | |
| 265 end | |
| 266 local data_type, data, data_subtype, subdata = _G.GetCursorInfo() | |
| 267 local name_func = CURSOR_NAME_FUNCS[data_type] | |
| 268 | |
| 269 if name_func then | |
| 270 line = display:AddLine(("Cursor: %s"):format(name_func(data, data_subtype, subdata))) | |
| 271 display:SetLineScript(line, "OnMouseUp", CreateComment, CreateCursorComment) | |
| 272 end | |
| 273 | |
| 274 local quest_index = _G.GetQuestLogSelection() | |
| 275 | |
| 276 if quest_index and _G.QuestLogFrame:IsShown() then | |
| 277 local title, _, tag, _, is_header, _, _, _, idnum = _G.GetQuestLogTitle(quest_index) | |
| 278 | |
| 279 if not is_header then | |
| 280 line = display:AddLine(("Quest: %s"):format(title)) | |
| 281 display:SetLineScript(line, "OnMouseUp", CreateComment, CreateQuestComment) | |
| 282 end | |
| 283 end | |
| 284 | |
| 285 if display:GetLineCount() == 3 then | |
| 286 WDP:Print("There are no possible comment subjects.") | |
| 287 return | |
| 288 end | |
| 289 display:Show() | |
| 290 end | |
| 291 end -- do-block | |
| 292 | |
| 293 -- METHODS ------------------------------------------------------------ | |
| 294 | |
| 295 function private.ProcessCommentCommand(arg) | |
| 296 if not arg or arg == "" then | |
| 297 ShowPossibleSubjects(nil) | |
| 298 return | |
| 299 end | |
| 300 | |
| 301 if arg == "achievement" then | |
| 302 CreateAchievementComment() | |
| 303 return | |
| 304 elseif arg == "cursor" then | |
| 305 CreateCursorComment() | |
| 306 return | |
| 307 elseif arg == "quest" then | |
| 308 CreateQuestComment() | |
| 309 return | |
| 310 end | |
| 311 CreateUnitComment(arg) | |
| 312 end | |
| 313 | |
| 314 function private.InitializeCommentSystem() | |
| 79 local panel = _G.CreateFrame("Frame", "WDP_CommentFrame", _G.UIParent, "TranslucentFrameTemplate") | 315 local panel = _G.CreateFrame("Frame", "WDP_CommentFrame", _G.UIParent, "TranslucentFrameTemplate") |
| 80 panel:SetSize(480, 350) | 316 panel:SetSize(480, 350) |
| 81 panel:SetPoint("CENTER", _G.UIParent, "CENTER") | 317 panel:SetPoint("CENTER", _G.UIParent, "CENTER") |
| 82 panel:SetFrameStrata("DIALOG") | 318 panel:SetFrameStrata("DIALOG") |
| 83 panel.Bg:SetTexture([[Interface\FrameGeneral\UI-Background-Rock]], true, true) | 319 panel.Bg:SetTexture([[Interface\FrameGeneral\UI-Background-Rock]], true, true) |
| 288 | 524 |
| 289 edit_box:SetText("") | 525 edit_box:SetText("") |
| 290 _G.HideUIPanel(panel) | 526 _G.HideUIPanel(panel) |
| 291 end) | 527 end) |
| 292 panel.submitButton = submit | 528 panel.submitButton = submit |
| 529 | |
| 530 local data_obj = LibStub("LibDataBroker-1.1"):NewDataObject(ADDON_NAME, { | |
| 531 type = "data source", | |
| 532 label = ADDON_NAME, | |
| 533 text = " ", | |
| 534 icon = [[Interface\CHATFRAME\UI-ChatIcon-Chat-Up]], | |
| 535 OnClick = function(self, button, down) | |
| 536 ShowPossibleSubjects(self) | |
| 537 end, | |
| 538 OnTooltipShow = function(self) | |
| 539 self:AddLine(_G.CLICK_TO_ENTER_COMMENT) | |
| 540 end, | |
| 541 }) | |
| 542 | |
| 543 private.data_obj = data_obj | |
| 544 LibStub("LibDBIcon-1.0"):Register(ADDON_NAME, data_obj, private.db.global.config.minimap_icon) | |
| 293 end | 545 end |
| 294 | |
| 295 local function NewComment(type_name, label, id) | |
| 296 comment_subject.id = id | |
| 297 comment_subject.label = label | |
| 298 comment_subject.type_name = type_name | |
| 299 | |
| 300 comment_frame.subject_name:SetText(label) | |
| 301 comment_frame.subject_data:SetFormattedText("(%s #%d)", type_name, id) | |
| 302 comment_frame.scroll_frame.edit_box:SetText("") | |
| 303 _G.ShowUIPanel(comment_frame) | |
| 304 end | |
| 305 | |
| 306 local function CreateUnitComment(unit_id) | |
| 307 if not _G.UnitExists(unit_id) then | |
| 308 WDP:Printf("Unit '%s' does not exist.", unit_id) | |
| 309 return | |
| 310 end | |
| 311 local unit_type, unit_idnum = ParseGUID(_G.UnitGUID(unit_id)) | |
| 312 | |
| 313 if not unit_idnum then | |
| 314 WDP:Printf("Unable to determine unit from '%s'", unit_id) | |
| 315 return | |
| 316 end | |
| 317 local type_name = private.UNIT_TYPE_NAMES[unit_type + 1] | |
| 318 local unit_name = _G.UnitName(unit_id) | |
| 319 NewComment(type_name, unit_name, unit_idnum) | |
| 320 end | |
| 321 | |
| 322 local DATA_TYPE_MAPPING = { | |
| 323 merchant = "ITEM", | |
| 324 } | |
| 325 | |
| 326 local CreateCursorComment | |
| 327 do | |
| 328 local CURSOR_DATA_FUNCS = { | |
| 329 item = function(type_name, id_num, data_subtype) | |
| 330 local item_name = _G.GetItemInfo(id_num) | |
| 331 NewComment(type_name, item_name, id_num) | |
| 332 end, | |
| 333 merchant = function(type_name, item_index) | |
| 334 local item_link = _G.GetMerchantItemLink(item_index) | |
| 335 local item_name = _G.GetItemInfo(item_link) | |
| 336 NewComment(type_name, item_name, ItemLinkToID(item_link)) | |
| 337 end, | |
| 338 spell = function(type_name, data, data_subtype, spell_id) | |
| 339 local spell_name = _G.GetSpellInfo(spell_id) | |
| 340 NewComment(type_name, spell_name, spell_id) | |
| 341 end, | |
| 342 } | |
| 343 | |
| 344 function CreateCursorComment() | |
| 345 local data_type, data, data_subtype, subdata = _G.GetCursorInfo() | |
| 346 local comment_func = CURSOR_DATA_FUNCS[data_type] | |
| 347 | |
| 348 if not comment_func then | |
| 349 WDP:Print("Unable to determine comment subject from cursor.") | |
| 350 return | |
| 351 end | |
| 352 comment_func(DATA_TYPE_MAPPING[data_type] or data_type:upper(), data, data_subtype, subdata) | |
| 353 end | |
| 354 end | |
| 355 | |
| 356 local function CreateQuestComment() | |
| 357 local index = _G.GetQuestLogSelection() | |
| 358 | |
| 359 if not index or not _G.QuestLogFrame:IsShown() then | |
| 360 WDP:Print("You must select a quest from the Quest frame.") | |
| 361 return | |
| 362 end | |
| 363 local title, _, tag, _, is_header, _, _, _, idnum = _G.GetQuestLogTitle(index) | |
| 364 | |
| 365 if is_header then | |
| 366 WDP:Print("You must select a quest from the Quest frame.") | |
| 367 return | |
| 368 end | |
| 369 NewComment("QUEST", title, idnum) | |
| 370 end | |
| 371 | |
| 372 local function CreateAchievementComment() | |
| 373 if not _G.AchievementFrame or not _G.AchievementFrameAchievements.selection then | |
| 374 WDP:Print("You must select an achievement from the Achievement frame.") | |
| 375 return | |
| 376 end | |
| 377 | |
| 378 for _, button in next, _G.AchievementFrameAchievementsContainer.buttons do | |
| 379 if button.selected then | |
| 380 NewComment("ACHIEVEMENT", button.label:GetText(), button.id) | |
| 381 break | |
| 382 end | |
| 383 end | |
| 384 end | |
| 385 | |
| 386 -- METHODS ------------------------------------------------------------ | |
| 387 | |
| 388 function private.ProcessCommentCommand(arg) | |
| 389 if not arg or arg == "" then | |
| 390 WDP:Print("You must supply a valid comment type.") | |
| 391 return | |
| 392 end | |
| 393 | |
| 394 if arg == "achievement" then | |
| 395 CreateAchievementComment() | |
| 396 return | |
| 397 elseif arg == "cursor" then | |
| 398 CreateCursorComment() | |
| 399 return | |
| 400 elseif arg == "quest" then | |
| 401 CreateQuestComment() | |
| 402 return | |
| 403 end | |
| 404 CreateUnitComment(arg) | |
| 405 end |
