/
cl_util.lua
107 lines (89 loc) · 3.4 KB
/
cl_util.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
local plyMeta = FindMetaTable("Player")
--[[---------------------------------------------------------------------------
Show a black screen
---------------------------------------------------------------------------]]
local function blackScreen(um)
local toggle = um:ReadBool()
if toggle then
local black = color_black
local w, h = ScrW(), ScrH()
hook.Add("HUDPaintBackground", "BlackScreen", function()
surface.SetDrawColor(black)
surface.DrawRect(0, 0, w, h)
end)
else
hook.Remove("HUDPaintBackground", "BlackScreen")
end
end
usermessage.Hook("blackScreen", blackScreen)
--[[---------------------------------------------------------------------------
Wrap strings to not become wider than the given amount of pixels
---------------------------------------------------------------------------]]
local function charWrap(text, remainingWidth, maxWidth)
local totalWidth = 0
text = text:gsub(".", function(char)
totalWidth = totalWidth + surface.GetTextSize(char)
-- Wrap around when the max width is reached
if totalWidth >= remainingWidth then
-- totalWidth needs to include the character width because it's inserted in a new line
totalWidth = surface.GetTextSize(char)
remainingWidth = maxWidth
return "\n" .. char
end
return char
end)
return text, totalWidth
end
function DarkRP.textWrap(text, font, maxWidth)
local totalWidth = 0
surface.SetFont(font)
local spaceWidth = surface.GetTextSize(' ')
text = text:gsub("(%s?[%S]+)", function(word)
local char = string.sub(word, 1, 1)
if char == "\n" or char == "\t" then
totalWidth = 0
end
local wordlen = surface.GetTextSize(word)
totalWidth = totalWidth + wordlen
-- Wrap around when the max width is reached
if wordlen >= maxWidth then -- Split the word if the word is too big
local splitWord, splitPoint = charWrap(word, maxWidth - (totalWidth - wordlen), maxWidth)
totalWidth = splitPoint
return splitWord
elseif totalWidth < maxWidth then
return word
end
-- Split before the word
if char == ' ' then
totalWidth = wordlen - spaceWidth
return '\n' .. string.sub(word, 2)
end
totalWidth = wordlen
return '\n' .. word
end)
return text
end
--[[---------------------------------------------------------------------------
Decides whether a given player is in the same room as the local player
note: uses a heuristic
---------------------------------------------------------------------------]]
function plyMeta:isInRoom()
local tracedata = {}
tracedata.start = LocalPlayer():GetShootPos()
tracedata.endpos = self:GetShootPos()
local trace = util.TraceLine(tracedata)
return not trace.HitWorld
end
--[[---------------------------------------------------------------------------
Key name to key int mapping
---------------------------------------------------------------------------]]
local keyNames
function input.KeyNameToNumber(str)
if not keyNames then
keyNames = {}
for i = 1, 107, 1 do
keyNames[input.GetKeyName(i)] = i
end
end
return keyNames[str]
end