Jump to content

dxDrawColorText & dxCreateFont Problem


Recommended Posts

Hello , I want to change font in killmessages and change dxDrawText to dxDrawColorText, but when I change the font, it doesn't shows any text.. When I change dxDrawText to dxDrawColorText , text change position.. It's fragment of code.

local myFont = dxCreateFont("font.otf", 14) 
addEventHandler ( "onClientRender", getRootElement(), 
    function() 
        for self,_ in pairs(visibleText) do 
            while true do 
                if self.bDestroyed then 
                    visibleText[self] = nil 
                    break 
                end 
                local l,t,r,b 
                --If we arent using a bounding box 
                if not self.tBoundingBox then 
                    --Decide if we use relative or absolute 
                    local p_screenX,p_screenY = 1,1 
                    if self.bRelativePosition then 
                        p_screenX,p_screenY = g_screenX,g_screenY 
                    end 
                    local fX,fY = (self.fX)*p_screenX,(self.fY)*p_screenY 
                    if self.bHorizontalAlign == "left" then 
                        l = fX 
                        r = fX + g_screenX 
                    elseif self.bHorizontalAlign == "right" then 
                        l = fX - g_screenX 
                        r = fX 
                    else 
                        l = fX - g_screenX 
                        r = fX + g_screenX 
                    end 
                    if self.bVerticalAlign == "top" then 
                        t = fY 
                        b = fY + g_screenY 
                    elseif self.bVerticalAlign == "bottom" then 
                        t = fY - g_screenY 
                        b = fY 
                    else 
                        t = fY - g_screenY 
                        b = fY + g_screenY 
                    end 
                elseif type(self.tBoundingBox) == "table" then 
                    local b_screenX,b_screenY = 1,1 
                    if self.bRelativeBoundingBox then 
                        b_screenX,b_screenY = g_screenX,g_screenY 
                    end 
                    l,t,r,b = self.tBoundingBox[1],self.tBoundingBox[2],self.tBoundingBox[3],self.tBoundingBox[4] 
                    l = l*b_screenX 
                    t = t*b_screenY 
                    r = r*b_screenX 
                    b = b*b_screenY 
                end 
                local type,att1,att2,att3,att4,att5 = self:type() 
                if type == "border" or type == "stroke" then 
                    att2 = att2 or 0 
                    att3 = att3 or 0 
                    att4 = att4 or 0 
                    att5 = att5 or self.tColor[4] 
                    outlinesize = att1 or 2 
                    outlinesize = math.min(self.fScale,outlinesize) --Make sure the outline size isnt thicker than the size of the label 
                    if outlinesize > 0 then 
                        for offsetX=-outlinesize,outlinesize,outlinesize do 
                            for offsetY=-outlinesize,outlinesize,outlinesize do 
                                if not (offsetX == 0 and offsetY == 0) then 
                                    dxDrawColorText(self.strText, l + offsetX, t + offsetY, r + offsetX, b + offsetY, tocolor(att2, att3, att4, att5), self.fScale, myFont, self.bHorizontalAlign, self.bVerticalAlign, self.bClip, self.bWordWrap, self.bPostGUI ) 
                                end 
                            end 
                        end 
                    end 
                elseif type == "shadow" then 
                    local shadowDist = att1 
                    att2 = att2 or 0 
                    att3 = att3 or 0 
                    att4 = att4 or 0 
                    att5 = att5 or self.tColor[4] 
                    string.gsub(self.strText, '#%x%x%x%x%x%x', '') 
                    dxDrawColorText(self.strText, l + shadowDist, t + shadowDist, r + shadowDist, b + shadowDist, tocolor(att2, att3, att4, att5), self.fScale, myFont, self.bHorizontalAlign, self.bVerticalAlign, self.bClip, self.bWordWrap, self.bPostGUI ) 
                end 
                dxDrawColorText ( self.strText, l, t, r, b, tocolor(unpack(self.tColor)), self.fScale, myFont, self.bHorizontalAlign, self.bVerticalAlign, self.bClip, self.bWordWrap, self.bPostGUI ) 
                break 
            end 
        end 
    end 
) 
  
if addEvent ( "updateDisplays", true ) then 
    addEventHandler ( "updateDisplays", getRootElement(), 
        function(self) 
            setmetatable( self, dxText_mt ) 
            --Remove any old ones with the same id 
            for text,_ in pairs(visibleText) do 
                if text.id == self.id then 
                    visibleText[text] = nil 
                end 
            end 
            if self.bVisible and not self.bDestroyed then 
                visibleText[self] = true 
            end 
        end 
    ) 
end 
  
function dxDrawColorText(str, ax, ay, bx, by, color, scale, font, alignX, alignY) 
  bx, by, color, scale, font = bx or ax, by or ay, color or tocolor(255,255,255,255), scale or 1, font or "default" 
  if alignX then 
    if alignX == "center" then 
      ax = ax + (bx - ax - dxGetTextWidth(str:gsub("#%x%x%x%x%x%x",""), scale, font))/2 
    elseif alignX == "right" then 
      ax = bx - dxGetTextWidth(str:gsub("#%x%x%x%x%x%x",""), scale, font) 
    end 
  end 
  if alignY then 
    if alignY == "center" then 
      ay = ay + (by - ay - dxGetFontHeight(scale, font))/2 
    elseif alignY == "bottom" then 
      ay = by - dxGetFontHeight(scale, font) 
    end 
  end 
  local alpha = string.format("%08X", color):sub(1,2) 
  local pat = "(.-)#(%x%x%x%x%x%x)" 
  local s, e, cap, col = str:find(pat, 1) 
  local last = 1 
  while s do 
    if cap == "" and col then color = tocolor(getColorFromString("#"..col..alpha)) end 
    if s ~= 1 or cap ~= "" then 
      local w = dxGetTextWidth(cap, scale, font) 
      dxDrawText(cap, ax, ay, ax + w, by, color, scale, font) 
      ax = ax + w 
      color = tocolor(getColorFromString("#"..col..alpha)) 
    end 
    last = e + 1 
    s, e, cap, col = str:find(pat, last) 
  end 
  if last <= #str then 
    cap = str:sub(last) 
    dxDrawText(cap, ax, ay, ax + dxGetTextWidth(cap, scale, font), by, color, scale, font) 
  end 
end 

Link to comment

try this:

local visibleText = {} 
addEventHandler ( "onClientRender",root,function() 
    local myFont = dxCreateFont("font.otf", 14) 
        for self,_ in pairs(visibleText) do 
            while true do 
                if self.bDestroyed then 
                    visibleText[self] = nil 
                    break 
                end 
                local l,t,r,b 
                --If we arent using a bounding box 
                if not self.tBoundingBox then 
                    --Decide if we use relative or absolute 
                    local p_screenX,p_screenY = 1,1 
                    if self.bRelativePosition then 
                        p_screenX,p_screenY = g_screenX,g_screenY 
                    end 
                    local fX,fY = (self.fX)*p_screenX,(self.fY)*p_screenY 
                    if self.bHorizontalAlign == "left" then 
                        l = fX 
                        r = fX + g_screenX 
                    elseif self.bHorizontalAlign == "right" then 
                        l = fX - g_screenX 
                        r = fX 
                    else 
                        l = fX - g_screenX 
                        r = fX + g_screenX 
                    end 
                    if self.bVerticalAlign == "top" then 
                        t = fY 
                        b = fY + g_screenY 
                    elseif self.bVerticalAlign == "bottom" then 
                        t = fY - g_screenY 
                        b = fY 
                    else 
                        t = fY - g_screenY 
                        b = fY + g_screenY 
                    end 
                elseif type(self.tBoundingBox) == "table" then 
                    local b_screenX,b_screenY = 1,1 
                    if self.bRelativeBoundingBox then 
                        b_screenX,b_screenY = g_screenX,g_screenY 
                    end 
                    l,t,r,b = self.tBoundingBox[1],self.tBoundingBox[2],self.tBoundingBox[3],self.tBoundingBox[4] 
                    l = l*b_screenX 
                    t = t*b_screenY 
                    r = r*b_screenX 
                    b = b*b_screenY 
                end 
                local type,att1,att2,att3,att4,att5 = self:type() 
                if type == "border" or type == "stroke" then 
                    att2 = att2 or 0 
                    att3 = att3 or 0 
                    att4 = att4 or 0 
                    att5 = att5 or self.tColor[4] 
                    outlinesize = att1 or 2 
                    outlinesize = math.min(self.fScale,outlinesize) --Make sure the outline size isnt thicker than the size of the label 
                    if outlinesize > 0 then 
                        for offsetX=-outlinesize,outlinesize,outlinesize do 
                            for offsetY=-outlinesize,outlinesize,outlinesize do 
                                if not (offsetX == 0 and offsetY == 0) then 
                                    dxDrawColorText(self.strText, l + offsetX, t + offsetY, r + offsetX, b + offsetY, tocolor(att2, att3, att4, att5), self.fScale, myFont, self.bHorizontalAlign, self.bVerticalAlign, self.bClip, self.bWordWrap, self.bPostGUI ) 
                                end 
                            end 
                        end 
                    end 
                elseif type == "shadow" then 
                    local shadowDist = att1 
                    att2 = att2 or 0 
                    att3 = att3 or 0 
                    att4 = att4 or 0 
                    att5 = att5 or self.tColor[4] 
                    string.gsub(self.strText, '#%x%x%x%x%x%x', '') 
                    dxDrawColorText(self.strText, l + shadowDist, t + shadowDist, r + shadowDist, b + shadowDist, tocolor(att2, att3, att4, att5), self.fScale, myFont, self.bHorizontalAlign, self.bVerticalAlign, self.bClip, self.bWordWrap, self.bPostGUI ) 
                end 
                dxDrawColorText ( self.strText, l, t, r, b, tocolor(unpack(self.tColor)), self.fScale, myFont, self.bHorizontalAlign, self.bVerticalAlign, self.bClip, self.bWordWrap, self.bPostGUI ) 
                break 
            end 
        end 
    end 
) 
  
addEvent ( "updateDisplays", true ) then 
addEventHandler ( "updateDisplays", getRootElement(),function(self) 
    setmetatable( self, dxText_mt ) 
    --Remove any old ones with the same id 
    for text,_ in pairs(visibleText) do 
        if text.id == self.id then 
            visibleText[text] = nil 
        end 
    end 
    if self.bVisible and not self.bDestroyed then 
        visibleText[self] = true 
    end 
end) 
  
function dxDrawColorText(str, ax, ay, bx, by, color, scale, font, alignX, alignY) 
  bx, by, color, scale, font = bx or ax, by or ay, color or tocolor(255,255,255,255), scale or 1, font or "default" 
  if alignX then 
    if alignX == "center" then 
      ax = ax + (bx - ax - dxGetTextWidth(str:gsub("#%x%x%x%x%x%x",""), scale, font))/2 
    elseif alignX == "right" then 
      ax = bx - dxGetTextWidth(str:gsub("#%x%x%x%x%x%x",""), scale, font) 
    end 
  end 
  if alignY then 
    if alignY == "center" then 
      ay = ay + (by - ay - dxGetFontHeight(scale, font))/2 
    elseif alignY == "bottom" then 
      ay = by - dxGetFontHeight(scale, font) 
    end 
  end 
  local alpha = string.format("%08X", color):sub(1,2) 
  local pat = "(.-)#(%x%x%x%x%x%x)" 
  local s, e, cap, col = str:find(pat, 1) 
  local last = 1 
  while s do 
    if cap == "" and col then color = tocolor(getColorFromString("#"..col..alpha)) end 
    if s ~= 1 or cap ~= "" then 
      local w = dxGetTextWidth(cap, scale, font) 
      dxDrawText(cap, ax, ay, ax + w, by, color, scale, font) 
      ax = ax + w 
      color = tocolor(getColorFromString("#"..col..alpha)) 
    end 
    last = e + 1 
    s, e, cap, col = str:find(pat, last) 
  end 
  if last <= #str then 
    cap = str:sub(last) 
    dxDrawText(cap, ax, ay, ax + dxGetTextWidth(cap, scale, font), by, color, scale, font) 
  end 
end 

EDITED it

Edited by Guest
Link to comment
local visibleText = {} 
local myFont = dxCreateFont("font.otf", 14) 
addEventHandler ( "onClientRender",root,function() 
        for self,_ in pairs(visibleText) do 
            while true do 
                if self.bDestroyed then 
                    visibleText[self] = nil 
                    break 
                end 
                local l,t,r,b 
                --If we arent using a bounding box 
                if not self.tBoundingBox then 
                    --Decide if we use relative or absolute 
                    local p_screenX,p_screenY = 1,1 
                    if self.bRelativePosition then 
                        p_screenX,p_screenY = g_screenX,g_screenY 
                    end 
                    local fX,fY = (self.fX)*p_screenX,(self.fY)*p_screenY 
                    if self.bHorizontalAlign == "left" then 
                        l = fX 
                        r = fX + g_screenX 
                    elseif self.bHorizontalAlign == "right" then 
                        l = fX - g_screenX 
                        r = fX 
                    else 
                        l = fX - g_screenX 
                        r = fX + g_screenX 
                    end 
                    if self.bVerticalAlign == "top" then 
                        t = fY 
                        b = fY + g_screenY 
                    elseif self.bVerticalAlign == "bottom" then 
                        t = fY - g_screenY 
                        b = fY 
                    else 
                        t = fY - g_screenY 
                        b = fY + g_screenY 
                    end 
                elseif type(self.tBoundingBox) == "table" then 
                    local b_screenX,b_screenY = 1,1 
                    if self.bRelativeBoundingBox then 
                        b_screenX,b_screenY = g_screenX,g_screenY 
                    end 
                    l,t,r,b = self.tBoundingBox[1],self.tBoundingBox[2],self.tBoundingBox[3],self.tBoundingBox[4] 
                    l = l*b_screenX 
                    t = t*b_screenY 
                    r = r*b_screenX 
                    b = b*b_screenY 
                end 
                local type,att1,att2,att3,att4,att5 = self:type() 
                if type == "border" or type == "stroke" then 
                    att2 = att2 or 0 
                    att3 = att3 or 0 
                    att4 = att4 or 0 
                    att5 = att5 or self.tColor[4] 
                    outlinesize = att1 or 2 
                    outlinesize = math.min(self.fScale,outlinesize) --Make sure the outline size isnt thicker than the size of the label 
                    if outlinesize > 0 then 
                        for offsetX=-outlinesize,outlinesize,outlinesize do 
                            for offsetY=-outlinesize,outlinesize,outlinesize do 
                                if not (offsetX == 0 and offsetY == 0) then 
                                    dxDrawText(string.gsub(self.strText, '#%x%x%x%x%x%x', ''), l + offsetX, t + offsetY, r + offsetX, b + offsetY, tocolor(att2, att3, att4, att5), self.fScale, myFont, self.bHorizontalAlign, self.bVerticalAlign, self.bClip, self.bWordWrap, self.bPostGUI ) 
                                end 
                            end 
                        end 
                    end 
                elseif type == "shadow" then 
                    local shadowDist = att1 
                    att2 = att2 or 0 
                    att3 = att3 or 0 
                    att4 = att4 or 0 
                    att5 = att5 or self.tColor[4] 
                    dxDrawText(string.gsub(self.strText, '#%x%x%x%x%x%x', ''), l + shadowDist, t + shadowDist, r + shadowDist, b + shadowDist, tocolor(att2, att3, att4, att5), self.fScale, myFont, self.bHorizontalAlign, self.bVerticalAlign, self.bClip, self.bWordWrap, self.bPostGUI ) 
                end 
                dxDrawColorText ( self.strText, l, t, r, b, tocolor(unpack(self.tColor)), self.fScale, myFont, self.bHorizontalAlign, self.bVerticalAlign, self.bClip, self.bWordWrap, self.bPostGUI ) 
                break 
            end 
        end 
    end 
) 
  
addEvent ( "updateDisplays", true ) then 
addEventHandler ( "updateDisplays", getRootElement(),function(self) 
    setmetatable( self, dxText_mt ) 
    --Remove any old ones with the same id 
    for text,_ in pairs(visibleText) do 
        if text.id == self.id then 
            visibleText[text] = nil 
        end 
    end 
    if self.bVisible and not self.bDestroyed then 
        visibleText[self] = true 
    end 
end) 
  
function dxDrawColorText(str, ax, ay, bx, by, color, scale, font,alignX,alignY,clip, wordBreak, postGUI) 
    if not str then return false end 
    str = " "..str 
    ax= ax - dxGetTextWidth(" ", scale, font) 
    local pat = "(.-)#(%x%x%x%x%x%x)" 
    local s, e, cap, col = str:find(pat, 1) 
    local last = 1 
    while s do 
        if s ~= 1 or cap ~= "" then 
            local w = dxGetTextWidth(cap, scale, font) 
            dxDrawText(cap, ax, ay, ax + w, by, color, scale, font,alignX,alignY,clip, wordBreak, postGUI) 
            ax = ax + w 
            color = tocolor(tonumber("0x"..string.sub(col, 1, 2)), tonumber("0x"..string.sub(col, 3, 4)), tonumber("0x"..string.sub(col, 5, 6)), 255) 
        end 
        last = e+1 
        s, e, cap, col = str:find(pat, last) 
    end 
    if last <= #str then 
        cap = str:sub(last) 
        local w = dxGetTextWidth(cap, scale, font) 
        dxDrawText(cap, ax, ay, ax + w, by, color, scale, font,alignX,alignY,clip, wordBreak, postGUI) 
    end 
end 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...