Quantcast
--[[-------------------------------------------------------------------
--  TomTomLite - Copyright 2010 - James N. Whitehead II
-------------------------------------------------------------------]]--

local addonName, addon = ...
local L = addon.L

addon.callbacks = LibStub("CallbackHandler-1.0"):New(addon)
addon.mapdata = LibStub("LibMapData-1.0")
addon.waypoints = {}

function addon:Initialize()
    self.db = LibStub("AceDB-3.0"):New("TomTomLiteDB", self.defaults)

    self.arrow = self:CreateCrazyArrow("TomTomLiteArrow")
    self.arrow:SetPoint("CENTER", 0, 0)
    self.arrow:Hide()

    self:RegisterMessage("TOMTOMLITE_WAYPOINT_ADDED")
end

function addon:CreateCrazyArrow(name, parent)
    parent = parent or UIParent
    local frame = CreateFrame("Button", name, parent)

    frame:SetSize(128, 128)
    frame.arrow = frame:CreateTexture("OVERLAY")
    frame.arrow:SetAllPoints()
    frame.arrow:SetTexture("Interface\\Addons\\TomTomLite\\images\\arrow-grey")

    frame.title = frame:CreateFontString("OVERLAY", name .. "Title", "GameFontHighlight")
    frame.info = frame:CreateFontString("OVERLAY", name .. "Info", "GameFontHighlight")
    frame.subtitle = frame:CreateFontString("OVERLAY", name .. "Subtitle", "GameFontHighlight")

    frame.title:SetPoint("TOP", frame, "BOTTOM", 0, 0)
    frame.info:SetPoint("TOP", frame.title, "BOTTOM", 0, 0)
    frame.subtitle:SetPoint("TOP", frame.info, "BOTTOM", 0, 0)

    frame:Hide()

    -- Set up the OnUpdate handler
    frame:SetScript("OnUpdate", function(self, elapsed)
        local map, floor, x, y = unpack(self.waypoint)
        local distance, angle = addon.mapdata:DistanceAndDirection(map, 0, x, y)
        local facing = GetPlayerFacing()

        local faceangle = angle - facing

        local perc = math.abs((math.pi - math.abs(faceangle)) / math.pi)
        local gr,gg,gb = unpack(addon.db.profile.goodcolor)
        local mr,mg,mb = unpack(addon.db.profile.middlecolor)
        local br,bg,bb = unpack(addon.db.profile.badcolor)
        local r,g,b = addon:ColorGradient(perc, br, bg, bb, mr, mg, mb, gr, gg, gb)

        self.arrow:SetVertexColor(r,g,b)
        self.arrow:SetRotation(faceangle)

        self.subtitle:SetFormattedText("%.1f yards", distance)
    end)

    -- Code to handle moving the frame
    frame:SetMovable(true)
    frame:RegisterForDrag("LeftButton")
    frame:SetScript("OnDragStart", frame.StartMoving)
    frame:SetScript("OnDragStop", function(self)
        self:SetUserPlaced(false)
        self:StopMovingOrSizing()
        addon:SavePosition(self)
    end)
    frame:SetScript("OnHide", frame:GetScript("OnDragStop"))
    self:RestorePosition(frame)

    return frame
end

function addon:AddWaypoint(map, floor, x, y, opt)
    local waypoint = {map, floor, x, y}
    if type(opt) == "table" then
        for k, v in pairs(opt) do
            if type(k) ~= "number" then
                waypoint[k] = v
            end
        end
    end

    table.insert(self.waypoints, waypoint)
    self:FireMessage("TOMTOMLITE_WAYPOINT_ADDED", waypoint)
end

function addon:TOMTOMLITE_WAYPOINT_ADDED(msg, waypoint, ...)
    local zone, floor, x, y = unpack(waypoint)
    local lzone = self.mapdata:MapLocalize(zone)
    self:Printf("Added a waypoint at (%.2f, %.2f) in %s", x * 100, y * 100, lzone)

    self.arrow.waypoint = waypoint
    self.arrow.title:SetText(waypoint.title or L["Unknown waypoint"])
    self.arrow.info:SetFormattedText("%.2f, %.2f - %s", x * 100, y * 100, lzone)
    self.arrow:Show()
end