Skip to content

Commit

Permalink
Merge pull request #20585 from MarianoGnu/tileset_editor
Browse files Browse the repository at this point in the history
New TileSet Editor
  • Loading branch information
reduz authored Aug 10, 2018
2 parents ae67c0b + ce87a30 commit b293dd3
Show file tree
Hide file tree
Showing 12 changed files with 1,302 additions and 862 deletions.
3 changes: 3 additions & 0 deletions editor/icons/icon_add_atlas_tile.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions editor/icons/icon_add_autotile.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions editor/icons/icon_add_single_tile.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 1 addition & 27 deletions editor/plugins/texture_region_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ void TextureRegionEditor::_region_draw() {
base_tex = obj_styleBox->get_texture();
else if (atlas_tex.is_valid())
base_tex = atlas_tex->get_atlas();
else if (tile_set.is_valid() && selected_tile != -1 && tile_set->has_tile(selected_tile))
base_tex = tile_set->tile_get_texture(selected_tile);

if (base_tex.is_null())
return;
Expand Down Expand Up @@ -284,8 +282,6 @@ void TextureRegionEditor::_region_input(const Ref<InputEvent> &p_input) {
r = obj_styleBox->get_region_rect();
else if (atlas_tex.is_valid())
r = atlas_tex->get_region();
else if (tile_set.is_valid() && selected_tile != -1)
r = tile_set->tile_get_region(selected_tile);
rect.expand_to(r.position);
rect.expand_to(r.position + r.size);
}
Expand All @@ -302,9 +298,6 @@ void TextureRegionEditor::_region_input(const Ref<InputEvent> &p_input) {
} else if (atlas_tex.is_valid()) {
undo_redo->add_do_method(atlas_tex.ptr(), "set_region", rect);
undo_redo->add_undo_method(atlas_tex.ptr(), "set_region", atlas_tex->get_region());
} else if (tile_set.is_valid() && selected_tile != -1) {
undo_redo->add_do_method(tile_set.ptr(), "tile_set_region", selected_tile, rect);
undo_redo->add_undo_method(tile_set.ptr(), "tile_set_region", selected_tile, tile_set->tile_get_region(selected_tile));
}
undo_redo->add_do_method(edit_draw, "update");
undo_redo->add_undo_method(edit_draw, "update");
Expand All @@ -327,8 +320,6 @@ void TextureRegionEditor::_region_input(const Ref<InputEvent> &p_input) {
rect_prev = obj_styleBox->get_region_rect();
else if (atlas_tex.is_valid())
rect_prev = atlas_tex->get_region();
else if (tile_set.is_valid() && selected_tile != -1)
rect_prev = tile_set->tile_get_region(selected_tile);

for (int i = 0; i < 8; i++) {
Vector2 tuv = endpoints[i];
Expand Down Expand Up @@ -372,9 +363,6 @@ void TextureRegionEditor::_region_input(const Ref<InputEvent> &p_input) {
} else if (obj_styleBox.is_valid()) {
undo_redo->add_do_method(obj_styleBox.ptr(), "set_region_rect", obj_styleBox->get_region_rect());
undo_redo->add_undo_method(obj_styleBox.ptr(), "set_region_rect", rect_prev);
} else if (tile_set.is_valid()) {
undo_redo->add_do_method(tile_set.ptr(), "tile_set_region", selected_tile, tile_set->tile_get_region(selected_tile));
undo_redo->add_undo_method(tile_set.ptr(), "tile_set_region", selected_tile, rect_prev);
}
drag_index = -1;
}
Expand Down Expand Up @@ -595,8 +583,6 @@ void TextureRegionEditor::apply_rect(const Rect2 &rect) {
obj_styleBox->set_region_rect(rect);
else if (atlas_tex.is_valid())
atlas_tex->set_region(rect);
else if (tile_set.is_valid() && selected_tile != -1)
tile_set->tile_set_region(selected_tile, rect);
}

void TextureRegionEditor::_notification(int p_what) {
Expand All @@ -623,12 +609,11 @@ void TextureRegionEditor::_notification(int p_what) {
}

void TextureRegionEditor::_node_removed(Object *p_obj) {
if (p_obj == node_sprite || p_obj == node_ninepatch || p_obj == obj_styleBox.ptr() || p_obj == atlas_tex.ptr() || p_obj == tile_set.ptr()) {
if (p_obj == node_sprite || p_obj == node_ninepatch || p_obj == obj_styleBox.ptr() || p_obj == atlas_tex.ptr()) {
node_ninepatch = NULL;
node_sprite = NULL;
obj_styleBox = Ref<StyleBox>(NULL);
atlas_tex = Ref<AtlasTexture>(NULL);
tile_set = Ref<TileSet>(NULL);
hide();
}
}
Expand Down Expand Up @@ -677,25 +662,20 @@ void TextureRegionEditor::edit(Object *p_obj) {
obj_styleBox->remove_change_receptor(this);
if (atlas_tex.is_valid())
atlas_tex->remove_change_receptor(this);
if (tile_set.is_valid())
tile_set->remove_change_receptor(this);
if (p_obj) {
node_sprite = Object::cast_to<Sprite>(p_obj);
node_ninepatch = Object::cast_to<NinePatchRect>(p_obj);
if (Object::cast_to<StyleBoxTexture>(p_obj))
obj_styleBox = Ref<StyleBoxTexture>(Object::cast_to<StyleBoxTexture>(p_obj));
if (Object::cast_to<AtlasTexture>(p_obj))
atlas_tex = Ref<AtlasTexture>(Object::cast_to<AtlasTexture>(p_obj));
if (Object::cast_to<TileSet>(p_obj))
tile_set = Ref<TileSet>(Object::cast_to<TileSet>(p_obj));
p_obj->add_change_receptor(this);
_edit_region();
} else {
node_sprite = NULL;
node_ninepatch = NULL;
obj_styleBox = Ref<StyleBoxTexture>(NULL);
atlas_tex = Ref<AtlasTexture>(NULL);
tile_set = Ref<TileSet>(NULL);
}
edit_draw->update();
if (node_sprite && !node_sprite->is_region()) {
Expand Down Expand Up @@ -724,8 +704,6 @@ void TextureRegionEditor::_edit_region() {
texture = obj_styleBox->get_texture();
else if (atlas_tex.is_valid())
texture = atlas_tex->get_atlas();
else if (tile_set.is_valid() && selected_tile != -1 && tile_set->has_tile(selected_tile))
texture = tile_set->tile_get_texture(selected_tile);

if (texture.is_null()) {
edit_draw->update();
Expand Down Expand Up @@ -794,8 +772,6 @@ void TextureRegionEditor::_edit_region() {
rect = obj_styleBox->get_region_rect();
else if (atlas_tex.is_valid())
rect = atlas_tex->get_region();
else if (tile_set.is_valid() && selected_tile != -1)
rect = tile_set->tile_get_region(selected_tile);

edit_draw->update();
}
Expand All @@ -814,10 +790,8 @@ TextureRegionEditor::TextureRegionEditor(EditorNode *p_editor) {
node_ninepatch = NULL;
obj_styleBox = Ref<StyleBoxTexture>(NULL);
atlas_tex = Ref<AtlasTexture>(NULL);
tile_set = Ref<TileSet>(NULL);
editor = p_editor;
undo_redo = editor->get_undo_redo();
selected_tile = -1;

snap_step = Vector2(10, 10);
snap_separation = Vector2(0, 0);
Expand Down
5 changes: 0 additions & 5 deletions editor/plugins/texture_region_editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
#include "scene/gui/nine_patch_rect.h"
#include "scene/resources/style_box.h"
#include "scene/resources/texture.h"
#include "scene/resources/tile_set.h"

/**
@author Mariano Suligoy
Expand All @@ -56,8 +55,6 @@ class TextureRegionEditor : public Control {
};

friend class TextureRegionEditorPlugin;
friend class TileSetEditor;
friend class TileSetEditorPlugin;
MenuButton *snap_mode_button;
TextureRect *icon_zoom;
ToolButton *zoom_in;
Expand Down Expand Up @@ -91,14 +88,12 @@ class TextureRegionEditor : public Control {
Sprite *node_sprite;
Ref<StyleBoxTexture> obj_styleBox;
Ref<AtlasTexture> atlas_tex;
Ref<TileSet> tile_set;

Rect2 rect;
Rect2 rect_prev;
float prev_margin;
int edited_margin;
List<Rect2> autoslice_cache;
int selected_tile;

bool drag;
bool creating;
Expand Down
20 changes: 11 additions & 9 deletions editor/plugins/tile_map_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,11 @@ void TileMapEditor::_menu_option(int p_option) {
}

void TileMapEditor::_palette_selected(int index) {
_update_palette();
}

if (manual_autotile) {
_update_palette();
}
void TileMapEditor::_palette_multi_selected(int index, bool selected) {
_update_palette();
}

void TileMapEditor::_canvas_mouse_enter() {
Expand Down Expand Up @@ -296,7 +297,7 @@ void TileMapEditor::_set_cell(const Point2i &p_pos, Vector<int> p_values, bool p
}

node->set_cell(p_pos.x, p_pos.y, p_value, p_flip_h, p_flip_v, p_transpose);
if (manual_autotile) {
if (manual_autotile || node->get_tileset()->tile_get_tile_mode(p_value) == TileSet::ATLAS_TILE) {
if (current != -1) {
node->set_cell_autotile_coord(p_pos.x, p_pos.y, position);
}
Expand All @@ -317,7 +318,6 @@ void TileMapEditor::_text_entered(const String &p_text) {
}

void TileMapEditor::_text_changed(const String &p_text) {

_update_palette();
}

Expand Down Expand Up @@ -427,7 +427,7 @@ void TileMapEditor::_update_palette() {
if (tex.is_valid()) {
Rect2 region = tileset->tile_get_region(entries[i].id);

if (tileset->tile_get_tile_mode(entries[i].id) == TileSet::AUTO_TILE) {
if (tileset->tile_get_tile_mode(entries[i].id) == TileSet::AUTO_TILE || tileset->tile_get_tile_mode(entries[i].id) == TileSet::ATLAS_TILE) {
int spacing = tileset->autotile_get_spacing(entries[i].id);
region.size = tileset->autotile_get_size(entries[i].id);
region.position += (region.size + Vector2(spacing, spacing)) * tileset->autotile_get_icon_coordinate(entries[i].id);
Expand All @@ -450,7 +450,7 @@ void TileMapEditor::_update_palette() {
palette->select(0);
}

if (manual_autotile && tileset->tile_get_tile_mode(sel_tile) == TileSet::AUTO_TILE) {
if ((manual_autotile && tileset->tile_get_tile_mode(sel_tile) == TileSet::AUTO_TILE) || tileset->tile_get_tile_mode(sel_tile) == TileSet::ATLAS_TILE) {

const Map<Vector2, uint16_t> &tiles = tileset->autotile_get_bitmask_map(sel_tile);

Expand Down Expand Up @@ -676,10 +676,10 @@ void TileMapEditor::_draw_cell(int p_cell, const Point2i &p_point, bool p_flip_h
Vector2 tile_ofs = node->get_tileset()->tile_get_texture_offset(p_cell);

Rect2 r = node->get_tileset()->tile_get_region(p_cell);
if (node->get_tileset()->tile_get_tile_mode(p_cell) == TileSet::AUTO_TILE) {
if (node->get_tileset()->tile_get_tile_mode(p_cell) == TileSet::AUTO_TILE || node->get_tileset()->tile_get_tile_mode(p_cell) == TileSet::ATLAS_TILE) {
Vector2 offset;
int selected = manual_palette->get_current();
if (manual_autotile && selected != -1) {
if ((manual_autotile || node->get_tileset()->tile_get_tile_mode(p_cell) == TileSet::ATLAS_TILE) && selected != -1) {
offset = manual_palette->get_item_metadata(selected);
} else {
offset = node->get_tileset()->autotile_get_icon_coordinate(p_cell);
Expand Down Expand Up @@ -1673,6 +1673,7 @@ void TileMapEditor::_bind_methods() {
ClassDB::bind_method(D_METHOD("_tileset_settings_changed"), &TileMapEditor::_tileset_settings_changed);
ClassDB::bind_method(D_METHOD("_update_transform_buttons"), &TileMapEditor::_update_transform_buttons);
ClassDB::bind_method(D_METHOD("_palette_selected"), &TileMapEditor::_palette_selected);
ClassDB::bind_method(D_METHOD("_palette_multi_selected"), &TileMapEditor::_palette_multi_selected);

ClassDB::bind_method(D_METHOD("_fill_points"), &TileMapEditor::_fill_points);
ClassDB::bind_method(D_METHOD("_erase_points"), &TileMapEditor::_erase_points);
Expand Down Expand Up @@ -1800,6 +1801,7 @@ TileMapEditor::TileMapEditor(EditorNode *p_editor) {
palette->set_max_text_lines(2);
palette->set_select_mode(ItemList::SELECT_MULTI);
palette->connect("item_selected", this, "_palette_selected");
palette->connect("multi_selected", this, "_palette_multi_selected");
palette_container->add_child(palette);

// Add autotile override palette
Expand Down
1 change: 1 addition & 0 deletions editor/plugins/tile_map_editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ class TileMapEditor : public VBoxContainer {
void _update_palette();
void _menu_option(int p_option);
void _palette_selected(int index);
void _palette_multi_selected(int index, bool selected);

void _start_undo(const String &p_action);
void _finish_undo();
Expand Down
Loading

0 comments on commit b293dd3

Please sign in to comment.