zz_Coords/core.lua

131 lines
3.7 KiB
Lua
Executable File

local addonName, addon = ...
local childName = addon['childName']
local child = addon[childName]
local events = child['events']
local L = LibStub("AceLocale-3.0"):GetLocale(childName, true)
local db
local delayOnUpdate = .5
LibStub("zzHelper"):Embed(child)
local function prepareDB()
db = addon['db']['profile'][childName]
child['db'] = db
addon['callbacks']:Fire(format("Init%s", childName))
child:ZoneEvent()
end
local function setPlayerCoords()
local playerCoords = "-, -"
local mapID = C_Map.GetBestMapForUnit("player")
if(mapID) then
local pmp = C_Map.GetPlayerMapPosition(mapID,"player")
if(pmp) then
local x,y = pmp:GetXY()
if(x and y) then
playerCoords = format("%.1f, %.1f", x*100, y*100)
end
end
end
child['playerCoords'] = playerCoords
end
local colorlist = {
['arena'] = "ff1919",
['combat'] = "ff1919",
['hostile'] = "ff1919",
['friendly'] = "19ff19",
['sanctuary'] = "68ccef",
['contested'] = "ffb200",
}
local function pvpColor(txt)
local pvpType = GetZonePVPInfo()
if(colorlist[pvpType]) then
return child:colorize(txt, colorlist[pvpType])
end
return txt
end
local function OnDataUpdate()
setPlayerCoords()
if(child['zoneText'] and child['zoneText']~="") then
local coordsline = db['hideLDBCoords'] and "%s" or "%s (%s)"
child:OnText(childName, format(coordsline,(db['colorLDB'] and pvpColor(child['zoneText']) or child['zoneText']) or "", child['playerCoords'] or ""))
else
if(db['hideLDBCoords']) then
child:OnText(childName, "")
else
child:OnText(childName, child['playerCoords'] or "")
end
end
if(child['moving']) then
C_Timer.After(delayOnUpdate, OnDataUpdate)
end
end
local function zzCoords_MinimapToggle()
if(MinimapCluster:IsVisible()) then
MinimapCluster:Hide()
else
MinimapCluster:Show()
end
end
local function limitLength(info, maxlength)
if(info and maxlength) then
if(maxlength > 0 and info:len() > maxlength) then
info = format("%s~",info:sub(1,maxlength))
end
end
return info
end
function child:ZoneEvent()
local zoneText = db['ShowZone'] and GetRealZoneText() or ""
if(db['ShowSubZone']) then
if(GetSubZoneText() and GetSubZoneText()~="") then
if(db['ShowZone']) then
zoneText = zoneText ~= GetSubZoneText() and format("%s, %s",zoneText,GetSubZoneText()) or zoneText
else
zoneText = GetSubZoneText()
end
elseif(not db['ShowZone']) then
zoneText = GetRealZoneText()
end
end
child['zoneText'] = limitLength(zoneText, db['maxLength'])
if(not db['hideMapCoords']) then
child['mapframe']:Show()
else
child['mapframe']:Hide()
end
OnDataUpdate()
end
function child:MoveEvent(lib, event)
child['moving'] = event == "PLAYER_STARTED_MOVING" and true or nil
OnDataUpdate()
end
child['specs'] = {
['name'] = childName,
['sv'] = true,
['cfg'] = true,
['cfgReset'] = prepareDB,
['ldb'] = child['ldb'],
['defaults'] = {
['profile'] = {
[childName] = {
["colorLDB"] = true,
["ShowZone"] = false,
["ShowSubZone"] = true,
["ShowTooltip"] = false,
["windowX"] = 10,
["windowY"] = 5
}
}
}
}
function events:PLAYER_ENTERING_WORLD(event)
child:UnregisterEvent('PLAYER_ENTERING_WORLD')
local m = CreateFrame("BUTTON", "zzCoords_MinimapToggleFrame")
m:SetScript("OnClick", zzCoords_MinimapToggle)
if(db['toggleMap'] and db['toggleMap'] ~= '') then
SetBinding(db['toggleMap'], "CLICK zzCoords_MinimapToggleFrame:LeftButton")
end
child:RegisterEvent({"ZONE_CHANGED_NEW_AREA","ZONE_CHANGED","ZONE_CHANGED_INDOORS","NEW_WMO_CHUNK"}, child.ZoneEvent)
child:RegisterEvent({"PLAYER_STARTED_MOVING", "PLAYER_STOPPED_MOVING"}, child.MoveEvent)
end
child:NewAddOn(...)