Skip to content

Commit

Permalink
Text: Added text and nickname limiting (#1498)
Browse files Browse the repository at this point in the history
Adds the following two functions to the game:

```lua
---
-- Creates a text that is limited to the provided length. Adds a limiting char (e.g. '...') after the
-- text if so desired. The limiting char is only added to the string (and also only then considered
-- for the length) if the text without the limiting char is too long for the provided width.
-- @param string text The text that may be limited in length
-- @param number width The maximum width that should be used to limit the text
-- @param[default="DefaultBold"] string font The font ID
-- @param[opt] string limitChar The limiting character(s) that might be appended to the end
-- @param[default=1.0] number scale The UI scale factor
-- @return string The length limited text
-- @realm client
function draw.GetLimitedLengthText(text, width, font, limitChar, scale)
```

```lua
---
-- Creates an elliptic nick for a given length.
-- @param number width The maximum width that should be used to limit the nick
-- @param[default="DefaultBold"] string font The font ID
-- @param[default=1.0] number scale The UI scale factor
-- @return string The length limited nick
-- @realm client
function plymeta:NickElliptic(width, font, scale)
```

These can be used to limit text / nickname to a given maximum length,
while also adding a limiting character.

**Example:**

```lua
print(LocalPlayer():NickElliptic(50, "DefaultBold", 1.0))

>>> "Tim | M..."
```

---------

Co-authored-by: Histalek <16392835+Histalek@users.noreply.github.com>
  • Loading branch information
TimGoll and Histalek authored Apr 7, 2024
1 parent d4df150 commit 79b7b72
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ All notable changes to TTT2 will be documented here. Inspired by [keep a changel
- Added `TTT2CanTakeCredits` hook for overriding whether a player is allowed to take credits from a given corpse. (by @Spanospy)
- Disabled locational voice during the preparing phase by default
- Added a ConVar `ttt_locational_voice_prep` to reenable it
- Added Text / Nickname length limiting (by @TimGoll)
- Added `ttt_locational_voice_range` to set a cut-off radius for the locational voice chat range

### Changed
Expand Down
11 changes: 11 additions & 0 deletions gamemodes/terrortown/gamemode/client/cl_player_ext.lua
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,17 @@ function plymeta:AnimUpdateGesture()
end
end

---
-- Creates an elliptic nick for a given length.
-- @param number width The maximum width that should be used to limit the nick
-- @param[default="DefaultBold"] string font The font ID
-- @param[default=1.0] number scale The UI scale factor
-- @return string The length limited nick
-- @realm client
function plymeta:NickElliptic(width, font, scale)
return draw.GetLimitedLengthText(self:Nick(), width, font, "...", scale)
end

---
-- @hook
-- @param Player ply The player to update the animation info for.
Expand Down
31 changes: 31 additions & 0 deletions lua/ttt2/extensions/draw.lua
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,37 @@ function draw.GetTextSize(text, font, scale)
return w * scale, h * scale
end

---
-- Creates a text that is limited to the provided length. Adds a limiting char (e.g. '...') after the
-- text if so desired. The limiting char is only added to the string (and also only then considered
-- for the length) if the text without the limiting char is too long for the provided width.
-- @param string text The text that may be limited in length
-- @param number width The maximum width that should be used to limit the text
-- @param[default="DefaultBold"] string font The font ID
-- @param[opt] string limitChar The limiting character(s) that might be appended to the end
-- @param[default=1.0] number scale The UI scale factor
-- @return string The length limited text
-- @realm client
function draw.GetLimitedLengthText(text, width, font, limitChar, scale)
scale = scale or 1.0
limitChar = limitChar or ""

local widthText = draw.GetTextSize(text, font, scale)

if widthText <= width then
return text
end

if limitChar ~= "" then
width = width - draw.GetTextSize(limitChar, font, scale)
end

-- we use this function here that splits the text in multiple lines
local lines = InternalSplitLongWord(text, width, widthText)

return lines[1] .. limitChar
end

local cachedArcs = {}

-- Generates an arc out of triangles that is cached in a table to reduce rendering time
Expand Down

0 comments on commit 79b7b72

Please sign in to comment.