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.

296 lines
7.8KB

  1. #include "BaconPlugs.hpp"
  2. #include <jansson.h>
  3. struct InternalFontMgr
  4. {
  5. static std::map< std::string, int > fontMap;
  6. static int get( NVGcontext *vg, std::string resName )
  7. {
  8. if( fontMap.find( resName ) == fontMap.end() )
  9. {
  10. fontMap[ resName ] = nvgCreateFont( vg, resName.c_str(), assetPlugin( plugin, resName.c_str() ).c_str() );
  11. }
  12. return fontMap[ resName ];
  13. }
  14. };
  15. std::map< std::string, int > InternalFontMgr::fontMap;
  16. struct InternalRoundedBorder : virtual TransparentWidget
  17. {
  18. bool doFill;
  19. NVGcolor fillColor;
  20. InternalRoundedBorder( Vec pos, Vec sz, NVGcolor fc )
  21. {
  22. box.pos = pos;
  23. box.size = sz;
  24. doFill = true;
  25. fillColor = fc;
  26. }
  27. InternalRoundedBorder( Vec pos, Vec sz )
  28. {
  29. box.pos = pos;
  30. box.size = sz;
  31. doFill = false;
  32. }
  33. void draw( NVGcontext *vg ) override
  34. {
  35. nvgBeginPath( vg );
  36. nvgRoundedRect( vg, 0, 0, box.size.x, box.size.y, 5 );
  37. if( doFill )
  38. {
  39. nvgFillColor( vg, fillColor );
  40. nvgFill( vg );
  41. }
  42. nvgStrokeColor( vg, COLOR_BLACK );
  43. nvgStroke( vg );
  44. }
  45. };
  46. struct InternalTextLabel : virtual TransparentWidget
  47. {
  48. int memFont = -1;
  49. std::string label;
  50. int pxSize;
  51. int align;
  52. NVGcolor color;
  53. InternalTextLabel( Vec pos, const char* lab, int px, int al, NVGcolor col ) : label( lab ), pxSize( px ), align( al ), color( col )
  54. {
  55. box.pos = pos;
  56. }
  57. void draw( NVGcontext *vg ) override {
  58. if( memFont < 0 )
  59. memFont = InternalFontMgr::get( vg, "res/Monitorica-Bd.ttf" );
  60. nvgBeginPath( vg );
  61. nvgFontFaceId( vg, memFont );
  62. nvgFontSize( vg, pxSize );
  63. nvgFillColor( vg, color );
  64. nvgTextAlign( vg, align );
  65. nvgText( vg, 0, 0, label.c_str(), NULL );
  66. }
  67. };
  68. struct InternalPlugLabel : virtual TransparentWidget
  69. {
  70. int memFont = -1;
  71. BaconBackground::LabelStyle st;
  72. BaconBackground::LabelAt at;
  73. std::string label;
  74. InternalPlugLabel( Vec portPos, BaconBackground::LabelAt l, BaconBackground::LabelStyle s, const char* ilabel );
  75. void draw( NVGcontext *vg ) override;
  76. };
  77. void BaconBackground::draw( NVGcontext *vg )
  78. {
  79. if( memFont < 0 )
  80. memFont = InternalFontMgr::get( vg, "res/Monitorica-Bd.ttf" );
  81. nvgBeginPath( vg );
  82. nvgRect( vg, 0, 0, box.size.x, box.size.y );
  83. nvgFillColor( vg, BaconBackground::bg );
  84. nvgFill( vg );
  85. nvgBeginPath( vg );
  86. nvgMoveTo( vg, 0, 0 );
  87. nvgLineTo( vg, box.size.x, 0 );
  88. nvgLineTo( vg, box.size.x, box.size.y );
  89. nvgLineTo( vg, 0, box.size.y );
  90. nvgLineTo( vg, 0, 0 );
  91. nvgStrokeColor( vg, BaconBackground::bgOutline );
  92. nvgStroke( vg );
  93. nvgFontFaceId( vg, memFont );
  94. nvgFontSize( vg, 14 );
  95. nvgFillColor( vg, COLOR_BLACK );
  96. nvgStrokeColor( vg, COLOR_BLACK );
  97. nvgTextAlign( vg, NVG_ALIGN_CENTER|NVG_ALIGN_BOTTOM );
  98. nvgText( vg, box.size.x / 2, box.size.y - 5, "Bacon Music", NULL );
  99. nvgFontFaceId( vg, memFont );
  100. nvgFontSize( vg, 16 );
  101. nvgFillColor( vg, COLOR_BLACK );
  102. nvgStrokeColor( vg, COLOR_BLACK );
  103. nvgTextAlign( vg, NVG_ALIGN_CENTER|NVG_ALIGN_TOP );
  104. nvgText( vg, box.size.x / 2, 5, title.c_str(), NULL );
  105. for( auto w : children )
  106. {
  107. nvgTranslate( vg, w->box.pos.x, w->box.pos.y );
  108. w->draw( vg );
  109. nvgTranslate( vg, -w->box.pos.x, -w->box.pos.y );
  110. }
  111. for( auto it = rects.begin(); it != rects.end(); ++it )
  112. {
  113. col_rect_t tu = *it;
  114. Rect r = std::get< 0 >(tu);
  115. NVGcolor c = std::get< 1 >(tu);
  116. bool f = std::get< 2 >(tu);
  117. nvgBeginPath( vg );
  118. nvgRect( vg, r.pos.x, r.pos.y, r.size.x, r.size.y );
  119. if( f )
  120. {
  121. nvgFillColor( vg, c );
  122. nvgFill( vg );
  123. }
  124. else
  125. {
  126. nvgStrokeColor( vg, c );
  127. nvgStroke( vg );
  128. }
  129. }
  130. }
  131. InternalPlugLabel::InternalPlugLabel( Vec portPos, BaconBackground::LabelAt l, BaconBackground::LabelStyle s, const char* ilabel )
  132. :
  133. st( s ), at( l ), label( ilabel )
  134. {
  135. box.size.x = 24 + 5;
  136. box.size.y = 24 + 5 + 20;
  137. // switch on position but for now just do above
  138. box.pos.x = portPos.x - 2.5;
  139. box.pos.y = portPos.y - 2.5 - 17;
  140. }
  141. void InternalPlugLabel::draw( NVGcontext *vg )
  142. {
  143. if( memFont < 0 )
  144. memFont = InternalFontMgr::get( vg, "res/Monitorica-Bd.ttf" );
  145. NVGcolor txtCol = COLOR_BLACK;
  146. switch( st ) {
  147. case( BaconBackground::SIG_IN ) :
  148. {
  149. nvgBeginPath( vg );
  150. nvgRoundedRect( vg, 0, 0, box.size.x, box.size.y, 5 );
  151. nvgStrokeColor( vg, COLOR_BLACK );
  152. nvgStroke( vg );
  153. break;
  154. }
  155. case( BaconBackground::SIG_OUT ) :
  156. {
  157. nvgBeginPath( vg );
  158. nvgRoundedRect( vg, 0, 0, box.size.x, box.size.y, 5 );
  159. nvgFillColor( vg, BaconBackground::highlight );
  160. nvgFill( vg );
  161. nvgStrokeColor( vg, COLOR_BLACK );
  162. nvgStroke( vg );
  163. txtCol = COLOR_WHITE;
  164. break;
  165. }
  166. case( BaconBackground::OTHER ) :
  167. {
  168. nvgBeginPath( vg );
  169. nvgRoundedRect( vg, 0, 0, box.size.x, box.size.y, 5 );
  170. nvgStrokeColor( vg, COLOR_RED );
  171. nvgStroke( vg );
  172. break;
  173. }
  174. }
  175. nvgFontFaceId( vg, memFont );
  176. nvgFontSize( vg, 13 );
  177. nvgFillColor( vg, txtCol );
  178. nvgTextAlign( vg, NVG_ALIGN_CENTER|NVG_ALIGN_TOP );
  179. nvgText( vg, box.size.x / 2, 3, label.c_str(), NULL );
  180. }
  181. BaconBackground *BaconBackground::addLabel( Vec pos, const char* lab, int px, int align, NVGcolor col )
  182. {
  183. addChild( new InternalTextLabel( pos, lab, px, align, col ) );
  184. return this;
  185. }
  186. BaconBackground *BaconBackground::addPlugLabel( Vec plugPos, LabelAt l, LabelStyle s, const char* ilabel )
  187. {
  188. addChild( new InternalPlugLabel( plugPos, l, s, ilabel ) );
  189. return this;
  190. }
  191. BaconBackground *BaconBackground::addRoundedBorder( Vec pos, Vec sz )
  192. {
  193. addChild( new InternalRoundedBorder( pos, sz ) );
  194. return this;
  195. }
  196. BaconBackground *BaconBackground::addRoundedBorder( Vec pos, Vec sz, NVGcolor fill )
  197. {
  198. addChild( new InternalRoundedBorder( pos, sz, fill ) );
  199. return this;
  200. }
  201. NVGcolor BaconBackground::bg = nvgRGBA( 220, 220, 210, 255 );
  202. NVGcolor BaconBackground::bgOutline = nvgRGBA( 180, 180, 170, 255 );
  203. NVGcolor BaconBackground::highlight = nvgRGBA( 90, 90, 60, 255 );
  204. BaconBackground::BaconBackground( Vec size, const char* lab ) : title( lab )
  205. {
  206. box.pos = Vec( 0, 0 );
  207. box.size = size;
  208. }
  209. FramebufferWidget* BaconBackground::wrappedInFramebuffer()
  210. {
  211. FramebufferWidget *fb = new FramebufferWidget();
  212. fb->box = box;
  213. fb->addChild( this );
  214. return fb;
  215. }
  216. BaconBackground *BaconBackground::addLabelsForHugeKnob( Vec topLabelPos,
  217. const char* knobLabel,
  218. const char* zeroLabel,
  219. const char* oneLabel,
  220. Vec &putKnobHere )
  221. {
  222. addLabel( topLabelPos, knobLabel, 14, NVG_ALIGN_CENTER | NVG_ALIGN_MIDDLE );
  223. addLabel( Vec( topLabelPos.x + 10, topLabelPos.y + 72 ),
  224. oneLabel, 13, NVG_ALIGN_LEFT | NVG_ALIGN_TOP );
  225. addLabel( Vec( topLabelPos.x - 10, topLabelPos.y + 72 ),
  226. zeroLabel, 13, NVG_ALIGN_RIGHT | NVG_ALIGN_TOP );
  227. putKnobHere.y = topLabelPos.y + 10;
  228. putKnobHere.x = topLabelPos.x - 28;
  229. return this;
  230. }
  231. BaconBackground *BaconBackground::addLabelsForLargeKnob( Vec topLabelPos,
  232. const char* knobLabel,
  233. const char* zeroLabel,
  234. const char* oneLabel,
  235. Vec &putKnobHere )
  236. {
  237. addLabel( topLabelPos, knobLabel, 14, NVG_ALIGN_CENTER | NVG_ALIGN_MIDDLE );
  238. addLabel( Vec( topLabelPos.x + 10, topLabelPos.y + 48 ),
  239. oneLabel, 13, NVG_ALIGN_LEFT | NVG_ALIGN_TOP );
  240. addLabel( Vec( topLabelPos.x - 10, topLabelPos.y + 48 ),
  241. zeroLabel, 13, NVG_ALIGN_RIGHT | NVG_ALIGN_TOP );
  242. putKnobHere.y = topLabelPos.y + 10;
  243. putKnobHere.x = topLabelPos.x - 18;
  244. return this;
  245. }