I needed some editable text boxes for a project (cant use input forever
)
This is what i came up with...
rem text box template
rem by bob marley
rem selection variable
global tbsel as dword:global tbcur as dword:global tbcur2 as dword:global tbcur3 as byte: global tbselm as integer: global mtimer as byte : global bksptimer as byte : global tabtimer as byte : global lefttimer as byte : global righttimer as byte : global copytimer as byte : global pastetimer as byte : global ltime as double float
rem array
type tbt
flag as byte
left as integer
top as integer
right as integer
bottom as integer
tlcol as dword
trcol as dword
blcol as dword
brcol as dword
borcol as dword
fontcol as dword
selcol as dword
content$ as string
endtype
dim tba(0) as tbt
rem make tb
rem maketb(flag,left,top,right,bottom,tlcol,trcol,blcol,brcol,borcol,fontcol,entry)-- flag-> 0=hidden 1= text only 2= text and box 3=text,box and border
maketb(3,200,200,400,216,rgb(100,100,255),rgb(50,50,255),rgb(50,50,255),rgb(0,0,150),rgb(0,0,50),rgb(0,0,0),rgb(180,180,180),"jamie is awesome")
maketb(3,200,400,400,416,rgb(100,100,255),rgb(50,50,255),rgb(50,50,255),rgb(0,0,150),rgb(0,0,50),rgb(0,0,0),rgb(180,180,180),"jamie is not so awesome")
rem background
make object sphere 1,1
do
text 10,10,str$(scancode())
text 10,20,str$(tbcur)
text 10,30,str$(tbcur2)
text 10,40,str$(LEFTTIMER)
text 10,50,str$(RIGHTTIMER)
tb()
loop
function tb()
rem set font
set text font "ariel":set text to bold:set text size 14
rem needed for clik and drag selection
if mouseclick()=1 then inc mtimer else mtimer=0
rem needed for backspace
if keystate(14) or keystate(211) or (controlkey()=1 and keystate(45) )then bksptimer=bksptimer+timer()-ltime else bksptimer=90
rem check all tb
tb as dword
for tb=1 to (array count(tba(0)))
rem visible?
if tba(tb).flag > 0
rem selection
if mouseclick()=1
rem if mousekey not held down
if mtimer<2
rem needed for clik and drag selection-gets original mousex pos
tbselm=mousex()
rem if mouse over tb
if mousey()>tba(tb).top and mousey()<tba(tb).bottom and mousex()>tba(tb).left and mousex()<tba(tb).right
rem if selecting new box rest cursor position
if tb<>tbsel then tbsel=tb:tbcur=0:tbcur2=0
rem position cursor(s)
if tbsel>0
d=999999999
for c=0 to len(tba(tb).content$)
x=abs(mousex()-(tba(tb).left+1+text width(left$(tba(tb).content$,c))))
if d>x
d=x:tbcur=c
rem if shift key is held dont move cursor 2
if shiftkey()=0 then tbcur2=c
else
exit
endif
next c
endif
else
rem needed for unselection
z=z+1
endif
else
rem clik and drag selection
rem note cursr2 is positioned in the above loop whic does the "clik" this only does the "drag"
if tbsel=tb
rem only if mouse has move 5 horizontal pix since last
if abs(mousex()-tbselm)>5
tbselm = mousex()
d=999999999
rem position cursor1
for c=0 to len(tba(tb).content$)
x=abs(mousex()-(tba(tb).left+1+text width(left$(tba(tb).content$,c))))
if d>x
d=x:tbcur=c
else
exit
endif
next c
endif
endif
endif
endif
rem draw box(s)
rem border
if tba(tb).flag = 3 then box tba(tb).left-1,tba(tb).top-1,tba(tb).right+1,tba(tb).bottom+1,tba(tb).borcol,tba(tb).borcol,tba(tb).borcol,tba(tb).borcol
rem text box
if tba(tb).flag > 1 then box tba(tb).left,tba(tb).top,tba(tb).right,tba(tb).bottom,tba(tb).tlcol,tba(tb).trcol,tba(tb).blcol,tba(tb).brcol
rem if selected?
if tb = tbsel
ink tba(tb).selcol,0
rem cursor
inc tbcur3
if tbcur3 >80 then tbcur3 =0
if tbcur3 <40 then box text width(left$(tba(tb).content$,tbcur))+tba(tb).left,tba(tb).top+1,text width(left$(tba(tb).content$,tbcur))+1+tba(tb).left,tba(tb).bottom-1
if tbcur2>tbcur then box text width(left$(tba(tb).content$,tbcur))+1+tba(tb).left,tba(tb).top+2,text width(left$(tba(tb).content$,tbcur2))+tba(tb).left,tba(tb).bottom-2
if tbcur>tbcur2 then box text width(left$(tba(tb).content$,tbcur2))+tba(tb).left,tba(tb).top+2,text width(left$(tba(tb).content$,tbcur))+tba(tb).left,tba(tb).bottom-2
endif
rem text
ink tba(tb).fontcol,0:text tba(tb).left+1,tba(tb).top+1,tba(tb).content$
endif
next tb
rem copy and cut
if tbsel>0
if controlkey()=1 and (keystate(45) or keystate(46)) and copytimer>150
copytimer=0
if tbcur<tbcur2
write to clipboard left$(right$(tba(tbsel).content$,len(tba(tbsel).content$)-tbcur),tbcur2-tbcur)
else
write to clipboard left$(right$(tba(tbsel).content$,len(tba(tbsel).content$)-tbcur2),tbcur-tbcur2)
endif
endif
rem editing
if tbsel>0
rem paste
if controlkey()=1 and keystate(47)=1 and pastetimer>150
pastetimer=0
if tbcur<=tbcur2
tba(tbsel).content$=left$(tba(tbsel).content$,tbcur)+get clipboard$()+right$(tba(tbsel).content$,len(tba(tbsel).content$)-tbcur2):tbcur2=tbcur+len(get clipboard$()):tbcur=tbcur+len(get clipboard$())
else
tba(tbsel).content$=left$(tba(tbsel).content$,tbcur2)+get clipboard$()+right$(tba(tbsel).content$,len(tba(tbsel).content$)-tbcur):tbcur=tbcur2+len(get clipboard$()):tbcur2=tbcur2+len(get clipboard$())
endif
endif
if tbcur=tbcur2
if bksptimer>100 and tbcur>0 then bksptimer=0: tba(tbsel).content$=left$(tba(tbsel).content$,tbcur-1)+right$(tba(tbsel).content$,len(tba(tbsel).content$)-tbcur2):tbcur=tbcur-1:tbcur2=tbcur
endif
if tbcur<=tbcur2
if bksptimer>100 and tbcur>0 then bksptimer=0: tba(tbsel).content$=left$(tba(tbsel).content$,tbcur)+right$(tba(tbsel).content$,len(tba(tbsel).content$)-tbcur2):tbcur2=tbcur
if entry$(1)<>"" and controlkey()=0 then tba(tbsel).content$=left$(tba(tbsel).content$,tbcur)+entry$(1)+right$(tba(tbsel).content$,len(tba(tbsel).content$)-tbcur2):tbcur2=tbcur+len(entry$(1)):tbcur=tbcur+len(entry$(1))
else
if bksptimer>100 and tbcur>0 then bksptimer=0: tba(tbsel).content$=left$(tba(tbsel).content$,tbcur2)+right$(tba(tbsel).content$,len(tba(tbsel).content$)-tbcur):tbcur=tbcur2
if entry$(1)<>"" and controlkey()=0 then tba(tbsel).content$=left$(tba(tbsel).content$,tbcur2)+entry$(1)+right$(tba(tbsel).content$,len(tba(tbsel).content$)-tbcur):tbcur=tbcur2+len(entry$(1)):tbcur2=tbcur2+len(entry$(1))
check_size:
if text width(tba(tbsel).content$)>tba(tbsel).right-tba(tbsel).left-2 then tba(tbsel).content$=left$(tba(tbsel).content$,len(tba(tbsel).content$)-1):goto check_size
endif
endif
rem unselect if not cliked on any boxes
if z=array count(tba(0)) then tbsel=0:tbcur=0:tbcur2=0
clear entry buffer
rem key shortcuts
rem tab between boxes note needs contol key or entry$ makes invisible characters
if keystate(15)=1 and controlkey()=1 then tabtimer=tabtimer+timer()-ltime else tabtimer=90
if tabtimer>100
inc tbsel:tabtimer=0
if tbsel>array count(tba(0)) then tbsel=1
tbcur=len(tba(tbsel).content$):tbcur2=tbcur
endif
rem only if tb selected
if tbsel>0
rem ctrl+a select all
if keystate(30)=1 and controlkey()=1 then tbcur2=0:tbcur=len(tba(tbsel).content$)
rem step cursor(s)
if lefttimer>100 and shiftkey()=0 then tbcur=tbcur-1 : tbcur2=tbcur:lefttimer=0
if lefttimer>100 and shiftkey()=1 then tbcur=tbcur-1 : lefttimer=0
if righttimer>100 and shiftkey()=0 then tbcur=tbcur+1 : tbcur2=tbcur:righttimer=0
if righttimer>100 and shiftkey()=1 then tbcur=tbcur+1 : righttimer=0
endif
endif
rem cursor paremeters
if tbcur>4200000000 then tbcur = 0
if tbcur2>4200000000 then tbcur2 = 0
if tbcur>len(tba(tbsel).content$) then tbcur = len(tba(tbsel).content$)
if tbcur2>len(tba(tbsel).content$) then tbcur2 = len(tba(tbsel).content$)
if leftkey()=1 then lefttimer=lefttimer+timer()-ltime else lefttimer=90
if rightkey()=1 then righttimer=righttimer+timer()-ltime else righttimer=90
if controlkey()=1 and keystate(47)=1 then pastetimer=pastetimer+timer()-ltime else pastetimer=140
if controlkey()=1 and (keystate(45)=1 or keystate(46)=1) then copytimer=copytimer+timer()-ltime else copytimer=140
ltime=timer()
endfunction
function maketb(flag,left,top,right,bottom,tlcol,trcol,blcol,brcol,borcol,fontcol,selcol,content$)
array insert at bottom tba(0)
tba(array count(tba(0))).flag=flag
tba(array count(tba(0))).top=top
tba(array count(tba(0))).left=left
tba(array count(tba(0))).right=right
tba(array count(tba(0))).bottom=bottom
tba(array count(tba(0))).tlcol=tlcol
tba(array count(tba(0))).trcol=trcol
tba(array count(tba(0))).blcol=blcol
tba(array count(tba(0))).brcol=brcol
tba(array count(tba(0))).borcol=borcol
tba(array count(tba(0))).fontcol=fontcol
tba(array count(tba(0))).selcol=selcol
tba(array count(tba(0))).content$=content$
endfunction
Works:
multiple text boxs
click and drag selection
arrowkeys move cursor
shift and arrowkeys change selection
ctrl+a/x/c/v do stuff
text boxes easily editable (just change array data)
ctrl tab between them (pressing tab makes invisible character when ctrl not pressed down)-if anyone can fix this be my guest..
Its rather...massive for a simple function so any reductions appreiciated.
Also apologies not very well rem'ed,any Q's just ask...
P.s if this has been done before forgive my nooblyness
edit: added 2 lines so text boxs dont "overflow"
check_size:
if text width(tba(tbsel).content$)>tba(tbsel).right-tba(tbsel).left-2 then tba(tbsel).content$=left$(tba(tbsel).content$,len(tba(tbsel).content$)-1):goto check_size
hi