port-manager.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. PortManager::PortManager(View* view) : PanelList(view, Size{~0, ~0}) {
  2. setCollapsible().setVisible(false);
  3. listView.onChange([&] { onChange(); });
  4. }
  5. auto PortManager::show() -> void {
  6. root = emulator.root;
  7. listView.selectNone().doChange();
  8. refresh();
  9. setVisible(true);
  10. }
  11. auto PortManager::hide() -> void {
  12. setVisible(false);
  13. root = {};
  14. }
  15. auto PortManager::refresh() -> void {
  16. //save the currently selected node to try and reselect it after rebuilding the tree
  17. higan::Node::Object selected;
  18. if(auto item = listView.selected()) {
  19. selected = item.attribute<higan::Node::Object>("node");
  20. }
  21. listView.reset();
  22. if(root) for(auto& node : *root) refresh(node);
  23. //try and restore the previously selected node
  24. for(auto& item : listView.items()) {
  25. if(item.attribute<higan::Node::Object>("node") == selected) {
  26. item.setSelected().setFocused();
  27. }
  28. }
  29. }
  30. auto PortManager::refresh(higan::Node::Object node) -> void {
  31. if(auto port = node->cast<higan::Node::Port>()) {
  32. ListViewItem item{&listView};
  33. item.setAttribute<higan::Node::Object>("node", port);
  34. item.setText(port->attribute("name") ? port->attribute("name") : port->name());
  35. if(auto peripheral = port->connected()) {
  36. ListViewItem item{&listView};
  37. item.setAttribute<higan::Node::Object>("node", peripheral);
  38. item.setText({" ", peripheral->attribute("name") ? peripheral->attribute("name") : peripheral->name()});
  39. }
  40. }
  41. for(auto& node : *node) refresh(node);
  42. }
  43. auto PortManager::onChange() -> void {
  44. if(auto node = listView.selected().attribute<higan::Node::Object>("node")) {
  45. if(auto port = node->cast<higan::Node::Port>()) {
  46. portConnector.refresh(port);
  47. return program.setPanelItem(portConnector);
  48. }
  49. if(auto setting = node->cast<higan::Node::Setting>()) {
  50. settingEditor.refresh(setting);
  51. return program.setPanelItem(settingEditor);
  52. }
  53. if(auto inputs = node->find<higan::Node::Input>()) {
  54. if(inputs.first()->parent() == node) {
  55. inputMapper.refresh(node);
  56. return program.setPanelItem(inputMapper);
  57. }
  58. }
  59. if(auto peripheral = node->cast<higan::Node::Peripheral>()) {
  60. peripheralOverview.refresh(peripheral);
  61. return program.setPanelItem(peripheralOverview);
  62. }
  63. }
  64. program.setPanelItem(home);
  65. }