esc to close outside of text entry, re-open to chat tab

This commit is contained in:
Cynthia Foxwell 2023-12-23 19:24:41 -07:00
parent 10e902ddcf
commit 465a7fb58e
3 changed files with 63 additions and 18 deletions

View file

@ -179,6 +179,7 @@ local function CreateChatbox()
if anim.Finished then if anim.Finished then
if out then if out then
self:Close() self:Close()
self:SetVisible(false)
else else
self:MakePopup() self:MakePopup()
end end
@ -269,6 +270,13 @@ local function CreateChatbox()
frame:SetCookie("height", h) frame:SetCookie("height", h)
end end
function frame:OnKeyCodeTyped(key)
if key == KEY_ESCAPE then
cbox.chatbox.Close()
gui.HideGameUI()
end
end
cbox.chatbox.panels.frame = frame cbox.chatbox.panels.frame = frame
local tabs = vgui.Create("DPropertySheet", frame, "cbox.chatbox.tabs") local tabs = vgui.Create("DPropertySheet", frame, "cbox.chatbox.tabs")
@ -283,6 +291,8 @@ local function CreateChatbox()
end end
end end
cbox.chatbox.panels.tabs = tabs
for id, tab in next, cbox.chatbox.tabs do for id, tab in next, cbox.chatbox.tabs do
local function catch(err) local function catch(err)
cbox.utils.RealmError(("Failed to create tab %q:"):format(id), err) cbox.utils.RealmError(("Failed to create tab %q:"):format(id), err)
@ -342,6 +352,11 @@ local function Init()
end end
hook.Add("PlayerBindPress", "cbox.chatbox", function(ply, bind, pressed) hook.Add("PlayerBindPress", "cbox.chatbox", function(ply, bind, pressed)
local lply = LocalPlayer()
if IsValid(lply) and ply ~= lply then
cbox.utils.RealmError("Got mismatched player. How???", ply)
return
end
if bind ~= "messagemode" and bind ~= "messagemode2" then return end if bind ~= "messagemode" and bind ~= "messagemode2" then return end
if not pressed then return end if not pressed then return end
@ -350,6 +365,13 @@ local function Init()
return true return true
end) end)
hook.Add("Think", "cbox.chatbox", function()
if cbox.chatbox.IsOpen() and input.IsKeyDown(KEY_ESCAPE) then
cbox.chatbox.Close()
gui.HideGameUI()
end
end)
hook.Run("cbox.chatbox.Initialize") hook.Run("cbox.chatbox.Initialize")
end end
@ -366,14 +388,14 @@ function cbox.chatbox.Open(alt)
frame:SetVisible(true) frame:SetVisible(true)
if frame.animFade and CHATBOX_FADE:GetBool() then if frame.animFade ~= nil and CHATBOX_FADE:GetBool() then
frame.animFade:Start(0.1, false) frame.animFade:Start(0.1, false)
else else
frame:SetAlpha(255) frame:SetAlpha(255)
frame:MakePopup() frame:MakePopup()
end end
if not IsValid(cbox.chatbox.panels.input) then if not IsValid(cbox.chatbox.panels.input) or not IsValid(cbox.chatbox.panels.tabs) then
-- attempt to reinit -- attempt to reinit
if IsValid(frame) then if IsValid(frame) then
frame:Remove() frame:Remove()
@ -381,8 +403,8 @@ function cbox.chatbox.Open(alt)
Init() Init()
end end
if not IsValid(cbox.chatbox.panels.input) then if not IsValid(cbox.chatbox.panels.input) or not IsValid(cbox.chatbox.panels.tabs) then
cbox.utils.RealmError("Input isn't valid, chat tab failed to load, bailing!") cbox.utils.RealmError("Input or tabs aren't valid, chat tab failed to load, bailing!")
if IsValid(frame) then if IsValid(frame) then
frame:Remove() frame:Remove()
end end
@ -390,6 +412,12 @@ function cbox.chatbox.Open(alt)
return return
end end
for _, tab in ipairs(cbox.chatbox.panels.tabs:GetItems()) do
if tab.Panel.cbox_id == "\1chat" and cbox.chatbox.panels.tabs:GetActiveTab() ~= tab.Tab then
cbox.chatbox.panels.tabs:SetActiveTab(tab.Tab)
end
end
cbox.chatbox.panels.input:RequestFocus() cbox.chatbox.panels.input:RequestFocus()
if IsValid(cbox.chatbox.panels.mode_switch) then if IsValid(cbox.chatbox.panels.mode_switch) then
@ -407,10 +435,11 @@ end
function cbox.chatbox.Close() function cbox.chatbox.Close()
local frame = cbox.chatbox.panels.frame local frame = cbox.chatbox.panels.frame
if frame.animFade and CHATBOX_FADE:GetBool() then if frame.animFade ~= nil and CHATBOX_FADE:GetBool() then
frame.animFade:Start(0.1, true) frame.animFade:Start(0.1, true)
else else
frame:Close() frame:Close()
frame:SetVisible(false)
end end
hook.Run("FinishChat") hook.Run("FinishChat")
@ -419,6 +448,13 @@ function cbox.chatbox.Close()
hook.Run("ChatTextChanged", "") hook.Run("ChatTextChanged", "")
end end
---Gets if the chatbox is open
---@return boolean
function cbox.chatbox.IsOpen()
local frame = cbox.chatbox.panels.frame
return IsValid(frame) and frame:IsVisible() == true and frame:GetAlpha() == 255
end
hook.Add("Initialize", "cbox.chatbox", Init) hook.Add("Initialize", "cbox.chatbox", Init)
concommand.Add("cbox_chatbox_reload", function() concommand.Add("cbox_chatbox_reload", function()

View file

@ -9,6 +9,7 @@ local tonumber = tonumber
local tostring = tostring local tostring = tostring
local color_white = Color(255, 255, 255) local color_white = Color(255, 255, 255)
local color_black = Color(0, 0, 0)
local utils = {} local utils = {}
utils.colors = { utils.colors = {
@ -131,4 +132,11 @@ function utils.SharedError(...)
MsgC(utils.colors.ERROR, table.concat(args, " "), "\n") MsgC(utils.colors.ERROR, table.concat(args, " "), "\n")
end end
---Gets a readable foreground color for a background color based on W3C standards
---@param col Color
---@return Color
function utils.GetReadableColor(col)
return (col.r * 0.299 + col.g * 0.587 + col.b * 0.114) > 186 and color_black or color_white
end
cbox.utils = utils cbox.utils = utils

View file

@ -103,10 +103,11 @@ cbox.chatbox.AddTab("\1chat", "Chat", "icon16/comments.png", function()
end end
function input:OnKeyCodeTyped(key) function input:OnKeyCodeTyped(key)
if key == KEY_ESCAPE then --[[if key == KEY_ESCAPE then
cbox.chatbox.Close() cbox.chatbox.Close()
gui.HideGameUI() gui.HideGameUI()
elseif key == KEY_BACKQUOTE then else--]]
if key == KEY_BACKQUOTE then
gui.HideGameUI() gui.HideGameUI()
elseif key == KEY_ENTER then elseif key == KEY_ENTER then
local text = self:GetText():Trim() local text = self:GetText():Trim()