Custom Textfield Function

BlitzMax Forums/BlitzMax Beginners Area/Custom Textfield Function

Apollonius(Posted 2007) [#1]
Well, as some of you may or may not know, I've been trying to make a function to create a text field but have been having alot of problems here and there. After a few days of messing around with this, I've decided to ask for some help.

This is my code currently. I'm trying to make it so whenever the mouse is over the text field, it lights up and if they left click on it, it lights up. How would I make that work with my current code? Tips and Tricks are welcomed, I'm learning :)

SuperStrict
AppTitle$="Textfield"
Graphics 800,600,0

' Global VARIABLES
Global gRoom:Int=0
Global gExit:Int=False

Global TextfieldList:TList=CreateList()

' Main Loop
Repeat
If KeyHit(KEY_ESCAPE) Or AppTerminate() Then gExit=True

Local test1:TTextfield
cTextfield( test1:TTextfield, 10, 10, 100, 15 )

Flip; Cls
Until gExit=True
End
 
Type TTextfield
	Field x:Int, y:Int, w:Int, h:Int
	Field text:String, length:Int
	Field mouseover:Int=False
	Field selected:Int=False
	Method Draw()
		If mouseover=True Or selected=True Then 
			SetColor(0,255,200)
		Else
			SetColor(0,130,130)
		EndIf
		DrawLine( x, y, x+w, y) 				' top
		DrawLine( x, y+h, x+w, y+h) 			' bottom
		DrawLine( x, y, x, y+h) 				' left
		DrawLine( x+w, y, x+w, y+h) 			' right
		DrawText( text, x+2, y+1 )
	End Method
	Method Update()
		If(MouseX()>x)And(MouseX()<(x+w))And(MouseY()>y)And(MouseY()<(y+h))
			mouseover=True
			If(MouseHit(1))
				selected=True
			EndIf
		Else
			mouseover=False
		EndIf
	End Method
End Type

Function cTextfield( handler:TTextfield Var, x:Int, y:Int, w:Int, h:Int )
	If Not TextfieldList.Contains( handler ) Then
		handler= New TTextfield
		handler.x=x; handler.y=y
		handler.w=w; handler.h=h
		TextfieldList.AddLast( handler )
	EndIf
	handler.Update()
	handler.Draw()
End Function


Fixed the fact that it didnt light up I just inversed Draw() and Update(). (was edited in the code above, but it still doesnt light up.


AltanilConard(Posted 2007) [#2]
You will have to put "Local test1:TTextfield" out of the main loop. Just define it in the beginning and it should work.


Apollonius(Posted 2007) [#3]
woot thanks it works, I was wondering is there a way to make it so I don't have to put local anywhere and just have to put the function in the main loop?


CS_TBL(Posted 2007) [#4]
Good luck, you've chosen to do something which is quite difficult.. :P Sofar there's no biggie, but just wait for the moment you'll actually have to implement editing and typing text, including wordwrap, scrolling through formatted text 'n such!


Apollonius(Posted 2007) [#5]
It's really that difficult? This is something all games need, it should be quite commun.

Yeah I've been thinking why is this hard? Cant wait for the text input.. -.- lol


AltanilConard(Posted 2007) [#6]

woot thanks it works, I was wondering is there a way to make it so I don't have to put local anywhere and just have to put the function in the main loop?


Your TTextField is a type, wich means it will need a instance of it for every textfield you want to have, so you will need to define a textfield with Local of Global before you can use it.

The text input won't be to hard, until you want to add wordwrapping and scrolling, as CS_TBL said. Good luck with it ;)


CS_TBL(Posted 2007) [#7]
kaisuo: was this function purely to display texts, in the way you see in games? Or was it a replacement for the textarea widget? (where you can really *input* text and such?)


Apollonius(Posted 2007) [#8]
Basicly these textboxs will be solely to input text ingame. You will click on it then input text.

scrolling looks really hard.. as for wordwrapping i dont even know what that is :o


AltanilConard(Posted 2007) [#9]
Wordwrapping is that when you type a word that will exceed the width of the textbox, it will be put totally, or partially, on the next line.


CS_TBL(Posted 2007) [#10]
Which on its own is not that complex, except that you also have to move up the following words, including their wordwrapping etc. if you were changing something in the middle of an alinea.

Would you be using non-proportional fonts btw?


dmaz(Posted 2007) [#11]
yeah, I was wondering about that statement... I'm not trying to be a d&*k or anything but I really don't see anything all that difficult even for a beginner. if anything, it's actually a really good exercise.


Who was John Galt?(Posted 2007) [#12]
You know I spent some time looking at that code and not being able to figure out why it didn't work (admittedly without max available to try it).

What's the problem with the local declaration in the main loop?


Apollonius(Posted 2007) [#13]
Well I was thinking of using the default font for the moment, and so far the default font looks good so I don't see a reason to change.

Now I'm at the input part... I only see one way of going at this and it would be keyhit every character you want and adding it to a string and probably making a input function that I could add in the objects(type) itself so I can re-use the code.

There might be another way to do this but it does not come to this beginner's mind at the moment.

Basicly I wana end up making a chat room(made myself)... I'm not even at 1% of this game's progress... I've been stuck on text boxes for a weekish (cant code all day real life ya know). Slow progress but progress none the less :)


CS_TBL(Posted 2007) [#14]
I'd first focus on displaying text, prior to inputing text. For example grab your text from "Will I" to "the less :)" put it in a single string and try to display it using a non-proportional font in any given number of columns, incorporating wordwarp. Plain, no formatting. Then think about how to do the same using a proportional font, including formatting such as indenting and perhaps bold/italics/underlined. If you've got the output format right then inputting is a matter of adjusting/inserting into this output format.


Apollonius(Posted 2007) [#15]
Where can I find the string manipulation codes? I'm trying to remove the last letter on keyhit(key_backspace)... ?


CS_TBL(Posted 2007) [#16]
You might want to read about Left$, Right$, Mid$, Replace$, LSet$, RSet$, those are all functions you might need, or find a use for. If there's no help for these commands, then check assari's request for new help text in the "Blitzmax programming" corner. I've added strings stuff there somewhere.


Apollonius(Posted 2007) [#17]
I've been trying to figure it out with no success... Man BlitzMax Documentation suck ass lol. Headache... ._.


dmaz(Posted 2007) [#18]
strings can be sliced...

Local s:String = "abcd"
Print s
s = s[..s.length-1]
Print s



Apollonius(Posted 2007) [#19]
I wish if there isn't one already for a string manipulation tutorial with extensive explications of how the commands work. That would help me and others alike. I'm sure it would only take a small portion of time to add such a tutorial to the tutorial section.

Just that code above looks like chinese..


dmaz(Posted 2007) [#20]
it's actually quite simple. (and documented) make sure you read the whole "language" section under help in the ide. I probably should have entered a space though [.. s.length-1]

the "slices" section has this:

StringOrArray[StartIndex..EndIndex]
so you can do this
Local a[200]    'initialize a[] to 200 elements
a=a[50..150]    'extract middle 100 elements of a[]
a=a[..50]       'extract first 50 elements of a[]
a=a[25..]       'extract elements starting from index 25 of a[]
a=a[..]         'copy all elements of a[]
a=a[..200]      'resize a[] to 200 elements
the "strings" section has this:
Print t.length 'return length of string: 17

but I could have used the "Len" operator instead.

so
".." is the start of the string and "s.length-1" is one less than the length of the string. so with that I'm making s = to the beginning of the string to 1 less than the length.


dmaz(Posted 2007) [#21]
.


dmaz(Posted 2007) [#22]
.


Jesse(Posted 2007) [#23]
.


Jesse(Posted 2007) [#24]
.


Jesse(Posted 2007) [#25]
.


Jesse(Posted 2007) [#26]
.


Jesse(Posted 2007) [#27]
.


Jesse(Posted 2007) [#28]
.


Jesse(Posted 2007) [#29]
.


Jesse(Posted 2007) [#30]
.


Jesse(Posted 2007) [#31]
.


Jesse(Posted 2007) [#32]
if you ever figure out the commands here is an example that might help you figure out how to do that:



You do need to know the commands mentioned by CS_TBL and how to manipulate arrays(strings are also arrays).

<EDITED>
sorry about all those other post I deleted. I guess the bmax server was not working. I kept on trying to post the same post but it wouldn't show on the page. when it finally showed I saw the same post a bunch of times. and That was the only way I know to remove them.


CS_TBL(Posted 2007) [#33]
I've this rough idea:

* have one string of a subparagraph, this is the string in which you can type/modify directly. Your whole text is made out of multiple subparagraphs put after each other.

* format the string, wrap words, and output each visual line to an array. So if your subparagraph spans 5 lines, then you'll have 5 arrays. You store these 5 arrays in a the same object as your subparagraph string.

* Now you have the input string, and a formatted version in a format you can browse through, while you never need to adjust the formatting on-the-fly.

As a result, the text is roughly twice in memory, but in today's machines it hardly matters, esp. as you didn't intend to write a whole novel in that game screen.