Another Text Box...I need help...

Blitz3D Forums/Blitz3D Beginners Area/Another Text Box...I need help...

Cp(Posted 2008) [#1]
Okay, I'm trying to do a TextBox Thing sort of like DarkShadowWings...Except using an image for the box. I'd like to have word wrap too...It's a pain to do 16 chars per line,no more and have to print a million times.
I have no idea where to even start.
I guess I'm still a noob. :)

So could anybody help me????????
P.S.The reason I'm doing this is for an RPG I'm working on.

EDIT:
You know, the kind in videogames when youre talking?


Warner(Posted 2008) [#2]
Have you looked in the code archive ?
http://www.blitzmax.com/codearcs/codearcs.php?cat=5


Cp(Posted 2008) [#3]
Just checked.Much to my dismay, there was nothing.


Warner(Posted 2008) [#4]
Well, there is some code for a text box:
http://www.blitzmax.com/codearcs/codearcs.php?code=2022
Remove the clipboardcommands to see it running.

As for breaking a string in series of 16 chars, use Mid$. It returns a specified part of a string.
t$ = "12047ghikjghdfsakjhgkjdsaliuryewiuyrioewqiroye q ruiewoq iurye wiqyrewiuqoyreiuow qyriue"

For i = 1 To Len(t$)
	Print Mid$(t$, i, 16)
	i = i + 15
Next


To search a string for spaces, use Instr(). It searches for a string in another string and returns the location where it was found.


Cp(Posted 2008) [#5]
Not input textbox, I mean the kind like in RPGs where in battles, it says stuff like "You cause the enemy 10 damage"


Warner(Posted 2008) [#6]
Okay, I should read better. Try to combine the above with some Millisecs() timeout routine to show the text line-by-line.
timeout = MilliSecs()

While Not KeyHit(1)

	If MilliSecs() > timeout + 500 Then
		timeout = MilliSecs()
		Print timeout
	End If
	
Wend



Cp(Posted 2008) [#7]
16 was an example. :) Im getting it to work
For some reason it only works with print...I can't use text?


GIB3D(Posted 2008) [#8]
I think Text doesn't work because you specify where the text is put onscreen

I tried putting Locate 0,0 into the mix and it puts it all on one line.

t$ = "12047ghikjghdfsakjhgkjdsaliuryewiuyrioewqiroye q ruiewoq iurye wiqyrewiuqoyreiuow qyriue"

For i = 1 To Len(t$)
        Locate 0,0
	Print Mid$(t$, i, 16)
	i = i + 15
Next



Warner(Posted 2008) [#9]
If you use Text, use Flip to show it:
Graphics3D 800, 600, 0, 2
SetBuffer BackBuffer()

Repeat

Cls
Text 400, 300, "test" + Rand(0, 360)
Flip

Until KeyHit(1)

End



Cp(Posted 2008) [#10]
Not exactly...It doesnt word wrap with text.I'm gonna just use print for now. If you can help with text, by all means, tell me. thanks!


Warner(Posted 2008) [#11]
Indeed with Text, you specify a location. In order to 'print' multiple lines using Text, you'll need an increasing var. for the y-position of the text:
Graphics 800,600,0,2
t$ = "12047ghikjghdfsakjhgkjdsaliuryewiuyrioewqiroye q ruiewoq iurye wiqyrewiuqoyreiuow qyriue"

yy = 15
For i = 1 To Len(t$)
	Text 400, yy, Mid$(t$, i, 16), 1, 1
	i = i + 15
        yy = yy + 12
Next



Cp(Posted 2008) [#12]
Okay. I can use text now. Thanks for helping Warner.


Cp(Posted 2008) [#13]
Hmm...The word wrap...It needs a little work. can you help me make it wrap the individual words? I think that might be complicated.


Ross C(Posted 2008) [#14]
You need to draw your text, word by word. To do that, you get the width of each character and add them up. You also have a start x co-ord. You keep check to see if the word length + the current X co-rd is greater than the screen width. If so, move your Y drawing co-ord down a line, and reset your current x co-ord to 0 again.


Cp(Posted 2008) [#15]
Huh?
Okay, I get you to some extent, but I have NO idea of how to seperate the words.
EDIT:
What commands do i use?
EDIT 2:
Seriosly, ross, help. I'm lost.


Cp(Posted 2008) [#16]
Ok, I'm going to post my code, because I'm seriously lost on how to word wrap properly.
here it is and im putting my money on a total re-write.
Function TextBox(t$)
font = LoadFont("Blue Highway Bold",23,False,False,False)
SetFont font
Local textboximg = LoadImage("TextBox.bmp")
DrawImage textboximg,320,65
yy = 5
For i = 1 To Len(t$)
	Text 75, yy, Mid$(t$, i,45)
	i = i + 44
    yy = yy + 25	
Next
Flip
End Function


ummm....what do i need to rewrite, if not all of it?


Warner(Posted 2008) [#17]
For this purpose, there is more than one solution, as seen more often in programming. To solve an issue like this, think about how you would do it yourself. How can you tell words apart from each other ? I would think that is the space between the words. So logically, the solution lies not in breaking up the sentence into words, but into finding the spaces in the sentence. Perhaps you could use this code as a base:
t$ = "this is a sentence"
For i = 1 to len(t$)
char$ = Mid$(t$, i, 1)
Print char$
Next

It breaks down the sentence into single characters.


Cp(Posted 2008) [#18]
next: here is the new code;Not wrapping the text.
Function TextBox2(t$)
font = LoadFont("Blue Highway Bold",23,False,False,False)
SetFont font
Local textboximg = LoadImage("TextBox.bmp")
DrawImage textboximg,320,65
yy = 5
xx = 75
For i = 1 To Len(t$)
	char$ = Mid$(t$,i,1)
	If char$ = " "
		If StringWidth(Mid$(t$,i,16) + xx) > ImageWidth(textboximg) + 75
			yy = yy + 25
			xx = 75
		EndIf
	EndIf
	Text xx,yy,char$
	xx = xx + 10
Next
Flip
End Function


I have a feeling its because of this:
 
If StringWidth(Mid$(t$,i,16) + xx) > ImageWidth(textboximg) + 75

but that is all I could think of.
is there something else I could do to calculate it?


Warner(Posted 2008) [#19]
Hmm .. I think in the first part, the bracket is wrong. Your adding xx to the string itself before calculating its width.
-->StringWidth(Mid$(t$,i,16)) + xx
However, I don't think this method would do what you want. It now takes the width of 16 characters, then adds 'xx' to it, and compares that to the width of the textboximage. But youre using xx to print out the string one char at the time.
So, instead, as a next sub-goal, I would suggest to try wrapping each word that is found. When that works you can implement the width-comparisation.
To achieve wrapping every word, store every character that is not a space into a temp. string. Then, when a space is found, this string is printed at once, and then cleared. Ie. Print temp$: temp$ = ""


Cp(Posted 2008) [#20]
Okay, I managed the individual word wrap, but when I add in the code for comparing it to the width, its all messed up and does not wrap at all.
why?


Warner(Posted 2008) [#21]
Lets see .. if you have individual words, then put them in a variable and add that to another variable for instance result$
if mid$(t$, i, 1) = " " then       
   result$ = result$ + word$ + " "    
else                                   
   word$ = word$ + mid$(t$, i, 1)      
end if

Next, before adding the new word to the final result, check if the results length has already surpassed the 16 chars.
So, in the above example, replace result$ = result$ + word$ with this
if len(result$ + word$) > 16 then   
    print result$
    result$ = word$
else
    result$ = result$ + word$ + " "
end if

or
result$ = result$ + word$ + " "
if len(result$) > 16 then
   print result$
   result$ = ""
end if



Ross C(Posted 2008) [#22]
i can email you the code i did for dark shadow wing if you want


Cp(Posted 2008) [#23]
EDIT:
Okay, send me the code.


Cp(Posted 2008) [#24]
Okay,send me the code,ross.
From looking at the posts, Im guessing there is a bit of XML involved with it. I know html, not XML, but they are similar,so i should be able to work with it fine.


Ross C(Posted 2008) [#25]
Sorry man, i've not been at my computer, so to speak, so haven't emailed the code. I'll def do it when i get in :o) Promise!


Cp(Posted 2008) [#26]
K. Thanks for letting me know.
Send it when you can.


Cp(Posted 2008) [#27]
Ross, can you send it soon? I won't be able to make any progress in the field without the textbox.Talking is next, then scrolling.


Ross C(Posted 2008) [#28]
I did send it... It's sitting in my sent box. Odd, i'll re-send it again.


Cp(Posted 2008) [#29]
Weird...I still didnt get it...
send it to cpdying08@...
I should be able to get it there.
EDIT: I think the attachment is too big. sometimes I cant get a message if the attachment is too big.


Cp(Posted 2008) [#30]
You are sending it to jon08@... , right?
No spelling errors?
EDIT: dont click on the mailto:blah blah.
Open up an email program and do it manually.
EDIT2:
Let me know when you sent it.


Ross C(Posted 2008) [#31]
Ok, i was in my work when writing that. I've sent it again :o)


Cp(Posted 2008) [#32]
Ok. I got the mail. I do need it in 2d. How do I implement it? There are a lot of entity commands.
EDIT:Okay,I'm lost. I have no idea what functions are what, and how to get rid of the entity stuff. Can you help, ross?


Ross C(Posted 2008) [#33]
It's the included function you need to worry about "code.bb". The main.bb is just an example code to use the functions.

What this code does, it read in a text data statement. It then draws this text string to a texture, word wrapping it as it goes, with controllable speed and outline etc etc

The only thing you really need to change, is the texture to an image, and draw the image at the correct location. I'll knock up an example in 2d for ya.


Cp(Posted 2008) [#34]
K. I know its code.bb. I already know how to setup more text with Data.
Its just all the entities that confuse me.
EDIT:
How long do you think it will take to make the code 2d?
I reeeeeally need this. I'd appreciate it if you could get it done quickly.


Ross C(Posted 2008) [#35]
Was pretty late last night. I'll have it for sometime today. Couldn't you be getting on with other parts of your game just now ;o)


Cp(Posted 2008) [#36]
Yeah. I'll work on the map scrolling.Send it when its ready.


Cp(Posted 2008) [#37]
Is it ready yet?
EDIT:
Ross, I reeeeeeeeally need it.I just finished scrolling,and I need to work on talking.
I also need a seperate textbox for the battles that does not have the "Press enter to continue" and allows me to input text into the function that doesn't use data statements.


Cp(Posted 2008) [#38]
I'm not experienced in 3d, so I don't know 2d eqivelant commands.
Please, I need it soon.If you could send it in 3 days or less, I'd be greatful. :)
As long as you send it along at some point, I'm planning on putting you in the credits.


Ross C(Posted 2008) [#39]
Well, i'll knock it up in 2d for you, but really, if your intending in making a game, it's not THAT difficult to change it, so please don't make time demands, i got other things happening in my life :o)


Cp(Posted 2008) [#40]
I'm sorry.Ok, finish it whenever.But I really am NOT experienced in 3d, and I have no idea what commands do what(the blitz ones and functions.)And I understand. I'm on the computer a lot, but its not my WHOLE life. I'll look at it again.But could I have a hint as with what to do with all the mesh stuff?And what is the main command that triggers all the others?


Ross C(Posted 2008) [#41]
The mesh stuff has nothing really to do with it. I'll strip it down for you instead, that way you'll know how it works, as the actual word wrapping and drawing part of the text is in 2d. :o)


Cp(Posted 2008) [#42]
ok.Its just I guess a lot of stuff I dont know what it is, so I don't know what to do with it.If you strip the uneeded commands, I should be able to do it.Thanks!


Cp(Posted 2008) [#43]
Ross, have you sent it yet? I haven't got it.


Ross C(Posted 2008) [#44]
Ok, well i'm looking through it and it takes a fair bit of setting up. So how about i write you what you need?

I know you need so far:

- 2d
- text that wraps to fit a window

So, you'd first off need a window width and height. 2d i easier i guess to do this, as you don't need to worry about texture sizes.

So, what else do you need? I may as well code it, cause stripping this out isn't a task i fancy much. Poor coding has resulted in something that relies on globals in functions, which isn't great...


Cp(Posted 2008) [#45]
Ok,Here is what I need:
-2d
-text wrap
-changeable colors of text
-a window for battles that scrolls,it would be easier to use
-need it for 640x480
-Maybe not use data at all, instead input text into a function

Can you do that? I'll help in any way I can.


Cp(Posted 2008) [#46]
ross, i just edited the frame and text_texture. here are the new ones:
frame:

text_texture:



Ross C(Posted 2008) [#47]
Well, how would you store the text data before the game is played? The data statements are simply read into each paragraph type objects :o) Thanks for the backround.


Cp(Posted 2008) [#48]
Like this:
If ImagesCollide(blah)
TextBox("Hi, this is a test",blah,blah)
endif

Battles are kind of random, so data would not be a good choice.
oh yeah, a couple more things:

-Adjustable text speed
-No "press enter to continue" for battles.


Ross C(Posted 2008) [#49]
oh right that kind of thing. nps.


Cp(Posted 2008) [#50]
So, do you think you can do it? I'll help to the best of my extent.


Ross C(Posted 2008) [#51]
Yeah, i'm half way there. Got some time, so you should have it tonight hopefully :o)

Another point i thought of. Does this window just accept text? Or does the text you pass across to the function, get added on?

And is it just a battle window you need?


Cp(Posted 2008) [#52]
no, both battle and field, the battle just needs to be altered a little to work better with random stuff.
I think data would be easier for the field.I couldnt accidentally erase stuff. haha.
hmm. added on would be good.But after no text is used anymore, like a battle is finished, the text is erased from the memory.


Ross C(Posted 2008) [#53]
Ok, the way i have this working is as follows:

2 window modes:

0 = continuous mode. You call RC_text_box("blah blah") to add this text to a window. My code will add a terminator code onto the end of this. ¬ will be that code, so you can't use that character in the text you pass across.

When you wish to close the battle window, simply call RC_close_window()

1 = One Shot mode. You call RC_text_box("blah blah"). This text scrolls until finished. You can query the status of this textbox with RC_text_box_mode.

Global RC_text_box_status = 0 ; 0 = no text_box showing
; 1 = window opened
; 2 = waiting advance key
; 3 = waiting new text
; 4 = force window close (user must set this in code)

It will return 2 if it's waiting for the advance key to be pressed.


I don't know about the data thing. It's a little too much to implement with current time restraints. No reason you can't implement a data system of your own and send the new text strings into the RC_text_box function.


Cp(Posted 2008) [#54]
Sounds good. Did you send it? Its not in my email...
EDIT:
One more thing:can I change the text mode whenever?If I can't, I'll need to build it in. shouldn't be too hard though.


Ross C(Posted 2008) [#55]
You can change the text mode yeah. I have included commands to clear the text box and reset it. And close it. I won't have time to finish it tonight, but to prove i have actualy done something, here is the code ;o)

I'll def have it with you tommorow. Initial tests looks good :o)

The below sample won't run without my additional setup code.




Cp(Posted 2008) [#56]
hmm...why won't it run?
this is the code:
Graphics 640,480

Include "textbox.bb"

RC_text_box_init()
RC_open_text_box(1)
RC_text_box("Blah,Blah,Blah BLAAAAAAAAAAAAAAAAH")
While Not KeyDown(1)
RC_update_text_box()
Wend

It says stuff like checking RGB and all that, but it doesn't display anything.
Should I be using AutoMidhandle True?
Whats the extra init code?
EDIT 2:
and also, what I mean by scrolling is a fixed textbox width and height. the text inside moves up as the lines get too big. If the text is above the box, its deleted.
EDIT AGAIN:
Never mind, i didn't read your post good enough. I NEED the extra code to run it. :)


mtnhome3d(Posted 2008) [#57]
and an line terminator in your text.


Ross C(Posted 2008) [#58]
Yeah :o) It's not completely finished yet. WIll finish it when i get in from work (famous last words<)


Cp(Posted 2008) [#59]
WIll finish it when i get in from work (famous last words<)

LOL


Ross C(Posted 2008) [#60]
Ok, i'm pretty much sure i have this finished now. You'll need to giev it the once over and check it works with everything you need.

A few points though:

You only need to provide an image for the backround. The code will work out the drawable text area. Since this isn't 3d, the code doesn't need to work which powers of 2 textures, which is nice :o)

I use border variables, which can be adjusted. This provides a padding space between the frame and the text area.

I have provided function names to adjust the text box and control it:

RC_text_box_init(background,frame)

This is used to setup the images needed and such. It will fail without this. You must pass across a preloaded backround image and preloaded frame image. This is so you can easily resize the textbox to another backround image, without midgame loading.


RC_set_font(font$,size,bold=0,italic=0,underline=0)

Simple really. Set the font you want to use, plus the size. You don't need to send any bold, italic or underline info to the function though, if you don't need too. BE WARNED. This code uses setfont every time it draws a character, so you will need to reset the font your using, if any, after the RC_update_text_box() function.

RC_open_text_box()

Opens a blank text box in mode 0 (continuous), ready for you to send text to it. Only will work on mode 0. Before sending off text to the text box, make sure you check to see if it's already open, via:

RC_text_box_status = 0 means no text box is open
the information is commented in the textbox.bb file

RC_close_text_box()

Simple. It closes the text box, clears the text and resets the character positions.

RC_adjust_y_border(value)

RC_adjust_x_border(value)

These two are pretty self evident. Set values for the borders/margins. Note that these margin position are measured from where the frame finishes. The code will add on the border thickness of the frame to the value you pass across.

RC_position_text_box(x,y,x_justify=0,y_justify=0)

With this function you can set the position of the text box manually, OR, justify it left/centre/right, on the two axis. So:

RC_position_text_box(0,0,2,2) would centre the text box on the x and y axis. use the following:

x_justify
---------
0 = use x co-ord passed across
1 = left justify
2 = centre the text box
3 = right justify

Same for the y_justify.

RC_set_font_color(R,G,B,style=0,r=0,g=0,b=0)

To set the font color used in your textbox. You only need to provide the RGB if needed.
The style command determines whether your text is outlined or not. 0 = no outline, 1 = outline darken, 2 = outline custom color.

In custom outline, you pass across the rgb colour of the outline you want. outline darken mode will simple choose a darker color for the outline.


Finally! When choosing a frame style, since i'm not using 3d anymore, as mentioned above, what i will be doing now, is taking an image strip for the frame. Basically:

You provide all the 8 pieces of the frame in this order:

Top left
Middle
Top right
Middle left
Middle right
Bottom left
Bottom middle
Bottom right.

They must be in a strip going left to right and they must be SQUARE in size. This is because i will load in the image. Take the image height, as use that as the width of each piece, and build the frame from that. That means you can make the border as thick as you please. The frame will be drawn onto of the textbox, not outside it, so the code will set the margins for you to start with.

Still having problems trying to figure out the quickest way to scale the frame pieces, but that's practically it :o)


Cp(Posted 2008) [#61]
OK.Umm....Wheres the code?
Also, what size of image do I need to use?never mind
Its not in my email?
One more thing...What about a top-middle frame?


Ross C(Posted 2008) [#62]
Yeah, i'm just touching some parts up. Not released just yet! I'll provide a sample frame image, so you get what i mean.


Cp(Posted 2008) [#63]
OK.


Ross C(Posted 2008) [#64]
Ok, i have sent it.

The justify aspect doesn't work yet, as i've ran out of time, but it's fully functional apart from that. I have included a small example. Please report any errors and i'll fix 'em!


Cp(Posted 2008) [#65]
whats with the windows live messanger link saying "Get Started"?
EDIT:
WHOA.Awesome! And I can just resize the RC_text_box_background!
Sooooooooooooooooooo awesome.
EDIT AGAIN:
OOPS. I forgot my manners. *Ahem* THANK YOOOOOU.
EDIT ONCE AGAIN:
I'm pretty sure I don't have the terminator on my keyboard.Where do i set it to something else?


Ross C(Posted 2008) [#66]
I don't know about that link. Probably because i use hotmail. It's not related to me in any way.

About the line terminator. Open up the include file, and search for this character ¬. Then just replace it with whatever you'd rather use.

On my keyboard, the ¬ key is at the very top left. One key left of the 1 key at the top of the key board:

¬1234567890-=

I have kept all my variables and function with the prefix of RC_ so try not to use any variables with those prefixes as they may overwrite mine.

I'm thinking of expanding this, so you can spawn as many windows as you want. Need to see if i have time :o)


Cp(Posted 2008) [#67]
nope. I don't have it on my keyboard. I'm using QWERTY.
I changed the character to a |(Pipe).


Ross C(Posted 2008) [#68]


Look at the key to the left of the 1 and above the TAB key. Top left corner of the keyboard. I thought that was a pretty standard key. You press shift to get it.


Cp(Posted 2008) [#69]

Here is mine:
I have ~.


Ross C(Posted 2008) [#70]
Ah, american layout i see :o)


mtnhome3d(Posted 2008) [#71]
when you are done finishing this please post the final version somewhere, maby the code archives.


Ross C(Posted 2008) [#72]
Ah good point, sorry :o)


mtnhome3d(Posted 2008) [#73]
thank you i would appreciate it much as at some point i might make an rpg or game that requires such and system.


Cp(Posted 2008) [#74]
hey, ross. is there some way to build the updating into the textbox function? just wondering.


Ross C(Posted 2008) [#75]
I'm not sure what you mean?


Cp(Posted 2008) [#76]
like: rc_text_box("blal blabberlah")
would automatically update the textbox, instead of calling
RC_update_textbox()
Because it can't print more than one thing at a time. If you feed it too much text before updating,it only prints the first line.


Ross C(Posted 2008) [#77]
You need to call the update function, as nothing will happen unless you do. You can't have code running, unless the compiler processes it :o) It shouldn't only print the most recent though. I will look into this. It sounds like a bug to me :o)


Cp(Posted 2008) [#78]
Urghh... I hate to revive this topic from the dead, but I have to.
So, I started ANOTHER TEXTBOX. It's going better than the last few(except Ross C's, but I can't use that in 320x240 windowed res.)
However, I've hit a snag. the following code produces the following output.



It doesn't print the rest of it. If anybody could point out my problem, I'd appreciate it.
[EDIT]Anybody? Oops, I forgot the phrase:Pokemon and Mudkip and mother series rock out loud! Woooohoo!


Ross C(Posted 2008) [#79]
I'll have a look over my old code if you want, and see whats up with it. It shouldn't have an problem doing 320 x 240 res.


Cp(Posted 2008) [#80]
Nono... It's not the res that's a problem,[correction] It's the TEXT SIZE compared to the res. And also, my textbox has a few different needs this time. The font color changer can be removed, and I need to use a different font than arial it also needs to be smaller.
I also need "Control Codes" like [font] that would switch to a different font, initiate a battle, etc., etc.
That's too much to ask for though, so, with my current code, why doesn't it print the whole text?


Ross C(Posted 2008) [#81]
Seems as if it's a problem with your word wrap function. That's the only thing i can think of, based on what i see. I could implement font changing in mine i suppose, if that helps?


Cp(Posted 2008) [#82]
I just found a bug in yours. I run it in 320x240, there is no frame, and when there is a newline, it deletes text on the lines above it, and just draws the newline. What is wrong with it?
No, Thank you for the offer, but I'd rather not cause you too much trouble. Thanks though.