My Marlin configs for Fabrikator Mini and CTC i3 Pro B
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.

files_screen.cpp 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /********************
  2. * files_screen.cpp *
  3. ********************/
  4. /****************************************************************************
  5. * Written By Mark Pelletier 2017 - Aleph Objects, Inc. *
  6. * Written By Marcio Teixeira 2018 - Aleph Objects, Inc. *
  7. * *
  8. * This program is free software: you can redistribute it and/or modify *
  9. * it under the terms of the GNU General Public License as published by *
  10. * the Free Software Foundation, either version 3 of the License, or *
  11. * (at your option) any later version. *
  12. * *
  13. * This program is distributed in the hope that it will be useful, *
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  16. * GNU General Public License for more details. *
  17. * *
  18. * To view a copy of the GNU General Public License, go to the following *
  19. * location: <https://www.gnu.org/licenses/>. *
  20. ****************************************************************************/
  21. #include "../config.h"
  22. #if BOTH(TOUCH_UI_FTDI_EVE, SDSUPPORT)
  23. #include "screens.h"
  24. #include "screen_data.h"
  25. using namespace FTDI;
  26. using namespace ExtUI;
  27. using namespace Theme;
  28. void FilesScreen::onEntry() {
  29. screen_data.FilesScreen.cur_page = 0;
  30. screen_data.FilesScreen.selected_tag = 0xFF;
  31. #if ENABLED(SCROLL_LONG_FILENAMES) && (FTDI_API_LEVEL >= 810)
  32. CLCD::mem_write_32(CLCD::REG::MACRO_0,DL::NOP);
  33. #endif
  34. gotoPage(0);
  35. BaseScreen::onEntry();
  36. }
  37. const char *FilesScreen::getSelectedFilename(bool longName) {
  38. FileList files;
  39. files.seek(getSelectedFileIndex(), true);
  40. return longName ? files.longFilename() : files.shortFilename();
  41. }
  42. void FilesScreen::drawSelectedFile() {
  43. FileList files;
  44. files.seek(getSelectedFileIndex(), true);
  45. screen_data.FilesScreen.flags.is_dir = files.isDir();
  46. drawFileButton(
  47. files.filename(),
  48. screen_data.FilesScreen.selected_tag,
  49. screen_data.FilesScreen.flags.is_dir,
  50. true
  51. );
  52. }
  53. uint16_t FilesScreen::getSelectedFileIndex() {
  54. return getFileForTag(screen_data.FilesScreen.selected_tag);
  55. }
  56. uint16_t FilesScreen::getFileForTag(uint8_t tag) {
  57. return screen_data.FilesScreen.cur_page * files_per_page + tag - 2;
  58. }
  59. #ifdef TOUCH_UI_PORTRAIT
  60. #define GRID_COLS 6
  61. #define GRID_ROWS (files_per_page + header_h + footer_h)
  62. #else
  63. #define GRID_COLS 6
  64. #define GRID_ROWS (files_per_page + header_h + footer_h)
  65. #endif
  66. void FilesScreen::drawFileButton(const char* filename, uint8_t tag, bool is_dir, bool is_highlighted) {
  67. const uint8_t line = getLineForTag(tag)+1;
  68. CommandProcessor cmd;
  69. cmd.tag(tag);
  70. cmd.cmd(COLOR_RGB(is_highlighted ? fg_action : bg_color));
  71. cmd.font(font_medium)
  72. .rectangle( 0, BTN_Y(header_h+line), display_width, BTN_H(1));
  73. cmd.cmd(COLOR_RGB(is_highlighted ? normal_btn.rgb : bg_text_enabled));
  74. constexpr uint16_t dim[2] = {BTN_SIZE(6,1)};
  75. #define POS_AND_SHORTEN(SHORTEN) BTN_POS(1,header_h+line), dim[0] - (SHORTEN), dim[1]
  76. #define POS_AND_SIZE POS_AND_SHORTEN(0)
  77. #if ENABLED(SCROLL_LONG_FILENAMES)
  78. if (is_highlighted) {
  79. cmd.cmd(SAVE_CONTEXT());
  80. cmd.cmd(MACRO(0));
  81. cmd.text(POS_AND_SIZE, filename, OPT_CENTERY | OPT_NOFIT);
  82. } else
  83. #endif
  84. draw_text_with_ellipsis(cmd, POS_AND_SHORTEN(is_dir ? 20 : 0), filename, OPT_CENTERY, font_medium);
  85. if (is_dir && !is_highlighted) {
  86. cmd.text(POS_AND_SIZE, F("> "), OPT_CENTERY | OPT_RIGHTX);
  87. }
  88. #if ENABLED(SCROLL_LONG_FILENAMES)
  89. if (is_highlighted) {
  90. cmd.cmd(RESTORE_CONTEXT());
  91. }
  92. #endif
  93. }
  94. void FilesScreen::drawFileList() {
  95. FileList files;
  96. screen_data.FilesScreen.num_page = max(1,ceil(float(files.count()) / files_per_page));
  97. screen_data.FilesScreen.cur_page = min(screen_data.FilesScreen.cur_page, screen_data.FilesScreen.num_page-1);
  98. screen_data.FilesScreen.flags.is_root = files.isAtRootDir();
  99. #undef MARGIN_T
  100. #undef MARGIN_B
  101. #define MARGIN_T 0
  102. #define MARGIN_B 0
  103. uint16_t fileIndex = screen_data.FilesScreen.cur_page * files_per_page;
  104. for(uint8_t i = 0; i < files_per_page; i++, fileIndex++) {
  105. if (files.seek(fileIndex)) {
  106. drawFileButton(files.filename(), getTagForLine(i), files.isDir(), false);
  107. }
  108. else {
  109. break;
  110. }
  111. }
  112. }
  113. void FilesScreen::drawHeader() {
  114. const bool prev_enabled = screen_data.FilesScreen.cur_page > 0;
  115. const bool next_enabled = screen_data.FilesScreen.cur_page < (screen_data.FilesScreen.num_page - 1);
  116. #undef MARGIN_T
  117. #undef MARGIN_B
  118. #define MARGIN_T 0
  119. #define MARGIN_B 2
  120. char str[16];
  121. sprintf_P(str, PSTR("Page %d of %d"),
  122. screen_data.FilesScreen.cur_page + 1, screen_data.FilesScreen.num_page);
  123. CommandProcessor cmd;
  124. cmd.colors(normal_btn)
  125. .font(font_small)
  126. .tag(0).button( BTN_POS(2,1), BTN_SIZE(4,header_h), str, OPT_CENTER | OPT_FLAT)
  127. .font(font_medium)
  128. .colors(action_btn)
  129. .tag(241).enabled(prev_enabled).button( BTN_POS(1,1), BTN_SIZE(1,header_h), F("<"))
  130. .tag(242).enabled(next_enabled).button( BTN_POS(6,1), BTN_SIZE(1,header_h), F(">"));
  131. }
  132. void FilesScreen::drawFooter() {
  133. #undef MARGIN_T
  134. #undef MARGIN_B
  135. #ifdef TOUCH_UI_PORTRAIT
  136. #define MARGIN_T 15
  137. #define MARGIN_B 5
  138. #else
  139. #define MARGIN_T 5
  140. #define MARGIN_B 5
  141. #endif
  142. const bool has_selection = screen_data.FilesScreen.selected_tag != 0xFF;
  143. const uint8_t back_tag = screen_data.FilesScreen.flags.is_root ? 240 : 245;
  144. const uint8_t y = GRID_ROWS - footer_h + 1;
  145. const uint8_t h = footer_h;
  146. CommandProcessor cmd;
  147. cmd.colors(normal_btn)
  148. .font(font_medium)
  149. .colors(has_selection ? normal_btn : action_btn)
  150. .tag(back_tag).button( BTN_POS(4,y), BTN_SIZE(3,h), GET_TEXT_F(MSG_BACK))
  151. .enabled(has_selection)
  152. .colors(has_selection ? action_btn : normal_btn);
  153. if (screen_data.FilesScreen.flags.is_dir) {
  154. cmd.tag(244).button( BTN_POS(1, y), BTN_SIZE(3,h), GET_TEXT_F(MSG_BUTTON_OPEN));
  155. } else {
  156. cmd.tag(243).button( BTN_POS(1, y), BTN_SIZE(3,h), GET_TEXT_F(MSG_BUTTON_PRINT));
  157. }
  158. }
  159. void FilesScreen::onRedraw(draw_mode_t what) {
  160. if (what & FOREGROUND) {
  161. drawHeader();
  162. drawSelectedFile();
  163. drawFooter();
  164. }
  165. }
  166. void FilesScreen::gotoPage(uint8_t page) {
  167. screen_data.FilesScreen.selected_tag = 0xFF;
  168. screen_data.FilesScreen.cur_page = page;
  169. CommandProcessor cmd;
  170. cmd.cmd(CMD_DLSTART)
  171. .cmd(CLEAR_COLOR_RGB(bg_color))
  172. .cmd(CLEAR(true,true,true))
  173. .colors(normal_btn);
  174. drawFileList();
  175. storeBackground();
  176. }
  177. bool FilesScreen::onTouchEnd(uint8_t tag) {
  178. switch (tag) {
  179. case 240: GOTO_PREVIOUS(); return true;
  180. case 241:
  181. if (screen_data.FilesScreen.cur_page > 0) {
  182. gotoPage(screen_data.FilesScreen.cur_page-1);
  183. }
  184. break;
  185. case 242:
  186. if (screen_data.FilesScreen.cur_page < (screen_data.FilesScreen.num_page-1)) {
  187. gotoPage(screen_data.FilesScreen.cur_page+1);
  188. }
  189. break;
  190. case 243:
  191. ConfirmStartPrintDialogBox::show(getSelectedFileIndex());
  192. return true;
  193. case 244:
  194. {
  195. FileList files;
  196. files.changeDir(getSelectedShortFilename());
  197. gotoPage(0);
  198. }
  199. break;
  200. case 245:
  201. {
  202. FileList files;
  203. files.upDir();
  204. gotoPage(0);
  205. }
  206. break;
  207. default:
  208. if (tag < 240) {
  209. screen_data.FilesScreen.selected_tag = tag;
  210. #if ENABLED(SCROLL_LONG_FILENAMES) && (FTDI_API_LEVEL >= 810)
  211. if (FTDI::ftdi_chip >= 810) {
  212. const char *longFilename = getSelectedLongFilename();
  213. if (longFilename[0]) {
  214. CommandProcessor cmd;
  215. uint16_t text_width = cmd.font(font_medium).text_width(longFilename);
  216. screen_data.FilesScreen.scroll_pos = 0;
  217. if (text_width > display_width)
  218. screen_data.FilesScreen.scroll_max = text_width - display_width + MARGIN_L + MARGIN_R;
  219. else
  220. screen_data.FilesScreen.scroll_max = 0;
  221. }
  222. }
  223. #endif
  224. }
  225. break;
  226. }
  227. return true;
  228. }
  229. void FilesScreen::onIdle() {
  230. #if ENABLED(SCROLL_LONG_FILENAMES) && (FTDI_API_LEVEL >= 810)
  231. if (FTDI::ftdi_chip >= 810) {
  232. CLCD::mem_write_32(CLCD::REG::MACRO_0,
  233. VERTEX_TRANSLATE_X(-int32_t(screen_data.FilesScreen.scroll_pos)));
  234. if (screen_data.FilesScreen.scroll_pos < screen_data.FilesScreen.scroll_max * 16)
  235. screen_data.FilesScreen.scroll_pos++;
  236. }
  237. #endif
  238. }
  239. #endif // TOUCH_UI_FTDI_EVE