project_manager.cpp 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  1. /*************************************************************************/
  2. /* project_manager.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 "version.h"
  30. #include "project_manager.h"
  31. #include "os/os.h"
  32. #include "os/dir_access.h"
  33. #include "os/file_access.h"
  34. #include "editor_settings.h"
  35. #include "scene/gui/separator.h"
  36. #include "scene/gui/tool_button.h"
  37. #include "io/config_file.h"
  38. #include "scene/gui/line_edit.h"
  39. #include "scene/gui/panel_container.h"
  40. #include "scene/gui/empty_control.h"
  41. #include "scene/gui/texture_frame.h"
  42. #include "scene/gui/margin_container.h"
  43. #include "io/resource_saver.h"
  44. #include "editor_icons.h"
  45. class NewProjectDialog : public ConfirmationDialog {
  46. OBJ_TYPE(NewProjectDialog,ConfirmationDialog);
  47. bool import_mode;
  48. Label *pp,*pn;
  49. Label *error;
  50. LineEdit *project_path;
  51. LineEdit *project_name;
  52. FileDialog *fdialog;
  53. bool _test_path() {
  54. error->set_text("");
  55. get_ok()->set_disabled(true);
  56. DirAccess *d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  57. if (d->change_dir(project_path->get_text())!=OK) {
  58. error->set_text("Invalid Path for Project, Path Must Exist!");
  59. memdelete(d);
  60. return false;
  61. }
  62. if (!import_mode) {
  63. if (d->file_exists("engine.cfg")) {
  64. error->set_text("Invalid Project Path (engine.cfg must not exist).");
  65. memdelete(d);
  66. return false;
  67. }
  68. } else {
  69. if (!d->file_exists("engine.cfg")) {
  70. error->set_text("Invalid Project Path (engine.cfg must exist).");
  71. memdelete(d);
  72. return false;
  73. }
  74. }
  75. memdelete(d);
  76. get_ok()->set_disabled(false);
  77. return true;
  78. }
  79. void _path_text_changed(const String& p_path) {
  80. if ( _test_path() ) {
  81. String sp=p_path;
  82. sp=sp.replace("\\","/");
  83. int lidx=sp.find_last("/");
  84. if (lidx!=-1) {
  85. sp=sp.substr(lidx+1,sp.length());
  86. }
  87. if (sp=="" && import_mode )
  88. sp="Imported Project";
  89. project_name->set_text(sp);
  90. }
  91. }
  92. void _file_selected(const String& p_path) {
  93. String p = p_path;
  94. if (import_mode) {
  95. if (p.ends_with("engine.cfg")) {
  96. p=p.get_base_dir();
  97. }
  98. }
  99. String sp = p.simplify_path();
  100. project_path->set_text(sp);
  101. _path_text_changed(p);
  102. }
  103. void _path_selected(const String& p_path) {
  104. String p = p_path;
  105. String sp = p.simplify_path();
  106. project_path->set_text(sp);
  107. _path_text_changed(p);
  108. }
  109. void _browse_path() {
  110. if (import_mode) {
  111. fdialog->set_mode(FileDialog::MODE_OPEN_FILE);
  112. fdialog->clear_filters();
  113. fdialog->add_filter("engine.cfg ; "_MKSTR(VERSION_NAME)" Project");
  114. } else {
  115. fdialog->set_mode(FileDialog::MODE_OPEN_DIR);
  116. }
  117. fdialog->popup_centered_ratio();
  118. }
  119. void _text_changed(const String& p_text) {
  120. _test_path();
  121. }
  122. void ok_pressed() {
  123. if (!_test_path())
  124. return;
  125. String dir;
  126. if (import_mode) {
  127. dir=project_path->get_text();
  128. } else {
  129. DirAccess *d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  130. if (d->change_dir(project_path->get_text())!=OK) {
  131. error->set_text("Invalid Path for Project (changed anything?)");
  132. memdelete(d);
  133. return;
  134. }
  135. dir=d->get_current_dir();
  136. memdelete(d);
  137. FileAccess *f = FileAccess::open(dir.plus_file("/engine.cfg"),FileAccess::WRITE);
  138. if (!f) {
  139. error->set_text("Couldn't create engine.cfg in project path");
  140. } else {
  141. f->store_line("; Engine configuration file.");
  142. f->store_line("; It's best to edit using the editor UI, not directly,");
  143. f->store_line("; becausethe parameters that go here are not obvious.");
  144. f->store_line("; ");
  145. f->store_line("; Format: ");
  146. f->store_line("; [section] ; section goes between []");
  147. f->store_line("; param=value ; assign values to parameters");
  148. f->store_line("\n");
  149. f->store_line("[application]");
  150. f->store_line("name=\""+project_name->get_text()+"\"");
  151. f->store_line("icon=\"icon.png\"");
  152. memdelete(f);
  153. ResourceSaver::save(dir.plus_file("/icon.png"),get_icon("DefaultProjectIcon","EditorIcons"));
  154. }
  155. }
  156. dir=dir.replace("\\","/");
  157. if (dir.ends_with("/"))
  158. dir=dir.substr(0,dir.length()-1);
  159. String proj=dir.replace("/","::");
  160. EditorSettings::get_singleton()->set("projects/"+proj,dir);
  161. EditorSettings::get_singleton()->save();
  162. hide();
  163. emit_signal("project_created");
  164. }
  165. protected:
  166. static void _bind_methods() {
  167. ObjectTypeDB::bind_method("_browse_path",&NewProjectDialog::_browse_path);
  168. ObjectTypeDB::bind_method("_text_changed",&NewProjectDialog::_text_changed);
  169. ObjectTypeDB::bind_method("_path_text_changed",&NewProjectDialog::_path_text_changed);
  170. ObjectTypeDB::bind_method("_path_selected",&NewProjectDialog::_path_selected);
  171. ObjectTypeDB::bind_method("_file_selected",&NewProjectDialog::_file_selected);
  172. ADD_SIGNAL( MethodInfo("project_created") );
  173. }
  174. public:
  175. void set_import_mode(bool p_import ) {
  176. import_mode=p_import;
  177. }
  178. void show_dialog() {
  179. project_path->clear();
  180. project_name->clear();
  181. if (import_mode) {
  182. set_title("Import Existing Project:");
  183. pp->set_text("Project Path: (Must exist)");
  184. pn->set_text("Project Name:");
  185. pn->hide();
  186. project_name->hide();
  187. popup_centered(Size2(500,125));
  188. } else {
  189. set_title("Create New Project:");
  190. pp->set_text("Project Path:");
  191. pn->set_text("Project Name:");
  192. pn->show();
  193. project_name->show();
  194. popup_centered(Size2(500,145));
  195. }
  196. _test_path();
  197. }
  198. NewProjectDialog() {
  199. VBoxContainer *vb = memnew( VBoxContainer );
  200. add_child(vb);
  201. set_child_rect(vb);
  202. Label* l = memnew(Label);
  203. l->set_text("Project Path:");
  204. vb->add_child(l);
  205. pp=l;
  206. project_path = memnew( LineEdit );
  207. MarginContainer *mc = memnew( MarginContainer );
  208. vb->add_child(mc);
  209. HBoxContainer *pphb = memnew( HBoxContainer );
  210. mc->add_child(pphb);
  211. pphb->add_child(project_path);
  212. project_path->set_h_size_flags(SIZE_EXPAND_FILL);
  213. Button* browse = memnew( Button );
  214. pphb->add_child(browse);
  215. browse->set_text("Browse");
  216. browse->connect("pressed", this,"_browse_path");
  217. l = memnew(Label);
  218. l->set_text("Project Name:");
  219. l->set_pos(Point2(5,50));
  220. vb->add_child(l);
  221. pn=l;
  222. project_name = memnew( LineEdit );
  223. mc = memnew( MarginContainer );
  224. vb->add_child(mc);
  225. mc->add_child(project_name);
  226. project_name->set_text("New Game Project");
  227. l = memnew(Label);
  228. l->set_text("That's a BINGO!");
  229. vb->add_child(l);
  230. error=l;
  231. l->add_color_override("font_color",Color(1,0.4,0.3,0.8));
  232. l->set_align(Label::ALIGN_CENTER);
  233. get_ok()->set_text("Create");
  234. DirAccess *d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  235. project_path->set_text(d->get_current_dir());
  236. memdelete(d);
  237. fdialog = memnew( FileDialog );
  238. add_child(fdialog);
  239. fdialog->set_access(FileDialog::ACCESS_FILESYSTEM);
  240. project_name->connect("text_changed", this,"_text_changed");
  241. project_path->connect("text_changed", this,"_path_text_changed");
  242. fdialog->connect("dir_selected", this,"_path_selected");
  243. fdialog->connect("file_selected", this,"_file_selected");
  244. set_hide_on_ok(false);
  245. import_mode=false;
  246. }
  247. };
  248. struct ProjectItem {
  249. String project;
  250. String path;
  251. String conf;
  252. uint64_t last_modified;
  253. bool favorite;
  254. ProjectItem() {}
  255. ProjectItem(const String &p_project, const String &p_path, const String &p_conf, uint64_t p_last_modified, bool p_favorite=false) {
  256. project = p_project; path = p_path; conf = p_conf; last_modified = p_last_modified; favorite=p_favorite;
  257. }
  258. _FORCE_INLINE_ bool operator <(const ProjectItem& l) const { return last_modified > l.last_modified; }
  259. _FORCE_INLINE_ bool operator ==(const ProjectItem& l) const { return project==l.project; }
  260. };
  261. void ProjectManager::_panel_draw(Node *p_hb) {
  262. HBoxContainer *hb = p_hb->cast_to<HBoxContainer>();
  263. hb->draw_line(Point2(0,hb->get_size().y+1),Point2(hb->get_size().x-10,hb->get_size().y+1),get_color("guide_color","Tree"));
  264. if (selected_list.has(hb->get_meta("name"))) {
  265. hb->draw_style_box(get_stylebox("selected","Tree"),Rect2(Point2(),hb->get_size()-Size2(10,0)));
  266. }
  267. }
  268. void ProjectManager::_panel_input(const InputEvent& p_ev,Node *p_hb) {
  269. if (p_ev.type==InputEvent::MOUSE_BUTTON && p_ev.mouse_button.pressed && p_ev.mouse_button.button_index==BUTTON_LEFT) {
  270. String clicked = p_hb->get_meta("name");
  271. String clicked_main_scene = p_hb->get_meta("main_scene");
  272. if (p_ev.key.mod.shift && selected_list.size()>0 && last_clicked!="" && clicked != last_clicked) {
  273. int clicked_id = -1;
  274. int last_clicked_id = -1;
  275. for(int i=0;i<scroll_childs->get_child_count();i++) {
  276. HBoxContainer *hb = scroll_childs->get_child(i)->cast_to<HBoxContainer>();
  277. if (!hb) continue;
  278. if (hb->get_meta("name") == clicked) clicked_id = i;
  279. if (hb->get_meta("name") == last_clicked) last_clicked_id = i;
  280. }
  281. if (last_clicked_id!=-1 && clicked_id!=-1) {
  282. int min = clicked_id < last_clicked_id? clicked_id : last_clicked_id;
  283. int max = clicked_id > last_clicked_id? clicked_id : last_clicked_id;
  284. for(int i=0; i<scroll_childs->get_child_count(); ++i) {
  285. HBoxContainer *hb = scroll_childs->get_child(i)->cast_to<HBoxContainer>();
  286. if (!hb) continue;
  287. if (i!=clicked_id && (i<min || i>max) && !p_ev.key.mod.control) {
  288. selected_list.erase(hb->get_meta("name"));
  289. } else if (i>=min && i<=max) {
  290. selected_list.insert(hb->get_meta("name"), hb->get_meta("main_scene"));
  291. }
  292. }
  293. }
  294. } else if (selected_list.has(clicked) && p_ev.key.mod.control) {
  295. selected_list.erase(clicked);
  296. } else {
  297. last_clicked = clicked;
  298. if (p_ev.key.mod.control || selected_list.size()==0) {
  299. selected_list.insert(clicked, clicked_main_scene);
  300. } else {
  301. selected_list.clear();
  302. selected_list.insert(clicked, clicked_main_scene);
  303. }
  304. }
  305. String single_selected = "";
  306. if (selected_list.size() == 1) {
  307. single_selected = selected_list.front()->key();
  308. }
  309. single_selected_main = "";
  310. for(int i=0;i<scroll_childs->get_child_count();i++) {
  311. CanvasItem *item = scroll_childs->get_child(i)->cast_to<CanvasItem>();
  312. item->update();
  313. if (single_selected!="" && single_selected == item->get_meta("name"))
  314. single_selected_main = item->get_meta("main_scene");
  315. }
  316. erase_btn->set_disabled(selected_list.size()<1);
  317. open_btn->set_disabled(selected_list.size()<1);
  318. run_btn->set_disabled(selected_list.size()<1 || (selected_list.size()==1 && single_selected_main==""));
  319. if (p_ev.mouse_button.doubleclick)
  320. _open_project(); //open if doubleclicked
  321. }
  322. }
  323. void ProjectManager::_favorite_pressed(Node *p_hb) {
  324. String clicked = p_hb->get_meta("name");
  325. bool favorite = !p_hb->get_meta("favorite");
  326. String proj=clicked.replace(":::",":/");
  327. proj=proj.replace("::","/");
  328. if (favorite) {
  329. EditorSettings::get_singleton()->set("favorite_projects/"+clicked,proj);
  330. } else {
  331. EditorSettings::get_singleton()->erase("favorite_projects/"+clicked);
  332. }
  333. EditorSettings::get_singleton()->save();
  334. call_deferred("_load_recent_projects");
  335. }
  336. void ProjectManager::_load_recent_projects() {
  337. ProjectListFilter::FilterOption filter_option = project_filter->get_filter_option();
  338. String search_term = project_filter->get_search_term();
  339. while(scroll_childs->get_child_count()>0) {
  340. memdelete( scroll_childs->get_child(0));
  341. }
  342. List<PropertyInfo> properties;
  343. EditorSettings::get_singleton()->get_property_list(&properties);
  344. Color font_color = get_color("font_color","Tree");
  345. List<ProjectItem> projects;
  346. List<ProjectItem> favorite_projects;
  347. for(List<PropertyInfo>::Element *E=properties.front();E;E=E->next()) {
  348. String _name = E->get().name;
  349. if (!_name.begins_with("projects/") && !_name.begins_with("favorite_projects/"))
  350. continue;
  351. String path = EditorSettings::get_singleton()->get(_name);
  352. if (filter_option == ProjectListFilter::FILTER_PATH && search_term!="" && path.findn(search_term)==-1)
  353. continue;
  354. String project = _name.get_slice("/",1);
  355. String conf=path.plus_file("engine.cfg");
  356. bool favorite = (_name.begins_with("favorite_projects/"))?true:false;
  357. uint64_t last_modified = 0;
  358. if (FileAccess::exists(conf))
  359. last_modified = FileAccess::get_modified_time(conf);
  360. String fscache = path.plus_file(".fscache");
  361. if (FileAccess::exists(fscache)) {
  362. uint64_t cache_modified = FileAccess::get_modified_time(fscache);
  363. if ( cache_modified > last_modified )
  364. last_modified = cache_modified;
  365. }
  366. ProjectItem item(project, path, conf, last_modified, favorite);
  367. if (favorite)
  368. favorite_projects.push_back(item);
  369. else
  370. projects.push_back(item);
  371. }
  372. projects.sort();
  373. favorite_projects.sort();
  374. for(List<ProjectItem>::Element *E=projects.front();E;) {
  375. List<ProjectItem>::Element *next = E->next();
  376. if (favorite_projects.find(E->get()) != NULL)
  377. projects.erase(E->get());
  378. E=next;
  379. }
  380. for(List<ProjectItem>::Element *E=favorite_projects.back();E;E=E->prev()) {
  381. projects.push_front(E->get());
  382. }
  383. Ref<Texture> favorite_icon = get_icon("Favorites","EditorIcons");
  384. for(List<ProjectItem>::Element *E=projects.front();E;E=E->next()) {
  385. ProjectItem &item = E->get();
  386. String project = item.project;
  387. String path = item.path;
  388. String conf = item.conf;
  389. bool is_favorite = item.favorite;
  390. Ref<ConfigFile> cf = memnew( ConfigFile );
  391. Error err = cf->load(conf);
  392. ERR_CONTINUE(err!=OK);
  393. String project_name="Unnamed Project";
  394. if (cf->has_section_key("application","name")) {
  395. project_name = cf->get_value("application","name");
  396. }
  397. if (filter_option==ProjectListFilter::FILTER_NAME && search_term!="" && project_name.findn(search_term)==-1)
  398. continue;
  399. Ref<Texture> icon;
  400. if (cf->has_section_key("application","icon")) {
  401. String appicon = cf->get_value("application","icon");
  402. if (appicon!="") {
  403. Image img;
  404. Error err = img.load(appicon.replace_first("res://",path+"/"));
  405. if (err==OK) {
  406. img.resize(64,64);
  407. Ref<ImageTexture> it = memnew( ImageTexture );
  408. it->create_from_image(img);
  409. icon=it;
  410. }
  411. }
  412. }
  413. if (icon.is_null()) {
  414. icon=get_icon("DefaultProjectIcon","EditorIcons");
  415. }
  416. String main_scene;
  417. if (cf->has_section_key("application","main_scene")) {
  418. main_scene = cf->get_value("application","main_scene");
  419. }
  420. HBoxContainer *hb = memnew( HBoxContainer );
  421. hb->set_meta("name",project);
  422. hb->set_meta("main_scene",main_scene);
  423. hb->set_meta("favorite",is_favorite);
  424. hb->connect("draw",this,"_panel_draw",varray(hb));
  425. hb->connect("input_event",this,"_panel_input",varray(hb));
  426. VBoxContainer *favorite_box = memnew( VBoxContainer );
  427. TextureButton *favorite = memnew( TextureButton );
  428. favorite->set_normal_texture(favorite_icon);
  429. if (!is_favorite)
  430. favorite->set_opacity(0.2);
  431. favorite->set_v_size_flags(SIZE_EXPAND);
  432. favorite->connect("pressed",this,"_favorite_pressed",varray(hb));
  433. favorite_box->add_child(favorite);
  434. hb->add_child(favorite_box);
  435. TextureFrame *tf = memnew( TextureFrame );
  436. tf->set_texture(icon);
  437. hb->add_child(tf);
  438. VBoxContainer *vb = memnew(VBoxContainer);
  439. hb->add_child(vb);
  440. EmptyControl *ec = memnew( EmptyControl );
  441. ec->set_minsize(Size2(0,1));
  442. vb->add_child(ec);
  443. Label *title = memnew( Label(project_name) );
  444. title->add_font_override("font",get_font("large","Fonts"));
  445. title->add_color_override("font_color",font_color);
  446. vb->add_child(title);
  447. Label *fpath = memnew( Label(path) );
  448. vb->add_child(fpath);
  449. fpath->set_opacity(0.5);
  450. fpath->add_color_override("font_color",font_color);
  451. scroll_childs->add_child(hb);
  452. }
  453. scroll->set_v_scroll(0);
  454. erase_btn->set_disabled(selected_list.size()<1);
  455. open_btn->set_disabled(selected_list.size()<1);
  456. run_btn->set_disabled(selected_list.size()<1 || (selected_list.size()==1 && single_selected_main==""));
  457. }
  458. void ProjectManager::_open_project_confirm() {
  459. for (Map<String,String>::Element *E=selected_list.front(); E; E=E->next()) {
  460. const String &selected = E->key();
  461. String path = EditorSettings::get_singleton()->get("projects/"+selected);
  462. print_line("OPENING: "+path+" ("+selected+")");
  463. List<String> args;
  464. args.push_back("-path");
  465. args.push_back(path);
  466. args.push_back("-editor");
  467. const String &selected_main = E->get();
  468. if (selected_main!="") {
  469. args.push_back(selected_main);
  470. }
  471. String exec = OS::get_singleton()->get_executable_path();
  472. OS::ProcessID pid=0;
  473. Error err = OS::get_singleton()->execute(exec,args,false,&pid);
  474. ERR_FAIL_COND(err);
  475. }
  476. get_scene()->quit();
  477. }
  478. void ProjectManager::_open_project() {
  479. if (selected_list.size()<1) {
  480. return;
  481. }
  482. if (selected_list.size()>1) {
  483. multi_open_ask->set_text("Are you sure to open more than one projects?");
  484. multi_open_ask->popup_centered(Size2(300,100));
  485. } else {
  486. _open_project_confirm();
  487. }
  488. }
  489. void ProjectManager::_run_project_confirm() {
  490. for (Map<String,String>::Element *E=selected_list.front(); E; E=E->next()) {
  491. const String &selected_main = E->get();
  492. if (selected_main == "") continue;
  493. const String &selected = E->key();
  494. String path = EditorSettings::get_singleton()->get("projects/"+selected);
  495. print_line("OPENING: "+path+" ("+selected+")");
  496. List<String> args;
  497. args.push_back("-path");
  498. args.push_back(path);
  499. String exec = OS::get_singleton()->get_executable_path();
  500. OS::ProcessID pid=0;
  501. Error err = OS::get_singleton()->execute(exec,args,false,&pid);
  502. ERR_FAIL_COND(err);
  503. }
  504. // get_scene()->quit(); do not quit
  505. }
  506. void ProjectManager::_run_project() {
  507. if (selected_list.size()<1) {
  508. return;
  509. }
  510. if (selected_list.size()>1) {
  511. multi_run_ask->set_text("Are you sure to run more than one projects?");
  512. multi_run_ask->popup_centered(Size2(300,100));
  513. } else {
  514. _run_project_confirm();
  515. }
  516. }
  517. void ProjectManager::_scan_dir(DirAccess *da,float pos, float total,List<String> *r_projects) {
  518. List<String> subdirs;
  519. da->list_dir_begin();
  520. String n = da->get_next();
  521. while(n!=String()) {
  522. if (da->current_is_dir() && !n.begins_with(".")) {
  523. subdirs.push_front(n);
  524. } else if (n=="engine.cfg") {
  525. r_projects->push_back(da->get_current_dir());
  526. }
  527. n=da->get_next();
  528. }
  529. da->list_dir_end();
  530. int m=0;
  531. for(List<String>::Element *E=subdirs.front();E;E=E->next()) {
  532. da->change_dir(E->get());
  533. float slice=total/subdirs.size();
  534. _scan_dir(da,pos+slice*m,slice,r_projects);
  535. da->change_dir("..");
  536. m++;
  537. }
  538. }
  539. void ProjectManager::_scan_begin(const String& p_base) {
  540. print_line("SCAN PROJECTS AT: "+p_base);
  541. List<String> projects;
  542. DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  543. da->change_dir(p_base);
  544. _scan_dir(da,0,1,&projects);
  545. memdelete(da);
  546. print_line("found: "+itos(projects.size())+" projects.");
  547. for(List<String>::Element *E=projects.front();E;E=E->next()) {
  548. String proj=E->get().replace("/","::");
  549. EditorSettings::get_singleton()->set("projects/"+proj,E->get());
  550. }
  551. EditorSettings::get_singleton()->save();
  552. _load_recent_projects();
  553. }
  554. void ProjectManager::_scan_projects() {
  555. scan_dir->popup_centered_ratio();
  556. }
  557. void ProjectManager::_new_project() {
  558. npdialog->set_import_mode(false);
  559. npdialog->show_dialog();
  560. }
  561. void ProjectManager::_import_project() {
  562. npdialog->set_import_mode(true);
  563. npdialog->show_dialog();
  564. }
  565. void ProjectManager::_erase_project_confirm() {
  566. if (selected_list.size()==0) {
  567. return;
  568. }
  569. for (Map<String,String>::Element *E=selected_list.front(); E; E=E->next()) {
  570. EditorSettings::get_singleton()->erase("projects/"+E->key());
  571. EditorSettings::get_singleton()->erase("favorite_projects/"+E->key());
  572. }
  573. EditorSettings::get_singleton()->save();
  574. selected_list.clear();
  575. last_clicked = "";
  576. single_selected_main="";
  577. _load_recent_projects();
  578. }
  579. void ProjectManager::_erase_project() {
  580. if (selected_list.size()==0)
  581. return;
  582. erase_ask->set_text("Erase project from list?? (Folder contents will not be modified)");
  583. erase_ask->popup_centered(Size2(300,100));
  584. }
  585. void ProjectManager::_exit_dialog() {
  586. get_scene()->quit();
  587. }
  588. void ProjectManager::_bind_methods() {
  589. ObjectTypeDB::bind_method("_open_project",&ProjectManager::_open_project);
  590. ObjectTypeDB::bind_method("_open_project_confirm",&ProjectManager::_open_project_confirm);
  591. ObjectTypeDB::bind_method("_run_project",&ProjectManager::_run_project);
  592. ObjectTypeDB::bind_method("_run_project_confirm",&ProjectManager::_run_project_confirm);
  593. ObjectTypeDB::bind_method("_scan_projects",&ProjectManager::_scan_projects);
  594. ObjectTypeDB::bind_method("_scan_begin",&ProjectManager::_scan_begin);
  595. ObjectTypeDB::bind_method("_import_project",&ProjectManager::_import_project);
  596. ObjectTypeDB::bind_method("_new_project",&ProjectManager::_new_project);
  597. ObjectTypeDB::bind_method("_erase_project",&ProjectManager::_erase_project);
  598. ObjectTypeDB::bind_method("_erase_project_confirm",&ProjectManager::_erase_project_confirm);
  599. ObjectTypeDB::bind_method("_exit_dialog",&ProjectManager::_exit_dialog);
  600. ObjectTypeDB::bind_method("_load_recent_projects",&ProjectManager::_load_recent_projects);
  601. ObjectTypeDB::bind_method("_panel_draw",&ProjectManager::_panel_draw);
  602. ObjectTypeDB::bind_method("_panel_input",&ProjectManager::_panel_input);
  603. ObjectTypeDB::bind_method("_favorite_pressed",&ProjectManager::_favorite_pressed);
  604. }
  605. ProjectManager::ProjectManager() {
  606. int margin = get_constant("margin","Dialogs");
  607. int button_margin = get_constant("button_margin","Dialogs");
  608. // load settings
  609. if (!EditorSettings::get_singleton())
  610. EditorSettings::create();
  611. set_area_as_parent_rect();
  612. Panel *panel = memnew( Panel );
  613. add_child(panel);
  614. panel->set_area_as_parent_rect();
  615. VBoxContainer *vb = memnew( VBoxContainer );
  616. panel->add_child(vb);
  617. vb->set_area_as_parent_rect(20);
  618. Label *l = memnew( Label );
  619. l->set_text(_MKSTR(VERSION_NAME)" - Project Manager");
  620. l->add_font_override("font",get_font("large","Fonts"));
  621. l->set_align(Label::ALIGN_CENTER);
  622. vb->add_child(l);
  623. l = memnew( Label );
  624. l->set_text("v"VERSION_MKSTRING);
  625. //l->add_font_override("font",get_font("bold","Fonts"));
  626. l->set_align(Label::ALIGN_CENTER);
  627. vb->add_child(l);
  628. vb->add_child(memnew(HSeparator));
  629. vb->add_margin_child("\n",memnew(Control));
  630. HBoxContainer *tree_hb = memnew( HBoxContainer);
  631. vb->add_margin_child("Recent Projects:",tree_hb,true);
  632. VBoxContainer *search_tree_vb = memnew(VBoxContainer);
  633. search_tree_vb->set_h_size_flags(SIZE_EXPAND_FILL);
  634. tree_hb->add_child(search_tree_vb);
  635. HBoxContainer *search_box = memnew(HBoxContainer);
  636. search_box->add_spacer(true);
  637. project_filter = memnew(ProjectListFilter);
  638. search_box->add_child(project_filter);
  639. project_filter->connect("filter_changed", this, "_load_recent_projects");
  640. project_filter->set_custom_minimum_size(Size2(250,10));
  641. search_tree_vb->add_child(search_box);
  642. PanelContainer *pc = memnew( PanelContainer);
  643. pc->add_style_override("panel",get_stylebox("bg","Tree"));
  644. search_tree_vb->add_child(pc);
  645. pc->set_v_size_flags(SIZE_EXPAND_FILL);
  646. scroll = memnew( ScrollContainer );
  647. pc->add_child(scroll);
  648. scroll->set_enable_h_scroll(false);
  649. VBoxContainer *tree_vb = memnew( VBoxContainer);
  650. tree_hb->add_child(tree_vb);
  651. scroll_childs = memnew( VBoxContainer );
  652. scroll_childs->set_h_size_flags(SIZE_EXPAND_FILL);
  653. scroll->add_child(scroll_childs);
  654. //HBoxContainer *hb = memnew( HBoxContainer );
  655. //vb->add_child(hb);
  656. Button *open = memnew( Button );
  657. open->set_text("Edit");
  658. tree_vb->add_child(open);
  659. open->connect("pressed", this,"_open_project");
  660. open_btn=open;
  661. Button *run = memnew( Button );
  662. run->set_text("Run");
  663. tree_vb->add_child(run);
  664. run->connect("pressed", this,"_run_project");
  665. run_btn=run;
  666. tree_vb->add_child(memnew( HSeparator ));
  667. Button *scan = memnew( Button );
  668. scan->set_text("Scan");
  669. tree_vb->add_child(scan);
  670. scan->connect("pressed", this,"_scan_projects");
  671. tree_vb->add_child(memnew( HSeparator ));
  672. scan_dir = memnew( FileDialog );
  673. scan_dir->set_access(FileDialog::ACCESS_FILESYSTEM);
  674. scan_dir->set_mode(FileDialog::MODE_OPEN_DIR);
  675. add_child(scan_dir);
  676. scan_dir->connect("dir_selected",this,"_scan_begin");
  677. Button* create = memnew( Button );
  678. create->set_text("New Project");
  679. tree_vb->add_child(create);
  680. create->connect("pressed", this,"_new_project");
  681. Button* import = memnew( Button );
  682. import->set_text("Import");
  683. tree_vb->add_child(import);
  684. import->connect("pressed", this,"_import_project");
  685. Button* erase = memnew( Button );
  686. erase->set_text("Erase");
  687. tree_vb->add_child(erase);
  688. erase->connect("pressed", this,"_erase_project");
  689. erase_btn=erase;
  690. tree_vb->add_spacer();
  691. Button * cancel = memnew( Button );
  692. cancel->set_text("Exit");
  693. tree_vb->add_child(cancel);
  694. cancel->connect("pressed", this,"_exit_dialog");
  695. vb->add_margin_child("\n",memnew(Control));
  696. vb->add_child(memnew(HSeparator));
  697. l = memnew( Label );
  698. String cp;
  699. cp.push_back(0xA9);
  700. cp.push_back(0);
  701. l->set_text(cp+" 2008-2012 Juan Linietsky, Ariel Manzur.");
  702. l->set_align(Label::ALIGN_CENTER);
  703. vb->add_child(l);
  704. erase_ask = memnew( ConfirmationDialog );
  705. erase_ask->get_ok()->set_text("Erase");
  706. erase_ask->get_ok()->connect("pressed", this,"_erase_project_confirm");
  707. add_child(erase_ask);
  708. multi_open_ask = memnew( ConfirmationDialog );
  709. multi_open_ask->get_ok()->set_text("Edit");
  710. multi_open_ask->get_ok()->connect("pressed", this, "_open_project_confirm");
  711. add_child(multi_open_ask);
  712. multi_run_ask = memnew( ConfirmationDialog );
  713. multi_run_ask->get_ok()->set_text("Run");
  714. multi_run_ask->get_ok()->connect("pressed", this, "_run_project_confirm");
  715. add_child(multi_run_ask);
  716. OS::get_singleton()->set_low_processor_usage_mode(true);
  717. npdialog = memnew( NewProjectDialog );
  718. add_child(npdialog);
  719. Ref<Theme> theme = memnew( Theme );
  720. editor_register_icons(theme);
  721. set_theme(theme);
  722. npdialog->connect("project_created", this,"_load_recent_projects");
  723. _load_recent_projects();
  724. //get_ok()->set_text("Open");
  725. //get_ok()->set_text("Exit");
  726. last_clicked = "";
  727. }
  728. ProjectManager::~ProjectManager() {
  729. if (EditorSettings::get_singleton())
  730. EditorSettings::destroy();
  731. }
  732. void ProjectListFilter::_setup_filters() {
  733. filter_option->clear();
  734. filter_option->add_item("Name");
  735. filter_option->add_item("Path");
  736. }
  737. void ProjectListFilter::_command(int p_command) {
  738. switch (p_command) {
  739. case CMD_CLEAR_FILTER: {
  740. if (search_box->get_text()!="") {
  741. search_box->clear();
  742. emit_signal("filter_changed");
  743. }
  744. }break;
  745. }
  746. }
  747. void ProjectListFilter::_search_text_changed(const String &p_newtext) {
  748. emit_signal("filter_changed");
  749. }
  750. String ProjectListFilter::get_search_term() {
  751. return search_box->get_text().strip_edges();
  752. }
  753. ProjectListFilter::FilterOption ProjectListFilter::get_filter_option() {
  754. return _current_filter;
  755. }
  756. void ProjectListFilter::_filter_option_selected(int p_idx) {
  757. FilterOption selected = (FilterOption)(filter_option->get_selected());
  758. if (_current_filter != selected ) {
  759. _current_filter = selected;
  760. emit_signal("filter_changed");
  761. }
  762. }
  763. void ProjectListFilter::_notification(int p_what) {
  764. switch(p_what) {
  765. case NOTIFICATION_ENTER_SCENE: {
  766. clear_search_button->set_icon(get_icon("CloseHover","EditorIcons"));
  767. } break;
  768. }
  769. }
  770. void ProjectListFilter::_bind_methods() {
  771. ObjectTypeDB::bind_method(_MD("_command"),&ProjectListFilter::_command);
  772. ObjectTypeDB::bind_method(_MD("_search_text_changed"), &ProjectListFilter::_search_text_changed);
  773. ObjectTypeDB::bind_method(_MD("_filter_option_selected"), &ProjectListFilter::_filter_option_selected);
  774. ADD_SIGNAL( MethodInfo("filter_changed") );
  775. }
  776. ProjectListFilter::ProjectListFilter() {
  777. _current_filter = FILTER_NAME;
  778. filter_option = memnew(OptionButton);
  779. filter_option->set_custom_minimum_size(Size2(80,10));
  780. filter_option->set_clip_text(true);
  781. filter_option->connect("item_selected", this, "_filter_option_selected");
  782. add_child(filter_option);
  783. _setup_filters();
  784. search_box = memnew( LineEdit );
  785. search_box->connect("text_changed",this,"_search_text_changed");
  786. search_box->set_h_size_flags(SIZE_EXPAND_FILL);
  787. add_child(search_box);
  788. clear_search_button = memnew( ToolButton );
  789. clear_search_button->connect("pressed",this,"_command",make_binds(CMD_CLEAR_FILTER));
  790. add_child(clear_search_button);
  791. }