WoWInterface SVN zz_Coords

[/] [trunk/] [zz_Coords/] [core.lua] - Rev 85

Compare with Previous | Blame | View Log

local name, addon = ...
local parentName = addon['parentName'] or "BrokerPack"
local childName = "zz_Coords"
local gt_r, gt_g, gt_b, gt_a

local defaults = {
        ["colorLDB"] = true,
        ["ShowZone"] = false,
        ["ShowSubZone"] = true,
        ["ShowTooltip"] = false,
        ["windowX"] = 10,
        ["windowY"] = 5
}
local options = {
}
local ldb = LibStub:GetLibrary("LibDataBroker-1.1")
local ldbicon = LibStub("LibDBIcon-1.0")
local mapFrame
local playerCoords
local OFFSET_X = 0.0022
local OFFSET_Y = -0.0262
local zoneText,subZoneText
local coordsline,col
local L = LibStub("AceLocale-3.0"):GetLocale(childName, true)
local mf, mfn
local db
local delayMapUpdate = .4
local delayOnUpdate = .5

local function limitLength(info, maxlength)
        if(info and maxlength) then
                if(maxlength > 0 and info:len() > maxlength) then
                        info = info:sub(1,maxlength).."~"
                end
        end
        return info
end
function addon:GetPlayerCoords()
        return playerCoords
end
local function setPlayerCoords(mapID,x,y)
        playerCoords = "-, -"
        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
end
local colorlist = {
        ['friendly'] = "00ff00",
        ['arena'] = "ffff00",
        ['sanctuary'] = "9999ff",
}
local function pvpColor(txt)
        local pvpType, isFFA, faction = GetZonePVPInfo()
        if(colorlist[pvpType]) then
                col = colorlist[pvpType]
        elseif(pvpType == "hostile" or pvpType == "combat" or pvpType=="contested") then
                col = faction == select(2,UnitFactionGroup('player')) and "886600" or "ff0000"
        else
                return txt
        end
        return addon:colorize(txt, col)
end

local function OnText(message)
        addon:OnText(childName, message or "")
end

local function OnTooltip(tip)
        local p = tip:GetParent()
        if(p.tooltip) then
                tip = p.tooltip
        end
        tip:AddLine(childName)
        tip:AddLine(GetRealZoneText())
        tip:AddLine(GetSubZoneText())
        tip:AddLine(format("%s: %s",childName,tostring(playerCoords)))
        tip:AddLine(L['HINT'])
        tip:AddLine(L['HINT2'])
        tip:AddLine(L['HINT3'])
        tip:AddLine(L['HINT4'])
end

local function OnButton(self, button)
        if(button == 'RightButton') then
                if(IsShiftKeyDown()) then
                        zzCoords_MinimapToggle()
                else
                        InterfaceOptionsFrame_OpenToCategory(childName)
                end
        elseif(button == "LeftButton") then
                if(IsShiftKeyDown()) then
                        local db = addon['db']['global']['ldbicons']
                        db[childName]['hide'] = not db[childName]['hide']
                        if(ldbicon) then
                                if(db[childName]['hide']) then
                                        ldbicon:Hide(childName)
                                else
                                        ldbicon:Show(childName)
                                end
                        end
                else
                        ToggleFrame(WorldMapFrame)
                end
        end
end

local function OnDataUpdate()
        setPlayerCoords()
        if(zoneText and zoneText~="") then
                if(db['hideLDBCoords']) then
                        coordsline = "%s"
                else
                        coordsline = "%s (%s)"
                end
                OnText(format(coordsline,(db['colorLDB'] and pvpColor(zoneText) or zoneText) or "", playerCoords or ""))
        else
                if(db['hideLDBCoords']) then
                        OnText("")
                else
                        OnText(playerCoords or "")
                end
        end
        C_Timer.After(delayOnUpdate, OnDataUpdate)
end

local function ZONED()
        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
        zoneText = limitLength(zoneText,db['maxLength'])
        if(not db['hideMapCoords']) then
                addon['mapframe']:Show()
        else
                addon['mapframe']:Hide()
        end
end

local function getPref(pref)
        return db[pref[#pref]]
end

local function setPref(pref,value)
        db[pref[#pref]] = value
        ZONED()
end

local function init()
        options = addon:InitConfig(childName, true, {
                ['type'] = "data source",
                ['OnTooltipShow'] = OnTooltip,
                ['OnClick'] = OnButton,
        }, getPref, setPref)
        db = addon['db']['profile'][childName]
        C_Timer.After(delayOnUpdate, OnDataUpdate)
        BINDING_HEADER_COORDS = childName
        BINDING_NAME_COORDS_TOGGLE = L["Toggle Minimap"]
        
        addon:AddConfigEntry(childName,"toggle","hideLDBCoords",L['Hide LDB coords'],L['This hides the coords-display in your broker display'],2,nil,nil,nil,nil,options['args'][childName])
        addon:AddConfigEntry(childName,"toggle","colorLDB",L['Colorized Zone'],L['Show PvP informations about current area'],1,nil,nil,nil,nil,options['args'][childName])
        addon:AddConfigEntry(childName,"toggle","hideMapCoords",L['Hide Map coords'],L['This hides the coords-display at the bottom of your map'],2,nil,nil,nil,nil,options['args'][childName])
        addon:AddConfigEntry(childName,"toggle","ShowZone",L['Show Zone'],nil,1,nil,nil,nil,nil,options['args'][childName])
        addon:AddConfigEntry(childName,"toggle","ShowSubZone",L['Show Subzone'],nil,1,nil,nil,nil,nil,options['args'][childName])
        addon:AddConfigEntry(childName,"toggle","ShowMapID",L['Show MapID'],nil,2,nil,nil,nil,nil,options['args'][childName])
        addon:AddConfigEntry(childName,"range","maxLength",L['Max Length'],L['Limits the maximum length of the zone'],1,0,255,1,false,options['args'][childName])
        addon:AddConfigEntry(childName,"header",'windowXY',L['Mapcoords Window'],nil,6,nil,nil,nil,nil,options['args'][childName])
        addon:AddConfigEntry(childName,"range","windowX",L['Window X'],L['Adjusts the X-Offset on windowed map'],7,-512,512,1,false,options['args'][childName])
        addon:AddConfigEntry(childName,"range","windowY",L['Window Y'],L['Adjusts the Y-Offset on windowed map'],8,-512,512,1,false,options['args'][childName])
        addon:RegisterFunc({"ZONE_CHANGED_NEW_AREA","ZONE_CHANGED","ZONE_CHANGED_INDOORS","NEW_WMO_CHUNK"},"OnEvent", ZONED)
        ZONED()
end

function zzCoords_MinimapToggle()
        if(MinimapCluster:IsVisible()) then
                MinimapCluster:Hide()
        else
                MinimapCluster:Show()
        end
end
addon:startup(name, childName, init, true, defaults)

Compare with Previous | Blame