Skip to content

Commit

Permalink
Added text to Button
Browse files Browse the repository at this point in the history
  • Loading branch information
bartez33a committed Nov 2, 2023
1 parent 106b9cf commit aa48ccf
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 19 deletions.
20 changes: 15 additions & 5 deletions src/Button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,38 @@

Button::Button(std::string text, point_t top_left_corner, unsigned int width, unsigned int height,
void (*callback)(void), MenuLevel* next_level)
: mText{text},
: mTextContent{text},
mCallback{callback},
MenuItem{top_left_corner},
mWidth{width},
mHeight{height},
mNextLevel{next_level} {}

bool Button::is_clicked(const sf::Event& event) {}

void Button::draw(sf::RenderWindow& window) {
void Button::draw(sf::RenderWindow& window, const sf::Font& font) {
// draw rectangle
sf::RectangleShape rectangle(sf::Vector2f(mWidth, mHeight));

// draw rectangle outline
if (mIsSelected) {
rectangle.setOutlineThickness(10.0f);
rectangle.setOutlineColor(sf::Color(255, 0, 0));
} else {
rectangle.setOutlineThickness(0.0f);
}

// set text
mText.setFont(font);
mText.setCharacterSize(24);
mText.setColor(sf::Color::Red);
mText.setString(mTextContent);

// move objects in window and draw
rectangle.move(mPosition);
window.draw(rectangle);
mText.move(mPosition);
window.draw(mText);
}

void Button::select() { mIsSelected = true; }

void Button::deselect() { mIsSelected = false; }
void Button::deselect() { mIsSelected = false; }
9 changes: 3 additions & 6 deletions src/Button.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,18 @@ using point_t = sf::Vector2f;
class MenuLevel;

class Button : MenuItem {
std::string mText;
std::string mTextContent;
sf::Text mText;
unsigned int mWidth;
unsigned int mHeight;

void (*mCallback)(void);

bool mIsSelected = false;

MenuLevel* mNextLevel;

public:
Button(std::string text, point_t top_left_corner, unsigned int width, unsigned int height,
void (*callback)(void), MenuLevel* next_level = nullptr);
bool is_clicked(const sf::Event& event);
void draw(sf::RenderWindow& window);
void draw(sf::RenderWindow& window, const sf::Font& font);
void select();
void deselect();
};
2 changes: 1 addition & 1 deletion src/GameManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <SFML/Graphics.hpp>

#include "Board.hpp"
#include "MainMenu.h"
#include "MainMenu.hpp"

enum class GameManagerState {
MainMenu,
Expand Down
11 changes: 8 additions & 3 deletions src/MainMenu.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
#include "MainMenu.h"
#include "MainMenu.hpp"

#include "Files.hpp"

MainMenu::MainMenu(sf::RenderWindow& window, std::initializer_list<MenuLevel*> menu_level_ptrs,
MenuLevel* current_level)
: mWindow{window}, mMenuLevelPtrs{menu_level_ptrs}, mCurrentLevel{current_level} {}
: mWindow{window}, mMenuLevelPtrs{menu_level_ptrs}, mCurrentLevel{current_level} {
mFont.loadFromFile(files::asset_path() + "DejaVuSans.ttf");
}

void MainMenu::process_and_draw(const sf::Event& event) {
// process inputs
mCurrentLevel->draw(mWindow, event);
mCurrentLevel->processEvents(event);

// draw menu
mCurrentLevel->draw(mWindow, mFont);

// perform actions
}
8 changes: 5 additions & 3 deletions src/MenuLevel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

MenuLevel::MenuLevel(std::initializer_list<Button> buttons) : mButtons{buttons} {}

void MenuLevel::draw(sf::RenderWindow& window, const sf::Event& event) {
void MenuLevel::processEvents(const sf::Event& event) {
// process input
static sf::Keyboard::Key lastPressedKey;
if (event.type == sf::Event::KeyPressed) {
Expand All @@ -26,8 +26,10 @@ void MenuLevel::draw(sf::RenderWindow& window, const sf::Event& event) {
} else if (event.type == sf::Event::KeyReleased) {
lastPressedKey = sf::Keyboard::Unknown;
}
}

// select button
void MenuLevel::draw(sf::RenderWindow& window, const sf::Font& font) {
// select button - draw outline
for (int i = 0; i < mButtons.size(); ++i) {
if (i == mSelectedButton) {
mButtons[i].select();
Expand All @@ -38,6 +40,6 @@ void MenuLevel::draw(sf::RenderWindow& window, const sf::Event& event) {

// draw buttons
for (auto& button : mButtons) {
button.draw(window);
button.draw(window, font);
}
}
3 changes: 2 additions & 1 deletion src/MenuLevel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ class MenuLevel {

public:
MenuLevel(std::initializer_list<Button> buttons);
void draw(sf::RenderWindow&, const sf::Event& event);
void draw(sf::RenderWindow&, const sf::Font& font);
void processEvents(const sf::Event& event);
};

0 comments on commit aa48ccf

Please sign in to comment.