|
Cheat Engine The Official Site of Cheat Engine
|
View previous topic :: View next topic |
Author |
Message |
DaSpamer Grandmaster Cheater Supreme Reputation: 52
Joined: 13 Sep 2011 Posts: 1578
|
Posted: Sun Jun 23, 2019 4:32 am Post subject: word warp function |
|
|
Some small function I wrote for word-wrapping text, useful when working with re-sizable forms.
Code: | function word_wrap(pharse,callback)
if(not(type(pharse)=='string'and type(callback)=='function'))then return;end;
local wordsTable,ti,tc = {},table.insert,table.concat;
for line in pharse:gmatch('([^%.\n]+%.?)') do -- break on dot/newline
local tmpLine = {};
for word in line:gmatch('([^%s]+)') do -- divide + capture space
-- we don't really care for the word position only length!-->{word,unpack(words)}
if (#tmpLine == 0 or callback(tc({word,unpack(tmpLine)},' '))) then -- connect with space, also important!
ti(tmpLine,word)
else
ti(wordsTable,tc(tmpLine,' '));tmpLine = {word};
end
end
if(#tmpLine>0)then ti(wordsTable,tc(tmpLine,' '));end; -- insert last line
end
return tc(wordsTable,'\n');
end |
Usage example:
Code: | local text = "I am writing something.\nYes, I plan to make it the most boring thing ever written.\nI go to the store.\nA car is parked.\nMany cars are parked or moving.\nSome are blue.\nSome are tan.\nThey have windows.\nIn the store, there are items for sale.\nThese include such things as soap, detergent, magazines, and lettuce.\nYou can enhance your life with these products.\nSoap can be used for bathing, be it in a bathtub or in a shower.\nApply the soap to your body and rinse.\nDetergent is used to wash clothes.\nPlace your dirty";
f = createForm();
f.borderStyle = 'bsSizeable';
f.width,f.height = 200,300;
l = createLabel(f);
l.font.size = 15;
f.onResize = function(sender)
l.caption = word_wrap(text,function(line)
-- limit by number of characters per line; (including space);
-- return(#line < 100);end;
-- limit by actual line width on object.
if (l.canvas.getTextWidth(line) < f.width)then
return true;
end
return false;
end);
end;
f.onResize(f); |
_________________
Last edited by DaSpamer on Sun Jun 23, 2019 11:40 am; edited 1 time in total |
|
Back to top |
|
|
Corroder Grandmaster Cheater Supreme Reputation: 75
Joined: 10 Apr 2015 Posts: 1667
|
Posted: Sun Jun 23, 2019 5:50 am Post subject: |
|
|
From your example:
this line
Code: | l.caption = word_warp(text,function(line)
|
Not identifying by Lua, this gives error:
Code: | Error:[string "function word_warp(pharse,callback)
..."]:27: attempt to call a nil value (global 'word_wrap') |
_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL |
|
Back to top |
|
|
DaSpamer Grandmaster Cheater Supreme Reputation: 52
Joined: 13 Sep 2011 Posts: 1578
|
Posted: Sun Jun 23, 2019 11:41 am Post subject: |
|
|
Corroder wrote: | From your example:
this line
Code: | l.caption = word_warp(text,function(line)
|
Not identifying by Lua, this gives error:
Code: | Error:[string "function word_warp(pharse,callback)
..."]:27: attempt to call a nil value (global 'word_wrap') |
|
Typo in function name;
Re try now, should work just fine.
Also a note:
While I'm aware that this method exists with labels(which seems does not work when using lua) and memos.
I do not re call such function is available when drawning using (canvas),
which we can do it like this:
Code: | function word_wrap(pharse,callback)
if(not(type(pharse)=='string'and type(callback)=='function'))then return;end;
local wordsTable,ti,tc = {},table.insert,table.concat;
for line in pharse:gmatch('([^%.\n]+%.?)') do -- break on dot/newline; remove %. to break only by new lines;
local tmpLine = {};
for word in line:gmatch('([^%s]+)') do -- split by space
-- we don't really care for the word position only length!-->{word,unpack(words)}
if (#tmpLine == 0 or callback(tc({word,unpack(tmpLine)},' '))) then -- connect with space, also important!
ti(tmpLine,word);
else
ti(wordsTable,tc(tmpLine,' '));tmpLine = {word};
end
end
if(#tmpLine>0)then ti(wordsTable,tc(tmpLine,' '));end; -- insert last line
end
local i = 0;
return function () -- returns intraitor so we could use in for do loop..;
i = i+1;
return wordsTable[i];
end
end
f = createForm();
f.borderStyle = 'bsSizeable'
i = createImage(f);
i.align = 'alClient';
i.canvas.brush.color = 0;
i.canvas.font.color = 0xFFFFFF;
i.canvas.font.size = 16;
local text = "I am writing something.\nYes, I plan to make it the most boring thing ever written.\nI go to the store.\nA car is parked.\nMany cars are parked or moving.\nSome are blue.\nSome are tan.\nThey have windows.\nIn the store, there are items for sale.\nThese include such things as soap, detergent, magazines, and lettuce.\nYou can enhance your life with these products.\nSoap can be used for bathing, be it in a bathtub or in a shower.\nApply the soap to your body and rinse.\nDetergent is used to wash clothes.\nPlace your dirty";
i.onResize = function(sender)
local bmp = sender.picture.bitmap;bmp.canvas.clear();
bmp.width,bmp.height = sender.width,sender.height; -- resize bitmap so we could draw if size was increased;
local x,y = 5,5;
local width =(sender.width-x*2);
for line in word_wrap(text,function(line)if (sender.canvas.getTextWidth(line) < width)then
return true;end;return false;end) do
sender.Canvas.textOut(x,y,line)
y = y + sender.canvas.getTextHeight(line); -- get line height and append to y;
end
end;
i.onResize(i);
|
_________________
|
|
Back to top |
|
|
Corroder Grandmaster Cheater Supreme Reputation: 75
Joined: 10 Apr 2015 Posts: 1667
|
Posted: Sun Jun 23, 2019 11:23 pm Post subject: |
|
|
Nice work, bravo
_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL |
|
Back to top |
|
|
johnrude How do I cheat? Reputation: 0
Joined: 21 Jun 2019 Posts: 2
|
Posted: Tue Jun 25, 2019 10:48 pm Post subject: word warp function |
|
|
Thats thoughtful
|
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
|
|