[SOLVED] Prevent the verb to overflow screen when mouse cursor near the edges

Started by Héctor Bometón, Thu 03/11/2022 17:25:58

Previous topic - Next topic

Héctor Bometón

In my game the verb is always following the mouse cursor, with the text centered over it. How could I prevent to overflow the screen limits when the text string is too long or the object is too near the edges? (see attached)

Speech displays automatically displaced when a character is on the side of the screen, so I was wondering if maybe there are already some functions for automating this.

Thank you!


Crimson Wizard

There are no built-in functions, but the solution was mentioned on the forums a number of times:

* find out the width and height of text;
* calculate the text's corner positions;
* use the above to find out if the label crosses the screen borders;
* if it does, then move label (or rather gui) to the screen border.

Code: ags
function repeatedly_execute_always()
{
    // Measure the text
    int width = GetTextWidth(MyLabel.Text, MyLabel.Font);
    int height = GetFontHeight(MyLabel.Font);
    // if you're using older versions of AGS, then use this instead:
    // int height = GetTextHeight(MyLabel.Text, MyLabel.Font, width + 1); // +1 needed because of the bug in AGS

    // Measure the text position on screen
    // NOTE: label's position is relative to GUI. If label is strictly at 0,0 on GUI,
    // then you may skip adding and subtracting MyLabel.X/Y below
    int x1 = MyGUIWithLabel.X + MyLabel.X;
    int y1 = MyGUIWithLabel.Y + MyLabel.Y;
    int x2 = x1 + width;
    int y2 = x2 + height;

    // Detect text crossing the screen edges
    // In older versions of AGS use System.ScreenWidth and System.ScreenHeight instead
    if (x1 < 0) x1 = 0;
    if (x2 >= Screen.Width) x1 = Screen.Width - width;
    if (y1 < 0) y1 = 0;
    if (y2 >= Screen.Height) y1 = Screen.Height - height;

    // Reposition the gui
    MyGUIWithLabel.X = x1 - MyLabel.X;
    MyGUIWithLabel.Y = y1 - MyLabel.Y;
}

Nahuel

That's strange  :confused:  I was using the same template and it's pushing the text from the edge, but I don't know if it's a resolution issue (300x200)



Life isn't a game. Let's develop a life-like-game.

Crimson Wizard

Quote from: Nahuel on Thu 03/11/2022 18:44:42That's strange  :confused:  I was using the same template and it's pushing the text from the edge, but I don't know if it's a resolution issue (300x200)

My guess then is that this may be a different template that looks similar, or an older version of 9-verb template. It may be even a completely custom game UI scripted from scratch.

Héctor Bometón


Thank you everybody. Your code was helpful and the issue is solved now.
Sorry I missed the posts that already explain how to do this.

SMF spam blocked by CleanTalk