Icons.cpp 28 KB

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