Jump to content

Just a question..


Timic

Recommended Posts

Posted
are you sure? and what is the output? white with color code or white with color code deleted?

the output is the name whitout codes but white

ok, my bad, function doesnt read the color value if string starts with it, update dxDrawColorText function:

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 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  
      local w = dxGetTextWidth(cap, scale, font) 
      dxDrawText(cap, ax, ay, ax + w, by, color, scale, font) 
      ax = ax + w 
      color = tocolor(tonumber("0x"..col:sub(1, 2)), tonumber("0x"..col:sub(3, 4)), tonumber("0x"..col:sub(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 
  
--[[ testing: 
testname1 = "#FF0000RedGuy"  
testname2 = "White#0000FFBlue"  
testname3 = "#00FF00Green, #FFFFFFWhite white white, #FF0000Red"  
  
addEventHandler("onClientRender", getRootElement(),  
  function() 
    dxDrawColorText(testname1, 200, 300, 200, 50, tocolor(255,255,255,255), 1, "bankgothic") 
    dxDrawColorText(testname2, 200, 320, 200, 50, tocolor(255,255,255,255), 1, "bankgothic") 
    dxDrawColorText(testname3, 200, 340, 200, 50, tocolor(255,255,255,255), 1, "bankgothic") 
  end 
) 
--]] 

test:

sshot20101_2111345_1331179.png

Posted
are you sure? and what is the output? white with color code or white with color code deleted?

the output is the name whitout codes but white

ok, my bad, function doesnt read the color value if string starts with it, update dxDrawColorText function:

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 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  
      local w = dxGetTextWidth(cap, scale, font) 
      dxDrawText(cap, ax, ay, ax + w, by, color, scale, font) 
      ax = ax + w 
      color = tocolor(tonumber("0x"..col:sub(1, 2)), tonumber("0x"..col:sub(3, 4)), tonumber("0x"..col:sub(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 
  
--[[ testing: 
testname1 = "#FF0000RedGuy"  
testname2 = "White#0000FFBlue"  
testname3 = "#00FF00Green, #FFFFFFWhite white white, #FF0000Red"  
  
addEventHandler("onClientRender", getRootElement(),  
  function() 
    dxDrawColorText(testname1, 200, 300, 200, 50, tocolor(255,255,255,255), 1, "bankgothic") 
    dxDrawColorText(testname2, 200, 320, 200, 50, tocolor(255,255,255,255), 1, "bankgothic") 
    dxDrawColorText(testname3, 200, 340, 200, 50, tocolor(255,255,255,255), 1, "bankgothic") 
  end 
) 
--]] 

test:

sshot20101_2111345_1331179.png

mhhh... no xD don't works... T_T

Posted

in that case i dont know. as you can see — i've tested.

maybe you dont have a hex code in the name at all.

maybe something else, i cant see from here.

  • 3 months later...
Posted
in that case i dont know. as you can see — i've tested.

maybe you dont have a hex code in the name at all.

maybe something else, i cant see from here.

It's works, but it doesn't works just whit player's name...

Posted
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

This is not related to tocolor function. tocolor actually returns 32bit signed integer, so if the value is bigger than 0x7FFFFFFF, it's negative. Therefore if color value is 0x80000000 or more, you need to subtract 0x100000000 from it to get a value which dx functions can understand.

  • 2 months later...
Posted
This is not related to tocolor function. tocolor actually returns 32bit signed integer, so if the value is bigger than 0x7FFFFFFF, it's negative. Therefore if color value is 0x80000000 or more, you need to subtract 0x100000000 from it to get a value which dx functions can understand.

actually i've figured that out, and tried to subtract and all that, but it didnt work out as i recall. also: http://bugs.mtasa.com/view.php?id=5854

Can someone do it with centering compatible?

I'm trying to do it, but I fail :(

well it's enough to calculate from size of the resulting dx text and bounding box size. something like this:

function dxDrawColorText(str, ax, ay, bx, by, color, scale, font, alignX, alignY) 
  
  if alignX then 
    if alignX == "center" then  
      local w = dxGetTextWidth(str:gsub("#%x%x%x%x%x%x",""), scale, font) 
      ax = ax + (bx-ax)/2 - w/2 
    elseif alignX == "right" then 
      local w = dxGetTextWidth(str:gsub("#%x%x%x%x%x%x",""), scale, font) 
      ax = bx - w 
    end 
  end 
  
  if alignY then 
    if alignY == "center" then  
      local h = dxGetFontHeight(scale, font) 
      ay = ay + (by-ay)/2 - h/2 
    elseif alignY == "bottom" then 
      local h = dxGetFontHeight(scale, font) 
      ay = by - h 
    end 
  end 
  
  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(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 
      local w = dxGetTextWidth(cap, scale, font) 
      dxDrawText(cap, ax, ay, ax + w, by, color, scale, font) 
      ax = ax + w 
      color = tocolor(tonumber("0x"..col:sub(1, 2)), tonumber("0x"..col:sub(3, 4)), tonumber("0x"..col:sub(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 

test:

Region2011_2640036_2194614.png

Posted

Thank you!

Works great, only one more problem:

dxDrawText(cap, ax, ay, ax + w, by, color, scale, font, "left", "top", false, true) 

Doesn't break the text to a new line.. :S

Posted
Thank you!

Works great, only one more problem:

dxDrawText(cap, ax, ay, ax + w, by, color, scale, font, "left", "top", false, true) 

Doesn't break the text to a new line.. :S

well because line breaks are not supported there. that is what needs to be done :/

Posted

Well, I want to do colored nick, but it gives me an error. Help please :arrowdown:

The code, client-side

  
local x, y = guiGetScreenSize () 
  
local timicSettings = { 
xOffset = 556, 
yOffset = (y / 2) - 445, 
xOffset1 = 552, 
yOffset1 = (y / 2) - 443, 
} 
  
local textX = x - timicSettings.xOffset 
local textY = timicSettings.yOffset 
local textX1 = x - timicSettings.xOffset1 
local textY1 = timicSettings.yOffset1 
  
addEventHandler("onClientRender",root, 
  
function() 
  
dxDrawText("~XG~Timic",textX1, textY1, x, y,tocolor(0,0,0,255),1.0,"bankgothic","left","top",false,false,false) 
  
dxDrawColorText(getPlayerName(),textX, textY, x, y,tocolor(255,255,255,255),1.0,"bankgothic","left","top",false,false,false) 
  
end 
  
) 
  
function dxDrawColorText(str, ax, ay, bx, by, color, scale, font, alignX, alignY) 
  
  if alignX then 
    if alignX == "center" then 
      local w = dxGetTextWidth(str:gsub("#%x%x%x%x%x%x",""), scale, font) 
      ax = ax + (bx-ax)/2 - w/2 
    elseif alignX == "right" then 
      local w = dxGetTextWidth(str:gsub("#%x%x%x%x%x%x",""), scale, font) 
      ax = bx - w 
    end 
  end 
  
  if alignY then 
    if alignY == "center" then 
      local h = dxGetFontHeight(scale, font) 
      ay = ay + (by-ay)/2 - h/2 
    elseif alignY == "bottom" then 
      local h = dxGetFontHeight(scale, font) 
      ay = by - h 
    end 
  end 
  
  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(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 
      local w = dxGetTextWidth(cap, scale, font) 
      dxDrawText(cap, ax, ay, ax + w, by, color, scale, font) 
      ax = ax + w 
      color = tocolor(tonumber("0x"..col:sub(1, 2)), tonumber("0x"..col:sub(3, 4)), tonumber("0x"..col:sub(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 

Picture:

http://www.shrani.si/f/2q/Yy/3YxUDuF9/m ... 1613-4.png

Posted

what is getPlayerName() at line 22? it need player element.

right now it returns false.

debug window pretty much shows all you need to fix this.

Posted

I've fixed something,

local x, y = guiGetScreenSize () 
  
local timicSettings = { 
xOffset = 556, 
yOffset = (y / 2) - 445,  
xOffset1 = 552,  
yOffset1 = (y / 2) - 443,  
} 
  
local textX = x - timicSettings.xOffset 
local textY = timicSettings.yOffset 
local textX1 = x - timicSettings.xOffset1 
local textY1 = timicSettings.yOffset1 
  
addEventHandler("onClientRender",root, 
  
function(player,name) 
local getp = type(name)=='string' and string.gsub ( name, '#%x%x%x%x%x%x', '' ) or name 
  
dxDrawText("~XG~Timic",textX1, textY1, x, y,tocolor(0,0,0,255),1.0,"bankgothic","left","top",false,false,false) 
  
dxDrawColorText(getp,textX, textY, x, y,tocolor(255,255,255,255),1.0,"bankgothic","left","top",false,false,false) 
  
end 
  
) 
  
--- --- --- --- --- 
function dxDrawColorText(str, ax, ay, bx, by, color, scale, font, alignX, alignY) 
  
  if alignX then 
    if alignX == "center" then 
      local w = dxGetTextWidth(str:gsub("#%x%x%x%x%x%x",""), scale, font) 
      ax = ax + (bx-ax)/2 - w/2 
    elseif alignX == "right" then 
      local w = dxGetTextWidth(str:gsub("#%x%x%x%x%x%x",""), scale, font) 
      ax = bx - w 
    end 
  end 
  
  if alignY then 
    if alignY == "center" then 
      local h = dxGetFontHeight(scale, font) 
      ay = ay + (by-ay)/2 - h/2 
    elseif alignY == "bottom" then 
      local h = dxGetFontHeight(scale, font) 
      ay = by - h 
    end 
  end 
  
  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(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 
      local w = dxGetTextWidth(cap, scale, font) 
      dxDrawText(cap, ax, ay, ax + w, by, color, scale, font) 
      ax = ax + w 
      color = tocolor(tonumber("0x"..col:sub(1, 2)), tonumber("0x"..col:sub(3, 4)), tonumber("0x"..col:sub(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 
  

It gives me:

ERROR: test\test_c.lua:52: attempt to index local 'str' (a nil value)

Posted

It gives me:

ERROR: test\test_c.lua:52: attempt to index local 'str' (a nil value)

very strange, it seems it IS defined before.

probably i'm missing something obvious.

Posted

function(player,name): and how do you plan to get player and name from onClientRender event?

use getPlayerName(getLocalPlayer()) (and you dont need to do it every frame)

but since you're stripping color tags, you dont need dxDrawColorText function anyway.

Posted

There are no errors, but it shows white nick :S

  
local x, y = guiGetScreenSize () 
  
local timicSettings = { 
xOffset = 556, 
yOffset = (y / 2) - 445, 
xOffset1 = 552, 
yOffset1 = (y / 2) - 443, 
} 
  
local textX = x - timicSettings.xOffset 
local textY = timicSettings.yOffset 
local textX1 = x - timicSettings.xOffset1 
local textY1 = timicSettings.yOffset1 
  
addEventHandler("onClientRender",root, 
  
function() 
  
dxDrawText("~XG~Timic",textX1, textY1, x, y,tocolor(0,0,0,255),1.0,"bankgothic","left","top",false,false,false) 
  
dxDrawColorText(getPlayerName(getLocalPlayer()),textX, textY, x, y,tocolor(255,255,255,255),1.0,"bankgothic","left","top",false,false,false) 
  
end 
  
) 
  
--- --- --- --- --- 
function dxDrawColorText(str, ax, ay, bx, by, color, scale, font, alignX, alignY) 
  
  if alignX then 
    if alignX == "center" then 
      local w = dxGetTextWidth(str:gsub("#%x%x%x%x%x%x",""), scale, font) 
      ax = ax + (bx-ax)/2 - w/2 
    elseif alignX == "right" then 
      local w = dxGetTextWidth(str:gsub("#%x%x%x%x%x%x",""), scale, font) 
      ax = bx - w 
    end 
  end 
  
  if alignY then 
    if alignY == "center" then 
      local h = dxGetFontHeight(scale, font) 
      ay = ay + (by-ay)/2 - h/2 
    elseif alignY == "bottom" then 
      local h = dxGetFontHeight(scale, font) 
      ay = by - h 
    end 
  end 
  
  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(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 
      local w = dxGetTextWidth(cap, scale, font) 
      dxDrawText(cap, ax, ay, ax + w, by, color, scale, font) 
      ax = ax + w 
      color = tocolor(tonumber("0x"..col:sub(1, 2)), tonumber("0x"..col:sub(3, 4)), tonumber("0x"..col:sub(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 
  

Posted

create simple dxDrawText below with the same text, and check if your name even contains color.

Posted
There are no errors, but it shows white nick :S
  
local x, y = guiGetScreenSize () 
  
local timicSettings = { 
xOffset = 556, 
yOffset = (y / 2) - 445, 
xOffset1 = 552, 
yOffset1 = (y / 2) - 443, 
} 
  
local textX = x - timicSettings.xOffset 
local textY = timicSettings.yOffset 
local textX1 = x - timicSettings.xOffset1 
local textY1 = timicSettings.yOffset1 
  
addEventHandler("onClientRender",root, 
  
function() 
  
dxDrawText("~XG~Timic",textX1, textY1, x, y,tocolor(0,0,0,255),1.0,"bankgothic","left","top",false,false,false) 
  
dxDrawColorText(getPlayerName(getLocalPlayer()),textX, textY, x, y,tocolor(255,255,255,255),1.0,"bankgothic","left","top",false,false,false) 
  
end 
  
) 
  
--- --- --- --- --- 
function dxDrawColorText(str, ax, ay, bx, by, color, scale, font, alignX, alignY) 
  
  if alignX then 
    if alignX == "center" then 
      local w = dxGetTextWidth(str:gsub("#%x%x%x%x%x%x",""), scale, font) 
      ax = ax + (bx-ax)/2 - w/2 
    elseif alignX == "right" then 
      local w = dxGetTextWidth(str:gsub("#%x%x%x%x%x%x",""), scale, font) 
      ax = bx - w 
    end 
  end 
  
  if alignY then 
    if alignY == "center" then 
      local h = dxGetFontHeight(scale, font) 
      ay = ay + (by-ay)/2 - h/2 
    elseif alignY == "bottom" then 
      local h = dxGetFontHeight(scale, font) 
      ay = by - h 
    end 
  end 
  
  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(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 
      local w = dxGetTextWidth(cap, scale, font) 
      dxDrawText(cap, ax, ay, ax + w, by, color, scale, font) 
      ax = ax + w 
      color = tocolor(tonumber("0x"..col:sub(1, 2)), tonumber("0x"..col:sub(3, 4)), tonumber("0x"..col:sub(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 
  

You didn't even reply him.

IS YOUR NICKNAME COLORCODED?

Example: #FF00FFEco =================> Eco

Posted
There are no errors, but it shows white nick :S
  
local x, y = guiGetScreenSize () 
  
local timicSettings = { 
xOffset = 556, 
yOffset = (y / 2) - 445, 
xOffset1 = 552, 
yOffset1 = (y / 2) - 443, 
} 
  
local textX = x - timicSettings.xOffset 
local textY = timicSettings.yOffset 
local textX1 = x - timicSettings.xOffset1 
local textY1 = timicSettings.yOffset1 
  
addEventHandler("onClientRender",root, 
  
function() 
  
dxDrawText("~XG~Timic",textX1, textY1, x, y,tocolor(0,0,0,255),1.0,"bankgothic","left","top",false,false,false) 
  
dxDrawColorText(getPlayerName(getLocalPlayer()),textX, textY, x, y,tocolor(255,255,255,255),1.0,"bankgothic","left","top",false,false,false) 
  
end 
  
) 
  
--- --- --- --- --- 
function dxDrawColorText(str, ax, ay, bx, by, color, scale, font, alignX, alignY) 
  
  if alignX then 
    if alignX == "center" then 
      local w = dxGetTextWidth(str:gsub("#%x%x%x%x%x%x",""), scale, font) 
      ax = ax + (bx-ax)/2 - w/2 
    elseif alignX == "right" then 
      local w = dxGetTextWidth(str:gsub("#%x%x%x%x%x%x",""), scale, font) 
      ax = bx - w 
    end 
  end 
  
  if alignY then 
    if alignY == "center" then 
      local h = dxGetFontHeight(scale, font) 
      ay = ay + (by-ay)/2 - h/2 
    elseif alignY == "bottom" then 
      local h = dxGetFontHeight(scale, font) 
      ay = by - h 
    end 
  end 
  
  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(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 
      local w = dxGetTextWidth(cap, scale, font) 
      dxDrawText(cap, ax, ay, ax + w, by, color, scale, font) 
      ax = ax + w 
      color = tocolor(tonumber("0x"..col:sub(1, 2)), tonumber("0x"..col:sub(3, 4)), tonumber("0x"..col:sub(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 
  

You didn't even reply him.

IS YOUR NICKNAME COLORCODED?

Example: #FF00FFEco =================> Eco

Yeah, I tried but it did not work.

I've tried with ~XG~Tim#ff9000ic name

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