Screen to Grid is working pretty reliably, even if switching to float coordinates did make zoom at high values a bit wobbly.
This commit is contained in:
parent
e295bfb742
commit
6a4150ec05
12
src/Grid.cpp
12
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);
|
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) {
|
void Grid::screenToGrid(int sx, int sy, int& gx, int& gy) {
|
||||||
float width_sq = box.getSize().x / (grid_size * zoom);
|
float width_sq = box.getSize().x / (grid_size * zoom);
|
||||||
float height_sq = box.getSize().y / (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 bottom_edge = center_y + (height_sq / 2.0);
|
||||||
|
|
||||||
float grid_px = zoom * grid_size;
|
float grid_px = zoom * grid_size;
|
||||||
std::cout << "##############################" <<
|
//std::cout << "##############################" <<
|
||||||
"\nscreen coord: (" << sx << ", " << sy << ")" << std::endl;
|
// "\nscreen coord: (" << sx << ", " << sy << ")" << std::endl;
|
||||||
|
|
||||||
sx -= box.getPosition().x;
|
sx -= box.getPosition().x;
|
||||||
sy -= box.getPosition().y;
|
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_x_sq = sx / grid_px;
|
||||||
float mouse_y_sq = sy / 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;
|
gx = ans_x;
|
||||||
gy = ans_y;
|
gy = ans_y;
|
||||||
|
/*
|
||||||
std::cout <<
|
std::cout <<
|
||||||
"C: (" << center_x << ", " << center_y << ")" << std::endl <<
|
"C: (" << center_x << ", " << center_y << ")" << std::endl <<
|
||||||
"W: " << width_sq << " H: " << height_sq << 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 <<
|
"answer: G(" << ans_x << ", " << ans_y << ")" << std::endl <<
|
||||||
"##############################" <<
|
"##############################" <<
|
||||||
std::endl;
|
std::endl;
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
void Grid::render(sf::RenderWindow & window)
|
void Grid::render(sf::RenderWindow & window)
|
||||||
|
|
|
@ -34,7 +34,7 @@ public:
|
||||||
std::vector<GridPoint> points; // grid visible contents
|
std::vector<GridPoint> points; // grid visible contents
|
||||||
void render(sf::RenderWindow&); // draw to screen
|
void render(sf::RenderWindow&); // draw to screen
|
||||||
GridPoint& at(int, int);
|
GridPoint& at(int, int);
|
||||||
|
bool inBounds(int, int);
|
||||||
void screenToGrid(int, int, int&, int&);
|
void screenToGrid(int, int, int&, int&);
|
||||||
GridPoint* atScreenPixel(int, int, int*, int*);
|
GridPoint* atScreenPixel(int, int, int*, int*);
|
||||||
|
|
||||||
|
|
|
@ -283,10 +283,19 @@ void UITestScene::doAction(std::string name, std::string type)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ACTION("gridtests", "start")) {
|
if (ACTION("gridtests", "start")) {
|
||||||
|
|
||||||
int tx, ty;
|
int tx, ty;
|
||||||
auto mousepos = sf::Mouse::getPosition(game->getWindow());
|
auto mousepos = sf::Mouse::getPosition(game->getWindow());
|
||||||
|
/*
|
||||||
GridPoint* pgrid = grid.atScreenPixel(mousepos.x, mousepos.y, &tx, &ty);
|
GridPoint* pgrid = grid.atScreenPixel(mousepos.x, mousepos.y, &tx, &ty);
|
||||||
std::cout << "\ntx: " << tx << " ty: " << ty << std::endl;
|
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
|
// after processing: set actionState
|
||||||
|
|
Loading…
Reference in New Issue