Jump to content

GUI with scroll log


Recommended Posts

Hello,

I have GUI set up, but I need to have a scrolling text log for it where I can input messages, similar to the debug log. At the moment I'm doing this through a label, as found in the Unity documentation, but it adds new lines through expanding the string, and I forsee problems here...

      		scrollPosition = GUILayout.BeginScrollView(scrollPosition, GUILayout.Width(300), GUILayout.Height(300));
GUILayout.Label(longString);
if (GUILayout.Button("Clear"))
longString = "";

GUILayout.EndScrollView();
if (GUILayout.Button("Add More Text")) longString += "\nHere is another line";

Is there a way I could achieve this? Basically I need longstring to input a new line every time with a new string, and not expand it... I guess that label is not the way to go for this. Could a text box be better, although I don't need the player to write anything in it?

Link to comment
Share on other sites

There are actually a couple tricks I found in doing the GUI for my AGX mod that will help.

First, you can calculate label positions as follows:


List<string> messageLog = new List<string>();
//add string messages to log somwhere


//inside the GUI window/scrollwindow
//note this is not a GUILayout, rather it is a GUI.Window that I am pulling this from but I'm pretty sure it still works

for(int i = 1;i <= messageLog.count;i++)
{
GUI.Label(new Rect(10,10 + ((i-1) * 20),100,20),messageLog.ElementAt(i-1),YourGuiStyleHere)
}

This will draw a label for each string entry in the messageLog list, stacking them downwards.

The first label will be anchored at coordinate 10,10 and is 100 wide by 20 high.

The second label will be anchored at coordiante 10,30, and so on.

The magic is in the y-coordinate of the position rectangle:

10 + ((i-1) * 20)

10: This is the offset of 10 down to start at.

+ : we then want to add 20 pixels for each label

(i-1): because the anchor is at the top of the label, we want to move down the amount of labels above us, not counting us. So label #3 needs to move down 2 label widths

*20: A label is 20 pixels high, so move down 20 pixels for the number of previous labels we have.

Then for our label string, pull the correct entry via messageLog.ElementAt(i-1)

Note that .ElementAt is a zero start, so label #1 is element #0, label #2 is element #1 and so on.

You can also do the same from a scroll window size, define the y coordinate of the interior scroll box as

(messageLog.count *20) + 20

to make it the correct height, with 10 pixels top and bottom for a border.

It's an uncommented mess, but you can see where I actually do this in my code at my github here.

Hope that helps,

D.

Link to comment
Share on other sites

Thanks a lot Diazo,

This is just what I need. I will study your code and see how to implement it. Hope you don't mind if I use some lines of code from your mod though! I'm just starting to mod, and I'm still a bit lost...

For the GUI I have been using the KSP example on the wiki and trying to expand on it, so I might have to redo some bits.

By the way, your code looks incredibly clean, you should see mine. I only have a couple of lines and I can't find anything in it already...

Link to comment
Share on other sites

Heh, we learn as we go.

I just recently ripped AGX apart for a total rewrite and the commentary is lagging behind, my vertical velocity mod is more in line with what I consider acceptable commentary in the code.

As for grabbing my code, go ahead. I license my mods under the GPL3 license for that exact reason. (Only 2 limitations are that you can't use my code in a commercial product and you have to release your mod under a license that allows people to use your code, or at least the part of your code that uses my code, in a project of their own as well.)

D.

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...