Skip to content

Creating a GUI: Adding the menubar

Rodrigo Cabaço edited this page Aug 14, 2020 · 5 revisions
Creating a basic menu bar in the GUI
Drawing it

First, we need to find the width and the height of our screen. I am currently using 320,200 as my size.

for(int w = 0; w < 320; w++)
{

    for(int h = 0; h < 200; h++
    {

    }
}

Second, we need to get x and y to set it in.

for(uint w = 0; w < 320; w++)
{

    for(uint h = 0; h < 30; h++)
    {
       uint x = w;
       uint y = 170 - h;   
    }

}

Finally, we need to SET the pixels

for(uint w = 0; w < 320; w++)
{

    for(uint h = 0; h < 30; h++)
    {

       uint x = w;

       uint y = 170 - h;   

       screen.SetPixel(x,y,30) //SetPixel(x,y,color)

    }
}

and there we go, we just made a rectangle at the bottom of the screen.

Mouse Handling

When creating buttons in your GUI, you'll want to check whether the Mouse's X or Y coordinates are inside the area of the rectangle whilst the mouse button was pressed. This simple snippet can be used to perform this check. If the mouse button is pressed and if its coordinates are inside the rectangle's area, then it will perform the function specified. (ifMouseClicked();)

In this example, there is no need to check the X coordinate, but you may want to in your Operating System's GUI.

if (mouse.Buttons == Mouse.MouseState.Left && mouse.y >= 170) //No need to check x
{
     ifMouseClicked();
}
Clone this wiki locally