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