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.

128 lines
2.9KB

  1. #include "app.hpp"
  2. #include "window.hpp"
  3. #include "engine.hpp"
  4. #include "componentlibrary.hpp"
  5. namespace rack {
  6. struct PlugLight : MultiLightWidget {
  7. PlugLight() {
  8. addBaseColor(color::GREEN);
  9. addBaseColor(color::RED);
  10. box.size = math::Vec(8, 8);
  11. bgColor = color::BLACK_TRANSPARENT;
  12. }
  13. };
  14. Port::Port() {
  15. plugLight = new PlugLight();
  16. }
  17. Port::~Port() {
  18. // plugLight is not a child and is thus owned by the Port, so we need to delete it here
  19. delete plugLight;
  20. gRackWidget->wireContainer->removeAllWires(this);
  21. }
  22. void Port::step() {
  23. std::vector<float> values(2);
  24. if (type == INPUT) {
  25. values[0] = module->inputs[portId].plugLights[0].getBrightness();
  26. values[1] = module->inputs[portId].plugLights[1].getBrightness();
  27. }
  28. else {
  29. values[0] = module->outputs[portId].plugLights[0].getBrightness();
  30. values[1] = module->outputs[portId].plugLights[1].getBrightness();
  31. }
  32. plugLight->setValues(values);
  33. }
  34. void Port::draw(NVGcontext *vg) {
  35. WireWidget *activeWire = gRackWidget->wireContainer->activeWire;
  36. if (activeWire) {
  37. // Dim the Port if the active wire cannot plug into this Port
  38. if (type == INPUT ? activeWire->inputPort : activeWire->outputPort)
  39. nvgGlobalAlpha(vg, 0.5);
  40. }
  41. }
  42. void Port::on(event::Button &e) {
  43. if (e.action == GLFW_PRESS && e.button == GLFW_MOUSE_BUTTON_LEFT) {
  44. gRackWidget->wireContainer->removeTopWire(this);
  45. // HACK
  46. // Update hovered*Port of active wire if applicable
  47. event::DragEnter eDragEnter;
  48. on(eDragEnter);
  49. }
  50. e.target = this;
  51. }
  52. void Port::on(event::DragStart &e) {
  53. // Try to grab wire on top of stack
  54. WireWidget *wire = gRackWidget->wireContainer->getTopWire(this);
  55. if (type == OUTPUT && windowIsModPressed()) {
  56. wire = NULL;
  57. }
  58. if (wire) {
  59. // Disconnect existing wire
  60. if (type == INPUT)
  61. wire->inputPort = NULL;
  62. else
  63. wire->outputPort = NULL;
  64. wire->updateWire();
  65. }
  66. else {
  67. // Create a new wire
  68. wire = new WireWidget();
  69. if (type == INPUT)
  70. wire->inputPort = this;
  71. else
  72. wire->outputPort = this;
  73. }
  74. gRackWidget->wireContainer->setActiveWire(wire);
  75. }
  76. void Port::on(event::DragEnd &e) {
  77. // FIXME
  78. // If the source Port is deleted, this will be called, removing the cable
  79. gRackWidget->wireContainer->commitActiveWire();
  80. }
  81. void Port::on(event::DragDrop &e) {
  82. }
  83. void Port::on(event::DragEnter &e) {
  84. // Reject ports if this is an input port and something is already plugged into it
  85. if (type == INPUT) {
  86. WireWidget *topWire = gRackWidget->wireContainer->getTopWire(this);
  87. if (topWire)
  88. return;
  89. }
  90. WireWidget *activeWire = gRackWidget->wireContainer->activeWire;
  91. if (activeWire) {
  92. if (type == INPUT)
  93. activeWire->hoveredInputPort = this;
  94. else
  95. activeWire->hoveredOutputPort = this;
  96. }
  97. }
  98. void Port::on(event::DragLeave &e) {
  99. WireWidget *activeWire = gRackWidget->wireContainer->activeWire;
  100. if (activeWire) {
  101. if (type == INPUT)
  102. activeWire->hoveredInputPort = NULL;
  103. else
  104. activeWire->hoveredOutputPort = NULL;
  105. }
  106. }
  107. } // namespace rack