tile_set_editor_plugin.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*************************************************************************/
  2. /* tile_set_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "tile_set_editor_plugin.h"
  31. #include "scene/2d/physics_body_2d.h"
  32. #include "scene/2d/sprite.h"
  33. void TileSetEditor::edit(const Ref<TileSet> &p_tileset) {
  34. tileset = p_tileset;
  35. }
  36. void TileSetEditor::_import_node(Node *p_node, Ref<TileSet> p_library) {
  37. for (int i = 0; i < p_node->get_child_count(); i++) {
  38. Node *child = p_node->get_child(i);
  39. if (!Object::cast_to<Sprite>(child)) {
  40. if (child->get_child_count() > 0) {
  41. _import_node(child, p_library);
  42. }
  43. continue;
  44. }
  45. Sprite *mi = Object::cast_to<Sprite>(child);
  46. Ref<Texture> texture = mi->get_texture();
  47. Ref<Texture> normal_map = mi->get_normal_map();
  48. Ref<ShaderMaterial> material = mi->get_material();
  49. if (texture.is_null())
  50. continue;
  51. int id = p_library->find_tile_by_name(mi->get_name());
  52. if (id < 0) {
  53. id = p_library->get_last_unused_tile_id();
  54. p_library->create_tile(id);
  55. p_library->tile_set_name(id, mi->get_name());
  56. }
  57. p_library->tile_set_texture(id, texture);
  58. p_library->tile_set_normal_map(id, normal_map);
  59. p_library->tile_set_material(id, material);
  60. p_library->tile_set_modulate(id, mi->get_modulate());
  61. Vector2 phys_offset;
  62. Size2 s;
  63. if (mi->is_region()) {
  64. s = mi->get_region_rect().size;
  65. p_library->tile_set_region(id, mi->get_region_rect());
  66. } else {
  67. const int frame = mi->get_frame();
  68. const int hframes = mi->get_hframes();
  69. s = texture->get_size() / Size2(hframes, mi->get_vframes());
  70. p_library->tile_set_region(id, Rect2(Vector2(frame % hframes, frame / hframes) * s, s));
  71. }
  72. if (mi->is_centered()) {
  73. phys_offset += -s / 2;
  74. }
  75. Vector<TileSet::ShapeData> collisions;
  76. Ref<NavigationPolygon> nav_poly;
  77. Ref<OccluderPolygon2D> occluder;
  78. bool found_collisions = false;
  79. for (int j = 0; j < mi->get_child_count(); j++) {
  80. Node *child2 = mi->get_child(j);
  81. if (Object::cast_to<NavigationPolygonInstance>(child2))
  82. nav_poly = Object::cast_to<NavigationPolygonInstance>(child2)->get_navigation_polygon();
  83. if (Object::cast_to<LightOccluder2D>(child2))
  84. occluder = Object::cast_to<LightOccluder2D>(child2)->get_occluder_polygon();
  85. if (!Object::cast_to<StaticBody2D>(child2))
  86. continue;
  87. found_collisions = true;
  88. StaticBody2D *sb = Object::cast_to<StaticBody2D>(child2);
  89. List<uint32_t> shapes;
  90. sb->get_shape_owners(&shapes);
  91. for (List<uint32_t>::Element *E = shapes.front(); E; E = E->next()) {
  92. if (sb->is_shape_owner_disabled(E->get())) continue;
  93. Transform2D shape_transform = sb->shape_owner_get_transform(E->get());
  94. bool one_way = sb->is_shape_owner_one_way_collision_enabled(E->get());
  95. shape_transform.set_origin(shape_transform.get_origin() - phys_offset);
  96. for (int k = 0; k < sb->shape_owner_get_shape_count(E->get()); k++) {
  97. Ref<Shape2D> shape = sb->shape_owner_get_shape(E->get(), k);
  98. TileSet::ShapeData shape_data;
  99. shape_data.shape = shape;
  100. shape_data.shape_transform = shape_transform;
  101. shape_data.one_way_collision = one_way;
  102. collisions.push_back(shape_data);
  103. }
  104. }
  105. }
  106. if (found_collisions) {
  107. p_library->tile_set_shapes(id, collisions);
  108. }
  109. p_library->tile_set_texture_offset(id, mi->get_offset());
  110. p_library->tile_set_navigation_polygon(id, nav_poly);
  111. p_library->tile_set_light_occluder(id, occluder);
  112. p_library->tile_set_occluder_offset(id, -phys_offset);
  113. p_library->tile_set_navigation_polygon_offset(id, -phys_offset);
  114. }
  115. }
  116. void TileSetEditor::_import_scene(Node *p_scene, Ref<TileSet> p_library, bool p_merge) {
  117. if (!p_merge)
  118. p_library->clear();
  119. _import_node(p_scene, p_library);
  120. }
  121. void TileSetEditor::_menu_confirm() {
  122. switch (option) {
  123. case MENU_OPTION_MERGE_FROM_SCENE:
  124. case MENU_OPTION_CREATE_FROM_SCENE: {
  125. EditorNode *en = editor;
  126. Node *scene = en->get_edited_scene();
  127. if (!scene)
  128. break;
  129. _import_scene(scene, tileset, option == MENU_OPTION_MERGE_FROM_SCENE);
  130. } break;
  131. }
  132. }
  133. void TileSetEditor::_name_dialog_confirm(const String &name) {
  134. switch (option) {
  135. case MENU_OPTION_REMOVE_ITEM: {
  136. int id = tileset->find_tile_by_name(name);
  137. if (id < 0 && name.is_valid_integer())
  138. id = name.to_int();
  139. if (tileset->has_tile(id)) {
  140. tileset->remove_tile(id);
  141. } else {
  142. err_dialog->set_text(TTR("Could not find tile:") + " " + name);
  143. err_dialog->popup_centered(Size2(300, 60));
  144. }
  145. } break;
  146. }
  147. }
  148. void TileSetEditor::_menu_cbk(int p_option) {
  149. option = p_option;
  150. switch (p_option) {
  151. case MENU_OPTION_ADD_ITEM: {
  152. tileset->create_tile(tileset->get_last_unused_tile_id());
  153. } break;
  154. case MENU_OPTION_REMOVE_ITEM: {
  155. nd->set_title(TTR("Remove Item"));
  156. nd->set_text(TTR("Item name or ID:"));
  157. nd->popup_centered(Size2(300, 95));
  158. } break;
  159. case MENU_OPTION_CREATE_FROM_SCENE: {
  160. cd->set_text(TTR("Create from scene?"));
  161. cd->popup_centered(Size2(300, 60));
  162. } break;
  163. case MENU_OPTION_MERGE_FROM_SCENE: {
  164. cd->set_text(TTR("Merge from scene?"));
  165. cd->popup_centered(Size2(300, 60));
  166. } break;
  167. }
  168. }
  169. Error TileSetEditor::update_library_file(Node *p_base_scene, Ref<TileSet> ml, bool p_merge) {
  170. _import_scene(p_base_scene, ml, p_merge);
  171. return OK;
  172. }
  173. void TileSetEditor::_bind_methods() {
  174. ClassDB::bind_method("_menu_cbk", &TileSetEditor::_menu_cbk);
  175. ClassDB::bind_method("_menu_confirm", &TileSetEditor::_menu_confirm);
  176. ClassDB::bind_method("_name_dialog_confirm", &TileSetEditor::_name_dialog_confirm);
  177. }
  178. TileSetEditor::TileSetEditor(EditorNode *p_editor) {
  179. Panel *panel = memnew(Panel);
  180. panel->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  181. add_child(panel);
  182. MenuButton *options = memnew(MenuButton);
  183. panel->add_child(options);
  184. options->set_position(Point2(1, 1));
  185. options->set_text("Theme");
  186. options->get_popup()->add_item(TTR("Add Item"), MENU_OPTION_ADD_ITEM);
  187. options->get_popup()->add_item(TTR("Remove Item"), MENU_OPTION_REMOVE_ITEM);
  188. options->get_popup()->add_separator();
  189. options->get_popup()->add_item(TTR("Create from Scene"), MENU_OPTION_CREATE_FROM_SCENE);
  190. options->get_popup()->add_item(TTR("Merge from Scene"), MENU_OPTION_MERGE_FROM_SCENE);
  191. options->get_popup()->connect("id_pressed", this, "_menu_cbk");
  192. editor = p_editor;
  193. cd = memnew(ConfirmationDialog);
  194. add_child(cd);
  195. cd->get_ok()->connect("pressed", this, "_menu_confirm");
  196. nd = memnew(EditorNameDialog);
  197. add_child(nd);
  198. nd->set_hide_on_ok(true);
  199. nd->get_line_edit()->set_margin(MARGIN_TOP, 28);
  200. nd->connect("name_confirmed", this, "_name_dialog_confirm");
  201. err_dialog = memnew(AcceptDialog);
  202. add_child(err_dialog);
  203. err_dialog->set_title(TTR("Error"));
  204. }
  205. void TileSetEditorPlugin::edit(Object *p_node) {
  206. if (Object::cast_to<TileSet>(p_node)) {
  207. tileset_editor->edit(Object::cast_to<TileSet>(p_node));
  208. tileset_editor->show();
  209. } else
  210. tileset_editor->hide();
  211. }
  212. bool TileSetEditorPlugin::handles(Object *p_node) const {
  213. return p_node->is_class("TileSet");
  214. }
  215. void TileSetEditorPlugin::make_visible(bool p_visible) {
  216. if (p_visible)
  217. tileset_editor->show();
  218. else
  219. tileset_editor->hide();
  220. }
  221. TileSetEditorPlugin::TileSetEditorPlugin(EditorNode *p_node) {
  222. tileset_editor = memnew(TileSetEditor(p_node));
  223. p_node->get_viewport()->add_child(tileset_editor);
  224. tileset_editor->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  225. tileset_editor->set_anchor(MARGIN_BOTTOM, Control::ANCHOR_BEGIN);
  226. tileset_editor->set_end(Point2(0, 22));
  227. tileset_editor->hide();
  228. }