tile_set_editor_plugin.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "tile_set_editor_plugin.h"
  30. #include "scene/2d/sprite.h"
  31. #include "scene/2d/physics_body_2d.h"
  32. void TileSetEditor::edit(const Ref<TileSet>& p_tileset) {
  33. tileset=p_tileset;
  34. }
  35. void TileSetEditor::_import_scene(Node *scene, Ref<TileSet> p_library, bool p_merge) {
  36. if (!p_merge)
  37. p_library->clear();
  38. for(int i=0;i<scene->get_child_count();i++) {
  39. Node *child = scene->get_child(i);
  40. if (!child->cast_to<Sprite>()) {
  41. if (child->get_child_count()>0) {
  42. child=child->get_child(0);
  43. if (!child->cast_to<Sprite>()) {
  44. continue;
  45. }
  46. } else
  47. continue;
  48. }
  49. Sprite *mi = child->cast_to<Sprite>();
  50. Ref<Texture> texture=mi->get_texture();
  51. Ref<CanvasItemMaterial> material=mi->get_material();
  52. if (texture.is_null())
  53. continue;
  54. int id = p_library->find_tile_by_name(mi->get_name());
  55. if (id<0) {
  56. id=p_library->get_last_unused_tile_id();
  57. p_library->create_tile(id);
  58. p_library->tile_set_name(id,mi->get_name());
  59. }
  60. p_library->tile_set_texture(id,texture);
  61. p_library->tile_set_material(id,material);
  62. Vector2 phys_offset;
  63. if (mi->is_centered()) {
  64. Size2 s;
  65. if (mi->is_region()) {
  66. s=mi->get_region_rect().size;
  67. } else {
  68. s=texture->get_size();
  69. }
  70. phys_offset+=-s/2;
  71. }
  72. if (mi->is_region()) {
  73. p_library->tile_set_region(id,mi->get_region_rect());
  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. int shape_count = sb->get_shape_count();
  88. if (shape_count==0)
  89. continue;
  90. for (int shape_index=0; shape_index<shape_count; ++shape_index)
  91. {
  92. Ref<Shape2D> collision=sb->get_shape(shape_index);
  93. if (collision.is_valid()) {
  94. collisions.push_back(collision);
  95. }
  96. }
  97. }
  98. if (collisions.size()) {
  99. p_library->tile_set_shapes(id,collisions);
  100. p_library->tile_set_shape_offset(id,-phys_offset);
  101. } else {
  102. p_library->tile_set_shape_offset(id,Vector2());
  103. }
  104. p_library->tile_set_texture_offset(id,mi->get_offset());
  105. p_library->tile_set_navigation_polygon(id,nav_poly);
  106. p_library->tile_set_light_occluder(id,occluder);
  107. p_library->tile_set_occluder_offset(id,-phys_offset);
  108. p_library->tile_set_navigation_polygon_offset(id,-phys_offset);
  109. }
  110. }
  111. void TileSetEditor::_menu_confirm() {
  112. switch(option) {
  113. case MENU_OPTION_MERGE_FROM_SCENE:
  114. case MENU_OPTION_CREATE_FROM_SCENE: {
  115. EditorNode *en = editor;
  116. Node * scene = en->get_edited_scene();
  117. if (!scene)
  118. break;
  119. _import_scene(scene,tileset,option==MENU_OPTION_MERGE_FROM_SCENE);
  120. } break;
  121. }
  122. }
  123. void TileSetEditor::_name_dialog_confirm(const String& name) {
  124. switch (option) {
  125. case MENU_OPTION_REMOVE_ITEM: {
  126. int id=tileset->find_tile_by_name(name);
  127. if (id<0 && name.is_valid_integer())
  128. id=name.to_int();
  129. if (tileset->has_tile(id)) {
  130. tileset->remove_tile(id);
  131. } else {
  132. err_dialog->set_text(TTR("Could not find tile: ") + name);
  133. err_dialog->popup_centered(Size2(300, 60));
  134. }
  135. } break;
  136. }
  137. }
  138. void TileSetEditor::_menu_cbk(int p_option) {
  139. option=p_option;
  140. switch(p_option) {
  141. case MENU_OPTION_ADD_ITEM: {
  142. tileset->create_tile(tileset->get_last_unused_tile_id());
  143. } break;
  144. case MENU_OPTION_REMOVE_ITEM: {
  145. nd->set_title(TTR("Remove Item"));
  146. nd->set_text(TTR("Item name or ID:"));
  147. nd->popup_centered(Size2(300, 95));
  148. } break;
  149. case MENU_OPTION_CREATE_FROM_SCENE: {
  150. cd->set_text(TTR("Create from scene?"));
  151. cd->popup_centered(Size2(300,60));
  152. } break;
  153. case MENU_OPTION_MERGE_FROM_SCENE: {
  154. cd->set_text(TTR("Merge from scene?"));
  155. cd->popup_centered(Size2(300,60));
  156. } break;
  157. }
  158. }
  159. Error TileSetEditor::update_library_file(Node *p_base_scene, Ref<TileSet> ml,bool p_merge) {
  160. _import_scene(p_base_scene,ml,p_merge);
  161. return OK;
  162. }
  163. void TileSetEditor::_bind_methods() {
  164. ObjectTypeDB::bind_method("_menu_cbk",&TileSetEditor::_menu_cbk);
  165. ObjectTypeDB::bind_method("_menu_confirm",&TileSetEditor::_menu_confirm);
  166. ObjectTypeDB::bind_method("_name_dialog_confirm",&TileSetEditor::_name_dialog_confirm);
  167. }
  168. TileSetEditor::TileSetEditor(EditorNode *p_editor) {
  169. Panel *panel = memnew( Panel );
  170. panel->set_area_as_parent_rect();
  171. add_child(panel);
  172. MenuButton * options = memnew( MenuButton );
  173. panel->add_child(options);
  174. options->set_pos(Point2(1,1));
  175. options->set_text("Theme");
  176. options->get_popup()->add_item(TTR("Add Item"),MENU_OPTION_ADD_ITEM);
  177. options->get_popup()->add_item(TTR("Remove Item"),MENU_OPTION_REMOVE_ITEM);
  178. options->get_popup()->add_separator();
  179. options->get_popup()->add_item(TTR("Create from Scene"),MENU_OPTION_CREATE_FROM_SCENE);
  180. options->get_popup()->add_item(TTR("Merge from Scene"),MENU_OPTION_MERGE_FROM_SCENE);
  181. options->get_popup()->connect("item_pressed", this,"_menu_cbk");
  182. editor=p_editor;
  183. cd = memnew(ConfirmationDialog);
  184. add_child(cd);
  185. cd->get_ok()->connect("pressed", this,"_menu_confirm");
  186. nd = memnew(EditorNameDialog);
  187. add_child(nd);
  188. nd->set_hide_on_ok(true);
  189. nd->get_line_edit()->set_margin(MARGIN_TOP,28);
  190. nd->connect("name_confirmed", this,"_name_dialog_confirm");
  191. err_dialog = memnew(AcceptDialog);
  192. add_child(err_dialog);
  193. err_dialog->set_title(TTR("Error"));
  194. }
  195. void TileSetEditorPlugin::edit(Object *p_node) {
  196. if (p_node && p_node->cast_to<TileSet>()) {
  197. tileset_editor->edit( p_node->cast_to<TileSet>() );
  198. tileset_editor->show();
  199. } else
  200. tileset_editor->hide();
  201. }
  202. bool TileSetEditorPlugin::handles(Object *p_node) const{
  203. return p_node->is_type("TileSet");
  204. }
  205. void TileSetEditorPlugin::make_visible(bool p_visible){
  206. if (p_visible)
  207. tileset_editor->show();
  208. else
  209. tileset_editor->hide();
  210. }
  211. TileSetEditorPlugin::TileSetEditorPlugin(EditorNode *p_node) {
  212. tileset_editor = memnew( TileSetEditor(p_node) );
  213. p_node->get_viewport()->add_child(tileset_editor);
  214. tileset_editor->set_area_as_parent_rect();
  215. tileset_editor->set_anchor( MARGIN_RIGHT, Control::ANCHOR_END );
  216. tileset_editor->set_anchor( MARGIN_BOTTOM, Control::ANCHOR_BEGIN );
  217. tileset_editor->set_end( Point2(0,22) );
  218. tileset_editor->hide();
  219. }