system-creation.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. SystemCreation::SystemCreation(View* parent) : PanelItem(parent, Size{~0, ~0}) {
  2. setCollapsible().setVisible(false);
  3. header.setText("Create New System").setFont(Font().setBold());
  4. systemList.onActivate([&] { eventAccept(); });
  5. systemList.onChange([&] { eventChange(); });
  6. nameLabel.setText("Name:");
  7. nameValue.onActivate([&] { eventAccept(); });
  8. createButton.setText("Create").onActivate([&] { eventAccept(); });
  9. }
  10. auto SystemCreation::show() -> void {
  11. refresh();
  12. setVisible(true);
  13. }
  14. auto SystemCreation::hide() -> void {
  15. setVisible(false);
  16. }
  17. auto SystemCreation::refresh() -> void {
  18. systemList.reset();
  19. for(auto& interface : interfaces) {
  20. ListViewItem item{&systemList};
  21. item.setAttribute<shared_pointer<higan::Interface>>("interface", interface);
  22. item.setText(interface->name());
  23. }
  24. systemList.doChange();
  25. }
  26. auto SystemCreation::eventChange() -> void {
  27. if(auto item = systemList.selected()) {
  28. if(auto interface = item.attribute<shared_pointer<higan::Interface>>("interface")) {
  29. nameValue.setText(interface->name());
  30. nameLabel.setEnabled(true);
  31. nameValue.setEnabled(true);
  32. createButton.setEnabled(true);
  33. return;
  34. }
  35. }
  36. //nothing selected
  37. nameLabel.setEnabled(false);
  38. nameValue.setEnabled(false).setText("");
  39. createButton.setEnabled(false);
  40. }
  41. auto SystemCreation::eventAccept() -> void {
  42. auto name = nameValue.text().strip();
  43. if(!name) return;
  44. name.append("/");
  45. auto location = Path::data;
  46. if(directory::exists({location, name})) {
  47. if(MessageDialog()
  48. .setTitle("Warning")
  49. .setText("A directory by this name already exists.\n"
  50. "Do you wish to delete the existing directory and create a new one?")
  51. .setAlignment(program).question() == "No"
  52. ) return;
  53. if(!directory::remove({location, name})) return (void)MessageDialog()
  54. .setTitle("Error")
  55. .setText("Failed to remove previous directory. The location may be read-only.")
  56. .setAlignment(program).error();
  57. }
  58. if(!directory::create({location, name})) return (void)MessageDialog()
  59. .setTitle("Error")
  60. .setText("Failed to create directory. Either the location is read-only, or the name contains invalid characters.")
  61. .setAlignment(program).error();
  62. if(auto interface = systemList.selected().attribute<shared_pointer<higan::Interface>>("interface")) {
  63. file::write({location, name, "manifest.bml"}, string{
  64. "system: ", interface->name(), "\n"
  65. });
  66. }
  67. systemManager.refresh();
  68. program.setPanelItem(home);
  69. }