October 28, 2020, 05:39:15 AM

Author Topic: [bb] Full-Screen Text Input by Grey Alien [ 1+ years ago ]  (Read 1060 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Full-Screen Text Input by Grey Alien [ 1+ years ago ]
« on: June 29, 2017, 12:28:42 AM »
Title : Full-Screen Text Input
Author : Grey Alien
Posted : 1+ years ago

Description : This mini program shows how you can accept text input from a user even in a full-screen graphics application (game).  Note that depending on the font loaded, you should change the line that says Local charwidth = 15.

Hope you find it useful!


Code :
Code: BlitzBasic
  1. Const Screenwidth = 800
  2. Graphics Screenwidth,600,32
  3.  
  4. Const ESCAPE = 1
  5. Const RETURN_KEY = 28
  6.  
  7. ;Text Input
  8. Const ccTextInputMax = 255
  9. Global ccTextInputMode = 0 ;calling app may want to set this to 1 so it doesn't process other input (it won't affect the proc here though)
  10. Dim ccTextArray$(ccTextInputMax) ;variable used by TextInputMode to store what the user is typing.
  11. Global ccText$ ;final output of ccTextArray
  12. Global ccTextCurrentChar% ;used by ccTextInput to track the current character
  13. Global ccTextInputLength = 20 ;default 20 chars.  Change to whatever you want.
  14. Global ccTextInputCaption$ = "" ;this prints above the input box.
  15.  
  16. Global fntCourierNew% = ccLoadFont("Courier New", 30, True, False, False)              
  17.  
  18. ccTextInputInit()
  19. ccTextInputMode = 1
  20. Repeat
  21.         Cls
  22.                 If ccTextInputMode = 1 Then ccTextInput(fntCourierNew, 100,20)
  23.         Flip
  24. Until KeyHit(ESCAPE)
  25.  
  26. ; -----------------------------------------------------------------------------
  27. ; Text Input
  28. ; -----------------------------------------------------------------------------
  29. Function ccTextInputInit()
  30.         ;clear the text array
  31.         For i = 0 To ccTextInputMax
  32.                 ccTextArray(i) = ""            
  33.         Next
  34.         ;clear the other variables
  35.         ccText = "" ;initialise the text
  36.         ccTextCurrentChar = 1  
  37.         ccTextInputCaption = ""
  38.         ccTextInputMode = 0
  39.  
  40.         ;clear any keys so they don't interfere with ccTextInput()
  41.         FlushKeys;
  42.         FlushMouse;
  43. End Function           
  44.  
  45. Function ccTextInputFill(StartText$)
  46.         ;fill the text array
  47.         For i = 1 To Len(StartText)
  48.                 ccTextArray(i) = Mid(StartText, i, 1)          
  49.         Next
  50. End Function
  51.                
  52. Function ccTextInput(Font%, StartY, MaxLength%)
  53.         Local charwidth = 15
  54.         Local x = (ScreenWidth - (charwidth*MaxLength)) / 2
  55.         SetFont Font
  56.        
  57.         Local lt = 0
  58.        
  59.         ;output the caption
  60.         Color 240,240,0
  61.         ccShadowText(ScreenWidth/2, StartY-50, ccTextInputCaption, 1)
  62.        
  63.         Color 240,240,240
  64.         ;output the current text
  65.         For lt = 1 To MaxLength ; Draw entered letters or dots if not entered.
  66.                 If ccTextArray$(lt) <> "" Then
  67.                         If lt = ccTextCurrentChar
  68.                                 ccShadowText(x + (lt-1)*charwidth, StartY, ccTextArray$(lt),0)
  69.                         Else
  70.                                 ccShadowText(x + (lt-1)*charwidth, StartY, ccTextArray$(lt),0)
  71.                         EndIf
  72.                 Else
  73.                         ;cursor (_) or blank space (.)
  74.                         If lt = ccTextCurrentChar
  75.                                 ccShadowText(x + (lt-1)*charwidth, StartY, "_",0)
  76.                         Else
  77.                                 ccShadowText(x + (lt-1)*charwidth, StartY, ".",0)
  78.                         EndIf
  79.                 EndIf
  80.         Next
  81.        
  82.         ;detect cursor and backspace keys
  83. ;       If KeyHit(RIGHT_ARROW)                                                                  ; Right key.
  84. ;               HighNameChar = HighNameChar + 1                                         ; Move one letter to right.
  85. ;               If HighNameChar > HIGH_SCORE_NAME_LENGTH Then HighNameChar = 1          ; Wrap to first letter if past letter 12.
  86. ;       EndIf
  87. ;       If KeyHit(LEFT_ARROW)                                                                   ; Left key.
  88. ;               HighNameChar = HighNameChar - 1                                         ; Move one letter to left.
  89. ;               If HighNameChar < 1 Then HighNameChar = HIGH_SCORE_NAME_LENGTH          ; Wrap to last letter if past letter 1.
  90. ;       EndIf
  91.  
  92.         ; 97 - 122 (letters), (48 - 57) numbers, 32 = space
  93.         ltr = GetKey()                                                                                  ; Get all the keys that can be entered.
  94.  
  95.         If ltr = 8 Then ;8 = backspace.  Use get key so key repeat works
  96.                 ccTextCurrentChar = ccTextCurrentChar - 1                                               ; Delete letter.
  97.                 If ccTextCurrentChar < 1 Then ccTextCurrentChar = 1                     ; Keep on first if needed.
  98.                 ccTextArray$(ccTextCurrentChar) = ""                                            ; Clear letter from array.
  99.         EndIf
  100.  
  101.         If (ltr >= 32 And ltr <= 126) Then ;all chars
  102.         ;If (ltr > 96 And ltr < 123) Or (ltr >= 48 And ltr <= 57) Or (ltr = 32) Or (ltr > 64 And ltr < 91) Then ;no special chars
  103.                 If ccTextCurrentChar <= MaxLength
  104.                         ccTextArray$(ccTextCurrentChar) = Chr$(ltr)     ; Put letter in array.
  105.                         ccTextCurrentChar = ccTextCurrentChar + 1                                       ; Move to next letter position.
  106.                 EndIf
  107.         EndIf
  108.  
  109.         Local pl=0
  110.                                        
  111.         If KeyHit(RETURN_KEY)                                                                   ; Return key.
  112.                 ccText$ = ""
  113.                 For pl = 1 To MaxLength                        
  114.                         If ccTextArray$(pl) = ""
  115.                                 ;don't put spaces in ;ccText$ = ccText$ + " "
  116.                         Else
  117.                                 ccText$ = ccText$ + ccTextArray$(pl)
  118.                         EndIf
  119.                 Next
  120.                                
  121.                 FlushKeys
  122.                 FlushMouse
  123.                 ccTextInputMode = 0 ;switch the mode off so calling proc knows user is done.
  124.                 Return 1 ;OK
  125.         Else
  126.                 ;if escape is pressed, abort.
  127.                 If KeyHit(ESCAPE)
  128.                         ccText$ = ""
  129.                         FlushKeys
  130.                         FlushMouse
  131.                         ccTextInputMode = 0 ;switch the mode off so calling proc knows user is done.
  132.                         Return -1 ;cancel
  133.                 EndIf
  134.                
  135.                 Return 0 ;user has not finished yet
  136.         EndIf
  137. End Function
  138.  
  139. ; -----------------------------------------------------------------------------
  140. ; ccShadowText
  141. ; -----------------------------------------------------------------------------
  142. Function ccShadowText(x%, y%, TheText$, Centre)
  143.         ;make a black shadow in the same font behind the text so it shows up on top of images
  144.         ;first store the current color
  145.         red = ColorRed()
  146.         green = ColorGreen()
  147.         blue = ColorBlue()
  148.        
  149.         Color 0,0,0
  150.         Text x + ShadowTextDepth, y + ShadowTextDepth, TheText, Centre
  151.         Color red, green, blue
  152.         Text x, y, TheText, Centre     
  153. End Function
  154.  
  155. ; -----------------------------------------------------------------------------
  156. ; Load a font and error if not found
  157. ; -----------------------------------------------------------------------------
  158. Function ccLoadFont (TheFont$, Size, Bold, Italic, Underline)
  159.         pointer = LoadFont(TheFont$, Size, Bold, Italic, Underline)
  160.         If Not pointer Then
  161.         RuntimeError ("Error loading font "+TheFont$)
  162.                 End
  163.         Else
  164.         Return Pointer 
  165.         EndIf
  166. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal