Text Box Positioning

Ask and answer questions about making games and related topics. Unrelated topics go in that other forum.

Moderators: marionline, SDHawk

Post Reply
User avatar
kylekrack
Liquid Metal Slime
Posts: 1242
Joined: Mon Jun 16, 2014 8:58 am
Location: USA
Contact:

Text Box Positioning

Post by kylekrack »

Ok, so in my new endeavor of making a point and click adventure, I've encountered a new dilemma in regards to the GUI. I'd still like to use the OHR's text boxes, because it will make scripting and other things much easier, however, I have the text area for the game in the bottom, shifted to the right. You can change the vertical position of the text boxes in the editor, but the horizontal positioning isn't an option.

I'd like to know if anyone knows of a way to adjust the width of a text box, because shifting them can likely be done simply with some parenting. I just don't want to make a new slice collection for every single text box or something cray like that. Although that might not be that bad...
Attachments
Screen Shot 2015-05-25 at 2.24.48 AM.png
Screen Shot 2015-05-25 at 2.24.48 AM.png (43.76 KiB) Viewed 1087 times
My pronouns are they/them
Ps. I love my wife
User avatar
Gizmog
Metal King Slime
Posts: 2622
Joined: Tue Feb 19, 2008 5:41 am

Post by Gizmog »

Lookup Slice has some codes that'll be pretty useful. I think the one you'll want is

Code: Select all

lookup slice(sl:textbox box)
You should be able to resize and position to your hearts content. Sometimes the special lookup slices don't like being reparented though, so be on the look out for that. And TextboxBox is only a valid slice when there's a textbox being displayed, so your code will have to detect when a textbox is shown and lookup that slice each time.


EDIT: Or for a more low-tech solution, especially since you've got a pretty ood border drawn, you could just have boxless textboxes and hit space a bunch of times to indent 'em over to that side of the scsreen.
Last edited by Gizmog on Tue May 26, 2015 8:29 am, edited 1 time in total.
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

You'll notice that there are quite a lot of textbox-related lookup codes.
Every time a textbox is opened, the existing textbox slices if any are deleted, and new ones created. sl:textbox root is the lookup code for the bottommost slice that gets deleted and recreated. Only sl:textbox layer is preserved, and the root slice is set to 'fill parent'. So you can permanently change the size and position of textboxes by moving and resizing the sl:textbox layer slice. If you change its y position, be sure to use a y position of 0 in the textbox editor, otherwise it'll be off the bottom of the screen.

You can also store text in regular textboxes, read the text out with the "textbox line" command, and put it into a text slice (using just a single slice collection for your custom textboxes).
User avatar
kylekrack
Liquid Metal Slime
Posts: 1242
Joined: Mon Jun 16, 2014 8:58 am
Location: USA
Contact:

Post by kylekrack »

Ok well I'm making some progress but I am still having issues with the text boxes formatting correctly. The positioning seems to work the way I've done it, but the width doesn't seem to want to change, and it's not clear why.

Here's the game with the scripts along with it:
http://www.slimesalad.com/forum/viewgame.php?p=116628

If you play it you can see what's going on. The text boxes go into the right place, but the words go off the right side of the screen. I assume this has something to do with the 'fill parent' of the textbox layer, but I don't know how to fix this. I use a script that repositions the textbox box slice for reasons I will explain later.

Code: Select all

script, show positioned box, num, begin
	variable(sl)
	show text box(num)
	sl := lookup slice(sl:textbox box)
	set slice width(sl,240)   ##### Width doesn't change??
	put slice(sl,75,126)
	set horiz align(sl,edge:left)
	set horiz anchor(sl,edge:left)
end
This is what I use to show the text boxes that open up, but there's no error message explaining anything about why the width doesn't change. However, the same does not go for the textbox layer itself, for when I try to format that in the initial script, it gives the error message that this slice cannot be altered in that way.

This script piece:

Code: Select all

variable(sl)
	sl := lookup slice(sl:textbox layer)       #### Text box perma moving
	set slice width(sl,240)
	put slice(sl,75,126)
It tells me this slice's width cannot be changed. That's why I use the previous script to alter each textbox individually once it's loaded.

So in whatever way possible, I'd like to get all the words on the screen without painstakingly going in and typing all the textboxes to make sure they don't surpass a certain point on the screen. That does not sound worth my time, especially given how much time it takes to draw one of these screens. [/quote]
My pronouns are they/them
Ps. I love my wife
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

OK, several things...

In the second script snippet, you're right that you can't change the width of the slice because it's set to fill its parent. The solution is to use "fill parent(sl, false)" to turn off filling before you set the width.

In the first example you're trying to resize a rect slice, and the actual text slice sl:textbox text is parented to it and set to fill but is not set to wrap. Your script is actually successfully setting the width of the rect (don't forget that you can use the slice debugger (Ctrl+F4) to check). However, you can only change the width of a textslice if it's set to wrap. (Aside: as a weird quirk, if a text slice or other non-resizeable slice (a sprite) is set to fill its parent then its width will change, but if it's not actually resizeable then this has no effect on the way it's drawn.) So what you could do is set the textbox text slice to wrap:

Code: Select all

set wrap(lookup slice(sl:textbox text), true)

But bad news, just reducing the width of the text isn't going to cause it to nicely wrap, as there are hard new-lines at the end of each line. You'll get something like this:

Image

So what can you do about this? Well you could write a script to trim the new lines out of the text so that it wraps as one big block... assuming that you don't want any at all. (Almost certainly not what you want, but I wrote this up quickly for fun.)

Code: Select all

script, remove newlines from textbox, begin
  variable(sl, idx)
  sl := lookup slice(sl:textbox text)
  get slice text(0, sl)  # into string 0
  for (idx, string length(0), 1, -1) do (  # loop backwards
    # ASCII 10 is the newline character
    if (ascii from string(0, idx) == 10) then (delete char(0, idx))
  )
  set slice text(sl, 0)
end
Or you could type your textboxes into text slices in the slice collection editor, or type them into your scripts as strings.

Finally, there is actually a missing error message when trying to resize a text slice, which is a bug (resizing text slices is odd, because you can only change their width if they're set to wrap, and you can't set their height in either case). (I need to add the new error messages in a way that won't cause errors to pop up in existing games.) But you didn't encounter this because you weren't trying to resize a text slice.
Last edited by TMC on Wed Jun 10, 2015 10:39 am, edited 3 times in total.
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

OK, so I actually went and played your demo this time. This is looking very nice! I really like the hand-drawn screens and the bits of polish like the music toggling.

Looking through the scripts (which I think are pretty well written), I noticed:

Code: Select all

	else if(clicked on slice(sli:clickable)) then(
		switch(get slice extra(lookup slice(sli:clickable),0)) do, begin
			case (0) #sound stuff
			case (1) #music stuff
			case (2) #style stuff
			case (3) save in slot(0)
			case (4) #load game stuff
			case (5) game over  #######NONE OF THIS WORKS
		end
	)
Beware.

Code: Select all

			case (2) #style stuff
			case (3) save in slot(0)
is equivalent to

Code: Select all

			case (2,3) save in slot(0)
If you don't want the cases to combine, then you need to put something (other than a comment) between them. Putting "do()" there will do the job.

Well, I guess you already know why this block of code won't work. However, being able to set slice extra data to define what should happen when you click on a slice would be nice. What you would have to do is to use "slice at pixel" or "find colliding slice" to loop over all the slices at a point (keep incrementing the 'number' argument until it returns 0), and check whether any of them has the 'clickable' lookup code.
Last edited by TMC on Wed Jun 10, 2015 5:14 pm, edited 1 time in total.
User avatar
kylekrack
Liquid Metal Slime
Posts: 1242
Joined: Mon Jun 16, 2014 8:58 am
Location: USA
Contact:

Post by kylekrack »

Well, sadly I discovered a new problem with this whole idea, which is the wrapping of it. In the text boxes, it stores new paragraphs as well, so when you change the width of a box, even though it wraps at the new width, the paragraphs still occur at the same places in the sentence, creating early breaks in the text and therefore wasting room.

I think I'm going to have to make text slices like you suggested before, which is not optimal, but it looks like it's going to be the best course of action here. Thank you for your help on the topic, and also for looking through my scripts, I really appreciate it. I'll keep trying on it.
My pronouns are they/them
Ps. I love my wife
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

Yes, that was the new-lines problem I posted a screenshot of. But my script to just remove all the hard new lines isn't going to work for your situation because you do have some newlines (paragraph breaks) that you want to keep.
User avatar
kylekrack
Liquid Metal Slime
Posts: 1242
Joined: Mon Jun 16, 2014 8:58 am
Location: USA
Contact:

Post by kylekrack »

I did it and it works perfectly! It's actually better this way because I don't have to worry about text boxes drawing over other slice collections that I load. I also got clickable areas to work and everything else and I can't believe it but I think the engine is pretty much complete. I just have to draw the rest of the screens and write the rest of the text boxes and write more music if I have time. I will upload the newest version. Thank you again for your help.
My pronouns are they/them
Ps. I love my wife
User avatar
Foxley
Metal Slime
Posts: 832
Joined: Sat Nov 09, 2013 5:54 pm

Post by Foxley »

I'm at the point where I need to implement text descriptions in my game. As opposed to kylekrack's approach I'm thinking that typing text descriptions right into the .HSS might be easier to organize and keep track of than having tons of text boxes. Problem is, the text doesn't seem to be wrapping based on the parent container slice's width.

The "print text" script (by the way, 'displayedtext' and 'textbox' are global variables, with textbox looking up sli:textboxcontainer which is located on the bottom right of the HUD):

Code: Select all

script, print text, textstuff, begin
	
	if (displayedtext) then (free slice (displayedtext))
	displayedtext := create text
	set wrap (displayedtext, true)
	set slice text (displayedtext, textstuff)
	set parent (displayedtext, textbox)
end
An example of how I'm using it in responses to player actions:

Code: Select all

case (else) do (print text ($1="YOU CAN'T GO THAT WAY. SERIOUSLY, YOU CAN'T."))
Anything after the first sentence hangs way the hell offscreen. Before I put in "set wrap", none of it wrapped. Now only "CAN'T." shows up on the second line.

Also, if there is a better way to script descriptive text, suggestions are welcome. Like, I'm not sure if using $id= is even the best approach.
User avatar
Bob the Hamster
Lord of the Slimes
Posts: 7660
Joined: Tue Oct 16, 2007 2:34 pm
Location: Hamster Republic (Ontario Enclave)
Contact:

Post by Bob the Hamster »

You do set wrap on displayed text, but you did not set it to fill its parent slice
User avatar
Foxley
Metal Slime
Posts: 832
Joined: Sat Nov 09, 2013 5:54 pm

Post by Foxley »

Aaaand that did it. Thanks!
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

That basically is the best way to script it. Actually, I'm glad to see someone finally make use of being able to write $1="" as a script argument.
User avatar
Foxley
Metal Slime
Posts: 832
Joined: Sat Nov 09, 2013 5:54 pm

Post by Foxley »

To be honest, I didn't even expect it to work when I tried it!
Post Reply