text editing hmmmm

Started by wadmixfm, August 24, 2023, 17:51:32

Previous topic - Next topic

wadmixfm

ok i am here again with another idea , this code i am putting in here is from the archive with a few tweaks i have added

when you run it you will see a black window with a cursor flashing

here you can type in a line and press enter to enter it and it will turn green (thats the easy bit )

The tab key works i have set it to 5 spaces

but what i want to achieve if possible is editing the already created lines

the variable lyr2[cs,lylid] is (cs) = song number but thats set to 1 for now the (lylid) is how many lines are possible
in this case 39 so when you get to the bottom it will stop

i have added cursor movement but its only temporary (this is the bit i need help with :) )

i know i am a pain in the butt ,but i just cant get the cursor to edit the lines that are already created
for example i want to move the cursor to the line and say press tab so the text moves rights and then i can type in something.

i think you get the jist

pressing enter with then enter that line updating the one previous

anyways on to the code

lee :)

oh by the way the line in the middle of the screen is so i could determine where the center was and the numbers at the bottom is a character count upto 110

Import brl.D3D9Max2D
Import brl.Max2D

Graphics 1024,768
Global lyr$[20,45]
Global cs
cs=1

Global x =100 ' cursor x pos
Global y=38 ' cursor y pos
Global lylid ' how many lines
lylid=0
Global kt ' this is just a flag
kt=0


Local lyr2:GInput = GInput.Create(x,y+lylid*15)
While Not KeyHit(KEY_F10)
Cls
lyr2.Update
Flip
Wend

Type GInput
Field lyr2:String, enabled:Int = True
Field width:Int, cursor:Int = False, x:Int, y:Int, time:Long = MilliSecs()

Method Update()
If enabled Then
Local q:Int = GetChar()
If q > 0 Then
Select q
Case KEY_BACKSPACE
lyr2 = Left(lyr2, Len(lyr2) - 1)


Case KEY_ENTER 'here we need to save data to string
kt=1 ' key pressed
If kt=1 Then lyr$[cs,lylid]=lyr2
lyr2=""

Case KEY_TAB
lyr2:+ "     " 'set to 5 spaces
If Len(lyr2)>110 Then lyr$[cs,lylid]=lyr2  ; lylid=lylid+1 ; lyr2=""
Case KEY_ESCAPE
Default
lyr2:+ Chr(q) 'draw what we enter
End Select
width = TextWidth(lyr2)
cursor = True
time = MilliSecs()
End If
If MilliSecs() > time + 400 Then
cursor = Not cursor
time = MilliSecs()
End If
If cursor Then
SetColor 255,255,120
DrawLine x + width, y+lylid*15 + 1, x + width, y+lylid*15 + TextHeight("1") - 1 'draw flashing cursor
SetColor 255,255,255
End If
End If

DrawText "Lyric Editor",430,0
DrawText "   Song Info : (110 Chars Per Line ) Chars Used : "+Len(lyr2),560,0 'key count (110)
DrawText "Line : "+lylid,5,0
DrawText "----------------------------------------------------------------------------------------------------------------------------------",0,14
DrawText "----------------------------------------------------------------------------------------------------------------------------------",0,656
'DrawText "Editor Commands :-  Type using the keyboard pressing enter to complete the line , use up and down arrows to go to the line and",0,669
'DrawText "then press Enter To Edit that line,then press Enter Again. Press Tab To Change To Time Edit. ",0,682
'DrawText "Press F1 To Return Cursor To The Top. Press Escape To Exit Back To Drum Pattern Page.",0,695


DrawText x,0,60

DrawLine 90,21,90,657
SetColor 80,80,80
DrawText"012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890",100,639
DrawLine 540,29,540,649 'center line 1
DrawLine 548,29,548,649 'center line 2
SetColor 255,255,255
DrawText "----------------------------------------------------------------------------------------------------------------------------------",0,28

'here we draw the array into the box all 40 lines this updates after you press enter on each line

SetColor 0,255,0
For Local i = 0 To 39
DrawText lyr$[cs,i],100,38+i*15
Next
SetColor 255,255,255

If kt=1 Then lylid=lylid+1 x=100 kt=0
If lylid>39 Then lylid=39

'If KeyHit(KEY_F1) Then lylid=lylid+1 lylid=0 x=100 y=38 lyr2="" ' this is a return to top cursor but not working as such
If KeyHit(KEY_UP) Then lylid=lylid-1 keycheck()
If KeyHit(KEY_DOWN) Then lylid=lylid+1 keycheck()
If KeyHit(KEY_LEFT) Then keycheck3()
If KeyHit(KEY_RIGHT) Then keycheck4()
DrawText lyr2, x, y+lylid*15 ' update text and cursor
End Method

Function Create:GInput(x:Int, y:Int, lyr2:String = "")
Local ret:GInput = New GInput
ret.lyr2 = lyr2
ret.x = x
ret.y = y
ret.width = TextWidth(lyr2)
Return ret
End Function
End Type

Function keycheck()
If lylid<0 Then lylid=0
End Function
Function keycheck2()
If lylid>39 Then lylid=39
End Function
Function keycheck3()
If x=<100 Then x=100 ; Else x=x-8
End Function
Function keycheck4()
If x=>1000 Then x=1000 ; Else x=x+8
End Function

Midimaster

You should add a variable CursorPos that represents the character position inside a string instead of using x as a pixel coordinate.

now you can divide the active line in two parts: left of the cursor and right of the cursor. This enables to enter characters between this parts.

Graphics 600,400

Global CursorPos:Int=3
Global Text:String = "1234567890"
Repeat
    Cls
    DrawText Text, 100,200
    DrawText "_", 100+CursorPos*8-4,205
   
    DrawText "CURSORPOS: " + CursorPos,100,50
    DrawText"Press Keys [LEFT] or [RIGHT] or [M]",100,30
   
    If KeyHit(KEY_RIGHT) Then CursorPos:+1
    If KeyHit(KEY_LEFT)  Then CursorPos:-1
    If KeyHit(KEY_M)
        Text = Text[..CursorPos] + "M" + Text[CursorPos..]
    EndIf
    Flip
Until AppTerminate()

...back from Egypt