Advertisement
Guest User

Untitled

a guest
May 30th, 2015
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.99 KB | None | 0 0
  1. --[[OpenMatrix v0.7.1 by Karasuro]]--
  2.  
  3. -- Requirements
  4. local computer = require "computer";
  5. local component = require "component";
  6. local term = require "term";
  7. local event = require "event";
  8. local gpu = component.gpu;
  9. local source = component.note_block;
  10. local note = require "note";
  11. local keyboard = require "keyboard";
  12. local color = require "colors";
  13.  
  14. -- Variables and Tables
  15. term.clear();
  16. local maxX, maxY = gpu.getResolution();
  17.  
  18. if (maxX ~= 80) then
  19. if (maxY ~= 25) then
  20. gpu.setResolution(80, 25);
  21. end
  22. end
  23.  
  24. local w, h = gpu.getResolution();
  25. local UI = {bg=0x000000, fg=0xFFFFFF, txt=0x000000, width=w, height=h};
  26. local UI_btn = {on=0xFFFFFF, off=0xFFAA00, play=0xFFFF55};
  27. local UI_menu = {default=0xFFAA00, hit=0xFFFF55};
  28. local buttons = {};
  29.  
  30. for Y = 2,16 do
  31. local row = {};
  32. table.insert(buttons, row);
  33. for X = 2,47,3 do
  34. if (Y == 2) then
  35. table.insert(row, {state=3, posX=X, posY=Y, pitch=25});
  36. elseif (Y == 3) then
  37. table.insert(row, {state=3, posX=X, posY=Y, pitch=24});
  38. elseif (Y == 4) then
  39. table.insert(row, {state=3, posX=X, posY=Y, pitch=22});
  40. elseif (Y == 5) then
  41. table.insert(row, {state=3, posX=X, posY=Y, pitch=20});
  42. elseif (Y == 6) then
  43. table.insert(row, {state=3, posX=X, posY=Y, pitch=18});
  44. elseif (Y == 7) then
  45. table.insert(row, {state=3, posX=X, posY=Y, pitch=17});
  46. elseif (Y == 8) then
  47. table.insert(row, {state=3, posX=X, posY=Y, pitch=15});
  48. elseif (Y == 9) then
  49. table.insert(row, {state=3, posX=X, posY=Y, pitch=13});
  50. elseif (Y == 10) then
  51. table.insert(row, {state=3, posX=X, posY=Y, pitch=12});
  52. elseif (Y == 11) then
  53. table.insert(row, {state=3, posX=X, posY=Y, pitch=10});
  54. elseif (Y == 12) then
  55. table.insert(row, {state=3, posX=X, posY=Y, pitch=8});
  56. elseif (Y == 13) then
  57. table.insert(row, {state=3, posX=X, posY=Y, pitch=6});
  58. elseif (Y == 14) then
  59. table.insert(row, {state=3, posX=X, posY=Y, pitch=5});
  60. elseif (Y == 15) then
  61. table.insert(row, {state=3, posX=X, posY=Y, pitch=3});
  62. else
  63. table.insert(row, {state=3, posX=X, posY=Y, pitch=1})
  64. end
  65. end
  66. end
  67.  
  68. local printDebug = false;
  69. local mX = 0;
  70. local mY = 0;
  71. local pitch = 1;
  72. local playing = false;
  73. local tick = 0;
  74. local beat = 16;
  75. local tempo = 40;
  76. local cycle = 0;
  77. local dt = 0;
  78.  
  79.  
  80.  
  81. local menu = {
  82. {id="play", posX=2, posY=(h - 1), state=1, label="Play", width=6, height=1}
  83. };
  84. local header = {posX=47,posY=18,width=2,height=1,col=0x00AA00};
  85.  
  86. local myEventHandlers = setmetatable({}, {__index = function() return unknownEvent end});
  87.  
  88. -- Functions
  89. function unknownEvent()
  90. -- Do Nothing!
  91. end
  92.  
  93. function myEventHandlers.key_up(adress, char, code, playerName)
  94. -- Space Bar
  95. if (code == 0x39) then
  96. playing = not playing;
  97. end
  98.  
  99. -- Backspace
  100. if (code == 0x0E) then
  101. gpu.setResolution(maxX, maxY);
  102. term.clear();
  103. os.exit();
  104. end
  105.  
  106. -- Grave or ~
  107. if (code == 0x29) then
  108. printDebug = not printDebug;
  109. end
  110. end
  111.  
  112. function myEventHandlers.touch(screenAdress, x, y, button, playerName)
  113. mX = x;
  114. mY = y;
  115. if (button == 0) then
  116. -- Buttons Table Handling
  117. for r in pairs(buttons) do
  118. for c in pairs(buttons[r]) do
  119. if (mX >= buttons[r][c].posX) and (mX <= (buttons[r][c].posX + 2)) and (mY == buttons[r][c].posY) then
  120. if (buttons[r][c].state == 3) then
  121. buttons[r][c].state = 1;
  122. else
  123. buttons[r][c].state = 3;
  124. end
  125. end
  126. end
  127. end
  128.  
  129. -- Menu Table Handling
  130. for i in pairs(menu) do
  131. if (mX >= menu[i].posX) and (mX <= (menu[i].posX + menu[i].width)) and (mY == menu[i].posY) then
  132. --play/stop button
  133. if menu[i].id == "play" then
  134. playing = not playing;
  135. menu[i].label = "Stop";
  136. end
  137. end
  138. end
  139. end
  140. end
  141.  
  142. function handleEvent(eventID, ...)
  143. if (eventID) then
  144. myEventHandlers[eventID](...);
  145. end
  146. end
  147.  
  148. function playTone(pitch)
  149. if (source ~= nil) then
  150. source.trigger(pitch);
  151. else
  152. computer.beep(pitch);
  153. end
  154. end
  155.  
  156. --Draw Instructions ONCE
  157. gpu.setBackground(UI.bg); --Black
  158. gpu.setForeground(UI.fg); --White
  159. gpu.set(2,20,"~ = debug info | space = play/stop | backspace = exit");
  160. gpu.set(2,21,"Multiple notes on each column = more processing time.")
  161.  
  162. -- Main Loop
  163. while true do
  164.  
  165. --[[UPDATE DATA]]--
  166. if playing then
  167. -- Update UI
  168. menu[1].label = "Stop";
  169.  
  170. -- Start ticking
  171. tick = tick + (tempo * 0.1);
  172. if tick > 1 then
  173. header.posX = header.posX + 3;
  174. if header.posX > 47 then
  175. header.posX = 2;
  176. end
  177. tick = 0;
  178. beat = beat + 1;
  179. end
  180.  
  181. if (beat > 16) then
  182. beat = 1;
  183. end
  184.  
  185. -- Get Active Notes
  186. for r in pairs(buttons) do
  187. for c in pairs(buttons[r]) do
  188. if (buttons[r][c].posX == header.posX) then
  189. if (buttons[r][c].state == 1) then
  190. buttons[r][c].state = 2;
  191. pitch = buttons[r][c].pitch;
  192. playTone(pitch);
  193. end
  194. else
  195. if (buttons[r][c].state == 2) then
  196. buttons[r][c].state = 1;
  197. end
  198. end
  199. end
  200. end
  201. else
  202. -- Reset UI
  203. header.posX = 47;
  204. tick = 0;
  205. beat = 16;
  206. menu[1].label = "Play";
  207. for r in pairs(buttons) do
  208. for c in pairs(buttons[r]) do
  209. if (buttons[r][c].state == 2) or (buttons[r][c].state == 1) then
  210. buttons[r][c].state = 1;
  211. else
  212. buttons[r][c].state = 3;
  213. end
  214. end
  215. end
  216. end
  217.  
  218. --[[DRAW STUFF]]--
  219. -- Draw Buttons and their states
  220. for r in pairs(buttons) do
  221. for c in pairs(buttons[r]) do
  222. if (buttons[r][c].state == 3) then
  223. --Draw Button OFF
  224. gpu.setBackground(UI_btn.off); --Orange
  225. gpu.fill(buttons[r][c].posX,buttons[r][c].posY,2,1," ");
  226. gpu.setBackground(UI.bg); --Black
  227. end
  228.  
  229. if (buttons[r][c].state == 2) then
  230. -- Draw Buttons ON
  231. gpu.setBackground(UI_btn.on); --White
  232. gpu.fill(buttons[r][c].posX,buttons[r][c].posY,2,1," ");
  233. gpu.setBackground(UI.bg); --Black
  234. end
  235.  
  236. if (buttons[r][c].state == 1) then
  237. -- Draw Buttons PLAY
  238. gpu.setBackground(UI_btn.play); --Yellow
  239. gpu.fill(buttons[r][c].posX,buttons[r][c].posY,2,1," ");
  240. gpu.setBackground(UI.bg); --Black
  241. end
  242. end
  243. end
  244.  
  245. -- Draw Play Head
  246. gpu.setBackground(header.col); --Green
  247. gpu.fill(header.posX,header.posY,2,1," ");
  248. gpu.setBackground(UI.bg); --Black
  249. if (header.posX ~= 2) then
  250. gpu.fill((header.posX - 3),header.posY,2,1," ");
  251. end
  252. if (header.posX == 2) then
  253. gpu.fill(47,18,2,1," ");
  254. end
  255. if (header.posX == 47) then
  256. gpu.fill(2,header.posY,45,1," ");
  257. end
  258.  
  259. --Draw Menu
  260. for i in pairs(menu) do
  261. if (menu[i].state == 1) then
  262. gpu.setBackground(UI_menu.default) --Orange
  263. gpu.fill(menu[i].posX,menu[i].posY,menu[i].width,menu[i].height," ");
  264. gpu.setForeground(UI.txt); --Black
  265. gpu.set(menu[i].posX,menu[i].posY,menu[i].label);
  266. gpu.setBackground(UI.bg); --Black
  267. gpu.setForeground(UI.fg); --White
  268. end
  269.  
  270. if (menu[i].state == 2) then
  271. gpu.setBackground(UI_menu.hit); --Yellow
  272. gpu.fill(menu[i].posX,menu[i].posY,menu[i].width,menu[i].height," ");
  273. gpu.setForeground(UI.txt); --Black
  274. gpu.set(menu[i].posX,menu[i].posY,menu[i].label);
  275. gpu.setBackground(UI.bg); --Black
  276. gpu.setForeground(UI.fg); --White
  277. end
  278. end
  279.  
  280. -- print debug information
  281. if (printDebug) then
  282. gpu.fill(50,2,80,5," ");
  283. gpu.setBackground(UI.bg) --Black
  284. gpu.setForeground(UI.fg) --White
  285. gpu.set(50,2,tostring(mX)..", "..tostring(mY));
  286. gpu.set(50,3,"playing: "..tostring(playing));
  287. gpu.set(50,4,"beat: "..tostring(beat));
  288. gpu.set(50,5,"pitch: "..tostring(pitch));
  289. else
  290. gpu.fill(50,2,80,5," ");
  291. end
  292.  
  293. handleEvent(event.pull(0.001));
  294. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement