Button - VictorAlvizo/Ventura GitHub Wiki

Button(unsigned int windowWidth, unsigned int windowHeight, std::shared_ptr buttonTexture, glm::vec2 pos, glm::vec2 size, std::string buttonText = "", float rotation = 0.0f, unsigned int fontSize = 0, std::string customFont = "Fonts/arial.ttf", glm::vec2 hitboxOffset = glm::vec2(0.0f), glm::vec2 hitboxSize = glm::vec2(0.0f))

This constructer is to be used when custom textures for the varying button states are not wanted. For the button texture, put in "button" for the default button texture, or put in another texture name and it will set the different button states to that texture as well. Putting in 0 for the font size will get the computer to automatically assign a font size that best fits the button according to its size. The default font is arial. Putting zero for the hitbox size will make the hitbox size equal to the button size. The window width and height must match the Game's window width and height.

Example: Button * button = new Button(m_GameClass->m_Width, m_GameClass->m_Height, "button", glm::vec2(0.0f), glm::vec2(300.0f, 200.0f), "Click Me!");

Button(unsigned int windowWidth, unsigned int windowHeight, std::shared_ptr buttonTexture, std::shared_ptr hoverTexture, std::shared_ptr clickTexture, glm::vec2 pos, glm::vec2 size, std::string buttonText = "", float rotation = 0.0f, unsigned int fontSize = 0, std::string customFont = "Fonts/arial.ttf", glm::vec2 hitboxOffset = glm::vec2(0.0f), glm::vec2 hitboxSize = glm::vec2(0.0f))

This constructer is to be used when custom textures for the varying button states are wanted. Putting in 0 for the font size will get the computer to automatically assign a font size that best fits the button according to its size. The default font is arial. Putting zero for the hitbox size will make the hitbox size equal to the button size. The window width and height must match the Game's window width and height.

Example: Button * button = new Button(m_GameClass->m_Width, m_GameClass->m_Height, "normalStateTexture", "hoverStateTexture", "clickedStateTexture", glm::vec2(0.0f), glm::vec2(300.0f, 200.0f), "Click Me!");

~Button()

The button deconstructer, frees up the TextRenderer and Hitbox object pointers inside the button object

void Draw(SpriteRenderer& spriteRenderer, bool drawHitbox = false, bool followCamera = true, glm::vec4 buttonColor = glm::vec4(1.0f), glm::vec3 textColor = glm::vec3(0.0f), glm::vec3 hitboxColor = glm::vec3(0.0f, 1.0f, 0.0f), glm::vec2 textOffsets = glm::vec2(0.0f))

Provide the SpriteRenderer that should be used to draw the button and decide whether or not to draw the hitbox of the button. If follow camera is true, no matter where the camera is, the button will be stuck to the position on the camera, unaffected by camera movement. Provide the button color (default white), the text color (default black), the color of the hitbox (default green) and the text offset in regards to the button position. If the text offset is left at 0.0f, the computer will decide the text's best placement.

Example: button->Draw(*m_SpriteRenderer, false, true);

void SetPos(glm::vec2 newPos)

Set the position of the button Example: button->SetPos(glm::vec2(250.0f)); //Set the button position to (250, 250)

void SetRotation(float newRotation)

Rotate the button

void SetDelegate(const std::function<void()>& func)

By setting a delegate, the method provided will be called everytime the button is clicked. The method provided must return void and have no parameters.

Example: std::function<void()> printFunc() = []() {"Hello! The button was clicked!"}; //Lamda to provide as a delegate, will be called everytime button is clicked button->SetDelegate(printFunc); //Give the lamda to the button to call everytime it's clicked.

void ChangeFont(unsigned int fontSize, std::string fontPath = "")

Change the font size or the font that is being used. If nothing ("") is provided for the font path, the same font will remain in usage. Example: button->ChangeFont(24, "Fonts/Cambria.ttf"); //Change the font size to 24 and use the font Cambria instead

bool isHovering(glm::vec2 mousePos, bool followingCamera, glm::vec2 cameraPos)

Check if the mouse is hovering over the button. Provide the mouse position and whether the button is being affected by the camera position or not. If it's not being affected by the camera (followingCamera is false) anything in cameraPos is fine. But if the movement of the camera does affect the button, put in the camera position of the button.

Example: button->isHovering(m_GameClass->m_MousePos, false, m_GameClass->m_Camera->getPos());

bool isClicked(glm::vec2 mousePos, bool mouseButton, int& mouseAllowement, bool followingCamera, glm::vec2 cameraPos);

Check if the button has been clicked. Will call its delegate (if one has been provided) if it has been clicked. The mouse button to look for, the mouse allowment to ensure only 1 click if processed. Provide the mouse position and whether the button is being affected by the camera position or not. If it's not being affected by the camera (followingCamera is false) anything in cameraPos is fine. But if the movement of the camera does affect the button, put in the camera position of the button.

Example: button->isClicked(m_GameClass->m_MousePos, m_GameClass->m_MouseButtons[GLFW_MOUSE_BUTTON_1], m_GameClass->m_MouseButtonAllowment[GLFW_MOUSE_BUTTON_1], false, m_GameClass->m_Camera->getPos())

Status currentStatus()

Returns the current state of the button

glm::vec2 getPos()

Returns the current position of the button

glm::vec2 getSize()

Returns the current size of the button

⚠️ **GitHub.com Fallback** ⚠️