InputBoxes Lib

Blitz3D Forums/Blitz3D Programming/InputBoxes Lib

Chispon(Posted 2014) [#1]
I was kind of bored, and when you don't even have a girlfriend. What else could you do?. Yesh, Programming! LOL

I made this little "Library", for the purpose of learning and using it later in future projects.

You can create all the Inputboxes you want, hide and re-show them, get the text from them, etc.

I will add better comments later, now I'm a bit tired.

If you learned something from it or are planning to using it, please let me know :)

Some feedbacks and what could I do to improve it will be appreciated.

Thanks!

AppTitle "Inputboxes Test by Chispon"
Graphics 640, 480, 32, 2
SetBuffer BackBuffer()

SetFont LoadFont("Arial", 16)

Global mx
Global my
Global mh1
Global ibox_time
Global ibox_active = -1
Global ibox_c$ = ""
Global test_txt$ = "Hello!"

Type input_box
	Field x
	Field y
	Field w
	Field h
	Field desc$
	Field txt$
	Field limit
	;Field lim
	Field id
	Field hide
	Field img
End Type

For i = 0 To 9
	CreateInputBox(100, 40 + i * 40, i, 100, 64, "ID(" + i + ") Test: ")
	
	If Rand(0, 1) = 0 Then HideInputBox(i)
Next

timer = CreateTimer(30)

While Not KeyDown(1)
	WaitTimer(timer)
	
	mx = MouseX()
	my = MouseY()
	mh1 = MouseHit(1)
	
	;get the data from a inputbox
	If KeyDown(59) Then
		test_txt$ = GetInputBoxText(1)
	EndIf
	
	;just for test, F2 to reshow all boxes
	If KeyDown(60) Then
		For i = 0 To 9
			ShowInputBox(i)
		Next
	EndIf
	
	UpdateInputBoxes()
	
	;draw the text of the box you get the data from
	Text 400, 100, test_txt$
	
	Flip
	
	ClsColor 128, 128, 128
	
	Cls
Wend

End

Function HideInputBox(id)
	For ib.input_box = Each input_box
		If id = ib\id Then ib\hide = 1
	Next
End Function

Function ShowInputBox(id)
	For ib.input_box = Each input_box
		If id = ib\id Then ib\hide = 0
	Next
End Function

Function GetInputBoxText$(id)
	For ib.input_box = Each input_box
		If id = ib\id Then Return ib\txt$
	Next
End Function

Function CreateInputBox(x, y, id, width = 100, limit = 64, desc$ = "")
	ib.input_box = New input_box
	ib\x = x
	ib\y = y
	ib\w = width ;StringWidth("A") * limit + 10
	ib\h = FontHeight() + 2
	ib\desc$ = desc$
	ib\limit = limit
	;ib\lim = 0
	ib\img = CreateImage(ib\w, ib\h)
	ib\id = id
End Function

Function UpdateInputBoxes()
	Local key
	;Local swidth
	
	For ib.input_box = Each input_box
		If ib\hide = 0 Then 
			If RectsOverlap(mx, my, 1, 1, ib\x, ib\y, ib\w, ib\h) Then
				If mh1 = 1 Then FlushKeys(): ibox_active = ib\id
			EndIf
			
			If ib\id = ibox_active Then
				ibox_time = ibox_time + 1
			
				If ibox_time < 15 Then ibox_c$ = "|" Else ibox_c$ = ""
				If ibox_time > 30 Then ibox_time = 0
				
				key = GetKey()
				
				If key > 31 And key < 128 Then
					If Len(ib\txt$) < ib\limit Then ib\txt$ = ib\txt$ + Chr(key)
				EndIf
				
				;supr or del key
				If key = 8 Or key = 4 Then
					If Len(ib\txt$) > 0 Then ib\txt$ = Left(ib\txt$, Len(ib\txt$) - 1)
				EndIf
				
				;enter key
				If key = 13 Then ibox_active = -1
			Else
				ibox_c$ = ""
			EndIf
			
			Color 255, 255, 255
			Rect ib\x, ib\y, ib\w, ib\h
			Text ib\x, ib\y - ib\h, ib\desc$
			
			If StringWidth(ib\txt$ + "|") < ib\w Then
				Color 0, 0, 0
				Text ib\x + 4, ib\y, ib\txt$ + ibox_c$
			Else
				SetBuffer ImageBuffer(ib\img)
					Color 255, 255, 255
					Rect 0, 0, ib\w, ib\h
					
					Color 0, 0, 0
					Text ib\w - StringWidth(ib\txt$ + "|") - 1, 0, ib\txt$ + ibox_c$
				SetBuffer BackBuffer()
				
				DrawImage ib\img, ib\x, ib\y
			EndIf
			
			Color 128, 128, 128
			Rect ib\x + 1, ib\y + 1, ib\w - 1, ib\h - 1, 0
			
			Color 0, 0, 0
			Rect ib\x, ib\y, ib\w, ib\h, 0
			
			;failed attemp :(
			
			;swidth = StringWidth(Right(ib\txt$ + "|", ib\lim)) ;StringWidth(ib\txt$ + "|")
			;
			;If StringWidth(Right(ib\txt$ + "|", ib\lim)) > ib\w Then
			;	Color 0, 0, 0
			;	Text ib\x + 4, ib\y, Right(ib\txt$, ib\lim) + ibox_c$
			;	
			;	ib\lim = Len(Right(ib\txt$ + "|", ib\lim))
			;	If StringWidth(Right(ib\txt$ + "|", ib\lim)) > ib\w Then ib\lim = Len(ib\txt$ + "|")
			;Else
			;	Color 0, 0, 0
			;	Text ib\x + 4, ib\y, ib\txt$ + ibox_c$
			;	
			;	ib\lim = Len(ib\txt$ + "|")
			;EndIf
		EndIf
	Next
End Function