Advertisement
Noneatme

CChatBox.lua

Jun 29th, 2013
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 16.74 KB | None | 0 0
  1. -- #######################################
  2. -- ## Project: MTA:The Walking Death    ##
  3. -- ## Name: ChatBox                     ##
  4. -- ## Author: Noneatme                  ##
  5. -- ## Version: 1.0                      ##
  6. -- ## License: See top Folder           ##
  7. -- #######################################
  8.  
  9. -- FUNCTIONS / METHODS --
  10.  
  11. local cFunc = {};       -- Local Functions
  12. local cSetting = {};    -- Local Settings
  13.  
  14. ChatBox = {};
  15. ChatBox.__index = ChatBox;
  16.  
  17. local sx, sy = guiGetScreenSize()
  18. local aesx, aesy = 1600, 900;
  19. --[[
  20.  
  21. ]]
  22.  
  23. -- ///////////////////////////////
  24. -- ///// New                //////
  25. -- ///// Returns:   object  //////
  26. -- ///////////////////////////////
  27.  
  28. function ChatBox:New(...)
  29.     local obj = setmetatable({}, {__index = self});
  30.     if obj.Constructor then
  31.         obj:Constructor(...);
  32.     end
  33.     return obj;
  34. end
  35.  
  36. -- ///////////////////////////////
  37. -- ///// Render             //////
  38. -- ///// Returns:   void    //////
  39. -- ///////////////////////////////
  40.  
  41.  
  42. function ChatBox:Render()
  43.     if(self.state == true) then
  44.         dxDrawText("Select channel:", 5, sy-(aesy-650), 190, sy-(aesy-684), tocolor(255, 255, 255, 255), 1, "default-bold", "center", "center", false, false, true, false, false)
  45.         dxDrawRectangle(5, sy-(aesy-649), 368, 251, tocolor(0, 0, 0, 128), false)
  46.         dxDrawRectangle(372, sy-(aesy-649), 0, 27, tocolor(255, 255, 255, 255), true)
  47.         dxDrawRectangle(5, sy-(aesy-649), 368, 36, tocolor(0, 0, 0, 128), false)
  48.         dxDrawRectangle(5, sy-(aesy-862), 368, 36, tocolor(0, 0, 0, 128), false)
  49.        
  50.         local add = 0
  51.         local increment = 17
  52.         --[[
  53.         dxDrawText("Line 1", 3, 687, 363, 705, tocolor(255, 255, 255, 255), 1, "default-bold", "left", "center", false, false, true, false, false)
  54.         dxDrawText("Line 2", 3, 704, 363, 722, tocolor(255, 255, 255, 255), 1, "default-bold", "left", "center", false, false, true, false, false)]]
  55.        
  56.         for i = self.drawingPos, self.drawingPos+9, 1 do
  57.             if(self.groupText[self.selectedGroup]) then
  58.                 local text = self.groupText[self.selectedGroup][i];
  59.                 if(text) then
  60.                     local temp = string.gsub(text, '#%x%x%x%x%x%x', '')
  61.                     local cc = false;
  62.                    
  63.                     if(temp ~= text) then    -- Colorcodes
  64.                         cc = true;
  65.                     end
  66.                    
  67.                     local r, g, b = self.groupTextColor[self.selectedGroup][i]["r"], self.groupTextColor[self.selectedGroup][i]["g"], self.groupTextColor[self.selectedGroup][i]["b"];
  68.                     local a = self.groupTextColor[self.selectedGroup][i]["a"];
  69.                    
  70.                     if(a < 255) then
  71.                         a = a+5
  72.                     end
  73.  
  74.                    
  75.                     self.groupTextColor[self.selectedGroup][i]["a"] = a; -- 705
  76.                     dxDrawText(text, 10, sy-(aesy-(687+add-self.add)), 363, sy-(aesy-(705+add-self.add)), tocolor(r or 255, g or 255, b or 255, a), 1, "default-bold", "left", "center", true, false, true, cc, false)
  77.                    
  78.                    
  79.  
  80.                     add = add+increment;
  81.                    
  82.                    
  83.                    
  84.                     if(self.add > 0) then
  85.                         self.add = self.add-0.1
  86.                     end
  87.                    
  88.                 end
  89.             end
  90.         end
  91.     end
  92. end
  93.  
  94. -- ///////////////////////////////
  95. -- ///// GetTextCount       //////
  96. -- ///// Returns:   int     //////
  97. -- ///////////////////////////////
  98.  
  99. function ChatBox:GetTextCount(text)
  100.     local count = 0;
  101.  
  102.         if(text) then
  103.             if(string.len(text) > 0) and (string.len(text) < 40) then
  104.                 count = count+1;
  105.             elseif(string.len(text) > 40) and (string.len(text) < 80) then
  106.                 count = count+2;
  107.             elseif(string.len(text) > 80) and (string.len(text) < 120) then
  108.                 count = count+3;
  109.             elseif(string.len(text) > 120) and (string.len(text) < 160) then
  110.                 count = count+4;
  111.             elseif(string.len(text) > 160) and (string.len(text) < 200) then
  112.                 count = count+5;
  113.             elseif(string.len(text) > 200) and (string.len(text) < 240) then
  114.                 count = count+6;
  115.             elseif(string.len(text) > 240) and (string.len(text) < 280) then
  116.                 count = count+7;
  117.             else
  118.                 count = count+1;
  119.             end
  120.         end
  121.    
  122.     return count;
  123. end
  124.  
  125.  
  126. -- ///////////////////////////////
  127. -- ///// CreateGui          //////
  128. -- ///// Returns:   bool    //////
  129. -- ///////////////////////////////
  130.  
  131.  
  132. function ChatBox:CreateGui()
  133.     guiSetInputMode ( "no_binds_when_editing" )
  134.    
  135.     self.guiEle.comboBox = guiCreateComboBox(195, sy-(aesy-655), 174, 115, "", false)
  136.     guiSetFont(self.guiEle.comboBox, "default-bold-small")
  137.  
  138.    
  139.     self.guiEle.absenden = guiCreateEdit(6, sy-(aesy-865), 363, 31, "Enter your message...", false);
  140.     guiSetFont(self.guiEle.absenden, "default-bold-small")
  141.    
  142.     guiEditSetMaxLength (self.guiEle.absenden, 64);
  143.    
  144.     return true;
  145. end
  146.  
  147.  
  148. -- ///////////////////////////////
  149. -- ///// DestroyGui         //////
  150. -- ///// Returns:   bool    //////
  151. -- ///////////////////////////////
  152.  
  153. function ChatBox:DestroyGui()
  154.     for index, guiEle in pairs(self.guiEle) do
  155.         if(isElement(guiEle)) then
  156.             destroyElement(guiEle)
  157.         end
  158.     end
  159.     return true;
  160. end
  161.  
  162. -- ///////////////////////////////
  163. -- ///// AcceptText         //////
  164. -- ///// Returns:   bool    //////
  165. -- ///////////////////////////////
  166.  
  167. function ChatBox:AcceptText()
  168.     --guiMoveToBack(self.guiEle.absenden);
  169.    
  170.     local text = guiGetText(self.guiEle.absenden);
  171.     toggleAllControls(true);
  172.     if(string.len(string.gsub(text, " ", "")) > 0) then
  173.         guiSetText(self.guiEle.absenden, "");
  174.         guiMoveToBack(self.guiEle.absenden);
  175.    
  176.        
  177.         if(self:IsCommand(text)) then
  178.             local cmd = gettok(text, 1, "/");
  179.            
  180.             local params = split(cmd, " ");
  181.             table.remove(params, 1);
  182.             triggerServerEvent("ChatBox:SendCommand", getLocalPlayer(), cmd, params);
  183.             executeCommandHandler(cmd, unpack(params))
  184.            
  185.         else
  186.             if(self.groupText[self.selectedGroup]["locked"] ~= true) then
  187.                 if(self:ContainsBadWords(text) == true) then
  188.                
  189.                 else
  190.  
  191.                     local r, g, b = self.groupText[self.selectedGroup]["color"][1], self.groupText[self.selectedGroup]["color"][2], self.groupText[self.selectedGroup]["color"][3]
  192.                     triggerServerEvent("ChatBox:SendText", getLocalPlayer(), self.selectedGroup, text, r, g, b);
  193.                 end
  194.             end
  195.         end
  196.     else
  197.         guiSetText(self.guiEle.absenden, "");
  198.         guiMoveToBack(self.guiEle.absenden);
  199.     end
  200.    
  201.     return true;
  202. end
  203.  
  204. -- ///////////////////////////////
  205. -- ///// ClickEdit          //////
  206. -- ///// Returns:   bool    //////
  207. -- ///////////////////////////////
  208.  
  209. function ChatBox:ClickEdit()
  210.     if(guiGetText(self.guiEle.absenden) == "Enter your message...") then
  211.         guiSetText(self.guiEle.absenden, "");
  212.     end
  213.     return true;
  214. end
  215.  
  216. -- ///////////////////////////////
  217. -- ///// ContainsBadWords   //////
  218. -- ///// Returns:   bool    //////
  219. -- ///////////////////////////////
  220.  
  221. function ChatBox:ContainsBadWords()
  222.     return false;
  223. end
  224.  
  225.  
  226. -- ///////////////////////////////
  227. -- ///// AddGroup           //////
  228. -- ///// Returns:   bool    //////
  229. -- ///////////////////////////////
  230.  
  231.  
  232. function ChatBox:AddGroup(name, locked, r, g, b, key)
  233.     if(self.groups[name] == nil) then
  234.         local id = guiComboBoxAddItem(self.guiEle.comboBox, name);
  235.         self.itemIds[name] = id;
  236.        
  237.         self.groups[name] = true;
  238.         self.groupText[name] = {};
  239.         self.groupTextColor[name] = {};
  240.  
  241.        
  242.         self.groupText[name]["index"] = 1;
  243.         self.groupText[name]["locked"] = locked or false;
  244.        
  245.         if(r and g and b) then
  246.             self.groupText[name]["color"] = {r, g, b};
  247.         end
  248.        
  249.         -- KEY --
  250.        
  251.         if(key) then
  252.             self.groupFunc[name] = function()
  253.                 self:SelectGroup(name);
  254.             end
  255.            
  256.             bindKey(key, "down", self.groupFunc[name]);
  257.         end
  258.         return true;
  259.     end
  260.     return false;
  261. end
  262.  
  263. -- ///////////////////////////////
  264. -- ///// RemoveGroup        //////
  265. -- ///// Returns:   bool    //////
  266. -- ///////////////////////////////
  267.  
  268.  
  269. function ChatBox:RemoveGroup(name)
  270.     if(self.groups[name] ~= nil) then
  271.         guiComboBoxRemoveItem(self.guiEle.comboBox, self.itemIds[name]);
  272.         if(self.selectedGroup == name) then
  273.             self:SelectGroup("System")
  274.         end
  275.        
  276.         if(self.groupFunc[name]) then
  277.             unbindKey(key, "down", self.groupFunc[name]);
  278.         end
  279.         return true;
  280.     end
  281.     return false;
  282. end
  283.  
  284. -- ///////////////////////////////
  285. -- ///// SelectGroup        //////
  286. -- ///// Returns:   bool    //////
  287. -- ///////////////////////////////
  288.  
  289. function ChatBox:SelectGroup(name)
  290.     self.selectedGroup = name;
  291.     if(self.groups[name] ~= nil) then
  292.         guiComboBoxSetSelected (self.guiEle.comboBox, self.itemIds[name]);
  293.     end
  294.     self.drawingPos = 1;
  295.     return true;
  296. end
  297.  
  298. -- ///////////////////////////////
  299. -- ///// SaveToLog          //////
  300. -- ///// Returns:   bool    //////
  301. -- ///////////////////////////////
  302.  
  303. function ChatBox:SaveToLog(group, text)
  304.     if(text) then
  305.         local date1 = (getRealTime().monthday).."."..(getRealTime().month+1).."."..(getRealTime().year+1900).." "..getRealTime().hour..":"..getRealTime().minute..":"..getRealTime().second;
  306.         local date2 = (getRealTime().monthday).."."..(getRealTime().month+1).."."..(getRealTime().year+1900);
  307.         local fileString = "savedFiles/logs/"..date2.."/"..group..".log";
  308.         local file;
  309.        
  310.         text = string.gsub(text, '#%x%x%x%x%x%x', '')
  311.        
  312.         if(fileExists(fileString)) then
  313.             file = fileOpen(fileString);
  314.         else
  315.             file = fileCreate(fileString);
  316.         end
  317.         fileSetPos(file, fileGetSize(file));
  318.         fileWrite(file, "["..date1.."]"..text.."\n");
  319.         fileFlush(file);
  320.         fileClose(file);
  321.        
  322.         return true;
  323.     end
  324.     return false;
  325. end
  326.  
  327.  
  328. -- ///////////////////////////////
  329. -- ///// SendTextBack       //////
  330. -- ///// Returns:   bool    //////
  331. -- ///////////////////////////////
  332.  
  333. function ChatBox:SendTextBack(group, text, r, g, b)
  334.  
  335.     if not(self.groupText[group]) then
  336.         self.groupText[group] = {}
  337.         self.groupText[group]["index"] = 1;
  338.     end
  339.     self.groupText[group][self.groupText[group]["index"]] = text;
  340.     self.groupTextColor[group][self.groupText[group]["index"]] = {};
  341.     self.groupTextColor[group][self.groupText[group]["index"]]["r"] = r or 255;
  342.     self.groupTextColor[group][self.groupText[group]["index"]]["g"] = g or 255;
  343.     self.groupTextColor[group][self.groupText[group]["index"]]["b"] = b or 255;
  344.     self.groupTextColor[group][self.groupText[group]["index"]]["a"] = 0;
  345.    
  346.     self.groupText[group]["index"] = self.groupText[group]["index"]+1;
  347.     self.drawingPos = #self.groupText[group]-9;
  348.    
  349.  
  350.     self:SaveToLog(group, text);
  351.    
  352.     if(group ~= "System") then
  353.         self:SendTextBack("System", "["..string.upper(gettok(group, 1, " ")).."] "..text, r, g, b)
  354.     end
  355.    
  356.     return true;
  357. end
  358.  
  359. -- ///////////////////////////////
  360. -- ///// MoveChatboxUp      //////
  361. -- ///// Returns:   bool    //////
  362. -- ///////////////////////////////
  363.  
  364. function ChatBox:MoveChatboxUp()
  365.     --if(inventory.state == false) then
  366.         if(self.drawingPos > 1) then
  367.             self.drawingPos = self.drawingPos-1
  368.         end
  369.     --end
  370.     return true;
  371. end
  372.  
  373. -- ///////////////////////////////
  374. -- ///// MoveChatboxDown    //////
  375. -- ///// Returns:   bool    //////
  376. -- ///////////////////////////////
  377.  
  378. function ChatBox:MoveChatboxDown()
  379.     --if(inventory.state == false) then
  380.         if(self.drawingPos < #self.groupText[self.selectedGroup]-9) then
  381.             self.drawingPos = self.drawingPos+1
  382.         end
  383.     --end
  384.     return true;
  385. end
  386.  
  387.  
  388. -- ///////////////////////////////
  389. -- ///// IsCommand          //////
  390. -- ///// Returns:   bool    //////
  391. -- ///////////////////////////////
  392.  
  393. function ChatBox:IsCommand(str)
  394.     local start, ende = string.find(str, "/", 0, true);
  395.     if(tonumber(start) == 1 and tonumber(ende) == 1) then
  396.         return true;
  397.     else
  398.         return false;
  399.     end
  400. end
  401.  
  402. -- ///////////////////////////////
  403. -- ///// Hide               //////
  404. -- ///// Returns:   bool    //////
  405. -- ///////////////////////////////
  406.  
  407.  
  408. function ChatBox:Hide()
  409.     if(self.state == true) then
  410.         for index, guiEle in pairs(self.guiEle) do
  411.             guiSetAlpha(guiEle, 0);
  412.         end
  413.         self.state = false;
  414.         showChat(false);
  415.        
  416.         self.btnShow = guiCreateButton(5, sy-(aesy-649), 15, 29, ">", false)
  417.         guiSetFont(self.btnShow, "default-bold-small");
  418.         addEventHandler("onClientGUIClick", self.btnShow, function()
  419.             destroyElement(source);
  420.             self:Show();
  421.         end, false)
  422.        
  423.         if(isElement(self.btnHide)) then
  424.             destroyElement(self.btnHide);
  425.         end
  426.        
  427.         return true;
  428.     end
  429.     return false;
  430. end
  431.  
  432. -- ///////////////////////////////
  433. -- ///// Show               //////
  434. -- ///// Returns:   bool    //////
  435. -- ///////////////////////////////
  436.  
  437.  
  438. function ChatBox:Show()
  439.     if(self.state == false) then
  440.         for index, guiEle in pairs(self.guiEle) do
  441.             guiSetAlpha(guiEle, 255);
  442.         end
  443.         self.state = true;
  444.         showChat(false);
  445.        
  446.         self.btnHide = guiCreateButton(373, sy-(aesy-649), 15, 29, "<", false)
  447.         guiSetFont(self.btnHide, "default-bold-small");
  448.        
  449.         addEventHandler("onClientGUIClick", self.btnHide, function()
  450.             destroyElement(source);
  451.             self:Hide();
  452.         end, false)
  453.        
  454.         if(isElement(self.btnShow)) then
  455.             destroyElement(self.btnShow);
  456.         end
  457.        
  458.         return true;
  459.     end
  460.     return false;
  461. end
  462.  
  463. -- ///////////////////////////////
  464. -- ///// MoveToBack         //////
  465. -- ///// Returns:   bool    //////
  466. -- ///////////////////////////////
  467.  
  468. function ChatBox:MoveToBack()
  469.     return guiMoveToBack(self.guiEle.absenden);
  470. end
  471.  
  472. -- ///////////////////////////////
  473. -- ///// Constructor        //////
  474. -- ///// Returns:   void    //////
  475. -- ///////////////////////////////
  476.  
  477. function ChatBox:Constructor(...)
  478.    
  479.     -- METHODS --
  480.    
  481.     self.renderFunc = function() self:Render() end
  482.     self.acceptFunc = function() self:AcceptText() end
  483.     self.clickFunc = function() self:ClickEdit() end
  484.     self.selectFunc = function() self:SelectGroup(guiComboBoxGetItemText(self.guiEle.comboBox, guiComboBoxGetSelected(self.guiEle.comboBox))) end
  485.     self.sendTextBackFunc = function(...) self:SendTextBack(...) end;
  486.     self.normalChatFunc = function(text, r, g, b) self:SendTextBack("System", text, r, g, b) end;
  487.    
  488.     self.moveUpFunc = function() self:MoveChatboxUp() end;
  489.     self.moveDownFunc = function() self:MoveChatboxDown() end;
  490.     self.removeChatInput = function(button, state) if(button ~= "left") and (state == "down") then self:MoveToBack() end end;
  491.  
  492.     self.openChatFunc = function()
  493.         guiSetInputMode("no_binds_when_editing");
  494.         toggleAllControls(false);
  495.         guiBringToFront(self.guiEle.absenden)
  496.         guiEditSetCaretIndex(self.guiEle.absenden, 0)
  497.         setTimer(function()
  498.             guiSetText(self.guiEle.absenden, "");
  499.         end, 50, 1)
  500.     end;
  501.    
  502.     -- INSTANZEN --
  503.     self.state = false;
  504.     self.guiEle = {};
  505.    
  506.     self.groups = {};
  507.     self.itemIds = {};
  508.     self.groupFunc = {};
  509.    
  510.     self.add = 0;
  511.    
  512.     self.groupText = {};
  513.     self.groupTextColor = {};
  514.    
  515.    
  516.     self.selectedGroup = "none";
  517.    
  518.     self.drawingPos = 0;
  519.    
  520.     -- Aufrufen der Methoden bei Erstellung um Fehler zu vermeiden
  521.     self:CreateGui();
  522.    
  523.     self:Show();
  524.        
  525.     showPlayerHudComponent("radar", false);
  526.    
  527.     -- EVENTS? --
  528.     addEvent("ChatBox:SendTextBack", true);
  529.    
  530.     addEventHandler("onClientRender", getRootElement(), self.renderFunc);
  531.     addEventHandler("onClientGUIAccepted", self.guiEle.absenden, self.acceptFunc);
  532.     addEventHandler("onClientGUIClick", self.guiEle.absenden, self.clickFunc);
  533.     addEventHandler("onClientGUIComboBoxAccepted", self.guiEle.comboBox, self.selectFunc);
  534.     addEventHandler("ChatBox:SendTextBack", getLocalPlayer(), self.sendTextBackFunc);
  535.     addEventHandler("onClientChatMessage", getRootElement(), self.normalChatFunc);
  536.     addEventHandler("onClientClick", getRootElement(), self.removeChatInput);
  537.    
  538.     -- Binds
  539.     bindKey("pgup", "down", self.moveUpFunc);
  540.     bindKey("pgdn", "down", self.moveDownFunc);
  541.  
  542.     bindKey("t", "down", self.openChatFunc);
  543.    
  544.     --logger:OutputInfo("[CALLING] ChatBox: Constructor");
  545.  
  546. end
  547.  
  548. -- ///////////////////////////////
  549. -- ///// Destructor         //////
  550. -- ///// Returns:   void    //////
  551. -- ///////////////////////////////
  552.  
  553. function ChatBox:Destructor(...)
  554.     -- Erst weg mit dem Gui
  555.     self:DestroyGui();
  556.    
  557.     -- Dann die Event Handler weg
  558.     removeEventHandler("onClientRender", getRootElement(), self.renderFunc);
  559.     removeEventHandler("onClientGUIAccepted", self.guiEle.absenden, self.acceptFunc);
  560.     removeEventHandler("onClientGUIClick", self.guiEle.absenden, self.clickFunc);
  561.     removeEventHandler("onClientGUIComboBoxAccepted", self.guiEle.comboBox, self.selectFunc);
  562.     removeEventHandler("ChatBox:SendTextBack", getLocalPlayer(), self.sendTextBackFunc);
  563.     removeEventHandler("onClientChatMessage", getRootElement(), self.normalChatFunc);
  564.        
  565.     -- Und die Binds
  566.     unbindKey("pgup", "down", self.moveUpFunc);
  567.     unbindKey("pgdn", "down", self.moveDownFunc);
  568.    
  569.     unbindKey("t", "down", self.openChatFunc);
  570.    
  571.     --logger:OutputInfo("[CALLING] ChatBox: Denstructor");
  572. end
  573.  
  574.  
  575. -- EVENT HANDLER --
  576.     -- END OF CLASS: CChatBox.lua
  577.    
  578.    
  579.     -- Erstellung der Chatbox
  580. do
  581.     -- Lade die Chatbox in die Globale Lua-Umgebung
  582.     _G["chatBox"] = ChatBox:New();
  583.     -- Fuege Gruppen hinzu
  584.  
  585.     _G["chatBox"]:AddGroup("System", true);         -- System muss IMMER DRINNE SEIN!!
  586.    
  587.     -- Name, read-only, r, g, b, key
  588.     _G["chatBox"]:AddGroup("Local chat", false, 255, 255, 0, "l");
  589.     _G["chatBox"]:AddGroup("Global chat", false, 255, 255, 255, "g");
  590.     _G["chatBox"]:AddGroup("Support chat", false, 0, 255, 255, "h");
  591.    
  592.     -- Dann die Systemgruppe auswaehlen, um Fehler zu vermeiden.
  593.     _G["chatBox"]:SelectGroup("System");
  594. end
  595.  
  596.  
  597. --[[
  598.     -- METHODEN:
  599.    
  600.     chatBox:AddGroup(string Name, bool ReadOnly, int Red, int Green, int Blue, string Key-Bind);
  601.     Fuegt eine Neue Gruppe in der Chatbox hinzu, die benutzt werden kann und abgeloggt wird.
  602.    
  603.     chatBox:SelectGroup(string Name);
  604.     Waehlt eine Vorhandene Gruppe aus.
  605.    
  606.     chatBox:RemoveGroup(string Name);
  607.     Entfernt eine vorhandene Gruppe.
  608.    
  609.  
  610. ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement