From eaa806f59c28e79c5514478394ed2a6ead5d9b5b Mon Sep 17 00:00:00 2001 From: shagu Date: Tue, 24 Aug 2021 16:51:41 +0200 Subject: [PATCH] toc: add initial framework and api files --- ShaguTweaks.toc | 51 ++++++++++ helpers.lua | 252 ++++++++++++++++++++++++++++++++++++++++++++++++ main.lua | 39 ++++++++ 3 files changed, 342 insertions(+) create mode 100644 ShaguTweaks.toc create mode 100644 helpers.lua create mode 100644 main.lua diff --git a/ShaguTweaks.toc b/ShaguTweaks.toc new file mode 100644 index 0000000..28779b6 --- /dev/null +++ b/ShaguTweaks.toc @@ -0,0 +1,51 @@ +## Interface: 11200 +## Title: |cffffcc00Shagu|cffffffffTweaks +## Author: Shagu +## Notes: A collection of tweaks and enhancements to the Default UI +## Version: GIT +## SavedVariablesPerCharacter: ShaguTweaks_config, ShaguTweaks_cache + +# locales +locales\locales_deDE.lua +locales\locales_enUS.lua +locales\locales_esES.lua +locales\locales_frFR.lua +locales\locales_koKR.lua +locales\locales_ruRU.lua +locales\locales_zhCN.lua + +# basic +main.lua +helpers.lua +config.lua + +# libraries +libs\libtipscan.lua +libs\libspell.lua +libs\libcast.lua +libs\libdebuff.lua +libs\libunitscan.lua +libs\libhealth.lua +libs\libnameplate.lua + +# modules +mods\dark-ui-elements.lua +mods\equip-compare.lua +mods\minimap-clock.lua +mods\minimap-square.lua +mods\minimap-tweaks.lua +mods\nameplate-scale.lua +mods\sell-junk.lua +mods\unitframes-classcolor.lua +mods\vendor-values.lua +mods\worldmap-coordinates.lua +mods\worldmap-window.lua +mods\blue-shaman.lua +mods\nameplate-classcolor.lua +mods\move-unitframes.lua +mods\target-castbar.lua +mods\target-debufftimer.lua +mods\social-colors.lua +mods\item-colors.lua +mods\chat-links.lua +mods\health-numbers.lua diff --git a/helpers.lua b/helpers.lua new file mode 100644 index 0000000..3b93585 --- /dev/null +++ b/helpers.lua @@ -0,0 +1,252 @@ +local _G = _G or getfenv(0) +local gfind = string.gmatch or string.gfind + +local gradientcolors = {} +ShaguTweaks.GetColorGradient = function(perc) + perc = perc > 1 and 1 or perc + perc = perc < 0 and 0 or perc + perc = floor(perc*100)/100 + + local index = perc + if not gradientcolors[index] then + local r1, g1, b1, r2, g2, b2 + + if perc <= 0.5 then + perc = perc * 2 + r1, g1, b1 = 1, 0, 0 + r2, g2, b2 = 1, 1, 0 + else + perc = perc * 2 - 1 + r1, g1, b1 = 1, 1, 0 + r2, g2, b2 = 0, 1, 0 + end + + local r = ShaguTweaks.round(r1 + (r2 - r1) * perc, 4) + local g = ShaguTweaks.round(g1 + (g2 - g1) * perc, 4) + local b = ShaguTweaks.round(b1 + (b2 - b1) * perc, 4) + local h = ShaguTweaks.rgbhex(r,g,b) + + gradientcolors[index] = {} + gradientcolors[index].r = r + gradientcolors[index].g = g + gradientcolors[index].b = b + gradientcolors[index].h = h + end + + return gradientcolors[index].r, + gradientcolors[index].g, + gradientcolors[index].b, + gradientcolors[index].h +end + +ShaguTweaks.GetExpansion = function() + local _, _, _, client = GetBuildInfo() + client = client or 11200 + + -- detect client expansion + if client >= 20000 and client <= 20400 then + return "tbc" + elseif client >= 30000 and client <= 30300 then + return "wotlk" + else + return "vanilla" + end +end + +ShaguTweaks.HookAddonOrVariable = function(addon, func) + local lurker = CreateFrame("Frame", nil) + lurker.func = func + lurker:RegisterEvent("ADDON_LOADED") + lurker:RegisterEvent("VARIABLES_LOADED") + lurker:RegisterEvent("PLAYER_ENTERING_WORLD") + lurker:SetScript("OnEvent",function() + if IsAddOnLoaded(addon) or _G[addon] then + this:func() + this:UnregisterAllEvents() + end + end) +end + +local hooks = {} +ShaguTweaks.hooksecurefunc = function(name, func, append) + if not _G[name] then return end + + hooks[tostring(func)] = {} + hooks[tostring(func)]["old"] = _G[name] + hooks[tostring(func)]["new"] = func + + if append then + hooks[tostring(func)]["function"] = function(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) + hooks[tostring(func)]["old"](a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) + hooks[tostring(func)]["new"](a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) + end + else + hooks[tostring(func)]["function"] = function(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) + hooks[tostring(func)]["new"](a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) + hooks[tostring(func)]["old"](a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) + end + end + + _G[name] = hooks[tostring(func)]["function"] +end + +local sanitize_cache = {} +ShaguTweaks.SanitizePattern = function(pattern) + if not sanitize_cache[pattern] then + local ret = pattern + -- escape magic characters + ret = gsub(ret, "([%+%-%*%(%)%?%[%]%^])", "%%%1") + -- remove capture indexes + ret = gsub(ret, "%d%$","") + -- catch all characters + ret = gsub(ret, "(%%%a)","%(%1+%)") + -- convert all %s to .+ + ret = gsub(ret, "%%s%+",".+") + -- set priority to numbers over strings + ret = gsub(ret, "%(.%+%)%(%%d%+%)","%(.-%)%(%%d%+%)") + -- cache it + sanitize_cache[pattern] = ret + end + + return sanitize_cache[pattern] +end + +local capture_cache = {} +ShaguTweaks.GetCaptures = function(pat) + local r = capture_cache + if not r[pat] then + for a, b, c, d, e in gfind(gsub(pat, "%((.+)%)", "%1"), gsub(pat, "%d%$", "%%(.-)$")) do + r[pat] = { a, b, c, d, e} + end + end + + if not r[pat] then return nil, nil, nil, nil end + return r[pat][1], r[pat][2], r[pat][3], r[pat][4], r[pat][5] +end + +ShaguTweaks.cmatch = function(str, pat) + -- read capture indexes + local a,b,c,d,e = ShaguTweaks.GetCaptures(pat) + local _, _, va, vb, vc, vd, ve = string.find(str, ShaguTweaks.SanitizePattern(pat)) + + -- put entries into the proper return values + local ra, rb, rc, rd, re + ra = e == "1" and ve or d == "1" and vd or c == "1" and vc or b == "1" and vb or va + rb = e == "2" and ve or d == "2" and vd or c == "2" and vc or a == "2" and va or vb + rc = e == "3" and ve or d == "3" and vd or a == "3" and va or b == "3" and vb or vc + rd = e == "4" and ve or a == "4" and va or c == "4" and vc or b == "4" and vb or vd + re = a == "5" and va or d == "5" and vd or c == "5" and vc or b == "5" and vb or ve + + return ra, rb, rc, rd, re +end + + +local timer +ShaguTweaks.QueueFunction = function(a1,a2,a3,a4,a5,a6,a7,a8,a9) + if not timer then + timer = CreateFrame("Frame") + timer.queue = {} + timer.interval = TOOLTIP_UPDATE_TIME + timer.DeQueue = function() + local item = table.remove(timer.queue,1) + if item then + item[1](item[2],item[3],item[4],item[5],item[6],item[7],item[8],item[9]) + end + if table.getn(timer.queue) == 0 then + timer:Hide() -- no need to run the OnUpdate when the queue is empty + end + end + timer:SetScript("OnUpdate",function() + this.sinceLast = (this.sinceLast or 0) + arg1 + while (this.sinceLast > this.interval) do + this.DeQueue() + this.sinceLast = this.sinceLast - this.interval + end + end) + end + table.insert(timer.queue,{a1,a2,a3,a4,a5,a6,a7,a8,a9}) + timer:Show() -- start the OnUpdate +end + +ShaguTweaks.strsplit = function(delimiter, subject) + if not subject then return nil end + local delimiter, fields = delimiter or ":", {} + local pattern = string.format("([^%s]+)", delimiter) + string.gsub(subject, pattern, function(c) fields[table.getn(fields)+1] = c end) + return unpack(fields) +end + +ShaguTweaks.rgbhex = function(r, g, b, a) + if type(r) == "table" then + if r.r then + _r, _g, _b, _a = r.r, r.g, r.b, (r.a or 1) + elseif table.getn(r) >= 3 then + _r, _g, _b, _a = r[1], r[2], r[3], (r[4] or 1) + end + elseif tonumber(r) then + _r, _g, _b, _a = r, g, b, (a or 1) + end + + if _r and _g and _b and _a then + -- limit values to 0-1 + _r = _r + 0 > 1 and 1 or _r + 0 + _g = _g + 0 > 1 and 1 or _g + 0 + _b = _b + 0 > 1 and 1 or _b + 0 + _a = _a + 0 > 1 and 1 or _a + 0 + return string.format("|c%02x%02x%02x%02x", _a*255, _r*255, _g*255, _b*255) + end + + return "" +end + +local border = { + edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border", + tile = true, tileSize = 8, edgeSize = 16, + insets = { left = 0, right = 0, top = 0, bottom = 0 } +} +ShaguTweaks.AddBorder = function(frame, inset, color) + if not frame then return end + if frame.ShaguTweaks_border then return frame.ShaguTweaks_border end + + local top, right, bottom, left + + if type(inset) == "table" then + top, right, bottom, left = unpack((inset)) + left, bottom = -left, -bottom + end + + if not frame.ShaguTweaks_border then + frame.ShaguTweaks_border = CreateFrame("Frame", nil, frame) + frame.ShaguTweaks_border:SetPoint("TOPLEFT", frame, "TOPLEFT", (left or -inset), (top or inset)) + frame.ShaguTweaks_border:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", (right or inset), (bottom or -inset)) + frame.ShaguTweaks_border:SetBackdrop(border) + + if color then + frame.ShaguTweaks_border:SetBackdropBorderColor(color.r, color.g, color.b, 1) + end + end + + return frame.ShaguTweaks_border +end + +ShaguTweaks.round = function(input, places) + if not places then places = 0 end + if type(input) == "number" and type(places) == "number" then + local pow = 1 + for i = 1, places do pow = pow * 10 end + return floor(input * pow + 0.5) / pow + end +end + +ShaguTweaks.Abbreviate = function(number) + local sign = number < 0 and -1 or 1 + number = math.abs(number) + + if number > 1000000 then + return ShaguTweaks.round(number/1000000*sign,2) .. "m" + elseif number > 10000 then + return ShaguTweaks.round(number/10000*sign,2) .. "k" + end + + return number +end diff --git a/main.lua b/main.lua new file mode 100644 index 0000000..62ba102 --- /dev/null +++ b/main.lua @@ -0,0 +1,39 @@ +local _G = _G or getfenv(0) + +SLASH_RELOAD1 = '/rl' +function SlashCmdList.RELOAD(msg, editbox) ReloadUI() end + +message = function(msg) + DEFAULT_CHAT_FRAME:AddMessage("|cffcccc33INFO: |cffffff55" .. ( msg or "nil" )) +end +print = message + +ShaguTweaks = CreateFrame("Frame") +ShaguTweaks.mods = {} + +-- load translation tables +ShaguTweaks.L = (ShaguTweaks_locale[GetLocale()] or ShaguTweaks_locale["enUS"]) + +ShaguTweaks:RegisterEvent("VARIABLES_LOADED") +ShaguTweaks:SetScript("OnEvent", function() + -- initialize empty config + if not ShaguTweaks_config then ShaguTweaks_config = {} end + + -- read all registered mods + for title, mod in pairs(ShaguTweaks.mods) do + -- write initial default config + if not ShaguTweaks_config[title] then + ShaguTweaks_config[title] = mod.enabled and 1 or 0 + end + + -- load enabled mods + if ShaguTweaks_config[title] == 1 then + mod:enable() + end + end +end) + +ShaguTweaks.register = function(self, mod) + ShaguTweaks.mods[mod.title] = mod + return ShaguTweaks.mods[mod.title] +end