Skip to content

Commit a4bde7b

Browse files
committed
Scale entry font size with screen resolution
1 parent f8048b9 commit a4bde7b

File tree

1 file changed

+43
-10
lines changed

1 file changed

+43
-10
lines changed

lua/custom_chat/client/vgui/chat_frame.lua

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
surface.CreateFont( "CustomChatEntry", {
2+
font = "Roboto",
3+
extended = true,
4+
size = math.floor( ScrH() * 0.019 ),
5+
weight = 400,
6+
blursize = 0,
7+
scanlines = 0,
8+
antialias = true,
9+
underline = false,
10+
italic = false,
11+
strikeout = false,
12+
symbol = false,
13+
rotary = false,
14+
shadow = false,
15+
additive = false,
16+
outline = false
17+
} )
18+
119
local L = CustomChat.GetLanguageText
220
local PANEL = {}
321

@@ -38,10 +56,18 @@ function PANEL:Init()
3856

3957
self.entryDock.Paint = function( s, w, h )
4058
draw.RoundedBox( 0, 0, 0, w, h, s._backgroundColor )
59+
60+
if not s._calculatedFontHeight then
61+
surface.SetFont( "CustomChatEntry" )
62+
local _, textH = surface.GetTextSize( "A" )
63+
s._calculatedFontHeight = textH
64+
end
4165
end
4266

67+
self.entryDock._originalPerformLayout = self.entryDock.PerformLayout
68+
4369
self.entry = vgui.Create( "DTextEntry", self.entryDock )
44-
self.entry:SetFont( "ChatFont" )
70+
self.entry:SetFont( "CustomChatEntry" )
4571
self.entry:SetDrawBorder( false )
4672
self.entry:SetPaintBackground( false )
4773
self.entry:SetMaximumCharCount( CustomChat.MAX_MESSAGE_LENGTH )
@@ -55,18 +81,16 @@ function PANEL:Init()
5581
derma.SkinHook( "Paint", "TextEntry", s, w, h )
5682
end
5783

84+
self.entry.GetLineCount = function( s )
85+
local _, lineCount = string.gsub( s:GetText(), "\n", "\n" )
86+
return math.Clamp( lineCount + 1, 1, 5 )
87+
end
88+
5889
self.entry.OnChange = function( s )
5990
if not s.GetText then return end
6091

61-
local text = s:GetText() or ""
62-
63-
hook.Run( "ChatTextChanged", text )
64-
65-
local _, lineCount = string.gsub( text, "\n", "\n" )
66-
lineCount = math.Clamp( lineCount + 1, 1, 5 )
67-
68-
self.entryDock:SetTall( 20 * lineCount )
69-
self.entry._multilineMode = lineCount > 1
92+
hook.Run( "ChatTextChanged", s:GetText() or "" )
93+
self.entryDock:InvalidateLayout()
7094
end
7195

7296
self.entry.OnKeyCodeTyped = function( s, code )
@@ -103,15 +127,24 @@ function PANEL:Init()
103127
if code == KEY_UP then
104128
s.HistoryPos = s.HistoryPos - 1
105129
s:UpdateFromHistory()
130+
s:SetCaretPos( 0 )
106131
end
107132

108133
if code == KEY_DOWN then
109134
s.HistoryPos = s.HistoryPos + 1
110135
s:UpdateFromHistory()
136+
s:SetCaretPos( 0 )
111137
end
112138
end
113139
end
114140

141+
self.entryDock.PerformLayout = function( s )
142+
local lineCount = self.entry:GetLineCount()
143+
144+
self.entry._multilineMode = lineCount > 1
145+
s:SetTall( lineCount * ( s._calculatedFontHeight or 20 ) * 1.3 )
146+
end
147+
115148
local emojisButton = vgui.Create( "DImageButton", self.entryDock )
116149
emojisButton:SetImage( "icon16/emoticon_smile.png" )
117150
emojisButton:SetStretchToFit( false )

0 commit comments

Comments
 (0)