diff --git a/src/app.cpp b/src/app.cpp index 119450d..5e4b97a 100755 --- a/src/app.cpp +++ b/src/app.cpp @@ -94,10 +94,7 @@ void run() // Create a circle for displaying the current question ui::items::QuestionCircle question_circle; - core::string::Text memo_text; - memo_text.setCharacterSize(16); - memo_text.setFillColor(core::settings::colors::text); - memo_text.setPosition({400.f, 270.f}); + ui::items::Memo memo_text; ui::items::Percentage percentage_display; @@ -163,7 +160,7 @@ void run() circle.set_invalid(); } current_game_state = game_state_t::no_entries_enabled; - memo_text.setString(""); + memo_text.hide(); } else { correct_entry = maybe_entry.value(); @@ -176,7 +173,7 @@ void run() } } question_circle.set_question(is_hangul ? correct_entry.hangul : correct_entry.latin); - memo_text.setString(""); + memo_text.hide(); for (std::size_t i = 0; i < 4; ++i) { answer_circles[i].set_available(is_hangul ? options[i].latin : options[i].hangul); } @@ -220,7 +217,7 @@ void run() for (auto &circle : answer_circles) { circle.set_invalid(); } - memo_text.setString(""); + memo_text.hide(); } else { correct_entry = new_entry.value(); @@ -234,7 +231,7 @@ void run() } } question_circle.set_question(is_hangul ? correct_entry.hangul : correct_entry.latin); - memo_text.setString(""); + memo_text.hide(); for (std::size_t j = 0; j < 4; ++j) { answer_circles[j].set_available(is_hangul ? opts[j].latin : opts[j].hangul); } @@ -296,10 +293,7 @@ void run() } } - memo_text.setString(correct_entry.memo); - const sf::FloatRect memo_bounds = memo_text.getLocalBounds(); - memo_text.setOrigin({memo_bounds.position.x + memo_bounds.size.x / 2.f, - memo_bounds.position.y + memo_bounds.size.y / 2.f}); + memo_text.set(correct_entry.memo); current_game_state = game_state_t::show_result; break; } @@ -341,10 +335,7 @@ void run() } } - memo_text.setString(correct_entry.memo); - const sf::FloatRect memo_bounds = memo_text.getLocalBounds(); - memo_text.setOrigin({memo_bounds.position.x + memo_bounds.size.x / 2.f, - memo_bounds.position.y + memo_bounds.size.y / 2.f}); + memo_text.set(correct_entry.memo); current_game_state = game_state_t::show_result; } } @@ -352,7 +343,7 @@ void run() else if (current_game_state == game_state_t::show_result) { // Any mouse click or key press proceeds to the next question if ((event->is()) || event->is()) { - memo_text.setString(""); + memo_text.hide(); // Re-initialize a new question inline const auto maybe_entry = vocabulary_obj.get_random_enabled_entry(toggle_states); if (!maybe_entry.has_value()) { @@ -361,7 +352,7 @@ void run() for (auto &circle : answer_circles) { circle.set_invalid(); } - memo_text.setString(""); + memo_text.hide(); } else { correct_entry = maybe_entry.value(); @@ -374,7 +365,7 @@ void run() } } question_circle.set_question(is_hangul ? correct_entry.hangul : correct_entry.latin); - memo_text.setString(""); + memo_text.hide(); for (std::size_t i = 0; i < 4; ++i) { answer_circles[i].set_available(is_hangul ? opts[i].latin : opts[i].hangul); } @@ -392,7 +383,7 @@ void run() question_circle.draw(window); if (current_game_state == game_state_t::show_result) { - window.draw(memo_text); + memo_text.draw(window); } for (std::size_t i = 0; i < 4; ++i) { answer_circles[i].draw(window); diff --git a/src/ui/items.hpp b/src/ui/items.hpp index 3cbf92c..e25b81a 100644 --- a/src/ui/items.hpp +++ b/src/ui/items.hpp @@ -27,9 +27,9 @@ class Percentage { this->text_.setFillColor(core::settings::colors::text); const float top_left_offset = 10.f; // Offset from the top-left corner - const sf::Vector2f pos = sf::Vector2f(core::settings::screen::TOP_LEFT.x + top_left_offset, - core::settings::screen::TOP_LEFT.y + top_left_offset); - this->text_.setPosition(pos); + const sf::Vector2f position = sf::Vector2f(core::settings::screen::TOP_LEFT.x + top_left_offset, + core::settings::screen::TOP_LEFT.y + top_left_offset); + this->text_.setPosition(position); this->update_score_display(); } @@ -74,6 +74,37 @@ class Percentage { } }; +class Memo { + public: + explicit Memo() + { + this->text_.setCharacterSize(16); + this->text_.setFillColor(core::settings::colors::text); + const sf::Vector2f position = sf::Vector2f(core::settings::screen::CENTER.x + 0.f, + core::settings::screen::CENTER.y + 30.f); + this->text_.setPosition(position); + } + + void hide() + { + this->text_.setString(""); + } + + void set(const std::string &memo) + { + this->text_.setString(memo); + this->text_.resetOrigin(); + } + + void draw(sf::RenderWindow &window) const + { + window.draw(this->text_); + } + + private: + core::string::Text text_; +}; + class Circle : public sf::CircleShape { public: explicit Circle(const float radius) @@ -104,14 +135,16 @@ class QuestionCircle { { this->text_.setString("X"); this->text_.setCharacterSize(72); - this->reset_text_origin(); + this->text_.resetOrigin(); + ; } void set_question(const std::string &latin_or_hangul) { this->text_.setCharacterSize(48); this->text_.setString(latin_or_hangul); - this->reset_text_origin(); + this->text_.resetOrigin(); + ; } void draw(sf::RenderWindow &window) const @@ -121,13 +154,6 @@ class QuestionCircle { } private: - void reset_text_origin() - { - const sf::FloatRect tb = this->text_.getLocalBounds(); - this->text_.setOrigin({tb.position.x + tb.size.x / 2.f, - tb.position.y + tb.size.y / 2.f}); - } - Circle circle_; core::string::Text text_; }; @@ -182,7 +208,8 @@ class AnswerCircle { this->text_.setString(latin_or_hangul); // TODO: Find out why we have to do this every time we set a new string instead of doing it once in the constructor - this->reset_text_origin(); + this->text_.resetOrigin(); + ; } bool is_hovering(const sf::Vector2f mouse_pos) const @@ -224,13 +251,6 @@ class AnswerCircle { private: Circle circle_; core::string::Text text_; - - void reset_text_origin() - { - const sf::FloatRect tb = this->text_.getLocalBounds(); - this->text_.setOrigin({tb.position.x + tb.size.x / 2.f, - tb.position.y + tb.size.y / 2.f}); - } }; } // namespace ui::items