January 26, 2021, 12:22:15 PM

Author Topic: [bmx] Input in Graphics Mode by Tibit [ 1+ years ago ]  (Read 592 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bmx] Input in Graphics Mode by Tibit [ 1+ years ago ]
« on: June 29, 2017, 12:28:40 AM »
Title : Input in Graphics Mode
Author : Tibit
Posted : 1+ years ago

Description : 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 -> BlitzMaxmodpub.modinput.mod
in this directory put the code below, name it input.bmx. Now go into BlitzMaxBin in the cmd prompt
Write: bmk makemods pub.input


Code :
Code: BlitzMax
  1. 'Strict
  2.  
  3. Rem
  4. bbdoc: Input in GraphicsMode
  5. End Rem
  6.  
  7. ' Uncomment the four lines below to use this as a module (you have to build it then)
  8. 'Module Pub.input
  9.  
  10. 'ModuleInfo "Version: 1"
  11. 'ModuleInfo "Author: Wave~"
  12. 'ModuleInfo "License: Blitz Shared Source Code and Public Domain"
  13.  
  14. Import BRL.Max2D
  15. Import BRL.Retro
  16.  
  17. Rem
  18. bbdoc: InputText works just as a normal input but in graphicsmode. It waits for you to press enter then returns a string.
  19. endrem
  20. Function InputText$(Text$,X,Y)
  21. Local Inp$
  22.         Repeat
  23.                 Inp = TInput.Text(Text$,X,Y)
  24.                 Flip;Cls
  25.         Until Inp <> ""
  26.  
  27. Return Inp
  28. EndFunction
  29.  
  30. Rem
  31. 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.
  32. endrem
  33. Function DynamicInput$(Text$,X,Y)
  34.         Return TInput.Text(Text$,X,Y)
  35. EndFunction
  36.  
  37. Private
  38. Type TInput
  39.  
  40.         Global tempText$
  41.  
  42.         Function Text$(Text$,X,Y)
  43.                
  44.                         Local aKeytoGet = GetChar()
  45.                         If aKeytoGet'Anykey was pressed
  46.                        
  47.                                 If aKeytoGet = 13 'ENTER
  48.                                         Text$ = tempText$
  49.                                         If Text$ = "" Then Text = " "
  50.                                         tempText$ = ""
  51.                                         FlushKeys
  52.                                         Return Text$
  53.                                 Else If aKeytoGet = 8 Or aKeytoGet = 4 'AscII For Backspace And Delete
  54.                                         If Len( tempText$ ) > 0 Then tempText$ = Left$( tempText$, Len(tempText$) -1 ) 
  55.                                 Else' If aKeytoGet>=32 And aKeytoGet<=122 And Len(Message)<52
  56.                                         tempText$:+ Chr$(aKeytoGet)
  57.                                 EndIf
  58.        
  59.                         EndIf
  60.                        
  61.                         DrawText Text$ + tempText,X,Y
  62.                         Return ""
  63.  
  64.         EndFunction
  65.  
  66. EndType
  67.  
  68.  
  69.  
  70.  
  71. 'Rem
  72. 'Shows the use of Both input methods!
  73. '--------------------------------------------------------
  74.         Graphics 300,70,0 'Graphicsmode is a MUST
  75.        
  76.         Local Name$ = InputText("Enter Your Name: ",10,10)     
  77.         DrawText "Your Name was: "+Name$,30,30 ;Flip
  78.  
  79.         WaitKey()      
  80.  
  81.         Local X, Code$
  82.         While Not KeyDown(Key_Escape)
  83.  
  84.                 Code = DynamicInput$( Name+" enter code : ",10,10)
  85.                 If Upper(Code) = Upper("code")  Then DrawText "-- Correct Code! --",10,30;Flip;WaitKey() Else DrawText "Enter ~qCode~q ok?",10,30
  86.                 DrawRect X,50,40,5 ; X:+1 ; If X > GraphicsWidth() X = 0
  87.                
  88.         Flip;Cls
  89.         Wend
  90. '--------------------------------------------------------
  91. 'Good to have function
  92. 'EndRem


Comments :


bradford6(Posted 1+ years ago)

 try this
Code: [Select]

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



Ked(Posted 1+ years ago)

 I added a little modification so that it has a cursor.
Code: [Select]
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 Steel(Posted 1+ years ago)

 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
Code: [Select]
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


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal