[bb] simple text input for graphic modes by CloseToPerfect [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:42

Previous topic - Next topic

BlitzBot

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) Select
Global ccstring$=""
Global cccursorblink
Global ccursorblinkrate = 30; adjust this value to speed up or slow down the blink
Function cc_text_input$(ccinputtext$,ccinputxloc,ccinputyloc,ccmaxcharacters,ccmaxstringwidth)
;usage getstring$ = cc_text_input("display text", screen x location, screen y location, max number of characters, max string width)
;this function accepts ascii input
;this function erases with backspace and has a blinking cusor
;this function returns the finial string after the user presses the enter key
;this function does not stop the program execution if used in a loop
keyin=GetKey()
;8 is backspace
;13 is enter
If keyin > 0 Then
If keyin = 8
;backspace
If Len(ccstring$)>0 Then ccstring$=Left$(ccstring$,Len(ccstring$)-1)
ElseIf keyin = 13  
;enter
ccreturnstring$ = ccstring$
ccstring$ =""
Return ccreturnstring$
Else
ccstring$=ccstring$+Chr$(keyin)
EndIf
EndIf
;mod line if it has too many characters
If Len(ccstring$) > ccmaxcharacters  
ccstring$=Left$(ccstring$,Len(ccstring$)-1)
EndIf
;mod line if it is to wide by removing last letter added
If StringWidth(ccstring$) > ccmaxstringwidth
ccstring$=Left$(ccstring$,Len(ccstring$)-1)
EndIf
;add blinking cursor and show line
cccursorblink=cccursorblink-1
If cccursorblink <= 0 Then cccursorblink = ccursorblinkrate
If cccursorblink < ccursorblinkrate/2  
Text ccinputxloc,ccinputyloc,ccinputtext$+ccstring$+"_"
Else
Text ccinputxloc,ccinputyloc,ccinputtext$+ccstring$
EndIf
End Function


Function cc_color_int%(ccvalue)
;break integer color into rgb color
;why isn't there a included way To use integer colors values in blitz
;sets the color using a integer color value
red   = ccvalue Shr 16 And %11111111
green = ccvalue Shr 8 And %11111111
blue  = ccvalue And %11111111
Color red,green,blue
End Function




;example code

Type Tball
Field x,y,speed,direction,Colors
End Type

For i = 0 To 99 ; make 100 moving balls
ball.Tball = New Tball
ballx = Rand(0,GraphicsWidth())
bally = Rand(0,GraphicsHeight())
ballspeed = Rand(1,4)
balldirection  = Rand(0,3)
ballColors = Rand(0,255*255*255)
Next

Graphics 800,640,16,2
Repeat
Cls
;draw background items first
For ball.Tball = Each Tball
Select balldirection
Case 0 ;up
bally = bally - ballspeed
If bally<0 Then balldirection = 1
Case 1 ;down
bally = bally + ballspeed
If bally>GraphicsHeight() Then balldirection = 0
Case 2 ; left
ballx = ballx - ballspeed
If ballx<0 Then balldirection = 3
Case 3 ; right
ballx = ballx + ballspeed
If ballx>GraphicsWidth() Then balldirection = 2
End Select
cc_color_int(ballColors)
Oval ballx,bally,20,20
Next

Color 255,255,255 ; set text color
entry$=cc_text_input("enter something :",0,200,20,200)
If entry$<>"" Then lastentry$=entry$
Text 0,220,lastentry$
Flip
Until KeyHit(1)


Comments :


Dale Nation(Posted 1+ years ago)

 Nice!