actually load tabs, load in init hook, chat tab error handling

This commit is contained in:
Cynthia Foxwell 2023-12-18 14:46:16 -07:00
parent fad1e8eba6
commit 631874aebc
1 changed files with 45 additions and 11 deletions

View File

@ -16,7 +16,6 @@ cbox.chatbox = cbox.chatbox or {}
cbox.chatbox.tabs = cbox.chatbox.tabs or {}
cbox.chatbox.panels = cbox.chatbox.panels or {}
local CHATBOX_COLOR = CreateClientConVar("cbox_chatbox_color", "160 160 160", true, false, "Chatbox background color")
local CHATBOX_ALPHA = CreateClientConVar("cbox_chatbox_alpha", "128", true, false, "Chatbox background alpha")
local CHATBOX_BLUR = CreateClientConVar("cbox_chatbox_blur", "0", true, false, "Chatbox background is blurred")
@ -108,7 +107,7 @@ local function CreateChatbox()
tabs:SetPadding(0)
function tabs:Paint(w, h)
surface_SetDrawColor(0, 0, 0, 72)
surface_SetDrawColor(0, 0, 0, 128)
surface_DrawRect(0, 20, w, h - 20)
end
@ -130,8 +129,24 @@ local function CreateChatbox()
frame.btnClose:SetZPos(99)
end
if not IsValid(cbox.chatbox.panels.frame) then
CreateChatbox()
local function Init()
local tab_files = file.Find("cbox/tabs/*", "LUA")
for _, name in ipairs(tab_files) do
cbox.utils.RealmPrint("Loading chatbox tab:", name)
include("cbox/tabs/" .. name)
end
if not IsValid(cbox.chatbox.panels.frame) then
CreateChatbox()
end
hook.Add("PlayerBindPress", "cbox.chatbox", function(ply, bind, pressed)
if bind ~= "messagemode" and bind ~= "messagemode2" then return end
cbox.chatbox.Open(bind == "messagemode2")
return true
end)
end
---Opens the chatbox
@ -145,6 +160,24 @@ function cbox.chatbox.Open(alt)
cbox.chatbox.panels.frame:SetVisible(true)
cbox.chatbox.panels.frame:MakePopup()
if not IsValid(cbox.chatbox.panels.input) then
-- attempt to reinit
if IsValid(cbox.chatbox.panels.frame) then
cbox.chatbox.panels.frame:Remove()
end
Init()
end
if not IsValid(cbox.chatbox.panels.input) then
cbox.utils.RealmError("Input isn't valid, chat tab failed to load, bailing!")
if IsValid(cbox.chatbox.panels.frame) then
cbox.chatbox.panels.frame:Remove()
end
hook.Remove("PlayerBindPress", "cbox.chatbox")
return
end
cbox.chatbox.panels.input:RequestFocus()
hook.Run("StartChat")
@ -160,14 +193,15 @@ function cbox.chatbox.Close()
hook.Run("ChatTextChanged", "")
end
hook.Add("PlayerBindPress", "cbox.chatbox", function(ply, bind, pressed)
if bind ~= "messagemode" and bind ~= "messagemode2" then return end
cbox.chatbox.Open(bind == "messagemode2")
return true
end)
hook.Add("Initialize", "cbox.chatbox", Init)
concommand.Add("cbox_chatbox_reload", function()
CreateChatbox()
end, nil, "Reloads the chatbox")
concommand.Add("_cbox_chatbox_fullreload", function()
if IsValid(cbox.chatbox.panels.frame) then
cbox.chatbox.panels.frame:Remove()
end
Init()
end, nil, "Fully reinitializes the chatbox, use only in extreme breakage.")