Timic Posted November 22, 2010 Share Posted November 22, 2010 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 Link to comment
dzek (varez) Posted November 22, 2010 Share Posted November 22, 2010 not possible in simple way. you will need to write custom function that will be splitting text on every color code, then displaying few dx texts with various colors some links: http://lua-users.org/wiki/SplitJoin https://wiki.multitheftauto.com/wiki/DxDrawText https://wiki.multitheftauto.com/wiki/DxGetTextWidth Link to comment
dzek (varez) Posted November 22, 2010 Share Posted November 22, 2010 i thought so. as you probably know - nobody is going to fulfill your request Link to comment
Timic Posted November 23, 2010 Author Share Posted November 23, 2010 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) Link to comment
dzek (varez) Posted November 23, 2010 Share Posted November 23, 2010 it have no sense at all o_O Link to comment
dzek (varez) Posted November 23, 2010 Share Posted November 23, 2010 i gave you some infos - you are just doing "random scripting". just learn basics of lua, as you dont know even the most basic things as i can see.. Link to comment
Aibo Posted November 23, 2010 Share Posted November 23, 2010 (edited) 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: 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 November 23, 2010 by Guest Link to comment
dzek (varez) Posted November 23, 2010 Share Posted November 23, 2010 add this please to wiki's Useful Functions and add it to See Also for dxDrawText Link to comment
Aibo Posted November 23, 2010 Share Posted November 23, 2010 i don't think i'm quite "eligible" to edit the wiki Link to comment
dzek (varez) Posted November 23, 2010 Share Posted November 23, 2010 why not? everyone can do it. Link to comment
Aibo Posted November 23, 2010 Share Posted November 23, 2010 i know that, it's just i'm not confident enough Link to comment
dzek (varez) Posted November 23, 2010 Share Posted November 23, 2010 i'll do it tommorow for you then i'll mark you as author Link to comment
Aibo Posted November 23, 2010 Share Posted November 23, 2010 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 Link to comment
dzek (varez) Posted November 23, 2010 Share Posted November 23, 2010 lol, seems like something is broken indeed... you could make some variables local (like: color, ax). Link to comment
Aibo Posted November 23, 2010 Share Posted November 23, 2010 since they are passed to the function the are local by default, or am i mistaken? Link to comment
Aibo Posted November 24, 2010 Share Posted November 24, 2010 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 Link to comment
50p Posted November 24, 2010 Share Posted November 24, 2010 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 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
Timic Posted November 24, 2010 Author Share Posted November 24, 2010 (edited) Thank you so much Edited November 24, 2010 by Guest Link to comment
#Paper Posted December 7, 2010 Share Posted December 7, 2010 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: 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
Aibo Posted December 7, 2010 Share Posted December 7, 2010 does nick have color code in it at all? Link to comment
#Paper Posted December 8, 2010 Share Posted December 8, 2010 does nick have color code in it at all? yes it have... Link to comment
Aibo Posted December 8, 2010 Share Posted December 8, 2010 are you sure? and what is the output? white with color code or white with color code deleted? Link to comment
#Paper Posted December 8, 2010 Share Posted December 8, 2010 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 Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now