Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 28 additions & 69 deletions [gameplay]/killmessages/utils/textlib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,28 @@ local g_screenX,g_screenY = guiGetScreenSize()
local visibleText = {}
------
local defaults = {
fX = 0.5,
fY = 0.5,
bRelativePosition = true,
fX = 0.5,
fY = 0.5,
bRelativePosition = true,
strText = "",
bVerticalAlign = "center",
bHorizontalAlign = "center",
bVerticalAlign = "center",
bHorizontalAlign = "center",
tColor = {255,255,255,255},
fScale = 1,
strFont = "default",
strType = "normal",
tAttributes = {},
bPostGUI = false,
bColorCoded = true,
bClip = false,
bWordWrap = true,
bVisible = true,
tBoundingBox = false, --If a bounding box is not set, it will not be used.
bRelativeBoundingBox = true,
tBoundingBox = false, --If a bounding box is not set, it will not be used.
bRelativeBoundingBox = true,
}

local validFonts = {
default = true,
["default-bold"] = true,
["default-bold"] = true,
clear = true,
arial = true,
pricedown = true,
Expand All @@ -49,19 +48,19 @@ local validAlignTypes = {
right = true,
}

function dxText:create(text, x, y, relative)
function dxText:create( text, x, y, relative )
assert(not self.fX, "attempt to call method 'create' (a nil value)")
if type(text) ~= "string" or not tonumber(x) or not tonumber(y) then
outputDebugString("dxText:create - Bad argument",0,112,112,112)
if ( type(text) ~= "string" ) or ( not tonumber(x) ) or ( not tonumber(y) ) then
outputDebugString ( "dxText:create - Bad argument", 0, 112, 112, 112 )
return false
end
local new = {}
setmetatable(new, dxText_mt)
setmetatable( new, dxText_mt )
--Add default settings
for i,v in pairs(defaults) do
new[i] = v
end
idAssign = idAssign+1
idAssign = idAssign + 1
new.id = idPrefix..idAssign
new.strText = text or new.strText
new.fX = x or new.fX
Expand All @@ -80,7 +79,7 @@ function dxText:text(text)
end

function dxText:position(x,y,relative)
if not tonumber(x) then return self.fX,self.fY end
if not tonumber(x) then return self.fX, self.fY end
self.fX = x
self.fY = y
if type(relative) == "boolean" then
Expand All @@ -96,7 +95,7 @@ function dxText:color(r,g,b,a)
g = g or self.tColor[2]
b = b or self.tColor[3]
a = a or self.tColor[4]
self.tColor = {r,g,b,a}
self.tColor = { r,g,b,a }
return true
end

Expand Down Expand Up @@ -124,20 +123,15 @@ function dxText:destroy()
end

function dxText:extent()
if self.bColorCoded then
text = string.gsub(self.strText,"#%x%x%x%x%x%x","")
else
text = self.strText
end
local extent = dxGetTextWidth(text,self.fScale,self.strFont)
local extent = dxGetTextWidth ( self.strText, self.fScale, self.strFont )
if self.strType == "stroke" or self.strType == "border" then
extent = extent + self.tAttributes[1]
end
return extent
end

function dxText:height()
local height = dxGetFontHeight(self.fScale, self.strFont)
local height = dxGetFontHeight ( self.fScale, self.strFont )
if self.strType == "stroke" or self.strType == "border" then
height = height + self.tAttributes[1]
end
Expand Down Expand Up @@ -169,16 +163,16 @@ function dxText:wordWrap(bool)
end

function dxText:type(type,...)
if not validTypes[type] then return self.strType,unpack(self.tAttributes) end
if not validTypes[type] then return self.strType, unpack(self.tAttributes) end
self.strType = type
self.tAttributes = {...}
return true
end

function dxText:align(horzA, vertA)
if not validAlignTypes[horzA] then return self.bHorizontalAlign,self.bVerticalAlign end
if not validAlignTypes[horzA] then return self.bHorizontalAlign, self.bVerticalAlign end
vertA = vertA or self.bVerticalAlign
self.bHorizontalAlign,self.bVerticalAlign = horzA,vertA
self.bHorizontalAlign, self.bVerticalAlign = horzA, vertA
return true
end

Expand All @@ -202,42 +196,7 @@ function dxText:boundingBox(left,top,right,bottom,relative)
return true
end

function dxCreateCustomDX(text,left,top,right,bottom,color,scale,font,alignX,alignY,clip,wordBreak,postGUI)
if not text or not left or not top then
return false
end
local xOffset = nil
local fpat = "(.-)#(%x%x%x%x%x%x)"
local s,e,cap,col = text:find(fpat,1)
local last_end = 1
if not alignX then
alignX = "left"
elseif alignX == "center" then
xOffset = -(dxGetTextWidth(string.gsub(text,"#%x%x%x%x%x%x",""),scale,font)/2)
else
xOffset = 0
end
while s do
if cap == "" and col then
color = tocolor(tonumber("0x"..col:sub(1,2)), tonumber("0x"..col:sub(3,4)), tonumber("0x"..col:sub(5,6)),255)
end
if s ~= 1 or cap ~= "" then
dxDrawText(cap,left+xOffset,top,left+xOffset,bottom,color,scale,font,alignX,alignY,false,false,postGUI)
xOffset = xOffset+dxGetTextWidth(cap,scale,font)
color = tocolor(tonumber("0x"..col:sub(1,2)), tonumber("0x"..col:sub(3,4)), tonumber("0x"..col:sub(5,6)),255)
end
last_end = e+1
s,e,cap,col = text:find(fpat,last_end)
end
if last_end <= #text then
cap = text:sub(last_end)
dxDrawText(cap,left+xOffset,top,left+xOffset,bottom,color,scale,font,alignX,alignY,false,false,postGUI)
xOffset = xOffset+dxGetTextWidth(cap,scale,font)
end
return true
end

addEventHandler("onClientRender",getRootElement(),
addEventHandler ( "onClientRender", getRootElement(),
function()
for self,_ in pairs(visibleText) do
while true do
Expand Down Expand Up @@ -296,8 +255,8 @@ addEventHandler("onClientRender",getRootElement(),
if outlinesize > 0 then
for offsetX=-outlinesize,outlinesize,outlinesize do
for offsetY=-outlinesize,outlinesize,outlinesize do
if not offsetX == 0 and offsetY == 0 then
dxCreateCustomDX(self.strText,l+offsetX,t+offsetY,r+offsetX,b+offsetY,tocolor(att2,att3,att4,att5),self.fScale,self.strFont,self.bHorizontalAlign,self.bVerticalAlign,self.bClip,self.bWordWrap,self.bPostGUI,self.bColorCoded)
if not (offsetX == 0 and offsetY == 0) then
dxDrawText(self.strText, l + offsetX, t + offsetY, r + offsetX, b + offsetY, tocolor(att2, att3, att4, att5), self.fScale, self.strFont, self.bHorizontalAlign, self.bVerticalAlign, self.bClip, self.bWordWrap, self.bPostGUI )
end
end
end
Expand All @@ -308,19 +267,19 @@ addEventHandler("onClientRender",getRootElement(),
att3 = att3 or 0
att4 = att4 or 0
att5 = att5 or self.tColor[4]
dxCreateCustomDX(self.strText,l+shadowDist,t+shadowDist,r+shadowDist,b+shadowDist,tocolor(att2,att3,att4,att5),self.fScale,self.strFont,self.bHorizontalAlign,self.bVerticalAlign,self.bClip,self.bWordWrap,self.bPostGUI,self.bColorCoded)
dxDrawText(self.strText, l + shadowDist, t + shadowDist, r + shadowDist, b + shadowDist, tocolor(att2, att3, att4, att5), self.fScale, self.strFont, self.bHorizontalAlign, self.bVerticalAlign, self.bClip, self.bWordWrap, self.bPostGUI )
end
dxCreateCustomDX(self.strText,l,t,r,b,tocolor(unpack(self.tColor)),self.fScale,self.strFont,self.bHorizontalAlign,self.bVerticalAlign,self.bClip,self.bWordWrap,self.bPostGUI,self.bColorCoded)
dxDrawText ( self.strText, l, t, r, b, tocolor(unpack(self.tColor)), self.fScale, self.strFont, self.bHorizontalAlign, self.bVerticalAlign, self.bClip, self.bWordWrap, self.bPostGUI )
break
end
end
end
)

if addEvent("updateDisplays",true) then
addEventHandler("updateDisplays",getRootElement(),
if addEvent ( "updateDisplays", true ) then
addEventHandler ( "updateDisplays", getRootElement(),
function(self)
setmetatable(self,dxText_mt)
setmetatable( self, dxText_mt )
--Remove any old ones with the same id
for text,_ in pairs(visibleText) do
if text.id == self.id then
Expand Down