Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions src/button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,40 @@ Button::~Button()

void Button::Draw()
{
DrawTextureV(texture, position, WHITE);
DrawTextureV(texture, position, (wasHoveredBefore ? darkenColor : normalColor));
}

void Button::SetPosition(Vector2 newPosition)
{
position = newPosition;
}

bool Button::isPressed(Vector2 mousePos, bool mousePressed)
{
Rectangle rect = {position.x, position.y, static_cast<float>(texture.width), static_cast<float>(texture.height)};

bool isHovered = CheckCollisionPointRec(mousePos, rect);

if(CheckCollisionPointRec(mousePos,rect) && mousePressed)
if (!isHovered && wasHoveredBefore)
{
return true;
UnHovered();
wasHoveredBefore = false;
}
return false;
else if (isHovered && !wasHoveredBefore)
{
Hovered();
wasHoveredBefore = true;
}

return isHovered && mousePressed;
}

void Button::Hovered()
{
SetMouseCursor(MOUSE_CURSOR_POINTING_HAND);
}

void Button::UnHovered()
{
SetMouseCursor(MOUSE_CURSOR_DEFAULT);
}
9 changes: 9 additions & 0 deletions src/button.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,16 @@ class Button
~Button();
void Draw();
bool isPressed(Vector2 mousePos, bool mousePressed);
void SetPosition(Vector2 newPosition);

private:
Texture2D texture;
Vector2 position;
bool wasHoveredBefore = false;

Color normalColor = WHITE;
Color darkenColor = { 50, 50, 50, 255 };

void Hovered();
void UnHovered();
};