Simple string editing example

Jimmy

Well-known member
3rd Party Module Dev
Tutorial Author
Joined
Jan 2, 2020
Messages
1,021
The simplest menu and editor that you could ever imagine,
just to show how to use strings and keyboard. :coffee:

Try to move the white text to the left par of the screen using the cursors keys.

Miami.png


Code:
Import mojo2

Function Main()
    New Game
    Return 0
End

Class Game Extends App

    Field ascii:String
    Field str:String = ""
    Field screen:String[100]
    Field canvas:Canvas
    Field x:Int = 256
    Field y:Int = 12*8

    Method OnCreate()
       
        canvas = New Canvas()  
        Print Len("test") ' 4
        Print Mid("test",2,2) ' es
        Print Left("test",2) ' te
        Print Right("test",2) ' st
        Print Chr(65) ' A
        Print Val("A") ' 65
       
        ' Alphabet outputs 1 - 26 regardless if the letter A-Z is in uppercase or lowercase
        Print Alphabet("A") ' Outputs 1 for a
        Print Alphabet("a") ' Outputs 1 for a
        Print Alphabet("abba",0) ' First letter, outputs 1 for a
        Print Alphabet("abba",1) ' Second letter, outputs 2 for b
        Print Alphabet("abba",2) ' Third letter, 2 for b

        ascii = "D"
        Print Val(ascii)
       
        screen[0] = "Miami, Miami, you've got style. Blue sky, sunshine, white sand by the mile."
        screen[1] = "When you live in this town, each day is sublime. The coldest of winters are warm and divine."
        screen[2] = "Miami, Miami, you've got style. Blue sky, sunshine, white sand by the mile."
         screen[3] = "There's ball clubs and night clubs, all within reach."
         screen[4] = "Dance the samba 'till morning, then lie on the beach."
         screen[5] = "Each view is a postcard, each day a great time."
        screen[6] = "The cream of the crop, it's the top of the line."        
        screen[7] = "Miami, Miami, you've got style. Blue sky, sunshine, white sand by the mile."        
        screen[8] = "Miami, you've got style!"

        Return 0
    End
       
    Method OnUpdate()
   
        Local chr:Int = GetChar()
        If chr >= 32 And chr < 128 Then str = str + Chr(chr)
        If chr = 8 Then str = Left(str,Len(str)-1)

        If chr <> 0 Then Print chr
   
        Local oldy:Int = y
        If chr = 65573 Then Print "Left" ; x = x - 8 ; If x < -16 Then x = -16
       
        If chr = 65575 Then Print "Right" ; x = x + 8 ; If x > 640-8 Then x = 640-8
   
        If chr = 65574 Then Print "Up" ; y = y - 12 ; If y < 0 Then y = 0
       
        If chr = 65576 Then Print "Down" ; y = y + 12 ; If y > 480-12 Then y = 480-12
       
        If chr = 13 Then Print "Enter" ; y = y + 12 ; If y > 480-12 Then y = 480-12

        If oldy <> y Then str = screen[Floor(y/12)]
        screen[Floor(y/12)] = str
       
    End
 
    Method OnRender()
        canvas.Clear
             
        canvas.SetColor 0,0.2,0,1
        For Local i:Int = 0 To 99
                canvas.DrawText screen[i],0,i * 12
        Next
             
        canvas.SetColor 1,1,1,(Millisecs()/1000.0)
        canvas.DrawText "> " + str,x,y
       
        canvas.Flush
    End
 
End

Function Len:Int(s:String)
    Return s.Length
End

Function Mid:String(s:String,p:Int,n:Int)
    p=p-1
    Return s[(p)..(p+n)]
End

Function Left:String(s:String,n:Int)
    Return s[0..n]
End

Function Right:String(s:String,n:Int)
    Return s[(s.Length()-n)..(s.Length())]
End

Function Chr:String(n:Int)
    Local result:String
    result = result.FromChar(n)
    Return result
End

Function Val:Int(s:String)  
        Local chrs:Int[] = s.ToChars()
        Return chrs[0]
End

Function Alphabet:Int(s:String,n:Int=0)
        Local chrs:Int[] = s.ToChars()
        Return chrs[n] & 31
End
 
Last edited:

Jimmy

Well-known member
3rd Party Module Dev
Tutorial Author
Joined
Jan 2, 2020
Messages
1,021
And here's a full line-text editor. I just lays out the basics, you could add and modify as you wish.
You have both insert and overwrite mode.

Screenshot.png

Code:
Import mojo2

Function Main()
    New Game
    Return 0
End

Class Game Extends App

    Field canvas:Canvas
    Field str:String = "", screen:String[100]
    Field x:Int = 256, y:Int = 14*12
    Field mode:Int = 1 ' 2 = Overwritemode
    
    Method OnCreate()
        canvas = New Canvas() 
        screen[0] = "Miami, Miami, you've got style. Blue sky, sunshine, white sand by the mile."
        screen[1] = "When you live in this town, each day is sublime. The coldest of winters are warm and divine."
        screen[2] = "Miami, Miami,you've got style. Blue sky, sunshine, white sand by the mile."
        screen[3] = "There's ball clubs and night clubs, all within reach."
        screen[4] = "Dance the samba 'till morning, then lie on the beach."
        screen[5] = "Each view is a postcard, each day a great time."
        screen[6] = "The cream of the crop, it's the top of the line."       
        screen[7] = "Miami, Miami, you've got style. Blue sky, sunshine, white sand by the mile."       
        screen[8] = "Miami, you've got style!"     
        Return 0
    End

    Method OnUpdate()   
        Local chr:Int = GetChar()
        
        If chr > 31 And chr < 128 ' WRITING
            Local saved:String = Mid(str,x/8+mode,80)
            While Len(str) < (x/8)
                str = str + " "
            Wend
            str = Left(str,x/8) ; x = x + 8
            str = str + Chr(chr) + saved
        Endif
        
        If chr = 8 And x > 0 ' BACKSPACE
            Local saved:String = Mid(str,x/8+1,80)
            While Len(str) < (x/8)
                str = str + " "
            Wend
            str = Left(str,x/8) ; x = x - 8
            str = Left(str,Len(str)-1) + saved
        Endif
        
        Local oldy:Int = y
        If chr = 65573 Then x = x - 8 ; x = Max(x,0)        ' Left-key
        If chr = 65575 Then x = x + 8 ; x = Min(x,640-8)    ' Right-key
        If chr = 65574 Then y = y - 12 ; y = Max(y,0)        ' Up-key
        If chr = 65576 Then y = y + 12 ; y = Min(y,480-12)    ' Down-key
        If chr = 13 Then y = y + 12 ; y = Min(y,480-12)        ' Enter-key
        If oldy <> y Then str = screen[Floor(y/12)] ' Did we change line?
        screen[Floor(y/12)] = str
    End
 
    Method OnRender()
        canvas.Clear
        '
        ' Draw text
        canvas.SetColor 0,0.4,0,1
        For Local i:Int = 0 To 99
            canvas.DrawText screen[i],0,i * 12
        Next
         '
        ' Draw cursor
        canvas.SetColor 1,1,1,(Millisecs()/1000.0)
        canvas.DrawLine x,y,x,y+12
        canvas.DrawText str,0,y
        '
        canvas.Flush
    End
 
End

Function Len:Int(s:String)
    Return s.Length
End

Function Mid:String(s:String,p:Int,n:Int)
    p=p-1 ; Return s[(p)..(p+n)]
End

Function Left:String(s:String,n:Int)
    Return s[0..n]
End

Function Right:String(s:String,n:Int)
    Return s[(s.Length()-n)..(s.Length())]
End

Function Chr:String(n:Int)
    Local result:String ; result = result.FromChar(n) ; Return result
End
 
Function Val:Int(s:String) 
    Local chrs:Int[] = s.ToChars() ; Return chrs[0]
End

Function Alphabet:Int(s:String,n:Int=0)
    Local chrs:Int[] = s.ToChars() ; Return chrs[n] & 31
End
 
Last edited:
Top Bottom