Simple Text Input for Graphic Modes

Tweet code-archives user-input rated
(Posted 2 years ago) RonTek

Repost from

Author: CloseToPerfect

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.


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
;8 is backspace
;13 is enter
If keyin > 0 Then 
    If keyin = 8 
        If Len(ccstring$)>0 Then ccstring$=Left$(ccstring$,Len(ccstring$)-1)
    ElseIf keyin = 13  
        ccreturnstring$ = ccstring$
        ccstring$ =""
        Return ccreturnstring$
;mod line if it has too many characters
If Len(ccstring$) > ccmaxcharacters  
;mod line if it is to wide by removing last letter added
If StringWidth(ccstring$) > ccmaxstringwidth 
;add blinking cursor and show line
If cccursorblink <= 0 Then cccursorblink = ccursorblinkrate
If cccursorblink < ccursorblinkrate/2  
    Text ccinputxloc,ccinputyloc,ccinputtext$+ccstring$+"_"
    Text ccinputxloc,ccinputyloc,ccinputtext$+ccstring$
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
    ball\x          = Rand(0,GraphicsWidth())
    ball\y          = Rand(0,GraphicsHeight())
    ball\speed      = Rand(1,4)
    ball\direction  = Rand(0,3)
    ball\Colors     = Rand(0,255*255*255)

Graphics 800,600,0,2
    ;draw background items first
    For ball.Tball = Each Tball
        Select ball\direction
        Case 0 ;up
            ball\y = ball\y - ball\speed
            If ball\y<0 Then ball\direction = 1
        Case 1 ;down
            ball\y = ball\y + ball\speed
            If ball\y>GraphicsHeight() Then ball\direction = 0
        Case 2 ; left
            ball\x = ball\x - ball\speed
            If ball\x<0 Then ball\direction = 3
        Case 3 ; right
            ball\x = ball\x + ball\speed
            If ball\x>GraphicsWidth() Then ball\direction = 2
        End Select 
        Oval ball\x,ball\y,20,20

    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$
Until KeyHit(1)

Reply To Topic (minimum 10 characters)

Please log in to reply