2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* tile_set_editor_plugin.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2017-01-01 22:01:57 +01:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2017-04-08 00:11:42 +02:00
|
|
|
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
|
2014-02-10 02:10:30 +01:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
|
|
|
#include "tile_set_editor_plugin.h"
|
2017-01-16 08:04:19 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#include "scene/2d/physics_body_2d.h"
|
2017-03-05 16:44:50 +01:00
|
|
|
#include "scene/2d/sprite.h"
|
2017-01-16 08:04:19 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void TileSetEditor::edit(const Ref<TileSet> &p_tileset) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
tileset = p_tileset;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-05-19 23:49:37 +02:00
|
|
|
void TileSetEditor::_import_node(Node *p_node, Ref<TileSet> p_library) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-05-19 23:49:37 +02:00
|
|
|
for (int i = 0; i < p_node->get_child_count(); i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-05-19 23:49:37 +02:00
|
|
|
Node *child = p_node->get_child(i);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2015-03-09 06:34:56 +01:00
|
|
|
if (!child->cast_to<Sprite>()) {
|
2017-03-05 16:44:50 +01:00
|
|
|
if (child->get_child_count() > 0) {
|
2017-05-19 23:49:37 +02:00
|
|
|
_import_node(child, p_library);
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-05-19 23:49:37 +02:00
|
|
|
continue;
|
2015-03-09 06:34:56 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2015-03-09 06:34:56 +01:00
|
|
|
Sprite *mi = child->cast_to<Sprite>();
|
2017-03-05 16:44:50 +01:00
|
|
|
Ref<Texture> texture = mi->get_texture();
|
2017-04-07 04:36:37 +02:00
|
|
|
Ref<ShaderMaterial> material = mi->get_material();
|
2015-03-09 06:34:56 +01:00
|
|
|
|
|
|
|
if (texture.is_null())
|
2014-02-10 02:10:30 +01:00
|
|
|
continue;
|
|
|
|
|
2015-03-09 06:34:56 +01:00
|
|
|
int id = p_library->find_tile_by_name(mi->get_name());
|
2017-03-05 16:44:50 +01:00
|
|
|
if (id < 0) {
|
2015-03-09 06:34:56 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
id = p_library->get_last_unused_tile_id();
|
2015-03-09 06:34:56 +01:00
|
|
|
p_library->create_tile(id);
|
2017-03-05 16:44:50 +01:00
|
|
|
p_library->tile_set_name(id, mi->get_name());
|
2015-03-09 06:34:56 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
p_library->tile_set_texture(id, texture);
|
|
|
|
p_library->tile_set_material(id, material);
|
2015-03-09 06:34:56 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
p_library->tile_set_modulate(id, mi->get_modulate());
|
2016-11-30 14:33:39 +01:00
|
|
|
|
2015-03-09 06:34:56 +01:00
|
|
|
Vector2 phys_offset;
|
2016-07-05 20:40:22 +02:00
|
|
|
Size2 s;
|
2015-03-09 06:34:56 +01:00
|
|
|
|
|
|
|
if (mi->is_region()) {
|
2017-03-05 16:44:50 +01:00
|
|
|
s = mi->get_region_rect().size;
|
|
|
|
p_library->tile_set_region(id, mi->get_region_rect());
|
2016-07-05 20:40:22 +02:00
|
|
|
} else {
|
|
|
|
const int frame = mi->get_frame();
|
|
|
|
const int hframes = mi->get_hframes();
|
2017-03-05 16:44:50 +01:00
|
|
|
s = texture->get_size() / Size2(hframes, mi->get_vframes());
|
|
|
|
p_library->tile_set_region(id, Rect2(Vector2(frame % hframes, frame / hframes) * s, s));
|
2016-07-05 20:40:22 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
|
2016-07-05 20:40:22 +02:00
|
|
|
if (mi->is_centered()) {
|
2017-03-05 16:44:50 +01:00
|
|
|
phys_offset += -s / 2;
|
2015-03-09 06:34:56 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector<Ref<Shape2D> > collisions;
|
2015-03-09 06:34:56 +01:00
|
|
|
Ref<NavigationPolygon> nav_poly;
|
|
|
|
Ref<OccluderPolygon2D> occluder;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int j = 0; j < mi->get_child_count(); j++) {
|
2015-03-09 06:34:56 +01:00
|
|
|
|
|
|
|
Node *child2 = mi->get_child(j);
|
|
|
|
|
|
|
|
if (child2->cast_to<NavigationPolygonInstance>())
|
2017-03-05 16:44:50 +01:00
|
|
|
nav_poly = child2->cast_to<NavigationPolygonInstance>()->get_navigation_polygon();
|
2015-03-09 06:34:56 +01:00
|
|
|
|
|
|
|
if (child2->cast_to<LightOccluder2D>())
|
2017-03-05 16:44:50 +01:00
|
|
|
occluder = child2->cast_to<LightOccluder2D>()->get_occluder_polygon();
|
2015-03-09 06:34:56 +01:00
|
|
|
|
|
|
|
if (!child2->cast_to<StaticBody2D>())
|
|
|
|
continue;
|
|
|
|
StaticBody2D *sb = child2->cast_to<StaticBody2D>();
|
2017-06-24 04:30:43 +02:00
|
|
|
|
|
|
|
List<uint32_t> shapes;
|
|
|
|
sb->get_shape_owners(&shapes);
|
|
|
|
|
|
|
|
for (List<uint32_t>::Element *E = shapes.front(); E; E = E->next()) {
|
|
|
|
|
|
|
|
for (int k = 0; k < sb->shape_owner_get_shape_count(E->get()); k++) {
|
|
|
|
|
|
|
|
Ref<Shape> shape = sb->shape_owner_get_shape(E->get(), k);
|
|
|
|
collisions.push_back(shape); //uh what about transform?
|
2015-10-25 23:13:46 +01:00
|
|
|
}
|
2014-04-19 21:46:52 +02:00
|
|
|
}
|
2015-03-09 06:34:56 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2015-03-09 06:34:56 +01:00
|
|
|
if (collisions.size()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
p_library->tile_set_shapes(id, collisions);
|
|
|
|
p_library->tile_set_shape_offset(id, -phys_offset);
|
2015-03-09 06:34:56 +01:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
p_library->tile_set_shape_offset(id, Vector2());
|
2015-03-09 06:34:56 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
p_library->tile_set_texture_offset(id, mi->get_offset());
|
|
|
|
p_library->tile_set_navigation_polygon(id, nav_poly);
|
|
|
|
p_library->tile_set_light_occluder(id, occluder);
|
|
|
|
p_library->tile_set_occluder_offset(id, -phys_offset);
|
|
|
|
p_library->tile_set_navigation_polygon_offset(id, -phys_offset);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-19 23:49:37 +02:00
|
|
|
void TileSetEditor::_import_scene(Node *scene, Ref<TileSet> p_library, bool p_merge) {
|
|
|
|
|
|
|
|
if (!p_merge)
|
|
|
|
p_library->clear();
|
|
|
|
|
|
|
|
_import_node(scene, p_library);
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void TileSetEditor::_menu_confirm() {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
switch (option) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2015-03-09 06:34:56 +01:00
|
|
|
case MENU_OPTION_MERGE_FROM_SCENE:
|
|
|
|
case MENU_OPTION_CREATE_FROM_SCENE: {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2015-03-09 06:34:56 +01:00
|
|
|
EditorNode *en = editor;
|
2017-03-05 16:44:50 +01:00
|
|
|
Node *scene = en->get_edited_scene();
|
2015-03-09 06:34:56 +01:00
|
|
|
if (!scene)
|
|
|
|
break;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_import_scene(scene, tileset, option == MENU_OPTION_MERGE_FROM_SCENE);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2015-03-09 06:34:56 +01:00
|
|
|
} break;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void TileSetEditor::_name_dialog_confirm(const String &name) {
|
2015-12-15 23:35:35 +01:00
|
|
|
|
|
|
|
switch (option) {
|
|
|
|
|
|
|
|
case MENU_OPTION_REMOVE_ITEM: {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int id = tileset->find_tile_by_name(name);
|
2015-12-15 23:35:35 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (id < 0 && name.is_valid_integer())
|
|
|
|
id = name.to_int();
|
2015-12-15 23:35:35 +01:00
|
|
|
|
|
|
|
if (tileset->has_tile(id)) {
|
|
|
|
tileset->remove_tile(id);
|
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
err_dialog->set_text(TTR("Could not find tile:") + " " + name);
|
2015-12-15 23:35:35 +01:00
|
|
|
err_dialog->popup_centered(Size2(300, 60));
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void TileSetEditor::_menu_cbk(int p_option) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
option = p_option;
|
|
|
|
switch (p_option) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
case MENU_OPTION_ADD_ITEM: {
|
|
|
|
|
|
|
|
tileset->create_tile(tileset->get_last_unused_tile_id());
|
|
|
|
} break;
|
|
|
|
case MENU_OPTION_REMOVE_ITEM: {
|
|
|
|
|
2016-05-04 03:25:37 +02:00
|
|
|
nd->set_title(TTR("Remove Item"));
|
|
|
|
nd->set_text(TTR("Item name or ID:"));
|
2015-12-15 23:35:35 +01:00
|
|
|
nd->popup_centered(Size2(300, 95));
|
2014-02-10 02:10:30 +01:00
|
|
|
} break;
|
|
|
|
case MENU_OPTION_CREATE_FROM_SCENE: {
|
|
|
|
|
2016-05-04 03:25:37 +02:00
|
|
|
cd->set_text(TTR("Create from scene?"));
|
2017-03-05 16:44:50 +01:00
|
|
|
cd->popup_centered(Size2(300, 60));
|
2014-02-10 02:10:30 +01:00
|
|
|
} break;
|
2015-03-09 06:34:56 +01:00
|
|
|
case MENU_OPTION_MERGE_FROM_SCENE: {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-05-04 03:25:37 +02:00
|
|
|
cd->set_text(TTR("Merge from scene?"));
|
2017-03-05 16:44:50 +01:00
|
|
|
cd->popup_centered(Size2(300, 60));
|
2015-03-09 06:34:56 +01:00
|
|
|
} break;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Error TileSetEditor::update_library_file(Node *p_base_scene, Ref<TileSet> ml, bool p_merge) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_import_scene(p_base_scene, ml, p_merge);
|
2014-02-10 02:10:30 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TileSetEditor::_bind_methods() {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method("_menu_cbk", &TileSetEditor::_menu_cbk);
|
|
|
|
ClassDB::bind_method("_menu_confirm", &TileSetEditor::_menu_confirm);
|
|
|
|
ClassDB::bind_method("_name_dialog_confirm", &TileSetEditor::_name_dialog_confirm);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TileSetEditor::TileSetEditor(EditorNode *p_editor) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Panel *panel = memnew(Panel);
|
2014-02-10 02:10:30 +01:00
|
|
|
panel->set_area_as_parent_rect();
|
|
|
|
add_child(panel);
|
2017-03-05 16:44:50 +01:00
|
|
|
MenuButton *options = memnew(MenuButton);
|
2014-02-10 02:10:30 +01:00
|
|
|
panel->add_child(options);
|
2017-03-29 17:29:38 +02:00
|
|
|
options->set_position(Point2(1, 1));
|
2014-02-10 02:10:30 +01:00
|
|
|
options->set_text("Theme");
|
2017-03-05 16:44:50 +01:00
|
|
|
options->get_popup()->add_item(TTR("Add Item"), MENU_OPTION_ADD_ITEM);
|
|
|
|
options->get_popup()->add_item(TTR("Remove Item"), MENU_OPTION_REMOVE_ITEM);
|
2014-02-10 02:10:30 +01:00
|
|
|
options->get_popup()->add_separator();
|
2017-03-05 16:44:50 +01:00
|
|
|
options->get_popup()->add_item(TTR("Create from Scene"), MENU_OPTION_CREATE_FROM_SCENE);
|
|
|
|
options->get_popup()->add_item(TTR("Merge from Scene"), MENU_OPTION_MERGE_FROM_SCENE);
|
|
|
|
options->get_popup()->connect("id_pressed", this, "_menu_cbk");
|
|
|
|
editor = p_editor;
|
2014-02-10 02:10:30 +01:00
|
|
|
cd = memnew(ConfirmationDialog);
|
|
|
|
add_child(cd);
|
2017-03-05 16:44:50 +01:00
|
|
|
cd->get_ok()->connect("pressed", this, "_menu_confirm");
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2015-12-15 23:35:35 +01:00
|
|
|
nd = memnew(EditorNameDialog);
|
|
|
|
add_child(nd);
|
|
|
|
nd->set_hide_on_ok(true);
|
2017-03-05 16:44:50 +01:00
|
|
|
nd->get_line_edit()->set_margin(MARGIN_TOP, 28);
|
|
|
|
nd->connect("name_confirmed", this, "_name_dialog_confirm");
|
2015-12-15 23:35:35 +01:00
|
|
|
|
|
|
|
err_dialog = memnew(AcceptDialog);
|
|
|
|
add_child(err_dialog);
|
2016-05-04 03:25:37 +02:00
|
|
|
err_dialog->set_title(TTR("Error"));
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TileSetEditorPlugin::edit(Object *p_node) {
|
|
|
|
|
|
|
|
if (p_node && p_node->cast_to<TileSet>()) {
|
2017-03-05 16:44:50 +01:00
|
|
|
tileset_editor->edit(p_node->cast_to<TileSet>());
|
2014-02-10 02:10:30 +01:00
|
|
|
tileset_editor->show();
|
|
|
|
} else
|
|
|
|
tileset_editor->hide();
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool TileSetEditorPlugin::handles(Object *p_node) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-01-03 03:03:46 +01:00
|
|
|
return p_node->is_class("TileSet");
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void TileSetEditorPlugin::make_visible(bool p_visible) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (p_visible)
|
|
|
|
tileset_editor->show();
|
|
|
|
else
|
|
|
|
tileset_editor->hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
TileSetEditorPlugin::TileSetEditorPlugin(EditorNode *p_node) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
tileset_editor = memnew(TileSetEditor(p_node));
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
p_node->get_viewport()->add_child(tileset_editor);
|
|
|
|
tileset_editor->set_area_as_parent_rect();
|
2017-03-05 16:44:50 +01:00
|
|
|
tileset_editor->set_anchor(MARGIN_RIGHT, Control::ANCHOR_END);
|
|
|
|
tileset_editor->set_anchor(MARGIN_BOTTOM, Control::ANCHOR_BEGIN);
|
|
|
|
tileset_editor->set_end(Point2(0, 22));
|
2014-02-10 02:10:30 +01:00
|
|
|
tileset_editor->hide();
|
|
|
|
}
|