Skip to content

Commit 6ab1c9a

Browse files
committed
Improve some of the textdraw code, improve GameTextForPlayer, fix a small bug with RemoveBuildingForPlayer, apparently it's supposed to remove models around the radius, not all over the map
1 parent 6173e73 commit 6ab1c9a

File tree

3 files changed

+178
-68
lines changed

3 files changed

+178
-68
lines changed

amx/client/client.lua

Lines changed: 126 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -403,8 +403,8 @@ end
403403
-- Player objects
404404
function RemoveBuildingForPlayer(model, x, y, z, radius)
405405
if model == -1 then
406-
for i=550,20000 do --Remove all world models if they sent -1
407-
removeWorldModel(i,20000,0,0,0)
406+
for i=550,20000 do --Remove all world models around radius if they sent -1
407+
removeWorldModel(i, radius, x, y, z)
408408
end
409409
return true --Don't run the rest of the code
410410
end
@@ -790,26 +790,64 @@ local textDrawColorMapping = {
790790
}
791791

792792
local textDrawFonts = {
793-
[0] = { font = 'beckett', lsizemul = 3 }, -- TextDraw letter size -> dxDrawText scale multiplier
794-
[1] = { font = 'default-bold', lsizemul = 3 },
795-
[2] = { font = 'bankgothic', lsizemul = 2 },
796-
[3] = { font = 'default-bold', lsizemul = 3 }
793+
[0] = { font = 'beckett', lsizemul = 1.25 }, -- TextDraw letter size -> dxDrawText scale multiplier
794+
[1] = { font = 'default-bold', lsizemul = 1.25 },
795+
[2] = { font = 'bankgothic', lsizemul = 1.5 },
796+
[3] = { font = 'default-bold', lsizemul = 1.25 }
797797
}
798798

799+
function visibleTextDrawsExist()
800+
for name,amx in pairs(g_AMXs) do
801+
if table.find(amx.textdraws, 'visible', true) then
802+
return true
803+
end
804+
end
805+
return false
806+
end
807+
808+
function showTextDraw(textdraw)
809+
if not visibleTextDrawsExist() then
810+
addEventHandler('onClientRender', root, renderTextDraws)
811+
end
812+
textdraw.visible = true
813+
end
814+
815+
function hideTextDraw(textdraw)
816+
textdraw.visible = false
817+
if not visibleTextDrawsExist() then
818+
removeEventHandler('onClientRender', root, renderTextDraws)
819+
end
820+
end
821+
822+
function hudGetVerticalScale()
823+
return 0.002232143
824+
end
825+
826+
function hudGetHorizontalScale()
827+
return 0.0015625
828+
end
829+
799830
function initTextDraw(textdraw)
800831
local amx = textdraw.amx
801832
textdraw.id = textdraw.id or (#amx.textdraws + 1)
802833
amx.textdraws[textdraw.id] = textdraw
803834

804-
local lineHeight = 60*(textdraw.lsize or 0.5)
835+
local scale = (textdraw.lwidth or 0.5)
836+
local tWidth, tHeight = dxGetTextSize(textdraw.text, scale)
837+
local lineHeight = (tHeight or 0.25) / 2 --space between lines (vertical) also used to calculate size of the box if any
838+
local lineWidth = (textdraw.lwidth or 0.25) --space between words (horizontal)
805839

840+
--Set the height based on the text size
841+
textdraw.theight = tHeight
842+
textdraw.twidth = tWidth
843+
806844
local text = textdraw.text:gsub('~k~~(.-)~', getSAMPBoundKey)
807845
local lines = {}
808846
local pos, stop, c
809847
stop = 0
810848
while true do
811849
pos, stop, c = text:find('~(%a)~', stop + 1)
812-
if c == 'n' then
850+
if c == 'n' then --If we found a new line
813851
lines[#lines + 1] = text:sub(1, pos - 1)
814852
text = text:sub(stop + 1)
815853
stop = 0
@@ -825,12 +863,12 @@ function initTextDraw(textdraw)
825863
textdraw.parts = {}
826864
textdraw.width = 0
827865
local font = textDrawFonts[textdraw.font and textdraw.font >= 0 and textdraw.font <= #textDrawFonts and textdraw.font or 0]
828-
local scale = (textdraw.lsize or 0.5) * font.lsizemul
829866
font = font.font
830867

831-
local curX
832-
local curY = textdraw.y or screenHeight/2 - #lines*lineHeight/2
868+
local TDXPos = textdraw.x or 640 - #lines*lineWidth
869+
local TDYPos = textdraw.y or 448 - #lines*lineHeight
833870

871+
--Process the lines we previously found
834872
for i,line in ipairs(lines) do
835873
local colorpos = 1
836874
local color
@@ -871,13 +909,14 @@ function initTextDraw(textdraw)
871909
textdraw.width = math.max(textdraw.width, textWidth)
872910
if textdraw.align == 1 then
873911
-- left
874-
curX = textdraw.x
912+
TDXPos = textdraw.x
875913
elseif textdraw.align == 2 or not textdraw.align then
876914
-- center
877-
curX = screenWidth/2 - textWidth/2
915+
--outputConsole(string.format("Got centered text %d %d %s", TDXPos, TDYPos, textdraw.text))
916+
TDXPos = 640/2 - textWidth/2
878917
elseif textdraw.align == 3 then
879918
-- right
880-
curX = textdraw.x - textWidth
919+
TDXPos = textdraw.x - textWidth
881920
end
882921

883922
color = textdraw.color or tocolor(255, 255, 255)
@@ -890,65 +929,61 @@ function initTextDraw(textdraw)
890929
colorpos = colorpos + 7
891930
end
892931
nextcolorpos = line:find('#%x%x%x%x%x%x', colorpos) or line:len() + 1
893-
local part = { text = line:sub(colorpos, nextcolorpos - 1), x = curX, y = curY, color = color }
932+
local part = { text = line:sub(colorpos, nextcolorpos - 1), x = TDXPos, y = TDYPos, color = color }
894933
table.insert(textdraw.parts, part)
895-
curX = curX + dxGetTextWidth(part.text, scale, font)
934+
TDXPos = TDXPos + dxGetTextWidth(part.text, scale, font)
896935
colorpos = nextcolorpos
897936
end
898-
curY = curY + lineHeight
899-
end
900-
textdraw.absheight = lineHeight*#lines
901-
end
902-
903-
function visibleTextDrawsExist()
904-
for name,amx in pairs(g_AMXs) do
905-
if table.find(amx.textdraws, 'visible', true) then
906-
return true
907-
end
908-
end
909-
return false
910-
end
911-
912-
function showTextDraw(textdraw)
913-
if not visibleTextDrawsExist() then
914-
addEventHandler('onClientRender', root, renderTextDraws)
915-
end
916-
textdraw.visible = true
917-
end
918-
919-
function hideTextDraw(textdraw)
920-
textdraw.visible = false
921-
if not visibleTextDrawsExist() then
922-
removeEventHandler('onClientRender', root, renderTextDraws)
937+
TDYPos = TDYPos + lineHeight
923938
end
939+
textdraw.absheight = tHeight*#lines
924940
end
925941

926942
function renderTextDraws()
927943
for name,amx in pairs(g_AMXs) do
928944
for id,textdraw in pairs(amx.textdraws) do
929-
if textdraw.visible and textdraw.parts and not (textdraw.text:match('^%s*$') and not textdraw.usebox) then
945+
if textdraw.visible and textdraw.parts and not (textdraw.text:match('^%s*$')) then-- and not textdraw.usebox) then
930946
local font = textDrawFonts[textdraw.font and textdraw.font >= 0 and textdraw.font <= #textDrawFonts and textdraw.font or 0]
931-
local scale = (textdraw.lsize or 0.5) * font.lsizemul
947+
if textdraw.upscalex == nil then
948+
textdraw.upscalex = 1.0
949+
end
950+
if textdraw.upscaley == nil then
951+
textdraw.upscaley = 1.0
952+
end
953+
954+
local letterHeight = (textdraw.lheight * textdraw.upscaley or 0.25)
955+
local letterWidth = (textdraw.lwidth * textdraw.upscalex or 0.5)
956+
957+
local vertHudScale = hudGetVerticalScale()
958+
local horHudScale = hudGetHorizontalScale()
959+
960+
local scaley = SCREEN_SCALE_Y(screenHeight * vertHudScale * letterHeight * 0.175) --This should replicate what the game does
961+
local scalex = SCREEN_SCALE_X(screenWidth * horHudScale * letterWidth * 0.35)
962+
963+
local sourceY = screenHeight - ((DEFAULT_SCREEN_HEIGHT - textdraw.y) * (screenHeight * vertHudScale))
964+
local sourceX = screenWidth - ((DEFAULT_SCREEN_WIDTH - textdraw.x) * (screenWidth * horHudScale))
965+
932966
font = font.font
967+
--Process box alignments
933968
if textdraw.usebox then
934969
local boxcolor = textdraw.boxcolor or tocolor(0, 0, 0, 120*(textdraw.alpha or 1))
935970
local x, y, w, h
936-
if textdraw.align == 1 then
971+
if textdraw.align == 1 then --left
937972
x = textdraw.x
938973
if textdraw.boxsize then
939-
w = textdraw.boxsize[1] - x
974+
w = textdraw.boxsize[1]-- - x
940975
else
941976
w = textdraw.width
942977
end
943-
elseif textdraw.align == 2 then
978+
elseif textdraw.align == 2 then --centered
944979
if textdraw.boxsize then
945-
x = screenWidth/2 - textdraw.boxsize[1]/2
980+
x = textdraw.boxsize[1]/2
946981
w = textdraw.boxsize[1]
947982
else
948983
x = textdraw.x
949984
w = textdraw.width
950985
end
951-
elseif textdraw.align == 3 then
986+
elseif textdraw.align == 3 then --right
952987
if textdraw.boxsize then
953988
w = textdraw.x - textdraw.boxsize[1]
954989
else
@@ -957,21 +992,33 @@ function renderTextDraws()
957992
x = textdraw.x - w
958993
end
959994
y = textdraw.y
995+
996+
--Calculates box height
960997
if textdraw.boxsize and textdraw.text:match('^%s*$') then
961998
h = textdraw.boxsize[2]
962999
else
9631000
h = textdraw.absheight
9641001
end
965-
dxDrawRectangle(x - 3, y - 3, w + 6, h + 6, boxcolor)
1002+
1003+
dxDrawRectangle(sourceX, sourceY, w * getAspectRatio(), h * getAspectRatio(), boxcolor)
1004+
--outputConsole(string.format("Drawing textdraw box: sourceX: %f, sourceY: %f", sourceX, sourceY))
9661005
end
1006+
9671007
for i,part in pairs(textdraw.parts) do
968-
if textdraw.shade and textdraw.shade > 0 then
969-
dxDrawText(part.text, part.x + 5, part.y + 5, part.x + 5, part.y + 5, tocolor(0, 0, 0, 100*(textdraw.alpha or 1)), scale, font)
1008+
1009+
sourceY = screenHeight - ((DEFAULT_SCREEN_HEIGHT - part.y) * (screenHeight * vertHudScale))
1010+
sourceX = screenWidth - ((DEFAULT_SCREEN_WIDTH - part.x) * (screenWidth * horHudScale))
1011+
1012+
--outputConsole(string.format("text: %s partx: %f, party: %f sourceX: %f, sourceY: %f", part.text, part.x, part.y, sourceX, sourceY))
1013+
1014+
if textdraw.shade and textdraw.shade > 0 then --Draw the shadow
1015+
dxDrawText(part.text, sourceX + 5, sourceY + 5, sourceX + 5, sourceY + 5, tocolor(0, 0, 0, 100*(textdraw.alpha or 1)), scalex, scaley, font)
9701016
end
1017+
--Draw the actual text
9711018
drawBorderText(
972-
part.text, part.x, part.y,
1019+
part.text, sourceX, sourceY,
9731020
textdraw.alpha and setcoloralpha(part.color, math.floor(textdraw.alpha*255)) or part.color,
974-
scale, font, math.ceil((textdraw.outlinesize or (font == 'bankgothic' and 2 or 1))*scale),
1021+
scalex, scaley, font, textdraw.outlinesize,
9751022
textdraw.outlinecolor
9761023
)
9771024
end
@@ -996,12 +1043,29 @@ function GameTextForPlayer(amxName, text, time, style)
9961043
end
9971044
local amx = g_AMXs[amxName]
9981045
gameText = { amx = amx, text = text, font = 2 }
999-
if style == 2 then
1000-
gameText.x = 0.9*screenWidth
1001-
gameText.y = 0.7*screenHeight
1046+
if style == 1 then
1047+
gameText.x = 0.9 * 640
1048+
gameText.y = 0.8 * 448
1049+
gameText.lheight = 0.5
1050+
gameText.lwidth = 1.0
1051+
gameText.align = 3
1052+
gameText.upscaley = 3.0
1053+
gameText.upscalex = 1.0
1054+
time = 8000 --Fades out after 8 seconds regardless of time set according to the wiki
1055+
elseif style == 2 then
1056+
gameText.x = 0.9 * 640
1057+
gameText.y = 0.7 * 448
10021058
gameText.align = 3
1003-
elseif style == 6 then
1004-
gameText.y = 0.2*screenHeight
1059+
elseif style >= 3 then
1060+
--
1061+
-- GTA replaces these with stars
1062+
gameText.text = string.gsub(text, "]", "")
1063+
gameText.x = 0.5 * 640
1064+
gameText.y = 0.2 * 448
1065+
gameText.lheight = 0.5
1066+
gameText.lwidth = 1.0
1067+
gameText.align = 2
1068+
gameText.upscaley = 2.5
10051069
end
10061070
initTextDraw(gameText)
10071071
showTextDraw(gameText)
@@ -1103,8 +1167,8 @@ function TextDrawCreate(amxName, id, textdraw)
11031167
textdraw.id = id
11041168
amx.textdraws[id] = textdraw
11051169
if textdraw.x then
1106-
textdraw.x = textdraw.x*screenWidth
1107-
textdraw.y = textdraw.y*screenHeight
1170+
textdraw.x = textdraw.x
1171+
textdraw.y = textdraw.y
11081172
end
11091173
for prop,val in pairs(textdraw) do
11101174
TextDrawPropertyChanged(amxName, id, prop, val, true)
@@ -1124,8 +1188,8 @@ function TextDrawPropertyChanged(amxName, id, prop, newval, skipInit)
11241188
local textdraw = g_AMXs[amxName].textdraws[id]
11251189
textdraw[prop] = newval
11261190
if prop == 'boxsize' then
1127-
textdraw.boxsize[1] = textdraw.boxsize[1]*screenWidth
1128-
textdraw.boxsize[2] = textdraw.boxsize[2]*screenHeight
1191+
textdraw.boxsize[1] = textdraw.boxsize[1]
1192+
textdraw.boxsize[2] = textdraw.boxsize[2]
11291193
elseif prop:match('color') then
11301194
textdraw[prop] = tocolor(unpack(newval))
11311195
end

amx/client/util.lua

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,19 @@ function serverAMXEvent(eventName, ...)
6666
server.procCallOnAll(eventName, ...)
6767
end
6868

69-
function drawBorderText(text, x, y, color, scale, font, outlinesize, outlinecolor)
69+
function drawBorderText(text, x, y, color, scalex, scaley, font, outlinesize, outlinecolor)
7070
local alpha = math.floor(color / 16777216)
7171
outlinesize = outlinesize or 2
7272
if outlinesize > 0 then
7373
for offsetX=-outlinesize,outlinesize,outlinesize do
7474
for offsetY=-outlinesize,outlinesize,outlinesize do
7575
if not (offsetX == 0 and offsetY == 0) then
76-
dxDrawText(text, x + offsetX, y + offsetY, x + offsetX, y + offsetY, outlinecolor or tocolor(0, 0, 0, alpha), scale, font)
76+
dxDrawText(text, x + offsetX, y + offsetY, x + offsetX, y + offsetY, outlinecolor or tocolor(0, 0, 0, alpha), scalex, scaley, font)
7777
end
7878
end
7979
end
8080
end
81-
dxDrawText(text, x, y, x, y, color, scale, font)
81+
dxDrawText(text, x, y, x, y, color, scalex, scaley, font)
8282
end
8383

8484
function drawShadowText(text, x, y, color, scale, font, shadowDist, width, align)
@@ -278,3 +278,47 @@ end
278278
function fromcolor(color)
279279
return math.floor(color / 65536) % 255, math.floor(color / 255) % 255, color % 255, math.floor(color / 16777216)
280280
end
281+
282+
--From: https://github.com/GTAmodding/re3/blob/408f47fc9d85e930f2dc1a4cc9f50b3c0d4c60b8/src/core/common.h
283+
DEFAULT_SCREEN_WIDTH = 640.0
284+
DEFAULT_SCREEN_HEIGHT = 448.0
285+
DEFAULT_ASPECT_RATIO = 4.0/3.0
286+
DEFAULT_VIEWWINDOW = 0.7
287+
288+
local USCREEN_WIDTH, USCREEN_HEIGHT = guiGetScreenSize( )
289+
290+
function SCREEN_ASPECT_RATIO(a)
291+
return ((a) * USCREEN_WIDTH / DEFAULT_SCREEN_WIDTH)
292+
end
293+
--This scales from PS2 pixel coordinates to the real resolution
294+
function SCREEN_STRETCH_X(a)
295+
return ((a) * USCREEN_WIDTH / DEFAULT_SCREEN_WIDTH)
296+
end
297+
function SCREEN_STRETCH_Y(a)
298+
return ((a) * USCREEN_HEIGHT / DEFAULT_SCREEN_HEIGHT)
299+
end
300+
function SCREEN_STRETCH_FROM_RIGHT(a)
301+
return (USCREEN_WIDTH - SCREEN_STRETCH_X(a))
302+
end
303+
function SCREEN_STRETCH_FROM_BOTTOM(a)
304+
return (USCREEN_HEIGHT - SCREEN_STRETCH_Y(a))
305+
end
306+
--This scales from PS2 pixel coordinates while optionally maintaining the aspect ratio
307+
function SCREEN_SCALE_X(a)
308+
return SCREEN_SCALE_AR(SCREEN_STRETCH_X(a))
309+
end
310+
function SCREEN_SCALE_Y(a)
311+
return SCREEN_STRETCH_Y(a)
312+
end
313+
function SCREEN_SCALE_FROM_RIGHT(a)
314+
return USCREEN_WIDTH - SCREEN_SCALE_X(a)
315+
end
316+
function SCREEN_SCALE_FROM_BOTTOM(a)
317+
return USCREEN_HEIGHT - SCREEN_SCALE_Y(a)
318+
end
319+
function getAspectRatio()
320+
return USCREEN_WIDTH/USCREEN_HEIGHT
321+
end
322+
function SCREEN_SCALE_AR(a)
323+
return ((a) * DEFAULT_ASPECT_RATIO / getAspectRatio())
324+
end

0 commit comments

Comments
 (0)