Multi-line text test [as good as finished]

It’s on CC: http://codea.housercompany.com/alpha/index.php?v=827

it’s a little test of me using multi-line textbox, there are some useless vars in it, but those still have their purpose later on in my testing.

Sorry for the messy code and for the unotimised code

I know you guys will probably see a lot of optimisations, and I’m here to learn, so go ahead and share your thoughts :wink:

(further on in the testing will be: move your cursor where you want in the string)

raw code:



--# Main
--The name of the project must match your Codea project name if dependencies are used. 
--Project: Multi-line text test
--Version: Alpha 2.0
--Comments:

-- atext

-- Use this function to perform your initial setup
function setup()
    
    parameter.boolean('Show/Hide keyboard', false, keyswitch)
    
    --stroke color (cursor in this example)
    stroke(0, 0, 255, 255)
    strokeWidth(2)
    
    --text must be possitioned by corner
    textMode(CORNER)
    
    --'update' the variables of the font
    FontUpdate()
    
    pos = vec2(50, HEIGHT-10)
    
    --main string is the one that's displayed, but these
    --are the once being used for editing, and they compose
    --main in this same order
    --current = currentfirst .. currentlast
    --main = first .. current .. last
    --(current being the line you're currently on)
    --first being all line before
    --last being all lines (if any) behind current
    first = ''
        currentfirst = ''
        currentlast = ''
        current = currentfirst .. currentlast
    last = ''
    
    main = first .. current .. last
    
    y = 1 --which line's the cursor on?
    ymax = 1
    
    --cursor blinking (starting state)
    blink = true
    blink_count = 0
    blink_interval = 30
    
end

function draw()
    
    w, h = textSize(main)
    wcf, hcf = textSize(currentfirst)
    
    background(255, 255, 255, 255)
    
    --text placed will be pos.x from the left, and the top will be pos.y from the top
    text(main, pos.x, pos.y - h)
    
    
    --blink
    if blink_count%blink_interval == 0 then
        if blink then
            blink = false
        else
            blink = true
        end
        blink_count = 0
    end
    
    if blink then
        line(pos.x + wcf, pos.y - y*fheight, pos.x + wcf, pos.y - y*fheight + fheight)
    end
    
    blink_count = blink_count + 1
    --end of blink
    
end

function keyboard(key)
    
    if key == BACKSPACE then
        
        if currentfirst == '' and y > 1 then
            
            current = ''
            last = ''
            first = ''
            
            
            currentfirst = getline(main, y-1)
            current = currentfirst .. currentlast
            
            for i=1, y-2 do
                first = first .. getline(main, i)
                
                if i < ymax then
                    first = first .. '\
'
                end
            end
            
            for i=y, ymax do
                last = last .. getline(main, i)
                
                if i < ymax then
                    last = last .. '\
'
                end
            end
            
            last = string.sub(last, string.len(currentlast)+1, string.len(last))
            
            y = y - 1
            ymax = ymax - 1
            
        else
            
            currentfirst = string.sub(currentfirst, 0, string.len(currentfirst)-1)
                    
        end
        
    end
    
    currentfirst = currentfirst .. key
    
    current = currentfirst .. currentlast
    
    main = first .. current .. last
    
    if key == RETURN then
        y = y + 1
        ymax = ymax + 1
        first = first .. currentfirst
        currentfirst = ''
    end
    
end

function touched(t)
    
    changeloc(t)
    
end
--# functions
functions = class()

function changeloc(t)
    
    yy = pos.y - t.y
    
    yy = (yy-(yy%fheight))/fheight + 1
    
    if yy > 0 then
    
    y = yy
    
    if y > ymax then
        y = ymax
    end
    
    first = ''
    last = ''
    currentlast = ''
    current = ''
        
    currentfirst = getline(main, y)
    
    for i=1, y-1 do
        first = first .. getline(main, i)
        
        if i < ymax then
            first = first .. '\
'
        end
    end
    
    for i=y+1, ymax do
        last = last .. getline(main, i)
        
        if i < ymax then
            last = last .. '\
'
        end
    end
    
    last = '\
' .. last
    
    end
    
    x = t.x - pos.x
    
    wcfw, wcfh = textSize(currentfirst)
    
    if x < 0 then
        x = 0
    else
        x = math.floor(x/(wcfw/string.len(currentfirst)))
    end
    
    currentlast = string.sub(currentfirst, x+1, string.len(currentfirst))
    
    currentfirst = string.sub(currentfirst, 0, x)

end

function FontUpdate(fontsize, fontt)
   
    fontSize(fontsize or fontSize())
    font(fontt or font())
    
    update = 'a'
    
    -- the height of 1 'sentence'
    fwidth, fheight = textSize(update)
     
end

function getline(str, z)
    
    count = 0
    
    str = str .. "\
"
    for i in str:gmatch("(.-)\
") do
        
        count = count + 1
        if count == z then
            return i
        end
    end
    
end

function keyswitch(shown)
    if shown then
        showKeyboard()
    else
        hideKeyboard()
    end
end				



```


latest version

includes:

- multi-line textbox
- delete text with backspace
- new line with return
- change cursor inside the string
- change both the x and y possition (note that the x possition could slightly be different from where you place your finger, especially when having alot of 'big' chars in the beginning like 'w' and then after that alot of small chars like '.'  My mechanism for checking if it's at the right point isn't finished, i was to lazy to implement that right now, should only be 2 or 3 more lines of code but to tired to think)
- able to delete/add text at replaced cursor possition...!!

To do:

- text wrapping (meaning this could be used as small textboxes somewhere on the screen)
- scrolling when string's to high/to width but not wanting to wrap...

Well, I can see this is going to be a problem. Instead of posting code on the forum where it can be viewed easily along with the question, I have to switch to Codea, find CC in the list of projects, execute it, look thru another list of projects, and then load that project to look at the code. And if that isn’t bad enough, my iPad 1 crashes with CC, but they’re working on that. I can’t tell anyone where to post their code, but I think the code in CC should be completed, working code that is ready to share with everyone. I don’t think CC should be used as someplace to put questionable code. Of course that’s my opinion, and I guess I’ll just have to go along with the way everyone else wants to do it.

I think Codea Community is mostly for sharing projects that are done, or have the basic structure down but just new more stuff, I really don’t think it should become a place for people with questions to come, not only will it annoy people trying to help you, but it will also fill it up with content it doesn’t need. Just my opinion though.

EDIT: Also, you might want to look at my Hacker project on there, its has multiple line text, but in a different way…

Ok i removed it from CC
i’ll upload the code on here, tho i first need to change a couple of things, since i got a weird thing when changing the x-coordinate of my cursor, but i coded that part while in class, so didn’t have alot of time, code should be up in a hour or 2

Long press your version number and select copy. Then you can paste a link to it on the forums.

but now i am having problems…

without touching any of the typed lines… (so without using the ‘changeloc’ function) everything goes as i want, but once i start adding that function in, it starts acting freaked up… (check it out yourself to see what i mean)

it’s probably me not seeing something, and again, sorry for the messy code

I removed the f-word from the above post. Please moderate your language. There are seniors present.

ok I am sorry, I didn’t even notice i put it there, I’ll pay more attention to it next time

changed it to ‘freaked’

@steveon8ter I would help, but it’s too much trouble for me to get the code from CC, since CC doesn’t work on myiPad 1.

raw code: top post

Bug fixed, updated code in top post

Look at the top post for the current features :slight_smile:

@stevon8ter I was playing with your code. It looks like you have a lot of work to do, but you’re off to a good start. You might want to look at textWrapWidth() when you start inserting words in the middle of lines to keep the lines from going off the right side of the screen.

@dave1707 thx for the advice, I was already planning on doing that :wink:

once I have the bug of clicking fixed, I can move on, I already know how to determine the x possition in the string etc, and I’ll try to look for a more optimised way of doing what i did so far

ok sorry for triple post…
but I now got a weird bug… and it only appears sometimes…
when typing… everything’s ok
when deleting… ok
moving cursor… ok
editing after cursor move… ok

but sometime (like really only sometimes) after just having the cursor moved, pressing the backspace… deletes everything of the current line it’s in… well everything that’s behind the cursor in that line…

play around with it…

does anyone know what the problem is?

@stevon8ter Looks good. I was playing around and I couldn’t get your bug to happen. I’ll keep playing with it and see if I can get it to happen.

@dave1707 thx, and yes it’s something hard to get, but when switching possition fast enough, i almost always get it (well i mean everytime i try it, but it would still take a few tries) but i’ll see if i can post my vid on youtube tommorow, isn’t going through ipad --’

Here’s a video of the error (at 32seconds)
https://vimeo.com/77848225

@Stevon8ter Your video doesn’t show up, so I not sure what you were showing. I was able to see what I think you’re talking about. If a bunch of lines are keyed in an you touch the screen somewhere below the last line, it places the cursor on one of the typed lines and when you key something, part of the line disappears. One suggestion, if someone touches in the area below the last line, place the cursor at the end of the last line.

@dave1707 I never knew what caused it, looks like you found a possible explaination, i’ll look into it

thx

looks like that did it dave, thx :slight_smile:

http://codea.housercompany.com/alpha/index.php?v=827

code updated in first post as well

@stevon8ter I’m glad that helped. It’s really hard trying to fix a bug when you can’t find it. I tried your code and it works a lot better now. Keep on going.