Jump to content

Just a question..


Timic

Recommended Posts

Hey,

im asking for dxText

well my question is

if i can make dxText with HEX color,

for example:

test = dxText:create('#ff0000Hey #ffffff._.', screenWidth - 88, 145, false, 'bankgothic', 1.5, 'right'),

he have to show like this on the screen Hey ._.

it is possible?

ty for reply :P

Link to comment

i made it but it dont work

function justWords(str) 
  local t = {} 
  local function helper(word) table.insert(t, word) return "" end 
  if not str:gsub("%w+", helper):find"%S" then return t end 
end 
  
  
-- Direct X Drawing 
  
addEventHandler("onClientRender",root, 
     
function() 
         
dxDrawText("#ff0000TEST #00ff00CODE",958.0,16.0,1399.0,54.0,tocolor(255,255,255,255),1.0,"bankgothic","left","top",false,false,false) 
  
end)  
  
  

:cry:

Link to comment

using the info posted by varez above i wrote a function for you:

function dxDrawColorText(str, ax, ay, bx, by, color, 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) 
      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) 
  end 
end 
  
-- example usage: 
  
testline = "testing: #ff0000red #00FF00green #0000ffblue blue #ff0000red again" 
addEventHandler("onClientRender", getRootElement(),  
function() 
  dxDrawColorText(testline, 100, 100, 900, 900, tocolor(255,255,255,255),1.0,"bankgothic")  
end) 

example result:

SksAb.jpg

PS: maybe someone more experienced will optimize ('cause im not sure) this and/or add text alignment/line break support.

PS: variant for RGBA color codes with alpha:

function dxDrawColorAlphaText(str, ax, ay, bx, by, color, scale, font) 
  local pat = "(.-)#(%x%x%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) 
      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)), tonumber("0x"..string.sub(col, 7, 8))) 
    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) 
  end 
end 

Edited by Guest
Link to comment

ok, but i'm looking for a more efficient way to get tocolor() value (since it's a signed integer) from RGB, possibly without using tocolor().

according to wiki, dx function can use hex AARRGGBB format for color, and they do, problem is (when ive tried simply put hex color without all those tonumber()'s and stuff):

0xFFFF0000 -- is red

0xFF00FF00 -- is green

0xFF0000FF -- is black (?)

but

0x7F0000FF -- is blue (with half opacity)

something to do with signed integers messed up somewhere, or i'm dumb :D

Link to comment

guess it has something to do with how Lua treat signed/unsigned 32bit integers, because:

string.format("%X", "0x7F0000FF") returns 7F0000FF

but

string.format("%X", "0xFF0000FF") returns FF000100

idk why, maybe some Lua pros will explain :x

Link to comment
guess it has something to do with how Lua treat signed/unsigned 32bit integers, because:

string.format("%X", "0x7F0000FF") returns 7F0000FF

but

string.format("%X", "0xFF0000FF") returns FF000100

idk why, maybe some Lua pros will explain :x

To me, it looks like it's the Lua engine's issue with handling large numbers and that's been discovered. If you look at the example you posted:

FF0000FF == 4278190335

FF000100 == 4278190336 (it's just 1 more than it's supposed to be)

I had the same problem with saving player's bank balance in my bank resource. Lua is problematic at handling large numbers.

Link to comment
  • 2 weeks later...
using the info posted by varez above i wrote a function for you:
function dxDrawColorText(str, ax, ay, bx, by, color, 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) 
      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) 
  end 
end 
  
-- example usage: 
  
testline = "testing: #ff0000red #00FF00green #0000ffblue blue #ff0000red again" 
addEventHandler("onClientRender", getRootElement(),  
function() 
  dxDrawColorText(testline, 100, 100, 900, 900, tocolor(255,255,255,255),1.0,"bankgothic")  
end) 

example result:

SksAb.jpg

PS: maybe someone more experienced will optimize ('cause im not sure) this and/or add text alignment/line break support.

ok ty, works, but it's white don't take the nick color <_<

testline = getPlayerName(player) 
dxDrawColorText(testline, sx, sy - offset, sx, sy - offset, tocolor(255,255,255,textalpha),0.4,"bankgothic") 

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...