diff 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 diff
--- a/Main.lua	Sun Nov 28 18:59:31 2010 +0300
+++ b/Main.lua	Sat Dec 04 05:53:52 2010 +0300
@@ -1,18 +1,38 @@
 
 
-RTT = LibStub("AceAddon-3.0"):NewAddon("RaidTargetTactics", "AceConsole-3.0")
+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 AceGUI = LibStub("AceGUI-3.0")
+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() RTT:OnIconClick() end,
+    OnClick = function(frame, button) RTT:OnIconClick(frame, button) end,
     OnTooltipShow = function(tooltip)
         tooltip:AddLine("Raid Target Tactics")
         tooltip:AddLine(L.Minimap_Icon_Tooltip)
@@ -25,14 +45,21 @@
         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()
@@ -40,13 +67,43 @@
             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", nil, UIParent)
+    self.f = CreateFrame("Frame", "__RTT_MAIN", UIParent)
     self.f:SetWidth(300)
-    self.f:SetHeight(200)
+    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",
@@ -76,43 +133,118 @@
     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
-        icon:draw(self.f, 20, num * -20)
+        self.icons[num] = icon
+        icon:draw(self.f, 16, (num - 1) * -25 - 30)
 
-        local field = CreateFrame("EditBox", "__RTT_EditBox_" .. tostring(num), self.f, "InputBoxTemplate")
-        field:SetAutoFocus(false)
-        field:SetFontObject(ChatFontNormal)
-        field:SetWidth(100)
-        field:SetHeight(16)
-        field:SetTextInsets(0, 0, 3, 3)
-        field:SetPoint("TOPLEFT", 40, num * -20)
-        table.insert(self.fields, field)
-        field:Show()
+        local field = RoleField()
+        field:draw(self.f, 45, (num -1) * -25 - 30)
+        self.fields[num] = field
 
-        local userSelect = AceGUI:Create("Dropdown")
-        userSelect.frame:SetParent(self.f)
-        local raid = {}
-        for i=1, GetNumRaidMembers() do
-            raid[i] = UnitName("raid" .. i)
+        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
-        userSelect:SetList(raid)
-        userSelect:SetWidth(100)
-        userSelect:SetHeight(16)
-        userSelect:SetPoint("TOPLEFT", 160, num * -20)
-        table.insert(self.selects, userSelect)
-        userSelect.frame:Show()
+        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:OnIconClick()
-    if self.db.profile.visible then
-        self:HideFrame()
+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
-        self:ShowFrame()
+        if self.db.profile.visible then
+            self:HideFrame()
+        else
+            self:ShowFrame()
+        end
+        self.db.profile.visible = not self.db.profile.visible
     end
-    self.db.profile.visible = not self.db.profile.visible
 end
 
 function RTT:HideFrame()
@@ -123,6 +255,62 @@
     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
+
+