Skip to content

Commit

Permalink
Move memo text to a class.
Browse files Browse the repository at this point in the history
  • Loading branch information
ryouze committed Jan 6, 2025
1 parent 2d2c6a8 commit e25d70e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 40 deletions.
31 changes: 11 additions & 20 deletions src/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand All @@ -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);
}
Expand Down Expand Up @@ -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();
Expand All @@ -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);
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -341,18 +335,15 @@ 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;
}
}
}
else if (current_game_state == game_state_t::show_result) {
// Any mouse click or key press proceeds to the next question
if ((event->is<sf::Event::MouseButtonReleased>()) || event->is<sf::Event::KeyPressed>()) {
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()) {
Expand All @@ -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();
Expand All @@ -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);
}
Expand All @@ -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);
Expand Down
60 changes: 40 additions & 20 deletions src/ui/items.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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_;
};
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit e25d70e

Please sign in to comment.