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.

180 lines
4.7KB

  1. #include <global_pre.hpp>
  2. #include <global_ui.hpp>
  3. #include "SubmarineFree.hpp"
  4. #include "window.hpp"
  5. #include "torpedo.hpp"
  6. namespace rack_plugin_SubmarineFree {
  7. struct TDVText : LedDisplayTextField {
  8. NVGcolor bgColor;
  9. TDVText() {
  10. multiline = false;
  11. color = nvgRGB(0x28, 0xb0, 0xf3);
  12. bgColor = nvgRGBA(0, 0, 0, 0);
  13. }
  14. void draw(NVGcontext *vg) override {
  15. nvgScissor(vg, 0, 0, box.size.x, box.size.y);
  16. nvgBeginPath(vg);
  17. nvgRoundedRect(vg, 0, 0, box.size.x, box.size.y, 2);
  18. nvgFillColor(vg, bgColor);
  19. nvgFill(vg);
  20. nvgTranslate(vg, 24, 0);
  21. nvgRotate(vg, M_PI / 2.0f);
  22. //Text
  23. if (font->handle >= 0) {
  24. bndSetFont(font->handle);
  25. NVGcolor highlightColor = color;
  26. highlightColor.a = 0.5;
  27. int begin = min(cursor, selection);
  28. int end = (this == rack::global_ui->widgets.gFocusedWidget) ? max(cursor, selection) : -1;
  29. bndIconLabelCaret(vg, textOffset.y, textOffset.x,
  30. box.size.y - 2*textOffset.y, box.size.x - 2*textOffset.x,
  31. -1, color, 28, text.c_str(), highlightColor, begin, end);
  32. }
  33. nvgResetScissor(vg);
  34. bndSetFont(rack::global_ui->window.gGuiFont->handle);
  35. }
  36. };
  37. struct TD202 : ModuleWidget {
  38. TDVText *textField;
  39. TD202(Module *module) : ModuleWidget(module) {
  40. setPanel(SVG::load(assetPlugin(plugin, "res/TD-202.svg")));
  41. textField = Widget::create<TDVText>(Vec(2, 15));
  42. textField->box.size = Vec(26, 350);
  43. addChild(textField);
  44. }
  45. json_t *toJson() override {
  46. json_t *rootJ = ModuleWidget::toJson();
  47. json_object_set_new(rootJ, "text", json_string(textField->text.c_str()));
  48. json_object_set_new(rootJ, "fg", json_string(colorToHexString(textField->color).c_str()));
  49. json_object_set_new(rootJ, "bg", json_string(colorToHexString(textField->bgColor).c_str()));
  50. return rootJ;
  51. }
  52. void fromJson(json_t *rootJ) override {
  53. ModuleWidget::fromJson(rootJ);
  54. json_t *textJ = json_object_get(rootJ, "text");
  55. if (textJ)
  56. textField->text = json_string_value(textJ);
  57. json_t *fgJ = json_object_get(rootJ, "fg");
  58. if (fgJ) {
  59. if (json_is_object(fgJ))
  60. textField->color = jsonToColor(fgJ);
  61. else
  62. textField->color = colorFromHexString(json_string_value(fgJ));
  63. }
  64. json_t *bgJ = json_object_get(rootJ, "bg");
  65. if (bgJ) {
  66. if (json_is_object(bgJ))
  67. textField->bgColor = jsonToColor(bgJ);
  68. else
  69. textField->bgColor = colorFromHexString(json_string_value(bgJ));
  70. }
  71. }
  72. void reset() override {
  73. textField->text = "";
  74. textField->multiline = false;
  75. textField->color = nvgRGB(0x28, 0xb0, 0xf3);
  76. textField->bgColor = nvgRGBA(0, 0, 0, 0);
  77. ModuleWidget::reset();
  78. }
  79. void appendContextMenu(Menu *menu) override;
  80. };
  81. struct TD202_MenuItem : MenuItem {
  82. TD202 *widget;
  83. NVGcolor color;
  84. void onAction(EventAction &e) override {
  85. widget->textField->color = color;
  86. }
  87. };
  88. struct TD202_MenuItemB : MenuItem {
  89. TD202 *widget;
  90. NVGcolor color;
  91. void onAction(EventAction &e) override {
  92. widget->textField->bgColor = color;
  93. }
  94. };
  95. void TD202::appendContextMenu(Menu *menu) {
  96. menu->addChild(MenuEntry::create());
  97. TD202_MenuItem *m = MenuItem::create<TD202_MenuItem>("Blue");
  98. m->widget = this;
  99. m->color = nvgRGB(0x28, 0xb0, 0xf3);
  100. menu->addChild(m);
  101. m = MenuItem::create<TD202_MenuItem>("Yellow");
  102. m->widget = this;
  103. m->color = nvgRGB(0xc9, 0xb7, 0x0e);
  104. menu->addChild(m);
  105. m = MenuItem::create<TD202_MenuItem>("Red");
  106. m->widget = this;
  107. m->color = nvgRGB(0xff, 0x13, 0x13);
  108. menu->addChild(m);
  109. m = MenuItem::create<TD202_MenuItem>("Green");
  110. m->widget = this;
  111. m->color = nvgRGB(0x0a, 0xff, 0x13);
  112. menu->addChild(m);
  113. m = MenuItem::create<TD202_MenuItem>("Orange");
  114. m->widget = this;
  115. m->color = nvgRGB(0xff, 0xa5, 0x2d);
  116. menu->addChild(m);
  117. m = MenuItem::create<TD202_MenuItem>("Pink");
  118. m->widget = this;
  119. m->color = nvgRGB(0xff, 0x7d, 0xec);
  120. menu->addChild(m);
  121. m = MenuItem::create<TD202_MenuItem>("White");
  122. m->widget = this;
  123. m->color = nvgRGB(0xff, 0xff, 0xff);
  124. menu->addChild(m);
  125. m = MenuItem::create<TD202_MenuItem>("Black");
  126. m->widget = this;
  127. m->color = nvgRGB(0x00, 0x00, 0x00);
  128. menu->addChild(m);
  129. menu->addChild(MenuEntry::create());
  130. TD202_MenuItemB *b = MenuItem::create<TD202_MenuItemB>("Background - None");
  131. b->widget = this;
  132. b->color = nvgRGBA(0, 0, 0, 0);
  133. menu->addChild(b);
  134. b = MenuItem::create<TD202_MenuItemB>("Background - Black");
  135. b->widget = this;
  136. b->color = nvgRGB(0, 0, 0);
  137. menu->addChild(b);
  138. b = MenuItem::create<TD202_MenuItemB>("Background - White");
  139. b->widget = this;
  140. b->color = nvgRGB(0xff, 0xff, 0xff);
  141. menu->addChild(b);
  142. }
  143. } // namespace rack_plugin_SubmarineFree
  144. using namespace rack_plugin_SubmarineFree;
  145. RACK_PLUGIN_MODEL_INIT(SubmarineFree, TD202) {
  146. Model *modelTD202 = Model::create<Module, TD202>("Submarine (Free)", "TD-202", "TD-202 Vertical Text Display", VISUAL_TAG);
  147. return modelTD202;
  148. }