scene_edit.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #include "flipper.h"
  2. #include "resistors_app.h"
  3. #include "resistor_logic.h"
  4. #include "app_state.h"
  5. #include "scenes.h"
  6. #include "scene_edit.h"
  7. #include <applications/services/gui/modules/widget.h>
  8. #include <applications/services/gui/modules/widget_elements/widget_element.h>
  9. #include <applications/services/gui/view.h>
  10. const int values_left = 64;
  11. const int rows_tops[] = {0, 9, 18};
  12. const int footer_top = 55;
  13. const int bands_top = 30;
  14. const int bands_lefts[] = {18, 34, 50, 66, 86, 102};
  15. const int band_indices[6][6] =
  16. {{}, {}, {0, 1, 2}, {0, 1, 2, 5}, {0, 1, 2, 3, 5}, {0, 1, 2, 3, 4, 5}};
  17. const int band_w = 8;
  18. const int band_h = 22;
  19. void resistors_edit_view_redraw_widget(App* app) {
  20. widget_reset(app->widget);
  21. Icon* icon;
  22. switch(app->state->resistor_type) {
  23. case R3:
  24. icon = (Icon*)&I_r3;
  25. break;
  26. case R4:
  27. icon = (Icon*)&I_r4;
  28. break;
  29. case R5:
  30. icon = (Icon*)&I_r5;
  31. break;
  32. case R6:
  33. icon = (Icon*)&I_r6;
  34. break;
  35. default:
  36. FURI_LOG_E(TAG, "Unrecognised resistor type in resistors_edit_view_redraw_widget");
  37. app_quit(app);
  38. return;
  39. }
  40. // render resistor graphic
  41. widget_add_icon_element(app->widget, 0, 0, icon);
  42. // render band indicator
  43. if(app->state->edit_selection < app->state->resistor_type) {
  44. int band_index = band_indices[app->state->resistor_type - 1][app->state->edit_selection];
  45. widget_add_icon_element(app->widget, bands_lefts[band_index], bands_top, &I_box_8x22);
  46. }
  47. // render band colour descriptors (short)
  48. for(int i = 0; i < app->state->resistor_type; i++) {
  49. int description_index = band_indices[app->state->resistor_type - 1][i];
  50. int description_left = bands_lefts[description_index];
  51. widget_add_string_element(
  52. app->widget,
  53. description_left,
  54. footer_top,
  55. AlignLeft,
  56. AlignTop,
  57. FontPrimary,
  58. get_colour_short_description(app->state->resistor_bands[i]));
  59. }
  60. // render calculation
  61. char calculation[CHARS_CALCULATION + 1];
  62. update_resistance_calculation(
  63. app->state->resistor_type, app->state->resistor_bands, calculation);
  64. widget_add_string_element(
  65. app->widget, values_left, rows_tops[0], AlignLeft, AlignTop, FontSecondary, calculation);
  66. // update_tolerance
  67. if(has_tolerance(app->state->resistor_type)) {
  68. char tolerance[CHARS_TOLERANCE + 1];
  69. update_resistance_tolerance(
  70. app->state->resistor_type, app->state->resistor_bands, tolerance);
  71. widget_add_string_element(
  72. app->widget, values_left, rows_tops[1], AlignLeft, AlignTop, FontSecondary, tolerance);
  73. }
  74. // update_temp_coeff
  75. if(has_temp_coeff(app->state->resistor_type)) {
  76. char temp_coeff[CHARS_TEMP_COEFF + 1];
  77. update_resistance_temp_coeff(
  78. app->state->resistor_type, app->state->resistor_bands, temp_coeff);
  79. widget_add_string_element(
  80. app->widget, values_left, rows_tops[2], AlignLeft, AlignTop, FontSecondary, temp_coeff);
  81. }
  82. }
  83. /** main menu events */
  84. static bool widget_input_callback(InputEvent* input_event, void* context) {
  85. App* app = context;
  86. bool consumed = false;
  87. if(input_event->type == InputTypeShort) {
  88. switch(input_event->key) {
  89. case InputKeyRight:
  90. if(app->state->edit_selection < app->state->resistor_type - 1) {
  91. app->state->edit_selection += 1;
  92. }
  93. consumed = true;
  94. break;
  95. case InputKeyLeft:
  96. if(app->state->edit_selection > 0) {
  97. app->state->edit_selection -= 1;
  98. }
  99. consumed = true;
  100. break;
  101. case InputKeyUp:
  102. app->state->resistor_bands[app->state->edit_selection] = alter_resistor_band(
  103. app->state->resistor_type,
  104. app->state->edit_selection,
  105. app->state->resistor_bands[app->state->edit_selection],
  106. 1);
  107. consumed = true;
  108. break;
  109. case InputKeyDown:
  110. app->state->resistor_bands[app->state->edit_selection] = alter_resistor_band(
  111. app->state->resistor_type,
  112. app->state->edit_selection,
  113. app->state->resistor_bands[app->state->edit_selection],
  114. -1);
  115. consumed = true;
  116. break;
  117. default:
  118. consumed = false;
  119. break;
  120. }
  121. }
  122. if(consumed) resistors_edit_view_redraw_widget(app);
  123. return consumed;
  124. }
  125. /** edit view scene - resets the widget, and gives it content, callbacks and selection enums */
  126. void resistors_edit_scene_on_enter(void* context) {
  127. App* app = context;
  128. resistors_edit_view_redraw_widget(app);
  129. view_set_context(widget_get_view(app->widget), app);
  130. view_set_input_callback(widget_get_view(app->widget), widget_input_callback);
  131. view_dispatcher_switch_to_view(app->view_dispatcher, ResistorsEditView);
  132. }
  133. /** edit view event handler - switches scene based on the event */
  134. bool resistors_edit_scene_on_event(void* context, SceneManagerEvent event) {
  135. App* app = context;
  136. bool consumed = false;
  137. UNUSED(app);
  138. UNUSED(event);
  139. // if(event.type == SceneManagerEventTypeCustom && event.event == ResistorsEditViewUpdateEvent) {
  140. // ...
  141. // consumed = true;
  142. // }
  143. return consumed;
  144. }
  145. void resistors_edit_scene_on_exit(void* context) {
  146. App* app = context;
  147. widget_reset(app->widget);
  148. }