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.

130 lines
3.1KB

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