Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Align selection #1572

Merged
merged 2 commits into from
Nov 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 23 additions & 5 deletions src/editor/overlay_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -939,24 +939,29 @@ EditorOverlayWidget::draw_tile_tip(DrawingContext& context)
return;
}

if (m_editor.get_tiles()->empty())
return;

Vector screen_corner = context.get_cliprect().p2() +
m_editor.get_sector()->get_camera().get_translation();
Vector drawn_tile = m_hovered_tile;
auto tiles = m_editor.get_tiles();

for (drawn_tile.x = static_cast<float>(tiles->m_width) - 1.0f; drawn_tile.x >= 0.0f; drawn_tile.x--) {
for (drawn_tile.y = static_cast<float>(tiles->m_height) - 1.0f; drawn_tile.y >= 0.0f; drawn_tile.y--) {
Vector on_tile = m_hovered_tile + drawn_tile;

if (m_editor.get_tiles()->empty() ||
on_tile.x < 0 ||
if (on_tile.x < 0 ||
on_tile.y < 0 ||
on_tile.x >= static_cast<float>(tilemap->get_width()) ||
on_tile.y >= static_cast<float>(tilemap->get_height())) {
on_tile.y >= static_cast<float>(tilemap->get_height()) ||
on_tile.x >= ceilf(screen_corner.x / 32) ||
on_tile.y >= ceilf(screen_corner.y / 32)) {
continue;
}
uint32_t tile_id = tiles->pos(static_cast<int>(drawn_tile.x), static_cast<int>(drawn_tile.y));
draw_tile(context.color(), *m_editor.get_tileset(), tile_id,
tp_to_sp(on_tile) - m_editor.get_sector()->get_camera().get_translation(),
LAYER_GUI-11, Color(1, 1, 1, 0.5));
align_to_tilemap(on_tile), LAYER_GUI-11, Color(1, 1, 1, 0.5));
/*if (tile_id) {
const Tile* tg_tile = m_editor.get_tileset()->get( tile_id );
tg_tile->draw(context.color(), tp_to_sp(on_tile) - m_editor.get_sector()->camera->get_translation(),
Expand Down Expand Up @@ -1155,4 +1160,17 @@ EditorOverlayWidget::tile_screen_pos(const Vector& tp, int tile_size)
return sp - m_editor.get_sector()->get_camera().get_translation();
}

Vector
EditorOverlayWidget::align_to_tilemap(const Vector& sp, int tile_size)
{
auto tilemap = m_editor.get_selected_tilemap();
if (!tilemap)
{
return Vector(0, 0);
}

Vector sp_ = sp - tilemap->get_offset();
return (sp_ - (sp_ % 1.f)) * static_cast<float>(tile_size);
}

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

// in sector position
Rectf drag_rect();
Expand Down
5 changes: 5 additions & 0 deletions src/math/vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ class Vector final
return Vector(x / s, y / s);
}

Vector operator%(float s) const
{
return Vector(x - floorf(x / s) * s, y - floorf(y / s) * s);
}

Vector operator-() const
{
return Vector(-x, -y);
Expand Down