mirror of
https://github.com/HowestDAE/dae16-VerhulstBram.git
synced 2025-12-16 21:01:48 +01:00
45 lines
1.1 KiB
C++
45 lines
1.1 KiB
C++
#include "pch.h"
|
|
#include "GuiButton.h"
|
|
|
|
#include <iostream>
|
|
|
|
#include "colors.h"
|
|
#include "utils.h"
|
|
GuiButton::GuiButton(const std::string& filePath, Vector2f pos, Vector2f size, TextureManager* manager): m_Position(pos), m_Size(size) {
|
|
m_Texture = manager->GetTexture(filePath);
|
|
if(size.x == 0 && size.y == 0) {
|
|
m_Size = Vector2f{float(m_Texture->GetWidth()), float(m_Texture->GetHeight())};
|
|
}
|
|
std::cout << "Button created" << '\n';
|
|
}
|
|
GuiButton::~GuiButton() {
|
|
std::cout << "Button destroyed" << '\n';
|
|
}
|
|
void GuiButton::Draw() const {
|
|
Rectf dest = Rectf(m_Position, m_Size);
|
|
Rectf src = Rectf(0, 0, m_Texture->GetWidth(), m_Texture->GetHeight());
|
|
if(m_IsHovered) {
|
|
m_Texture->Draw(dest, src, false);
|
|
}
|
|
}
|
|
void GuiButton::Update(float elapsedSec) {
|
|
Vector2f mousePos = utils::GetMousePos();
|
|
Rectf buttonRect = Rectf(m_Position, m_Size);
|
|
|
|
this->m_IsHovered = utils::IsPointInRect(mousePos, buttonRect);
|
|
|
|
m_IsPressed = m_IsHovered && utils::IsMouseButtonDown(SDL_BUTTON_LEFT);
|
|
|
|
if(m_IsPressed && !m_IsPrimed) {
|
|
m_IsPrimed = true;
|
|
m_OnClick();
|
|
}
|
|
|
|
if(!m_IsPressed) {
|
|
m_IsPrimed = false;
|
|
}
|
|
}
|
|
|
|
|
|
|