You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
2.4KB

  1. #include "app.hpp"
  2. namespace rack {
  3. void WireContainer::setActiveWire(WireWidget *w) {
  4. if (activeWire) {
  5. removeChild(activeWire);
  6. delete activeWire;
  7. activeWire = NULL;
  8. }
  9. if (w) {
  10. if (w->parent == NULL)
  11. addChild(w);
  12. activeWire = w;
  13. }
  14. }
  15. void WireContainer::commitActiveWire() {
  16. if (!activeWire)
  17. return;
  18. if (activeWire->hoveredInputPort) {
  19. activeWire->inputPort = activeWire->hoveredInputPort;
  20. activeWire->hoveredInputPort = NULL;
  21. }
  22. if (activeWire->hoveredOutputPort) {
  23. activeWire->outputPort = activeWire->hoveredOutputPort;
  24. activeWire->hoveredOutputPort = NULL;
  25. }
  26. activeWire->updateWire();
  27. // Did it successfully connect?
  28. if (activeWire->wire) {
  29. // Make it permanent
  30. activeWire = NULL;
  31. }
  32. else {
  33. // Remove it
  34. setActiveWire(NULL);
  35. }
  36. }
  37. void WireContainer::removeTopWire(Port *port) {
  38. WireWidget *wire = getTopWire(port);
  39. if (wire) {
  40. removeChild(wire);
  41. delete wire;
  42. }
  43. }
  44. void WireContainer::removeAllWires(Port *port) {
  45. // As a convenience, de-hover the active wire so we don't attach them once it is dropped.
  46. if (activeWire) {
  47. if (activeWire->hoveredInputPort == port)
  48. activeWire->hoveredInputPort = NULL;
  49. if (activeWire->hoveredOutputPort == port)
  50. activeWire->hoveredOutputPort = NULL;
  51. }
  52. // Build a list of WireWidgets to delete
  53. std::list<WireWidget*> wires;
  54. for (Widget *child : children) {
  55. WireWidget *wire = dynamic_cast<WireWidget*>(child);
  56. assert(wire);
  57. if (wire->inputPort == port || wire->outputPort == port) {
  58. if (activeWire == wire) {
  59. activeWire = NULL;
  60. }
  61. // We can't delete from this list while we're iterating it, so add it to the deletion list.
  62. wires.push_back(wire);
  63. }
  64. }
  65. // Once we're done building the list, actually delete them
  66. for (WireWidget *wire : wires) {
  67. removeChild(wire);
  68. delete wire;
  69. }
  70. }
  71. WireWidget *WireContainer::getTopWire(Port *port) {
  72. for (auto it = children.rbegin(); it != children.rend(); it++) {
  73. WireWidget *wire = dynamic_cast<WireWidget*>(*it);
  74. assert(wire);
  75. // Ignore incomplete wires
  76. if (!(wire->inputPort && wire->outputPort))
  77. continue;
  78. if (wire->inputPort == port || wire->outputPort == port)
  79. return wire;
  80. }
  81. return NULL;
  82. }
  83. void WireContainer::draw(NVGcontext *vg) {
  84. Widget::draw(vg);
  85. // Wire plugs
  86. for (Widget *child : children) {
  87. WireWidget *wire = dynamic_cast<WireWidget*>(child);
  88. assert(wire);
  89. wire->drawPlugs(vg);
  90. }
  91. }
  92. } // namespace rack