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.

98 lines
2.7KB

  1. // Adapted from https://github.com/luckyxxl/vcv_luckyxxl
  2. #include "mtsch.hpp"
  3. #include <map>
  4. #include <utility>
  5. namespace rack_plugin_mtsch_plugins {
  6. #include "DigitDisplay.hpp"
  7. DigitDisplay::DigitDisplay(Vec position, float size, char *display)
  8. : position(position), size(size), display(display) {
  9. }
  10. static void drawSegment(NVGcontext *vg, const NVGcolor &color) {
  11. nvgBeginPath(vg);
  12. nvgMoveTo(vg, 0.f, 0.f);
  13. nvgLineTo(vg, .5f, .5f);
  14. nvgLineTo(vg, 1.5f, .5f);
  15. nvgLineTo(vg, 2.f, 0.f);
  16. nvgLineTo(vg, 1.5f, -.5f);
  17. nvgLineTo(vg, .5f, -.5f);
  18. nvgClosePath(vg);
  19. nvgFillColor(vg, color);
  20. nvgFill(vg);
  21. }
  22. #define BITMAP(a, b, c, d, e, f, g) (a << 0 | b << 1 | c << 2 | d << 3 | e << 4 | f << 5 | g << 6)
  23. static const std::map<char, uint8_t> bitmaps = {
  24. std::make_pair('\0', BITMAP(0, 0, 0, 0, 0, 0, 0)),
  25. std::make_pair('0', BITMAP(1, 1, 1, 0, 1, 1, 1)),
  26. std::make_pair('1', BITMAP(0, 0, 1, 0, 0, 1, 0)),
  27. std::make_pair('2', BITMAP(1, 0, 1, 1, 1, 0, 1)),
  28. std::make_pair('3', BITMAP(1, 0, 1, 1, 0, 1, 1)),
  29. std::make_pair('4', BITMAP(0, 1, 1, 1, 0, 1, 0)),
  30. std::make_pair('5', BITMAP(1, 1, 0, 1, 0, 1, 1)),
  31. std::make_pair('6', BITMAP(1, 1, 0, 1, 1, 1, 1)),
  32. std::make_pair('7', BITMAP(1, 0, 1, 0, 0, 1, 0)),
  33. std::make_pair('8', BITMAP(1, 1, 1, 1, 1, 1, 1)),
  34. std::make_pair('9', BITMAP(1, 1, 1, 1, 0, 1, 1)),
  35. std::make_pair('#', BITMAP(0, 1, 1, 1, 1, 1, 0)),
  36. std::make_pair('a', BITMAP(1, 1, 1, 1, 1, 1, 0)),
  37. std::make_pair('b', BITMAP(0, 1, 0, 1, 1, 1, 1)),
  38. std::make_pair('c', BITMAP(1, 1, 0, 0, 1, 0, 1)),
  39. std::make_pair('d', BITMAP(0, 0, 1, 1, 1, 1, 1)),
  40. std::make_pair('e', BITMAP(1, 1, 0, 1, 1, 0, 1)),
  41. std::make_pair('f', BITMAP(1, 1, 0, 1, 1, 0, 0)),
  42. std::make_pair('g', BITMAP(1, 1, 1, 1, 0, 1, 1)),
  43. };
  44. #undef BITMAP
  45. void DigitDisplay::draw(NVGcontext *vg) {
  46. nvgSave(vg);
  47. nvgTranslate(vg, position.x, position.y);
  48. nvgScale(vg, size, size);
  49. static const NVGcolor off_color = nvgRGB(200, 200, 200);
  50. static const NVGcolor on_color = nvgRGB(30, 30, 30);
  51. uint8_t bitmap = 0u;
  52. {
  53. auto b = bitmaps.find(*display);
  54. if(b != bitmaps.end()) bitmap = b->second;
  55. }
  56. #define BIT(x) (bitmap & 1 << x) ? on_color : off_color
  57. drawSegment(vg, BIT(0));
  58. nvgTranslate(vg, 0.f, 2.f);
  59. drawSegment(vg, BIT(3));
  60. nvgTranslate(vg, 0.f, 2.f);
  61. drawSegment(vg, BIT(6));
  62. nvgTranslate(vg, 0.f, -4.f);
  63. nvgRotate(vg, NVG_PI/2.f);
  64. drawSegment(vg, BIT(1));
  65. nvgTranslate(vg, 0.f, -2.f);
  66. drawSegment(vg, BIT(2));
  67. nvgTranslate(vg, 2.f, 2.f);
  68. drawSegment(vg, BIT(4));
  69. nvgTranslate(vg, 0.f, -2.f);
  70. drawSegment(vg, BIT(5));
  71. #undef BIT
  72. nvgRestore(vg);
  73. }
  74. } // namespace rack_plugin_mtsch_plugins