Quantcast
WoWInterface: SVN - LanguageCycle - Rev 8 - /LanguageCycle.lua

WoWInterface SVN LanguageCycle

[/] [LanguageCycle.lua] - Rev 8

Compare with Previous | Blame | View Log

local ButtonTexture = "Interface\\Addons\\LanguageCycle\\media\\LanguageCycleIcon"
local addonName = "LanguageCycle"

BINDING_HEADER_LANGUAGECYCLE="Language Cycle";

local defaults = {
        saved_language = {};
        translate_group = true;
        panel_visible = true;
        panel_locked = false;
        minimapIcon = {
                hide = false,
                minimapPos = 220,
                radius = 80,
                }
}
if not LANGUAGE_CYCLE then
                LANGUAGE_CYCLE = defaults
        else
                for k, v in pairs(defaults) do
                        if LANGUAGE_CYCLE[k] == nil or type(LANGUAGE_CYCLE[k]) ~= type(v) then
                                LANGUAGE_CYCLE[k] = v
                        end
                end
        end

LC_default_language = "";
LC_verbose = true;
LC_unknown = "Unknown Entity";
LC_realm_name = "";
LC_player_name = "";
LC_player_id = nil;
LC_panel_height = 0;
LC_panel_width = 0;
LC_version = "5.3.0c, by Halya, Argent Dawn, EU";
LC_Help_Text={};
LC_Help_Text[0]="Language Cycle will respond to the following commands:";
LC_Help_Text[1]="- /lc        : Toggles spoken language between racial/common";
LC_Help_Text[2]="- /lc status : Displays current settings";
LC_Help_Text[3]="- /lc toggle : Toggles Guild/Party translation on/off";
LC_Help_Text[4]="- /lc help   : Displays this message";
LC_Help_Text[5]="- /lc show   : Display the language cycle button";
LC_Help_Text[6]="- /lc hide   : Hide the language cycle button";
LC_Help_Text[7]="- /lc MM     : Toggle the language cycle mini-map button";
LC_Help_Text[8]="--------------------------------------------------------------";

--Save Blizzard's original SendText function
local LC_Blizzard_SendChatMessage;

local LCButtonFrameObject = LibStub:GetLibrary("LibDataBroker-1.1"):NewDataObject(addonName, {
                        type = "data source",
                        text = LANGUAGE_CYCLE.minimapIcon.text,
                        icon = ButtonTexture,
                        OnClick = function(frame, button)
                                GameTooltip:Hide()
                                if button =="LeftButton" then
                                        Language_Cycle_Cycle();
                                elseif button =="MiddleButton" then
                                        local t = 0;
                                        while LC_Help_Text[t] ~= nil do
                                                DEFAULT_CHAT_FRAME:AddMessage(LC_Help_Text[t],0.0, 1.0, 0.0);
                                        t = t + 1;
                                        end
                                elseif button == "RightButton"  then
                                        Language_Cycle_Panel_Visible();
                                end
                        end,
                        OnTooltipShow = function(tooltip)
                                if tooltip and tooltip.AddLine then
                                        tooltip:SetText("LanguageCycle")
                                        tooltip:AddLine("Left-Click: Cycle Language|nRight-Click: Show/Hide Built-in Panel|nMiddle-Click: Show Help",.8,.8,.8,1)
                                        tooltip:Show()
                                end
                        end,
                })

function Language_Cycle_OnLoad( self )
   -- Hook SendChatMessage
   LC_Blizzard_SendChatMessage = SendChatMessage;
   SendChatMessage = LC_SendChatMessage;

   SlashCmdList["LANGUAGECYCLE"] = Language_Cycle_Slash;
   SLASH_LANGUAGECYCLE1 = "/lc";
   ShowUIPanel(Language_Cycle_Panel);
   if ( not Language_Cycle_Panel:IsVisible() ) then
      Language_Cycle_Panel:SetOwner(UIParent, "ANCHOR_PRESERVE");
   end
   
   Language_Cycle_Panel_Backdrop:SetBackdropBorderColor(1,1,1);
   Language_Cycle_Panel_Backdrop:SetBackdropColor(0.3, 0.3, 0.3);
   Language_Cycle_Backdrop_Hide();
   LC_panel_height = Language_Cycle_Panel:GetHeight();
   LC_panel_width = Language_Cycle_Panel:GetWidth();
   Language_Cycle_Party_Toggle_Text:SetJustifyH("Right");
   Language_Cycle_Add_Message("LanguageCycle loaded; Type '/lc help' for a list of options;  Version " .. LC_version);

   -- self:SetPadding(16);
   self:RegisterForDrag("LeftButton");
   self:RegisterEvent("VARIABLES_LOADED");
   self:RegisterEvent("PLAYER_ENTERING_WORLD");

end

function Language_Cycle_Get_Version()
   return LC_version
end

-- replace Blizzard's SendChatMessage function instead. -Nhani
function LC_SendChatMessage(msg, chatType, language, channel)

  if (LC_player_id ~= nil) then
    language = LANGUAGE_CYCLE.saved_language[LC_player_id];
  end

-- Small change here. Instead of defining what channels we WANT
-- in a common language, we define what channels we DON'T want.
-- Since those are far fewer in number. -Nhani
  if (LANGUAGE_CYCLE.translate_group) then
    if (chatType ~= "SAY" and chatType ~= "YELL") then
      if (language ~= LC_default_language) then
        language = LC_default_language;
      end
    end
  end
        local numLanguages = GetNumLanguages();
        local i;
        for i = 1, numLanguages, 1 do
                local getlanguage, getlanguageID = GetLanguageByIndex(i);
                if (language == getlanguage) then
                        languageID = getlanguageID;
                end
        end
                
  LC_Blizzard_SendChatMessage(msg, chatType, languageID, channel);

end


function Language_Cycle_Panel_Visible()
   if (Language_Cycle_Panel:IsVisible() ) then
      HideUIPanel(Language_Cycle_Panel);
      LANGUAGE_CYCLE.panel_visible = false;
   else
      ShowUIPanel(Language_Cycle_Panel);
      LANGUAGE_CYCLE.panel_visible = true;
   end
end


function Language_Cycle_Backdrop_Show(override)
   if (LANGUAGE_CYCLE.panel_locked and not override) then
   else
      Language_Cycle_Panel_Backdrop:Show();
   end
end


function Language_Cycle_Backdrop_Hide(override)
   if (LANGUAGE_CYCLE.panel_locked and not override) then
   else
      Language_Cycle_Panel_Backdrop:Hide();
   end
end


function Language_Cycle_Cycle()
   local numLangs = GetNumLanguages();
   local currentlang = 0;
    local lang;
   lang = Language_Cycle_Get_Lang();

   for i=1,numLangs,1 do
      if (lang == GetLanguageByIndex(i)) then
         currentlang = i;
      end
   end
   if (currentlang == numLangs) then
      currentlang = 1;
   else
      currentlang = currentlang + 1;
   end
   Language_Cycle_Set_Lang(GetLanguageByIndex(currentlang));
   -- if (IsAddOnLoaded("Titan")) then
      -- TitanPanelButton_UpdateButton(TITAN_LANGUAGECYCLE_ID);
   -- end
      -- if (IsAddOnLoaded("FuBar")) then
        -- LanguageCycleFuBar:UpdateText()
      -- end
--   For debugging
--   Langauge_Cycle_Debug_Message();
end

function Language_Cycle_Language_Clicked(button)

   if (button == "LeftButton") then
      LC_verbose = false;
      Language_Cycle_Cycle();
      LC_verbose = true;
   end
   if (button == "RightButton") then
      LANGUAGE_CYCLE.panel_locked = not LANGUAGE_CYCLE.panel_locked;
      Langauge_Cycle_Lock_Backdrop();
   end
end


function Langauge_Cycle_Lock_Backdrop()
   if (LANGUAGE_CYCLE.panel_locked) then
      Language_Cycle_Backdrop_Hide(true);
      Language_Cycle_Panel:SetHeight(Language_Cycle_Button:GetHeight());
      Language_Cycle_Panel:SetWidth(Language_Cycle_Button:GetWidth());
   else
      Language_Cycle_Backdrop_Show(true);
      Language_Cycle_Panel:SetHeight(LC_panel_height);
      Language_Cycle_Panel:SetWidth(LC_panel_width);
   end
end


function Language_Cycle_Variables_Loaded()
   --Called when variables_loaded event fires, usually on UI reload
   if (LANGUAGE_CYCLE.panel_visible) then
      ShowUIPanel(Language_Cycle_Panel);
   else
      HideUIPanel(Language_Cycle_Panel);
   end
   Langauge_Cycle_Lock_Backdrop();
--   For debugging
--   Language_Cycle_Debug_Message();
-- Language_Cycle_Add_Message("Onload done");
end

function Language_Cycle_OnEvent( self, event, ... )

   if (event == "VARIABLES_LOADED") then
      Language_Cycle_Variables_Loaded();
      -- Add LanguageCycle to myAddOns addons list
      if(myAddOnsFrame) then
         myAddOnsList.LanguageCycle = {
            name = "LanguageCycle",
            description = "A button to change your spoken language",
            version = Language_Cycle_Get_Version(),
            category = MYADDONS_CATEGORY_CHAT,
            frame = "Language_Cycle_Panel"};
      end
                ----------------------------------------------------------------------------------------
        -- LDB Button Creation
        ----------------------------------------------------------------------------------------

        
        
                local defaults = {
                minimapIcon = {
                        hide = false,
                        minimapPos = 220,
                        radius = 80,
                        text = lang,
                        }
                }
                if not LANGUAGE_CYCLE then
                        LANGUAGE_CYCLE = defaults
                else
                        for k, v in pairs(defaults) do
                                if LANGUAGE_CYCLE[k] == nil or type(LANGUAGE_CYCLE[k]) ~= type(v) then
                                        LANGUAGE_CYCLE[k] = v
                                end
                        end
                end
                

                LibStub("LibDBIcon-1.0"):Register(addonName, LCButtonFrameObject, LANGUAGE_CYCLE.minimapIcon)
                
   end
   if (event == "PLAYER_ENTERING_WORLD") then 
      Language_Cycle_Init();
   end
end                             



function Language_Cycle_Init()
   --Called when the Player_Entering_World event fires, usually on character login.
   -- May be called later, if initialization fails
   LC_realm_name = string.gsub(GetRealmName(),"'","");
   LC_player_name = UnitName("player");
   LC_default_language = GetDefaultLanguage();
   LC_verbose = false;
   Language_Cycle_Backdrop_Hide(true);
   Language_Cycle_Set_Text_Color();
   local lang;

   if (LC_player_name == nil or LC_player_name == LC_unknown or LC_realm_name == nil) then
   else
      LC_player_id = LC_realm_name .. " " .. LC_player_name;
   end

   lang = Language_Cycle_Get_Lang();

   if (lang == nil) then
   else
      Language_Cycle_Set_Lang(lang)
   end

   if (LC_default_language == nil) then
   else
      Language_Cycle_Party_Toggle_Text:SetText("Speak " .. LC_default_language .. " in Party/Guild chat:");
   end

   Language_Cycle_Set_Party_Checkbutton();

   LC_verbose = true;
--   For debugging
--   Language_Cycle_Debug_Message();
--   Language_Cycle_Add_Message("Entering World Done");
end


function Language_Cycle_OnUpdate( self, elapsed )
   if (not LC_player_id) then
      Language_Cycle_Init();
   end
end


function Language_Cycle_Set_Lang(lang)
   -- Updates the button text, chat window and returns a chat window message
   Language_Cycle_Button:SetText(lang);
   DEFAULT_CHAT_FRAME.editBox.language = lang;
   LANGUAGE_CYCLE.minimapIcon.text = lang;
   LCButtonFrameObject.text = lang
  
   if (LC_verbose) then
      Language_Cycle_Add_Message(string.format("Now speaking %s", lang));
   end

   Language_Cycle_Save_Lang(lang);
end


function Language_Cycle_Save_Lang(lang)
   if (LC_player_id == nil) then
   else
      LANGUAGE_CYCLE.saved_language[LC_player_id] = lang;
   end
end


function Language_Cycle_Get_Lang()
   local lang, langID;
   if (LC_player_id == nil) then
   else
      lang = DEFAULT_CHAT_FRAME.editBox.language;
          if (LANGUAGE_CYCLE.saved_language[LC_player_id] == nil) then
         if (lang == nil) then
            lang = LC_default_language;
         end
      else
         lang = LANGUAGE_CYCLE.saved_language[LC_player_id];
      end
   end
   return lang
end


function Language_Cycle_Debug_Message()
   local lang;
   lang = LANGUAGE_CYCLE.saved_language[LC_player_id];
   if (lang == nil) then
      lang = "unknown";
   end
   if not(LC_player_name == nil) then
      message(LC_player_name .. " speaks " .. lang);
   else
      message("LC_player_name is nil");
   end
end


function Language_Cycle_Toggle_Group(verbose)
   if (LANGUAGE_CYCLE.translate_group) then
      LANGUAGE_CYCLE.translate_group = false;
      if (verbose) then
         Language_Cycle_Add_Message("Guild/Party translation is now off.");
      end
   else
      LANGUAGE_CYCLE.translate_group = true;
      if (verbose) then
         Language_Cycle_Add_Message("Guild/Party translation is now on.");
      end
   end
   Language_Cycle_Set_Text_Color();
   Language_Cycle_Set_Party_Checkbutton();
   -- if (IsAddOnLoaded("Titan")) then
      -- TitanPanelButton_UpdateButton(TITAN_LANGUAGECYCLE_ID);
   -- end
      -- if (IsAddOnLoaded("FuBar")) then
        -- LanguageCycleFuBar:UpdateText()
      -- end
end


function Language_Cycle_Set_Party_Checkbutton()
   if (LANGUAGE_CYCLE.translate_group) then
      Language_Cycle_Party_Toggle:SetChecked(1);
   else
      Language_Cycle_Party_Toggle:SetChecked(0);
   end
end


function Language_Cycle_Set_Text_Color()
   if (LANGUAGE_CYCLE.translate_group) then
      Language_Cycle_Button:GetNormalFontObject():SetTextColor(1, 1, 1);
   else
      Language_Cycle_Button:GetNormalFontObject():SetTextColor(.5, 1, .5);
   end
end

function Language_Cycle_Slash(arg)
   if (arg == "" or arg == nil) then
      Language_Cycle_Cycle();
   elseif (arg == "ver" or arg == "version") then
      Language_Cycle_Add_Message("LanguageCycle version: " .. LC_version);
   elseif (arg == "clear") then
      Language_Cycle_Clear();
   elseif (arg == "stat" or arg == "status") then
      local msg = "Speaking: " .. LANGUAGE_CYCLE.saved_language[LC_player_id] .. "; Guild/Party translation is ";
      if (LANGUAGE_CYCLE.translate_group) then
         msg = msg .. "on.";
      else
         msg = msg .. "off.";
      end
      Language_Cycle_Add_Message(msg);
   elseif (arg == "party" or arg == "guild" or arg == "group" or arg =="toggle") then
      Language_Cycle_Toggle_Group(true);
   elseif (arg == "show" or arg == "hide") then
      Language_Cycle_Panel_Visible();
   elseif (arg == "mm" or arg == "MM") then
                if (LANGUAGE_CYCLE.minimapIcon.hide == false) then
                        LibStub("LibDBIcon-1.0"):Hide("LanguageCycle")
                        LANGUAGE_CYCLE.minimapIcon.hide = true
                else
                        LibStub("LibDBIcon-1.0"):Show("LanguageCycle")
                        LANGUAGE_CYCLE.minimapIcon.hide = false
                end
        elseif (arg == "help") then
      local t = 0;
      while LC_Help_Text[t] ~= nil do
         DEFAULT_CHAT_FRAME:AddMessage(LC_Help_Text[t],0.0, 1.0, 0.0);
         t = t + 1;
      end
   end
end

function Language_Cycle_Clear()
   LANGUAGE_CYCLE.saved_language[LC_player_id] = nil;
   LANGUAGE_CYCLE.translate_group = true;
   Language_Cycle_Add_Message(string.format("Cleared language for %s", LC_player_name));
   Language_Cycle_Button:SetText("Pref Cleared...");
   Language_Cycle_Set_Text_Color();
end

function Language_Cycle_Add_Message(msg)
   DEFAULT_CHAT_FRAME:AddMessage(msg,0.0,1.0,0.0);
end 

Compare with Previous | Blame