Input Fields

Blitz3D Forums/Blitz3D Beginners Area/Input Fields

mintybreath(Posted 2007) [#1]
Hi, i was wondering how to make an input field. What i meen, is i want to make it so, you press a button, and it brings up a box, you can only type on the box, not anywhere else. I also want it to not end the programs execution. I am really not sure how to make an input field like so, that does not pause program execution. I would really love some help on this if anyone has the time. Thanks everyone!


semar(Posted 2007) [#2]
First of all, you need a 'status' variable, and some status constants to be used for:
Global Status
Global Input_Text$ ;store the text input of the user
Global Input_Finished = False ;flag to indicate when the user is done with the input
Const S_NORMAL = 1
Const S_INPUT  = 2
Status = S_NORMAL


When the user presses the 'input text button', the status variable, which normally is set to S_NORMAL, will be set to status S_INPUT.

You need this status variable to decide, in your code, when your program should display a rectangle on the screen, simulating an imput text box, and when not:

while not keydown(1) ;while not esc is pressed

Cls

;code logic here

if keyhit(your_input_key) then ;make input box available
status = S_INPUT ;set the status variable
Input_Text$ = "" ;reset the input text
Input_Finished = False ;reset the input finished flag
endif

if Input_Finished then ;user pressed enter, so input has finished
status = S_NORMAL
endif

select Status
case S_NORMAL
    ;display everything but the input box
    ;check the keyboard as usually
case S_INPUT
    ;display everything + the input box
    ;check the keyboard for input box
    Input_Box() ;the function to read user input
    ;display an input box rectangle
    Rect 100,100,150,80
    ;display the text inside it
    Text 110,110,Input_Text$

end select
flip
wend


Let's now talk about the function Input_Box(). With this function, your program should get the user input, and update the variable string Input_Text$. You may use the GetKey command, which returns the ASCII value of a character, so that your program does not stop while executing the function Input_Box.

A short example would resemble like this:
Function Input_Box()
local av = GetKey() ;gets the ascii value of a pressed key
if av > 0 then ;a key has been pressed
;first check if the user has pressed enter
If av = enter_ascii_code then
Input_Finished = True
Return ;exit the function
endif
;we have to convert the ascii value, which is an integer, to the corresponding string character using the command Chr, and update the input text:

Input_Text = Input_Text + Chr(av)

endif
end function


Note that the function Input_Text() is not complete yet. In fact, you need to find out the ascii code of the enter key, and also handle the delete key. But I think - and hope - that the code provided will help you to find out the way, otherwise post again if you are in trouble :)

You may also find the code archive a mine gold; look in the topic 'User Input', you'll find functions that already make what you need:
http://www.blitzbasic.com/codearcs/codearcs.php?cat=5


Sergio.


mintybreath(Posted 2007) [#3]
Ya i believe i know how to do the rest, but if i do get stuck, i will post here

Thanks alot for your help! : D


Yahfree(Posted 2007) [#4]
also, theres some good ones in here:

http://www.blitzbasic.com/codearcs/codearcs.php?cat=5

look through them


mintybreath(Posted 2007) [#5]
I believe i need some more help if anyone can spare it.

I finished the code that Semar gave me above. Now my problem is getting the input box to appear when whatwaspicked=notebook. I dont know why it wont display any input box. Some help would be really appreciated.
Here is the code.

Graphics3D 640,480,16,2
SetBuffer BackBuffer()
type_bracket=1
type_arrow=2

light=CreateLight()
camera=CreateCamera()
PositionEntity camera,0,0,-1.5


Global Status 
Global Input_Text$ ;store the text input of the user 
Global Input_Finished = False ;flag to indicate when the user is done with the input 
Const S_NORMAL = 1 
Const S_INPUT = 2 
Status = S_NORMAL 

Function input_box()
Local av=GetKey() ;gets the ascii value of a pressed key
If av>0 Then ;a key has been pressed
;first check if the user has pressed enter
If av=13 Then
input_finished=True
Return ;exit the function
EndIf
;we have to convert the ascii value, which is an integer, to the corresponding string character using command Chr, and update the input text

input_text=input_text + Chr(av)

EndIf
End Function

;the notebook
notebook=CreateCube()
ScaleEntity notebook,.15,.15,.000001
EntityOrder notebook,-1
PositionEntity notebook,-1.3,.8,0
tex4=LoadTexture("notebook.jpg")
EntityTexture notebook,tex4
EntityPickMode notebook,2

Cls
;code logic here
If whatwaspicked=notepad Then ;make input box available
status=s_input ;set the status variable
input_tex$=" " ;reset the tex variable
input_finished=False ;reset the value of the integer flag
EndIf

Select statue
Case S_NORMAL
	;display everything but the input box
	;check the keyboard as usual
Case S_INPUT
	;displays everything + input box
	;Check the keyboard for input box
	Input_Box() ;check to read user input
	;displays an input box rectangle
	Rect 100,100,150,80
	;display the text inside it.
	Text 110,110,input_text$
	

End Select



semar(Posted 2007) [#6]
You need a while..wend loop, which exits if the user press ESC, where to include the select status.

Also, in your code there's a typo, you have << select statue >> when it should be << select status >> instead. (statue-->status)

The variables whatwaspicked and notepad are not declared Global.

A flip command is needed to show the graphics.

Try starting with a simple 2D box, then improve it with the 3D cube.

Sergio.


Adam Novagen(Posted 2007) [#7]
Okay... Might I suggest getting a GUI DLL? That would probably simplify things... A BIT!!!


mintybreath(Posted 2007) [#8]
Im sorry, i dont really know much about global and local. I am new to blitz. I did not put in the entire code, cuz what i am doing is really really long. I do have a while..wend loop. While not keydown(1), then at the end it is, renderworld,flip, wend, end.


How do you declare it global? as i said, i dont know much about global and local, and i am trying to learn more of the blitz commands every day. I havent made it to trying out global and local yet though.

And Adam, what is a GUI DLL?


Adam Novagen(Posted 2007) [#9]
Okay, GUI stands for Graphics User Interface. (Look, I didn't invent the term! ^_ ) Anyway, a GUI is basically all the stuff that makes programs more usable; menus, buttons, extra pop-up windows, and so on. A DLL (just in case you don't know) is a file that "extends" an application like Blitz. In the case of Blitz Basic, a DLL adds more commands into your compiler. Check out the toolbox on this site (click the community tab, then click "Toolbox" underneath) for some various DLLs; I know there's at least one GUI DLL in there.


mintybreath(Posted 2007) [#10]
Oh k, ill look into that. can anyone also answer my first question please though?


semar(Posted 2007) [#11]
The main difference between Global and Local variables is the scope - that is, the range of code where the variable retains its value.

When a variable is declared as Local, its scope is limited to the function - or the part of the code - where the variable were declared.

Consider this example:
test() ;call a test function
Print "MyLocal outside the function: " + MyLocal ;prints 0 because MyLocal scope is only inside the function

function test()
local MyLocal = 5
Print "MyLocal inside the function: " + MyLocal ;prints 5
end function


So, when you need a local variable, just for calculations an the like, then use Local.

Instead, when you need a variable to be accessed in every point of your code - that is, in every function and every place of your code - then use Global declaration.

A variable can be declared Global only in the main part of your code - that is, you can't declare a Global variable inside a function.

Time for another example:
Global MyGlobal$ = "mintybreath"
;call a test function
print MyGlobal; prints "mintybreath"
test()
print MyGlobal ;prints "Hello mintybreath"

function test()
MyGlobal = "Hello " + MyGlobal ;updates the global variable
end function


To summarize: use Global when you need a global scope for your variable. Example: score, player name, speed, etc.

Use Local when you need to compute some value which is only temporary needed.

Don't forget also Costants. A Costant does not change its value, and it's also global by default - that is, you can access it in every piece of your code, exactly like a global variable. But the Costant value can't be changed:
Const PI# = 3.14
print "PI outside the function = " + PI; prints 3.14
test() ;call our lovely test function

;uncomment the next line to generate a compile error: a constant value can't be changed
;pi = pi + 1 ;generates a compile error.

function test()
print "PI inside the function = " + PI ;prints 3.14
end function

Hope this helps,
Sergio.