I was bored again and wrote some quite small and simple functions for user-text-inputs. It's more flexible than the dbp input-command, but also a bit more complex.. but I think it isn't complicated.
Functions:
-inp_Start *No parameters* [Has to be called before the other functions can be used]
-inp_New *No parameters* [Clears the input-string and resets other important variables]
-inp_Update *No parameters* [Must be called to update the inputs]
-inp_Done(*No parameters*) = bool [Returns '1', if returnkey is pressed, and wasn't pressed before]
-inp_SetInput strText [Changes the input to strText, and updates variables]
-inp_SetInputToString *No parameters* [Strings with all supported ascii-characters can be written]
-inp_SetInputToInteger *No parameters* [Only numbers from 0 to 9 can be written]
-inp_GetInput$(*No parameters*) = str [Returns the input-string]
-inp_GetLen(*No parameters*) = int [Returns the length of the input-string]
-inp_GetEntry$(*No parameters*) = chr [Returns the actual windows-entry-character]
-inp_StrToInt(strText) = str [Unimportant for you, deletes all not-numbers from a string]
Code with 2 examples:
rem Input-Functions
rem by MrKohlenstoff
rem Date of creation: 18th june '07
remstart
Functions:
-inp_Start *No parameters* [Has to be called before the other functions can be used]
-inp_New *No parameters* [Clears the input-string and resets other important variables]
-inp_Update *No parameters* [Must be called to update the inputs]
-inp_Done(*No parameters*) = bool [Returns '1', if returnkey is pressed, and wasn't pressed before]
-inp_SetInput strText [Changes the input to strText, and updates variables]
-inp_SetInputToString *No parameters* [Strings with all supported ascii-characters can be written]
-inp_SetInputToInteger *No parameters* [Only numbers from 0 to 9 can be written]
-inp_GetInput$(*No parameters*) = str [Returns the input-string]
-inp_GetLen(*No parameters*) = int [Returns the length of the input-string]
-inp_GetEntry$(*No parameters*) = chr [Returns the actual windows-entry-character]
-inp_StrToInt(strText) = str [Unimportant for you, deletes all not-numbers from a string]
How it works:
1. call "inp_Start" at the beginning
2. use inp_New to initialize a new input
3. You can change the input's text using "int_SetInput(Text$)"
4. Repeat the mainloop, e.g. until (inp_Done()=1)
5. In the mainloop you should call inp_Update and print inp_GetInput$() to the screen
6. You can use a variable to store the input, so you don't always have to call inp_GetInput$()
7. Before using a next input, call inp_New again
8. Have fun wiht it :P
remend
rem ----------------------------
rem Example 1
set display mode 800,600,32
set window on
inp_Start
rem First-Name-Input
inp_New
inp_SetInput ">>Your name here<<"
repeat
cls
inp_Update
fname$ = inp_GetInput$()
print "First-Name: ", fname$
until inp_Done()=1
rem Last-Name-Input
inp_New
repeat
cls
print "First-Name: ", fname$
inp_Update
lname$ = inp_GetInput$()
print "Last-Name: ", lname$
until inp_Done()=1
rem Age-Input
inp_New
inp_SetInputToInteger
repeat
cls
print "First-Name: ", fname$
print "Last-Name: ", lname$
inp_Update
age = val(inp_GetInput$())
print "Age: ", age
until inp_Done()=1
rem About-Input
inp_New
inp_SetInputToString
repeat
cls
print "First-Name: ", fname$
print "Last-Name: ", lname$
print "Age: ", Age
inp_Update
about$ = inp_GetInput$()
print "About you: ", About$
until inp_Done()=1
rem Show Information
cls
print "Your name is ", fname$, " ", lname$, " and you are ", age, " years old."
print "About you: ", about$
wait key
rem End of example 1
rem ----------------------------
rem Example 2
rem 2D-menu with 2 input-bars
inp_New
Selected_Input = 0
dim Editbox(1) as string
rem Menu-Loop
repeat
rem Clear screen
cls rgb(236,233,216)
print "Switch between inputs with TAB, end with RETURN!"
print "FPS: ", screen fps()
print "Key pressed: ", scancode()
rem Update input
inp_Update
Editbox(Selected_Input) = inp_GetInput$()
rem Avoid too long strings
if inp_GetLen()>30
s$ = left$(inp_GetInput$(),30)
inp_SetInput(s$)
endif
rem Show, which field is selected
ink rgb(255,255,255),0
box 195,98+30*Selected_Input,500,120+30*Selected_Input
rem Show edit-fields
ink rgb(0,0,0),0
text 100,100, "Input1: "
text 200,100, Editbox(0)
text 100,130, "Input2: "
text 200,130, Editbox(1)
rem Change selected input when TAB is pressed
if keystate(15)=1
rem change selected-variable
Selected_Input = ((Selected_Input+1) mod 2)
rem create new input
inp_New
rem change input-text to existing text
inp_SetInput(Editbox(Selected_Input))
rem wait for tab to be not pressed anymore
repeat : until keystate(15)=0
endif
until inp_Done()=1
end
rem End of example 2
rem ----------------------------
rem Functions:
#constant inp_Start = inp_Start()
#constant inp_New = inp_New()
#constant inp_Update = inp_Update()
#constant inp_SetInputToString = inp_SetInputToString()
#constant inp_SetInputToInteger = inp_SetInputToInteger()
#constant inp_SetInput = inp_SetInput()
function inp_Start()
global inp_String as string `Input
global inp_Len as integer `String-Length
global inp_Entry as string `Last inserted character
global inp_Return as boolean `if returnkey is pressed
global inp_NewReturn as boolean `if returnkey is pressed, and wasn't pressed before
global inp_type as byte `Input-Type: 0=string, 1=integer
endfunction
function inp_New()
inp_String = ""
inp_Len = 0
clear entry buffer
endfunction
function inp_Update()
rem End input if returnkey is pressed
if returnkey()
if inp_Return = 0 then inp_NewReturn = 1 else inp_NewReturn = 0
inp_Return = 1
exitfunction
else
inp_Return = 0
inp_NewReturn = 0
endif
e$ = entry$()
rem if user pressed key
L = len(e$)
a = asc(e$)
if (L>1) or ((L=1) and ((a>32) or (a=8))) or (controlkey() and keystate(47)=1)
rem Clipboard or key [Doesn't really work x.X]
if controlkey() and keystate(47)=1
e$ = get clipboard$()
L = len(e$)
endif
rem Backspace?
if e$ = chr$(8)
dec inp_Len, 1
if inp_Len < 0 then inp_Len = 0
inp_String = left$(inp_String, inp_Len)
else `no backspace -> normal key input
if inp_type = 1 then e$ = inp_StrToInt(e$) : L = len(e$) `integer-only-input
inp_String = inp_String + e$
inc inp_Len, L
endif
inp_Entry = e$
clear entry buffer
endif
endfunction
function inp_SetInputToString()
inp_type = 0
endfunction
function inp_SetInputToInteger()
inp_type = 1
endfunction
function inp_Done()
endfunction inp_NewReturn
function inp_GetInput$()
endfunction inp_String
function inp_SetInput(Set$)
inp_String = Set$
inp_Len = len(inp_String)
endfunction
function inp_GetLen()
endfunction inp_Len
function inp_GetEntry$()
endfunction inp_Entry
function inp_StrToInt(s$)
local IsNumber as boolean
local new$ as string
L = len(s$)
for Z = 1 to L
m$ = mid$(s$,Z)
IsNumber = 0
for x = 0 to 9
num$ = str$(x)
if m$ = num$ then IsNumber = 1 : exit
next x
if IsNumber = 1 then new$ = new$ + num$
next Z
endfunction new$
Functions only:
rem Functions:
#constant inp_Start = inp_Start()
#constant inp_New = inp_New()
#constant inp_Update = inp_Update()
#constant inp_SetInputToString = inp_SetInputToString()
#constant inp_SetInputToInteger = inp_SetInputToInteger()
#constant inp_SetInput = inp_SetInput()
function inp_Start()
global inp_String as string `Input
global inp_Len as integer `String-Length
global inp_Entry as string `Last inserted character
global inp_Return as boolean `if returnkey is pressed
global inp_NewReturn as boolean `if returnkey is pressed, and wasn't pressed before
global inp_type as byte `Input-Type: 0=string, 1=integer
endfunction
function inp_New()
inp_String = ""
inp_Len = 0
clear entry buffer
endfunction
function inp_Update()
rem End input if returnkey is pressed
if returnkey()
if inp_Return = 0 then inp_NewReturn = 1 else inp_NewReturn = 0
inp_Return = 1
exitfunction
else
inp_Return = 0
inp_NewReturn = 0
endif
e$ = entry$()
rem if user pressed key
L = len(e$)
a = asc(e$)
if (L>1) or ((L=1) and ((a>32) or (a=8))) or (controlkey() and keystate(47)=1)
rem Clipboard or key [Doesn't really work x.X]
if controlkey() and keystate(47)=1
e$ = get clipboard$()
L = len(e$)
endif
rem Backspace?
if e$ = chr$(8)
dec inp_Len, 1
if inp_Len < 0 then inp_Len = 0
inp_String = left$(inp_String, inp_Len)
else `no backspace -> normal key input
if inp_type = 1 then e$ = inp_StrToInt(e$) : L = len(e$) `integer-only-input
inp_String = inp_String + e$
inc inp_Len, L
endif
inp_Entry = e$
clear entry buffer
endif
endfunction
function inp_SetInputToString()
inp_type = 0
endfunction
function inp_SetInputToInteger()
inp_type = 1
endfunction
function inp_Done()
endfunction inp_NewReturn
function inp_GetInput$()
endfunction inp_String
function inp_SetInput(Set$)
inp_String = Set$
inp_Len = len(inp_String)
endfunction
function inp_GetLen()
endfunction inp_Len
function inp_GetEntry$()
endfunction inp_Entry
function inp_StrToInt(s$)
local IsNumber as boolean
local new$ as string
L = len(s$)
for Z = 1 to L
m$ = mid$(s$,Z)
IsNumber = 0
for x = 0 to 9
num$ = str$(x)
if m$ = num$ then IsNumber = 1 : exit
next x
if IsNumber = 1 then new$ = new$ + num$
next Z
endfunction new$
It's a bit buggy in some parts, e.g. pasting the clipboard, or controlling the maximum string-length, but I think, it could maybe help a few people (I think it's still often asked, how to do something while the input-command is active..).
As always: use it, change it, destroy it, call it stupid names (but maybe don't post them here..), whatever. Have fun and thanks.
Visit the DBPro - Speed up your game-Thread. http://forum.thegamecreators.com/?m=forum_view&t=88661&b=1