From 6a4150ec05840e8099e6b4cdae4f2c768a6a9325 Mon Sep 17 00:00:00 2001 From: John McCardle Date: Fri, 3 Mar 2023 22:26:38 -0500 Subject: [PATCH] Screen to Grid is working pretty reliably, even if switching to float coordinates did make zoom at high values a bit wobbly. --- src/Grid.cpp | 12 +++++++++--- src/Grid.h | 2 +- src/UITestScene.cpp | 9 +++++++++ 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/Grid.cpp b/src/Grid.cpp index bf64431..5438835 100644 --- a/src/Grid.cpp +++ b/src/Grid.cpp @@ -44,6 +44,10 @@ Grid::Grid(int gx, int gy, int gs, int _x, int _y, int _w, int _h): sprite.setTexture(texture); } +bool Grid::inBounds(int x, int y) { + return (x >= 0 && y >= 0 && x < grid_x && y < grid_y); +} + void Grid::screenToGrid(int sx, int sy, int& gx, int& gy) { float width_sq = box.getSize().x / (grid_size * zoom); float height_sq = box.getSize().y / (grid_size * zoom); @@ -53,13 +57,13 @@ void Grid::screenToGrid(int sx, int sy, int& gx, int& gy) { float bottom_edge = center_y + (height_sq / 2.0); float grid_px = zoom * grid_size; - std::cout << "##############################" << - "\nscreen coord: (" << sx << ", " << sy << ")" << std::endl; + //std::cout << "##############################" << + // "\nscreen coord: (" << sx << ", " << sy << ")" << std::endl; sx -= box.getPosition().x; sy -= box.getPosition().y; - std::cout << "box coord: (" << sx << ", " << sy << ")" << std::endl; + //std::cout << "box coord: (" << sx << ", " << sy << ")" << std::endl; float mouse_x_sq = sx / grid_px; float mouse_y_sq = sy / grid_px; @@ -68,6 +72,7 @@ void Grid::screenToGrid(int sx, int sy, int& gx, int& gy) { gx = ans_x; gy = ans_y; + /* std::cout << "C: (" << center_x << ", " << center_y << ")" << std::endl << "W: " << width_sq << " H: " << height_sq << std::endl << @@ -77,6 +82,7 @@ void Grid::screenToGrid(int sx, int sy, int& gx, int& gy) { "answer: G(" << ans_x << ", " << ans_y << ")" << std::endl << "##############################" << std::endl; + */ } void Grid::render(sf::RenderWindow & window) diff --git a/src/Grid.h b/src/Grid.h index 1fc8be2..f7b0899 100644 --- a/src/Grid.h +++ b/src/Grid.h @@ -34,7 +34,7 @@ public: std::vector points; // grid visible contents void render(sf::RenderWindow&); // draw to screen GridPoint& at(int, int); - + bool inBounds(int, int); void screenToGrid(int, int, int&, int&); GridPoint* atScreenPixel(int, int, int*, int*); diff --git a/src/UITestScene.cpp b/src/UITestScene.cpp index c975da7..756b326 100644 --- a/src/UITestScene.cpp +++ b/src/UITestScene.cpp @@ -283,10 +283,19 @@ void UITestScene::doAction(std::string name, std::string type) } if (ACTION("gridtests", "start")) { + int tx, ty; auto mousepos = sf::Mouse::getPosition(game->getWindow()); + /* GridPoint* pgrid = grid.atScreenPixel(mousepos.x, mousepos.y, &tx, &ty); std::cout << "\ntx: " << tx << " ty: " << ty << std::endl; + */ + grid.screenToGrid(mousepos.x, mousepos.y, tx, ty); + if (grid.inBounds(tx, ty)) { + auto gridsq = grid.at(tx, ty); + std::cout << "At (" << tx << ", " << ty << "): " << gridsq.tilesprite << std::endl; + } + } // after processing: set actionState