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 out then
self:Close()
self:SetVisible(false)
else
self:MakePopup()
end
@ -269,6 +270,13 @@ local function CreateChatbox()
frame:SetCookie("height", h)
end
function frame:OnKeyCodeTyped(key)
if key == KEY_ESCAPE then
cbox.chatbox.Close()
gui.HideGameUI()
end
end
cbox.chatbox.panels.frame = frame
local tabs = vgui.Create("DPropertySheet", frame, "cbox.chatbox.tabs")
@ -283,6 +291,8 @@ local function CreateChatbox()
end
end
cbox.chatbox.panels.tabs = tabs
for id, tab in next, cbox.chatbox.tabs do
local function catch(err)
cbox.utils.RealmError(("Failed to create tab %q:"):format(id), err)
@ -342,6 +352,11 @@ local function Init()
end
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 not pressed then return end
@ -350,6 +365,13 @@ local function Init()
return true
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")
end
@ -366,14 +388,14 @@ function cbox.chatbox.Open(alt)
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)
else
frame:SetAlpha(255)
frame:MakePopup()
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
if IsValid(frame) then
frame:Remove()
@ -381,8 +403,8 @@ function cbox.chatbox.Open(alt)
Init()
end
if not IsValid(cbox.chatbox.panels.input) then
cbox.utils.RealmError("Input isn't valid, chat tab failed to load, bailing!")
if not IsValid(cbox.chatbox.panels.input) or not IsValid(cbox.chatbox.panels.tabs) then
cbox.utils.RealmError("Input or tabs aren't valid, chat tab failed to load, bailing!")
if IsValid(frame) then
frame:Remove()
end
@ -390,6 +412,12 @@ function cbox.chatbox.Open(alt)
return
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()
if IsValid(cbox.chatbox.panels.mode_switch) then
@ -407,10 +435,11 @@ end
function cbox.chatbox.Close()
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)
else
frame:Close()
frame:SetVisible(false)
end
hook.Run("FinishChat")
@ -419,6 +448,13 @@ function cbox.chatbox.Close()
hook.Run("ChatTextChanged", "")
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)
concommand.Add("cbox_chatbox_reload", function()

View file

@ -9,6 +9,7 @@ local tonumber = tonumber
local tostring = tostring
local color_white = Color(255, 255, 255)
local color_black = Color(0, 0, 0)
local utils = {}
utils.colors = {
@ -131,4 +132,11 @@ function utils.SharedError(...)
MsgC(utils.colors.ERROR, table.concat(args, " "), "\n")
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

View file

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