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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. #include "../screens.h"
  23. #include "../screen_data.h"
  24. #ifdef FTDI_FILES_SCREEN
  25. #if ENABLED(TOUCH_UI_PORTRAIT)
  26. #define GRID_COLS 6
  27. #define GRID_ROWS 15
  28. #define FILES_PER_PAGE 11
  29. #define PREV_DIR LEFT
  30. #define NEXT_DIR RIGHT
  31. #define PREV_POS BTN_POS(1,1), BTN_SIZE(1,2)
  32. #define HEAD_POS BTN_POS(2,1), BTN_SIZE(4,2)
  33. #define NEXT_POS BTN_POS(6,1), BTN_SIZE(1,2)
  34. #define LIST_POS BTN_POS(1,3), BTN_SIZE(6,FILES_PER_PAGE)
  35. #define BTN1_POS BTN_POS(1,14), BTN_SIZE(3,2)
  36. #define BTN2_POS BTN_POS(4,14), BTN_SIZE(3,2)
  37. #else
  38. #define GRID_COLS 12
  39. #define GRID_ROWS 8
  40. #define FILES_PER_PAGE 6
  41. #define PREV_DIR UP
  42. #define NEXT_DIR DOWN
  43. #define PREV_POS BTN_POS(12,2), BTN_SIZE(1,3)
  44. #define HEAD_POS BTN_POS( 1,1), BTN_SIZE(12,1)
  45. #define NEXT_POS BTN_POS(12,5), BTN_SIZE(1,4)
  46. #define LIST_POS BTN_POS( 1,2), BTN_SIZE(11,FILES_PER_PAGE)
  47. #define BTN1_POS BTN_POS( 1,8), BTN_SIZE(6,1)
  48. #define BTN2_POS BTN_POS( 7,8), BTN_SIZE(5,1)
  49. #endif
  50. using namespace FTDI;
  51. using namespace ExtUI;
  52. using namespace Theme;
  53. constexpr static FilesScreenData &mydata = screen_data.FilesScreen;
  54. void FilesScreen::onEntry() {
  55. mydata.cur_page = 0;
  56. mydata.selected_tag = 0xFF;
  57. #if ENABLED(SCROLL_LONG_FILENAMES) && (FTDI_API_LEVEL >= 810)
  58. CLCD::mem_write_32(CLCD::REG::MACRO_0,DL::NOP);
  59. #endif
  60. gotoPage(0);
  61. BaseScreen::onEntry();
  62. }
  63. const char *FilesScreen::getSelectedFilename(bool shortName) {
  64. FileList files;
  65. files.seek(getSelectedFileIndex(), true);
  66. return shortName ? files.shortFilename() : files.filename();
  67. }
  68. void FilesScreen::drawSelectedFile() {
  69. if (mydata.selected_tag == 0xFF) return;
  70. FileList files;
  71. files.seek(getSelectedFileIndex(), true);
  72. mydata.flags.is_dir = files.isDir();
  73. drawFileButton(
  74. files.filename(),
  75. mydata.selected_tag,
  76. mydata.flags.is_dir,
  77. true
  78. );
  79. }
  80. uint16_t FilesScreen::getSelectedFileIndex() {
  81. return getFileForTag(mydata.selected_tag);
  82. }
  83. uint16_t FilesScreen::getFileForTag(uint8_t tag) {
  84. return mydata.cur_page * FILES_PER_PAGE + tag - 2;
  85. }
  86. void FilesScreen::drawFileButton(int x, int y, int w, int h, const char *filename, uint8_t tag, bool is_dir, bool is_highlighted) {
  87. #define SUB_COLS 6
  88. #define SUB_ROWS FILES_PER_PAGE
  89. const int bx = SUB_X(1);
  90. const int by = SUB_Y(getLineForTag(tag)+1);
  91. const int bw = SUB_W(6);
  92. const int bh = SUB_H(1);
  93. CommandProcessor cmd;
  94. cmd.tag(tag);
  95. cmd.cmd(COLOR_RGB(is_highlighted ? fg_action : bg_color));
  96. cmd.font(font_medium).rectangle(bx, by, bw, bh);
  97. cmd.cmd(COLOR_RGB(is_highlighted ? normal_btn.rgb : bg_text_enabled));
  98. #if ENABLED(SCROLL_LONG_FILENAMES)
  99. if (is_highlighted) {
  100. cmd.cmd(SAVE_CONTEXT());
  101. cmd.cmd(SCISSOR_XY(x,y));
  102. cmd.cmd(SCISSOR_SIZE(w,h));
  103. cmd.cmd(MACRO(0));
  104. cmd.text(bx, by, bw, bh, filename, OPT_CENTERY | OPT_NOFIT);
  105. } else
  106. #endif
  107. draw_text_with_ellipsis(cmd, bx,by, bw - (is_dir ? 20 : 0), bh, filename, OPT_CENTERY, font_medium);
  108. if (is_dir && !is_highlighted) cmd.text(bx, by, bw, bh, F("> "), OPT_CENTERY | OPT_RIGHTX);
  109. #if ENABLED(SCROLL_LONG_FILENAMES)
  110. if (is_highlighted) cmd.cmd(RESTORE_CONTEXT());
  111. #endif
  112. }
  113. void FilesScreen::drawFileList() {
  114. FileList files;
  115. mydata.num_page = max(1,ceil(float(files.count()) / FILES_PER_PAGE));
  116. mydata.cur_page = min(mydata.cur_page, mydata.num_page-1);
  117. mydata.flags.is_root = files.isAtRootDir();
  118. mydata.flags.is_empty = true;
  119. uint16_t fileIndex = mydata.cur_page * FILES_PER_PAGE;
  120. for (uint8_t i = 0; i < FILES_PER_PAGE; i++, fileIndex++) {
  121. if (!files.seek(fileIndex)) break;
  122. drawFileButton(files.filename(), getTagForLine(i), files.isDir(), false);
  123. mydata.flags.is_empty = false;
  124. }
  125. }
  126. void FilesScreen::drawHeader() {
  127. char str[16];
  128. sprintf_P(str, PSTR("Page %d of %d"), mydata.cur_page + 1, mydata.num_page);
  129. CommandProcessor cmd;
  130. cmd.colors(normal_btn)
  131. .font(font_small)
  132. .tag(0).button(HEAD_POS, str, OPT_CENTER | OPT_FLAT);
  133. }
  134. void FilesScreen::drawArrows() {
  135. const bool prev_enabled = mydata.cur_page > 0;
  136. const bool next_enabled = mydata.cur_page < (mydata.num_page - 1);
  137. CommandProcessor cmd;
  138. cmd.colors(normal_btn);
  139. cmd.tag(242).enabled(prev_enabled).button(PREV_POS, F("")); if (prev_enabled) drawArrow(PREV_POS, PREV_DIR);
  140. cmd.tag(243).enabled(next_enabled).button(NEXT_POS, F("")); if (next_enabled) drawArrow(NEXT_POS, NEXT_DIR);
  141. }
  142. void FilesScreen::drawFooter() {
  143. const bool has_selection = mydata.selected_tag != 0xFF;
  144. CommandProcessor cmd;
  145. cmd.colors(normal_btn)
  146. .font(font_medium)
  147. .colors(has_selection ? normal_btn : action_btn);
  148. if (mydata.flags.is_root)
  149. cmd.tag(240).button(BTN2_POS, GET_TEXT_F(MSG_BUTTON_DONE));
  150. else
  151. cmd.tag(245).button(BTN2_POS, F("Up Dir"));
  152. cmd.enabled(has_selection)
  153. .colors(has_selection ? action_btn : normal_btn);
  154. if (mydata.flags.is_dir)
  155. cmd.tag(244).button(BTN1_POS, GET_TEXT_F(MSG_BUTTON_OPEN));
  156. else
  157. cmd.tag(241).button(BTN1_POS, GET_TEXT_F(MSG_BUTTON_PRINT));
  158. }
  159. void FilesScreen::drawFileButton(const char *filename, uint8_t tag, bool is_dir, bool is_highlighted) {
  160. #undef MARGIN_L
  161. #undef MARGIN_R
  162. #define MARGIN_L 0
  163. #define MARGIN_R 0
  164. drawFileButton(LIST_POS, filename, tag, is_dir, is_highlighted);
  165. }
  166. void FilesScreen::onRedraw(draw_mode_t what) {
  167. if (what & FOREGROUND) {
  168. drawHeader();
  169. drawArrows();
  170. drawSelectedFile();
  171. drawFooter();
  172. }
  173. }
  174. void FilesScreen::gotoPage(uint8_t page) {
  175. mydata.selected_tag = 0xFF;
  176. mydata.cur_page = page;
  177. CommandProcessor cmd;
  178. cmd.cmd(CMD_DLSTART)
  179. .cmd(CLEAR_COLOR_RGB(bg_color))
  180. .cmd(CLEAR(true,true,true))
  181. .colors(normal_btn);
  182. drawFileList();
  183. storeBackground();
  184. }
  185. bool FilesScreen::onTouchEnd(uint8_t tag) {
  186. switch (tag) {
  187. case 240: // Done button
  188. GOTO_PREVIOUS();
  189. return true;
  190. case 241: // Print highlighted file
  191. ConfirmStartPrintDialogBox::show(getSelectedFileIndex());
  192. return true;
  193. case 242: // Previous page
  194. if (mydata.cur_page > 0) {
  195. gotoPage(mydata.cur_page-1);
  196. }
  197. break;
  198. case 243: // Next page
  199. if (mydata.cur_page < (mydata.num_page-1)) {
  200. gotoPage(mydata.cur_page+1);
  201. }
  202. break;
  203. case 244: // Select directory
  204. {
  205. FileList files;
  206. files.changeDir(getSelectedShortFilename());
  207. gotoPage(0);
  208. }
  209. break;
  210. case 245: // Up directory
  211. {
  212. FileList files;
  213. files.upDir();
  214. gotoPage(0);
  215. }
  216. break;
  217. default: // File selected
  218. if (tag < 240) {
  219. mydata.selected_tag = tag;
  220. #if ENABLED(SCROLL_LONG_FILENAMES) && (FTDI_API_LEVEL >= 810)
  221. mydata.scroll_pos = 0;
  222. mydata.scroll_max = 0;
  223. if (FTDI::ftdi_chip >= 810) {
  224. const char *filename = getSelectedFilename();
  225. if (filename[0]) {
  226. CommandProcessor cmd;
  227. constexpr int dim[4] = {LIST_POS};
  228. const uint16_t text_width = cmd.font(font_medium).text_width(filename);
  229. if (text_width > dim[2])
  230. mydata.scroll_max = text_width - dim[2] + MARGIN_L + MARGIN_R + 10;
  231. }
  232. }
  233. #endif
  234. }
  235. break;
  236. }
  237. return true;
  238. }
  239. void FilesScreen::onIdle() {
  240. #if ENABLED(SCROLL_LONG_FILENAMES) && (FTDI_API_LEVEL >= 810)
  241. if (FTDI::ftdi_chip >= 810) {
  242. CLCD::mem_write_32(CLCD::REG::MACRO_0,
  243. VERTEX_TRANSLATE_X(-int32_t(mydata.scroll_pos)));
  244. if (mydata.scroll_pos < mydata.scroll_max * 16)
  245. mydata.scroll_pos++;
  246. }
  247. #endif
  248. }
  249. void FilesScreen::onMediaRemoved() {
  250. if (AT_SCREEN(FilesScreen)) GOTO_SCREEN(StatusScreen);
  251. }
  252. #endif // FTDI_FILES_SCREEN