Paragorn

ButtonCreator (functionsAPI)

Oct 18th, 2015
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.71 KB | None | 0 0
  1. -- Alle Funktionen
  2.  
  3. -- design_fillMon(letter, bColor)
  4. -- design_redrawMonitor(buttons, bColor, hColor, tColor)
  5. -- design_dateiMenu(dateiOpen, tColor, hColor, bColor)
  6. -- design_drawButtons(buttons)
  7. -- object_checkIfClicked(objectX, objectY, clickedX, clickedY)
  8. -- prog_numberToColor(colorPos)
  9. -- prog_setButtonName()
  10. -- prog_setButtonBColor()
  11. -- prog_setButtonTColor()
  12. -- prog_closeMenus(buttons, bColor)
  13. -- prog_getMidOButton(buttonFX, buttonFY, buttonSX, buttonSY, text)
  14. -- prog_setProgName()
  15. -- prog_compileObjects(buttons, progName)
  16.  
  17. -- Tables
  18.  
  19. colorTable = {
  20.     colors.white,
  21.     colors.orange,
  22.     colors.magenta,
  23.     colors.lightBlue,
  24.     colors.yellow,
  25.     colors.lime,
  26.     colors.pink,
  27.     colors.gray,
  28.     colors.lightGray,
  29.     colors.cyan,
  30.     colors.purple,
  31.     colors.blue,
  32.     colors.brown,
  33.     colors.green,
  34.     colors.red,
  35.     colors.black
  36. };
  37.  
  38. -- Funktionen
  39.  
  40. function design_fillMon(letter, bColor)
  41.  
  42.   for row = 2,19 do
  43.     for column = 1,51 do
  44.       term.setTextColor(colors.lightGray);
  45.       term.setBackgroundColor(bColor);
  46.       term.setCursorPos(column, row);
  47.       term.write(letter);
  48.     end
  49.   end
  50.  
  51. end
  52.  
  53. function design_redrawMonitor(buttons, bColor, hColor, tColor)
  54.  
  55.     term.clear();
  56.  
  57.     local mainWindow = paintutils.loadImage(".mainWindow");
  58.     paintutils.drawImage(mainWindow, 1, 1);
  59.     design_fillMon("X", bColor);
  60.  
  61.     term.setCursorPos(1,1);
  62.     term.setBackgroundColor(hColor);
  63.     term.setTextColor(tColor);
  64.     term.write("                                                   ");
  65.  
  66.     term.setCursorPos(1,1);
  67.     term.write("Datei Bearbeiten                                  ?");
  68.    
  69.     design_drawButtons(buttons);
  70.  
  71. end
  72.  
  73. function design_dateiMenu(dateiOpen, tColor, hColor, bColor)
  74.  
  75.     if (not dateiOpen) then
  76.    
  77.         term.setTextColor(tColor);
  78.         term.setBackgroundColor(hColor);
  79.         term.setCursorPos(1,2);
  80.         term.write("Neu      ");
  81.         term.setCursorPos(1,3);
  82.         term.write("Speichern");
  83.        
  84.         return true;
  85.    
  86.     else
  87.    
  88.         term.setBackgroundColor(bColor);
  89.         term.setTextColor(colors.lightGray);
  90.         term.setCursorPos(1,2);
  91.         term.write("XXXXXXXXX");
  92.         term.setCursorPos(1,3);
  93.         term.write("XXXXXXXXX");
  94.         term.setCursorPos(1,4);
  95.         term.write("XXXXXXXXX");
  96.         term.setTextColor(tColor);
  97.    
  98.         return false;
  99.    
  100.     end
  101.  
  102. end
  103.  
  104. function design_drawButtons(buttons)
  105.    
  106.     buttonAttributes    = 8;
  107.     curTableNumber      = 1;
  108.    
  109.     if (#buttons == 0) then
  110.    
  111.         return;
  112.    
  113.     end
  114.    
  115.     for i = 1, (#buttons / buttonAttributes) do
  116.  
  117.         local buttonText = buttons[curTableNumber];
  118.         local bColor     = buttons[curTableNumber + 1];
  119.         local tColor     = buttons[curTableNumber + 2];
  120.         local fXPos      = buttons[curTableNumber + 3];
  121.         local fYPos      = buttons[curTableNumber + 4];
  122.         local sXPos      = buttons[curTableNumber + 5];
  123.         local sYPos      = buttons[curTableNumber + 6];
  124.         local code       = buttons[curTableNumber + 7];
  125.  
  126.         for yPos = fYPos, sYPos do
  127.        
  128.             for xPos = fXPos, sXPos do
  129.            
  130.                 term.setBackgroundColor(bColor)
  131.                 term.setCursorPos(xPos, yPos);
  132.                 term.write(" ");
  133.            
  134.             end
  135.            
  136.         end
  137.        
  138.         x, y = prog_getMidOButton(fXPos, fYPos, sXPos, sYPos, buttonText);
  139.         term.setTextColor(tColor);
  140.         term.setCursorPos(x, y);
  141.         term.write(buttonText);
  142.        
  143.         curTableNumber = curTableNumber + 8;
  144.    
  145.     end
  146.    
  147. end
  148.  
  149. function object_checkIfClicked(objectX, objectY, clickedX, clickedY)
  150.  
  151.     local objectX_a = objectX[1];
  152.     local objectX_b = objectX[2];
  153.     local objectY_a = objectY[1];
  154.     local objectY_b = objectY[2];
  155.    
  156.     if (clickedX >= objectX_a and clickedX <= objectX_b) then
  157.        
  158.         if (clickedY >= objectY_a and clickedY <= objectY_b) then
  159.        
  160.             return true;
  161.        
  162.         else
  163.        
  164.             return false;
  165.        
  166.         end
  167.        
  168.     else
  169.    
  170.         return false;
  171.    
  172.     end
  173.  
  174. end
  175.  
  176. function prog_numberToColor(colorPos)
  177.  
  178.     return colorTable[colorPos];
  179.    
  180. end
  181.  
  182. function prog_setButtonName(buttonFX, buttonSX)
  183.  
  184.     maxTextChars = buttonSX - buttonFX
  185.  
  186.     term.setBackgroundColor(colors.gray);
  187.     term.setTextColor(colors.white);
  188.     term.setCursorPos(14,7);
  189.     term.write("                         ");
  190.     term.setCursorPos(14,8);
  191.     term.write("                         ");
  192.     term.setCursorPos(14,9);
  193.     term.write("      Button Text:       ");
  194.     term.setCursorPos(14,10);
  195.     term.write("                         ");
  196.     term.setCursorPos(14,11);
  197.     term.write("                         ");
  198.     term.setCursorPos(14,12);
  199.     term.write("                         ");
  200.    
  201.     term.setCursorPos(18,8)
  202.     term.write("Maximal " .. tostring(maxTextChars) .. " Zeichen");
  203.    
  204.     term.setCursorPos(14,10);
  205.     text = read();
  206.    
  207.     if (text:len() > maxTextChars) then
  208.    
  209.         prog_setButtonName(buttonFX, buttonSX);
  210.    
  211.     end
  212.    
  213.     return text;
  214.  
  215. end
  216.  
  217. function prog_setButtonBColor()
  218.  
  219.     local chooseColorX = {17,32};
  220.     local chooseColorY = {10,10};
  221.  
  222.     term.setBackgroundColor(colors.gray);
  223.     term.setTextColor(colors.white);
  224.     term.setCursorPos(17,7);
  225.     term.write("                ");
  226.     term.setCursorPos(17,8);
  227.     term.write("                ");
  228.     term.setCursorPos(17,9);
  229.     term.write("  Hintergrund:  ");
  230.     term.setCursorPos(17,10);
  231.    
  232.     for i = 1,16 do
  233.    
  234.         term.setBackgroundColor(colorTable[i]);
  235.         term.write(" ");
  236.    
  237.     end
  238.    
  239.     term.setBackgroundColor(colors.gray);
  240.     term.setCursorPos(17,11);
  241.     term.write("                ");
  242.     term.setCursorPos(17,12);
  243.     term.write("                ");
  244.    
  245.     event, side, x, y = os.pullEvent("mouse_click");
  246.    
  247.     if (object_checkIfClicked(chooseColorX, chooseColorY, x, y)) then
  248.    
  249.         clickedColor = x - 16;
  250.    
  251.     else
  252.    
  253.         prog_setButtonBColor();
  254.    
  255.     end
  256.    
  257.     return colorTable[clickedColor];
  258.  
  259. end
  260.  
  261. function prog_setButtonTColor()
  262.  
  263.     local chooseColorX = {17,32};
  264.     local chooseColorY = {10,10};
  265.  
  266.     term.setBackgroundColor(colors.gray);
  267.     term.setTextColor(colors.white);
  268.     term.setCursorPos(17,7);
  269.     term.write("                ");
  270.     term.setCursorPos(17,8);
  271.     term.write("                ");
  272.     term.setCursorPos(17,9);
  273.     term.write("      Text:     ");
  274.     term.setCursorPos(17,10);
  275.    
  276.     for i = 1,16 do
  277.    
  278.         term.setBackgroundColor(colorTable[i]);
  279.         term.write(" ");
  280.    
  281.     end
  282.    
  283.     term.setBackgroundColor(colors.gray);
  284.     term.setCursorPos(17,11);
  285.     term.write("                ");
  286.     term.setCursorPos(17,12);
  287.     term.write("                ");
  288.    
  289.     event, side, x, y = os.pullEvent("mouse_click");
  290.    
  291.     if (object_checkIfClicked(chooseColorX, chooseColorY, x, y)) then
  292.    
  293.         clickedColor = x - 16;
  294.    
  295.     else
  296.    
  297.         prog_setButtonBColor();
  298.    
  299.     end
  300.    
  301.     return colorTable[clickedColor];
  302.  
  303. end
  304.  
  305. function prog_closeMenus(buttons, bColor)
  306.  
  307.     design_fillMon("X", bColor);
  308.     design_drawButtons(buttons);
  309.    
  310.     return false, false;
  311.  
  312. end
  313.  
  314. function prog_getMidOButton(buttonFX, buttonFY, buttonSX, buttonSY, text)
  315.  
  316.     local buttonSizeX   = buttonSX - buttonFX;
  317.     local buttonSizeY   = buttonSY - buttonFY;
  318.    
  319.     if (buttonSizeY % 2) then
  320.    
  321.         buttonSizeY = buttonSizeY - 1;
  322.    
  323.     end
  324.    
  325.     local y             = buttonFY + math.ceil(buttonSizeY / 2);
  326.     local x             = buttonFX + math.ceil((buttonSizeX / 2) - (text:len() / 2));
  327.    
  328.     return x, y;
  329.  
  330. end
  331.  
  332. function prog_setProgName()
  333.  
  334.     term.setBackgroundColor(colors.gray);
  335.     term.setTextColor(colors.white);
  336.     term.setCursorPos(14,7);
  337.     term.write("                         ");
  338.     term.setCursorPos(14,8);
  339.     term.write("                         ");
  340.     term.setCursorPos(14,9);
  341.     term.write("      Programmname:      ");
  342.     term.setCursorPos(14,10);
  343.     term.write("                         ");
  344.     term.setCursorPos(14,11);
  345.     term.write("                         ");
  346.     term.setCursorPos(14,12);
  347.     term.write("                         ");
  348.    
  349.     term.setCursorPos(15,10);
  350.     progName = read();
  351.    
  352.     if (text == "") then
  353.    
  354.         prog_setProgName()
  355.    
  356.     end
  357.    
  358.     return progName;
  359.  
  360. end
  361.  
  362.  
  363. function prog_compileObjects(buttons, progName)
  364.  
  365.     buttonAttributes    = 8;
  366.     curTableNumber      = 1;
  367.    
  368.     if (#buttons == 0) then
  369.    
  370.         return;
  371.    
  372.     end
  373.    
  374.     file = fs.open(progName, "w");
  375.    
  376.     file.writeLine("-- Variablen");
  377.     file.writeLine("");
  378.     file.writeLine("background = colors.black;");
  379.     file.writeLine("textcolor  = colors.white;");
  380.     file.writeLine("")
  381.     file.writeLine("-- Design");
  382.     file.writeLine("");
  383.     file.writeLine("term.setBackgroundColor(background);");
  384.     file.writeLine("term.setTextColor(textcolor);");
  385.     file.writeLine("term.clear();");
  386.     file.writeLine("term.setCursorPos(1,1);");
  387.     file.writeLine("");
  388.    
  389.     for i = 1, (#buttons / buttonAttributes) do
  390.  
  391.         local buttonText = buttons[curTableNumber];
  392.         local bColor     = buttons[curTableNumber + 1];
  393.         local tColor     = buttons[curTableNumber + 2];
  394.         local fXPos      = buttons[curTableNumber + 3];
  395.         local fYPos      = buttons[curTableNumber + 4];
  396.         local sXPos      = buttons[curTableNumber + 5];
  397.         local sYPos      = buttons[curTableNumber + 6];
  398.         local code       = buttons[curTableNumber + 7];
  399.  
  400.         x, y = prog_getMidOButton(fXPos, fYPos, sXPos, sYPos, buttonText);
  401.        
  402.         file.writeLine("for yPos = " .. fYPos .. ", " .. sYPos .. " do");
  403.         file.writeLine("");
  404.         file.writeLine(" for xPos = " .. fXPos .. ", " .. sXPos .. " do");
  405.         file.writeLine("");
  406.         file.writeLine("  term.setBackgroundColor(" .. bColor .. ");");
  407.         file.writeLine("  term.setCursorPos(xPos, yPos);");
  408.         file.writeLine("  term.write(\" \");");
  409.         file.writeLine("");
  410.         file.writeLine(" end");
  411.         file.writeLine("");
  412.         file.writeLine("end");
  413.         file.writeLine("");
  414.         file.writeLine("term.setTextColor(" .. tColor .. ");");
  415.         file.writeLine("term.setCursorPos(" .. x .. ", " .. y .. ");");
  416.         file.writeLine("term.write(\"" .. buttonText .. "\");");
  417.         file.writeLine("")
  418.        
  419.         curTableNumber = curTableNumber + 8;
  420.    
  421.     end
  422.    
  423.     file.writeLine("-- Programmtechnisch");
  424.     file.writeLine("");
  425.     file.writeLine("term.setCursorPos(1,1);");
  426.     file.writeLine("term.setBackgroundColor(colors.black);");
  427.     file.writeLine("term.setTextColor(colors.white);");
  428.     file.writeLine("");
  429.     file.writeLine("while true do");
  430.     file.writeLine("");
  431.     file.writeLine("event, side, x, y = os.pullEvent(\"mouse_click\");");
  432.     file.writeLine("")
  433.    
  434.     curTableNumber  = 1;
  435.     curButton       = 1;
  436.    
  437.     for i = 1, (#buttons / buttonAttributes) do
  438.  
  439.         local buttonText = buttons[curTableNumber];
  440.         local bColor     = buttons[curTableNumber + 1];
  441.         local tColor     = buttons[curTableNumber + 2];
  442.         local fXPos      = buttons[curTableNumber + 3];
  443.         local fYPos      = buttons[curTableNumber + 4];
  444.         local sXPos      = buttons[curTableNumber + 5];
  445.         local sYPos      = buttons[curTableNumber + 6];
  446.         local code       = buttons[curTableNumber + 7];
  447.  
  448.         if (curButton == 1) then
  449.             file.writeLine("if (x >= " .. fXPos .. " and x <= " .. sXPos .. " and y >= " .. fYPos .. " and y <= " .. sYPos .. ") then");
  450.             file.writeLine("");
  451.         else
  452.             file.writeLine("elseif (x >= " .. fXPos .. " and x <= " .. sXPos .. " and y >= " .. fYPos .. " and y <= " .. sYPos .. ") then");
  453.             file.writeLine("");
  454.         end
  455.    
  456.         file.writeLine(code);
  457.         file.writeLine("");
  458.        
  459.         curTableNumber  = curTableNumber + 8;
  460.         curButton       = curButton + 1;
  461.    
  462.     end
  463.    
  464.     file.writeLine("end");
  465.     file.writeLine("");
  466.     file.writeLine("end");
  467.     file.close();
  468.  
  469. end
Advertisement
Add Comment
Please, Sign In to add comment