Skip to content

Commit

Permalink
Fix mouse position.
Browse files Browse the repository at this point in the history
  • Loading branch information
ArthurSonzogni committed Jul 15, 2023
1 parent 1b42411 commit 0393e64
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
11 changes: 6 additions & 5 deletions src/board.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ bool Board::OnEvent(ftxui::Event event) {
return false;
}

mouse_x_ = (event.mouse().x - 1) * 2;
mouse_y_ = (event.mouse().y - 1) * 4;
mouse_x_ = event.mouse().x - box_.x_min;
mouse_y_ = event.mouse().y - box_.y_min;

// Ignore the first few events, to avoid miss click.
if (step_ < 30)
Expand Down Expand Up @@ -211,7 +211,7 @@ void Board::MoveUp() {
}

ftxui::Element Board::Draw(ftxui::Element back_btn,
ftxui::Element quit_btn) const {
ftxui::Element quit_btn) {
using namespace ftxui;
auto c = Canvas(g_board_width, g_board_height);

Expand All @@ -228,7 +228,7 @@ ftxui::Element Board::Draw(ftxui::Element back_btn,
"/" + std::to_string(config_.balls);

auto frame = hbox({
canvas(std::move(c)),
canvas(std::move(c)) | reflect(box_),
separator(),
vbox({
window(text("bricks:"), text(std::to_string(bricks_.size()))),
Expand All @@ -252,6 +252,7 @@ void Board::DrawShootingLine(ftxui::Canvas& c) const {

b2Vec2 position = ShootPosition();
b2Vec2 speed = ShootSpeed();

float timeStep = 1.0f / 60.0f; // NOLINT

const float ball_friction = 0.5F;
Expand Down Expand Up @@ -295,7 +296,7 @@ b2Vec2 Board::ShootSpeed() const {
const b2Vec2 position = ShootPosition();
const auto mouse_x = static_cast<float>(mouse_x_);
const auto mouse_y = static_cast<float>(mouse_y_);
const b2Vec2 target(mouse_x, mouse_y);
const b2Vec2 target(mouse_x*2, mouse_y*4);
b2Vec2 speed = target - position;
speed.Normalize();
const float speed_norm = 100.F;
Expand Down
4 changes: 3 additions & 1 deletion src/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Board {

bool OnEvent(ftxui::Event event);
void Step();
ftxui::Element Draw(ftxui::Element back_btn, ftxui::Element quit_btn) const;
ftxui::Element Draw(ftxui::Element back_btn, ftxui::Element quit_btn);

private:
void InitializeBricks();
Expand Down Expand Up @@ -62,6 +62,8 @@ class Board {
bool is_shooting_ = false;
int remaining_balls_to_shoot_ = 0;
b2Vec2 shooting_direction_;

ftxui::Box box_;
};

} // namespace term_breaker
Expand Down

0 comments on commit 0393e64

Please sign in to comment.