stoneharry

Untitled

May 12th, 2014
587
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.48 KB | None | 0 0
  1.  
  2. local BATTLEGROUND_MAP_STR = "Warsong Gulch" -- DO NOT CHANGE
  3. local WELCOME_MESSAGE_STR = "Version 1.0"
  4. local CLICK_GAME_TO_JOIN_STR = "Click a game to join it."
  5. local WAITING_FOR_PLRS_STR = "Waiting for all players to be ready..."
  6.  
  7. local PLAYER_IN_GAME_STR = nil
  8. local MENU_SELECTED = 0
  9. -- Lobby channels WIP, disabled atm since buggy
  10. local MENU_CHANNELS = {"Lobby", "Game"}
  11. local numScrollBarButtons = 50
  12. local dur = 27
  13. local mus_dur = 0
  14. local ONLINE_PLAYERS = {}
  15. local UPDATE_INTERVALS = {30, 5, 5}
  16. local playing = nil
  17. local links = {}
  18.  
  19. -- Set up background model
  20. local model = CreateFrame("Model"--[[, "BackgroundF", MainFrame]])
  21. model:SetCamera(0)
  22. --model:SetFrameStrata("BACKGROUND")
  23. model:SetPoint("TOPLEFT", 0, 0)
  24. model:SetPoint("BOTTOMRIGHT", 0, 0)
  25. model:SetFrameLevel(0)
  26. model:SetLight(1,0,0,-0.5,-0.5,0.7,1.0,1.0,1.0,0.8,1.0,1.0,0.8)
  27.  
  28. -- This gets the width/height of the screen
  29. --[[
  30. local res = GetCVar("gxResolution")
  31. local vars = 1
  32. for value in string.gmatch(res, '([^x]+)') do
  33.     if vars == 1 then
  34.         --model:SetWidth(value)
  35.         SCREEN_WIDTH = tonumber(value)
  36.         vars = nil
  37.     else
  38.         --model:SetHeight(value)
  39.         SCREEN_HEIGHT = tonumber(value)
  40.     end
  41. end
  42. res = nil
  43. ]]
  44.  
  45. function toggleInterface(hide)
  46.     if hide then
  47.         MiniMapWorldMapButton:Hide()
  48.         GameTimeFrame:Hide()
  49.         MinimapZoomIn:Hide() -- hides the zoom-in-button (+)
  50.         MinimapZoomOut:Hide() -- hides the zoom-out-button (-)
  51.         --RemoveMainActionBar()
  52.         MainMenuBar:Hide()
  53.         MainMenuBarLeftEndCap:Hide() -- to hide the left one
  54.         MainMenuBarRightEndCap:Hide() -- to hide the right one
  55.         ShapeshiftBarFrame:Hide()
  56.         MultiBarBottomLeft:Hide()
  57.         MultiBarBottomRight:Hide()
  58.         MultiBarLeft:Hide()
  59.         MultiBarRight:Hide()
  60.         MainMenuExpBar:Hide()
  61.     else
  62.         MiniMapWorldMapButton:Show()
  63.         GameTimeFrame:Show()
  64.         MinimapZoomIn:Show() -- hides the zoom-in-button (+)
  65.         MinimapZoomOut:Show() -- hides the zoom-out-button (-)
  66.         --AddMainActionBar()
  67.         MainMenuBar:Show()
  68.         MainMenuBarLeftEndCap:Show() -- to hide the left one
  69.         MainMenuBarRightEndCap:Show() -- to hide the right one
  70.         --ShapeshiftBarFrame:Show()
  71.     end
  72. end
  73.  
  74. function mainFrameLoaded()
  75.     -- Set background model
  76.     model:SetModel([[Interface\Glues\Models\UI_Orc\UI_Orc.m2]])
  77.     -- Set frame levels
  78.     MainFrame_Back:SetFrameLevel(1)
  79.     MainFrame_Header:SetFrameLevel(1)
  80.  
  81.     -- Hide main interface
  82.     toggleInterface(true)
  83.    
  84.     -- Make the edges a higher layer
  85.     for i = -1, -8, -1 do
  86.         select(i, MainFrame_Back:GetRegions()):SetDrawLayer("OVERLAY")
  87.         select(i, MainFrame_Header:GetRegions()):SetDrawLayer("OVERLAY")
  88.     end
  89.    
  90.     -- Buttons are easier to do in Lua it seems
  91.     local button = CreateFrame("Button", nil, MainFrame_Back, "BigButtonTemplate")
  92.     button:SetPoint("TOPLEFT", MainFrame_Back, "TOPLEFT", 337, -20)
  93.     button:SetText("Play Game")
  94.     button:SetFrameLevel(3)
  95.     button:SetScript("OnClick", PlayGame)
  96.    
  97.     button = CreateFrame("Button", "SmallButtonOnTopRight1", MainFrame_Back, "SmallButtonTemplate")
  98.     button:SetPoint("TOPLEFT", MainFrame_Back, "TOPLEFT", 641, -23)
  99.     button:SetFrameLevel(3)
  100.     _G["SmallButtonOnTopRight1Icon"]:SetTexture([[Interface\Portal\SmallButtonIcons\1]])
  101.    
  102.     button = CreateFrame("Button", "SmallButtonOnTopRight2", MainFrame_Back, "SmallButtonTemplate")
  103.     button:SetPoint("TOPLEFT", MainFrame_Back, "TOPLEFT", 727, -23)
  104.     button:SetFrameLevel(3)
  105.     _G["SmallButtonOnTopRight2Icon"]:SetTexture([[Interface\Portal\SmallButtonIcons\2]])
  106.     button:SetScript("OnClick", function() ToggleAchievementFrame(false) end)
  107.    
  108.     button = CreateFrame("Button", "SmallButtonOnTopRight3", MainFrame_Back, "SmallButtonTemplate")
  109.     button:SetPoint("TOPLEFT", MainFrame_Back, "TOPLEFT", 813, -23)
  110.     button:SetFrameLevel(3)
  111.     _G["SmallButtonOnTopRight3Icon"]:SetTexture([[Interface\Portal\SmallButtonIcons\3]])
  112.    
  113.     button = CreateFrame("Button", nil, MainFrame_Back, "LeftMiddleButtonTemplate")
  114.     button:SetPoint("TOPLEFT", MainFrame_Back, "TOPLEFT", 30, -117)
  115.     button:SetText("Change Lobby")
  116.     button:SetFrameLevel(4)
  117.     button:Disable()
  118.    
  119.     button = CreateFrame("Button", nil, MainFrame_Back, "LeftMiddleButtonTemplate")
  120.     button:SetPoint("BOTTOMRIGHT", MainFrame_Back, "BOTTOMRIGHT", -28, 120)
  121.     button:SetText("Logout")
  122.     button:SetWidth(200)
  123.     button:SetFrameLevel(4)
  124.     button:SetScript("OnClick", function() Logout() end)
  125.    
  126.     button = CreateFrame("Button", nil, MainFrame_Back, "LeftMiddleButtonTemplate")
  127.     button:SetPoint("BOTTOMRIGHT", MainFrame_Back, "BOTTOMRIGHT", -32, 30)
  128.     button:SetText("Exit Game")
  129.     button:SetWidth(190)
  130.     button:SetFrameLevel(4)
  131.     button:SetScript("OnClick", function() Quit() end)
  132.    
  133.     local fontString = MainFrame_Back:CreateFontString("MainFrame_Back_WelcomeFontString", "OVERLAY")
  134.     fontString:SetFontObject("GameFontNormalHuge")
  135.     fontString:SetTextColor(1, 1, 1)
  136.     fontString:SetText(WELCOME_MESSAGE_STR)
  137.     fontString:SetPoint("TOPLEFT", MainFrame_Back, "TOPLEFT", 255, -124)
  138.    
  139.         -- Listen for addon messages
  140.     MainFrame_Chat_2:RegisterEvent("CHAT_MSG_ADDON")
  141.     MainFrame_Chat_2:SetScript("OnEvent", eventHandlerMainFrame)
  142.    
  143.     fontString = MainFrame_OnlinePlayerList_2:CreateFontString("HowToJoinGameStr", "OVERLAY")
  144.     fontString:SetFontObject("GameFontNormalHuge")
  145.     fontString:SetTextColor(1, 1, 1)
  146.     fontString:SetText(CLICK_GAME_TO_JOIN_STR)
  147.     fontString:SetPoint("TOPLEFT", MainFrame_OnlinePlayerList_2, "TOPLEFT", 30, -50)
  148.    
  149.     fontString = MainFrame_Chat_2:CreateFontString("GameNameStr", "OVERLAY")
  150.     fontString:SetFontObject("GameFontNormalHuge")
  151.     fontString:SetTextColor(1, 1, 1)
  152.     fontString:SetText("Game Name:")
  153.     fontString:SetPoint("TOPLEFT", MainFrame_Chat_2, "TOPLEFT", 30, -50)
  154.    
  155.     local editBoxBackdrop = {
  156.       bgFile = [[Interface\Tooltips\UI-Tooltip-Background]],
  157.       edgeFile = [[Interface\Glues\Common\Glue-Tooltip-Border]],
  158.       tile = true,
  159.       tileSize = 16,
  160.       edgeSize = 16,
  161.       insets = {
  162.         left = 10,
  163.         right = 5,
  164.         top = 4,
  165.         bottom = 9
  166.       }
  167.     }
  168.     fontString = CreateFrame("EditBox", "GameNameInput", MainFrame_Chat_2)
  169.     fontString:SetWidth(250)
  170.     fontString:SetHeight(37)
  171.     fontString:SetPoint("TOPLEFT", MainFrame_Chat_2, "TOPLEFT", 160, -47)
  172.     fontString:SetFontObject("NumberFontNormalYellow")
  173.     fontString:SetMaxLetters(40)
  174.     fontString:SetBackdrop(editBoxBackdrop)
  175.     fontString:SetBackdropBorderColor(0.8, 0.8, 0.8)
  176.     -- Sets the insets from the edit box's edges which determine its interactive text area
  177.     fontString:SetTextInsets(14, -14, 0, 4)
  178.     fontString:SetScript("OnTextChanged",
  179.         function(self, userInput)
  180.             if string.len(self:GetText()) <= 3 then
  181.                 CreateGameBtn:Disable();
  182.             else
  183.                 CreateGameBtn:Enable();
  184.             end
  185.         end)
  186.    
  187.     button = CreateFrame("Button", "CreateGameBtn", MainFrame_OnlinePlayerList_2, "LeftMiddleButtonTemplate")
  188.     button:SetPoint("BOTTOMRIGHT", MainFrame_OnlinePlayerList_2, "BOTTOMRIGHT", -118, -65)
  189.     button:SetText("Create Game")
  190.     button:SetWidth(200)
  191.     button:SetFrameLevel(4)
  192.     button:Disable()
  193.     button:SetScript("OnClick",
  194.         function()
  195.             SendAddonMessage("CREATEGAME", _G["GameNameInput"]:GetText(), "WHISPER", UnitName("player"))
  196.             OpenGameLobby(_G["GameNameInput"]:GetText())
  197.         end)
  198.    
  199.     button = CreateFrame("Button", "GoBackBtn", MainFrame_OnlinePlayerList_2, "LeftMiddleButtonTemplate")
  200.     button:SetPoint("BOTTOMRIGHT", MainFrame_OnlinePlayerList_2, "BOTTOMRIGHT", -123, -155)
  201.     button:SetText("Go Back")
  202.     button:SetWidth(190)
  203.     button:SetFrameLevel(4)
  204.     button:SetScript("OnClick", GoBackToMainMenu)
  205.    
  206.     for i=1,50 do
  207.         _G["ScrollBarEntry"..tostring(i)]:SetScript("OnClick", HandleScollBarEntryClick)
  208.     end
  209.    
  210.     --JoinChannelByName(MENU_CHANNELS[1])
  211. end
  212.  
  213. function HandleScollBarEntryClick(self)
  214.     if not self:GetText() then
  215.         return
  216.     end
  217.     -- If wanting to join a game
  218.     if MENU_SELECTED == 1 then
  219.         -- substring 9, 10 if whitespace
  220.         local lobbyName = string.sub(self:GetText(), 9)
  221.         if string.starts(lobbyName, " ") then
  222.             lobbyName = string.sub(lobbyName, 1)
  223.         end
  224.         SendAddonMessage("JoinGame", lobbyName, "WHISPER", UnitName("player"))
  225.         OpenGameLobby(lobbyName)
  226.     -- If in game lobby or main lobby
  227.     elseif MENU_SELECTED == 0 or MENU_SELECTED == 2 then
  228.         -- This needs writing properly
  229.         local buttonNumber = tonumber(string.sub(self:GetName(), 15))
  230.         table.insert(ONLINE_PLAYERS, buttonNumber + 1, {"  |cFF00FF00 0 Wins", "1"})
  231.         table.insert(ONLINE_PLAYERS, buttonNumber + 2, {"  |cFFFF0000 0 Losses", "1"})
  232.         -- update view
  233.         SB_Main_ScrollBar_Update()
  234.     end
  235. end
  236.  
  237. function PlayGame()
  238.     PlaySound("igMainMenuOption");
  239.     MainFrame_Back:Hide()
  240.     MainFrame_Chat:Hide()
  241.     MainFrame_OnlinePlayerList_2:Show()
  242.     MainFrame_Chat_2:Show()
  243.     MainFrame_OnlinePlayerList:Show()
  244.     local str = _G["HowToJoinGameStr"]
  245.     if str then
  246.         str:SetText(CLICK_GAME_TO_JOIN_STR)
  247.     end
  248.     str = _G["CreateGameBtn"]
  249.     if str then
  250.         str:Show()
  251.     end
  252.     str = _G["GoBackBtn"]
  253.     if str then
  254.         str:Show()
  255.     end
  256.     -- Update menu selected
  257.     MENU_SELECTED = 1
  258.     dur = 31
  259. end
  260.  
  261. function GoBackToMainMenu()
  262.     PlaySound("igMainMenuOption");
  263.     MainFrame_Back:Show()
  264.     MainFrame_Chat:Show()
  265.     MainFrame_OnlinePlayerList_2:Hide()
  266.     MainFrame_Chat_2:Hide()
  267.    
  268.     if PLAYER_IN_GAME_STR then
  269.         --LeaveChannelByName(PLAYER_IN_GAME_STR)
  270.     end
  271.     --JoinChannelByName(MENU_CHANNELS[1])
  272.    
  273.     -- Update menu selected
  274.     MENU_SELECTED = 0
  275.     dur = 31
  276. end
  277.  
  278. function OpenGameLobby(gameName)
  279.     PlaySound("igMainMenuOption");
  280.     PLAYER_IN_GAME_STR = gameName
  281.     --LeaveChannelByName(MENU_CHANNELS[1])
  282.     --JoinChannelByName(gameName)
  283.     MENU_SELECTED = 2
  284.     dur = 31
  285.     _G["HowToJoinGameStr"]:SetText(WAITING_FOR_PLRS_STR)
  286.     _G["CreateGameBtn"]:Hide()
  287.     _G["GoBackBtn"]:Hide()
  288.     MainFrame_Chat_2:Hide()
  289. end
  290.  
  291. function eventHandlerMainFrame(self, event, message, _, Type, Sender)
  292.     if (event == "CHAT_MSG_ADDON" and Sender == UnitName("player")) then
  293.         local packet, link, linkCount, MSG = message:match("(%d%d%d)(%d%d)(%d%d)(.+)");
  294.         if not packet or not link or not linkCount or not MSG then
  295.             return
  296.         end
  297.         packet, link, linkCount = tonumber(packet), tonumber(link), tonumber(linkCount);
  298.        
  299.         links[packet] = links[packet] or {count = 0};
  300.         links[packet][link] = MSG;
  301.         links[packet].count = links[packet].count + 1;
  302.        
  303.         if (links[packet].count ~= linkCount) then
  304.             return
  305.         end
  306.        
  307.         local fullMessage = table.concat(links[packet]);
  308.         links[packet] = {count = 0};
  309.        
  310.         -- Handle addon messages
  311.         -- Handle game list
  312.         if string.starts(fullMessage, "GAMES-") then
  313.            
  314.             local tokens = scen_split(fullMessage)
  315.             local pos = 1
  316.             for i=2, #tokens, 2 do
  317.                 ONLINE_PLAYERS[pos] = {tokens[i + 1], tokens[i]}
  318.                 pos = pos + 1
  319.             end
  320.            
  321.             -- update view
  322.             SB_Main_ScrollBar_Update()
  323.         elseif string.starts(fullMessage, "PLAYERS-") then
  324.             local tokens = scen_split(fullMessage)
  325.             for i=2, #tokens do
  326.                 ONLINE_PLAYERS[i - 1] = {tokens[i], "1"}
  327.             end
  328.            
  329.             -- update view
  330.             SB_Main_ScrollBar_Update()     
  331.         end
  332.     end
  333. end
  334.  
  335. function string.starts(String,Start)
  336.    return string.sub(String,1,string.len(Start))==Start
  337. end
  338.  
  339. function scen_split(str)
  340.     local b = {}
  341.     local c = 1
  342.     local d = {}
  343.     string.gsub(str, "[%w%s-]", function(a)
  344.         if (a == "-") then
  345.             c = c + 1
  346.         else
  347.             if (not b[c]) then
  348.                 b[c] = {}
  349.             end
  350.             table.insert(b[c], a)
  351.         end
  352.     end)
  353.     for k, v in pairs (b) do
  354.         table.insert(d, table.concat(v))
  355.     end
  356.     return d
  357. end
  358.  
  359. -- SCROOLLLL BARRSSSS
  360.  
  361. function SB_Main_ScrollBar_Update()
  362.     -- 50 is max entries, 5 is number of lines, 16 is pixel height of each line
  363.     FauxScrollFrame_Update(MainScrollBar, 50, 5, 16)
  364.    
  365.     local line
  366.     local lineplusoffset
  367.     for line=1,12 do
  368.         lineplusoffset = line + FauxScrollFrame_GetOffset(MainScrollBar)
  369.         if lineplusoffset <= 50 then
  370.             if ONLINE_PLAYERS[lineplusoffset] then
  371.                 _G["ScrollBarEntry"..line]:SetText("     "..lineplusoffset..": "..ONLINE_PLAYERS[lineplusoffset][1])
  372.                 _G["ScrollBarEntry"..line]:Show()
  373.                 _G["ScrollBarEntry"..line.."Icon"]:Show()
  374.                 -- Main menu
  375.                 if MENU_SELECTED == 0 then
  376.                     if (ONLINE_PLAYERS[lineplusoffset][2] == BATTLEGROUND_MAP_STR) then
  377.                         _G["ScrollBarEntry"..line.."Icon"]:SetTexture([[Interface\Portal\Icons\2]])
  378.                     else
  379.                         _G["ScrollBarEntry"..line.."Icon"]:SetTexture([[Interface\Portal\Icons\1]])
  380.                     end
  381.                 -- Play Game menu
  382.                 elseif MENU_SELECTED == 1 then
  383.                     if (ONLINE_PLAYERS[lineplusoffset][2] == "1") then
  384.                         _G["ScrollBarEntry"..line.."Icon"]:SetTexture([[Interface\Portal\Icons\1]])
  385.                     else
  386.                         _G["ScrollBarEntry"..line.."Icon"]:SetTexture([[Interface\Portal\Icons\2]])
  387.                     end
  388.                 -- Players in game lobby
  389.                 elseif MENU_SELECTED == 2 then
  390.                     _G["ScrollBarEntry"..line.."Icon"]:SetTexture([[Interface\Portal\Icons\1]])        
  391.                 end
  392.             else
  393.                 _G["ScrollBarEntry"..line]:Hide()
  394.                 _G["ScrollBarEntry"..line.."Icon"]:Hide()
  395.             end
  396.         else
  397.             _G["ScrollBarEntry"..line]:Hide()
  398.             _G["ScrollBarEntry"..line.."Icon"]:Hide()
  399.         end
  400.     end
  401. end
  402.  
  403. function mainFrameUpdate(self, elapsed)
  404.     dur = dur + elapsed
  405.     mus_dur = mus_dur + elapsed
  406.     if not playing then
  407.         if mus_dur > 1.5 then
  408.             PlayMusic([[Interface\FrameXML\1.mp3]])
  409.             playing = 1
  410.             mus_dur = 0
  411.         end
  412.     end
  413.     if playing == 1 then
  414.         if mus_dur > 69 then
  415.             mus_dur = 0
  416.             playing = 2
  417.             PlayMusic([[Interface\FrameXML\2.mp3]])
  418.         end
  419.     elseif playing == 2 then
  420.         if mus_dur > 81 then
  421.             mus_dur = 0
  422.             playing = 1
  423.             PlayMusic([[Interface\FrameXML\1.mp3]])
  424.         end
  425.     end
  426.     if dur > UPDATE_INTERVALS[MENU_SELECTED + 1] then
  427.         dur = 0
  428.         ONLINE_PLAYERS = {}
  429.         if MENU_SELECTED == 0 then
  430.             SendWho("")
  431.             for i=1, GetNumWhoResults() do
  432.                 -- name, guild, level, race, class, zone, classFileName
  433.                 local name, _, _, _, _, zone, _ = GetWhoInfo(i)
  434.                 ONLINE_PLAYERS[i] = {name, zone}
  435.             end
  436.         elseif MENU_SELECTED == 1 then
  437.             -- Retrieve list of games running
  438.             SendAddonMessage("MAINMENU", "GetTheGamesAvailable", "WHISPER", UnitName("player"))
  439.         elseif MENU_SELECTED == 2 then
  440.             -- Retrieve list of people in lobby
  441.             if not PLAYER_IN_GAME_STR then
  442.                 print("ERROR: Game name is null and trying to retrieve the players in this games lobby.")
  443.             else
  444.                 SendAddonMessage("PLRSLB", PLAYER_IN_GAME_STR, "WHISPER", UnitName("player"))
  445.             end
  446.         end
  447.         -- hackfix location
  448.         ScrollBarEntry1:SetPoint("TOPLEFT", MainScrollBar, "TOPLEFT", 8, 0)
  449.         -- update view
  450.         SB_Main_ScrollBar_Update()
  451.     end
  452.     -- This is hacky as hell, but hey ho. It's set on update as there
  453.     --  needs to be a delay between on load and setting this.
  454.     --  Also this conveniently prevents resizing.
  455.     ChatFrame1:ClearAllPoints()
  456.     ChatFrame1:SetFading(nil)
  457.     ChatFrame1:SetPoint("BOTTOMLEFT", MainFrame_Back, "BOTTOMLEFT", 60, 50)
  458.     ChatFrame1:SetFrameStrata("MEDIUM")
  459.     ChatFrame1:SetWidth(700)
  460.     ChatFrame1:SetHeight(300)
  461.     -- Keep that friends frame hidden
  462.     _G["FriendsFrame"]:Hide()
  463. end
Advertisement
Add Comment
Please, Sign In to add comment