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.

110 lines
3.1KB

  1. #include "ValleyWidgets.hpp"
  2. #include "global_ui.hpp"
  3. DynamicItem::DynamicItem(unsigned long itemNumber) {
  4. _itemNumber = itemNumber;
  5. _choice = nullptr;
  6. }
  7. void DynamicItem::onAction(EventAction &e) {
  8. if(_choice != nullptr) {
  9. *_choice = _itemNumber;
  10. }
  11. }
  12. DynamicChoice::DynamicChoice() {
  13. _choice = nullptr;
  14. _visibility = nullptr;
  15. _viewMode = ACTIVE_HIGH_VIEW;
  16. _font = Font::load(assetPlugin(plugin, "res/din1451alt.ttf"));
  17. _text = std::make_shared<std::string>("");
  18. _textSize = 14;
  19. }
  20. void DynamicChoice::onAction(EventAction &e) {
  21. #ifdef USE_VST2
  22. Menu* menu = rack::global_ui->ui.gScene->createMenu();
  23. #else
  24. Menu* menu = gScene->createMenu();
  25. #endif // USE_VST2
  26. menu->box.pos = getAbsoluteOffset(Vec(0, box.size.y)).round();
  27. menu->box.size.x = box.size.x;
  28. for(unsigned long i = 0; i < _items.size(); ++i){
  29. DynamicItem *item = new DynamicItem(i);
  30. item->_choice = _choice;
  31. item->_itemNumber = i;
  32. item->text = _items[i];
  33. menu->addChild(item);
  34. }
  35. }
  36. void DynamicChoice::step() {
  37. if(_visibility != nullptr) {
  38. if(*_visibility) {
  39. visible = true;
  40. }
  41. else {
  42. visible = false;
  43. }
  44. if(_viewMode == ACTIVE_LOW_VIEW) {
  45. visible = !visible;
  46. }
  47. }
  48. else {
  49. visible = true;
  50. }
  51. if(_choice != nullptr) {
  52. *_text = _items[*_choice];
  53. }
  54. }
  55. void DynamicChoice::draw(NVGcontext *vg) {
  56. nvgBeginPath(vg);
  57. NVGcolor bgColor = nvgRGB(0x1A, 0x1A, 0x1A);
  58. nvgFillColor(vg, bgColor);
  59. nvgStrokeWidth(vg, 0.f);
  60. nvgRect(vg, 0, 0, this->box.size.x, this->box.size.y - 3);
  61. nvgFill(vg);
  62. nvgClosePath(vg);
  63. nvgBeginPath(vg);
  64. NVGcolor outlineColor = nvgRGB(0xF9, 0xF9, 0xF9);
  65. nvgStrokeColor(vg, outlineColor);
  66. nvgStrokeWidth(vg, 1.f);
  67. nvgMoveTo(vg, 0.f, 0.f);
  68. nvgLineTo(vg, this->box.size.x, 0.f);
  69. nvgLineTo(vg, this->box.size.x, this->box.size.y - 3);
  70. nvgLineTo(vg, 0.f, this->box.size.y -3);
  71. nvgLineTo(vg, 0.f, 0.f);
  72. nvgStroke(vg);
  73. nvgClosePath(vg);
  74. if(_choice != nullptr) {
  75. *_text = _items[*_choice];
  76. }
  77. nvgFontSize(vg, _textSize);
  78. nvgFontFaceId(vg, _font->handle);
  79. nvgTextLetterSpacing(vg, 0.f);
  80. Vec textPos = Vec(this->box.size.x / 2.f, this->box.size.y / 2.f - 2.f);
  81. NVGcolor textColor = nvgRGB(0xFF,0xFF,0xFF);
  82. nvgFillColor(vg, textColor);
  83. nvgTextAlign(vg, NVG_ALIGN_CENTER | NVG_ALIGN_MIDDLE);
  84. nvgText(vg, textPos.x, textPos.y, _text->c_str(), NULL);
  85. }
  86. DynamicChoice* createDynamicChoice(const Vec& pos,
  87. float width,
  88. const std::vector<std::string>& items,
  89. unsigned long* choiceHandle,
  90. int* visibilityHandle,
  91. DynamicViewMode viewMode) {
  92. DynamicChoice* choice = new DynamicChoice;
  93. choice->_choice = choiceHandle;
  94. choice->box.pos = pos;
  95. choice->box.size.x = width;
  96. choice->_items = items;
  97. choice->_visibility = visibilityHandle;
  98. choice->_viewMode = viewMode;
  99. return choice;
  100. }