add modes
This commit is contained in:
parent
387d2606cb
commit
3d10c63702
4 changed files with 91 additions and 9 deletions
|
@ -35,11 +35,14 @@ local surface_SetMaterial = surface.SetMaterial
|
|||
cbox.chatbox = cbox.chatbox or {}
|
||||
cbox.chatbox.tabs = cbox.chatbox.tabs or {}
|
||||
cbox.chatbox.panels = cbox.chatbox.panels or {}
|
||||
cbox.chatbox.modes = cbox.chatbox.modes 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")
|
||||
local CHATBOX_FADE = CreateClientConVar("cbox_chatbox_fade", "1", true, false, "Chatbox fades in and out")
|
||||
local CHATBOX_MODE1 = CreateClientConVar("cbox_chatbox_mode1", "say", true, false, "Default mode when pressing messagemode")
|
||||
local CHATBOX_MODE2 = CreateClientConVar("cbox_chatbox_mode2", "say_team", true, false, "Default mode when pressing messagemode2")
|
||||
|
||||
---@param height number
|
||||
---@return number
|
||||
|
@ -69,6 +72,17 @@ function cbox.chatbox.AddTab(key, name, icon, callback)
|
|||
}
|
||||
end
|
||||
|
||||
---Add a new chatbox send mode
|
||||
---@param key string Internal unique name
|
||||
---@param name string Display name
|
||||
---@param callback fun(text) What to do when enter is pressed on this mode
|
||||
function cbox.chatbox.AddMode(key, name, callback)
|
||||
cbox.chatbox.modes[key] = {
|
||||
name = name,
|
||||
callback = callback,
|
||||
}
|
||||
end
|
||||
|
||||
local MATERIAL_BLUR = Material("pp/blurscreen")
|
||||
|
||||
local function add_rounded_poly(poly, x, y, rad, seg, offset)
|
||||
|
@ -138,9 +152,12 @@ local function CreateChatbox()
|
|||
frame:SetMinWidth(dw)
|
||||
frame:SetMinWidth(dh)
|
||||
|
||||
-- TODO: save resizing/moving
|
||||
frame:SetPos(dx, dy)
|
||||
frame:SetSize(dw, dh)
|
||||
local x = frame:GetCookie("pos_x", dx)
|
||||
local y = frame:GetCookie("pos_y", dy)
|
||||
local w = frame:GetCookie("width", dw)
|
||||
local h = frame:GetCookie("height", dh)
|
||||
frame:SetPos(x, y)
|
||||
frame:SetSize(w, h)
|
||||
|
||||
frame:SetVisible(false)
|
||||
|
||||
|
@ -231,6 +248,15 @@ local function CreateChatbox()
|
|||
render_SetStencilEnable(false)
|
||||
end
|
||||
|
||||
function frame:OnClose()
|
||||
local x, y = frame:GetPos()
|
||||
local w, h = frame:GetSize()
|
||||
frame:SetCookie("pos_x", x)
|
||||
frame:SetCookie("pos_y", y)
|
||||
frame:SetCookie("width", w)
|
||||
frame:SetCookie("height", h)
|
||||
end
|
||||
|
||||
cbox.chatbox.panels.frame = frame
|
||||
|
||||
local tabs = vgui.Create("DPropertySheet", frame, "cbox.chatbox.tabs")
|
||||
|
@ -291,6 +317,8 @@ local function CreateChatbox()
|
|||
end
|
||||
|
||||
local function Init()
|
||||
include("cbox/cl_modes.lua")
|
||||
|
||||
local tab_files = file.Find("cbox/tabs/*", "LUA")
|
||||
for _, name in ipairs(tab_files) do
|
||||
cbox.utils.RealmPrint("Loading chatbox tab:", name)
|
||||
|
@ -309,6 +337,8 @@ local function Init()
|
|||
|
||||
return true
|
||||
end)
|
||||
|
||||
hook.Run("cbox.chatbox.Initialize")
|
||||
end
|
||||
|
||||
---Opens the chatbox
|
||||
|
@ -350,6 +380,14 @@ function cbox.chatbox.Open(alt)
|
|||
|
||||
cbox.chatbox.panels.input:RequestFocus()
|
||||
|
||||
if IsValid(cbox.chatbox.panels.mode_switch) then
|
||||
if alt then
|
||||
cbox.chatbox.panels.mode_switch:UpdateMode(CHATBOX_MODE2:GetString())
|
||||
else
|
||||
cbox.chatbox.panels.mode_switch:UpdateMode(CHATBOX_MODE1:GetString())
|
||||
end
|
||||
end
|
||||
|
||||
hook.Run("StartChat")
|
||||
end
|
||||
|
||||
|
|
15
lua/cbox/cl_modes.lua
Normal file
15
lua/cbox/cl_modes.lua
Normal file
|
@ -0,0 +1,15 @@
|
|||
cbox.chatbox.AddMode("say", "Say", function(text)
|
||||
RunConsoleCommand("say", text)
|
||||
end)
|
||||
|
||||
cbox.chatbox.AddMode("say_team", "Team", function(text)
|
||||
RunConsoleCommand("say_team", text)
|
||||
end)
|
||||
|
||||
cbox.chatbox.AddMode("cmd", "Console", function(text)
|
||||
local function catch(err)
|
||||
LocalPlayer():ChatPrint("Failed to run command: " .. err)
|
||||
end
|
||||
|
||||
xpcall(function() LocalPlayer():ConCommand(text) end, catch)
|
||||
end)
|
|
@ -7,6 +7,7 @@ include("cbox/sh_utils.lua")
|
|||
if SERVER then
|
||||
AddCSLuaFile("cbox/cl_hooks.lua")
|
||||
AddCSLuaFile("cbox/cl_chatbox.lua")
|
||||
AddCSLuaFile("cbox/cl_modes.lua")
|
||||
|
||||
local tab_files = file.Find("cbox/tabs/*", "LUA")
|
||||
for _, name in ipairs(tab_files) do
|
||||
|
|
|
@ -45,11 +45,20 @@ cbox.chatbox.AddTab("\1chat", "Chat", "icon16/comments.png", function()
|
|||
|
||||
local mode_switch = vgui.Create("DButton", input_wrapper)
|
||||
mode_switch:SetTextColor(INPUT_TEXT_COLOR)
|
||||
mode_switch:SetText("Say")
|
||||
mode_switch:SizeToContents()
|
||||
mode_switch:Dock(LEFT)
|
||||
cbox.chatbox.panels.mode_switch = mode_switch
|
||||
|
||||
function mode_switch:UpdateMode(mode)
|
||||
local mode_data = cbox.chatbox.modes[mode]
|
||||
if not mode_data then
|
||||
self.current_mode = "say"
|
||||
mode_data = cbox.chatbox.modes.say
|
||||
end
|
||||
|
||||
self.current_mode = mode
|
||||
self:SetText(mode_data.name)
|
||||
end
|
||||
|
||||
function mode_switch:Paint(w, h)
|
||||
surface_SetDrawColor(0, 0, 0, 128)
|
||||
surface_DrawRect(0, 0, w, h)
|
||||
|
@ -66,13 +75,31 @@ cbox.chatbox.AddTab("\1chat", "Chat", "icon16/comments.png", function()
|
|||
DLabel.ApplySchemeSettings(self)
|
||||
end
|
||||
mode_switch:SetFont("ChatFont")
|
||||
mode_switch:UpdateMode("say")
|
||||
|
||||
function mode_switch:NextMode()
|
||||
local keys = table.GetKeys(cbox.chatbox.modes)
|
||||
table.sort(keys)
|
||||
|
||||
local _, next_mode = next(keys, table.KeyFromValue(keys, self.current_mode))
|
||||
if not next_mode then next_mode = keys[1] end
|
||||
|
||||
self:UpdateMode(next_mode)
|
||||
end
|
||||
|
||||
function mode_switch:DoClick()
|
||||
-- TODO
|
||||
self:NextMode()
|
||||
end
|
||||
function mode_switch:DoRightClick()
|
||||
-- TODO
|
||||
local menu = DermaMenu()
|
||||
|
||||
for mode, data in SortedPairs(cbox.chatbox.modes) do
|
||||
menu:AddOption(data.name, function()
|
||||
self:UpdateMode(mode)
|
||||
end)
|
||||
end
|
||||
|
||||
menu:Open()
|
||||
end
|
||||
|
||||
function input:OnKeyCodeTyped(key)
|
||||
|
@ -84,16 +111,17 @@ cbox.chatbox.AddTab("\1chat", "Chat", "icon16/comments.png", function()
|
|||
elseif key == KEY_ENTER then
|
||||
local text = self:GetText():Trim()
|
||||
if text ~= "" then
|
||||
RunConsoleCommand("say", text)
|
||||
cbox.chatbox.modes[mode_switch.current_mode].callback(text)
|
||||
end
|
||||
|
||||
cbox.chatbox.Close()
|
||||
elseif key == KEY_TAB then
|
||||
if #self:GetText() == 0 then
|
||||
-- TODO: mode switch
|
||||
mode_switch:NextMode()
|
||||
else
|
||||
-- TODO: autocomplete
|
||||
end
|
||||
timer.Simple(0, function() self:RequestFocus() end)
|
||||
else
|
||||
hook.Run("ChatTextChanged", self:GetText())
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue