From 465a7fb58ece5944c2d848418662304f143ca43e Mon Sep 17 00:00:00 2001 From: Cynthia Foxwell Date: Sat, 23 Dec 2023 19:24:41 -0700 Subject: [PATCH] esc to close outside of text entry, re-open to chat tab --- lua/cbox/cl_chatbox.lua | 68 +++++++++++++++++++++++++++++++---------- lua/cbox/sh_utils.lua | 8 +++++ lua/cbox/tabs/chat.lua | 5 +-- 3 files changed, 63 insertions(+), 18 deletions(-) diff --git a/lua/cbox/cl_chatbox.lua b/lua/cbox/cl_chatbox.lua index 3ab8bcb..379377c 100644 --- a/lua/cbox/cl_chatbox.lua +++ b/lua/cbox/cl_chatbox.lua @@ -179,6 +179,7 @@ local function CreateChatbox() if anim.Finished then if out then self:Close() + self:SetVisible(false) else self:MakePopup() end @@ -216,27 +217,27 @@ local function CreateChatbox() local alpha = CHATBOX_ALPHA:GetInt() -- Reset everything to known good - render_SetStencilWriteMask( 0xFF ) - render_SetStencilTestMask( 0xFF ) - render_SetStencilReferenceValue( 0 ) - render_SetStencilPassOperation( STENCIL_KEEP ) - render_SetStencilZFailOperation( STENCIL_KEEP ) + render_SetStencilWriteMask(0xFF) + render_SetStencilTestMask(0xFF) + render_SetStencilReferenceValue(0) + render_SetStencilPassOperation(STENCIL_KEEP) + render_SetStencilZFailOperation(STENCIL_KEEP) render_ClearStencil() -- Enable stencils - render_SetStencilEnable( true ) + render_SetStencilEnable(true) -- Set everything up everything draws to the stencil buffer instead of the screen - render_SetStencilReferenceValue( 1 ) - render_SetStencilCompareFunction( STENCIL_NEVER ) - render_SetStencilFailOperation( STENCIL_REPLACE ) + render_SetStencilReferenceValue(1) + render_SetStencilCompareFunction(STENCIL_NEVER) + render_SetStencilFailOperation(STENCIL_REPLACE) draw_NoTexture() surface_SetDrawColor(255, 255, 255) surface_DrawPoly(BACKGROUND_POLY) -- Only draw things that are in the stencil buffer - render_SetStencilCompareFunction( STENCIL_EQUAL ) - render_SetStencilFailOperation( STENCIL_KEEP ) + render_SetStencilCompareFunction(STENCIL_EQUAL) + render_SetStencilFailOperation(STENCIL_KEEP) if CHATBOX_BLUR:GetBool() and alpha ~= 255 then local x, y = self:LocalToScreen(0, 0) @@ -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() diff --git a/lua/cbox/sh_utils.lua b/lua/cbox/sh_utils.lua index c5ff487..fd33bc6 100644 --- a/lua/cbox/sh_utils.lua +++ b/lua/cbox/sh_utils.lua @@ -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 diff --git a/lua/cbox/tabs/chat.lua b/lua/cbox/tabs/chat.lua index a89b5b4..b0be0e5 100644 --- a/lua/cbox/tabs/chat.lua +++ b/lua/cbox/tabs/chat.lua @@ -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()