songinfo.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #include "songinfo.h"
  2. void return_from_keyboard_callback(void *ctx)
  3. {
  4. FlizzerTrackerApp *tracker = (FlizzerTrackerApp *)ctx;
  5. uint8_t string_length = 0;
  6. char *string = NULL;
  7. if (tracker->focus == EDIT_SONGINFO && tracker->mode == PATTERN_VIEW)
  8. {
  9. switch (tracker->selected_param)
  10. {
  11. case SI_SONGNAME:
  12. {
  13. string_length = MUS_SONG_NAME_LEN;
  14. string = (char *)&tracker->song.song_name;
  15. break;
  16. }
  17. case SI_INSTRUMENTNAME:
  18. {
  19. string_length = MUS_INST_NAME_LEN;
  20. string = (char *)&tracker->song.instrument[tracker->current_instrument]->name;
  21. break;
  22. }
  23. }
  24. }
  25. if (tracker->focus == EDIT_INSTRUMENT && tracker->mode == INST_EDITOR_VIEW)
  26. {
  27. switch (tracker->selected_param)
  28. {
  29. case INST_INSTRUMENTNAME:
  30. {
  31. string_length = MUS_INST_NAME_LEN;
  32. string = (char *)&tracker->song.instrument[tracker->current_instrument]->name;
  33. break;
  34. }
  35. }
  36. }
  37. if (string == NULL || string_length == 0)
  38. return;
  39. for (uint8_t i = 0; i < string_length; i++) // I tinyfied the font by deleting lowercase chars, and I don't like the lowercase chars of any 3x5 pixels font
  40. {
  41. string[i] = toupper(string[i]);
  42. }
  43. view_dispatcher_switch_to_view(tracker->view_dispatcher, VIEW_TRACKER);
  44. }
  45. void edit_songinfo_param(FlizzerTrackerApp *tracker, uint8_t selected_param, int8_t delta)
  46. {
  47. if (!(tracker->current_digit))
  48. {
  49. delta *= 16;
  50. }
  51. switch (selected_param)
  52. {
  53. case SI_PATTERNPOS:
  54. {
  55. if ((int16_t)tracker->song.pattern_length + (int16_t)delta > 1 && (int16_t)tracker->song.pattern_length + (int16_t)delta <= 0xff)
  56. {
  57. tracker->song.pattern_length += delta;
  58. }
  59. break;
  60. }
  61. case SI_SEQUENCEPOS:
  62. {
  63. if ((int16_t)tracker->song.num_sequence_steps + (int16_t)delta > 1 && (int16_t)tracker->song.num_sequence_steps + (int16_t)delta <= 0xff)
  64. {
  65. tracker->song.num_sequence_steps += delta;
  66. }
  67. break;
  68. }
  69. case SI_SONGSPEED:
  70. {
  71. if ((int16_t)tracker->song.speed + (int16_t)delta > 1 && (int16_t)tracker->song.speed + (int16_t)delta <= 0xff)
  72. {
  73. tracker->song.speed += delta;
  74. }
  75. break;
  76. }
  77. case SI_SONGRATE:
  78. {
  79. if ((int16_t)tracker->song.rate + (int16_t)delta > 1 && (int16_t)tracker->song.rate + (int16_t)delta <= 0xff)
  80. {
  81. tracker->song.rate += delta;
  82. }
  83. break;
  84. }
  85. case SI_MASTERVOL:
  86. {
  87. if ((int16_t)tracker->tracker_engine.master_volume + (int16_t)delta > 0 && (int16_t)tracker->tracker_engine.master_volume + (int16_t)delta <= 0xff)
  88. {
  89. tracker->tracker_engine.master_volume += delta;
  90. }
  91. break;
  92. }
  93. case SI_SONGNAME:
  94. {
  95. text_input_set_header_text(tracker->text_input, "Song name:");
  96. text_input_set_result_callback(tracker->text_input, return_from_keyboard_callback, tracker, (char *)&tracker->song.song_name, MUS_SONG_NAME_LEN + 1, false);
  97. view_dispatcher_switch_to_view(tracker->view_dispatcher, VIEW_KEYBOARD);
  98. break;
  99. }
  100. case SI_CURRENTINSTRUMENT:
  101. {
  102. int16_t inst = tracker->current_instrument;
  103. if (inst + delta >= MUS_NOTE_INSTRUMENT_NONE)
  104. {
  105. if (delta > 0)
  106. {
  107. inst = 0;
  108. }
  109. else
  110. {
  111. inst = MUS_NOTE_INSTRUMENT_NONE - 1;
  112. }
  113. }
  114. clamp(inst, delta, 0, tracker->song.num_instruments - 1);
  115. tracker->current_instrument = inst;
  116. break;
  117. }
  118. case SI_INSTRUMENTNAME:
  119. {
  120. text_input_set_header_text(tracker->text_input, "Instrument name:");
  121. text_input_set_result_callback(tracker->text_input, return_from_keyboard_callback, tracker, (char *)&tracker->song.instrument[tracker->current_instrument]->name, MUS_INST_NAME_LEN + 1, false);
  122. view_dispatcher_switch_to_view(tracker->view_dispatcher, VIEW_KEYBOARD);
  123. break;
  124. }
  125. default:
  126. break;
  127. }
  128. }
  129. void songinfo_edit_event(FlizzerTrackerApp *tracker, FlizzerTrackerEvent *event)
  130. {
  131. if (event->input.key == InputKeyOk && event->input.type == InputTypeShort && !tracker->tracker_engine.playing)
  132. {
  133. tracker->editing = !tracker->editing;
  134. }
  135. if (event->input.key == InputKeyRight && event->input.type == InputTypeShort && tracker->editing)
  136. {
  137. switch (tracker->selected_param)
  138. {
  139. default:
  140. {
  141. tracker->current_digit++;
  142. if (tracker->current_digit > 1)
  143. {
  144. tracker->selected_param++;
  145. tracker->current_digit = 0;
  146. if (tracker->selected_param > SI_PARAMS - 1)
  147. {
  148. tracker->selected_param = 0;
  149. }
  150. }
  151. break;
  152. }
  153. case SI_CURRENTINSTRUMENT:
  154. case SI_SONGNAME:
  155. case SI_INSTRUMENTNAME:
  156. {
  157. tracker->selected_param++;
  158. tracker->current_digit = 0;
  159. if (tracker->selected_param > SI_PARAMS - 1)
  160. {
  161. tracker->selected_param = 0;
  162. }
  163. break;
  164. }
  165. }
  166. }
  167. if (event->input.key == InputKeyLeft && event->input.type == InputTypeShort && tracker->editing)
  168. {
  169. switch (tracker->selected_param)
  170. {
  171. default:
  172. {
  173. tracker->current_digit--;
  174. if (tracker->current_digit > 1) // unsigned int overflow
  175. {
  176. tracker->selected_param--;
  177. tracker->current_digit = 1;
  178. if (tracker->selected_param > SI_PARAMS - 1) // unsigned int overflow
  179. {
  180. tracker->selected_param = SI_PARAMS - 1;
  181. }
  182. }
  183. break;
  184. }
  185. case SI_CURRENTINSTRUMENT:
  186. case SI_SONGNAME:
  187. case SI_INSTRUMENTNAME:
  188. {
  189. tracker->selected_param--;
  190. tracker->current_digit = 0;
  191. if (tracker->selected_param > SI_PARAMS - 1) // unsigned int overflow
  192. {
  193. tracker->selected_param = SI_PARAMS - 1;
  194. }
  195. break;
  196. }
  197. }
  198. }
  199. if (event->input.key == InputKeyDown && event->input.type == InputTypeShort)
  200. {
  201. if (tracker->editing)
  202. {
  203. edit_songinfo_param(tracker, tracker->selected_param, -1);
  204. }
  205. }
  206. if (event->input.key == InputKeyUp && event->input.type == InputTypeShort)
  207. {
  208. if (tracker->editing)
  209. {
  210. edit_songinfo_param(tracker, tracker->selected_param, 1);
  211. }
  212. }
  213. }