Code archives/User Input/Input in Graphics Mode

This code has been declared by its author to be Public Domain code.

Download source code

Input in Graphics Mode by Tibit2005
Here is two handy input functions.

Works as input from Blitz3D but it does NOT stop the program. Displays the text you write.

Second input works just as the "old" input, waits for you to write a text (and display that text), continues when you press enter.

Both require graphics mode before they can be used.

NOTE: Before you use them or makemods change the names of the functions to suit your style.

'MAKE MODULE
If you want to use them as normal commands then you have to make a module out of this file. Simple uncomment the four lines at top (see below) and UNcomment the demonstration. Then create a folder like this -> BlitzMax\mod\pub.mod\input.mod\
in this directory put the code below, name it input.bmx. Now go into BlitzMax\Bin\ in the cmd prompt
Write: bmk makemods pub.input
'Strict

Rem 
bbdoc: Input in GraphicsMode
End Rem 

' Uncomment the four lines below to use this as a module (you have to build it then)
'Module Pub.input

'ModuleInfo "Version: 1"
'ModuleInfo "Author: Wave~"
'ModuleInfo "License: Blitz Shared Source Code and Public Domain"

Import BRL.Max2D
Import BRL.Retro

Rem 
bbdoc: InputText works just as a normal input but in graphicsmode. It waits for you to press enter then returns a string.
endrem 
Function InputText$(Text$,X,Y)
Local Inp$
	Repeat
		Inp = TInput.Text(Text$,X,Y)
		Flip;Cls
	Until Inp <> ""

Return Inp
EndFunction

Rem 
bbdoc: InputText works just as a normal Textinput but it does NOT stop the program! Returns "" until ENTER is pressed then the message you have written is returned as a string.
endrem 
Function DynamicInput$(Text$,X,Y)
	Return TInput.Text(Text$,X,Y)
EndFunction

Private
Type TInput

	Global tempText$

	Function Text$(Text$,X,Y)
		
			Local aKeytoGet = GetChar()
			If aKeytoGet'Anykey was pressed
			
				If aKeytoGet = 13 'ENTER
					Text$ = tempText$
					If Text$ = "" Then Text = " "
					tempText$ = ""
					FlushKeys
					Return Text$
				Else If aKeytoGet = 8 Or aKeytoGet = 4 'AscII For Backspace And Delete
					If Len( tempText$ ) > 0 Then tempText$ = Left$( tempText$, Len(tempText$) -1 )	
				Else' If aKeytoGet>=32 And aKeytoGet<=122 And Len(Message)<52
					tempText$:+ Chr$(aKeytoGet)
				EndIf
	
			EndIf
			
			DrawText Text$ + tempText,X,Y
			Return ""

	EndFunction

EndType




'Rem 
'Shows the use of Both input methods!
'--------------------------------------------------------
	Graphics 300,70,0 'Graphicsmode is a MUST
	
	Local Name$ = InputText("Enter Your Name: ",10,10)	
	DrawText "Your Name was: "+Name$,30,30 ;Flip

	WaitKey()	

	Local X, Code$
	While Not KeyDown(Key_Escape)

		Code = DynamicInput$( Name+" enter code : ",10,10)
		If Upper(Code) = Upper("code")	Then DrawText "-- Correct Code! --",10,30;Flip;WaitKey() Else DrawText "Enter ~qCode~q ok?",10,30
		DrawRect X,50,40,5 ; X:+1 ; If X > GraphicsWidth() X = 0
		
	Flip;Cls
	Wend
'--------------------------------------------------------
'Good to have function
'EndRem

Comments

bradford62005
try this


Graphics 640,480,0

foo$ = gl_input$(10,10,"what is your name? ")

SetColor 255,255,0 ; SetScale(2,2) 

DrawText "hello "+foo$+", how are you?",100,100

Flip
WaitMouse

End 


'-------------------------------------
Function gl_input$(x,y,prompt$ = "?")

	Repeat
	
		Cls
		DrawText prompt$+m$,10,10
		DrawText key,10,50
		hit_key = 0
		For key = 1 To 226
		
			hit_key = KeyHit(key)
			If hit_key 
				m$ = m$ + Chr(key)
			
				If key = KEY_ENTER
					Return m$
				EndIf
				If key = KEY_BACKSPACE
					l = Len(m$)
					m = m[..l-2]
				EndIf
			EndIf 
		Next
	
 	Flip
	Until KeyHit(KEY_ESCAPE)

End Function



Ked2008
I added a little modification so that it has a cursor.
Strict

Rem 
bbdoc: Input in GraphicsMode
End Rem 

' Uncomment the four lines below to use this as a module (you have to build it then)
'Module Pub.input

'ModuleInfo "Version: 1"
'ModuleInfo "Author: Wave~"
'ModuleInfo "License: Blitz Shared Source Code and Public Domain"

Import BRL.Max2D
Import BRL.Retro

Rem 
bbdoc: InputText works just as a normal input but in graphicsmode. It waits for you to press enter then returns a string.
endrem 
Function InputText$(Text$,X,Y)
Local Inp$
	Repeat
		Cls
		Inp = TInput.Text(Text$,X,Y)
		Flip
		If KeyDown(KEY_ESCAPE) 
			Exit
		EndIf
	Until Inp <> ""

Return Inp
EndFunction

Rem 
bbdoc: InputText works just as a normal Textinput but it does NOT stop the program! Returns "" until ENTER is pressed then the message you have written is returned as a string.
endrem 
Function DynamicInput$(Text$,X,Y)
	Return TInput.Text(Text$,X,Y)
EndFunction

Private
Type TInput

	Global tempText$
	Global blinktimer:Int = Null
	Global show:Int = True

	Function Text$(Text$, X, Y) 
		If blinktimer = Null
			blinktimer = MilliSecs() 
		EndIf
		Local aKeytoGet = GetChar()
		If aKeytoGet'Anykey was pressed
			blinktimer = Null
			show = False	
			If aKeytoGet = 13 'ENTER
				Text$ = tempText$
				If Text$ = "" Then Text = " "
				tempText$ = ""
				blinktimer = Null
				show = True
				FlushKeys() 
				Return Text$
			ElseIf aKeytoGet = 8 Or aKeytoGet = 4 'AscII For Backspace And Delete
				If Len( tempText$ ) > 0 Then tempText$ = Left$( tempText$, Len(tempText$) -1 )	
			Else' If aKeytoGet>=32 And aKeytoGet<=122 And Len(Message)<52
				tempText$:+ Chr$(aKeytoGet)
			EndIf
		EndIf
		
		If MilliSecs() > blinktimer + 500
			If show = True	
				show = False
				blinktimer = MilliSecs() 
			Else
				show = True
				blinktimer = MilliSecs() 
			EndIf
		EndIf
		
		If show = True
			DrawText Text$ + tempText + "|", X, Y
		Else
			DrawText Text$ + tempText, X, Y
		EndIf
		
		Return ""
	EndFunction

EndType




'Rem 
'Shows the use of Both input methods!
'--------------------------------------------------------
	Graphics 640,480,0 'Graphicsmode is a MUST
	
	Local Name$ = InputText("Enter Your Name: ",10,10)	
	DrawText "Your Name was: "+Name$,30,30 ;Flip

	WaitKey()	

	Local X, Code$
	While Not KeyDown(Key_Escape)
	Cls
		Code = DynamicInput$( Name+" enter code : ",10,10)
		If Upper(Code) = Upper("code")	Then DrawText "-- Correct Code! --",10,30;Flip;WaitKey() Else DrawText "Enter ~qCode~q ok?",10,30
		DrawRect X,50,40,5 ; X:+1 ; If X > GraphicsWidth() X = 0
		
	Flip
	Wend
'--------------------------------------------------------
'Good to have function
'EndRem


EDIT: Now that I look this over, it might not work. :)


Blue Steel2008
Added ability to change Blink rate of cursor ( <0 = no cursor)

I noticed a problem of the cursor being Blinked on for longer than its blinked off ..

I thought it would blink off for the same length of time that its blinked on.

Can someone please tell me how to fix this

Strict

Rem 
bbdoc: Input in GraphicsMode
End Rem 

' Uncomment the four lines below to use this as a module (you have to build it then)
'Module Pub.input

'ModuleInfo "Version: 1"
'ModuleInfo "Author: Wave~ Modified by Ked and then by Blue Steel"
'ModuleInfo "License: Blitz Shared Source Code and Public Domain"

Import BRL.Max2D
Import BRL.Retro

Rem 
bbdoc: InputText works just as a normal input but in graphicsmode. It waits for you to press enter then returns a string.
endrem 
Function InputText$(Text$,X,Y,BlinkRate)
Local Inp$
	Repeat
		Cls
		Inp = TInput.Text(Text$,X,Y,BlinkRate)
		Flip
		If KeyDown(KEY_ESCAPE) 
			Exit
		EndIf
	Until Inp <> ""

Return Inp
EndFunction

Rem 
bbdoc: InputText works just as a normal Textinput but it does NOT stop the program! Returns "" until ENTER is pressed then the message you have written is returned as a string.
endrem 
Function DynamicInput$(Text$,X,Y,BlinkRate)
	Return TInput.Text(Text$,X,Y,BlinkRate)
EndFunction

Private
Type TInput

	Global tempText$
	Global blinktimer:Int = Null
	Global show:Int
	Global BlinkRate:Int
	Function Text$(Text$, X, Y, BlinkRate) 

		If BlinkRate >0
			show = True
		Else
			show = False
		EndIf

		If blinktimer = Null
			blinktimer = MilliSecs() 
		EndIf

		Local aKeytoGet = GetChar()

		If aKeytoGet'Anykey was pressed
			blinktimer = Null
			show = False	
			
			If aKeytoGet = 13 'ENTER
				Text$ = tempText$
				If Text$ = "" Then Text = " "
				tempText$ = ""
				blinktimer = Null
				show = BlinkRate
				FlushKeys() 
				Return Text$
			ElseIf aKeytoGet = 8 Or aKeytoGet = 4 'AscII For Backspace And Delete
				If Len( tempText$ ) > 0 Then tempText$ = Left$( tempText$, Len(tempText$) -1 )	
			Else ' If aKeytoGet>=32 And aKeytoGet<=122 And Len(Message)<52
				tempText$:+ Chr$(aKeytoGet)
			EndIf
			
		EndIf
		

		If BlinkRate >0
			If MilliSecs() > blinktimer + BlinkRate
				If show = True	
					show = False
					blinktimer = MilliSecs() 
				Else
					show = True
					blinktimer = MilliSecs() 
				EndIf
			EndIf
		EndIf
		
		If show = True
			DrawText Text$ + tempText + "|", X, Y
		Else
			DrawText Text$ + tempText, X, Y
		EndIf
		
		Return ""
	EndFunction

EndType




'Rem 
'Shows the use of Both input methods!
'--------------------------------------------------------
	Graphics 640,480,0 'Graphicsmode is a MUST
	
	Local Name$ = InputText("Enter Your Name: ",10,10,1000)	
	DrawText "Hello "+Name$+". Please Press a key to continue",30,30 ;Flip

	WaitKey()	

	Local X, Code$
	While Not KeyDown(Key_Escape)
	Cls
		Code = DynamicInput$( Name+" enter code : ",10,10,1000)
		If Upper(Code) = Upper("code")	Then DrawText "-- Correct Code! --",10,30;Flip;WaitKey() Else DrawText "Enter ~qCode~q ok?",10,30
		DrawRect X,50,40,5 ; X:+1 ; If X > GraphicsWidth() X = 0
		
	Flip
	Wend
'--------------------------------------------------------
'Good to have function
'EndRem



Code Archives Forum