TheCryptek

[LUA][OC] OpenMatrix

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