subbrute_attack_view.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. #include "subbrute_attack_view.h"
  2. #include "../subbrute_i.h"
  3. #include "assets_icons.h"
  4. #include <input/input.h>
  5. #include <gui/elements.h>
  6. #include <gui/icon_i.h>
  7. #include <gui/icon_animation_i.h>
  8. #define TAG "SubBruteAttackView"
  9. struct SubBruteAttackView {
  10. View* view;
  11. SubBruteAttackViewCallback callback;
  12. void* context;
  13. };
  14. typedef struct {
  15. SubBruteAttacks index;
  16. uint64_t max_value;
  17. uint64_t current_step;
  18. bool is_attacking;
  19. IconAnimation* icon;
  20. } SubBruteAttackViewModel;
  21. void subbrute_attack_view_set_callback(
  22. SubBruteAttackView* instance,
  23. SubBruteAttackViewCallback callback,
  24. void* context) {
  25. furi_assert(instance);
  26. furi_assert(callback);
  27. instance->callback = callback;
  28. instance->context = context;
  29. }
  30. bool subbrute_attack_view_input(InputEvent* event, void* context) {
  31. furi_assert(event);
  32. furi_assert(context);
  33. #ifdef FURI_DEBUG
  34. FURI_LOG_D(TAG, "InputKey: %d", event->key);
  35. #endif
  36. SubBruteAttackView* instance = context;
  37. if(event->key == InputKeyBack && event->type == InputTypeShort) {
  38. instance->callback(SubBruteCustomEventTypeBackPressed, instance->context);
  39. with_view_model(
  40. instance->view, (SubBruteAttackViewModel * model) {
  41. model->is_attacking = false;
  42. return true;
  43. });
  44. return true;
  45. }
  46. bool is_attacking = false;
  47. with_view_model(
  48. instance->view, (SubBruteAttackViewModel * model) {
  49. is_attacking = model->is_attacking;
  50. return false;
  51. });
  52. // if(!is_attacking) {
  53. // instance->callback(SubBruteCustomEventTypeTransmitNotStarted, instance->context);
  54. // } else {
  55. // instance->callback(SubBruteCustomEventTypeTransmitStarted, instance->context);
  56. // }
  57. if(!is_attacking) {
  58. if((event->type == InputTypeShort || event->type == InputTypeRepeat) &&
  59. event->key == InputKeyOk) {
  60. #ifdef FURI_DEBUG
  61. FURI_LOG_D(TAG, "InputKey: %d OK", event->key);
  62. #endif
  63. with_view_model(
  64. instance->view, (SubBruteAttackViewModel * model) {
  65. model->is_attacking = true;
  66. icon_animation_stop(model->icon);
  67. icon_animation_start(model->icon);
  68. return true;
  69. });
  70. instance->callback(SubBruteCustomEventTypeTransmitStarted, instance->context);
  71. // } else if(event->key == InputKeyBack) {
  72. // if(previous_scene == SubBruteSceneLoadFile) {
  73. // instance->callback(SubBruteCustomEventTypeLoadFile, instance->context);
  74. // } else {
  75. // instance->callback(SubBruteCustomEventTypeBackPressed, instance->context);
  76. // }
  77. } else if(event->key == InputKeyUp) {
  78. instance->callback(SubBruteCustomEventTypeSaveFile, instance->context);
  79. } else if(event->key == InputKeyDown) {
  80. instance->callback(SubBruteCustomEventTypeTransmitCustom, instance->context);
  81. } else if(event->type == InputTypeShort) {
  82. if(event->key == InputKeyLeft) {
  83. instance->callback(SubBruteCustomEventTypeChangeStepDown, instance->context);
  84. } else if(event->key == InputKeyRight) {
  85. instance->callback(SubBruteCustomEventTypeChangeStepUp, instance->context);
  86. }
  87. // with_view_model(
  88. // instance->view, (SubBruteAttackViewModel * model) {
  89. // if(event->key == InputKeyLeft) {
  90. // model->current_step =
  91. // ((model->current_step - 1) + model->max_value) % model->max_value;
  92. // } else if(event->key == InputKeyRight) {
  93. // model->current_step = (model->current_step + 1) % model->max_value;
  94. // }
  95. // return true;
  96. // });
  97. // instance->callback(SubBruteCustomEventTypeChangeStep, instance->context);
  98. } else if(event->type == InputTypeRepeat) {
  99. if(event->key == InputKeyLeft) {
  100. instance->callback(SubBruteCustomEventTypeChangeStepDownMore, instance->context);
  101. } else if(event->key == InputKeyRight) {
  102. instance->callback(SubBruteCustomEventTypeChangeStepUpMore, instance->context);
  103. }
  104. /*with_view_model(
  105. instance->view, (SubBruteAttackViewModel * model) {
  106. if(event->key == InputKeyLeft) {
  107. model->current_step =
  108. ((model->current_step - 100) + model->max_value) % model->max_value;
  109. } else if(event->key == InputKeyRight) {
  110. model->current_step = (model->current_step + 100) % model->max_value;
  111. }
  112. return true;
  113. });
  114. instance->callback(SubBruteCustomEventTypeChangeStep, instance->context);*/
  115. }
  116. } else {
  117. if((event->type == InputTypeShort || event->type == InputTypeRepeat) &&
  118. (event->key == InputKeyOk || event->key == InputKeyBack)) {
  119. with_view_model(
  120. instance->view, (SubBruteAttackViewModel * model) {
  121. model->is_attacking = false;
  122. icon_animation_stop(model->icon);
  123. icon_animation_start(model->icon);
  124. return true;
  125. });
  126. instance->callback(SubBruteCustomEventTypeTransmitNotStarted, instance->context);
  127. }
  128. }
  129. return true;
  130. }
  131. SubBruteAttackView* subbrute_attack_view_alloc() {
  132. SubBruteAttackView* instance = malloc(sizeof(SubBruteAttackView));
  133. instance->view = view_alloc();
  134. view_allocate_model(instance->view, ViewModelTypeLocking, sizeof(SubBruteAttackViewModel));
  135. view_set_context(instance->view, instance);
  136. with_view_model(
  137. instance->view, (SubBruteAttackViewModel * model) {
  138. model->icon = icon_animation_alloc(&A_Sub1ghz_14);
  139. view_tie_icon_animation(instance->view, model->icon);
  140. return false;
  141. });
  142. view_set_draw_callback(instance->view, (ViewDrawCallback)subbrute_attack_view_draw);
  143. view_set_input_callback(instance->view, subbrute_attack_view_input);
  144. view_set_enter_callback(instance->view, subbrute_attack_view_enter);
  145. view_set_exit_callback(instance->view, subbrute_attack_view_exit);
  146. return instance;
  147. }
  148. void subbrute_attack_view_enter(void* context) {
  149. furi_assert(context);
  150. #ifdef FURI_DEBUG
  151. FURI_LOG_D(TAG, "subbrute_attack_view_enter");
  152. #endif
  153. }
  154. void subbrute_attack_view_free(SubBruteAttackView* instance) {
  155. furi_assert(instance);
  156. #ifdef FURI_DEBUG
  157. FURI_LOG_D(TAG, "subbrute_attack_view_free");
  158. #endif
  159. with_view_model(
  160. instance->view, (SubBruteAttackViewModel * model) {
  161. icon_animation_free(model->icon);
  162. return false;
  163. });
  164. view_free(instance->view);
  165. free(instance);
  166. }
  167. View* subbrute_attack_view_get_view(SubBruteAttackView* instance) {
  168. furi_assert(instance);
  169. return instance->view;
  170. }
  171. void subbrute_attack_view_set_current_step(SubBruteAttackView* instance, uint64_t current_step) {
  172. furi_assert(instance);
  173. #ifdef FURI_DEBUG
  174. FURI_LOG_D(TAG, "Set step: %d", current_step);
  175. #endif
  176. with_view_model(
  177. instance->view, (SubBruteAttackViewModel * model) {
  178. model->current_step = current_step;
  179. return true;
  180. });
  181. }
  182. uint64_t subbrute_attack_view_get_current_step(SubBruteAttackView* instance) {
  183. uint64_t current_step;
  184. with_view_model(
  185. instance->view, (SubBruteAttackViewModel * model) {
  186. current_step = model->current_step;
  187. return false;
  188. });
  189. #ifdef FURI_DEBUG
  190. FURI_LOG_D(TAG, "Get step: %d", current_step);
  191. #endif
  192. return current_step;
  193. }
  194. // We need to call init every time, because not every time we calls enter
  195. // normally, call enter only once
  196. void subbrute_attack_view_init_values(
  197. SubBruteAttackView* instance,
  198. uint8_t index,
  199. uint64_t max_value,
  200. uint64_t current_step,
  201. bool is_attacking) {
  202. #ifdef FURI_DEBUG
  203. FURI_LOG_D(
  204. TAG, "init, index: %d, max_value: %d, current_step: %d", index, max_value, current_step);
  205. #endif
  206. with_view_model(
  207. instance->view, (SubBruteAttackViewModel * model) {
  208. model->max_value = max_value;
  209. model->index = index;
  210. model->current_step = current_step;
  211. model->is_attacking = is_attacking;
  212. if(is_attacking) {
  213. icon_animation_start(model->icon);
  214. } else {
  215. icon_animation_stop(model->icon);
  216. }
  217. return true;
  218. });
  219. }
  220. void subbrute_attack_view_exit(void* context) {
  221. furi_assert(context);
  222. SubBruteAttackView* instance = context;
  223. #ifdef FURI_DEBUG
  224. FURI_LOG_D(TAG, "subbrute_attack_view_exit");
  225. #endif
  226. with_view_model(
  227. instance->view, (SubBruteAttackViewModel * model) {
  228. icon_animation_stop(model->icon);
  229. return false;
  230. });
  231. }
  232. void elements_button_top_left(Canvas* canvas, const char* str) {
  233. const Icon* icon = &I_ButtonUp_7x4;
  234. const uint8_t button_height = 12;
  235. const uint8_t vertical_offset = 9; //
  236. const uint8_t horizontal_offset = 3;
  237. const uint8_t string_width = canvas_string_width(canvas, str);
  238. const uint8_t icon_h_offset = 3;
  239. const uint8_t icon_width_with_offset = icon->width + icon_h_offset;
  240. const uint8_t icon_v_offset = icon->height; //
  241. const uint8_t button_width = string_width + horizontal_offset * 2 + icon_width_with_offset + 1;
  242. const uint8_t x = 0;
  243. const uint8_t y = 0;
  244. canvas_draw_box(canvas, x, y, button_width, button_height);
  245. #ifdef FURI_DEBUG
  246. FURI_LOG_D(
  247. TAG, "lbox, x: %d, y: %d, width: %d, height: %d", x, y, button_width, button_height);
  248. #endif
  249. // canvas_draw_line(canvas, x + button_width + 0, y, x + button_width + 0, y + button_height - 0); //
  250. // canvas_draw_line(canvas, x + button_width + 1, y, x + button_width + 1, y + button_height - 1);
  251. // canvas_draw_line(canvas, x + button_width + 2, y, x + button_width + 2, y + button_height - 2);
  252. canvas_invert_color(canvas);
  253. canvas_draw_icon(canvas, x + horizontal_offset, y + icon_v_offset, icon);
  254. canvas_draw_str(
  255. canvas, x + horizontal_offset + icon_width_with_offset, y + vertical_offset, str);
  256. canvas_invert_color(canvas);
  257. }
  258. void elements_button_top_right(Canvas* canvas, const char* str) {
  259. const Icon* icon = &I_ButtonDown_7x4;
  260. const uint8_t button_height = 12;
  261. const uint8_t vertical_offset = 9;
  262. const uint8_t horizontal_offset = 3;
  263. const uint8_t string_width = canvas_string_width(canvas, str);
  264. const uint8_t icon_h_offset = 3;
  265. const uint8_t icon_width_with_offset = icon->width + icon_h_offset;
  266. const uint8_t icon_v_offset = icon->height; // + vertical_offset;
  267. const uint8_t button_width = string_width + horizontal_offset * 2 + icon_width_with_offset + 1;
  268. const uint8_t x = canvas_width(canvas);
  269. const uint8_t y = 0;
  270. canvas_draw_box(canvas, x - button_width, y, button_width, button_height);
  271. #ifdef FURI_DEBUG
  272. FURI_LOG_D(
  273. TAG,
  274. "rbox, x: %d, y: %d, width: %d, height: %d",
  275. x - button_width,
  276. y,
  277. button_width,
  278. button_height);
  279. #endif
  280. // canvas_draw_line(canvas, x - button_width - 1, y, x + button_width - 1, y + button_height - 0);
  281. // canvas_draw_line(canvas, x - button_width - 2, y, x + button_width - 2, y + button_height - 1);
  282. // canvas_draw_line(canvas, x - button_width - 3, y, x + button_width - 3, y + button_height - 2);
  283. canvas_invert_color(canvas);
  284. canvas_draw_str(canvas, x - button_width + horizontal_offset, y + vertical_offset, str);
  285. canvas_draw_icon(canvas, x - horizontal_offset - icon->width, y + icon_v_offset, icon);
  286. canvas_invert_color(canvas);
  287. }
  288. void subbrute_attack_view_draw(Canvas* canvas, void* context) {
  289. furi_assert(context);
  290. SubBruteAttackViewModel* model = (SubBruteAttackViewModel*)context;
  291. char buffer[26];
  292. const char* attack_name = NULL;
  293. attack_name = subbrute_get_menu_name(model->index);
  294. // Title
  295. if(model->is_attacking) {
  296. canvas_set_color(canvas, ColorBlack);
  297. canvas_set_font(canvas, FontSecondary);
  298. canvas_draw_str_aligned(canvas, 64, 2, AlignCenter, AlignTop, attack_name);
  299. }
  300. // Value
  301. canvas_set_font(canvas, FontBigNumbers);
  302. snprintf(buffer, sizeof(buffer), "%04d/%04d", (int)model->current_step, (int)model->max_value);
  303. canvas_draw_str_aligned(canvas, 64, 17, AlignCenter, AlignTop, buffer);
  304. canvas_set_font(canvas, FontSecondary);
  305. if(!model->is_attacking) {
  306. canvas_set_color(canvas, ColorBlack);
  307. canvas_set_font(canvas, FontSecondary);
  308. canvas_draw_str_aligned(canvas, 64, 44, AlignCenter, AlignBottom, attack_name);
  309. elements_button_left(canvas, "-1");
  310. elements_button_right(canvas, "+1");
  311. elements_button_center(canvas, "Start");
  312. elements_button_top_left(canvas, "Save");
  313. elements_button_top_right(canvas, "Resend");
  314. } else {
  315. // canvas_draw_icon_animation
  316. const uint8_t icon_h_offset = 0;
  317. const uint8_t icon_width_with_offset = model->icon->icon->width + icon_h_offset;
  318. const uint8_t icon_v_offset = model->icon->icon->height; // + vertical_offset;
  319. const uint8_t x = canvas_width(canvas);
  320. const uint8_t y = canvas_height(canvas);
  321. canvas_draw_icon_animation(
  322. canvas, x - icon_width_with_offset, y - icon_v_offset, model->icon);
  323. // Progress bar
  324. // Resolution: 128x64 px
  325. float progress_value = (float)model->current_step / model->max_value;
  326. elements_progress_bar(canvas, 8, 37, 110, progress_value > 1 ? 1 : progress_value);
  327. elements_button_center(canvas, "Stop");
  328. }
  329. }