Dem1se

1,814 bytes added ,  06:48, 12 November 2021
Add supporting code for Create UIs section
Line 33: Line 33:  
## This method is called by Stardew Valley's draw code every render tick whenever your UI is actively displayed.
 
## This method is called by Stardew Valley's draw code every render tick whenever your UI is actively displayed.
 
## The latter statements draw '''over''' the earlier statment's output. So things like the cursor should always be drawn at last so they are on top of everything else.
 
## The latter statements draw '''over''' the earlier statment's output. So things like the cursor should always be drawn at last so they are on top of everything else.
 +
 +
===== The general code layout will look something like this: =====
 +
 +
<syntaxhighlight lang="c#">
 +
public class MyUserInterface : IClickableMenu
 +
{
 +
    // Some of the constants you'll use to relatively lay out all the UI elements
 +
    int UIWidth = 632;
 +
    int UIHeight = 600;
 +
    int XPos = (Game1.viewport.Width / 2) - (UIWidth / 2);
 +
    int YPos = (Game1.viewport.Height / 2) - UIHeight;
 +
   
 +
    // Declare all the UI elements
 +
    ClickableComponent TitleLabel;
 +
    ...
 +
 +
    public MyUserInterface()
 +
    {
 +
        base.initialize(XPos, YPos, UIWidth, UIHeight);
 +
 +
        // initialize and lay out all the declared UI components
 +
        TitleLabel = new ClickableComponent(new Rectangle(XPos + 200, YPos + 96, UIwidth - 400, 64), &quot;Some Title&quot;);
 +
        ...
 +
    }
 +
 +
    // The method invoked when the player left-clicks on the menu.
 +
    public override void receiveLeftClick(int x, int y, bool playSound = true)
 +
    {
 +
        if (TitleLabel.containsPoint(x, y))
 +
        {
 +
            // handle user clicking on the title. More practical use-case would be with buttons
 +
            ...
 +
        }
 +
    }
 +
 +
    ...
 +
 +
    // Render the UI that has been set up to the screen.
 +
    // Gets called automatically every render tick when this UI is active
 +
    public override void draw(SpriteBatch b)
 +
    {
 +
        //draw screen fade
 +
        b.Draw(Game1.fadeToBlackRect, Game1.graphics.GraphicsDevice.Viewport.Bounds, Color.Black * 0.75f);
 +
 +
        // draw menu dialogue box
 +
        Game1.drawDialogueBox(XPos, YPos, UIWidth, UIHeight, false, true);
 +
 +
        // draw the TitleLabel
 +
        Utility.drawTextWithShadow(b, TitleLabel.name, Game1.dialogueFont, new Vector2(TitleLabel.bounds.X, TitleLabel.bounds.Y), Color.Black);
 +
        ...
 +
 +
        // draw cursor at last
 +
        drawMouse(b);
 +
    }
 +
}
 +
</syntaxhighlight>
21

edits