Some useful text functions
-
Share Code: NX2L29NDNN
// some useful text functions: // getString : returns a string of the specified number of a character // getTextSize : returns the current text size // getTextWidth : returns the width of a string at the specified text size // author: pianofire // get a string consisting of number chars function getString( char, number ) string result = "" int i = 0 for i = 0 to number loop result += char repeat return result // find the current text size function getTextSize( ) int result = 0 string text = getString( "M", 50 ) result = textWidth( text ) result = int( round( result / 25.625 ) ) return result // find the width of text at the specified size function getTextWidth( text, size ) var result = 0 var saveTextSize = getTextSize( ) textSize( size ) result = textWidth( text ) textSize( saveTextSize ) return result // press X to continue function pressXtoContinue( ) int press = false var c println( " " ) println( "Press X to continue" ) while !press loop c = controls( 0 ) press = c.x update() repeat while press loop c = controls( 0 ) press = c.x update() repeat return void // Unit test code by @PB____ // print with new line function printLn( value ) int tw = twidth() - 2 while len( value ) > tw loop print( value[ :tw ], "\n" ) value = value[ tw: ] repeat print( value, "\n" ) return void // join a series of values together in a string function join(values) string result = "" int i = 0 for i = 0 to len( values ) loop result += str( values[ i ] ) repeat return result // assert that value1 is equal to value2 function assertEqual( value1, value2 ) string str1 = str( value1 ) string str2 = str( value2 ) string outcome = "" if str1 == str2 then ink( lime ) outcome = "success" else ink( red ) outcome = "fail" endif println( join( [ "", str1, " equals to ", str2, ": ", outcome ] ) ) ink( white ) return void // print test description function description( text ) ink( { 0.5, 0.5, 0.5, 1 } ) println(text) return void // run unit tests function runUnitTests( ) var result = "" description( "test getString" ) result = getString( "M", 10 ) assertEqual( result, "MMMMMMMMMM" ) description( "test getTextSize" ) result = getTextSize( ) assertEqual( result, 28 ) description( "test getTextWidth" ) result = getTextWidth( "The quick brown fox jumped over the lazy dog", 56 ) assertEqual( result, 1262.799927 ) pressXtoContinue( ) clear() return void runUnitTests( )
-
@pianofire Would be nice if some of them would get implemented in fuze as built in functions
-
@petermeisenstein yes it would but in the meantime...
-
@pianofire Yeah i know. its very kind that you posted a summary of all functions.
-
Very, very useful. Thanks a bunch ...
-
Yes, very useful indeed. Any chance for a getInk() function?
-
@vinicity Not sure how you could do that at the moment as there is no getPixel() function
-
@pianofire said in Some useful text functions:
@vinicity Not sure how you could do that at the moment as there is no getPixel() function
Yeah, thought as much. Any chance that getPixel() is coming to Fuze?
-
There is a feature request. Not scheduled as yet
-
@pianofire said in Some useful text functions:
@vinicity Not sure how you could do that at the moment as there is no getPixel() function
I guess I could implement my own ink() variant that sets the ink, and saves the value in a global. Then I could do a getInk() that returns the value...
-
@vinicity That should work
-
Any chance for a solution to my simple problem, i.e. MID$("a simple text", 4, 1). Tried everything - including writing to a file, reading it back, seeking etc. File write limit keeps popping up. So that idea went out of the window. Without simple string functions Fuze is now becoming a nightmare. All I'm trying to do is split - as an example - "D4" into "D" and "4". (chess notation by the way for my chess pgn reader program). Any help would really be appreciated. I'm now desperate! Thanks in advance.
-
You can access a string like an array so in your example text[0] would give you "D" and text[1] would give you "4". If you need more than 1 character you can use a colon eg text[0:1] would give "D4".
-
Also re: file write limit – the write limit occurs when the file is repeatedly opened and closed in a short period of time and not strictly when the write() function is called repeatedly (that is, it occurs when the file is repeatedly written to long-term storage and not when the file is modified in RAM). The write limit can be avoided by keeping the file open while you do reading/writing and only closing it once you're completely done accessing it.