Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- #######################################
- -- ## Project: MTA:The Walking Death ##
- -- ## Name: ChatBox ##
- -- ## Author: Noneatme ##
- -- ## Version: 1.0 ##
- -- ## License: See top Folder ##
- -- #######################################
- -- FUNCTIONS / METHODS --
- local cFunc = {}; -- Local Functions
- local cSetting = {}; -- Local Settings
- ChatBox = {};
- ChatBox.__index = ChatBox;
- local sx, sy = guiGetScreenSize()
- local aesx, aesy = 1600, 900;
- --[[
- ]]
- -- ///////////////////////////////
- -- ///// New //////
- -- ///// Returns: object //////
- -- ///////////////////////////////
- function ChatBox:New(...)
- local obj = setmetatable({}, {__index = self});
- if obj.Constructor then
- obj:Constructor(...);
- end
- return obj;
- end
- -- ///////////////////////////////
- -- ///// Render //////
- -- ///// Returns: void //////
- -- ///////////////////////////////
- function ChatBox:Render()
- if(self.state == true) then
- 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)
- dxDrawRectangle(5, sy-(aesy-649), 368, 251, tocolor(0, 0, 0, 128), false)
- dxDrawRectangle(372, sy-(aesy-649), 0, 27, tocolor(255, 255, 255, 255), true)
- dxDrawRectangle(5, sy-(aesy-649), 368, 36, tocolor(0, 0, 0, 128), false)
- dxDrawRectangle(5, sy-(aesy-862), 368, 36, tocolor(0, 0, 0, 128), false)
- local add = 0
- local increment = 17
- --[[
- dxDrawText("Line 1", 3, 687, 363, 705, tocolor(255, 255, 255, 255), 1, "default-bold", "left", "center", false, false, true, false, false)
- dxDrawText("Line 2", 3, 704, 363, 722, tocolor(255, 255, 255, 255), 1, "default-bold", "left", "center", false, false, true, false, false)]]
- for i = self.drawingPos, self.drawingPos+9, 1 do
- if(self.groupText[self.selectedGroup]) then
- local text = self.groupText[self.selectedGroup][i];
- if(text) then
- local temp = string.gsub(text, '#%x%x%x%x%x%x', '')
- local cc = false;
- if(temp ~= text) then -- Colorcodes
- cc = true;
- end
- local r, g, b = self.groupTextColor[self.selectedGroup][i]["r"], self.groupTextColor[self.selectedGroup][i]["g"], self.groupTextColor[self.selectedGroup][i]["b"];
- local a = self.groupTextColor[self.selectedGroup][i]["a"];
- if(a < 255) then
- a = a+5
- end
- self.groupTextColor[self.selectedGroup][i]["a"] = a; -- 705
- 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)
- add = add+increment;
- if(self.add > 0) then
- self.add = self.add-0.1
- end
- end
- end
- end
- end
- end
- -- ///////////////////////////////
- -- ///// GetTextCount //////
- -- ///// Returns: int //////
- -- ///////////////////////////////
- function ChatBox:GetTextCount(text)
- local count = 0;
- if(text) then
- if(string.len(text) > 0) and (string.len(text) < 40) then
- count = count+1;
- elseif(string.len(text) > 40) and (string.len(text) < 80) then
- count = count+2;
- elseif(string.len(text) > 80) and (string.len(text) < 120) then
- count = count+3;
- elseif(string.len(text) > 120) and (string.len(text) < 160) then
- count = count+4;
- elseif(string.len(text) > 160) and (string.len(text) < 200) then
- count = count+5;
- elseif(string.len(text) > 200) and (string.len(text) < 240) then
- count = count+6;
- elseif(string.len(text) > 240) and (string.len(text) < 280) then
- count = count+7;
- else
- count = count+1;
- end
- end
- return count;
- end
- -- ///////////////////////////////
- -- ///// CreateGui //////
- -- ///// Returns: bool //////
- -- ///////////////////////////////
- function ChatBox:CreateGui()
- guiSetInputMode ( "no_binds_when_editing" )
- self.guiEle.comboBox = guiCreateComboBox(195, sy-(aesy-655), 174, 115, "", false)
- guiSetFont(self.guiEle.comboBox, "default-bold-small")
- self.guiEle.absenden = guiCreateEdit(6, sy-(aesy-865), 363, 31, "Enter your message...", false);
- guiSetFont(self.guiEle.absenden, "default-bold-small")
- guiEditSetMaxLength (self.guiEle.absenden, 64);
- return true;
- end
- -- ///////////////////////////////
- -- ///// DestroyGui //////
- -- ///// Returns: bool //////
- -- ///////////////////////////////
- function ChatBox:DestroyGui()
- for index, guiEle in pairs(self.guiEle) do
- if(isElement(guiEle)) then
- destroyElement(guiEle)
- end
- end
- return true;
- end
- -- ///////////////////////////////
- -- ///// AcceptText //////
- -- ///// Returns: bool //////
- -- ///////////////////////////////
- function ChatBox:AcceptText()
- --guiMoveToBack(self.guiEle.absenden);
- local text = guiGetText(self.guiEle.absenden);
- toggleAllControls(true);
- if(string.len(string.gsub(text, " ", "")) > 0) then
- guiSetText(self.guiEle.absenden, "");
- guiMoveToBack(self.guiEle.absenden);
- if(self:IsCommand(text)) then
- local cmd = gettok(text, 1, "/");
- local params = split(cmd, " ");
- table.remove(params, 1);
- triggerServerEvent("ChatBox:SendCommand", getLocalPlayer(), cmd, params);
- executeCommandHandler(cmd, unpack(params))
- else
- if(self.groupText[self.selectedGroup]["locked"] ~= true) then
- if(self:ContainsBadWords(text) == true) then
- else
- local r, g, b = self.groupText[self.selectedGroup]["color"][1], self.groupText[self.selectedGroup]["color"][2], self.groupText[self.selectedGroup]["color"][3]
- triggerServerEvent("ChatBox:SendText", getLocalPlayer(), self.selectedGroup, text, r, g, b);
- end
- end
- end
- else
- guiSetText(self.guiEle.absenden, "");
- guiMoveToBack(self.guiEle.absenden);
- end
- return true;
- end
- -- ///////////////////////////////
- -- ///// ClickEdit //////
- -- ///// Returns: bool //////
- -- ///////////////////////////////
- function ChatBox:ClickEdit()
- if(guiGetText(self.guiEle.absenden) == "Enter your message...") then
- guiSetText(self.guiEle.absenden, "");
- end
- return true;
- end
- -- ///////////////////////////////
- -- ///// ContainsBadWords //////
- -- ///// Returns: bool //////
- -- ///////////////////////////////
- function ChatBox:ContainsBadWords()
- return false;
- end
- -- ///////////////////////////////
- -- ///// AddGroup //////
- -- ///// Returns: bool //////
- -- ///////////////////////////////
- function ChatBox:AddGroup(name, locked, r, g, b, key)
- if(self.groups[name] == nil) then
- local id = guiComboBoxAddItem(self.guiEle.comboBox, name);
- self.itemIds[name] = id;
- self.groups[name] = true;
- self.groupText[name] = {};
- self.groupTextColor[name] = {};
- self.groupText[name]["index"] = 1;
- self.groupText[name]["locked"] = locked or false;
- if(r and g and b) then
- self.groupText[name]["color"] = {r, g, b};
- end
- -- KEY --
- if(key) then
- self.groupFunc[name] = function()
- self:SelectGroup(name);
- end
- bindKey(key, "down", self.groupFunc[name]);
- end
- return true;
- end
- return false;
- end
- -- ///////////////////////////////
- -- ///// RemoveGroup //////
- -- ///// Returns: bool //////
- -- ///////////////////////////////
- function ChatBox:RemoveGroup(name)
- if(self.groups[name] ~= nil) then
- guiComboBoxRemoveItem(self.guiEle.comboBox, self.itemIds[name]);
- if(self.selectedGroup == name) then
- self:SelectGroup("System")
- end
- if(self.groupFunc[name]) then
- unbindKey(key, "down", self.groupFunc[name]);
- end
- return true;
- end
- return false;
- end
- -- ///////////////////////////////
- -- ///// SelectGroup //////
- -- ///// Returns: bool //////
- -- ///////////////////////////////
- function ChatBox:SelectGroup(name)
- self.selectedGroup = name;
- if(self.groups[name] ~= nil) then
- guiComboBoxSetSelected (self.guiEle.comboBox, self.itemIds[name]);
- end
- self.drawingPos = 1;
- return true;
- end
- -- ///////////////////////////////
- -- ///// SaveToLog //////
- -- ///// Returns: bool //////
- -- ///////////////////////////////
- function ChatBox:SaveToLog(group, text)
- if(text) then
- local date1 = (getRealTime().monthday).."."..(getRealTime().month+1).."."..(getRealTime().year+1900).." "..getRealTime().hour..":"..getRealTime().minute..":"..getRealTime().second;
- local date2 = (getRealTime().monthday).."."..(getRealTime().month+1).."."..(getRealTime().year+1900);
- local fileString = "savedFiles/logs/"..date2.."/"..group..".log";
- local file;
- text = string.gsub(text, '#%x%x%x%x%x%x', '')
- if(fileExists(fileString)) then
- file = fileOpen(fileString);
- else
- file = fileCreate(fileString);
- end
- fileSetPos(file, fileGetSize(file));
- fileWrite(file, "["..date1.."]"..text.."\n");
- fileFlush(file);
- fileClose(file);
- return true;
- end
- return false;
- end
- -- ///////////////////////////////
- -- ///// SendTextBack //////
- -- ///// Returns: bool //////
- -- ///////////////////////////////
- function ChatBox:SendTextBack(group, text, r, g, b)
- if not(self.groupText[group]) then
- self.groupText[group] = {}
- self.groupText[group]["index"] = 1;
- end
- self.groupText[group][self.groupText[group]["index"]] = text;
- self.groupTextColor[group][self.groupText[group]["index"]] = {};
- self.groupTextColor[group][self.groupText[group]["index"]]["r"] = r or 255;
- self.groupTextColor[group][self.groupText[group]["index"]]["g"] = g or 255;
- self.groupTextColor[group][self.groupText[group]["index"]]["b"] = b or 255;
- self.groupTextColor[group][self.groupText[group]["index"]]["a"] = 0;
- self.groupText[group]["index"] = self.groupText[group]["index"]+1;
- self.drawingPos = #self.groupText[group]-9;
- self:SaveToLog(group, text);
- if(group ~= "System") then
- self:SendTextBack("System", "["..string.upper(gettok(group, 1, " ")).."] "..text, r, g, b)
- end
- return true;
- end
- -- ///////////////////////////////
- -- ///// MoveChatboxUp //////
- -- ///// Returns: bool //////
- -- ///////////////////////////////
- function ChatBox:MoveChatboxUp()
- --if(inventory.state == false) then
- if(self.drawingPos > 1) then
- self.drawingPos = self.drawingPos-1
- end
- --end
- return true;
- end
- -- ///////////////////////////////
- -- ///// MoveChatboxDown //////
- -- ///// Returns: bool //////
- -- ///////////////////////////////
- function ChatBox:MoveChatboxDown()
- --if(inventory.state == false) then
- if(self.drawingPos < #self.groupText[self.selectedGroup]-9) then
- self.drawingPos = self.drawingPos+1
- end
- --end
- return true;
- end
- -- ///////////////////////////////
- -- ///// IsCommand //////
- -- ///// Returns: bool //////
- -- ///////////////////////////////
- function ChatBox:IsCommand(str)
- local start, ende = string.find(str, "/", 0, true);
- if(tonumber(start) == 1 and tonumber(ende) == 1) then
- return true;
- else
- return false;
- end
- end
- -- ///////////////////////////////
- -- ///// Hide //////
- -- ///// Returns: bool //////
- -- ///////////////////////////////
- function ChatBox:Hide()
- if(self.state == true) then
- for index, guiEle in pairs(self.guiEle) do
- guiSetAlpha(guiEle, 0);
- end
- self.state = false;
- showChat(false);
- self.btnShow = guiCreateButton(5, sy-(aesy-649), 15, 29, ">", false)
- guiSetFont(self.btnShow, "default-bold-small");
- addEventHandler("onClientGUIClick", self.btnShow, function()
- destroyElement(source);
- self:Show();
- end, false)
- if(isElement(self.btnHide)) then
- destroyElement(self.btnHide);
- end
- return true;
- end
- return false;
- end
- -- ///////////////////////////////
- -- ///// Show //////
- -- ///// Returns: bool //////
- -- ///////////////////////////////
- function ChatBox:Show()
- if(self.state == false) then
- for index, guiEle in pairs(self.guiEle) do
- guiSetAlpha(guiEle, 255);
- end
- self.state = true;
- showChat(false);
- self.btnHide = guiCreateButton(373, sy-(aesy-649), 15, 29, "<", false)
- guiSetFont(self.btnHide, "default-bold-small");
- addEventHandler("onClientGUIClick", self.btnHide, function()
- destroyElement(source);
- self:Hide();
- end, false)
- if(isElement(self.btnShow)) then
- destroyElement(self.btnShow);
- end
- return true;
- end
- return false;
- end
- -- ///////////////////////////////
- -- ///// MoveToBack //////
- -- ///// Returns: bool //////
- -- ///////////////////////////////
- function ChatBox:MoveToBack()
- return guiMoveToBack(self.guiEle.absenden);
- end
- -- ///////////////////////////////
- -- ///// Constructor //////
- -- ///// Returns: void //////
- -- ///////////////////////////////
- function ChatBox:Constructor(...)
- -- METHODS --
- self.renderFunc = function() self:Render() end
- self.acceptFunc = function() self:AcceptText() end
- self.clickFunc = function() self:ClickEdit() end
- self.selectFunc = function() self:SelectGroup(guiComboBoxGetItemText(self.guiEle.comboBox, guiComboBoxGetSelected(self.guiEle.comboBox))) end
- self.sendTextBackFunc = function(...) self:SendTextBack(...) end;
- self.normalChatFunc = function(text, r, g, b) self:SendTextBack("System", text, r, g, b) end;
- self.moveUpFunc = function() self:MoveChatboxUp() end;
- self.moveDownFunc = function() self:MoveChatboxDown() end;
- self.removeChatInput = function(button, state) if(button ~= "left") and (state == "down") then self:MoveToBack() end end;
- self.openChatFunc = function()
- guiSetInputMode("no_binds_when_editing");
- toggleAllControls(false);
- guiBringToFront(self.guiEle.absenden)
- guiEditSetCaretIndex(self.guiEle.absenden, 0)
- setTimer(function()
- guiSetText(self.guiEle.absenden, "");
- end, 50, 1)
- end;
- -- INSTANZEN --
- self.state = false;
- self.guiEle = {};
- self.groups = {};
- self.itemIds = {};
- self.groupFunc = {};
- self.add = 0;
- self.groupText = {};
- self.groupTextColor = {};
- self.selectedGroup = "none";
- self.drawingPos = 0;
- -- Aufrufen der Methoden bei Erstellung um Fehler zu vermeiden
- self:CreateGui();
- self:Show();
- showPlayerHudComponent("radar", false);
- -- EVENTS? --
- addEvent("ChatBox:SendTextBack", true);
- addEventHandler("onClientRender", getRootElement(), self.renderFunc);
- addEventHandler("onClientGUIAccepted", self.guiEle.absenden, self.acceptFunc);
- addEventHandler("onClientGUIClick", self.guiEle.absenden, self.clickFunc);
- addEventHandler("onClientGUIComboBoxAccepted", self.guiEle.comboBox, self.selectFunc);
- addEventHandler("ChatBox:SendTextBack", getLocalPlayer(), self.sendTextBackFunc);
- addEventHandler("onClientChatMessage", getRootElement(), self.normalChatFunc);
- addEventHandler("onClientClick", getRootElement(), self.removeChatInput);
- -- Binds
- bindKey("pgup", "down", self.moveUpFunc);
- bindKey("pgdn", "down", self.moveDownFunc);
- bindKey("t", "down", self.openChatFunc);
- --logger:OutputInfo("[CALLING] ChatBox: Constructor");
- end
- -- ///////////////////////////////
- -- ///// Destructor //////
- -- ///// Returns: void //////
- -- ///////////////////////////////
- function ChatBox:Destructor(...)
- -- Erst weg mit dem Gui
- self:DestroyGui();
- -- Dann die Event Handler weg
- removeEventHandler("onClientRender", getRootElement(), self.renderFunc);
- removeEventHandler("onClientGUIAccepted", self.guiEle.absenden, self.acceptFunc);
- removeEventHandler("onClientGUIClick", self.guiEle.absenden, self.clickFunc);
- removeEventHandler("onClientGUIComboBoxAccepted", self.guiEle.comboBox, self.selectFunc);
- removeEventHandler("ChatBox:SendTextBack", getLocalPlayer(), self.sendTextBackFunc);
- removeEventHandler("onClientChatMessage", getRootElement(), self.normalChatFunc);
- -- Und die Binds
- unbindKey("pgup", "down", self.moveUpFunc);
- unbindKey("pgdn", "down", self.moveDownFunc);
- unbindKey("t", "down", self.openChatFunc);
- --logger:OutputInfo("[CALLING] ChatBox: Denstructor");
- end
- -- EVENT HANDLER --
- -- END OF CLASS: CChatBox.lua
- -- Erstellung der Chatbox
- do
- -- Lade die Chatbox in die Globale Lua-Umgebung
- _G["chatBox"] = ChatBox:New();
- -- Fuege Gruppen hinzu
- _G["chatBox"]:AddGroup("System", true); -- System muss IMMER DRINNE SEIN!!
- -- Name, read-only, r, g, b, key
- _G["chatBox"]:AddGroup("Local chat", false, 255, 255, 0, "l");
- _G["chatBox"]:AddGroup("Global chat", false, 255, 255, 255, "g");
- _G["chatBox"]:AddGroup("Support chat", false, 0, 255, 255, "h");
- -- Dann die Systemgruppe auswaehlen, um Fehler zu vermeiden.
- _G["chatBox"]:SelectGroup("System");
- end
- --[[
- -- METHODEN:
- chatBox:AddGroup(string Name, bool ReadOnly, int Red, int Green, int Blue, string Key-Bind);
- Fuegt eine Neue Gruppe in der Chatbox hinzu, die benutzt werden kann und abgeloggt wird.
- chatBox:SelectGroup(string Name);
- Waehlt eine Vorhandene Gruppe aus.
- chatBox:RemoveGroup(string Name);
- Entfernt eine vorhandene Gruppe.
- ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement