Scrolling Label (code snippet)

Started by bulka_tarta, Thu 22/11/2018 07:44:28

Previous topic - Next topic

bulka_tarta

Hi!

This comes from a Discord chat, but I'm putting it here for future reference and hopefully someone will find it useful!
I was asking on Discord if there's a way to scroll a label in a GUI. eri0o suggested the approach which to take, and crimson wizard developed the code.

The idea here is to use a large label on a much smaller GUI. The GUI should be the size of area that you want the text to be visible on. Any part of the label that goes beyond the GUI will not be drawn to the screen. You can then add a slider which will move the label's position, and so revealing more text as you scroll.

crimson wizard was kind enough to work on the code to make the scrolling as nice as possible. You can see it in the snippet below.

Code: ags
int DEFAULT_LABEL_Y = 10;
int DEFAULT_PAGE_HEIGHT = 178;
int LABEL_LINE_SPACING = 1;

void DisplayQuest(String text)
{
    gQuestGui.Visible = true;
 
    lblQuestDetails.Text = text;
    int text_height = GetTextHeight(text, lblQuestDetails.Font, lblQuestDetails.Width + 1);
    int line_height = GetTextHeight("AAA", lblQuestDetails.Font, 100);
    int num_lines = text_height / line_height;
    lblQuestDetails.Height = text_height + (num_lines - 1) * LABEL_LINE_SPACING;
    
    lblQuestDetails.Y = DEFAULT_LABEL_Y;
    if (text_height <= DEFAULT_PAGE_HEIGHT)
    {
        sldQuestDetails.Visible = false;
    }
    else
    {
        sldQuestDetails.Visible = true;
        sldQuestDetails.Min = 0;
        sldQuestDetails.Max = lblQuestDetails.Height - DEFAULT_PAGE_HEIGHT;
        sldQuestDetails.Value = sldQuestDetails.Max;
    }
}

function sldQuestDetails_OnChange(GUIControl *control)
{
    lblQuestDetails.Y = DEFAULT_LABEL_Y -(sldQuestDetails.Max - sldQuestDetails.Value);
}


Again, big thanks to eri0o and crimson wizard for the help!

SMF spam blocked by CleanTalk