Navigation

    Fuze Arena Logo
    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Popular
    • Users
    • Groups
    • Help
    • Discord

    Some useful text functions

    Functions
    6
    14
    1105
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • pianofire
      pianofire Fuze Team last edited by pianofire

      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( )
      
      P 1 Reply Last reply Reply Quote 9
      • P
        petermeisenstein F @pianofire last edited by

        @pianofire Would be nice if some of them would get implemented in fuze as built in functions

        pianofire 1 Reply Last reply Reply Quote 0
        • pianofire
          pianofire Fuze Team @petermeisenstein last edited by

          @petermeisenstein yes it would but in the meantime...

          P 1 Reply Last reply Reply Quote 0
          • P
            petermeisenstein F @pianofire last edited by

            @pianofire Yeah i know. its very kind that you posted a summary of all functions.

            1 Reply Last reply Reply Quote 0
            • F
              faz808 F last edited by

              Very, very useful. Thanks a bunch ...

              1 Reply Last reply Reply Quote 0
              • vinicity
                vinicity F last edited by

                Yes, very useful indeed. Any chance for a getInk() function?

                pianofire 1 Reply Last reply Reply Quote 0
                • pianofire
                  pianofire Fuze Team @vinicity last edited by

                  @vinicity Not sure how you could do that at the moment as there is no getPixel() function

                  vinicity 2 Replies Last reply Reply Quote 0
                  • vinicity
                    vinicity F @pianofire last edited by

                    @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?

                    1 Reply Last reply Reply Quote 0
                    • pianofire
                      pianofire Fuze Team last edited by

                      There is a feature request. Not scheduled as yet

                      1 Reply Last reply Reply Quote 1
                      • vinicity
                        vinicity F @pianofire last edited by

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

                        pianofire 1 Reply Last reply Reply Quote 1
                        • pianofire
                          pianofire Fuze Team @vinicity last edited by

                          @vinicity That should work

                          1 Reply Last reply Reply Quote 0
                          • F
                            faz808 F last edited by

                            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.

                            1 Reply Last reply Reply Quote 0
                            • R
                              Richard F last edited by

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

                              1 Reply Last reply Reply Quote 1
                              • mozipha
                                mozipha last edited by mozipha

                                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.

                                1 Reply Last reply Reply Quote 1
                                • First post
                                  Last post