TGC Codebase Backup



Multi line text input routine by jasuk70

6th May 2004 18:52
Summary

This is a multi-line input routine that returns a single string containing all the data from the input box.



Description

I've needed a multi-line input routine for quite a while and after many failed attempts i've come up with this one, it's quite fast and the code is a bit messy. You can use it as a basis to modify and make your own versions of this.

Jas



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    SET TEXT FONT "Courier New"
SET TEXT SIZE 15
GLOBAL fontWidth  AS INTEGER
GLOBAL fontHeight AS INTEGER
#CONSTANT MaxTextBoxWidth 100
#CONSTANT MaxTextBoxHeight 100
fontWidth=TEXT WIDTH("Q")
fontHeight=TEXT HEIGHT("Q")

GLOBAL DIM TextBoxArray(MaxTextBoxWidth,MaxTextBoxHeight) AS INTEGER
test AS STRING

test=TextBoxInput(10,10,40,20,"")
CLS
PRINT ">";test;"<"
WAIT KEY

function TextBoxInput(startX AS INTEGER,startY AS INTEGER,numCols AS INTEGER,numRows AS INTEGER, originalText AS STRING)
	LOCAL posX AS INTEGER
	LOCAL posY AS INTEGER
	LOCAL endX AS INTEGER
	LOCAL endY AS INTEGER
	LOCAL char AS STRING
	LOCAL key AS STRING
	LOCAL finalString AS STRING
	
` Set up array to spaces 

	ClearTextBox()
		
` Add converstion routine for previous text below


` Set up text styles and colours	
	SET TEXT OPAQUE
	INK RGB(255,255,255),0

` Display contents of window

	RefreshTextBox(startX, startY, numCols, numRows)

` Main loop
	posX=1
	posY=1
	
	do
		`Display the cursor over current square
		SET TEXT TRANSPARENT
		TEXT startX+(posX*fontWidth),StartY+(posY*fontHeight),"_"
		SET TEXT OPAQUE
		
		if RETURNKEY()
			EXIT
		endif
		
		if LEFTKEY()
			char=CHR$(TextBoxArray(posX,posY))
      	TEXT startX+(posX*fontWidth),StartY+(posY*fontHeight),char
      	posX=posX-1
      	if posX<1
      		posX=numCols
      		posY=posY-1
      		if posY<1
      			posY=numRows
      		endif
      	endif
			SET TEXT TRANSPARENT
			TEXT startX+(posX*fontWidth),StartY+(posY*fontHeight),"_"
			SET TEXT OPAQUE
      	while LEFTKEY()
      		
      	endwhile
		endif

		if RIGHTKEY()
			char=CHR$(TextBoxArray(posX,posY))
      	TEXT startX+(posX*fontWidth),StartY+(posY*fontHeight),char
      	posX=posX+1
      	if posX>numCols
      		posX=1
      		posY=posY+1
      		if posY>numRows
      			posY=1
      		endif
      	endif
			SET TEXT TRANSPARENT
			TEXT startX+(posX*fontWidth),StartY+(posY*fontHeight),"_"
			SET TEXT OPAQUE
      	while RIGHTKEY()
      		
      	endwhile
		endif
		
		if UPKEY()
			char=CHR$(TextBoxArray(posX,posY))
      	TEXT startX+(posX*fontWidth),StartY+(posY*fontHeight),char
      	posY=posY-1
      	if posY<1
      		posY=numRows
      	endif
			SET TEXT TRANSPARENT
			TEXT startX+(posX*fontWidth),StartY+(posY*fontHeight),"_"
			SET TEXT OPAQUE
      	while UPKEY()
      		
      	endwhile
		endif

		if DOWNKEY()
			char=CHR$(TextBoxArray(posX,posY))
      	TEXT startX+(posX*fontWidth),StartY+(posY*fontHeight),char
      	posY=posY+1
      	if posY>numRows
      		posY=1
      	endif
			SET TEXT TRANSPARENT
			TEXT startX+(posX*fontWidth),StartY+(posY*fontHeight),"_"
			SET TEXT OPAQUE
      	while DOWNKEY()
      		
      	endwhile
		endif

		key=ENTRY$()
		key=MID$(key,1)
		` Backspace
      if ASC(key)=8
      	char=CHR$(TextBoxArray(posX,posY))
      	TEXT startX+(posX*fontWidth),StartY+(posY*fontHeight),char
      	posX=posX-1
      	if posX<1
      		posX=numCols
      		posY=posY-1
      		if posY<1
      			posY=numRows
      		endif
      	endif
      	TextBoxArray(posX,posY)=32
      	TEXT startX+(posX*fontWidth),StartY+(posY*fontHeight)," "
      endif
      
      `Normal keypress
      if key<>"" AND ASC(key)>31
      	TextBoxArray(posX,posY)=ASC(key)
      	TEXT startX+(posX*fontWidth),StartY+(posY*fontHeight),key
      	posX=posX+1
      	if posX>numCols
      		posX=1
      		posY=posY+1
      		if posY>numRows
      			posY=1
      		endif
      	endif
      endif
		CLEAR ENTRY BUFFER
	loop

	CLEAR ENTRY BUFFER
	
	endX=0
	endY=0
	` work out end of string
	for posY=numRows TO 1 STEP -1
		for posX=numCols TO 1 STEP -1
			if TextBoxArray(posX,posY)<>32 
				if endX=0
					endX=posX
					endY=posY
				endif
			endif
		next posX
	next posY
	
	` Generate final string
	finalString=""
	for posY=1 TO endY-1
		for posX=1 TO numCols
			finalString=finalString+CHR$(TextBoxArray(posX,posY))
		next posX
	next posY
	for posX=1 TO endX
		finalString=finalString+CHR$(TextBoxArray(posX,endY))
	next posX
	SET TEXT TRANSPARENT
endfunction finalString

function ClearTextBox()
	LOCAL posX AS INTEGER
	LOCAL posY AS INTEGER
	
	for posX=1 TO MaxTextBoxWidth
		for posY=1 TO MaxTextBoxHeight
			TextBoxArray(posX,posY)=32
		next posY
	next posX
endfunction

function RefreshTextBox(startX AS INTEGER, startY AS INTEGER, numCols AS INTEGER, numRows AS INTEGER)
	LOCAL posX AS INTEGER
	LOCAL posY AS INTEGER
	LOCAL code AS INTEGER
	LOCAL char AS STRING
	
	for posY=1 TO numRows
		for posX=1 TO numCols
			code=TextBoxArray(posX,posY)
			char=CHR$(code)
			TEXT startX+(posX*fontWidth),StartY+(posY*fontHeight),char
		next posX
	next posY
		
endfunction