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.

162 lines
4.1KB

  1. #include "app/ParamWidget.hpp"
  2. #include "ui/MenuOverlay.hpp"
  3. #include "ui/TextField.hpp"
  4. #include "app/Scene.hpp"
  5. #include "app/ParamQuantity.hpp"
  6. #include "app.hpp"
  7. #include "settings.hpp"
  8. #include "random.hpp"
  9. #include "history.hpp"
  10. namespace rack {
  11. struct ParamField : TextField {
  12. ParamWidget *paramWidget;
  13. void step() override {
  14. // Keep selected
  15. app()->event->setSelected(this);
  16. }
  17. void setParamWidget(ParamWidget *paramWidget) {
  18. this->paramWidget = paramWidget;
  19. if (paramWidget->paramQuantity)
  20. text = paramWidget->paramQuantity->getDisplayValueString();
  21. selectAll();
  22. }
  23. void onSelectKey(const event::SelectKey &e) override {
  24. if (e.action == GLFW_PRESS && (e.key == GLFW_KEY_ENTER || e.key == GLFW_KEY_KP_ENTER)) {
  25. float oldValue = paramWidget->paramQuantity->getValue();
  26. if (paramWidget->paramQuantity)
  27. paramWidget->paramQuantity->setDisplayValueString(text);
  28. float newValue = paramWidget->paramQuantity->getValue();
  29. if (oldValue != newValue) {
  30. // Push ParamChange history action
  31. history::ParamChange *h = new history::ParamChange;
  32. h->moduleId = paramWidget->paramQuantity->module->id;
  33. h->paramId = paramWidget->paramQuantity->paramId;
  34. h->oldValue = oldValue;
  35. h->newValue = newValue;
  36. app()->history->push(h);
  37. }
  38. MenuOverlay *overlay = getAncestorOfType<MenuOverlay>();
  39. overlay->requestedDelete = true;
  40. e.consume(this);
  41. }
  42. if (e.action == GLFW_PRESS && e.key == GLFW_KEY_ESCAPE) {
  43. MenuOverlay *overlay = getAncestorOfType<MenuOverlay>();
  44. overlay->requestedDelete = true;
  45. e.consume(this);
  46. }
  47. if (!e.getConsumed())
  48. TextField::onSelectKey(e);
  49. }
  50. };
  51. ParamWidget::~ParamWidget() {
  52. if (paramQuantity)
  53. delete paramQuantity;
  54. }
  55. void ParamWidget::step() {
  56. if (paramQuantity) {
  57. float value = paramQuantity->getValue();
  58. // Trigger change event when paramQuantity value changes
  59. if (value != dirtyValue) {
  60. dirtyValue = value;
  61. event::Change eChange;
  62. onChange(eChange);
  63. }
  64. }
  65. if (tooltip) {
  66. if (paramQuantity) {
  67. // Quantity string
  68. tooltip->text = paramQuantity->getString();
  69. // Param description
  70. std::string description = paramQuantity->getParamInfo()->description;
  71. if (!description.empty())
  72. tooltip->text += "\n" + description;
  73. }
  74. // Position at bottom-right of parameter
  75. tooltip->box.pos = getAbsoluteOffset(box.size).round();
  76. }
  77. OpaqueWidget::step();
  78. }
  79. void ParamWidget::fromJson(json_t *rootJ) {
  80. json_t *valueJ = json_object_get(rootJ, "value");
  81. if (valueJ) {
  82. if (paramQuantity)
  83. paramQuantity->setValue(json_number_value(valueJ));
  84. }
  85. }
  86. void ParamWidget::onButton(const event::Button &e) {
  87. // Right click to reset
  88. if (e.action == GLFW_PRESS && e.button == GLFW_MOUSE_BUTTON_RIGHT && !(e.mods & WINDOW_MOD) && !(e.mods & GLFW_MOD_SHIFT)) {
  89. if (paramQuantity) {
  90. float oldValue = paramQuantity->getValue();
  91. paramQuantity->reset();
  92. float newValue = paramQuantity->getValue();
  93. if (oldValue != newValue) {
  94. // Push ParamChange history action
  95. history::ParamChange *h = new history::ParamChange;
  96. h->moduleId = paramQuantity->module->id;
  97. h->paramId = paramQuantity->paramId;
  98. h->oldValue = oldValue;
  99. h->newValue = newValue;
  100. app()->history->push(h);
  101. }
  102. }
  103. // Here's another way of doing it, but either works.
  104. // paramQuantity->getParam()->reset();
  105. e.consume(this);
  106. }
  107. // Shift-click to open value entry
  108. if (e.action == GLFW_PRESS && e.button == GLFW_MOUSE_BUTTON_LEFT && !(e.mods & WINDOW_MOD) && (e.mods & GLFW_MOD_SHIFT)) {
  109. // Create ParamField
  110. MenuOverlay *overlay = new MenuOverlay;
  111. app()->scene->addChild(overlay);
  112. ParamField *paramField = new ParamField;
  113. paramField->box.size.x = 100;
  114. paramField->box.pos = getAbsoluteOffset(box.size).round();
  115. paramField->setParamWidget(this);
  116. overlay->addChild(paramField);
  117. e.consume(this);
  118. }
  119. if (!e.getConsumed())
  120. OpaqueWidget::onButton(e);
  121. }
  122. void ParamWidget::onEnter(const event::Enter &e) {
  123. if (settings::paramTooltip && !tooltip) {
  124. tooltip = new Tooltip;
  125. app()->scene->addChild(tooltip);
  126. }
  127. }
  128. void ParamWidget::onLeave(const event::Leave &e) {
  129. if (tooltip) {
  130. app()->scene->removeChild(tooltip);
  131. delete tooltip;
  132. tooltip = NULL;
  133. }
  134. }
  135. } // namespace rack