cube_grid_theme_editor_plugin.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*************************************************************************/
  2. /* cube_grid_theme_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 "cube_grid_theme_editor_plugin.h"
  30. #include "scene/3d/mesh_instance.h"
  31. #include "scene/3d/physics_body.h"
  32. #include "scene/main/viewport.h"
  33. #include "scene/resources/packed_scene.h"
  34. #include "tools/editor/editor_node.h"
  35. #include "main/main.h"
  36. #include "tools/editor/editor_settings.h"
  37. #include "scene/3d/navigation_mesh.h"
  38. void MeshLibraryEditor::edit(const Ref<MeshLibrary>& p_theme) {
  39. theme=p_theme;
  40. if (theme.is_valid())
  41. menu->get_popup()->set_item_disabled(menu->get_popup()->get_item_index(MENU_OPTION_UPDATE_FROM_SCENE),!theme->has_meta("_editor_source_scene"));
  42. }
  43. void MeshLibraryEditor::_menu_confirm() {
  44. switch(option) {
  45. case MENU_OPTION_REMOVE_ITEM: {
  46. theme->remove_item(to_erase);
  47. } break;
  48. case MENU_OPTION_UPDATE_FROM_SCENE: {
  49. String existing = theme->get_meta("_editor_source_scene");
  50. ERR_FAIL_COND(existing=="");
  51. _import_scene_cbk(existing);
  52. } break;
  53. default: {};
  54. }
  55. }
  56. void MeshLibraryEditor::_import_scene(Node *p_scene, Ref<MeshLibrary> p_library, bool p_merge) {
  57. if (!p_merge)
  58. p_library->clear();
  59. for(int i=0;i<p_scene->get_child_count();i++) {
  60. Node *child = p_scene->get_child(i);
  61. if (!child->cast_to<MeshInstance>()) {
  62. if (child->get_child_count()>0) {
  63. child=child->get_child(0);
  64. if (!child->cast_to<MeshInstance>()) {
  65. continue;
  66. }
  67. } else
  68. continue;
  69. }
  70. MeshInstance *mi = child->cast_to<MeshInstance>();
  71. Ref<Mesh> mesh=mi->get_mesh();
  72. if (mesh.is_null())
  73. continue;
  74. int id = p_library->find_item_name(mi->get_name());
  75. if (id<0) {
  76. id=p_library->get_last_unused_item_id();
  77. p_library->create_item(id);
  78. p_library->set_item_name(id,mi->get_name());
  79. }
  80. p_library->set_item_mesh(id,mesh);
  81. Ref<Shape> collision;
  82. for(int j=0;j<mi->get_child_count();j++) {
  83. #if 1
  84. Node *child2 = mi->get_child(j);
  85. if (!child2->cast_to<StaticBody>())
  86. continue;
  87. StaticBody *sb = child2->cast_to<StaticBody>();
  88. if (sb->get_shape_count()==0)
  89. continue;
  90. collision=sb->get_shape(0);
  91. if (!collision.is_null())
  92. break;
  93. #endif
  94. }
  95. if (!collision.is_null()) {
  96. p_library->set_item_shape(id,collision);
  97. }
  98. Ref<NavigationMesh> navmesh;
  99. for(int j=0;j<mi->get_child_count();j++) {
  100. Node *child2 = mi->get_child(j);
  101. if (!child2->cast_to<NavigationMeshInstance>())
  102. continue;
  103. NavigationMeshInstance *sb = child2->cast_to<NavigationMeshInstance>();
  104. navmesh=sb->get_navigation_mesh();
  105. if (!navmesh.is_null())
  106. break;
  107. }
  108. if(!navmesh.is_null()){
  109. p_library->set_item_navmesh(id, navmesh);
  110. }
  111. }
  112. //generate previews!
  113. if (1) {
  114. Vector<int> ids = p_library->get_item_list();
  115. RID vp = VS::get_singleton()->viewport_create();
  116. VS::ViewportRect vr;
  117. vr.x=0;
  118. vr.y=0;
  119. vr.width=EditorSettings::get_singleton()->get("grid_map/preview_size");
  120. vr.height=EditorSettings::get_singleton()->get("grid_map/preview_size");
  121. VS::get_singleton()->viewport_set_rect(vp,vr);
  122. VS::get_singleton()->viewport_set_as_render_target(vp,true);
  123. VS::get_singleton()->viewport_set_render_target_update_mode(vp,VS::RENDER_TARGET_UPDATE_ALWAYS);
  124. RID scen = VS::get_singleton()->scenario_create();
  125. VS::get_singleton()->viewport_set_scenario(vp,scen);
  126. RID cam = VS::get_singleton()->camera_create();
  127. VS::get_singleton()->camera_set_transform(cam, Transform() );
  128. VS::get_singleton()->viewport_attach_camera(vp,cam);
  129. RID light = VS::get_singleton()->light_create(VS::LIGHT_DIRECTIONAL);
  130. RID lightinst = VS::get_singleton()->instance_create2(light,scen);
  131. VS::get_singleton()->camera_set_orthogonal(cam,1.0,0.01,1000.0);
  132. EditorProgress ep("mlib","Creating Mesh Library",ids.size());
  133. for(int i=0;i<ids.size();i++) {
  134. int id=ids[i];
  135. Ref<Mesh> mesh = p_library->get_item_mesh(id);
  136. if (!mesh.is_valid())
  137. continue;
  138. AABB aabb= mesh->get_aabb();
  139. print_line("aabb: "+aabb);
  140. Vector3 ofs = aabb.pos + aabb.size*0.5;
  141. aabb.pos-=ofs;
  142. Transform xform;
  143. xform.basis=Matrix3().rotated(Vector3(0,1,0),Math_PI*0.25);
  144. xform.basis = Matrix3().rotated(Vector3(1,0,0),-Math_PI*0.25)*xform.basis;
  145. AABB rot_aabb = xform.xform(aabb);
  146. print_line("rot_aabb: "+rot_aabb);
  147. float m = MAX(rot_aabb.size.x,rot_aabb.size.y)*0.5;
  148. if (m==0)
  149. continue;
  150. m=1.0/m;
  151. m*=0.5;
  152. print_line("scale: "+rtos(m));
  153. xform.basis.scale(Vector3(m,m,m));
  154. xform.origin=-xform.basis.xform(ofs); //-ofs*m;
  155. xform.origin.z-=rot_aabb.size.z*2;
  156. RID inst = VS::get_singleton()->instance_create2(mesh->get_rid(),scen);
  157. VS::get_singleton()->instance_set_transform(inst,xform);
  158. ep.step("Thumbnail..",i);
  159. VS::get_singleton()->viewport_queue_screen_capture(vp);
  160. Main::iteration();
  161. Image img = VS::get_singleton()->viewport_get_screen_capture(vp);
  162. ERR_CONTINUE(img.empty());
  163. Ref<ImageTexture> it( memnew( ImageTexture ));
  164. it->create_from_image(img);
  165. p_library->set_item_preview(id,it);
  166. // print_line("loaded image, size: "+rtos(m)+" dist: "+rtos(dist)+" empty?"+itos(img.empty())+" w: "+itos(it->get_width())+" h: "+itos(it->get_height()));
  167. VS::get_singleton()->free(inst);
  168. }
  169. VS::get_singleton()->free(lightinst);
  170. VS::get_singleton()->free(light);
  171. VS::get_singleton()->free(vp);
  172. VS::get_singleton()->free(cam);
  173. VS::get_singleton()->free(scen);
  174. }
  175. }
  176. void MeshLibraryEditor::_import_scene_cbk(const String& p_str) {
  177. print_line("Impot Callback!");
  178. Ref<PackedScene> ps = ResourceLoader::load(p_str,"PackedScene");
  179. ERR_FAIL_COND(ps.is_null());
  180. Node *scene = ps->instance();
  181. _import_scene(scene,theme,option==MENU_OPTION_UPDATE_FROM_SCENE);
  182. memdelete(scene);
  183. theme->set_meta("_editor_source_scene",p_str);
  184. menu->get_popup()->set_item_disabled(menu->get_popup()->get_item_index(MENU_OPTION_UPDATE_FROM_SCENE),false);
  185. }
  186. Error MeshLibraryEditor::update_library_file(Node *p_base_scene, Ref<MeshLibrary> ml,bool p_merge) {
  187. _import_scene(p_base_scene,ml,p_merge);
  188. return OK;
  189. }
  190. void MeshLibraryEditor::_menu_cbk(int p_option) {
  191. option=p_option;
  192. switch(p_option) {
  193. case MENU_OPTION_ADD_ITEM: {
  194. theme->create_item(theme->get_last_unused_item_id());
  195. } break;
  196. case MENU_OPTION_REMOVE_ITEM: {
  197. String p = editor->get_property_editor()->get_selected_path();
  198. if (p.begins_with("/MeshLibrary/item") && p.get_slice_count("/")>=3) {
  199. to_erase = p.get_slice("/",3).to_int();
  200. cd->set_text("Remove Item "+itos(to_erase)+"?");
  201. cd->popup_centered(Size2(300,60));
  202. }
  203. } break;
  204. case MENU_OPTION_IMPORT_FROM_SCENE: {
  205. file->popup_centered_ratio();
  206. } break;
  207. case MENU_OPTION_UPDATE_FROM_SCENE: {
  208. cd->set_text("Update from existing scene?:\n"+String(theme->get_meta("_editor_source_scene")));
  209. cd->popup_centered(Size2(500,60));
  210. } break;
  211. }
  212. }
  213. void MeshLibraryEditor::_bind_methods() {
  214. ObjectTypeDB::bind_method("_menu_cbk",&MeshLibraryEditor::_menu_cbk);
  215. ObjectTypeDB::bind_method("_menu_confirm",&MeshLibraryEditor::_menu_confirm);
  216. ObjectTypeDB::bind_method("_import_scene_cbk",&MeshLibraryEditor::_import_scene_cbk);
  217. }
  218. MeshLibraryEditor::MeshLibraryEditor(EditorNode *p_editor) {
  219. file = memnew( EditorFileDialog );
  220. file->set_mode(EditorFileDialog::MODE_OPEN_FILE);
  221. //not for now?
  222. List<String> extensions;
  223. ResourceLoader::get_recognized_extensions_for_type("PackedScene",&extensions);
  224. file->clear_filters();
  225. file->set_title("Import Scene");
  226. for(int i=0;i<extensions.size();i++) {
  227. file->add_filter("*."+extensions[i]+" ; "+extensions[i].to_upper());
  228. }
  229. add_child(file);
  230. file->connect("file_selected",this,"_import_scene_cbk");
  231. Panel *panel = memnew( Panel );
  232. panel->set_area_as_parent_rect();
  233. add_child(panel);
  234. MenuButton * options = memnew( MenuButton );
  235. panel->add_child(options);
  236. options->set_pos(Point2(1,1));
  237. options->set_text("Theme");
  238. options->get_popup()->add_item("Add Item",MENU_OPTION_ADD_ITEM);
  239. options->get_popup()->add_item("Remove Selected Item",MENU_OPTION_REMOVE_ITEM);
  240. options->get_popup()->add_separator();
  241. options->get_popup()->add_item("Import from Scene",MENU_OPTION_IMPORT_FROM_SCENE);
  242. options->get_popup()->add_item("Update from Scene",MENU_OPTION_UPDATE_FROM_SCENE);
  243. options->get_popup()->set_item_disabled(options->get_popup()->get_item_index(MENU_OPTION_UPDATE_FROM_SCENE),true);
  244. options->get_popup()->connect("item_pressed", this,"_menu_cbk");
  245. menu=options;
  246. editor=p_editor;
  247. cd = memnew(ConfirmationDialog);
  248. add_child(cd);
  249. cd->get_ok()->connect("pressed", this,"_menu_confirm");
  250. }
  251. void MeshLibraryEditorPlugin::edit(Object *p_node) {
  252. if (p_node && p_node->cast_to<MeshLibrary>()) {
  253. theme_editor->edit( p_node->cast_to<MeshLibrary>() );
  254. theme_editor->show();
  255. } else
  256. theme_editor->hide();
  257. }
  258. bool MeshLibraryEditorPlugin::handles(Object *p_node) const{
  259. return p_node->is_type("MeshLibrary");
  260. }
  261. void MeshLibraryEditorPlugin::make_visible(bool p_visible){
  262. if (p_visible)
  263. theme_editor->show();
  264. else
  265. theme_editor->hide();
  266. }
  267. MeshLibraryEditorPlugin::MeshLibraryEditorPlugin(EditorNode *p_node) {
  268. EDITOR_DEF("grid_map/preview_size",64);
  269. theme_editor = memnew( MeshLibraryEditor(p_node) );
  270. p_node->get_viewport()->add_child(theme_editor);
  271. theme_editor->set_area_as_parent_rect();
  272. theme_editor->set_anchor( MARGIN_RIGHT, Control::ANCHOR_END );
  273. theme_editor->set_anchor( MARGIN_BOTTOM, Control::ANCHOR_BEGIN );
  274. theme_editor->set_end( Point2(0,22) );
  275. theme_editor->hide();
  276. }