Skip to content

Commit

Permalink
Add widgets.cpp.
Browse files Browse the repository at this point in the history
  • Loading branch information
ryouze committed Jan 7, 2025
1 parent 9f9725c commit 1318975
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 68 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ add_library(${PROJECT_NAME}-lib STATIC
src/modules/vocabulary.cpp
src/ui/circles.cpp
src/ui/components/base.cpp
src/ui/widgets.cpp
)

# Include headers relatively to the src directory
Expand Down
90 changes: 90 additions & 0 deletions src/ui/widgets.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/**
* @file widets.cpp
*/

#include <SFML/Graphics.hpp>

#include "core/settings/colors.hpp"
#include "widgets.hpp"

namespace ui::widgets {

Memo::Memo()
{
// Appearance
this->text_.setCharacterSize(16);
this->text_.setFillColor(core::settings::colors::text);

// Position
this->text_.setPosition({core::settings::screen::CENTER.x,
core::settings::screen::CENTER.y - 30.f});
}

void Memo::hide()
{
this->text_.setString("");
}

void Memo::set(const std::string &str)
{
this->text_.setString(str);
this->text_.resetOrigin();
}

void Memo::draw(sf::RenderWindow &window) const
{
window.draw(this->text_);
}

Percentage::Percentage()
: correct_answers_(0),
total_answers_(0)
{
// Appearance
this->text_.setCharacterSize(18);
this->text_.setFillColor(core::settings::colors::text);

// Position
constexpr float top_left_offset = 10.f; // Offset from the top-left corner
this->text_.setPosition({core::settings::screen::TOP_LEFT.x + top_left_offset,
core::settings::screen::TOP_LEFT.y + top_left_offset});
this->update_text();
}

void Percentage::add_correct_answer()
{
++this->correct_answers_;
++this->total_answers_;
this->update_text();
}

void Percentage::add_incorrect_answer()
{
++this->total_answers_;
this->update_text();
}

void Percentage::reset()
{
this->correct_answers_ = 0;
this->total_answers_ = 0;
this->update_text();
}

void Percentage::draw(sf::RenderWindow &window) const
{
window.draw(this->text_);
}

void Percentage::update_text()
{
float percent = 100.f; // If no answers yet, default to 100%
if (this->total_answers_ > 0) {
percent = (static_cast<float>(this->correct_answers_) /
static_cast<float>(this->total_answers_)) *
100.f;
}
this->text_.setString(fmt::format("게임 점수: {:.1f}%", percent));
}

} // namespace ui::widgets
81 changes: 13 additions & 68 deletions src/ui/widgets.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,44 +28,26 @@ class Memo {
/**
* @brief Construct a new Memo object.
*/
explicit Memo()
{
// Appearance
this->text_.setCharacterSize(16);
this->text_.setFillColor(core::settings::colors::text);

// Position
this->text_.setPosition({core::settings::screen::CENTER.x,
core::settings::screen::CENTER.y - 30.f});
}
explicit Memo();

/**
* @brief Hide the memo text.
*/
void hide()
{
this->text_.setString("");
}
void hide();

/**
* @brief Set the memo text to a string.
*
* @param str Text to set (e.g., "This is a memo").
*/
void set(const std::string &str)
{
this->text_.setString(str);
this->text_.resetOrigin();
}
void set(const std::string &str);

/**
* @brief Draw the text to the window.
*
* @param window Window to draw to.
*/
void draw(sf::RenderWindow &window) const
{
window.draw(this->text_);
}
void draw(sf::RenderWindow &window) const;

private:
/**
Expand All @@ -84,74 +66,37 @@ class Percentage {
/**
* @brief Construct a new Percentage object.
*/
explicit Percentage()
: correct_answers_(0),
total_answers_(0)
{
// Appearance
this->text_.setCharacterSize(18);
this->text_.setFillColor(core::settings::colors::text);

// Position
constexpr float top_left_offset = 10.f; // Offset from the top-left corner
this->text_.setPosition({core::settings::screen::TOP_LEFT.x + top_left_offset,
core::settings::screen::TOP_LEFT.y + top_left_offset});
this->update_text();
}
explicit Percentage();

/**
* @brief Add a correct answer to the total and update the text.
*/
void add_correct_answer()
{
++this->correct_answers_;
++this->total_answers_;
this->update_text();
}
void add_correct_answer();

/**
* @brief Add an incorrect answer to the total and update the text.
*/
void add_incorrect_answer()
{
++this->total_answers_;
this->update_text();
}
void add_incorrect_answer();

/**
* @brief Reset the correct/total answers to zero and update the text.
*/
void reset()
{
this->correct_answers_ = 0;
this->total_answers_ = 0;
this->update_text();
}
void reset();

/**
* @brief Draw the text to the window.
*
* @param window Window to draw to.
*/
void draw(sf::RenderWindow &window) const
{
window.draw(this->text_);
}
void draw(sf::RenderWindow &window) const;

private:
/**
* @brief Update the text to show the current percentage of correct answers.
*
* If no answers have been given yet, the percentage defaults to 100%.
*/
void update_text()
{
float percent = 100.f; // If no answers yet, default to 100%
if (this->total_answers_ > 0) {
percent = (static_cast<float>(this->correct_answers_) /
static_cast<float>(this->total_answers_)) *
100.f;
}
this->text_.setString(fmt::format("게임 점수: {:.1f}%", percent));
}
void update_text();

/**
* @brief Text object.
Expand Down

0 comments on commit 1318975

Please sign in to comment.