Skip to content

Developer Examples & Explanations

Earu edited this page Apr 23, 2020 · 2 revisions

Adding a module

As glua developer you probably already have made those silly little scripts using PlayerSay or OnPlayerChat hooks, well if you were looking for a place to put those little scripts, you can just put them in easychat/modules/ directory, its as simple as that. In theory EasyChat can run everything as a module, if it placed correctly within its module directory.

  • A client-side script goes into modules/client/
  • A server-side script goes into modules/server
  • A shared script just goes in modules/

Adding a typing mode

To add a mode to EasyChat main tab its very easy, there is a function for it and its called EasyChat.AddMode, this function has two parameters.

The first one is a string that will be the name of the mode, and the second one is a function. The function passed as second argument have one parameter which is a string, it is the currently typed DTextEntry text.

Example:

EasyChat.AddMode("Console", function(text) 
    LocalPlayer():ConCommand(text) 
end)

Adding a tab

To add a tab to EasyChat, you need to use EasyChat.AddTab which has two parameters.

The first parameter is a string that is the name of the tab, and the second one is a vgui panel. /!\ you must create the panel with vgui.Create before adding it as a tab/!\

Example:

local panel = vgui.Create("DPanel")
EasyChat.AddTab("A new tab", panel)

Auto-focus

EasyChat has an auto-focus feature that allows you to set a panel the user will be focused on when your tab will be active. For that to happen you need to use EasyChat.SetFocusForOn, the function has two parameters.

First parameter is a string, its the name of your tab, and the second one is the panel that the user will be focused on when opening your tab and pressing keys.

Example:

local tab_name = "test"
local panel = vgui.Create("DPanel")

EasyChat.AddTab(tab_name,panel)
local text_entry = panel:Add("DTextEntry")
EasyChat.SetFocusFor(tab_name, text_entry)

Notifications

Like for the private message tab you can create notifications for your tab by using the function EasyChat.FlashTab, this function has one argument.

The only argument of that function is the name of your tab, afterward it will proceed to flash it as the function name says. Each time EasyChat.FlashTab is called, it creates a new notification, the user will be shown those notifications when the chatbox is opened.

Example:

local tab_name = "test"
local panel = vgui.Create("DPanel")

EasyChat.AddTab(tab_name,panel)
EasyChat.FlashTab(tab_name)
EasyChat.FlashTab(tab_name)

Output: "EC ⮞ 2 new notifications from test"

Shortcuts

EasyChat provides a way to "emulate" keyboard shortcut, there are two functions with the same parameters that allows such thing,EasyChat.AddALTShortcut and EasyChat.AddCTRLShortcut. The first parameter is the second key combination, the second the function that will be called when the key is pressed, the function has 5 parameters, the DTextEntry panel, its text, the caret position, the text before caret, and the text after caret, if you want to change the current text typed you need to return a string.

Example:

EasyChat.AddCTRLShortcut(KEY_BACKSLASH, function(panel, text, caret_pos, text_before, text_after) 
    return "" 
end)

The example above will remove everything from the DTextEntry if you press CTRL + BACKSLASH, for a panel to use the registered shortcuts you need to use EasyChat.UseRegisteredShortcuts. This function has 3 parameters, the panel you want to use it on (should be a DTextEntry or a child inheriting its properties), the last key that was pressed and the current key that is getting pressed e.g EasyChat.UseRegisteredShortcuts(panel,lastkey,currentkey)

Settings

You can register convars of your modules via EasyChat.RegisterConvar, it will create a specific checkbox in the settings tab in the Others category for the passed convar, you must also provide a description to go along the checkbox.

Example:

local cvar = CreateConVar("hello_world", "1", FCVAR_ARCHIVE, "Hello world!")
EasyChat.RegisterConvar(cvar, "Say hello world!")

For more control over the settings you can use the EasyChat.Settings API. For a complete working example on how to use it you can check this file.

local settings = EasyChat.Settings

local my_convar = GetConVar("my_convar")
local category_name = "My New Settings Category"

-- create our new settings category here
settings:AddCategory(category_name)

-- create a number setting based on a convar
-- category_name, setting_type, convar, setting_name, max_setting_value, min_setting_value
-- valid convar setting types are: number, string, boolean
settings:AddConvarSetting(category_name, "number", my_convar, "Setting Name", 10, 0)

-- creates a spacer
settings:AddSpacer(category_name)

-- creates an "action" setting (a button) and returns the newly created panel
local my_action_setting = settings:AddSetting(category_name, "action", "do something!")

-- set what the setting should do
my_action_setting.DoClick = function()
    call_my_stuff_here()
end

Chat HUD Tags

With the latest version of EasyChat you have been granted the ability to create fun and useful things with the chathud. Easychat's chathud has "tags" those tags can be created very easily like so:

local chathud = EasyChat.ChatHUD

local my_part = {
    Usable = true, -- Defines if players can use your part in the chat.
    OkInNicks = true, -- Defines if its okay to display this part when used in player nicknames.
}

-- str: The parameters passed by the player using the tag as a single string.
function my_part:Ctor(str)
    -- Initialize your part properties in there

    return self -- Never forget to return self in your constructor!
end

function my_part:ComputeSize()
    -- This is where you set the Size property.
    -- If you're not rendering anything with this part 
    -- you should probably not override this function.
    self.Size = { W = 0, H = 0 }
end

-- ctx: Drawing context, can be used to pass variables around, push matrices etc...
function my_part:Draw(ctx)
    -- This is where you should draw cool things, text, rectangles and whatnot.
    -- This is run within a HUDPaint hook (2d context).
end

-- str: A string, this is where you can modify strings before they're drawn or processed
function my_part:Normalize(str)
    -- You MUST return a string that corresponds to your changes here.
    -- If you dont change anything just return the passed string as is.
    -- Not returning a string will break the chathud.
    return str
end

function my_part:LineBreak()
    -- This is a very important function if you're rendering anything that has a size.
    -- It defines what behaviour the chathud should follow when your part is too big for 
    -- the current line.
end

-- line: The line at which your part is about to get pushed to.
-- last_index: The position of the last part of the line where your part is about to get pushed.
function my_part:PreLinePush(line, last_index)
    -- Anything you want to do before your part is pushed onto a line of the chathud.
end

function my_part:PostLinePush()
    -- Anything you want to do after your part is pushed onto a line of the chathud.
end

-- Let's register our part, so players can use it.
-- It would be as following: <coolpart=param1,param2>text
chathud:RegisterPart("coolpart", my_part)

More examples on how to create new tags (parts) can be found here.

Drawing using EasyChat markup

Cool, you can add tags to the chat hud but what if you want to draw that text like EasyChat does? Well its fairly easy you can use EasyChat markup exactly like gmod markup.

local markup = ec_markup.Parse("<hsv=t()*300>rainbow text")
hook.Add("HUDPaint", "rainbow_text", function()
    markup:Draw(500, 500)
end)

For example of advanced uses of the markup there are a few examples available:

Recurrent questions:

Q: I want to strip out all tags from a string, how can I do that?

A: You can use the markup to parse the string and then use :GetText() on the markup object.

local stripped_text = ec_markup.Parse("<hsv=t()*300> woah rainbow text"):GetText()
Clone this wiki locally