tile_set_editor_plugin.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*************************************************************************/
  2. /* tile_set_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.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 (!child->cast_to<Sprite>()) {
  40. if (child->get_child_count() > 0) {
  41. _import_node(child, p_library);
  42. }
  43. continue;
  44. }
  45. Sprite *mi = child->cast_to<Sprite>();
  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<Ref<Shape2D> > collisions;
  76. Ref<NavigationPolygon> nav_poly;
  77. Ref<OccluderPolygon2D> occluder;
  78. for (int j = 0; j < mi->get_child_count(); j++) {
  79. Node *child2 = mi->get_child(j);
  80. if (child2->cast_to<NavigationPolygonInstance>())
  81. nav_poly = child2->cast_to<NavigationPolygonInstance>()->get_navigation_polygon();
  82. if (child2->cast_to<LightOccluder2D>())
  83. occluder = child2->cast_to<LightOccluder2D>()->get_occluder_polygon();
  84. if (!child2->cast_to<StaticBody2D>())
  85. continue;
  86. StaticBody2D *sb = child2->cast_to<StaticBody2D>();
  87. List<uint32_t> shapes;
  88. sb->get_shape_owners(&shapes);
  89. for (List<uint32_t>::Element *E = shapes.front(); E; E = E->next()) {
  90. Vector2 shape_offset = sb->shape_owner_get_transform(E->get()).get_origin();
  91. bool one_way = sb->is_shape_owner_one_way_collision_enabled(E->get());
  92. for (int k = 0; k < sb->shape_owner_get_shape_count(E->get()); k++) {
  93. Ref<Shape> shape = sb->shape_owner_get_shape(E->get(), k);
  94. p_library->tile_add_shape(id, shape, shape_offset, one_way);
  95. }
  96. }
  97. }
  98. p_library->tile_set_texture_offset(id, mi->get_offset());
  99. p_library->tile_set_navigation_polygon(id, nav_poly);
  100. p_library->tile_set_light_occluder(id, occluder);
  101. p_library->tile_set_occluder_offset(id, -phys_offset);
  102. p_library->tile_set_navigation_polygon_offset(id, -phys_offset);
  103. }
  104. }
  105. void TileSetEditor::_import_scene(Node *scene, Ref<TileSet> p_library, bool p_merge) {
  106. if (!p_merge)
  107. p_library->clear();
  108. _import_node(scene, p_library);
  109. }
  110. void TileSetEditor::_menu_confirm() {
  111. switch (option) {
  112. case MENU_OPTION_MERGE_FROM_SCENE:
  113. case MENU_OPTION_CREATE_FROM_SCENE: {
  114. EditorNode *en = editor;
  115. Node *scene = en->get_edited_scene();
  116. if (!scene)
  117. break;
  118. _import_scene(scene, tileset, option == MENU_OPTION_MERGE_FROM_SCENE);
  119. } break;
  120. }
  121. }
  122. void TileSetEditor::_name_dialog_confirm(const String &name) {
  123. switch (option) {
  124. case MENU_OPTION_REMOVE_ITEM: {
  125. int id = tileset->find_tile_by_name(name);
  126. if (id < 0 && name.is_valid_integer())
  127. id = name.to_int();
  128. if (tileset->has_tile(id)) {
  129. tileset->remove_tile(id);
  130. } else {
  131. err_dialog->set_text(TTR("Could not find tile:") + " " + name);
  132. err_dialog->popup_centered(Size2(300, 60));
  133. }
  134. } break;
  135. }
  136. }
  137. void TileSetEditor::_menu_cbk(int p_option) {
  138. option = p_option;
  139. switch (p_option) {
  140. case MENU_OPTION_ADD_ITEM: {
  141. tileset->create_tile(tileset->get_last_unused_tile_id());
  142. } break;
  143. case MENU_OPTION_REMOVE_ITEM: {
  144. nd->set_title(TTR("Remove Item"));
  145. nd->set_text(TTR("Item name or ID:"));
  146. nd->popup_centered(Size2(300, 95));
  147. } break;
  148. case MENU_OPTION_CREATE_FROM_SCENE: {
  149. cd->set_text(TTR("Create from scene?"));
  150. cd->popup_centered(Size2(300, 60));
  151. } break;
  152. case MENU_OPTION_MERGE_FROM_SCENE: {
  153. cd->set_text(TTR("Merge from scene?"));
  154. cd->popup_centered(Size2(300, 60));
  155. } break;
  156. }
  157. }
  158. Error TileSetEditor::update_library_file(Node *p_base_scene, Ref<TileSet> ml, bool p_merge) {
  159. _import_scene(p_base_scene, ml, p_merge);
  160. return OK;
  161. }
  162. void TileSetEditor::_bind_methods() {
  163. ClassDB::bind_method("_menu_cbk", &TileSetEditor::_menu_cbk);
  164. ClassDB::bind_method("_menu_confirm", &TileSetEditor::_menu_confirm);
  165. ClassDB::bind_method("_name_dialog_confirm", &TileSetEditor::_name_dialog_confirm);
  166. }
  167. TileSetEditor::TileSetEditor(EditorNode *p_editor) {
  168. Panel *panel = memnew(Panel);
  169. panel->set_area_as_parent_rect();
  170. add_child(panel);
  171. MenuButton *options = memnew(MenuButton);
  172. panel->add_child(options);
  173. options->set_position(Point2(1, 1));
  174. options->set_text("Theme");
  175. options->get_popup()->add_item(TTR("Add Item"), MENU_OPTION_ADD_ITEM);
  176. options->get_popup()->add_item(TTR("Remove Item"), MENU_OPTION_REMOVE_ITEM);
  177. options->get_popup()->add_separator();
  178. options->get_popup()->add_item(TTR("Create from Scene"), MENU_OPTION_CREATE_FROM_SCENE);
  179. options->get_popup()->add_item(TTR("Merge from Scene"), MENU_OPTION_MERGE_FROM_SCENE);
  180. options->get_popup()->connect("id_pressed", this, "_menu_cbk");
  181. editor = p_editor;
  182. cd = memnew(ConfirmationDialog);
  183. add_child(cd);
  184. cd->get_ok()->connect("pressed", this, "_menu_confirm");
  185. nd = memnew(EditorNameDialog);
  186. add_child(nd);
  187. nd->set_hide_on_ok(true);
  188. nd->get_line_edit()->set_margin(MARGIN_TOP, 28);
  189. nd->connect("name_confirmed", this, "_name_dialog_confirm");
  190. err_dialog = memnew(AcceptDialog);
  191. add_child(err_dialog);
  192. err_dialog->set_title(TTR("Error"));
  193. }
  194. void TileSetEditorPlugin::edit(Object *p_node) {
  195. if (p_node && p_node->cast_to<TileSet>()) {
  196. tileset_editor->edit(p_node->cast_to<TileSet>());
  197. tileset_editor->show();
  198. } else
  199. tileset_editor->hide();
  200. }
  201. bool TileSetEditorPlugin::handles(Object *p_node) const {
  202. return p_node->is_class("TileSet");
  203. }
  204. void TileSetEditorPlugin::make_visible(bool p_visible) {
  205. if (p_visible)
  206. tileset_editor->show();
  207. else
  208. tileset_editor->hide();
  209. }
  210. TileSetEditorPlugin::TileSetEditorPlugin(EditorNode *p_node) {
  211. tileset_editor = memnew(TileSetEditor(p_node));
  212. p_node->get_viewport()->add_child(tileset_editor);
  213. tileset_editor->set_area_as_parent_rect();
  214. tileset_editor->set_anchor(MARGIN_RIGHT, Control::ANCHOR_END);
  215. tileset_editor->set_anchor(MARGIN_BOTTOM, Control::ANCHOR_BEGIN);
  216. tileset_editor->set_end(Point2(0, 22));
  217. tileset_editor->hide();
  218. }