editor_feature_profile.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  1. #include "editor_feature_profile.h"
  2. #include "core/io/json.h"
  3. #include "core/os/dir_access.h"
  4. #include "editor/editor_settings.h"
  5. #include "editor_node.h"
  6. #include "editor_scale.h"
  7. const char *EditorFeatureProfile::feature_names[FEATURE_MAX] = {
  8. TTRC("3D Editor"),
  9. TTRC("Script Editor"),
  10. TTRC("Asset Library"),
  11. TTRC("Scene Tree Editing"),
  12. TTRC("Import Dock"),
  13. TTRC("Node Dock"),
  14. TTRC("Filesystem Dock")
  15. };
  16. const char *EditorFeatureProfile::feature_identifiers[FEATURE_MAX] = {
  17. "3d",
  18. "script",
  19. "asset_lib",
  20. "scene_tree",
  21. "import_dock",
  22. "node_dock",
  23. "filesystem_dock"
  24. };
  25. void EditorFeatureProfile::set_disable_class(const StringName &p_class, bool p_disabled) {
  26. if (p_disabled) {
  27. disabled_classes.insert(p_class);
  28. } else {
  29. disabled_classes.erase(p_class);
  30. }
  31. }
  32. bool EditorFeatureProfile::is_class_disabled(const StringName &p_class) const {
  33. return disabled_classes.has(p_class);
  34. }
  35. void EditorFeatureProfile::set_disable_class_editor(const StringName &p_class, bool p_disabled) {
  36. if (p_disabled) {
  37. disabled_editors.insert(p_class);
  38. } else {
  39. disabled_editors.erase(p_class);
  40. }
  41. }
  42. bool EditorFeatureProfile::is_class_editor_disabled(const StringName &p_class) const {
  43. return disabled_editors.has(p_class);
  44. }
  45. void EditorFeatureProfile::set_disable_class_property(const StringName &p_class, const StringName &p_property, bool p_disabled) {
  46. if (p_disabled) {
  47. if (!disabled_properties.has(p_class)) {
  48. disabled_properties[p_class] = Set<StringName>();
  49. }
  50. disabled_properties[p_class].insert(p_property);
  51. } else {
  52. ERR_FAIL_COND(!disabled_properties.has(p_class));
  53. disabled_properties[p_class].erase(p_property);
  54. if (disabled_properties[p_class].empty()) {
  55. disabled_properties.erase(p_class);
  56. }
  57. }
  58. }
  59. bool EditorFeatureProfile::is_class_property_disabled(const StringName &p_class, const StringName &p_property) const {
  60. if (!disabled_properties.has(p_class)) {
  61. return false;
  62. }
  63. if (!disabled_properties[p_class].has(p_property)) {
  64. return false;
  65. }
  66. return true;
  67. }
  68. bool EditorFeatureProfile::has_class_properties_disabled(const StringName &p_class) const {
  69. return disabled_properties.has(p_class);
  70. }
  71. void EditorFeatureProfile::set_disable_feature(Feature p_feature, bool p_disable) {
  72. ERR_FAIL_INDEX(p_feature, FEATURE_MAX);
  73. features_disabled[p_feature] = p_disable;
  74. }
  75. bool EditorFeatureProfile::is_feature_disabled(Feature p_feature) const {
  76. ERR_FAIL_INDEX_V(p_feature, FEATURE_MAX, false);
  77. return features_disabled[p_feature];
  78. }
  79. String EditorFeatureProfile::get_feature_name(Feature p_feature) {
  80. ERR_FAIL_INDEX_V(p_feature, FEATURE_MAX, String());
  81. return feature_names[p_feature];
  82. }
  83. Error EditorFeatureProfile::save_to_file(const String &p_path) {
  84. Dictionary json;
  85. json["type"] = "feature_profile";
  86. Array dis_classes;
  87. for (Set<StringName>::Element *E = disabled_classes.front(); E; E = E->next()) {
  88. dis_classes.push_back(String(E->get()));
  89. }
  90. dis_classes.sort();
  91. json["disabled_classes"] = dis_classes;
  92. Array dis_editors;
  93. for (Set<StringName>::Element *E = disabled_editors.front(); E; E = E->next()) {
  94. dis_editors.push_back(String(E->get()));
  95. }
  96. dis_editors.sort();
  97. json["disabled_editors"] = dis_editors;
  98. Array dis_props;
  99. for (Map<StringName, Set<StringName> >::Element *E = disabled_properties.front(); E; E = E->next()) {
  100. for (Set<StringName>::Element *F = E->get().front(); F; F = F->next()) {
  101. dis_props.push_back(String(E->key()) + ":" + String(F->get()));
  102. }
  103. }
  104. json["disabled_properties"] = dis_props;
  105. Array dis_features;
  106. for (int i = 0; i < FEATURE_MAX; i++) {
  107. if (features_disabled[i]) {
  108. dis_features.push_back(feature_identifiers[i]);
  109. }
  110. }
  111. json["disabled_features"] = dis_features;
  112. FileAccessRef f = FileAccess::open(p_path, FileAccess::WRITE);
  113. ERR_FAIL_COND_V(!f, ERR_CANT_OPEN);
  114. String text = JSON::print(json, "\t");
  115. f->store_string(text);
  116. f->close();
  117. return OK;
  118. }
  119. Error EditorFeatureProfile::load_from_file(const String &p_path) {
  120. Error err;
  121. String text = FileAccess::get_file_as_string(p_path, &err);
  122. if (err != OK) {
  123. return err;
  124. }
  125. String err_str;
  126. int err_line;
  127. Variant v;
  128. err = JSON::parse(text, v, err_str, err_line);
  129. if (err != OK) {
  130. ERR_PRINTS("Error parsing '" + p_path + "' on line " + itos(err_line) + ": " + err_str);
  131. return ERR_PARSE_ERROR;
  132. }
  133. Dictionary json = v;
  134. if (!json.has("type") || String(json["type"]) != "feature_profile") {
  135. ERR_PRINTS("Error parsing '" + p_path + "', it's not a feature profile.");
  136. return ERR_PARSE_ERROR;
  137. }
  138. disabled_classes.clear();
  139. if (json.has("disabled_classes")) {
  140. Array disabled_classes_arr = json["disabled_classes"];
  141. for (int i = 0; i < disabled_classes_arr.size(); i++) {
  142. disabled_classes.insert(disabled_classes_arr[i]);
  143. }
  144. }
  145. disabled_editors.clear();
  146. if (json.has("disabled_editors")) {
  147. Array disabled_editors_arr = json["disabled_editors"];
  148. for (int i = 0; i < disabled_editors_arr.size(); i++) {
  149. disabled_editors.insert(disabled_editors_arr[i]);
  150. }
  151. }
  152. disabled_properties.clear();
  153. if (json.has("disabled_properties")) {
  154. Array disabled_properties_arr = json["disabled_properties"];
  155. for (int i = 0; i < disabled_properties_arr.size(); i++) {
  156. String s = disabled_properties_arr[i];
  157. set_disable_class_property(s.get_slice(":", 0), s.get_slice(":", 1), true);
  158. }
  159. }
  160. if (json.has("disabled_features")) {
  161. Array disabled_features_arr = json["disabled_features"];
  162. for (int i = 0; i < FEATURE_MAX; i++) {
  163. bool found = false;
  164. String f = feature_identifiers[i];
  165. for (int j = 0; j < disabled_features_arr.size(); j++) {
  166. String fd = disabled_features_arr[j];
  167. if (fd == f) {
  168. found = true;
  169. break;
  170. }
  171. }
  172. features_disabled[i] = found;
  173. }
  174. }
  175. return OK;
  176. }
  177. void EditorFeatureProfile::_bind_methods() {
  178. ClassDB::bind_method(D_METHOD("set_disable_class", "class_name", "disable"), &EditorFeatureProfile::set_disable_class);
  179. ClassDB::bind_method(D_METHOD("is_class_disabled", "class_name"), &EditorFeatureProfile::is_class_disabled);
  180. ClassDB::bind_method(D_METHOD("set_disable_class_editor", "class_name", "disable"), &EditorFeatureProfile::set_disable_class_editor);
  181. ClassDB::bind_method(D_METHOD("is_class_editor_disabled", "class_name"), &EditorFeatureProfile::is_class_editor_disabled);
  182. ClassDB::bind_method(D_METHOD("set_disable_class_property", "class_name", "property"), &EditorFeatureProfile::set_disable_class_property);
  183. ClassDB::bind_method(D_METHOD("is_class_property_disabled", "class_name"), &EditorFeatureProfile::is_class_property_disabled);
  184. ClassDB::bind_method(D_METHOD("set_disable_feature", "feature", "disable"), &EditorFeatureProfile::set_disable_feature);
  185. ClassDB::bind_method(D_METHOD("is_feature_disabled", "feature"), &EditorFeatureProfile::is_feature_disabled);
  186. ClassDB::bind_method(D_METHOD("get_feature_name", "feature"), &EditorFeatureProfile::_get_feature_name);
  187. ClassDB::bind_method(D_METHOD("save_to_file", "path"), &EditorFeatureProfile::save_to_file);
  188. ClassDB::bind_method(D_METHOD("load_from_file", "path"), &EditorFeatureProfile::load_from_file);
  189. BIND_ENUM_CONSTANT(FEATURE_3D);
  190. BIND_ENUM_CONSTANT(FEATURE_SCRIPT);
  191. BIND_ENUM_CONSTANT(FEATURE_ASSET_LIB);
  192. BIND_ENUM_CONSTANT(FEATURE_SCENE_TREE);
  193. BIND_ENUM_CONSTANT(FEATURE_IMPORT_DOCK);
  194. BIND_ENUM_CONSTANT(FEATURE_NODE_DOCK);
  195. BIND_ENUM_CONSTANT(FEATURE_FILESYSTEM_DOCK);
  196. BIND_ENUM_CONSTANT(FEATURE_MAX);
  197. }
  198. EditorFeatureProfile::EditorFeatureProfile() {
  199. for (int i = 0; i < FEATURE_MAX; i++) {
  200. features_disabled[i] = false;
  201. }
  202. }
  203. //////////////////////////
  204. void EditorFeatureProfileManager::_notification(int p_what) {
  205. if (p_what == NOTIFICATION_READY) {
  206. current_profile = EDITOR_GET("_default_feature_profile");
  207. if (current_profile != String()) {
  208. current.instance();
  209. Error err = current->load_from_file(EditorSettings::get_singleton()->get_feature_profiles_dir().plus_file(current_profile + ".profile"));
  210. if (err != OK) {
  211. ERR_PRINTS("Error loading default feature profile: " + current_profile);
  212. current_profile = String();
  213. current.unref();
  214. }
  215. }
  216. _update_profile_list(current_profile);
  217. }
  218. }
  219. String EditorFeatureProfileManager::_get_selected_profile() {
  220. int idx = profile_list->get_selected();
  221. if (idx < 0) {
  222. return String();
  223. }
  224. return profile_list->get_item_metadata(idx);
  225. }
  226. void EditorFeatureProfileManager::_update_profile_list(const String &p_select_profile) {
  227. String selected_profile;
  228. if (p_select_profile == String()) { //default, keep
  229. if (profile_list->get_selected() >= 0) {
  230. selected_profile = profile_list->get_item_metadata(profile_list->get_selected());
  231. if (!FileAccess::exists(EditorSettings::get_singleton()->get_feature_profiles_dir().plus_file(selected_profile + ".profile"))) {
  232. selected_profile = String(); //does not exist
  233. }
  234. }
  235. } else {
  236. selected_profile = p_select_profile;
  237. }
  238. Vector<String> profiles;
  239. DirAccessRef d = DirAccess::open(EditorSettings::get_singleton()->get_feature_profiles_dir());
  240. ERR_FAIL_COND(!d);
  241. d->list_dir_begin();
  242. while (true) {
  243. String f = d->get_next();
  244. if (f == String()) {
  245. break;
  246. }
  247. if (!d->current_is_dir()) {
  248. int last_pos = f.find_last(".profile");
  249. if (last_pos != -1) {
  250. profiles.push_back(f.substr(0, last_pos));
  251. }
  252. }
  253. }
  254. profiles.sort();
  255. profile_list->clear();
  256. for (int i = 0; i < profiles.size(); i++) {
  257. String name = profiles[i];
  258. if (i == 0 && selected_profile == String()) {
  259. selected_profile = name;
  260. }
  261. if (name == current_profile) {
  262. name += " (current)";
  263. }
  264. profile_list->add_item(name);
  265. int index = profile_list->get_item_count() - 1;
  266. profile_list->set_item_metadata(index, profiles[i]);
  267. if (profiles[i] == selected_profile) {
  268. profile_list->select(index);
  269. }
  270. }
  271. profile_actions[PROFILE_CLEAR]->set_disabled(current_profile == String());
  272. profile_actions[PROFILE_ERASE]->set_disabled(selected_profile == String());
  273. profile_actions[PROFILE_EXPORT]->set_disabled(selected_profile == String());
  274. profile_actions[PROFILE_SET]->set_disabled(selected_profile == String());
  275. current_profile_name->set_text(current_profile);
  276. _update_selected_profile();
  277. }
  278. void EditorFeatureProfileManager::_profile_action(int p_action) {
  279. switch (p_action) {
  280. case PROFILE_CLEAR: {
  281. EditorSettings::get_singleton()->set("_default_feature_profile", "");
  282. EditorSettings::get_singleton()->save();
  283. current_profile = "";
  284. current.unref();
  285. _update_profile_list();
  286. } break;
  287. case PROFILE_SET: {
  288. String selected = _get_selected_profile();
  289. ERR_FAIL_COND(selected == String());
  290. if (selected == current_profile) {
  291. return; //nothing to do here
  292. }
  293. EditorSettings::get_singleton()->set("_default_feature_profile", selected);
  294. EditorSettings::get_singleton()->save();
  295. current_profile = selected;
  296. current = edited;
  297. _update_profile_list();
  298. } break;
  299. case PROFILE_IMPORT: {
  300. import_profiles->popup_centered_ratio();
  301. } break;
  302. case PROFILE_EXPORT: {
  303. export_profile->popup_centered_ratio();
  304. export_profile->set_current_file(_get_selected_profile() + ".profile");
  305. } break;
  306. case PROFILE_NEW: {
  307. new_profile_dialog->popup_centered_minsize();
  308. new_profile_name->clear();
  309. new_profile_name->grab_focus();
  310. } break;
  311. case PROFILE_ERASE: {
  312. String selected = _get_selected_profile();
  313. ERR_FAIL_COND(selected == String());
  314. erase_profile_dialog->set_text(vformat(TTR("Erase profile '%s'? (no undo)"), selected));
  315. erase_profile_dialog->popup_centered_minsize();
  316. } break;
  317. }
  318. }
  319. void EditorFeatureProfileManager::_erase_selected_profile() {
  320. String selected = _get_selected_profile();
  321. ERR_FAIL_COND(selected == String());
  322. DirAccessRef da = DirAccess::open(EditorSettings::get_singleton()->get_feature_profiles_dir());
  323. ERR_FAIL_COND(!da);
  324. da->remove(selected + ".profile");
  325. if (selected == current_profile) {
  326. _profile_action(PROFILE_CLEAR);
  327. } else {
  328. _update_profile_list();
  329. }
  330. }
  331. void EditorFeatureProfileManager::_create_new_profile() {
  332. String name = new_profile_name->get_text().strip_edges();
  333. if (!name.is_valid_filename() || name.find(".") != -1) {
  334. EditorNode::get_singleton()->show_warning(TTR("Profile must be a valid filename and must not contain '.'"));
  335. return;
  336. }
  337. String file = EditorSettings::get_singleton()->get_feature_profiles_dir().plus_file(name + ".profile");
  338. if (FileAccess::exists(file)) {
  339. EditorNode::get_singleton()->show_warning(TTR("Profile with this name already exists."));
  340. return;
  341. }
  342. Ref<EditorFeatureProfile> new_profile;
  343. new_profile.instance();
  344. new_profile->save_to_file(file);
  345. _update_profile_list(name);
  346. }
  347. void EditorFeatureProfileManager::_profile_selected(int p_what) {
  348. _update_selected_profile();
  349. }
  350. void EditorFeatureProfileManager::_fill_classes_from(TreeItem *p_parent, const String &p_class, const String &p_selected) {
  351. TreeItem *class_item = class_list->create_item(p_parent);
  352. class_item->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  353. class_item->set_icon(0, EditorNode::get_singleton()->get_class_icon(p_class, "Node"));
  354. String text = p_class;
  355. bool disabled = edited->is_class_disabled(p_class);
  356. bool disabled_editor = edited->is_class_editor_disabled(p_class);
  357. bool disabled_properties = edited->has_class_properties_disabled(p_class);
  358. if (disabled) {
  359. class_item->set_custom_color(0, get_color("disabled_font_color", "Editor"));
  360. } else if (disabled_editor && disabled_properties) {
  361. text += " " + TTR("(Editor Disabled, Properties Disabled)");
  362. } else if (disabled_properties) {
  363. text += " " + TTR("(Properties Disabled)");
  364. } else if (disabled_editor) {
  365. text += " " + TTR("(Editor Disabled)");
  366. }
  367. class_item->set_text(0, text);
  368. class_item->set_editable(0, true);
  369. class_item->set_selectable(0, true);
  370. class_item->set_metadata(0, p_class);
  371. if (p_class == p_selected) {
  372. class_item->select(0);
  373. }
  374. if (disabled) {
  375. //class disabled, do nothing else (do not show further)
  376. return;
  377. }
  378. class_item->set_checked(0, true); // if its not disabled, its checked
  379. List<StringName> child_classes;
  380. ClassDB::get_direct_inheriters_from_class(p_class, &child_classes);
  381. child_classes.sort_custom<StringName::AlphCompare>();
  382. for (List<StringName>::Element *E = child_classes.front(); E; E = E->next()) {
  383. String name = E->get();
  384. if (name.begins_with("Editor") || ClassDB::get_api_type(name) != ClassDB::API_CORE) {
  385. continue;
  386. }
  387. _fill_classes_from(class_item, name, p_selected);
  388. }
  389. }
  390. void EditorFeatureProfileManager::_class_list_item_selected() {
  391. if (updating_features)
  392. return;
  393. property_list->clear();
  394. TreeItem *item = class_list->get_selected();
  395. if (!item) {
  396. return;
  397. }
  398. Variant md = item->get_metadata(0);
  399. if (md.get_type() != Variant::STRING) {
  400. return;
  401. }
  402. String class_name = md;
  403. if (edited->is_class_disabled(class_name)) {
  404. return;
  405. }
  406. updating_features = true;
  407. TreeItem *root = property_list->create_item();
  408. TreeItem *options = property_list->create_item(root);
  409. options->set_text(0, TTR("Class Options:"));
  410. {
  411. TreeItem *option = property_list->create_item(options);
  412. option->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  413. option->set_editable(0, true);
  414. option->set_selectable(0, true);
  415. option->set_checked(0, !edited->is_class_editor_disabled(class_name));
  416. option->set_text(0, TTR("Enable Contextual Editor"));
  417. option->set_metadata(0, CLASS_OPTION_DISABLE_EDITOR);
  418. }
  419. TreeItem *properties = property_list->create_item(root);
  420. properties->set_text(0, TTR("Enabled Properties:"));
  421. List<PropertyInfo> props;
  422. ClassDB::get_property_list(class_name, &props, true);
  423. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  424. String name = E->get().name;
  425. if (!(E->get().usage & PROPERTY_USAGE_EDITOR))
  426. continue;
  427. TreeItem *property = property_list->create_item(properties);
  428. property->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  429. property->set_editable(0, true);
  430. property->set_selectable(0, true);
  431. property->set_checked(0, !edited->is_class_property_disabled(class_name, name));
  432. property->set_text(0, name.capitalize());
  433. property->set_metadata(0, name);
  434. String icon_type = Variant::get_type_name(E->get().type);
  435. property->set_icon(0, EditorNode::get_singleton()->get_class_icon(icon_type));
  436. }
  437. updating_features = false;
  438. }
  439. void EditorFeatureProfileManager::_class_list_item_edited() {
  440. if (updating_features)
  441. return;
  442. TreeItem *item = class_list->get_edited();
  443. if (!item) {
  444. return;
  445. }
  446. bool checked = item->is_checked(0);
  447. Variant md = item->get_metadata(0);
  448. if (md.get_type() == Variant::STRING) {
  449. String class_selected = md;
  450. edited->set_disable_class(class_selected, !checked);
  451. _save_and_update();
  452. _update_selected_profile();
  453. } else if (md.get_type() == Variant::INT) {
  454. int feature_selected = md;
  455. edited->set_disable_feature(EditorFeatureProfile::Feature(feature_selected), !checked);
  456. _save_and_update();
  457. }
  458. }
  459. void EditorFeatureProfileManager::_property_item_edited() {
  460. if (updating_features)
  461. return;
  462. TreeItem *class_item = class_list->get_selected();
  463. if (!class_item) {
  464. return;
  465. }
  466. Variant md = class_item->get_metadata(0);
  467. if (md.get_type() != Variant::STRING) {
  468. return;
  469. }
  470. String class_name = md;
  471. TreeItem *item = property_list->get_edited();
  472. if (!item) {
  473. return;
  474. }
  475. bool checked = item->is_checked(0);
  476. md = item->get_metadata(0);
  477. if (md.get_type() == Variant::STRING) {
  478. String property_selected = md;
  479. edited->set_disable_class_property(class_name, property_selected, !checked);
  480. _save_and_update();
  481. _update_selected_profile();
  482. } else if (md.get_type() == Variant::INT) {
  483. int feature_selected = md;
  484. switch (feature_selected) {
  485. case CLASS_OPTION_DISABLE_EDITOR: {
  486. edited->set_disable_class_editor(class_name, !checked);
  487. _save_and_update();
  488. _update_selected_profile();
  489. } break;
  490. }
  491. }
  492. }
  493. void EditorFeatureProfileManager::_update_selected_profile() {
  494. String class_selected;
  495. int feature_selected = -1;
  496. if (class_list->get_selected()) {
  497. Variant md = class_list->get_selected()->get_metadata(0);
  498. if (md.get_type() == Variant::STRING) {
  499. class_selected = md;
  500. } else if (md.get_type() == Variant::INT) {
  501. feature_selected = md;
  502. }
  503. }
  504. class_list->clear();
  505. String profile = _get_selected_profile();
  506. if (profile == String()) { //nothing selected, nothing edited
  507. property_list->clear();
  508. edited.unref();
  509. return;
  510. }
  511. if (profile == current_profile) {
  512. edited = current; //reuse current profile (which is what editor uses)
  513. ERR_FAIL_COND(current.is_null()); //nothing selected, current should never be null
  514. } else {
  515. //reload edited, if different from current
  516. edited.instance();
  517. Error err = edited->load_from_file(EditorSettings::get_singleton()->get_feature_profiles_dir().plus_file(profile + ".profile"));
  518. ERR_FAIL_COND(err != OK);
  519. }
  520. updating_features = true;
  521. TreeItem *root = class_list->create_item();
  522. TreeItem *features = class_list->create_item(root);
  523. features->set_text(0, TTR("Enabled Features:"));
  524. for (int i = 0; i < EditorFeatureProfile::FEATURE_MAX; i++) {
  525. TreeItem *feature = class_list->create_item(features);
  526. feature->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  527. feature->set_text(0, TTRGET(EditorFeatureProfile::get_feature_name(EditorFeatureProfile::Feature(i))));
  528. feature->set_selectable(0, true);
  529. feature->set_editable(0, true);
  530. feature->set_metadata(0, i);
  531. if (!edited->is_feature_disabled(EditorFeatureProfile::Feature(i))) {
  532. feature->set_checked(0, true);
  533. }
  534. if (i == feature_selected) {
  535. feature->select(0);
  536. }
  537. }
  538. TreeItem *classes = class_list->create_item(root);
  539. classes->set_text(0, TTR("Enabled Classes:"));
  540. _fill_classes_from(classes, "Node", class_selected);
  541. _fill_classes_from(classes, "Resource", class_selected);
  542. updating_features = false;
  543. _class_list_item_selected();
  544. }
  545. void EditorFeatureProfileManager::_import_profiles(const Vector<String> &p_paths) {
  546. //test it first
  547. for (int i = 0; i < p_paths.size(); i++) {
  548. Ref<EditorFeatureProfile> profile;
  549. profile.instance();
  550. Error err = profile->load_from_file(p_paths[i]);
  551. String basefile = p_paths[i].get_file();
  552. if (err != OK) {
  553. EditorNode::get_singleton()->show_warning(vformat(TTR("File '%s' format is invalid, import aborted."), basefile));
  554. return;
  555. }
  556. String dst_file = EditorSettings::get_singleton()->get_feature_profiles_dir().plus_file(basefile);
  557. if (FileAccess::exists(dst_file)) {
  558. EditorNode::get_singleton()->show_warning(vformat(TTR("Profile '%s' already exists. Remote it first before importing, import aborted."), basefile.get_basename()));
  559. return;
  560. }
  561. }
  562. //do it second
  563. for (int i = 0; i < p_paths.size(); i++) {
  564. Ref<EditorFeatureProfile> profile;
  565. profile.instance();
  566. Error err = profile->load_from_file(p_paths[i]);
  567. ERR_CONTINUE(err != OK);
  568. String basefile = p_paths[i].get_file();
  569. String dst_file = EditorSettings::get_singleton()->get_feature_profiles_dir().plus_file(basefile);
  570. profile->save_to_file(dst_file);
  571. }
  572. _update_profile_list();
  573. }
  574. void EditorFeatureProfileManager::_export_profile(const String &p_path) {
  575. ERR_FAIL_COND(edited.is_null());
  576. Error err = edited->save_to_file(p_path);
  577. if (err != OK) {
  578. EditorNode::get_singleton()->show_warning(vformat(TTR("Error saving profile to path: '%s'."), p_path));
  579. }
  580. }
  581. void EditorFeatureProfileManager::_save_and_update() {
  582. String edited_path = _get_selected_profile();
  583. ERR_FAIL_COND(edited_path == String());
  584. ERR_FAIL_COND(edited.is_null());
  585. edited->save_to_file(EditorSettings::get_singleton()->get_feature_profiles_dir().plus_file(edited_path + ".profile"));
  586. if (edited == current) {
  587. update_timer->start();
  588. }
  589. }
  590. void EditorFeatureProfileManager::_emit_current_profile_changed() {
  591. emit_signal("current_feature_profile_changed");
  592. }
  593. void EditorFeatureProfileManager::notify_changed() {
  594. _emit_current_profile_changed();
  595. }
  596. Ref<EditorFeatureProfile> EditorFeatureProfileManager::get_current_profile() {
  597. return current;
  598. }
  599. EditorFeatureProfileManager *EditorFeatureProfileManager::singleton = NULL;
  600. void EditorFeatureProfileManager::_bind_methods() {
  601. ClassDB::bind_method("_update_selected_profile", &EditorFeatureProfileManager::_update_selected_profile);
  602. ClassDB::bind_method("_profile_action", &EditorFeatureProfileManager::_profile_action);
  603. ClassDB::bind_method("_create_new_profile", &EditorFeatureProfileManager::_create_new_profile);
  604. ClassDB::bind_method("_profile_selected", &EditorFeatureProfileManager::_profile_selected);
  605. ClassDB::bind_method("_erase_selected_profile", &EditorFeatureProfileManager::_erase_selected_profile);
  606. ClassDB::bind_method("_import_profiles", &EditorFeatureProfileManager::_import_profiles);
  607. ClassDB::bind_method("_export_profile", &EditorFeatureProfileManager::_export_profile);
  608. ClassDB::bind_method("_class_list_item_selected", &EditorFeatureProfileManager::_class_list_item_selected);
  609. ClassDB::bind_method("_class_list_item_edited", &EditorFeatureProfileManager::_class_list_item_edited);
  610. ClassDB::bind_method("_property_item_edited", &EditorFeatureProfileManager::_property_item_edited);
  611. ClassDB::bind_method("_emit_current_profile_changed", &EditorFeatureProfileManager::_emit_current_profile_changed);
  612. ADD_SIGNAL(MethodInfo("current_feature_profile_changed"));
  613. }
  614. EditorFeatureProfileManager::EditorFeatureProfileManager() {
  615. VBoxContainer *main_vbc = memnew(VBoxContainer);
  616. add_child(main_vbc);
  617. HBoxContainer *name_hbc = memnew(HBoxContainer);
  618. current_profile_name = memnew(LineEdit);
  619. name_hbc->add_child(current_profile_name);
  620. current_profile_name->set_editable(false);
  621. current_profile_name->set_h_size_flags(SIZE_EXPAND_FILL);
  622. profile_actions[PROFILE_CLEAR] = memnew(Button(TTR("Unset")));
  623. name_hbc->add_child(profile_actions[PROFILE_CLEAR]);
  624. profile_actions[PROFILE_CLEAR]->set_disabled(true);
  625. profile_actions[PROFILE_CLEAR]->connect("pressed", this, "_profile_action", varray(PROFILE_CLEAR));
  626. main_vbc->add_margin_child(TTR("Current Profile"), name_hbc);
  627. HBoxContainer *profiles_hbc = memnew(HBoxContainer);
  628. profile_list = memnew(OptionButton);
  629. profile_list->set_h_size_flags(SIZE_EXPAND_FILL);
  630. profiles_hbc->add_child(profile_list);
  631. profile_list->connect("item_selected", this, "_profile_selected");
  632. profile_actions[PROFILE_SET] = memnew(Button(TTR("Make Current")));
  633. profiles_hbc->add_child(profile_actions[PROFILE_SET]);
  634. profile_actions[PROFILE_SET]->set_disabled(true);
  635. profile_actions[PROFILE_SET]->connect("pressed", this, "_profile_action", varray(PROFILE_SET));
  636. profile_actions[PROFILE_ERASE] = memnew(Button(TTR("Remove")));
  637. profiles_hbc->add_child(profile_actions[PROFILE_ERASE]);
  638. profile_actions[PROFILE_ERASE]->set_disabled(true);
  639. profile_actions[PROFILE_ERASE]->connect("pressed", this, "_profile_action", varray(PROFILE_ERASE));
  640. profiles_hbc->add_child(memnew(VSeparator));
  641. profile_actions[PROFILE_NEW] = memnew(Button(TTR("New")));
  642. profiles_hbc->add_child(profile_actions[PROFILE_NEW]);
  643. profile_actions[PROFILE_NEW]->connect("pressed", this, "_profile_action", varray(PROFILE_NEW));
  644. profiles_hbc->add_child(memnew(VSeparator));
  645. profile_actions[PROFILE_IMPORT] = memnew(Button(TTR("Import")));
  646. profiles_hbc->add_child(profile_actions[PROFILE_IMPORT]);
  647. profile_actions[PROFILE_IMPORT]->connect("pressed", this, "_profile_action", varray(PROFILE_IMPORT));
  648. profile_actions[PROFILE_EXPORT] = memnew(Button(TTR("Export")));
  649. profiles_hbc->add_child(profile_actions[PROFILE_EXPORT]);
  650. profile_actions[PROFILE_EXPORT]->set_disabled(true);
  651. profile_actions[PROFILE_EXPORT]->connect("pressed", this, "_profile_action", varray(PROFILE_EXPORT));
  652. main_vbc->add_margin_child(TTR("Available Profiles"), profiles_hbc);
  653. h_split = memnew(HSplitContainer);
  654. h_split->set_v_size_flags(SIZE_EXPAND_FILL);
  655. main_vbc->add_child(h_split);
  656. VBoxContainer *class_list_vbc = memnew(VBoxContainer);
  657. h_split->add_child(class_list_vbc);
  658. class_list_vbc->set_h_size_flags(SIZE_EXPAND_FILL);
  659. class_list = memnew(Tree);
  660. class_list_vbc->add_margin_child(TTR("Enabled Classes"), class_list, true);
  661. class_list->set_hide_root(true);
  662. class_list->set_hide_folding(true);
  663. class_list->set_edit_checkbox_cell_only_when_checkbox_is_pressed(true);
  664. class_list->connect("cell_selected", this, "_class_list_item_selected");
  665. class_list->connect("item_edited", this, "_class_list_item_edited", varray(), CONNECT_DEFERRED);
  666. VBoxContainer *property_list_vbc = memnew(VBoxContainer);
  667. h_split->add_child(property_list_vbc);
  668. property_list_vbc->set_h_size_flags(SIZE_EXPAND_FILL);
  669. property_list = memnew(Tree);
  670. property_list_vbc->add_margin_child(TTR("Class Options"), property_list, true);
  671. property_list->set_hide_root(true);
  672. property_list->set_hide_folding(true);
  673. property_list->set_edit_checkbox_cell_only_when_checkbox_is_pressed(true);
  674. property_list->connect("item_edited", this, "_property_item_edited", varray(), CONNECT_DEFERRED);
  675. new_profile_dialog = memnew(ConfirmationDialog);
  676. new_profile_dialog->set_title(TTR("New profile name:"));
  677. new_profile_name = memnew(LineEdit);
  678. new_profile_dialog->add_child(new_profile_name);
  679. new_profile_name->set_custom_minimum_size(Size2(300 * EDSCALE, 1));
  680. add_child(new_profile_dialog);
  681. new_profile_dialog->connect("confirmed", this, "_create_new_profile");
  682. new_profile_dialog->register_text_enter(new_profile_name);
  683. new_profile_dialog->get_ok()->set_text(TTR("Create"));
  684. erase_profile_dialog = memnew(ConfirmationDialog);
  685. add_child(erase_profile_dialog);
  686. erase_profile_dialog->set_title(TTR("Erase Profile"));
  687. erase_profile_dialog->connect("confirmed", this, "_erase_selected_profile");
  688. import_profiles = memnew(EditorFileDialog);
  689. add_child(import_profiles);
  690. import_profiles->set_mode(EditorFileDialog::MODE_OPEN_FILES);
  691. import_profiles->add_filter("*.profile; Godot Feature Profile");
  692. import_profiles->connect("files_selected", this, "_import_profiles");
  693. import_profiles->set_title(TTR("Import Profile(s)"));
  694. import_profiles->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  695. export_profile = memnew(EditorFileDialog);
  696. add_child(export_profile);
  697. export_profile->set_mode(EditorFileDialog::MODE_SAVE_FILE);
  698. export_profile->add_filter("*.profile; Godot Feature Profile");
  699. export_profile->connect("file_selected", this, "_export_profile");
  700. export_profile->set_title(TTR("Export Profile"));
  701. export_profile->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  702. set_title(TTR("Manage Editor Feature Profiles"));
  703. EDITOR_DEF("_default_feature_profile", "");
  704. update_timer = memnew(Timer);
  705. update_timer->set_wait_time(1); //wait a second before updating editor
  706. add_child(update_timer);
  707. update_timer->connect("timeout", this, "_emit_current_profile_changed");
  708. update_timer->set_one_shot(true);
  709. updating_features = false;
  710. singleton = this;
  711. }