<commit_before>#include "GuiButton.h"

#include <iostream>

#include "Gui.h"
#include "graphics/FontType.h"
#include "graphics/Texture.h"
#include "graphics/TextCentering.h"
#include "graphics/TextEngine.h"
#include "graphics/device/Device.h"

namespace gfx
{

namespace gui
{

unsigned const GuiButton::width = 190;
unsigned const GuiButton::height = 45;

std::string const GuiButton::textureName = "sys/gfx/bigbutton.bmp";
std::string const GuiButton::textureHoveredName = "sys/gfx/bigbutton_over.bmp";

GuiButton::GuiButton(Gui & gui, GuiElement * parent, math::Vector2i position,
		std::string const & text, FontType, std::function<void(void)> onPressed) :
	super(gui, parent, position),
	texture(gui.getDevice().grabTexture(gui.getModPath() + textureName)),
	textureHovered(gui.getDevice().grabTexture(gui.getModPath() + textureHoveredName)),
	text(text),
	isHovered(false),
	onPressed(onPressed)
{}

GuiButton::~GuiButton()
{
	gui.getDevice().releaseTexture(gui.getModPath() + textureName);
	gui.getDevice().releaseTexture(gui.getModPath() + textureHoveredName);
}

void GuiButton::draw()
{
	auto & device = gui.getDevice();
	auto & textEngine = gui.getTextEngine();

	device.draw2DImage(isHovered ? textureHovered : texture, getPosition());
	if (!text.empty()) {
		math::Vector2i textPosition = getPosition() + math::Vector2i(width, height) / 2;
		textEngine.drawText(isHovered ? NormalOverFont : NormalFont, text, textPosition, Centered);
	}
}

bool GuiButton::onMouseButtonPressed(uint8_t button, int x, int y)
{
	if (!isVisible())
		return false;

	math::Vector2i lowerRight = getPosition() + math::Vector2i(width, height);

	bool pressed = (x > getPosition().x && y > getPosition().y && x < lowerRight.x && y < lowerRight.y);
	if (pressed && onPressed != nullptr)
		onPressed();

	return super::onMouseButtonPressed(button, x, y);
}

bool GuiButton::onMouseMoved(int x, int y)
{
	if (!isVisible())
		return false;

	math::Vector2i lowerRight = getPosition() + math::Vector2i(width, height);

	isHovered = (x > getPosition().x && y > getPosition().y && x < lowerRight.x && y < lowerRight.y);

	return super::onMouseMoved(x, y);
}

void GuiButton::onVisibilityChange(bool isVisible)
{
	if (!isVisible) {
		isHovered = false;
	} else {
		onMouseMoved(gui.getMousePosition().x, gui.getMousePosition().y);
	}
}

} // namespace gui<commit_msg>Fixed GuiButton triggering on any mouse button click<commit_after>#include "GuiButton.h"

#include <iostream>

#include "Gui.h"
#include "graphics/FontType.h"
#include "graphics/Texture.h"
#include "graphics/TextCentering.h"
#include "graphics/TextEngine.h"
#include "graphics/device/Device.h"
#include "input/Mouse.h"

namespace gfx
{

namespace gui
{

unsigned const GuiButton::width = 190;
unsigned const GuiButton::height = 45;

std::string const GuiButton::textureName = "sys/gfx/bigbutton.bmp";
std::string const GuiButton::textureHoveredName = "sys/gfx/bigbutton_over.bmp";

GuiButton::GuiButton(Gui & gui, GuiElement * parent, math::Vector2i position,
		std::string const & text, FontType, std::function<void(void)> onPressed) :
	super(gui, parent, position),
	texture(gui.getDevice().grabTexture(gui.getModPath() + textureName)),
	textureHovered(gui.getDevice().grabTexture(gui.getModPath() + textureHoveredName)),
	text(text),
	isHovered(false),
	onPressed(onPressed)
{}

GuiButton::~GuiButton()
{
	gui.getDevice().releaseTexture(gui.getModPath() + textureName);
	gui.getDevice().releaseTexture(gui.getModPath() + textureHoveredName);
}

void GuiButton::draw()
{
	auto & device = gui.getDevice();
	auto & textEngine = gui.getTextEngine();

	device.draw2DImage(isHovered ? textureHovered : texture, getPosition());
	if (!text.empty()) {
		math::Vector2i textPosition = getPosition() + math::Vector2i(width, height) / 2;
		textEngine.drawText(isHovered ? NormalOverFont : NormalFont, text, textPosition, Centered);
	}
}

bool GuiButton::onMouseButtonPressed(uint8_t button, int x, int y)
{
	if (!isVisible())
		return false;
	if (button == mouse::Left) {
		math::Vector2i lowerRight = getPosition() + math::Vector2i(width, height);

		bool pressed = (x > getPosition().x && y > getPosition().y && x < lowerRight.x && y < lowerRight.y);
		if (pressed && onPressed != nullptr) {
			onPressed();
			return true;
		}
	}

	return super::onMouseButtonPressed(button, x, y);
}

bool GuiButton::onMouseMoved(int x, int y)
{
	if (!isVisible())
		return false;

	math::Vector2i lowerRight = getPosition() + math::Vector2i(width, height);

	isHovered = (x > getPosition().x && y > getPosition().y && x < lowerRight.x && y < lowerRight.y);

	return super::onMouseMoved(x, y);
}

void GuiButton::onVisibilityChange(bool isVisible)
{
	if (!isVisible) {
		isHovered = false;
	} else {
		onMouseMoved(gui.getMousePosition().x, gui.getMousePosition().y);
	}
}

} // namespace gui
{'fraction_non_alphanumeric': 0.1564964715649647, 'fraction_numerical': 0.0062266500622665, 'mean_word_length': 5.768258426966292, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4419974', 'n_tokens_mistral': 1730, 'n_tokens_neox': 1666, 'n_words': 337}