Icons.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. #include "ui/Icons.h"
  2. #include <cmath>
  3. #include <map>
  4. #include "ui/Theme.h"
  5. namespace lynx
  6. {
  7. namespace icons
  8. {
  9. namespace
  10. {
  11. constexpr float pi = juce::MathConstants<float>::pi;
  12. void line (juce::Path& p, float x1, float y1, float x2, float y2)
  13. {
  14. p.startNewSubPath (x1, y1);
  15. p.lineTo (x2, y2);
  16. }
  17. void roundRect (juce::Path& p, float x, float y, float w, float h, float r)
  18. {
  19. p.addRoundedRectangle (x, y, w, h, r);
  20. }
  21. void polyline (juce::Path& p, std::initializer_list<juce::Point<float>> points, bool close = false)
  22. {
  23. bool first = true;
  24. for (auto pt : points)
  25. {
  26. if (first)
  27. p.startNewSubPath (pt);
  28. else
  29. p.lineTo (pt);
  30. first = false;
  31. }
  32. if (close)
  33. p.closeSubPath();
  34. }
  35. Shape make (Icon icon)
  36. {
  37. Shape s;
  38. auto& st = s.stroke;
  39. auto& fi = s.fill;
  40. // a page with a folded corner (file icons)
  41. auto page = [&st] {
  42. juce::Path p;
  43. polyline (p, { { 6.0f, 3.5f }, { 13.5f, 3.5f }, { 18.0f, 8.0f }, { 18.0f, 20.5f }, { 6.0f, 20.5f } }, true);
  44. st.addPath (p.createPathWithRoundedCorners (1.5f));
  45. polyline (st, { { 13.5f, 3.5f }, { 13.5f, 8.0f }, { 18.0f, 8.0f } });
  46. };
  47. // a speaker cone (mute and playback tools)
  48. auto speaker = [&st] {
  49. juce::Path p;
  50. polyline (p, { { 3.5f, 9.5f }, { 7.0f, 9.5f }, { 11.5f, 5.5f }, { 11.5f, 18.5f }, { 7.0f, 14.5f }, { 3.5f, 14.5f } }, true);
  51. st.addPath (p.createPathWithRoundedCorners (1.2f));
  52. };
  53. // a window outline with a title band (view icons)
  54. auto window = [&st] {
  55. roundRect (st, 3.5f, 4.5f, 17.0f, 15.0f, 2.0f);
  56. };
  57. switch (icon)
  58. {
  59. case Icon::none:
  60. break;
  61. // --- transport (solid) ---
  62. case Icon::play:
  63. {
  64. juce::Path tri;
  65. tri.addTriangle (7.0f, 4.5f, 7.0f, 19.5f, 19.5f, 12.0f);
  66. fi = tri.createPathWithRoundedCorners (2.0f);
  67. break;
  68. }
  69. case Icon::pause:
  70. fi.addRoundedRectangle (6.0f, 5.0f, 4.5f, 14.0f, 1.25f);
  71. fi.addRoundedRectangle (13.5f, 5.0f, 4.5f, 14.0f, 1.25f);
  72. break;
  73. case Icon::stop:
  74. fi.addRoundedRectangle (5.5f, 5.5f, 13.0f, 13.0f, 2.25f);
  75. break;
  76. case Icon::record:
  77. fi.addEllipse (5.0f, 5.0f, 14.0f, 14.0f);
  78. break;
  79. // --- small glyphs ---
  80. case Icon::chevronDown:
  81. polyline (st, { { 7.0f, 9.5f }, { 12.0f, 14.5f }, { 17.0f, 9.5f } });
  82. break;
  83. case Icon::more:
  84. for (float x : { 5.5f, 12.0f, 18.5f })
  85. fi.addEllipse (x - 1.75f, 10.25f, 3.5f, 3.5f);
  86. break;
  87. case Icon::plus:
  88. line (st, 12.0f, 5.0f, 12.0f, 19.0f);
  89. line (st, 5.0f, 12.0f, 19.0f, 12.0f);
  90. break;
  91. case Icon::check:
  92. polyline (st, { { 5.0f, 12.5f }, { 9.5f, 17.0f }, { 19.0f, 7.0f } });
  93. break;
  94. case Icon::close:
  95. line (st, 6.5f, 6.5f, 17.5f, 17.5f);
  96. line (st, 17.5f, 6.5f, 6.5f, 17.5f);
  97. break;
  98. case Icon::minimise:
  99. line (st, 6.0f, 12.0f, 18.0f, 12.0f);
  100. break;
  101. case Icon::maximise:
  102. roundRect (st, 5.5f, 5.5f, 13.0f, 13.0f, 2.0f);
  103. line (st, 5.5f, 9.0f, 18.5f, 9.0f);
  104. break;
  105. case Icon::restore: // two stacked windows
  106. roundRect (st, 4.5f, 8.5f, 11.0f, 11.0f, 1.75f);
  107. polyline (st, { { 8.5f, 4.5f }, { 17.5f, 4.5f }, { 19.5f, 6.5f }, { 19.5f, 15.5f } });
  108. break;
  109. case Icon::panelMenu: // three bars of decreasing length: "this window's options"
  110. line (st, 5.0f, 7.0f, 19.0f, 7.0f);
  111. line (st, 5.0f, 12.0f, 16.0f, 12.0f);
  112. line (st, 5.0f, 17.0f, 13.0f, 17.0f);
  113. break;
  114. // --- modes ---
  115. case Icon::pattern: // a 2x2 block of steps, one lit
  116. roundRect (st, 4.0f, 4.0f, 6.5f, 6.5f, 1.5f);
  117. roundRect (st, 4.0f, 13.5f, 6.5f, 6.5f, 1.5f);
  118. roundRect (st, 13.5f, 13.5f, 6.5f, 6.5f, 1.5f);
  119. fi.addRoundedRectangle (12.75f, 3.25f, 8.0f, 8.0f, 2.0f);
  120. break;
  121. case Icon::song: // clips staggered along three lanes
  122. fi.addRoundedRectangle (3.5f, 4.5f, 9.0f, 4.0f, 1.25f);
  123. fi.addRoundedRectangle (9.0f, 10.0f, 11.5f, 4.0f, 1.25f);
  124. fi.addRoundedRectangle (5.5f, 15.5f, 8.0f, 4.0f, 1.25f);
  125. break;
  126. case Icon::metronome:
  127. {
  128. juce::Path body;
  129. polyline (body, { { 9.0f, 3.5f }, { 15.0f, 3.5f }, { 19.0f, 20.5f }, { 5.0f, 20.5f } }, true);
  130. st.addPath (body.createPathWithRoundedCorners (2.0f));
  131. line (st, 12.0f, 16.5f, 17.5f, 6.0f);
  132. line (st, 7.0f, 16.5f, 17.0f, 16.5f);
  133. fi.addEllipse (14.0f, 8.0f, 3.5f, 3.5f);
  134. break;
  135. }
  136. // --- file ---
  137. case Icon::newFile:
  138. page();
  139. line (st, 12.0f, 11.0f, 12.0f, 17.0f);
  140. line (st, 9.0f, 14.0f, 15.0f, 14.0f);
  141. break;
  142. case Icon::file:
  143. page();
  144. break;
  145. case Icon::folder:
  146. {
  147. juce::Path f;
  148. polyline (f, { { 3.5f, 5.5f }, { 9.0f, 5.5f }, { 11.0f, 7.5f }, { 20.5f, 7.5f }, { 20.5f, 18.5f }, { 3.5f, 18.5f } }, true);
  149. st.addPath (f.createPathWithRoundedCorners (1.75f));
  150. break;
  151. }
  152. case Icon::open: // a folder with its front flap tipped open
  153. {
  154. juce::Path back;
  155. polyline (back, { { 4.0f, 18.5f }, { 3.5f, 5.5f }, { 9.0f, 5.5f }, { 11.0f, 7.5f }, { 18.0f, 7.5f }, { 18.0f, 10.5f } });
  156. st.addPath (back.createPathWithRoundedCorners (1.5f));
  157. juce::Path flap;
  158. polyline (flap, { { 4.0f, 18.5f }, { 7.0f, 10.5f }, { 21.0f, 10.5f }, { 18.0f, 18.5f } }, true);
  159. st.addPath (flap.createPathWithRoundedCorners (1.5f));
  160. break;
  161. }
  162. case Icon::save: // a disk
  163. {
  164. juce::Path body;
  165. polyline (body, { { 4.0f, 4.0f }, { 16.5f, 4.0f }, { 20.0f, 7.5f }, { 20.0f, 20.0f }, { 4.0f, 20.0f } }, true);
  166. st.addPath (body.createPathWithRoundedCorners (2.0f));
  167. polyline (st, { { 8.0f, 4.0f }, { 8.0f, 8.5f }, { 15.0f, 8.5f }, { 15.0f, 4.0f } });
  168. roundRect (st, 7.5f, 13.0f, 9.0f, 7.0f, 1.0f);
  169. break;
  170. }
  171. case Icon::exportAudio: // a waveform leaving a tray
  172. polyline (st, { { 3.5f, 14.0f }, { 3.5f, 19.5f }, { 20.5f, 19.5f }, { 20.5f, 14.0f } });
  173. line (st, 12.0f, 15.5f, 12.0f, 4.0f);
  174. polyline (st, { { 8.0f, 8.0f }, { 12.0f, 4.0f }, { 16.0f, 8.0f } });
  175. break;
  176. case Icon::settings: // three sliders
  177. line (st, 4.0f, 7.0f, 20.0f, 7.0f);
  178. line (st, 4.0f, 12.0f, 20.0f, 12.0f);
  179. line (st, 4.0f, 17.0f, 20.0f, 17.0f);
  180. fi.addEllipse (6.5f, 4.5f, 5.0f, 5.0f);
  181. fi.addEllipse (13.0f, 9.5f, 5.0f, 5.0f);
  182. fi.addEllipse (8.5f, 14.5f, 5.0f, 5.0f);
  183. break;
  184. case Icon::help:
  185. st.addEllipse (3.5f, 3.5f, 17.0f, 17.0f);
  186. st.startNewSubPath (9.5f, 10.0f);
  187. st.addCentredArc (12.0f, 10.0f, 2.5f, 2.5f, 0.0f, -pi * 0.5f, pi * 0.7f, false);
  188. st.lineTo (12.0f, 13.5f);
  189. fi.addEllipse (10.9f, 15.6f, 2.2f, 2.2f);
  190. break;
  191. case Icon::undo:
  192. case Icon::redo:
  193. {
  194. juce::Path p;
  195. p.startNewSubPath (19.0f, 18.5f);
  196. p.cubicTo (19.0f, 12.0f, 15.0f, 9.0f, 5.5f, 9.0f);
  197. polyline (p, { { 9.5f, 5.0f }, { 5.5f, 9.0f }, { 9.5f, 13.0f } });
  198. if (icon == Icon::redo)
  199. p.applyTransform (juce::AffineTransform::scale (-1.0f, 1.0f, 12.0f, 12.0f));
  200. st.addPath (p);
  201. break;
  202. }
  203. // --- views (the window buttons) ---
  204. case Icon::playlist: // clips on lanes inside a window
  205. window();
  206. fi.addRoundedRectangle (6.0f, 7.5f, 6.5f, 2.5f, 0.75f);
  207. fi.addRoundedRectangle (10.5f, 11.0f, 7.5f, 2.5f, 0.75f);
  208. fi.addRoundedRectangle (6.0f, 14.5f, 4.5f, 2.5f, 0.75f);
  209. fi.addRoundedRectangle (12.5f, 14.5f, 5.5f, 2.5f, 0.75f);
  210. break;
  211. case Icon::channelRack: // three channels: a name pad and a row of steps each
  212. for (int r = 0; r < 3; ++r)
  213. {
  214. const auto y = 4.5f + (float) r * 5.75f;
  215. roundRect (st, 3.5f, y, 6.5f, 3.5f, 1.0f);
  216. for (int c = 0; c < 3; ++c)
  217. {
  218. const auto x = 12.5f + (float) c * 3.0f;
  219. if ((r + c) % 2 == 0)
  220. fi.addRoundedRectangle (x - 0.25f, y - 0.25f, 2.5f, 4.0f, 0.6f);
  221. else
  222. fi.addRoundedRectangle (x + 0.5f, y + 1.25f, 1.0f, 1.0f, 0.3f);
  223. }
  224. }
  225. break;
  226. case Icon::pianoRoll: // a keyboard edge and notes
  227. window();
  228. line (st, 8.0f, 4.5f, 8.0f, 19.5f);
  229. line (st, 3.5f, 9.5f, 6.0f, 9.5f);
  230. line (st, 3.5f, 14.5f, 6.0f, 14.5f);
  231. fi.addRoundedRectangle (10.0f, 7.0f, 4.5f, 2.5f, 0.75f);
  232. fi.addRoundedRectangle (13.0f, 10.75f, 5.5f, 2.5f, 0.75f);
  233. fi.addRoundedRectangle (10.5f, 14.5f, 4.0f, 2.5f, 0.75f);
  234. break;
  235. case Icon::mixer: // three faders
  236. for (int i = 0; i < 3; ++i)
  237. line (st, 6.0f + (float) i * 6.0f, 4.0f, 6.0f + (float) i * 6.0f, 20.0f);
  238. fi.addRoundedRectangle (3.5f, 12.0f, 5.0f, 3.5f, 1.0f);
  239. fi.addRoundedRectangle (9.5f, 6.5f, 5.0f, 3.5f, 1.0f);
  240. fi.addRoundedRectangle (15.5f, 14.5f, 5.0f, 3.5f, 1.0f);
  241. break;
  242. case Icon::browser: // a tree: a folder and two items under it
  243. roundRect (st, 3.5f, 4.0f, 6.0f, 5.0f, 1.25f);
  244. polyline (st, { { 6.5f, 9.0f }, { 6.5f, 17.5f }, { 10.0f, 17.5f } });
  245. line (st, 6.5f, 12.5f, 10.0f, 12.5f);
  246. line (st, 12.5f, 6.5f, 20.5f, 6.5f);
  247. line (st, 13.0f, 12.5f, 20.5f, 12.5f);
  248. line (st, 13.0f, 17.5f, 18.0f, 17.5f);
  249. break;
  250. case Icon::pluginPicker: // a plug
  251. {
  252. line (st, 9.0f, 3.5f, 9.0f, 7.5f);
  253. line (st, 15.0f, 3.5f, 15.0f, 7.5f);
  254. juce::Path body;
  255. polyline (body, { { 5.5f, 7.5f }, { 18.5f, 7.5f }, { 18.5f, 11.0f }, { 14.5f, 16.0f }, { 9.5f, 16.0f }, { 5.5f, 11.0f } }, true);
  256. st.addPath (body.createPathWithRoundedCorners (1.75f));
  257. line (st, 12.0f, 16.0f, 12.0f, 20.5f);
  258. break;
  259. }
  260. // --- content ---
  261. case Icon::audioFile: // a short waveform
  262. {
  263. const float heights[] = { 5.0f, 11.0f, 16.0f, 8.0f, 12.0f, 4.0f };
  264. for (int i = 0; i < 6; ++i)
  265. {
  266. const auto x = 4.5f + (float) i * 3.0f;
  267. line (st, x, 12.0f - heights[i] / 2.0f, x, 12.0f + heights[i] / 2.0f);
  268. }
  269. break;
  270. }
  271. case Icon::plugin: // a module with a jack
  272. roundRect (st, 4.0f, 6.5f, 16.0f, 12.0f, 2.0f);
  273. line (st, 8.5f, 3.5f, 8.5f, 6.5f);
  274. line (st, 15.5f, 3.5f, 15.5f, 6.5f);
  275. st.addEllipse (9.5f, 10.0f, 5.0f, 5.0f);
  276. break;
  277. case Icon::preset: // a knob
  278. st.addCentredArc (12.0f, 12.0f, 8.0f, 8.0f, 0.0f, -pi * 0.75f, pi * 0.75f, true);
  279. line (st, 12.0f, 12.0f, 15.5f, 8.5f);
  280. break;
  281. case Icon::project: // a diamond with a core
  282. {
  283. juce::Path d;
  284. polyline (d, { { 12.0f, 3.0f }, { 20.0f, 12.0f }, { 12.0f, 21.0f }, { 4.0f, 12.0f } }, true);
  285. st.addPath (d.createPathWithRoundedCorners (2.0f));
  286. fi.addEllipse (9.75f, 9.75f, 4.5f, 4.5f);
  287. break;
  288. }
  289. // --- edit tools ---
  290. case Icon::draw: // pencil
  291. {
  292. juce::Path p;
  293. polyline (p, { { 15.5f, 4.0f }, { 20.0f, 8.5f }, { 9.0f, 19.5f }, { 4.0f, 20.0f }, { 4.5f, 15.0f } }, true);
  294. st.addPath (p.createPathWithRoundedCorners (1.25f));
  295. line (st, 13.0f, 6.5f, 17.5f, 11.0f);
  296. break;
  297. }
  298. case Icon::brush: // a round brush: handle, ferrule and a filled tip
  299. {
  300. line (st, 20.0f, 4.0f, 13.5f, 10.5f);
  301. juce::Path ferrule;
  302. polyline (ferrule, { { 11.5f, 9.5f }, { 14.5f, 12.5f }, { 12.5f, 14.5f }, { 9.5f, 11.5f } }, true);
  303. st.addPath (ferrule.createPathWithRoundedCorners (0.75f));
  304. juce::Path tip;
  305. tip.startNewSubPath (9.0f, 12.5f);
  306. tip.quadraticTo (4.0f, 13.0f, 3.5f, 20.5f);
  307. tip.quadraticTo (11.0f, 20.0f, 11.5f, 15.0f);
  308. tip.closeSubPath();
  309. fi.addPath (tip);
  310. break;
  311. }
  312. case Icon::erase: // an eraser on a line
  313. {
  314. juce::Path p;
  315. polyline (p, { { 13.0f, 4.0f }, { 20.0f, 11.0f }, { 12.0f, 19.0f }, { 7.5f, 19.0f }, { 4.0f, 15.5f } }, true);
  316. st.addPath (p.createPathWithRoundedCorners (1.25f));
  317. line (st, 8.5f, 8.5f, 15.5f, 15.5f);
  318. line (st, 12.0f, 19.0f, 20.5f, 19.0f);
  319. break;
  320. }
  321. case Icon::mute: // a speaker with a cross
  322. speaker();
  323. line (st, 15.0f, 9.0f, 20.5f, 15.0f);
  324. line (st, 20.5f, 9.0f, 15.0f, 15.0f);
  325. break;
  326. case Icon::listen: // a speaker with two waves (playback tool)
  327. speaker();
  328. st.addCentredArc (12.0f, 12.0f, 3.5f, 3.5f, 0.0f, pi * 0.25f, pi * 0.75f, true);
  329. st.addCentredArc (12.0f, 12.0f, 7.5f, 7.5f, 0.0f, pi * 0.22f, pi * 0.78f, true);
  330. break;
  331. case Icon::knife: // a blade with a handle, cutting down to the left
  332. {
  333. juce::Path blade;
  334. polyline (blade, { { 4.0f, 20.0f }, { 13.5f, 8.0f }, { 17.0f, 11.5f } }, true);
  335. st.addPath (blade.createPathWithRoundedCorners (1.25f));
  336. line (st, 15.5f, 9.5f, 20.0f, 5.0f);
  337. break;
  338. }
  339. case Icon::select: // the four corners of a selection box
  340. {
  341. const float a = 4.0f, b = 20.0f, l = 4.5f;
  342. polyline (st, { { a, a + l }, { a, a }, { a + l, a } });
  343. polyline (st, { { b - l, a }, { b, a }, { b, a + l } });
  344. polyline (st, { { b, b - l }, { b, b }, { b - l, b } });
  345. polyline (st, { { a + l, b }, { a, b }, { a, b - l } });
  346. fi.addRoundedRectangle (9.5f, 9.5f, 5.0f, 5.0f, 1.0f);
  347. break;
  348. }
  349. case Icon::slice:
  350. case Icon::scissors:
  351. st.addEllipse (3.5f, 14.0f, 6.0f, 6.0f);
  352. st.addEllipse (14.5f, 14.0f, 6.0f, 6.0f);
  353. line (st, 8.5f, 15.0f, 17.5f, 3.5f);
  354. line (st, 15.5f, 15.0f, 6.5f, 3.5f);
  355. break;
  356. case Icon::zoom: // a magnifier with a plus
  357. st.addEllipse (3.5f, 3.5f, 13.0f, 13.0f);
  358. line (st, 14.5f, 14.5f, 20.5f, 20.5f);
  359. line (st, 10.0f, 7.0f, 10.0f, 13.0f);
  360. line (st, 7.0f, 10.0f, 13.0f, 10.0f);
  361. break;
  362. case Icon::slip: // a fixed frame with its content sliding inside
  363. roundRect (st, 3.5f, 5.5f, 17.0f, 13.0f, 2.0f);
  364. polyline (st, { { 9.0f, 9.5f }, { 6.5f, 12.0f }, { 9.0f, 14.5f } });
  365. polyline (st, { { 15.0f, 9.5f }, { 17.5f, 12.0f }, { 15.0f, 14.5f } });
  366. line (st, 7.0f, 12.0f, 17.0f, 12.0f);
  367. break;
  368. case Icon::ghost: // two overlapping notes, the back one hollow
  369. roundRect (st, 3.5f, 5.5f, 12.0f, 5.5f, 1.5f);
  370. fi.addRoundedRectangle (8.5f, 13.0f, 12.0f, 5.5f, 1.5f);
  371. break;
  372. case Icon::link:
  373. roundRect (st, 3.0f, 8.5f, 11.0f, 7.0f, 3.5f);
  374. roundRect (st, 10.0f, 8.5f, 11.0f, 7.0f, 3.5f);
  375. break;
  376. case Icon::magnet: // snap
  377. {
  378. juce::Path m;
  379. m.startNewSubPath (5.5f, 4.0f);
  380. m.lineTo (5.5f, 12.0f);
  381. m.addCentredArc (12.0f, 12.0f, 6.5f, 6.5f, 0.0f, pi * 1.5f, pi * 0.5f, false);
  382. m.lineTo (18.5f, 4.0f);
  383. st.addPath (m);
  384. line (st, 3.5f, 8.0f, 7.5f, 8.0f);
  385. line (st, 16.5f, 8.0f, 20.5f, 8.0f);
  386. break;
  387. }
  388. // --- workspace and control contract ---
  389. case Icon::detach: // a window with an arrow leaving its corner
  390. polyline (st, { { 10.5f, 4.5f }, { 6.0f, 4.5f }, { 4.5f, 6.0f }, { 4.5f, 18.0f }, { 6.0f, 19.5f }, { 18.0f, 19.5f }, { 19.5f, 18.0f }, { 19.5f, 13.5f } });
  391. line (st, 12.0f, 12.0f, 19.5f, 4.5f);
  392. polyline (st, { { 14.0f, 4.5f }, { 19.5f, 4.5f }, { 19.5f, 10.0f } });
  393. break;
  394. case Icon::dock: // an arrow going into a window's corner
  395. polyline (st, { { 10.5f, 4.5f }, { 6.0f, 4.5f }, { 4.5f, 6.0f }, { 4.5f, 18.0f }, { 6.0f, 19.5f }, { 18.0f, 19.5f }, { 19.5f, 18.0f }, { 19.5f, 13.5f } });
  396. line (st, 19.5f, 4.5f, 12.0f, 12.0f);
  397. polyline (st, { { 12.0f, 6.5f }, { 12.0f, 12.0f }, { 17.5f, 12.0f } });
  398. break;
  399. case Icon::keyboard: // a small piano: outline, white-key gaps, two black keys
  400. roundRect (st, 3.0f, 6.0f, 18.0f, 12.0f, 2.0f);
  401. line (st, 9.0f, 13.0f, 9.0f, 18.0f);
  402. line (st, 15.0f, 13.0f, 15.0f, 18.0f);
  403. fi.addRoundedRectangle (7.25f, 6.0f, 3.5f, 7.0f, 0.75f);
  404. fi.addRoundedRectangle (13.25f, 6.0f, 3.5f, 7.0f, 0.75f);
  405. break;
  406. case Icon::automate: // a curve through two points
  407. {
  408. juce::Path c;
  409. c.startNewSubPath (4.0f, 17.0f);
  410. c.cubicTo (9.0f, 17.0f, 10.0f, 7.0f, 15.0f, 7.0f);
  411. c.lineTo (20.5f, 7.0f);
  412. st.addPath (c);
  413. fi.addEllipse (2.0f, 15.0f, 4.0f, 4.0f);
  414. fi.addEllipse (13.0f, 5.0f, 4.0f, 4.0f);
  415. break;
  416. }
  417. case Icon::rightClick: // a mouse with the right button lit
  418. {
  419. roundRect (st, 6.5f, 3.5f, 11.0f, 17.0f, 5.5f);
  420. line (st, 6.5f, 10.5f, 17.5f, 10.5f);
  421. line (st, 12.0f, 3.5f, 12.0f, 10.5f);
  422. juce::Path b;
  423. b.startNewSubPath (12.0f, 3.5f);
  424. b.lineTo (12.0f, 10.5f);
  425. b.lineTo (17.5f, 10.5f);
  426. b.lineTo (17.5f, 9.0f);
  427. b.quadraticTo (17.5f, 3.5f, 12.0f, 3.5f);
  428. b.closeSubPath();
  429. fi.addPath (b);
  430. break;
  431. }
  432. case Icon::tap: // a dot with two rings
  433. fi.addEllipse (9.5f, 9.5f, 5.0f, 5.0f);
  434. st.addCentredArc (12.0f, 12.0f, 5.5f, 5.5f, 0.0f, -pi * 0.35f, pi * 0.35f, true);
  435. st.addCentredArc (12.0f, 12.0f, 8.5f, 8.5f, 0.0f, -pi * 0.3f, pi * 0.3f, true);
  436. break;
  437. case Icon::mic: // recording options: a microphone
  438. roundRect (st, 9.0f, 3.0f, 6.0f, 11.0f, 3.0f);
  439. st.startNewSubPath (18.5f, 11.0f);
  440. st.addCentredArc (12.0f, 11.0f, 6.5f, 6.0f, 0.0f, pi * 0.5f, pi * 1.5f, false);
  441. line (st, 12.0f, 17.0f, 12.0f, 20.5f);
  442. line (st, 8.5f, 20.5f, 15.5f, 20.5f);
  443. break;
  444. case Icon::countIn: // a stopwatch: dial, crown and a hand, with the first quarter filled
  445. {
  446. st.addEllipse (4.5f, 6.0f, 15.0f, 15.0f);
  447. line (st, 10.0f, 3.0f, 14.0f, 3.0f);
  448. line (st, 12.0f, 3.0f, 12.0f, 6.0f);
  449. line (st, 12.0f, 13.5f, 12.0f, 9.5f);
  450. juce::Path quarter;
  451. quarter.startNewSubPath (12.0f, 13.5f);
  452. quarter.addCentredArc (12.0f, 13.5f, 5.0f, 5.0f, 0.0f, 0.0f, pi * 0.5f, false);
  453. quarter.closeSubPath();
  454. fi.addPath (quarter);
  455. break;
  456. }
  457. case Icon::waitInput: // an hourglass: two triangles between two bars, sand in the bottom one
  458. line (st, 6.0f, 3.5f, 18.0f, 3.5f);
  459. line (st, 6.0f, 20.5f, 18.0f, 20.5f);
  460. polyline (st, { { 7.5f, 3.5f }, { 7.5f, 6.5f }, { 12.0f, 12.0f }, { 7.5f, 17.5f }, { 7.5f, 20.5f } });
  461. polyline (st, { { 16.5f, 3.5f }, { 16.5f, 6.5f }, { 12.0f, 12.0f }, { 16.5f, 17.5f }, { 16.5f, 20.5f } });
  462. polyline (fi, { { 12.0f, 16.0f }, { 15.0f, 19.5f }, { 9.0f, 19.5f } }, true);
  463. break;
  464. case Icon::mouseLeft: // a mouse with the left button lit (the mirror of rightClick)
  465. {
  466. roundRect (st, 6.5f, 3.5f, 11.0f, 17.0f, 5.5f);
  467. line (st, 6.5f, 10.5f, 17.5f, 10.5f);
  468. line (st, 12.0f, 3.5f, 12.0f, 10.5f);
  469. juce::Path b;
  470. b.startNewSubPath (12.0f, 3.5f);
  471. b.lineTo (12.0f, 10.5f);
  472. b.lineTo (6.5f, 10.5f);
  473. b.lineTo (6.5f, 9.0f);
  474. b.quadraticTo (6.5f, 3.5f, 12.0f, 3.5f);
  475. b.closeSubPath();
  476. fi.addPath (b);
  477. break;
  478. }
  479. case Icon::mouseMiddle: // a mouse with its wheel button lit
  480. roundRect (st, 6.5f, 3.5f, 11.0f, 17.0f, 5.5f);
  481. line (st, 6.5f, 11.0f, 17.5f, 11.0f);
  482. fi.addRoundedRectangle (10.25f, 4.5f, 3.5f, 6.0f, 1.75f);
  483. break;
  484. case Icon::mouseWheel: // a narrower mouse with its wheel, and up/down chevrons beside it
  485. roundRect (st, 3.5f, 4.5f, 10.0f, 15.0f, 5.0f);
  486. line (st, 3.5f, 11.0f, 13.5f, 11.0f);
  487. fi.addRoundedRectangle (7.25f, 5.5f, 3.5f, 5.0f, 1.75f);
  488. polyline (st, { { 16.5f, 8.0f }, { 18.5f, 5.5f }, { 20.5f, 8.0f } });
  489. polyline (st, { { 16.5f, 16.0f }, { 18.5f, 18.5f }, { 20.5f, 16.0f } });
  490. break;
  491. case Icon::drumBrush: // a brush laying a row of short hits on the grid
  492. {
  493. line (st, 20.5f, 3.5f, 15.0f, 9.0f);
  494. juce::Path tip;
  495. tip.startNewSubPath (14.0f, 8.0f);
  496. tip.lineTo (16.0f, 10.0f);
  497. tip.quadraticTo (14.5f, 14.0f, 10.0f, 13.5f);
  498. tip.quadraticTo (10.5f, 9.5f, 14.0f, 8.0f);
  499. tip.closeSubPath();
  500. fi.addPath (tip);
  501. fi.addRoundedRectangle (3.0f, 17.0f, 4.0f, 3.5f, 1.0f);
  502. fi.addRoundedRectangle (9.0f, 17.0f, 4.0f, 3.5f, 1.0f);
  503. fi.addRoundedRectangle (15.0f, 17.0f, 4.0f, 3.5f, 1.0f);
  504. break;
  505. }
  506. case Icon::graph: // bars of different heights standing on a base line (a step graph)
  507. line (st, 3.5f, 20.5f, 20.5f, 20.5f);
  508. fi.addRoundedRectangle (4.0f, 11.0f, 3.0f, 8.0f, 1.0f);
  509. fi.addRoundedRectangle (8.5f, 6.0f, 3.0f, 13.0f, 1.0f);
  510. fi.addRoundedRectangle (13.0f, 13.5f, 3.0f, 5.5f, 1.0f);
  511. fi.addRoundedRectangle (17.5f, 9.0f, 3.0f, 10.0f, 1.0f);
  512. break;
  513. case Icon::follow: // a playhead line with an arrow carrying it to the right
  514. {
  515. line (st, 7.5f, 4.5f, 7.5f, 19.5f);
  516. juce::Path head;
  517. head.addTriangle (4.5f, 3.5f, 10.5f, 3.5f, 7.5f, 7.0f);
  518. fi.addPath (head);
  519. line (st, 11.5f, 12.0f, 19.5f, 12.0f);
  520. line (st, 16.5f, 9.0f, 19.5f, 12.0f);
  521. line (st, 16.5f, 15.0f, 19.5f, 12.0f);
  522. break;
  523. }
  524. case Icon::loop: // two arcs chasing each other around (a loop), arrow heads at their ends
  525. {
  526. constexpr float r = 7.0f;
  527. auto arcWithHead = [&] (float from, float to) {
  528. juce::Path a;
  529. a.addCentredArc (12.0f, 12.0f, r, r, 0.0f, from, to, true);
  530. st.addPath (a);
  531. // the head at the arc's end, pointing along it (clockwise); angles from 12 o'clock as JUCE's arcs
  532. const juce::Point<float> end (12.0f + r * std::sin (to), 12.0f - r * std::cos (to));
  533. const juce::Point<float> along (std::cos (to), std::sin (to)), out (std::sin (to), -std::cos (to));
  534. juce::Path h;
  535. h.addTriangle (end + along * 3.6f, end + out * 3.2f - along * 0.6f, end - out * 3.2f - along * 0.6f);
  536. fi.addPath (h);
  537. };
  538. arcWithHead (0.45f, 2.55f);
  539. arcWithHead (3.6f, 5.7f);
  540. break;
  541. }
  542. case Icon::dice: // a die showing five
  543. st.addRoundedRectangle (4.5f, 4.5f, 15.0f, 15.0f, 3.0f);
  544. for (const auto& [x, y] : { std::pair { 8.5f, 8.5f }, { 15.5f, 8.5f }, { 12.0f, 12.0f }, { 8.5f, 15.5f }, { 15.5f, 15.5f } })
  545. fi.addEllipse (x - 1.4f, y - 1.4f, 2.8f, 2.8f);
  546. break;
  547. case Icon::tuningFork: // two prongs on a stem, with a weighted foot
  548. {
  549. juce::Path f;
  550. f.startNewSubPath (8.5f, 3.5f);
  551. f.lineTo (8.5f, 10.0f);
  552. f.quadraticTo (8.5f, 14.0f, 12.0f, 14.0f);
  553. f.quadraticTo (15.5f, 14.0f, 15.5f, 10.0f);
  554. f.lineTo (15.5f, 3.5f);
  555. st.addPath (f);
  556. line (st, 12.0f, 14.0f, 12.0f, 18.0f);
  557. fi.addEllipse (10.0f, 17.0f, 4.0f, 4.0f);
  558. break;
  559. }
  560. }
  561. return s;
  562. }
  563. } // namespace
  564. const Shape& get (Icon icon)
  565. {
  566. static std::map<Icon, Shape> cache;
  567. auto it = cache.find (icon);
  568. if (it == cache.end())
  569. it = cache.emplace (icon, make (icon)).first;
  570. return it->second;
  571. }
  572. float strokeWidth (float size)
  573. {
  574. // a fine line that thickens only a little with the size: 1.25 px at 14 px, 1.4 px at 20 px, 1.6 px from
  575. // 30 px (logical pixels; HiDPI screens draw it with proportionally more device pixels)
  576. return juce::jlimit (1.2f, 1.6f, 1.0f + size * 0.019f);
  577. }
  578. void draw (juce::Graphics& g, Icon icon, juce::Rectangle<float> area, juce::Colour colour)
  579. {
  580. if (icon == Icon::none || area.isEmpty())
  581. return;
  582. // a whole-pixel box on whole-pixel coordinates: the grid's half units then land on the same
  583. // sub-pixel phase on every button, so strokes look equally sharp everywhere
  584. const auto size = std::floor (juce::jmin (area.getWidth(), area.getHeight()));
  585. const auto box = juce::Rectangle<float> (std::round (area.getCentreX() - size * 0.5f), std::round (area.getCentreY() - size * 0.5f), size, size);
  586. const auto scale = size / 24.0f;
  587. const auto transform = juce::AffineTransform::scale (scale).translated (box.getX(), box.getY());
  588. const auto& shape = get (icon);
  589. g.setColour (colour);
  590. if (! shape.fill.isEmpty())
  591. g.fillPath (shape.fill, transform);
  592. if (! shape.stroke.isEmpty())
  593. g.strokePath (shape.stroke, juce::PathStrokeType (strokeWidth (size), juce::PathStrokeType::curved, juce::PathStrokeType::rounded), transform);
  594. }
  595. } // namespace icons
  596. IconButton::IconButton (const juce::String& name, Icon i, const juce::String& t)
  597. : juce::Button (name), icon (i), text (t), onColour (theme::colour::accent), iconOnColour (theme::colour::onAccent)
  598. {
  599. setMouseCursor (juce::MouseCursor::PointingHandCursor);
  600. }
  601. void IconButton::setIconSizing (float fraction, float maxPx)
  602. {
  603. if (fraction != iconFraction || maxPx != iconMax)
  604. {
  605. iconFraction = fraction;
  606. iconMax = maxPx;
  607. repaint();
  608. }
  609. }
  610. void IconButton::setIcon (Icon newIcon)
  611. {
  612. if (icon != newIcon)
  613. {
  614. icon = newIcon;
  615. repaint();
  616. }
  617. }
  618. void IconButton::setText (const juce::String& newText)
  619. {
  620. if (text != newText)
  621. {
  622. text = newText;
  623. repaint();
  624. }
  625. }
  626. void IconButton::setPrimary (bool b) { primary = b; repaint(); }
  627. void IconButton::setOnColour (juce::Colour c) { onColour = c; repaint(); }
  628. void IconButton::setIconColourWhenOn (juce::Colour c) { iconOnColour = c; repaint(); }
  629. void IconButton::setFlat (bool b) { flat = b; repaint(); }
  630. void IconButton::setIconColour (juce::Colour c) { iconColour = c; repaint(); }
  631. void IconButton::setCorners (bool left, bool right)
  632. {
  633. roundLeft = left;
  634. roundRight = right;
  635. repaint();
  636. }
  637. void IconButton::mouseDown (const juce::MouseEvent& e)
  638. {
  639. if (e.mods.isPopupMenu() && onRightClick != nullptr)
  640. return; // handled on mouse up, without pressing the button
  641. juce::Button::mouseDown (e);
  642. }
  643. void IconButton::mouseUp (const juce::MouseEvent& e)
  644. {
  645. if (e.mods.isPopupMenu() && onRightClick != nullptr)
  646. {
  647. if (getLocalBounds().contains (e.getPosition()))
  648. onRightClick();
  649. return;
  650. }
  651. juce::Button::mouseUp (e);
  652. }
  653. void IconButton::mouseDoubleClick (const juce::MouseEvent& e)
  654. {
  655. if (onDoubleClick != nullptr && ! e.mods.isPopupMenu())
  656. onDoubleClick();
  657. }
  658. int IconButton::getIdealWidth (int height) const
  659. {
  660. const auto iconSize = icon == Icon::none ? 0 : juce::roundToInt (height * 0.55f);
  661. const auto textWidth = text.isEmpty() ? 0
  662. : juce::roundToInt (juce::GlyphArrangement::getStringWidth (juce::Font (theme::font (theme::type::body)), text));
  663. const auto gap = iconSize > 0 && textWidth > 0 ? 6 : 0;
  664. return juce::jmax (height, iconSize + gap + textWidth + (textWidth > 0 ? 20 : 0));
  665. }
  666. void IconButton::buttonStateChanged()
  667. {
  668. juce::Button::buttonStateChanged();
  669. const auto target = isOver() || isDown() ? 1.0f : 0.0f;
  670. if (target != hover && ! isTimerRunning())
  671. startTimerHz (60);
  672. }
  673. void IconButton::timerCallback()
  674. {
  675. const auto target = isOver() || isDown() ? 1.0f : 0.0f;
  676. const auto step = target > hover ? 0.34f : 0.18f; // in quickly, out a little slower
  677. hover = target > hover ? juce::jmin (target, hover + step) : juce::jmax (target, hover - step);
  678. if (hover == target)
  679. stopTimer();
  680. repaint();
  681. }
  682. void IconButton::paintButton (juce::Graphics& g, bool highlighted, bool down)
  683. {
  684. using namespace theme;
  685. const auto bounds = getLocalBounds().toFloat().reduced (0.5f);
  686. const auto on = getToggleState();
  687. const auto enabled = isEnabled();
  688. // the animated hover level (snaps to the state when nothing animates, e.g. in snapshots)
  689. const auto h = isTimerRunning() ? hover : (highlighted ? 1.0f : 0.0f);
  690. juce::Colour bg;
  691. if (on || primary)
  692. bg = down ? colour::accentPressed : onColour.interpolatedWith (onColour.brighter (0.12f), h);
  693. else if (down)
  694. bg = flat ? colour::textPrimary.withAlpha (0.13f) : colour::bg6;
  695. else // flat buttons get a light wash under the mouse instead of a solid surface
  696. bg = flat ? colour::textPrimary.withAlpha (0.075f * h) : colour::bg4.interpolatedWith (colour::bg5, h);
  697. if (! enabled)
  698. bg = bg.withMultipliedAlpha (0.5f);
  699. if (! bg.isTransparent())
  700. {
  701. juce::Path p;
  702. p.addRoundedRectangle (bounds.getX(), bounds.getY(), bounds.getWidth(), bounds.getHeight(), radius::md, radius::md,
  703. roundLeft, roundRight, roundLeft, roundRight);
  704. g.setColour (bg);
  705. g.fillPath (p);
  706. }
  707. auto fg = (on || primary) ? iconOnColour
  708. : (! iconColour.isTransparent() ? iconColour.interpolatedWith (iconColour.brighter (0.2f), h)
  709. : colour::textSecondary.brighter (0.25f).interpolatedWith (colour::textPrimary, h));
  710. if (! enabled)
  711. fg = colour::textDisabled;
  712. auto area = getLocalBounds().toFloat();
  713. const auto iconSize = juce::jmin (area.getHeight() * iconFraction, iconMax);
  714. if (text.isEmpty())
  715. {
  716. icons::draw (g, icon, area.withSizeKeepingCentre (iconSize, iconSize), fg);
  717. return;
  718. }
  719. const auto font = juce::Font (theme::font (type::body));
  720. const auto textWidth = juce::GlyphArrangement::getStringWidth (font, text);
  721. const auto gap = icon == Icon::none ? 0.0f : 6.0f;
  722. const auto contentWidth = juce::jmin (area.getWidth() - 12.0f, (icon == Icon::none ? 0.0f : iconSize) + gap + textWidth);
  723. auto content = area.withSizeKeepingCentre (contentWidth, area.getHeight());
  724. if (icon != Icon::none)
  725. icons::draw (g, icon, content.removeFromLeft (iconSize).withSizeKeepingCentre (iconSize, iconSize), fg);
  726. content.removeFromLeft (gap);
  727. g.setColour (fg);
  728. g.setFont (font);
  729. g.drawFittedText (text, content.toNearestInt(), juce::Justification::centredLeft, 1, 0.85f);
  730. }
  731. } // namespace lynx