raspberry pi zero based drum machine
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.

sEnterText.py 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import FSM
  2. import utils as u
  3. import menus as m
  4. import pygame
  5. from itertools import cycle
  6. from datetime import datetime
  7. from datetime import date
  8. #====================================
  9. State = type("State", (object,), {})
  10. #====================================
  11. class State(object):
  12. def __init__(self, FSM):
  13. self.FSM = FSM
  14. def Enter(self):
  15. pass
  16. def Execute(self):
  17. pass
  18. def Exit(self):
  19. pass
  20. class EnterText(State):
  21. def __init__(self,FSM):
  22. super(EnterText, self).__init__(FSM)
  23. def Enter(self):
  24. o = self.FSM.owner
  25. m.menu1_actions(self, o)
  26. if o.keyState[16] == 1:
  27. m.draw_menu1(o)
  28. o.update_display(0)
  29. elif o.keyState[16] == 4:
  30. self.draw(o)
  31. o.update_display(0)
  32. self.input_string = ""
  33. if o.sconf['title'] == 'blank':
  34. self.input_string += str(datetime.today().strftime('%Y%m%d'))
  35. else:
  36. self.input_string += o.sconf['title']
  37. o.header_text = "EnterText"
  38. o.pub.register("beat", self)
  39. self.last_action = pygame.time.get_ticks()
  40. self.caps = False
  41. self.cur_but = 18
  42. self.cur_letter = ""
  43. if o.keyState[16] > 0 or o.keyState[17] > 0:
  44. pass
  45. else:
  46. pass
  47. self.skips = [7, 3, 11, 12, 13, 14, 15]
  48. self.lc_labels = [["p", "q", "r", "s"], ["t", "u", "v"], ["w", "x", "y", "z"], ["C", "A", "P", "S"],
  49. ["g", "h", "i"], ["j", "k", "l"], ["m", "n", "o"], ["D", "E", "L"],
  50. ["_", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-"], ["a", "b", "c"], ["d", "e", "f"], ["O", "K"],
  51. [], [], [], []]
  52. self.uc_labels = [["P", "Q", "R", "S"], ["T", "U", "V"], ["W", "X", "Y", "Z"], ["C", "A", "P", "S"],
  53. ["G", "H", "I"], ["J", "K", "L"], ["M", "N", "O"], ["D", "E", "L"],
  54. ["_", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-"], ["A", "B", "C"], ["D", "E", "F"], ["O", "K"],
  55. [], [], [], []]
  56. self.labels = self.lc_labels
  57. self.cycles = []
  58. for l in self.labels:
  59. self.cycles.append(cycle(l))
  60. self.draw(o)
  61. o.update_display(0)
  62. super(EnterText, self).Enter()
  63. def Execute(self):
  64. o = self.FSM.owner
  65. y_size = o.height / 4
  66. if o.keyState[11] == 1:
  67. if self.input_string != 'blank':
  68. o.title = self.input_string
  69. o.sconf['title'] = self.input_string
  70. o.save_song()
  71. self.FSM.ToTransition('toFile')
  72. m.menu1_actions(self, o)
  73. m.menu2_actions_saveas(self, o)
  74. if o.keyState[16] == 1:
  75. m.draw_menu1(o)
  76. o.update_display(0)
  77. elif o.keyState[16] == 4:
  78. u.draw_header(o)
  79. self.draw(o)
  80. o.update_display(0)
  81. if o.keyState[17] == 1:
  82. m.draw_menu_saveas(o)
  83. o.update_display(0)
  84. elif o.keyState[17] == 4:
  85. u.draw_header(o)
  86. self.draw(o)
  87. o.update_display(0)
  88. # if o.keyState[0] == 1 or o.keyState[0] == 3 or o.keyState[1] == 1 or o.keyState[1] == 3 or o.keyState[2] == 1 or o.keyState[2] == 3 or o.keyState[3] == 1 or o.keyState[3] == 3:
  89. # u.draw_header(o)
  90. # self.draw(o)
  91. # o.update_display(0)
  92. if o.keyState[16] > 0 or o.keyState[17] > 0:
  93. pass
  94. else:
  95. _iter = 0
  96. for k in o.keyState:
  97. if k == 1:
  98. cur_time = pygame.time.get_ticks()
  99. if _iter not in self.skips:
  100. self.last_action = cur_time
  101. if _iter < len(self.cycles):
  102. self.cur_letter = next(self.cycles[_iter])
  103. self.cur_but = _iter
  104. self.draw(o)
  105. o.center_block(self.input_string + self.cur_letter, o.h2, [0, 0, o.width, y_size], o.light_grey)
  106. if _iter == 7:
  107. self.input_string = self.input_string[:-1]
  108. self.last_action = 100000000
  109. self.draw(o)
  110. o.center_block(self.input_string, o.h2, [0, 0, o.width, y_size], o.light_grey)
  111. elif _iter == 3:
  112. if self.caps:
  113. self.caps = False
  114. self.labels = self.lc_labels
  115. else:
  116. self.caps = True
  117. self.labels = self.uc_labels
  118. self.draw(o)
  119. o.center_block(self.input_string, o.h2, [0, 0, o.width, y_size], o.light_grey)
  120. self.cycles = []
  121. for l in self.labels:
  122. self.cycles.append(cycle(l))
  123. o.update_display(0)
  124. _iter += 1
  125. cur_time = pygame.time.get_ticks()
  126. delay = cur_time - self.last_action
  127. if delay > 1000 and delay < 100000:
  128. self.input_string += self.cur_letter
  129. self.last_action = 100000000
  130. self.reset_cycles()
  131. self.draw(o)
  132. o.center_block(self.input_string, o.h2, [0, 0, o.width, y_size], o.light_grey)
  133. o.update_display(0)
  134. def ReceiveMessage(self, message):
  135. o = self.FSM.owner
  136. u.play_seq(o, message)
  137. def draw(self, o):
  138. x_size = o.width / 4
  139. y_size = o.height / 4
  140. og_x = 0
  141. o_x = og_x
  142. o_y = o.height
  143. text_padding = 6
  144. _id = 0
  145. while _id < 16:
  146. lab = ""
  147. for i in self.labels[_id]:
  148. lab += i
  149. if _id == 8:
  150. lab = "_12"
  151. o.draw.rectangle((o_x, o_y, o_x + x_size, o_y - y_size), outline=0, fill=o.blue)
  152. o.center_block(lab, o.h2, [o_x, o_y, o_x + x_size, o_y - y_size], o.light_grey)
  153. o_x = o_x + x_size
  154. _id += 1
  155. if _id % 4 == 0:
  156. o_y -= y_size
  157. o_x = og_x
  158. o.draw.rectangle((0, 0, o.width, y_size), outline=0, fill=o.blue)
  159. def reset_cycles(self):
  160. self.cycles = []
  161. for l in self.labels:
  162. self.cycles.append(cycle(l))
  163. def Exit(self):
  164. self.FSM.owner.pub.unregister("beat", self)