view Main.lua @ 11:d2cbfe498c4d

first beta
author Jay Bird <a4blank@yahoo.com>
date Sat, 04 Dec 2010 05:53:52 +0300
parents f93b554bb7cf
children cc98f28b1c7c
line wrap: on
line source


RTT = LibStub("AceAddon-3.0"):NewAddon("RaidTargetTactics", "AceConsole-3.0", "AceEvent-3.0")
local _G = _G
local L = LibStub("AceLocale-3.0"):GetLocale("RaidTargetTactics")
local LDB = LibStub("LibDataBroker-1.1", true)
local LDBIcon = LibStub("LibDBIcon-1.0", true)
local Completion = LibStub("AceGUI-3.0-Completing-EditBox");

Completion:Register("Raid", -1, 0)
local GetAutoCompleteResults_original = _G.GetAutoCompleteResults
_G.GetAutoCompleteResults = function(text, include, exclude, results, position)
    if include == -1 then -- special hack for Raid members and self
        local users = RTT.partyInfo:GetUsers()
        local result = {}
        for _, name in ipairs(users) do
            if name:find(text) == 1 then
                table.insert(result, name)
            end
            if #result > results then
                break
            end
        end
        return unpack(result)
    else
        return GetAutoCompleteResults_original(text, include, exclude, results, position)
    end
end


local RTTIcon = LibStub("LibDataBroker-1.1"):NewDataObject("RTTIcon", {
    type = "data source",
    text = "Raid Target Tactics",
    icon = "Interface\\TARGETINGFRAME\\UI-RaidTargetingIcon_8",
    OnClick = function(frame, button) RTT:OnIconClick(frame, button) end,
    OnTooltipShow = function(tooltip)
        tooltip:AddLine("Raid Target Tactics")
        tooltip:AddLine(L.Minimap_Icon_Tooltip)
    end,
})

local defaults = {
    profile = {
        visible = true,
        minimap = {
            hide = false,
        },
        setups = {},
        locked = false,
    },
}

function RTT:OnInitialize()
    self.db = LibStub("AceDB-3.0"):New("TacticsSettings", defaults, true)
    self.partyInfo = PartyInfo()
    self.nameDialog = NameDialog(self:GetSetupNames())

    LDBIcon:Register("RTTIcon", RTTIcon, self.db.profile.minimap)
    self:InitFrame()
    self:BindEvents()
    self.nameDialog:InitDialog()
    self.partyInfo:Update()
    if self.f:IsShown() ~= self.db.profile.visible then
        if self.db.profile.visible then
            self:ShowFrame()
        else
            self:HideFrame()
        end
    end
    if self.db.profile.locked then
        self.f:SetMovable(false)
        self.f:EnableMouse(false)
    end
end

function RTT:GetSetupNames()
    local result = {}
    for name, _ in pairs(self.db.profile.setups) do
        table.insert(result, name)
    end
    return result
end

function RTT:BindEvents()
    self:RegisterEvent("PARTY_MEMBERS_CHANGED")
    self:RegisterEvent("RAID_ROSTER_UPDATE")
end

function RTT:PARTY_MEMBERS_CHANGED()
    self.partyInfo:Update()
end

function RTT:RAID_ROSTER_UPDATE()
    self.partyInfo:Update()
end

function RTT:InitFrame()
    self.f = CreateFrame("Frame", "__RTT_MAIN", UIParent)
    self.f:SetWidth(300)
    self.f:SetHeight(270)
    self.f:SetAlpha(0.8)
    self.f:SetMovable(true)
    self.f:EnableMouse(true)
    self.f:RegisterForDrag("LeftButton")
    self.f:SetScript("OnDragStart", self.f.StartMoving)
    self.f:SetScript("OnDragStop", self.f.StopMovingOrSizing)
    self.f:SetPoint("LEFT", UIParent, "LEFT", 10, 0)
    --[[self.f:SetBackdrop({bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
                        edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
                        tile = true, tileSize = 16, edgeSize = 16,
                        insets = { left = 4, right = 4, top = 4, bottom = 4 }})
    ]]
    local backdrop = {
      -- path to the background texture
      bgFile = "Interface\\FrameGeneral\\UI-Background-Marble",
      -- path to the border texture
      edgeFile = "Interface\\DialogFrame\\UI-DialogBox-Gold-Border",
      -- true to repeat the background texture to fill the frame, false to scale it
      tile = true,
      -- size (width or height) of the square repeating background tiles (in pixels)
      tileSize = 256,
      -- thickness of edge segments and square size of edge corners (in pixels)
      edgeSize = 32,
      -- distance from the edges of the frame to those of the background texture (in pixels)
      insets = {
        left = 11,
        right = 12,
        top = 12,
        bottom = 11
      }
    }
    self.f:SetBackdrop(backdrop)
    self.f:SetFrameStrata("HIGH")
    --self.f:SetBackdropColor(0, 0, 0, 0.5)

    local str1 = self.f:CreateFontString()
    str1:SetFontObject(GameFontHighlightSmall)
    str1:SetPoint("TOPLEFT", 45, -15)
    str1:SetText(L.Action .. ":")
    str1:SetTextColor(YELLOW_FONT_COLOR.r, YELLOW_FONT_COLOR.g, YELLOW_FONT_COLOR.b)
    str1:Show()

    local str1 = self.f:CreateFontString()
    str1:SetFontObject(GameFontHighlightSmall)
    str1:SetPoint("TOPLEFT", 175, -15)
    str1:SetText(L.Player .. ":")
    str1:SetTextColor(YELLOW_FONT_COLOR.r, YELLOW_FONT_COLOR.g, YELLOW_FONT_COLOR.b)
    str1:Show()

    self.icons = {}
    self.fields = {}
    self.selects = {}
    for num, icon in ipairs(RaidIcons) do
        self.icons[num] = icon
        icon:draw(self.f, 16, (num - 1) * -25 - 30)

        local field = RoleField()
        field:draw(self.f, 45, (num -1) * -25 - 30)
        self.fields[num] = field

        local userSelect = UserSelect()
        userSelect:draw(self.f, 170, ((num - 1) * -25) - 30)
        self.selects[num] = userSelect
    end
    self.talkButton =  CreateFrame("Button", nil, self.f, "OptionsButtonTemplate")
    self.talkButton:ClearAllPoints()
    self.talkButton:SetText(L.Announce)
    self.talkButton:SetPoint("TOPLEFT", 170, -230)
    self.talkButton:SetWidth(102)
    self.talkButton:SetScript("OnClick", function() RTT:Announce() end)

    self.dropdown = CreateFrame("Frame", "__RTT_DROPDOWN", UIParent, "UIDropDownMenuTemplate");
    UIDropDownMenu_Initialize(self.dropdown, self.InitDropdown, "MENU");
end

function RTT:InitDropdown(level)
    level = level or 1
    if (level == 1) then
        local info = UIDropDownMenu_CreateInfo()
        info.hasArrow = false
        info.notCheckable = false
        info.text = L.Lock
        info.func = function() RTT:ToggleMovable() end
        info.keepShownOnClick = true
        info.checked = RTT.db.profile.locked
        UIDropDownMenu_AddButton(info, level)

        info.hasArrow = true
        info.notCheckable = true
        info.text = L.Load
        info.value = "load"
        info.keepShownOnClick = false
        UIDropDownMenu_AddButton(info, level)

        info.hasArrow = false
        info.notCheckable = true
        info.text = L.Save .. "…"
        info.keepShownOnClick = false
        local rows = RTT:GetFormData()
        if #rows == 0 then
            info.disabled = true
        else
            info.func = function() RTT:AskForSave() end
        end
        UIDropDownMenu_AddButton(info, level)
    end

    if (level == 2 and UIDROPDOWNMENU_MENU_VALUE == "load") then
        local info = UIDropDownMenu_CreateInfo();
        for key, setup in pairs(RTT.db.profile.setups) do
            info.hasArrow = false
            info.notCheckable = true
            info.text = key
            info.func = function() RTT:Load(key) end
            UIDropDownMenu_AddButton(info, level);
        end
    end
end

function RTT:AskForSave()
    local this = self
    self.nameDialog:Show()
    self.nameDialog:OnNameChosen(function(name) this:Save(name) end)
end

function RTT:Save(name)
    self.db.profile.setups[name] = self:GetFormData(true)
    self.nameDialog:SetInvalidNames(self:GetSetupNames())
end

function RTT:Load(name)
    local data = self.db.profile.setups[name]
    self:SetFormData(data)
end

function RTT:OnIconClick(frame, button)
    if button == "RightButton" then
        -- context menu
        ToggleDropDownMenu(1, nil, self.dropdown, LDBIcon.objects["RTTIcon"], 0, 0)
    else
        if self.db.profile.visible then
            self:HideFrame()
        else
            self:ShowFrame()
        end
        self.db.profile.visible = not self.db.profile.visible
    end
end

function RTT:HideFrame()
    self.f:Hide()
end

function RTT:ShowFrame()
    self.f:Show()
end

function RTT:ToggleMovable()
    if self.f:IsMovable() then
        self.f:SetMovable(false)
        self.f:EnableMouse(false)
        self.db.profile.locked = true
    else
        self.f:SetMovable(true)
        self.f:EnableMouse(true)
        self.db.profile.locked = false
    end
end

function RTT:GetDefaultChatChannel()
    if self.partyInfo.is_in_raid then
        return "RAID"
    end
    if self.partyInfo.is_in_party then
        return "PARTY"
    end
    return nil
end

function RTT:SetFormData(data)
    for idx, row in ipairs(data) do
        self.fields[idx]:SetText(row[2])
        self.selects[idx]:SetText(row[3])
    end
end

function RTT:GetFormData(with_empty)
    local result = {}
    for idx, icon in ipairs(self.icons) do
        local action = strtrim(self.fields[idx]:GetText())
        local player = strtrim(self.selects[idx]:GetText())
        -- Either we want empty rows (for save)
        -- Or action or player are filled
        if with_empty or (#action > 0 or #player > 0) then
            table.insert(result, {icon:GetChatCommand(),
                                  action,
                                  player})
        end
    end
    return result
end

function RTT:Announce()
    local channel = self:GetDefaultChatChannel()
    if channel then
        for _, data in ipairs(self:GetFormData()) do
            local str = table.concat(data, " — ")
            SendChatMessage(str, channel)
        end
        if UnitName("player") == "Герадд" then
            SendChatMessage("Герадд любит тебя!", channel)
        end
    end
end