constants.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #include <gui/icon.h>
  2. #include "dice_app_icons.h"
  3. #define TAG "DiceApp"
  4. #define DICE_TYPES 8
  5. #define HISTORY_SIZE 10
  6. #define HISTORY_COL HISTORY_SIZE / 2
  7. #define HISTORY_START_POST_X 2
  8. #define HISTORY_START_POST_Y 10
  9. #define HISTORY_STEP_X 66
  10. #define HISTORY_STEP_Y 10
  11. #define HISTORY_X_GAP 11
  12. #define MAX_DICE_COUNT 10
  13. #define MAX_COIN_FRAMES 9
  14. #define MAX_DICE_FRAMES 4
  15. #define DICE_X 45
  16. #define DICE_Y 6
  17. #define DICE_Y_T 0
  18. #define DICE_GAP 44
  19. #define RESULT_BORDER_X 44
  20. #define RESULT_OFFSET 20
  21. #define SWIPE_DIST 11
  22. const Icon* coin_heads_start[] = {&I_coin_1, &I_coin_2};
  23. const Icon* coin_heads_end[] = {&I_coin_7, &I_coin_1};
  24. const Icon* coin_tails_start[] = {&I_coin_5, &I_coin_6};
  25. const Icon* coin_tails_end[] = {&I_coin_4, &I_coin_5};
  26. const Icon* coin_frames[] = {
  27. &I_coin_1,
  28. &I_coin_2,
  29. &I_coin_3,
  30. &I_coin_4,
  31. &I_coin_5,
  32. &I_coin_6,
  33. &I_coin_3,
  34. &I_coin_7,
  35. &I_coin_1,
  36. };
  37. const int8_t result_frame_pos_y[] = {-30, -20, -10, 0};
  38. const Icon* dice_frames[] = {
  39. &I_d4_1, &I_d4_2, &I_d4_3, &I_d4_1, // d4
  40. &I_d6_1, &I_d6_2, &I_d6_3, &I_d6_4, // d6
  41. &I_d8_1, &I_d8_2, &I_d8_3, &I_d8_4, // d8
  42. &I_d10_1, &I_d10_2, &I_d10_3, &I_d10_4, // d10
  43. &I_d12_1, &I_d12_2, &I_d12_3, &I_d12_4, // d12
  44. &I_d20_1, &I_d20_2, &I_d20_3, &I_d20_4, // d20
  45. &I_d100_1, &I_d100_2, &I_d100_3, &I_d100_4, // d100
  46. };
  47. const uint8_t screen_pos[] = {};
  48. typedef struct {
  49. uint8_t type;
  50. int x;
  51. int y;
  52. char* name;
  53. } Dice;
  54. typedef struct {
  55. int8_t index;
  56. uint8_t count;
  57. uint8_t result;
  58. } History;
  59. static const Dice dice_types[] = {
  60. {2, 0, 0, "Coin"},
  61. {4, 0, 0, "d4"},
  62. {6, 0, 0, "d6"},
  63. {8, 0, 0, "d8"},
  64. {10, 0, 0, "d10"},
  65. {12, 0, 0, "d12"},
  66. {20, 0, 0, "d20"},
  67. {100, 0, 0, "d100"},
  68. };
  69. typedef enum { EventTypeTick, EventTypeKey } EventType;
  70. typedef enum {
  71. SelectState,
  72. SwipeLeftState,
  73. SwipeRightState,
  74. AnimState,
  75. AnimResultState,
  76. ResultState,
  77. HistoryState,
  78. } AppState;
  79. typedef struct {
  80. EventType type;
  81. InputEvent input;
  82. } AppEvent;
  83. typedef struct {
  84. AppState app_state;
  85. uint16_t roll_result;
  86. uint8_t rolled_dices[MAX_DICE_COUNT];
  87. uint8_t anim_frame;
  88. uint8_t dice_index;
  89. uint8_t dice_count;
  90. int8_t result_pos;
  91. Dice dices[DICE_TYPES];
  92. History history[HISTORY_SIZE];
  93. FuriMutex* mutex;
  94. } State;
  95. void init(State* const state) {
  96. state->app_state = SelectState;
  97. state->roll_result = 0;
  98. state->dice_index = 0;
  99. state->anim_frame = 0;
  100. state->dice_count = 1;
  101. for(uint8_t i = 0; i < DICE_TYPES; i++) {
  102. state->dices[i] = dice_types[i];
  103. state->dices[i].x = DICE_X + (i * DICE_GAP);
  104. state->dices[i].y = i == 0 ? DICE_Y_T : DICE_Y;
  105. }
  106. for(uint8_t i = 0; i < HISTORY_SIZE; i++) {
  107. state->history[i].index = -1;
  108. }
  109. }
  110. void add_to_history(State* const state, uint8_t index, uint8_t count, uint8_t result) {
  111. uint8_t last = HISTORY_SIZE - 1;
  112. if(state->history[last].index >= 0) {
  113. for(uint8_t i = 1; i < HISTORY_SIZE; i++) {
  114. state->history[i - 1] = state->history[i];
  115. }
  116. state->history[last].index = index;
  117. state->history[last].count = count;
  118. state->history[last].result = result;
  119. return;
  120. }
  121. for(uint8_t i = 0; i < HISTORY_SIZE; i++) {
  122. if(state->history[i].index < 0) {
  123. state->history[i].index = index;
  124. state->history[i].count = count;
  125. state->history[i].result = result;
  126. return;
  127. }
  128. }
  129. }
  130. void coin_set_start(uint16_t type) {
  131. if(type == 1) {
  132. coin_frames[0] = coin_heads_start[0];
  133. coin_frames[1] = coin_heads_start[1];
  134. } else {
  135. coin_frames[0] = coin_tails_start[0];
  136. coin_frames[1] = coin_tails_start[1];
  137. }
  138. }
  139. void coin_set_end(uint16_t type) {
  140. if(type == 1) {
  141. coin_frames[MAX_COIN_FRAMES - 2] = coin_heads_end[0];
  142. coin_frames[MAX_COIN_FRAMES - 1] = coin_heads_end[1];
  143. } else {
  144. coin_frames[MAX_COIN_FRAMES - 2] = coin_tails_end[0];
  145. coin_frames[MAX_COIN_FRAMES - 1] = coin_tails_end[1];
  146. }
  147. }
  148. bool isResultVisible(AppState state, uint8_t dice_index) {
  149. return (state == ResultState || state == AnimResultState) && dice_index != 0;
  150. }
  151. bool isDiceNameVisible(AppState state) {
  152. return state != SwipeLeftState && state != SwipeRightState;
  153. }
  154. bool isDiceButtonsVisible(AppState state) {
  155. return isDiceNameVisible(state) && state != AnimResultState && state != ResultState &&
  156. state != AnimState && state != HistoryState;
  157. }
  158. bool isOneDice(uint8_t dice_index) {
  159. return dice_index == 0 || dice_index == 7;
  160. }
  161. bool isDiceSettingsDisabled(AppState state, uint8_t dice_index) {
  162. return isOneDice(dice_index) || state == ResultState || state == AnimResultState ||
  163. state == AnimState || state == HistoryState;
  164. }
  165. bool isAnimState(AppState state) {
  166. return state == SwipeLeftState || state == SwipeRightState || state == AnimResultState ||
  167. state == AnimState;
  168. }
  169. bool isMenuState(AppState state) {
  170. return state == SwipeLeftState || state == SwipeRightState || state == SelectState;
  171. }