ui.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. #include "ui.h"
  2. #include <gui/icon_i.h>
  3. #include "fonts.h"
  4. #include "game_vexed_icons.h"
  5. //-----------------------------------------------------------------------------
  6. const Icon* tile_to_icon(uint8_t tile, bool gameOver) {
  7. switch(tile) {
  8. case 1:
  9. return &I_a;
  10. case 2:
  11. return &I_b;
  12. case 3:
  13. return &I_c;
  14. case 4:
  15. return &I_alt_d;
  16. case 5:
  17. return &I_e;
  18. case 6:
  19. return &I_f;
  20. case 7:
  21. return &I_g;
  22. case 8:
  23. return &I_h;
  24. case 9:
  25. default:
  26. return gameOver ? &I_w_black : &I_w;
  27. };
  28. }
  29. //-----------------------------------------------------------------------------
  30. void gray_canvas(Canvas* const canvas) {
  31. canvas_set_color(canvas, ColorWhite);
  32. for(int x = 0; x < GUI_DISPLAY_WIDTH; x += 2) {
  33. for(int y = 0; y < GUI_DISPLAY_HEIGHT; y++) {
  34. canvas_draw_dot(canvas, x + (y % 2 == 1 ? 0 : 1), y);
  35. }
  36. }
  37. }
  38. //-----------------------------------------------------------------------------
  39. void set_bounding_box(
  40. BoundingBox* box,
  41. uint8_t offset_x,
  42. uint8_t offset_y,
  43. uint8_t width,
  44. uint8_t height) {
  45. furi_assert(box);
  46. box->offset_x = offset_x;
  47. box->offset_y = offset_y;
  48. box->width = width;
  49. box->height = height;
  50. }
  51. //-----------------------------------------------------------------------------
  52. void mask_canvas(Canvas* const canvas, uint8_t sx, uint8_t sy, uint8_t w, uint8_t h) {
  53. canvas_set_color(canvas, ColorWhite);
  54. for(uint8_t x = sx; x < sx + w; x += 2) {
  55. for(uint8_t y = sy; y < sy + h; y++) {
  56. canvas_draw_dot(canvas, x + (y % 2 == 1 ? 0 : 1), y);
  57. }
  58. }
  59. }
  60. //-----------------------------------------------------------------------------
  61. void canvas_draw_hline_dotted(Canvas* const canvas, uint8_t x, uint8_t y, uint8_t w) {
  62. for(uint8_t i = x; i < x + w; i += 2) {
  63. canvas_draw_dot(canvas, i, y);
  64. }
  65. }
  66. //-----------------------------------------------------------------------------
  67. void canvas_draw_vline_dotted(Canvas* const canvas, uint8_t x, uint8_t y, uint8_t h) {
  68. for(uint8_t i = y; i < y + h; i += 2) {
  69. canvas_draw_dot(canvas, x, i);
  70. }
  71. }
  72. //-----------------------------------------------------------------------------
  73. void elements_button_right_back(Canvas* canvas, const char* str) {
  74. const uint8_t button_height = 12;
  75. const uint8_t vertical_offset = 3;
  76. const uint8_t horizontal_offset = 3;
  77. const uint8_t string_width = canvas_string_width(canvas, str);
  78. const Icon* icon = &I_back_btn_10x8;
  79. const uint8_t icon_h_offset = 3;
  80. const uint8_t icon_width_with_offset = icon->width + icon_h_offset;
  81. const uint8_t icon_v_offset = icon->height + vertical_offset;
  82. const uint8_t button_width = string_width + horizontal_offset * 2 + icon_width_with_offset;
  83. const uint8_t x = canvas_width(canvas);
  84. const uint8_t y = canvas_height(canvas);
  85. canvas_draw_box(canvas, x - button_width, y - button_height, button_width, button_height);
  86. canvas_draw_line(canvas, x - button_width - 1, y, x - button_width - 1, y - button_height + 0);
  87. canvas_draw_line(canvas, x - button_width - 2, y, x - button_width - 2, y - button_height + 1);
  88. canvas_draw_line(canvas, x - button_width - 3, y, x - button_width - 3, y - button_height + 2);
  89. canvas_invert_color(canvas);
  90. canvas_draw_str(canvas, x - button_width + horizontal_offset, y - vertical_offset, str);
  91. canvas_draw_icon(
  92. canvas, x - horizontal_offset - icon->width, y - icon_v_offset, &I_back_btn_10x8);
  93. canvas_invert_color(canvas);
  94. }
  95. //-----------------------------------------------------------------------------
  96. size_t elements_get_max_chars_to_fit(
  97. Canvas* canvas,
  98. BoundingBox* box,
  99. Align horizontal,
  100. const char* text,
  101. uint8_t x) {
  102. const char* end = strchr(text, '\n');
  103. if(end == NULL) {
  104. end = text + strlen(text);
  105. }
  106. size_t text_size = end - text;
  107. FuriString* str;
  108. str = furi_string_alloc_set(text);
  109. furi_string_left(str, text_size);
  110. size_t result = 0;
  111. uint16_t len_px = canvas_string_width(canvas, furi_string_get_cstr(str));
  112. uint8_t px_left = 0;
  113. if(horizontal == AlignCenter) {
  114. if(x > (box->width / 2)) {
  115. px_left = (box->width - x) * 2;
  116. } else {
  117. px_left = x * 2;
  118. }
  119. } else if(horizontal == AlignLeft) {
  120. px_left = box->width - x;
  121. } else if(horizontal == AlignRight) {
  122. px_left = x;
  123. } else {
  124. furi_crash();
  125. }
  126. if(len_px > px_left) {
  127. size_t excess_symbols_approximately =
  128. ceilf((float)(len_px - px_left) / ((float)len_px / (float)text_size));
  129. // reduce to 5 to be sure dash fit, and next line will be at least 5 symbols long
  130. if(excess_symbols_approximately > 0) {
  131. excess_symbols_approximately = MAX(excess_symbols_approximately, 5u);
  132. result = text_size - excess_symbols_approximately - 1;
  133. } else {
  134. result = text_size;
  135. }
  136. } else {
  137. result = text_size;
  138. }
  139. furi_string_free(str);
  140. return result;
  141. }
  142. //-----------------------------------------------------------------------------
  143. void elements_multiline_text_aligned_limited(
  144. Canvas* canvas,
  145. BoundingBox* box,
  146. uint8_t x,
  147. uint8_t y,
  148. uint8_t h,
  149. Align horizontal,
  150. Align vertical,
  151. const char* text) {
  152. furi_assert(canvas);
  153. furi_assert(box);
  154. furi_assert(text);
  155. uint8_t lines_count = 0;
  156. uint8_t lineNo = 0;
  157. uint8_t font_height = canvas_current_font_height(canvas);
  158. FuriString* line;
  159. /* go through text line by line and count lines */
  160. for(const char* start = text; start[0];) {
  161. size_t chars_fit = elements_get_max_chars_to_fit(canvas, box, horizontal, start, x);
  162. ++lines_count;
  163. start += chars_fit;
  164. start += start[0] == '\n' ? 1 : 0;
  165. }
  166. bool overflow = lines_count > h;
  167. lines_count = MIN(lines_count, h);
  168. if(vertical == AlignBottom) {
  169. y -= font_height * (lines_count - 1);
  170. } else if(vertical == AlignCenter) {
  171. y -= (font_height * (lines_count - 1)) / 2;
  172. }
  173. /* go through text line by line and print them */
  174. for(const char* start = text; start[0];) {
  175. size_t chars_fit = elements_get_max_chars_to_fit(canvas, box, horizontal, start, x);
  176. lineNo++;
  177. if((start[chars_fit] == '\n') || (start[chars_fit] == 0)) {
  178. line = furi_string_alloc_printf("%.*s", chars_fit, start);
  179. } else if(((y + font_height) > canvas_height(canvas)) || ((lineNo >= h) && overflow)) {
  180. line = furi_string_alloc_printf("%.*s...\n", chars_fit, start);
  181. } else {
  182. line = furi_string_alloc_printf("%.*s\n", chars_fit, start);
  183. }
  184. canvas_draw_str_aligned(
  185. canvas,
  186. x + box->offset_x,
  187. y + box->offset_y,
  188. horizontal,
  189. vertical,
  190. furi_string_get_cstr(line));
  191. furi_string_free(line);
  192. y += font_height;
  193. if(y > canvas_height(canvas)) {
  194. break;
  195. }
  196. if(lineNo >= h) {
  197. break;
  198. }
  199. start += chars_fit;
  200. start += start[0] == '\n' ? 1 : 0;
  201. }
  202. }
  203. //-----------------------------------------------------------------------------
  204. void hint_pill_single(Canvas* canvas, const char* str) {
  205. canvas_draw_rframe(canvas, 82, 53, 46, 11, 3);
  206. canvas_set_custom_u8g2_font(canvas, app_u8g2_font_micro_tr);
  207. canvas_draw_str_aligned(canvas, 104, 56, AlignCenter, AlignTop, str);
  208. }
  209. //-----------------------------------------------------------------------------
  210. void hint_pill_double(Canvas* canvas, const char* str1, const char* str2, const Icon* icon) {
  211. canvas_set_color(canvas, ColorBlack);
  212. canvas_draw_rbox(canvas, 82, 49, 46, 15, 3);
  213. canvas_set_color(canvas, ColorWhite);
  214. canvas_draw_icon(canvas, 86, 51, icon);
  215. canvas_set_custom_u8g2_font(canvas, app_u8g2_font_micro_tr);
  216. canvas_draw_str_aligned(canvas, 110, 51, AlignCenter, AlignTop, str1);
  217. canvas_draw_str_aligned(canvas, 110, 57, AlignCenter, AlignTop, str2);
  218. }
  219. //-----------------------------------------------------------------------------
  220. void menu_pill(
  221. Canvas* canvas,
  222. int no,
  223. int count,
  224. bool selected,
  225. bool masked,
  226. const char* label,
  227. const Icon* icon) {
  228. UNUSED(count);
  229. uint8_t height = 12;
  230. uint8_t y_offset = 4;
  231. uint8_t menu_pad_v = 4;
  232. uint8_t menu_pad_h = 8;
  233. uint8_t menu_total_w = 114;
  234. uint8_t menu_off_x = (GUI_DISPLAY_WIDTH - menu_total_w) / 2;
  235. uint8_t col = no % 2;
  236. uint8_t row = no / 2;
  237. uint8_t width = (menu_total_w - menu_pad_h) / 2;
  238. uint8_t x = (col * (width + menu_pad_h)) + menu_off_x;
  239. uint8_t origin_x = x + (width / 2);
  240. uint8_t y = y_offset + row * (height + menu_pad_v);
  241. canvas_set_color(canvas, selected ? ColorWhite : ColorBlack);
  242. canvas_draw_rbox(canvas, x + 1, y + 1, width, height, 2);
  243. canvas_set_color(canvas, selected ? ColorBlack : ColorWhite);
  244. canvas_draw_rbox(canvas, x, y, width, height, 2);
  245. canvas_set_color(canvas, ColorBlack);
  246. canvas_draw_rframe(canvas, x, y, width, height, 2);
  247. canvas_set_color(canvas, selected ? ColorWhite : ColorBlack);
  248. canvas_set_font(canvas, FontSecondary);
  249. canvas_draw_str_aligned(
  250. canvas, origin_x + 6, y + (height / 2), AlignCenter, AlignCenter, label);
  251. canvas_draw_icon(canvas, x + 3, y + 2, icon);
  252. if(masked) {
  253. mask_canvas(canvas, x, y, width + 1, height + 1);
  254. }
  255. }
  256. //-----------------------------------------------------------------------------
  257. void main_menu_indi(Canvas* canvas, uint8_t x, uint8_t y, uint8_t xicon, const Icon* icon) {
  258. const uint8_t height = 12;
  259. const uint8_t width = 13;
  260. canvas_set_color(canvas, ColorBlack);
  261. canvas_draw_rbox(canvas, x + 1, y + 1, width, height, 2);
  262. canvas_set_color(canvas, ColorWhite);
  263. canvas_draw_rbox(canvas, x, y, width, height, 2);
  264. canvas_set_color(canvas, ColorBlack);
  265. canvas_draw_rframe(canvas, x, y, width, height, 2);
  266. canvas_draw_icon(canvas, x + 4 + xicon, y + 2, icon);
  267. }
  268. //-----------------------------------------------------------------------------
  269. void main_menu_pill(
  270. Canvas* canvas,
  271. uint8_t y,
  272. uint8_t w,
  273. bool selected,
  274. bool leftIni,
  275. bool rightIndi,
  276. const char* label) {
  277. uint8_t height = 12;
  278. uint8_t x = (GUI_DISPLAY_WIDTH - w) / 2;
  279. uint8_t origin_x = x + (w / 2);
  280. canvas_set_color(canvas, ColorBlack);
  281. canvas_draw_rbox(canvas, x + 1, y + 1, w, height, 2);
  282. canvas_set_color(canvas, selected ? ColorBlack : ColorWhite);
  283. canvas_draw_rbox(canvas, x, y, w, height, 2);
  284. canvas_set_color(canvas, ColorBlack);
  285. canvas_draw_rframe(canvas, x, y, w, height, 2);
  286. canvas_set_color(canvas, selected ? ColorWhite : ColorBlack);
  287. canvas_draw_str_aligned(canvas, origin_x, y + (height / 2), AlignCenter, AlignCenter, label);
  288. canvas_set_color(canvas, ColorBlack);
  289. if(leftIni) canvas_draw_icon(canvas, x - 8, y + 3, &I_ButtonLeft_4x7);
  290. if(rightIndi) canvas_draw_icon(canvas, x + w + 5, y + 3, &I_ButtonRight_4x7);
  291. }
  292. //-----------------------------------------------------------------------------
  293. #define HIST_BRICK_W 13
  294. #define HIST_BRICK_H 18
  295. #define HIST_SPC_X 6
  296. #define HIST_SPC_Y 6
  297. #define HIST_PAD_X 8
  298. #define HIST_PAD_Y 8
  299. //-----------------------------------------------------------------------------
  300. void panel_histogram_elem(Canvas* canvas, uint8_t x, uint8_t y, uint8_t brick, uint8_t count) {
  301. int bufSize = 8;
  302. char buf[bufSize];
  303. canvas_set_color(canvas, ColorBlack);
  304. canvas_draw_icon(canvas, x + 2, y, tile_to_icon(brick, false));
  305. canvas_set_font(canvas, FontPrimary);
  306. memset(buf, 0, bufSize);
  307. snprintf(buf, sizeof(buf), "%u", count);
  308. canvas_draw_str_aligned(
  309. canvas, x + (HIST_BRICK_W / 2), y + HIST_BRICK_H, AlignCenter, AlignBottom, buf);
  310. }
  311. //-----------------------------------------------------------------------------
  312. void panel_histogram(Canvas* canvas, const char* bricks, const uint8_t* values) {
  313. const uint8_t len = strlen(bricks);
  314. if(len == 0) {
  315. return;
  316. }
  317. const uint8_t rows = (len > 4) ? 2 : 1;
  318. const uint8_t breakpoint = (len > 4) ? (len - (len / 2)) : len;
  319. const uint8_t widths[2] = {breakpoint, len - breakpoint};
  320. const uint8_t columns = MAX(widths[0], widths[1]);
  321. const uint8_t w = (columns * HIST_BRICK_W) + ((columns - 1) * HIST_SPC_X) + (2 * HIST_PAD_X);
  322. const uint8_t h = (rows * HIST_BRICK_H) + ((rows - 1) * HIST_SPC_Y) + (2 * HIST_PAD_Y);
  323. const uint8_t x = (GUI_DISPLAY_WIDTH - w) / 2;
  324. const uint8_t y = (GUI_DISPLAY_HEIGHT - h) / 2;
  325. canvas_set_color(canvas, ColorBlack);
  326. canvas_draw_rbox(canvas, x + 2, y + 2, w, h, 4);
  327. canvas_set_color(canvas, ColorWhite);
  328. canvas_draw_rbox(canvas, x, y, w, h, 4);
  329. canvas_set_color(canvas, ColorBlack);
  330. canvas_draw_rframe(canvas, x, y, w, h, 4);
  331. uint8_t i, row = 0, col = 0;
  332. uint8_t posx = x + HIST_PAD_X;
  333. uint8_t posy = y + HIST_PAD_Y;
  334. for(i = 0; i < len; i++) {
  335. panel_histogram_elem(canvas, posx, posy, (uint8_t)(bricks[i]), values[i]);
  336. col++;
  337. posx += (HIST_BRICK_W + HIST_SPC_X);
  338. if((rows > 1) && (i == breakpoint - 1)) {
  339. row++;
  340. col = 0;
  341. posx = (x + HIST_PAD_X) + (widths[0] - widths[1]) * ((HIST_BRICK_W + HIST_SPC_X) / 2);
  342. posy += (HIST_BRICK_H + HIST_SPC_Y);
  343. }
  344. }
  345. }
  346. //-----------------------------------------------------------------------------
  347. uint8_t dialog_frame(
  348. Canvas* canvas,
  349. uint8_t w,
  350. uint8_t h,
  351. bool bigHeader,
  352. bool safeFrame,
  353. const char* label) {
  354. const uint8_t x = (GUI_DISPLAY_WIDTH - w) / 2;
  355. const uint8_t y = (53 - h) / 2;
  356. canvas_set_color(canvas, ColorWhite);
  357. if(safeFrame) canvas_draw_rbox(canvas, x - 3, y - 2, w + 7, h + 5, 3);
  358. canvas_set_color(canvas, ColorBlack);
  359. canvas_draw_rbox(canvas, x + 1, y + 1, w, h, 3);
  360. canvas_set_color(canvas, ColorWhite);
  361. canvas_draw_rbox(canvas, x, y, w, h, 3);
  362. canvas_set_color(canvas, ColorBlack);
  363. canvas_draw_rframe(canvas, x, y, w, h, 3);
  364. canvas_set_color(canvas, ColorBlack);
  365. canvas_draw_rbox(canvas, x, y, w, 11 + (bigHeader ? 1 : 0), 3);
  366. canvas_draw_box(canvas, x, y + 3, w, (11 - 3) + (bigHeader ? 1 : 0));
  367. canvas_set_color(canvas, ColorWhite);
  368. if(bigHeader) {
  369. canvas_set_font(canvas, FontPrimary);
  370. } else {
  371. canvas_set_custom_u8g2_font(canvas, app_u8g2_font_squeezed_r7_tr);
  372. }
  373. canvas_draw_str_aligned(canvas, GUI_DISPLAY_WIDTH / 2, y + 2, AlignCenter, AlignTop, label);
  374. return y + 12;
  375. }