Ooops
October 28, 2020, 05:50:45 AM

Author Topic: [bb] simple text input for graphic modes by CloseToPerfect [ 1+ years ago ]  (Read 1169 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : simple text input for graphic modes
Author : CloseToPerfect
Posted : 1+ years ago

Description : This code is a simple 1 function to get text input from the user.  
usage getstring$ = cc_text_input("display text", screen x location, screen y location, max number of characters, max string width)

Good points
You can place it anywhere on the screen.
It has description text, enter a null value, eg "", to just get input
You can cap the number of characters
You can cap the width of the return string
You can use it in real time with no loop stopping
Has a blinking cursor

Included is an example with 100 balls moving in the background that are unaffected by the input from the user.

Also included is a function to set the color using integer color value.  Very handy for setting a random color.
usage cc_color_int(integer color value)

I hope they are useful to someone, I use them alot.
CTP


Code :
Code: BlitzBasic
  1. Global ccstring$=""
  2. Global cccursorblink
  3. Global ccursorblinkrate = 30; adjust this value to speed up or slow down the blink
  4. Function cc_text_input$(ccinputtext$,ccinputxloc,ccinputyloc,ccmaxcharacters,ccmaxstringwidth)
  5. ;usage getstring$ = cc_text_input("display text", screen x location, screen y location, max number of characters, max string width)
  6. ;this function accepts ascii input
  7. ;this function erases with backspace and has a blinking cusor
  8. ;this function returns the finial string after the user presses the enter key
  9. ;this function does not stop the program execution if used in a loop
  10. keyin=GetKey()
  11. ;8 is backspace
  12. ;13 is enter
  13. If keyin > 0 Then
  14.         If keyin = 8
  15.                 ;backspace
  16.                 If Len(ccstring$)>0 Then ccstring$=Left$(ccstring$,Len(ccstring$)-1)
  17.         ElseIf keyin = 13  
  18.                 ;enter
  19.                 ccreturnstring$ = ccstring$
  20.                 ccstring$ =""
  21.                 Return ccreturnstring$
  22.         Else
  23.                 ccstring$=ccstring$+Chr$(keyin)
  24.         EndIf
  25. EndIf
  26. ;mod line if it has too many characters
  27. If Len(ccstring$) > ccmaxcharacters  
  28.         ccstring$=Left$(ccstring$,Len(ccstring$)-1)
  29. EndIf
  30. ;mod line if it is to wide by removing last letter added
  31. If StringWidth(ccstring$) > ccmaxstringwidth
  32.         ccstring$=Left$(ccstring$,Len(ccstring$)-1)
  33. EndIf
  34. ;add blinking cursor and show line
  35. cccursorblink=cccursorblink-1
  36. If cccursorblink <= 0 Then cccursorblink = ccursorblinkrate
  37. If cccursorblink < ccursorblinkrate/2  
  38.         Text ccinputxloc,ccinputyloc,ccinputtext$+ccstring$+"_"
  39. Else
  40.         Text ccinputxloc,ccinputyloc,ccinputtext$+ccstring$
  41. EndIf
  42. End Function
  43.  
  44.  
  45. Function cc_color_int%(ccvalue)
  46. ;break integer color into rgb color
  47. ;why isn't there a included way To use integer colors values in blitz
  48. ;sets the color using a integer color value
  49. red   = ccvalue Shr 16 And %11111111
  50. green = ccvalue Shr 8 And %11111111
  51. blue  = ccvalue And %11111111
  52. Color red,green,blue
  53. End Function
  54.  
  55.  
  56.  
  57.  
  58. ;example code
  59.  
  60. Type Tball
  61.         Field x,y,speed,direction,Colors
  62. End Type
  63.  
  64. For i = 0 To 99 ; make 100 moving balls
  65.         ball.Tball              = New Tball
  66.         ballx                   = Rand(0,GraphicsWidth())
  67.         bally                   = Rand(0,GraphicsHeight())
  68.         ballspeed               = Rand(1,4)
  69.         balldirection  = Rand(0,3)
  70.         ballColors              = Rand(0,255*255*255)
  71. Next
  72.  
  73. Graphics 800,640,16,2
  74. Repeat
  75.         Cls
  76.         ;draw background items first
  77.         For ball.Tball = Each Tball
  78.                 Select balldirection
  79.                 Case 0 ;up
  80.                         bally = bally - ballspeed
  81.                         If bally<0 Then balldirection = 1
  82.                 Case 1 ;down
  83.                         bally = bally + ballspeed
  84.                         If bally>GraphicsHeight() Then balldirection = 0
  85.                 Case 2 ; left
  86.                         ballx = ballx - ballspeed
  87.                         If ballx<0 Then balldirection = 3
  88.                 Case 3 ; right
  89.                         ballx = ballx + ballspeed
  90.                         If ballx>GraphicsWidth() Then balldirection = 2
  91.                 End Select
  92.                 cc_color_int(ballColors)
  93.                 Oval ballx,bally,20,20
  94.         Next
  95.  
  96.         Color 255,255,255 ; set text color
  97.         entry$=cc_text_input("enter something :",0,200,20,200)
  98.         If entry$<>"" Then lastentry$=entry$
  99.         Text 0,220,lastentry$
  100.         Flip
  101. Until KeyHit(1)


Comments :


Dale Nation(Posted 1+ years ago)

 Nice!


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal