function AquaChat.OpenChat()
if AquaChat.IsOpen then return end
AquaChat.IsOpen = true
for _, msg in ipairs(AquaChat.Messages) do
msg.alpha = 255
msg.visible = true
msg.fadingOut = false
end
if IsValid(AquaChat.ChatEntry) then
AquaChat.ChatEntry:Remove()
end
AquaChat.X = (ScrW() - AquaChat.Width) / 2
local inputY = ScrH() * 0.7
AquaChat.ChatEntry = vgui.Create("DTextEntry")
AquaChat.ChatEntry:SetSize(AquaChat.Width, 25)
AquaChat.ChatEntry:SetPos(AquaChat.X, inputY)
AquaChat.ChatEntry:SetText("")
AquaChat.ChatEntry:SetTextColor(Color(255, 255, 255))
AquaChat.ChatEntry:SetFont("AquaChatFont")
AquaChat.ChatEntry:SetDrawLanguageID(false)
AquaChat.ChatEntry:RequestFocus()
AquaChat.ChatEntry:SetHistoryEnabled(true)
AquaChat.ChatEntry.Paint = function(self, w, h)
draw.RoundedBox(4, 0, 0, w, h, Color(0, 0, 0, 200))
self:DrawTextEntryText(Color(255, 255, 255), Color(30, 130, 255), Color(255, 255, 255))
end
AquaChat.ChatEntry.OnEnter = function(self)
local text = self:GetText()
if text ~= "" then
table.insert(AquaChat.ChatHistory, 1, text)
if AquaChat.DefaultChatType ~= "LOCAL" and not string.StartWith(text, "/") then
local cmd = ({
["OOC"] = "/OOC ",
["Д"] = "/ME ",
["Р"] = "/DO ",
["Т"] = "/Т ",
["М"] = "/М ",
["В"] = "/SHOUT ",
})[AquaChat.DefaultChatType] or ""
text = cmd .. text
end
LocalPlayer():ConCommand("say " .. text)
end
AquaChat.HistoryIndex = 0
AquaChat.CloseChat()
end
AquaChat.ChatEntry.OnTextChanged = function(self)
AquaChat.InputText = self:GetText()
end
AquaChat.ChatEntry.OnKeyCodeTyped = function(self, code)
if code == KEY_UP then
if AquaChat.HistoryIndex < #AquaChat.ChatHistory then
AquaChat.HistoryIndex = AquaChat.HistoryIndex + 1
self:SetText(AquaChat.ChatHistory[AquaChat.HistoryIndex])
self:SetCaretPos(#self:GetText())
end
elseif code == KEY_DOWN then
if AquaChat.HistoryIndex > 0 then
AquaChat.HistoryIndex = AquaChat.HistoryIndex - 1
if AquaChat.HistoryIndex == 0 then
self:SetText("")
else
self:SetText(AquaChat.ChatHistory[AquaChat.HistoryIndex])
end
self:SetCaretPos(#self:GetText())
end
elseif code == KEY_TAB then
local types = {"LOCAL", "OOC", "Д", "Р", "Т", "М", "В"}
local currentIndex = table.KeyFromValue(types, AquaChat.DefaultChatType) or 1
AquaChat.DefaultChatType = types[(currentIndex % #types) + 1]
local currentText = self:GetText()
if not string.StartWith(currentText, "/") then
local cmd = ({
["OOC"] = "/ooc ",
["Д"] = "/me ",
["Р"] = "/do ",
["Т"] = "/t ",
["М"] = "/m ",
["В"] = "/shout ",
["LOCAL"] = ""
})[AquaChat.DefaultChatType] or ""
self:SetText(cmd .. currentText)
self:SetCaretPos(string.len(cmd .. currentText))
end
return true
end
end
AquaChat.CreateButtons()
end
function AquaChat.CreateButtons()
for _, btn in pairs(AquaChat.ChatButtons) do
if IsValid(btn) then
btn:Remove()
end
end
AquaChat.ChatButtons = {}
local buttonTypes = {
{type = "LOCAL", text = "Локальный", command = ""},
{type = "OOC", text = "OOC", command = "/ooc "},
{type = "Д", text = "Действие", command = "/me "},
{type = "Р", text = "Роль", command = "/do "},
{type = "Т", text = "Тихо", command = "/t "},
{type = "М", text = "МЕГАФОН", command = "/m "},
{type = "В", text = "Крик", command = "/shout "}
}
local buttonY = (AquaChat.ChatEntry and AquaChat.ChatEntry:GetY() or ScrH() * 0.7) + 30
local currentX = AquaChat.X
surface.SetFont("AquaChatFont")
for _, btnData in ipairs(buttonTypes) do
local textWidth = surface.GetTextSize(btnData.text)
local btnWidth = textWidth + AquaChat.ButtonPadding * 2
local btn = vgui.Create("DButton")
btn:SetSize(btnWidth, 25)
btn:SetPos(currentX, buttonY)
btn:SetText(btnData.text)
btn:SetTextColor(Color(255, 255, 255))
btn:SetFont("AquaChatFont")
btn.Paint = function(self, w, h)
local bgColor = Color(AquaChat.ButtonColor.r, AquaChat.ButtonColor.g, AquaChat.ButtonColor.b,
AquaChat.DefaultChatType == btnData.type and 200 or 100)
draw.RoundedBox(4, 0, 0, w, h, bgColor)
end
btn.DoClick = function()
AquaChat.DefaultChatType = btnData.type
if IsValid(AquaChat.ChatEntry) then
local currentText = AquaChat.ChatEntry:GetText()
if currentText == "" or not string.StartWith(currentText, "/") then
AquaChat.ChatEntry:SetText(btnData.command)
AquaChat.ChatEntry:SetCaretPos(string.len(btnData.command))
end
end
end
table.insert(AquaChat.ChatButtons, btn)
currentX = currentX + btnWidth + AquaChat.ButtonMargin
end
end
function AquaChat.CloseChat()
if not AquaChat.IsOpen then return end
AquaChat.IsOpen = false
local closeTime = CurTime()
for _, msg in ipairs(AquaChat.Messages) do
msg.fadingOut = true
msg.fadeStartTime = closeTime
end
if IsValid(AquaChat.ChatEntry) then
AquaChat.ChatEntry:Remove()
AquaChat.ChatEntry = nil
end
for _, btn in pairs(AquaChat.ChatButtons) do
if IsValid(btn) then
btn:Remove()
end
end
AquaChat.ChatButtons = {}
gui.HideGameUI()
end
hook.Add("PlayerBindPress", "AquaChat_MouseWheelScroll", function(ply, bind, pressed)
if not AquaChat.IsOpen then return end
if bind == "invnext" then
AquaChat.ScrollOffset = math.min(AquaChat.ScrollOffset + AquaChat.WheelScrollSpeed,
math.max(0, #AquaChat.Messages - AquaChat.VisibleMessages))
return true
elseif bind == "invprev" then
AquaChat.ScrollOffset = math.max(AquaChat.ScrollOffset - AquaChat.WheelScrollSpeed, 0)
return true
end
end)
hook.Add("GUIMousePressed", "AquaChat_CloseOnClick", function()
if AquaChat.IsOpen then
AquaChat.CloseChat()
end
end)
concommand.Add("aquachat_toggle_timestamps", function()
AquaChat.ShowTimestamps = not AquaChat.ShowTimestamps
print("Timestamps are now " .. (AquaChat.ShowTimestamps and "enabled" or "disabled"))
end)
concommand.Add("aquachat_toggle_background", function()
AquaChat.ShowBackground = not AquaChat.ShowBackground
print("Chat background is now " .. (AquaChat.ShowBackground and "enabled" or "disabled"))
end)