textblock

Blitz3D Forums/Blitz3D Beginners Area/textblock

blade007(Posted 2008) [#1]
I was working on a code to make textblocks in Blitz3D, and now im stumped. I need help moving the cursor and selecting text. Also, How do you convert an ASCII to a scancode. Thanks in advance!
Graphics 800,600,32,2
Global pointer = CreateImage(12,20)
SetBuffer ImageBuffer(pointer)
	;fill
	Color 255,255,255
	For mainarrow = 0 To 10
		Line 0,mainarrow,mainarrow,mainarrow
	Next
	For bottom = 0 To 5
		Line 0,(15-bottom),bottom,(15-bottom)
	Next
	For stick = 0 To 1
		Line 5+stick,11,8+stick,18
	Next
	;outline
	Color 1,1,1
	Line 0,0,0,15
	Line 1,0,11,10
	Line 11,10,7,10
	Line 7,11,10,18
	Line 0,15,4,11
	Line 4,11,7,18
	Line 8,19,9,19
	
SetBuffer BackBuffer()	
ClsColor 235,233,237
Type textfield
	Field x,y
	Field x2,y2
	Field selected
	Field drawborder
	Field strng$
End Type

textfield.textfield = New textfield
textfield\x = 10
textfield\y = 20
textfield\x2 = 200
textfield\y2 = 500
textfield\drawborder = True
textfield\strng$ = "This is a text box. You can enter words in here. It's quiet simple actually... Isn't this much better that input"

textfield.textfield = New textfield
textfield\x = 210
textfield\y = 20
textfield\x2 = 400
textfield\y2 = 500
textfield\drawborder = True
textfield\strng$ = "This is another box, but it doesn't work..."

Global keyboardcursor,cursorx,cursory
Global blink,blinkdelay = MilliSecs()
Global keypress,keyhold,lastpress = 2
Global lastlength,last2legth
While Not KeyDown(1)
	Color 0,0,0
	Text 0,0,"Text box example..."
	UpdateWindowsElements()
	Delay 6
	Flip
	Cls
Wend

Dim lettersperline(100)

Function UpdateWindowsElements()

		lastpress = keypress
		keypress = GetKey()
		If lastpress = keypress And KeyDown(lastpress) ;the if statement is supposed to enable repeated keystrokes on hold but it doesn't work
			DebugLog "It works!"
		Else
			keyhold = 0
		EndIf
	For textfield.textfield = Each textfield
		If textfield\drawborder = True
			Color 255,255,255
			Rect textfield\x,textfield\y,textfield\x2-textfield\x,textfield\y2-textfield\y,1
			Color 167,166,170
			Rect textfield\x,textfield\y,textfield\x2-textfield\x,textfield\y2-textfield\y,0
			Color 1,1,1
			;Detect the enter key
			brokenline$ = textfield\strng$
			letters = 0
			last2legth = 0
			lastlength = 0
			lines = 0
			
			For enterkey = 1 To Len(textfield\strng$)
				letters = letters + 1
				If Asc(Mid$(textfield\strng$,enterkey)) = 13 Or letters*FontWidth() > textfield\x2-textfield\x-FontWidth()
					If Asc(Mid$(textfield\strng$,enterkey)) = 13
						fragementline$ = Left$(brokenline$,(letters-1))
					Else
						fragementline$ = Left$(brokenline$,letters)
					EndIf
					brokenline$ = Right$(brokenline$,Len(brokenline$)-letters)
					Text textfield\x+1,textfield\y+1+(FontHeight()*lines),fragementline$
					last2legth = lastlength
					lastlength = letters
					letters = 0
					lines = lines + 1
				EndIf
			Next
			Text textfield\x+1,textfield\y+1+(FontHeight()*lines),brokenline$	
		EndIf
		If MouseDown(1)
			If MouseX() >= textfield\x And MouseY() >= textfield\y And MouseX() <= textfield\x2 And MouseY() <= textfield\y2
				textfield\selected = True
				cursorx = textfield\x+1
				cursory = textfield\y+1
			Else
				textfield\selected = False
			EndIf
		EndIf
		If textfield\selected = True
			If keypress > 0
				If cursorx > textfield\x2-textfield\x ;auto wrap (right side)
					cursorx = textfield\x+1
					cursory = textfield\y+1+((lines)*FontHeight())
				EndIf
				If cursorx < textfield\x+1 ;auto wrap (left side)
					cursorx = textfield\x2-textfield\x
					cursory = textfield\y+1+((lines)*FontHeight())
				EndIf

				
				Select keypress
					Case 8 ;backspace
						If Len(textfield\strng$) > 0
							If Asc(Right$(textfield\strng$,1)) = 13
								cursory = cursory - FontHeight()
								textfield\strng$ = Left$(textfield\strng$,Len(textfield\strng$)-1)
								cursorx = textfield\x+1+(FontWidth()*last2legth)
							Else
								cursorx = cursorx - FontWidth()
								textfield\strng$ = Left$(textfield\strng$,Len(textfield\strng$)-1)
							EndIf
							
							If cursorx < textfield\x ;auto wrap (left side)
								cursorx = (textfield\x2-(textfield\x+1));-(FontWidth()*1)
								cursory = textfield\y+1+((lines-1)*FontHeight())
							EndIf
						EndIf
					Case 13 ;enter
						textfield\strng$ = textfield\strng$ + Chr$(keypress)
						cursorx = textfield\x+3
						cursory = cursory + FontHeight()
						
					Case 31 ;left arrow key
						cursorx = cursorx - FontWidth()
						
					Case 30 ;right arrow key
						cursorx = cursorx + FontWidth()
						
					Case 28 ;up arrow key
						cursory = cursory - FontHeight()
						
					Case 29 ;down arrow key
						cursory = cursory + FontHeight()
						
					Default
						textfield\strng$ = textfield\strng$ + Chr$(keypress)
						cursorx = cursorx + FontWidth()
				End Select
				
				blink = 0
				blinkdelay = MilliSecs()
			EndIf
			
			If blink = 0 Then Color 0,0,0 Line cursorx,cursory,cursorx,cursory+FontHeight()
			If MilliSecs() - blinkdelay >= 500
				blinkdelay = MilliSecs()
				Select blink
					Case 0
						blink = 1
					Case 1
						blink = 0
				End Select
			EndIf
		EndIf
	Next
	DrawImage pointer,MouseX(),MouseY()
End Function



stayne(Posted 2008) [#2]
Might be something here for you...

http://www.blitzbasic.com/codearcs/codearcs.php?code=2022