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.

72 lines
1.4KB

  1. #include "app/ParamWidget.hpp"
  2. #include "app/Scene.hpp"
  3. #include "context.hpp"
  4. #include "random.hpp"
  5. namespace rack {
  6. ParamWidget::~ParamWidget() {
  7. if (quantity)
  8. delete quantity;
  9. }
  10. void ParamWidget::step() {
  11. if (quantity) {
  12. float value = quantity->getValue();
  13. // Trigger change event when quantity value changes
  14. if (value != dirtyValue) {
  15. dirtyValue = value;
  16. event::Change eChange;
  17. onChange(eChange);
  18. }
  19. }
  20. if (tooltip) {
  21. if (quantity)
  22. tooltip->text = quantity->getString();
  23. tooltip->box.pos = getAbsoluteOffset(box.size);
  24. }
  25. OpaqueWidget::step();
  26. }
  27. void ParamWidget::fromJson(json_t *rootJ) {
  28. json_t *valueJ = json_object_get(rootJ, "value");
  29. if (valueJ) {
  30. if (quantity)
  31. quantity->setValue(json_number_value(valueJ));
  32. }
  33. }
  34. void ParamWidget::onButton(const event::Button &e) {
  35. // Right click to reset
  36. if (e.action == GLFW_PRESS && e.button == GLFW_MOUSE_BUTTON_RIGHT) {
  37. if (quantity)
  38. quantity->reset();
  39. // Here's another way of doing it, but either works.
  40. // dynamic_cast<ParamQuantity*>(quantity)->getParam()->reset();
  41. }
  42. OpaqueWidget::onButton(e);
  43. }
  44. void ParamWidget::onEnter(const event::Enter &e) {
  45. if (tooltip)
  46. return;
  47. tooltip = new Tooltip;
  48. context()->scene->addChild(tooltip);
  49. }
  50. void ParamWidget::onLeave(const event::Leave &e) {
  51. if (!tooltip)
  52. return;
  53. context()->scene->removeChild(tooltip);
  54. delete tooltip;
  55. tooltip = NULL;
  56. }
  57. } // namespace rack