Skip to content

Commit

Permalink
Move tilemap position checks into its own function
Browse files Browse the repository at this point in the history
  • Loading branch information
tobbi committed Jul 15, 2023
1 parent ab13865 commit 9cfad1a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 28 deletions.
40 changes: 12 additions & 28 deletions src/editor/overlay_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,7 @@ void
EditorOverlayWidget::input_tile(const Vector& pos, uint32_t tile)
{
auto tilemap = m_editor.get_selected_tilemap();
if (!tilemap) {
return;
}

if ( pos.x < 0 ||
pos.y < 0 ||
pos.x >= static_cast<float>(tilemap->get_width()) ||
pos.y >= static_cast<float>(tilemap->get_height())) {
if (!tilemap || !is_position_inside_tilemap(tilemap, pos)) {
return;
}

Expand All @@ -172,14 +165,7 @@ void
EditorOverlayWidget::autotile(const Vector& pos, uint32_t tile)
{
auto tilemap = m_editor.get_selected_tilemap();
if (!tilemap) {
return;
}

if ( pos.x < 0 ||
pos.y < 0 ||
pos.x >= static_cast<float>(tilemap->get_width()) ||
pos.y >= static_cast<float>(tilemap->get_height())) {
if (!tilemap || !is_position_inside_tilemap(tilemap, pos)) {
return;
}

Expand Down Expand Up @@ -211,14 +197,7 @@ EditorOverlayWidget::autotile_corner(const Vector& pos, uint32_t tile,
TileMap::AutotileCornerOperation op)
{
auto tilemap = m_editor.get_selected_tilemap();
if (!tilemap) {
return;
}

if ( pos.x < 0 ||
pos.y < 0 ||
pos.x >= static_cast<float>(tilemap->get_width()) ||
pos.y >= static_cast<float>(tilemap->get_height())) {
if (!tilemap || !is_position_inside_tilemap(tilemap, pos)) {
return;
}

Expand Down Expand Up @@ -461,10 +440,7 @@ EditorOverlayWidget::fill()
Vector tpos = pos - m_hovered_tile;

// Tests for being inside tilemap:
if ( pos.x < 0 ||
pos.y < 0 ||
pos.x >= static_cast<float>(tilemap->get_width()) ||
pos.y >= static_cast<float>(tilemap->get_height()))
if (!is_position_inside_tilemap(tilemap, pos))
{
pos_stack.pop_back();
continue;
Expand Down Expand Up @@ -1522,4 +1498,12 @@ EditorOverlayWidget::align_to_tilemap(const Vector& sp, int tile_size) const
return glm::trunc(sp_) * static_cast<float>(tile_size);
}

bool
EditorOverlayWidget::is_position_inside_tilemap(const TileMap* tilemap, const Vector& pos) const
{
return pos.x > 0 && pos.y > 0 &&
pos.x <= static_cast<float>(tilemap->get_width()) &&
pos.y <= static_cast<float>(tilemap->get_height());
}

/* EOF */
1 change: 1 addition & 0 deletions src/editor/overlay_widget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ class EditorOverlayWidget final : public Widget
Vector sp_to_tp(const Vector& sp, int tile_size = 32) const;
Vector tile_screen_pos(const Vector& tp, int tile_size = 32) const;
Vector align_to_tilemap(const Vector& sp, int tile_size = 32) const;
bool is_position_inside_tilemap(const TileMap* tilemap, const Vector& pos) const;

// in sector position
Rectf drag_rect() const;
Expand Down

0 comments on commit 9cfad1a

Please sign in to comment.