tile_set_editor_plugin.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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-2014 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. if (texture.is_null())
  52. continue;
  53. int id = p_library->find_tile_by_name(mi->get_name());
  54. if (id<0) {
  55. id=p_library->get_last_unused_tile_id();
  56. p_library->create_tile(id);
  57. p_library->tile_set_name(id,mi->get_name());
  58. }
  59. p_library->tile_set_texture(id,texture);
  60. if (mi->is_centered()) {
  61. p_library->tile_set_texture_offset(id,texture->get_size()/2);
  62. }
  63. if (mi->is_region()) {
  64. p_library->tile_set_region(id,mi->get_region_rect());
  65. }
  66. Vector<Ref<Shape2D> >collisions;
  67. for(int j=0;j<mi->get_child_count();j++) {
  68. Node *child2 = mi->get_child(j);
  69. if (!child2->cast_to<StaticBody2D>())
  70. continue;
  71. StaticBody2D *sb = child2->cast_to<StaticBody2D>();
  72. if (sb->get_shape_count()==0)
  73. continue;
  74. Ref<Shape2D> collision=sb->get_shape(0);
  75. if (collision.is_valid())
  76. collisions.push_back(collision);
  77. }
  78. if (collisions.size()) {
  79. p_library->tile_set_shapes(id,collisions);
  80. }
  81. }
  82. }
  83. void TileSetEditor::_menu_confirm() {
  84. switch(option) {
  85. case MENU_OPTION_REMOVE_ITEM: {
  86. tileset->remove_tile(to_erase);
  87. } break;
  88. case MENU_OPTION_MERGE_FROM_SCENE:
  89. case MENU_OPTION_CREATE_FROM_SCENE: {
  90. EditorNode *en = editor;
  91. Node * scene = en->get_edited_scene();
  92. if (!scene)
  93. break;
  94. _import_scene(scene,tileset,option==MENU_OPTION_MERGE_FROM_SCENE);
  95. } break;
  96. }
  97. }
  98. void TileSetEditor::_menu_cbk(int p_option) {
  99. option=p_option;
  100. switch(p_option) {
  101. case MENU_OPTION_ADD_ITEM: {
  102. tileset->create_tile(tileset->get_last_unused_tile_id());
  103. } break;
  104. case MENU_OPTION_REMOVE_ITEM: {
  105. String p = editor->get_property_editor()->get_selected_path();
  106. if (p.begins_with("/TileSet") && p.get_slice_count("/")>=2) {
  107. to_erase = p.get_slice("/",2).to_int();
  108. cd->set_text("Remove Item "+itos(to_erase)+"?");
  109. cd->popup_centered(Size2(300,60));
  110. }
  111. } break;
  112. case MENU_OPTION_CREATE_FROM_SCENE: {
  113. cd->set_text("Create from scene?");
  114. cd->popup_centered(Size2(300,60));
  115. } break;
  116. case MENU_OPTION_MERGE_FROM_SCENE: {
  117. cd->set_text("Merge from scene?");
  118. cd->popup_centered(Size2(300,60));
  119. } break;
  120. }
  121. }
  122. Error TileSetEditor::update_library_file(Node *p_base_scene, Ref<TileSet> ml,bool p_merge) {
  123. _import_scene(p_base_scene,ml,p_merge);
  124. return OK;
  125. }
  126. void TileSetEditor::_bind_methods() {
  127. ObjectTypeDB::bind_method("_menu_cbk",&TileSetEditor::_menu_cbk);
  128. ObjectTypeDB::bind_method("_menu_confirm",&TileSetEditor::_menu_confirm);
  129. }
  130. TileSetEditor::TileSetEditor(EditorNode *p_editor) {
  131. Panel *panel = memnew( Panel );
  132. panel->set_area_as_parent_rect();
  133. add_child(panel);
  134. MenuButton * options = memnew( MenuButton );
  135. panel->add_child(options);
  136. options->set_pos(Point2(1,1));
  137. options->set_text("Theme");
  138. options->get_popup()->add_item("Add Item",MENU_OPTION_ADD_ITEM);
  139. options->get_popup()->add_item("Remove Selected Item",MENU_OPTION_REMOVE_ITEM);
  140. options->get_popup()->add_separator();
  141. options->get_popup()->add_item("Create from Scene",MENU_OPTION_CREATE_FROM_SCENE);
  142. options->get_popup()->add_item("Merge from Scene",MENU_OPTION_MERGE_FROM_SCENE);
  143. options->get_popup()->connect("item_pressed", this,"_menu_cbk");
  144. editor=p_editor;
  145. cd = memnew(ConfirmationDialog);
  146. add_child(cd);
  147. cd->get_ok()->connect("pressed", this,"_menu_confirm");
  148. }
  149. void TileSetEditorPlugin::edit(Object *p_node) {
  150. if (p_node && p_node->cast_to<TileSet>()) {
  151. tileset_editor->edit( p_node->cast_to<TileSet>() );
  152. tileset_editor->show();
  153. } else
  154. tileset_editor->hide();
  155. }
  156. bool TileSetEditorPlugin::handles(Object *p_node) const{
  157. return p_node->is_type("TileSet");
  158. }
  159. void TileSetEditorPlugin::make_visible(bool p_visible){
  160. if (p_visible)
  161. tileset_editor->show();
  162. else
  163. tileset_editor->hide();
  164. }
  165. TileSetEditorPlugin::TileSetEditorPlugin(EditorNode *p_node) {
  166. tileset_editor = memnew( TileSetEditor(p_node) );
  167. p_node->get_viewport()->add_child(tileset_editor);
  168. tileset_editor->set_area_as_parent_rect();
  169. tileset_editor->set_anchor( MARGIN_RIGHT, Control::ANCHOR_END );
  170. tileset_editor->set_anchor( MARGIN_BOTTOM, Control::ANCHOR_BEGIN );
  171. tileset_editor->set_end( Point2(0,22) );
  172. tileset_editor->hide();
  173. }