Multi-line DrawText/FryGUI adventures

BlitzMax Forums/BlitzMax Programming/Multi-line DrawText/FryGUI adventures

wmaass(Posted 2010) [#1]
First of all let me say that I love FryGUI.

I was happily putting together an app for a client and a request came in that they wanted the ability to "add some notes" as part of their app.

So I thought, no bid deal I'll just drop in a FryGUI gadget that works like an auto-wrapping text area. To my dismay there was no such gadget as far as I could tell.

So then I thought, "Well, I'll just use DrawText()". Well as most of you know it lacks multi-line, auto-wrapping goodness as well.

Thankfully, some cool dude made this:

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

So I combined that with a FryGUI canvas gadget and now I have my "notes" thingy for my important client. I added a not so nifty non-blinking cursor to the above code archive entry, so I thought I would share.



Here is blubberwasser’s function:


Function DrawMultilineText:Int( text:String, x:Int, y:Int, lineWidth:Int = 0, cWidth:Int = 0, cHeigth:Int = 0, autowrap:Byte = 0)

	Local linePos:Int =0
	Local textPos:Int = 0
	
	Local width:Int = 0
	Local height:Int = TextHeight( "A" )

	For Local pos:Int = 1 To text.length
	
		Local character:Byte = Asc( Mid( text, pos, 1 ) )
		'Print character
		
		width = TextWidth( Chr( character ) )
		
		If ( textPos / ( cWidth + 1 ) ) >= lineWidth And autowrap = 1
			linePos:+ 1
			textPos = 0
			
		EndIf
		
			
		If character = 13	'break
			linePos:+1
			textPos = 0
			
		Else
			DrawText Chr( character ), x + textPos, y + linePos * ( height + cHeigth)
			textPos:+ ( width + cWidth )		
		EndIf
	
	Next
	
	Return linePos

EndFunction



and here is my DrawToCanvas() function. Note that it supports a max number of characters and or lines so you need some variables such as:

Global x:Int = 0
Global ds:String = ""
Global numchars:Int = 0
Global nextline:Int = 0
Global boxwidth:Int=20
Global maxLines:Int = 26
Global maxChars:Int = 1500





Function DrawToCanvas()
	
	Local lines:Int = DrawMultilineText(ds,10,5,150,2,0,1)
	
	x = GetChar()
	
	If x > 8
		If numchars < maxChars And lines < maxLines
			ds = Replace(ds,"_","")
			ds=ds + Chr(x) + "_"
			numchars = numchars + 1
			
		EndIf
	Else
		
	
		If x = 8
			ds = Replace(ds,"_","")
			ds = Left(ds, Len(ds)-1) + "_"
			numchars = numchars - 1
			
		EndIf
	EndIf
End Function



BTW I would love to figure out how to make it scroll vertically when the lines exceed the height of the canvas...