Skip to content

Commit

Permalink
Merge pull request godotengine#60892 from KoBeWi/fat_selection
Browse files Browse the repository at this point in the history
Improve tile editor selection appearance
  • Loading branch information
akien-mga committed Dec 9, 2022
2 parents 9ee47b3 + 871278c commit 43bef98
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 35 deletions.
57 changes: 57 additions & 0 deletions editor/icons/TileSelection.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 6 additions & 10 deletions editor/plugins/tiles/tile_map_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1698,19 +1698,16 @@ void TileMapEditorTilesPlugin::_tile_atlas_control_draw() {
if (frame > 0) {
color.a *= 0.3;
}
tile_atlas_control->draw_rect(atlas->get_tile_texture_region(E.get_atlas_coords(), frame), color, false);
TilesEditorPlugin::draw_selection_rect(tile_atlas_control, atlas->get_tile_texture_region(E.get_atlas_coords(), frame), color);
}
}
}

// Draw the hovered tile.
if (hovered_tile.get_atlas_coords() != TileSetSource::INVALID_ATLAS_COORDS && hovered_tile.alternative_tile == 0 && !tile_set_dragging_selection) {
for (int frame = 0; frame < atlas->get_tile_animation_frames_count(hovered_tile.get_atlas_coords()); frame++) {
Color color = Color(1.0, 1.0, 1.0);
if (frame > 0) {
color.a *= 0.3;
}
tile_atlas_control->draw_rect(atlas->get_tile_texture_region(hovered_tile.get_atlas_coords(), frame), color, false);
Color color = Color(1.0, 0.8, 0.0, frame == 0 ? 0.6 : 0.3);
TilesEditorPlugin::draw_selection_rect(tile_atlas_control, atlas->get_tile_texture_region(hovered_tile.get_atlas_coords(), frame), color);
}
}

Expand All @@ -1731,9 +1728,8 @@ void TileMapEditorTilesPlugin::_tile_atlas_control_draw() {
}
}
}
Color selection_rect_color = selection_color.lightened(0.2);
for (const Vector2i &E : to_draw) {
tile_atlas_control->draw_rect(atlas->get_tile_texture_region(E), selection_rect_color, false);
TilesEditorPlugin::draw_selection_rect(tile_atlas_control, atlas->get_tile_texture_region(E));
}
}
}
Expand Down Expand Up @@ -1882,7 +1878,7 @@ void TileMapEditorTilesPlugin::_tile_alternatives_control_draw() {
if (E.source_id == source_id && E.get_atlas_coords() != TileSetSource::INVALID_ATLAS_COORDS && E.alternative_tile > 0) {
Rect2i rect = tile_atlas_view->get_alternative_tile_rect(E.get_atlas_coords(), E.alternative_tile);
if (rect != Rect2i()) {
alternative_tiles_control->draw_rect(rect, Color(0.2, 0.2, 1.0), false);
TilesEditorPlugin::draw_selection_rect(alternative_tiles_control, rect);
}
}
}
Expand All @@ -1891,7 +1887,7 @@ void TileMapEditorTilesPlugin::_tile_alternatives_control_draw() {
if (hovered_tile.get_atlas_coords() != TileSetSource::INVALID_ATLAS_COORDS && hovered_tile.alternative_tile > 0) {
Rect2i rect = tile_atlas_view->get_alternative_tile_rect(hovered_tile.get_atlas_coords(), hovered_tile.alternative_tile);
if (rect != Rect2i()) {
alternative_tiles_control->draw_rect(rect, Color(1.0, 1.0, 1.0), false);
TilesEditorPlugin::draw_selection_rect(alternative_tiles_control, rect, Color(1.0, 0.8, 0.0, 0.5));
}
}
}
Expand Down
37 changes: 12 additions & 25 deletions editor/plugins/tiles/tile_set_atlas_source_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1684,23 +1684,16 @@ Array TileSetAtlasSourceEditor::_get_selection_as_array() {
}

void TileSetAtlasSourceEditor::_tile_atlas_control_draw() {
// Colors.
Color grid_color = EDITOR_GET("editors/tiles_editor/grid_color");
Color selection_color = Color().from_hsv(Math::fposmod(grid_color.get_h() + 0.5, 1.0), grid_color.get_s(), grid_color.get_v(), 1.0);

// Draw the selected tile.
if (tools_button_group->get_pressed_button() == tool_select_button) {
for (const TileSelection &E : selection) {
TileSelection selected = E;
if (selected.alternative == 0) {
// Draw the rect.
for (int frame = 0; frame < tile_set_atlas_source->get_tile_animation_frames_count(selected.tile); frame++) {
Color color = selection_color;
if (frame > 0) {
color.a *= 0.3;
}
Color color = Color(0.0, 1.0, 0.0, frame == 0 ? 1.0 : 0.3);
Rect2 region = tile_set_atlas_source->get_tile_texture_region(selected.tile, frame);
tile_atlas_control->draw_rect(region, color, false);
TilesEditorPlugin::draw_selection_rect(tile_atlas_control, region, color);
}
}
}
Expand Down Expand Up @@ -1742,7 +1735,7 @@ void TileSetAtlasSourceEditor::_tile_atlas_control_draw() {
// Draw the tiles to be removed.
for (const Vector2i &E : drag_modified_tiles) {
for (int frame = 0; frame < tile_set_atlas_source->get_tile_animation_frames_count(E); frame++) {
tile_atlas_control->draw_rect(tile_set_atlas_source->get_tile_texture_region(E, frame), Color(0.0, 0.0, 0.0), false);
TilesEditorPlugin::draw_selection_rect(tile_atlas_control, tile_set_atlas_source->get_tile_texture_region(E, frame), Color(0.0, 0.0, 0.0));
}
}
} else if (drag_type == DRAG_TYPE_RECT_SELECT || drag_type == DRAG_TYPE_REMOVE_TILES_USING_RECT) {
Expand All @@ -1754,7 +1747,7 @@ void TileSetAtlasSourceEditor::_tile_atlas_control_draw() {

Color color = Color(0.0, 0.0, 0.0);
if (drag_type == DRAG_TYPE_RECT_SELECT) {
color = selection_color.lightened(0.2);
color = Color(1.0, 1.0, 0.0);
}

RBSet<Vector2i> to_paint;
Expand All @@ -1769,7 +1762,7 @@ void TileSetAtlasSourceEditor::_tile_atlas_control_draw() {

for (const Vector2i &E : to_paint) {
Vector2i coords = E;
tile_atlas_control->draw_rect(tile_set_atlas_source->get_tile_texture_region(coords), color, false);
TilesEditorPlugin::draw_selection_rect(tile_atlas_control, tile_set_atlas_source->get_tile_texture_region(coords), color);
}
} else if (drag_type == DRAG_TYPE_CREATE_TILES_USING_RECT) {
// Draw tiles to be created.
Expand All @@ -1786,7 +1779,7 @@ void TileSetAtlasSourceEditor::_tile_atlas_control_draw() {
Vector2i coords = Vector2i(x, y);
if (tile_set_atlas_source->get_tile_at_coords(coords) == TileSetSource::INVALID_ATLAS_COORDS) {
Vector2i origin = margins + (coords * (tile_size + separation));
tile_atlas_control->draw_rect(Rect2i(origin, tile_size), Color(1.0, 1.0, 1.0), false);
TilesEditorPlugin::draw_selection_rect(tile_atlas_control, Rect2i(origin, tile_size));
}
}
}
Expand All @@ -1803,19 +1796,16 @@ void TileSetAtlasSourceEditor::_tile_atlas_control_draw() {
Vector2i separation = tile_set_atlas_source->get_separation();
Vector2i tile_size = tile_set_atlas_source->get_texture_region_size();
Vector2i origin = margins + (area.position * (tile_size + separation));
tile_atlas_control->draw_rect(Rect2i(origin, area.size * tile_size), Color(1.0, 1.0, 1.0), false);
TilesEditorPlugin::draw_selection_rect(tile_atlas_control, Rect2i(origin, area.size * tile_size));
} else {
Vector2i grid_size = tile_set_atlas_source->get_atlas_grid_size();
if (hovered_base_tile_coords.x >= 0 && hovered_base_tile_coords.y >= 0 && hovered_base_tile_coords.x < grid_size.x && hovered_base_tile_coords.y < grid_size.y) {
Vector2i hovered_tile = tile_set_atlas_source->get_tile_at_coords(hovered_base_tile_coords);
if (hovered_tile != TileSetSource::INVALID_ATLAS_COORDS) {
// Draw existing hovered tile.
for (int frame = 0; frame < tile_set_atlas_source->get_tile_animation_frames_count(hovered_tile); frame++) {
Color color = Color(1.0, 1.0, 1.0);
if (frame > 0) {
color.a *= 0.3;
}
tile_atlas_control->draw_rect(tile_set_atlas_source->get_tile_texture_region(hovered_tile, frame), color, false);
Color color = Color(1.0, 0.8, 0.0, frame == 0 ? 0.6 : 0.3);
TilesEditorPlugin::draw_selection_rect(tile_atlas_control, tile_set_atlas_source->get_tile_texture_region(hovered_tile, frame), color);
}
} else {
// Draw empty tile, only in add/remove tiles mode.
Expand All @@ -1824,7 +1814,7 @@ void TileSetAtlasSourceEditor::_tile_atlas_control_draw() {
Vector2i separation = tile_set_atlas_source->get_separation();
Vector2i tile_size = tile_set_atlas_source->get_texture_region_size();
Vector2i origin = margins + (hovered_base_tile_coords * (tile_size + separation));
tile_atlas_control->draw_rect(Rect2i(origin, tile_size), Color(1.0, 1.0, 1.0), false);
TilesEditorPlugin::draw_selection_rect(tile_atlas_control, Rect2i(origin, tile_size));
}
}
}
Expand Down Expand Up @@ -1976,17 +1966,14 @@ void TileSetAtlasSourceEditor::_tile_alternatives_control_mouse_exited() {
}

void TileSetAtlasSourceEditor::_tile_alternatives_control_draw() {
Color grid_color = EDITOR_GET("editors/tiles_editor/grid_color");
Color selection_color = Color().from_hsv(Math::fposmod(grid_color.get_h() + 0.5, 1.0), grid_color.get_s(), grid_color.get_v(), 1.0);

// Update the hovered alternative tile.
if (tools_button_group->get_pressed_button() == tool_select_button) {
// Draw hovered tile.
Vector2i coords = Vector2(hovered_alternative_tile_coords.x, hovered_alternative_tile_coords.y);
if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
Rect2i rect = tile_atlas_view->get_alternative_tile_rect(coords, hovered_alternative_tile_coords.z);
if (rect != Rect2i()) {
alternative_tiles_control->draw_rect(rect, Color(1.0, 1.0, 1.0), false);
TilesEditorPlugin::draw_selection_rect(alternative_tiles_control, rect, Color(1.0, 0.8, 0.0, 0.5));
}
}

Expand All @@ -1996,7 +1983,7 @@ void TileSetAtlasSourceEditor::_tile_alternatives_control_draw() {
if (selected.alternative >= 1) {
Rect2i rect = tile_atlas_view->get_alternative_tile_rect(selected.tile, selected.alternative);
if (rect != Rect2i()) {
alternative_tiles_control->draw_rect(rect, selection_color, false);
TilesEditorPlugin::draw_selection_rect(alternative_tiles_control, rect);
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions editor/plugins/tiles/tiles_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,15 @@ bool TilesEditorPlugin::handles(Object *p_object) const {
return p_object->is_class("TileMap") || p_object->is_class("TileSet");
}

void TilesEditorPlugin::draw_selection_rect(CanvasItem *p_ci, const Rect2 &p_rect, const Color &p_color) {
real_t scale = p_ci->get_global_transform().get_scale().x * 0.5;
p_ci->draw_set_transform(p_rect.position, 0, Vector2(1, 1) / scale);
RS::get_singleton()->canvas_item_add_nine_patch(
p_ci->get_canvas_item(), Rect2(Vector2(), p_rect.size * scale), Rect2(), EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("TileSelection"), SNAME("EditorIcons"))->get_rid(),
Vector2(2, 2), Vector2(2, 2), RS::NINE_PATCH_STRETCH, RS::NINE_PATCH_STRETCH, false, p_color);
p_ci->draw_set_transform_matrix(Transform2D());
}

TilesEditorPlugin::TilesEditorPlugin() {
set_process_internal(true);

Expand Down
2 changes: 2 additions & 0 deletions editor/plugins/tiles/tiles_editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ class TilesEditorPlugin : public EditorPlugin {
virtual bool handles(Object *p_object) const override;
virtual void make_visible(bool p_visible) override;

static void draw_selection_rect(CanvasItem *p_ci, const Rect2 &p_rect, const Color &p_color = Color(1.0, 1.0, 1.0));

TilesEditorPlugin();
~TilesEditorPlugin();
};
Expand Down

0 comments on commit 43bef98

Please sign in to comment.