News:

Building a 3D Ray Tracer  By stevmjon

Main Menu

Inputbox for PlayBasic

Started by Adaz, November 16, 2006, 09:03:41 AM

Previous topic - Next topic

Adaz

Hi,

At least:) this is a one-line inputbox PSUB with scrolling and editing feature.

It's the first version, but fully functional.

It took 1 hour to write so it can be improved though :)

Usage:
inputbox(text$,ix,iy,iw,ih,bgcolor,bordercolor)

text$ = the text you wish to edit, or can be empty
ix,iy = the top left corner of the textbox
iw,ih = the width and height of the textbox
bgcolor, bordercolor = the background and border color of the textbox



Download InputBox Source Code form PlayBASIC.com


Ádáz

Hungary

kevin

#1
 Seems to work well!






PlayBASIC Code: [Select]
; PROJECT : Inputbox2
; AUTHOR : ?d?z
; CREATED : 2006. 12. 02.
; EDITED : 2006. 12. 02.
; MADE W/ : PlayBasic 1.59
; ---------------------------------------------------------------------

; Including CLIPBOARD.PBA by Kevin Picone (MODIFIED - not to emtpy clipboard contents)

;
; USAGE:
;
; inputtext$=inputbox(xpos,ypos,width,height,textcolor)
;
; You MUST always use "inputtext$" and no other string!
;
; You can now select text with SHIFT + Left / Right!
; Copy / paste to/from Windows clipboard with CTRL+C / CTRL+V !
;
; In this version the other routines of the program don't halt while we edit the text
; You can draw background, move sprites or anything, AND check the entered text in every frame,
; or deliberately finish the editing any time by setting the finished variable to True
;
; Feel free to send me questions, suggestions or bug reports: adaz@fw.hu


SetFPS 40

Global maxlength=100 ;the maximum length of inputbox (you can set this later for each inputboxes)
Global inputtext$ ;the placeholder for the actual text, it must have global scope
Global finished=True ;while this variable is False, a text editing is in progress
Global curscolor=RGB(45,45,45)
Global addcolor=RGB(15,15,15)


LoadFont "Arial",1,16,1
SetFont 1



;In this example there will be 3 individual inputboxes.
;Each MUST use "inputtext$" variable, then you can save its contents to your own variable (e.g.text1$)

inputtext$="PlayBasic" ;you can set the text to be edited beforehand
Repeat
drawbackground()
inputtext$=inputbox(100,100,300,20,RGB(255,255,255))
Sync
Until finished
text1$=inputtext$ ;we save the contents here



inputtext$="Inputbox 2.0" ;we can set the next editable text (or empty)
Repeat
drawbackground()
inputtext$=inputbox(100,150,400,20,RGB(255,255,255))
Sync
Until finished
text2$=inputtext$ ;we save the contents here



inputtext$="by Adaz" ;we can set the next editable text (or empty)
Repeat
drawbackground()
inputtext$=inputbox(100,200,200,20,RGB(255,255,255))
Sync
Until finished
text3$=inputtext$ ;we save the contents here

inputtext$="" ;we are nice people so we empty it :)




;now we can use the user edited texts for our secret purposes:)
Cls RGB(0,80,130)
Print "The fields are:"
Print ""
Print text1$
Print text2$
Print text3$

Sync : WaitNoKey: WaitKey




Psub drawbackground()
Ink Rnd(RGB(255,255,255)): Circle Rnd(640),300+Rnd(200),Rnd(100),Rnd(1)
EndPsub


Psub inputbox(ix,iy,iw,ih,textcolor)
Dim keys(255)
Ink textcolor
cur=True
curscolor=curscolor+addcolor
If curscolor=RGB(45,45,45) Or curscolor=RGB(255,255,255): addcolor=-addcolor: EndIf
text$=inputtext$
If Len(text$)>maxlength: text$=Left$(text$,maxlength): EndIf

curx=GetTextWidth(Left$(text$,curpos))
If finished
finished=False
textx=0: curpos=Len(text$)
If curx+textx>iw-6
textx=iw-6-GetTextWidth(Left$(text$,curpos))
EndIf
EndIf

ScreenViewPort ix,iy,ix+iw,iy+ih+1
BoxC ix,iy,ix+iw,iy+ih,True,RGB(205,125,0)
BoxC ix,iy,ix+iw,iy+ih,False,RGB(0,205,255)

i$=Inkey$(): inkey=Asc(i$): KeyBoardState keys()

If Not keys(47): lastclipboard$="": EndIf

If keys(29) And keys(46) ;ctrl + c
If selstart
TextToClipBoard(Mid$(text$,selstart,selend-selstart+1))
EndIf
Goto label1
EndIf
If keys(29) And keys(47);ctrl + v
toclipboard$=GetTextFromClipBoard()
iflonger=Len(text$)+Len(toclipboard$)
If iflonger>maxlength: toclipboard$=Left$(toclipboard$,maxlength-Len(text$)): EndIf
If toclipboard$<>"0" And toclipboard$<>lastclipboard$
If selstart
text$=Left$(text$,selstart-1)+Mid$(text$,selend+1,maxlength)
curpos=selstart-1
EndIf
selstart=0
text$=Insert$(text$,toclipboard$,curpos)
curpos=curpos+Len(toclipboard$)
curx=GetTextWidth(Left$(text$,curpos))
If curx+textx>iw-6
textx=textx-GetTextWidth(toclipboard$)
EndIf
If curx+textx<6
textx=GetTextWidth(Left$(text$,curpos))
If textx>0: textx=0: EndIf
EndIf
lastclipboard$=toclipboard$
EndIf
Login required to view complete source code

Adaz

Thank you very much :)
I'll improve it by adding ctrl+left/right word jumping and select/cut/paste text.

Ádáz

Hungary