Advertisement
Pirnogion

shaharproga

Aug 25th, 2015
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.32 KB | None | 0 0
  1. local com = require("component")
  2. local gpu = com.gpu
  3. local shell = require("shell")
  4. local event = require("event")
  5. local term = require("term")
  6. local unicode = require("unicode")
  7. local ecs = require("ECSAPI")
  8. local holo = require("component").hologram
  9.  
  10. ------------------------------------------------------------------
  11. local printer = com.printer3d
  12. local xSize, ySize = 80, 50
  13. gpu.setResolution(xSize*2, ySize)
  14. local rightPartSize = 30
  15.  
  16. local userData = {
  17.     label = "Printered Block",
  18.     tooltip = "byShahar",
  19.     emitRedstone = false,
  20.     buttonMode = false,
  21. }
  22.  
  23. local doingShapes = {}
  24. local onlineShape = 1
  25. local onlineLayer = 1
  26.  
  27. local mode = false
  28.  
  29. local startHolo = 16
  30.  
  31. local button = {
  32.     Name = {text = "Name...", x1, y1, x2, y2},
  33.     Tooltip = {text = "Description...", x1, y1, x2, y2},
  34.     IsEmitRedstone = {text = "isRedstone", x1, y1, x2, y2},
  35.     IsButtonMode = {text = "howButton", x1, y1, x2, y2},
  36.     Print = {text = "Print", x1, y1, x2, y2}
  37. }
  38.  
  39. local colors = {
  40.     [1] = 16711680,
  41.     [2] = 16725760,
  42.     [3] = 16739840,
  43.     [4] = 16754176,
  44.     [5] = 16768256,
  45.     [6] = 15400704,
  46.     [7] = 11730688,
  47.     [8] = 8126208,
  48.     [9] = 4521728,
  49.     [10] = 917248,
  50.     [11] = 6532.25,
  51.     [12] = 65377.75,
  52.     [13] = 65433,
  53.     [14] = 65488.25,
  54.     [15] = 63487,
  55.     [16] = 49151,
  56.     [17] = 35071,
  57.     [18] = 20991,
  58.     [19] = 6911,
  59.     [20] = 1966335,
  60.     [21] = 5570815,
  61.     [22] = 9175295,
  62.     [23] = 12779775,
  63.     [24] = 16449791
  64. }
  65.  
  66. -------------------------------------------------------------------------------------------------
  67.  
  68. local function Square(x, y, width, height, color)
  69.     ecs.square(x*2 - 1, y, width*2, height, color)
  70. end
  71.  
  72. local function isIn(x1, y1, x2, y2, xClick, yClick)
  73.     print(x1, y1, x2, y2, xClick, yClick)
  74.     if xClick >= x1 and xClick <= x2 and yClick >= y1 and yClick <= y2 then
  75.         return true
  76.     else
  77.         return false
  78.     end
  79. end
  80.  
  81. local function Text(x, y, text, colorBack, colorText)
  82.     gpu.setBackground(colorBack)
  83.     gpu.setForeground(colorText)
  84.     gpu.set(x, y, text)
  85. end
  86.  
  87. local function inDiapason(num1, num2, num)
  88.     if num >= num1 and num <= num2 then
  89.         return true
  90.     elseif num >= num2 and num <= num1 then
  91.         return true
  92.     else
  93.         return false
  94.     end
  95. end
  96.  
  97. local function whatsClickMain(x, y)
  98.     if x > 1 and x < 49 and y > 1 and y < 50 then
  99.         return math.floor((x+2)/ 3), math.floor((y + 1)/ 3)
  100.     else
  101.         return "fail", " "
  102.     end
  103. end
  104.  
  105. local function whatsClickShape(x, y)
  106.     for i = 1, 24 do
  107.         if isIn(doingShapes[i].x, doingShapes[i].y, doingShapes[i].x + 4, doingShapes[i].y, x, y) then
  108.             return "nofail", i
  109.         end
  110.     end
  111.     return "fail"
  112. end
  113.  
  114. local function whatsClickButton(x, y)
  115.     if isIn(button.Name.x1, button.Name.y1, button.Name.x2, button.Name.y2, x, y) then
  116.         return "name"
  117.     elseif isIn(button.Tooltip.x1, button.Tooltip.y1, button.Tooltip.x2, button.Tooltip.y2, x, y) then
  118.         return "tooltip"
  119.     elseif isIn(button.IsEmitRedstone.x1, button.IsEmitRedstone.y1, button.IsEmitRedstone.x2, button.IsEmitRedstone.y2, x, y) then
  120.         return "redstone"
  121.     elseif isIn(button.IsButtonMode.x1, button.IsButtonMode.y1, button.IsButtonMode.x2, button.IsButtonMode.y2, x, y) then
  122.         return "buttonMode"
  123.     elseif isIn(button.Print.x1, button.Print.y1, button.Print.x2, button.Print.y2, x, y) then
  124.         return "print"
  125.     else
  126.         return "fail"
  127.     end
  128. end
  129.  
  130. local function whatsClick(x, y)
  131.     local mainScreenX, mainScreenY = whatsClickMain(x/2, y)
  132.     if mainScreenX ~= "fail" then
  133.         return "main", mainScreenX, mainScreenY
  134.     end
  135.     local shape = {whatsClickShape(x, y)}
  136.     if shape[1] ~= "fail" then
  137.         return "shape", shape[2]
  138.     end
  139.  
  140.     local button = whatsClickButton(x/2, y)
  141.     if button ~= "fail" then
  142.         return "button", button
  143.     end
  144.     return "none", 0
  145. end
  146.  
  147. local function textInCount(text)
  148.     return
  149. end
  150.  
  151. ------------------------------Изичные функции-----------------------------------------
  152. local function getPosXForShapeButton(i)
  153.     local x
  154.     x = i % 6 * 5
  155.     return x
  156. end
  157.  
  158. local function getPosYForShapeButton(i)
  159.     local y
  160.     y = math.floor(i / 6) * 3
  161.     return y
  162. end
  163. ------------------------------------Прорисовка кнопок и окон---------------------
  164. local function drawLayerBar(x, y, number)
  165.     gpu.setBackground(0x111111)
  166.     gpu.setForeground(0xff0000)
  167.     gpu.set(x-7, y-1, "                 ")
  168.     gpu.set(x-7, y, " Layer:          ")
  169.     gpu.set(x-7, y+1, "                 ")
  170.     gpu.setBackground(0xffffff)
  171.     gpu.setForeground(0x000000)
  172.     if number < 10 then
  173.         gpu.set(x, y, "    "..tostring(number).."   ")
  174.     else
  175.         gpu.set(x, y, "   "..tostring(number).."   ")
  176.     end
  177. end
  178.  
  179. local function drawShapeButton(x, y, number)
  180.     if number ~= onlineShape then
  181.         gpu.setBackground(0x111111)
  182.         gpu.setForeground(0xffffff)
  183.     else
  184.         gpu.setBackground(colors[number])
  185.         gpu.setForeground(0xffffff)
  186.     end
  187.     if number < 10 then
  188.         gpu.set(x, y, "  "..tostring(number).." ")
  189.     else
  190.         gpu.set(x, y, " "..tostring(number).." ")
  191.     end
  192. end
  193.  
  194. local function calculateButtons(x, y)
  195.     x = x * 2
  196.     for i = 1, 24 do
  197.         doingShapes[i] = {
  198.             x = x + getPosXForShapeButton(i - 1),
  199.             y = y + getPosYForShapeButton(i - 1),
  200.             isNil = true
  201.         }
  202.     end
  203. end
  204.  
  205. local function drawShapeButtonsList()
  206.     for i = 1, 24 do
  207.         drawShapeButton(doingShapes[i].x, doingShapes[i].y, i)
  208.     end
  209. end
  210.  
  211. local function drawButtons(xN, yN, xT, yT, xP, yP)
  212.     button.Name.x1, button.Name.y1, button.Name.x2, button.Name.y2 = ecs.drawAdaptiveButton(xN, yN, 2, 1, button.Name.text, 0x111111, 0xffffff)
  213.     button.Tooltip.x1, button.Tooltip.y1, button.Tooltip.x2, button.Tooltip.y2 = ecs.drawAdaptiveButton(xT, yT, 2, 1, button.Tooltip.text, 0x111111, 0xffffff)
  214.     button.Print.x1, button.Print.y1, button.Print.x2, button.Print.y2 = ecs.drawAdaptiveButton(xP, yP, 2, 1, button.Print.text, 0x111111, 0xffffff)
  215.     ecs.error(button.Name.x1..button.Name.y1..button.Name.x2..button.Name.y2)
  216. end
  217.  
  218. local function digitToText(numb)
  219.     return unicode.char(numb)
  220. end
  221.  
  222. local function WindowsWithTextBox(x, y, width, height, mainText, buttonText)
  223.     local input = ""
  224.     local buttonWidth = 4
  225.     Square(x, y, width, 1, 0x0000dd)
  226.     Square(x, y + 1, width, height - 1, 0xdddddd)
  227.     Text(x*2 + 1, y, mainText, 0x0000dd, 0xffffff)
  228.     Square(x + 1, y + math.floor(height/2), width - 2, 1, 0xffffff)
  229.     Square(x + width - buttonWidth - 1, y + height - 2, buttonWidth, 1, 0x0000dd)
  230.     Text((x + width - buttonWidth - 1)*2,  y + height - 2, buttonText, 0x0000dd, 0xffffff)
  231.     while true do
  232.         local event = {event.pull()}
  233.         if event[1] == "key_down" then
  234.             input = input..digitToText(event[3])
  235.             Text((x + 1)*2, y + math.floor(height/2), input, 0xffffff, 0x000000)
  236.         end
  237.         if event[1] == "touch" then
  238.             break
  239.         end
  240.     end
  241.     return input
  242. end
  243. -------------------------------------------Главный экран-----------------------------------
  244. local function drawShapeScreen(numberShape, numberLayer)
  245.     for xShape = 1, 16 do
  246.         for yShape = 1, 16 do
  247.             local color = 0x9f8f8f
  248.             if doingShapes[numberShape].isNil == false then
  249.                 if doingShapes[numberShape].x1 == xShape and doingShapes[numberShape].y1 == yShape and doingShapes[numberShape].z1 == numberLayer then
  250.                     color = 0xffffff
  251.                 elseif doingShapes[numberShape].x2 == xShape and doingShapes[numberShape].y2 == yShape and doingShapes[numberShape].z2 == numberLayer then
  252.                     color = 0x000000
  253.                 elseif inDiapason(doingShapes[numberShape].x1, doingShapes[numberShape].x2, xShape) and inDiapason(doingShapes[numberShape].y1, doingShapes[numberShape].y2, yShape) and inDiapason(doingShapes[numberShape].z1, doingShapes[numberShape].z2, numberLayer) then
  254.                     color = colors[numberShape]
  255.                 end
  256.             end
  257.             Square((xShape - 1) * 3 + 2, (yShape - 1) * 3 + 2, 3, 3, color)
  258.         end
  259.     end
  260.    
  261. end
  262. ---------------------------------------------Холограмма------------------------------------
  263. local function ShapeInHolo(numb, color)
  264.     if numb == onlineShape then
  265.         holo.setPaletteColor(3, colors[numb])
  266.         for zHolo = doingShapes[numb].z1, doingShapes[numb].z2 do
  267.             for xHolo = doingShapes[numb].x1, doingShapes[numb].x2 do
  268.                 holo.fill(xHolo + startHolo, zHolo + startHolo, 32 - doingShapes[numb].y2, 32 - doingShapes[numb].y1, 3)
  269.             end
  270.         end    
  271.     else
  272.         for zHolo = doingShapes[numb].z1, doingShapes[numb].z2 do
  273.             for xHolo = doingShapes[numb].x1, doingShapes[numb].x2 do
  274.                 holo.fill(xHolo + startHolo, zHolo + startHolo, 32 - doingShapes[numb].y2, 32 - doingShapes[numb].y1, 1)
  275.             end    
  276.         end
  277.     end
  278. end
  279.  
  280. local function ShapeUgli(number)
  281.     holo.set(doingShapes[number].x1 + startHolo, 32 - doingShapes[number].y1, doingShapes[number].z1 + startHolo, 2)
  282.     holo.set(doingShapes[number].x2 + startHolo, 32 - doingShapes[number].y2, doingShapes[number].z2 + startHolo, 2)
  283. end
  284.  
  285. local function AllShapesInHolo(color, color2)
  286.     holo.clear()
  287.     holo.setPaletteColor(1, color)
  288.     holo.setPaletteColor(2, color2)
  289.     for numb = 1, 24 do
  290.         if doingShapes[numb].isNil == false then
  291.             ShapeInHolo(numb)
  292.         end
  293.     end
  294.     for numb = 1, 24 do
  295.         if doingShapes[numb].isNil == false then
  296.             ShapeUgli(numb)
  297.         end
  298.     end
  299. end
  300. ---------------------------------------Принтер-------------------------------------------
  301.  
  302. local function converForPrinter(oldShapes)
  303.     for i = 1, 24 do
  304.         if oldShapes[i].isNil == false then
  305.             local x1 = math.min(oldShapes[i].x1, oldShapes[i].x2)
  306.             local x2 = math.max(oldShapes[i].x1, oldShapes[i].x2)
  307.             local y1 = math.min(oldShapes[i].y1, oldShapes[i].y2)
  308.             local y2 = math.max(oldShapes[i].y1, oldShapes[i].y2)
  309.             local z1 = math.min(oldShapes[i].z1, oldShapes[i].z2)
  310.             local z2 = math.max(oldShapes[i].z1, oldShapes[i].z2)
  311.             oldShapes[i].x1 = x1
  312.             oldShapes[i].x2 = x2
  313.             oldShapes[i].y1 = y1
  314.             oldShapes[i].y2 = y2
  315.             oldShapes[i].z1 = z1
  316.             oldShapes[i].z2 = z2
  317.         end
  318.     end
  319.     return oldShapes
  320. end
  321.  
  322. local function inputData(newLabel, newTooltip, newEmitRedstone, newButtonMode, shapes)
  323.     printer.reset()
  324.     printer.setLabel(newLabel)
  325.     printer.setTooltip(newTooltip)
  326.     printer.setLightLevel(1)
  327.     printer.setRedstoneEmitter(newEmitRedstone)
  328.     printer.setButtonMode(newButtonMode)
  329.     local normShapes = converForPrinter(shapes)
  330.     for i = 1, 24 do
  331.         if normShapes[i].isNil == false then
  332.             printer.addShape(normShapes[i].x1 - 1, 16 - (normShapes[i].y1 - 1), normShapes[i].z1 - 1, normShapes[i].x2, 16 - normShapes[i].y2, normShapes[i].z2, "quartz_block_side", false, 0xffffff)
  333.         end
  334.     end
  335. end
  336.  
  337. local function print(count)
  338.     printer.commit(count)
  339. end
  340.  
  341.  
  342. -----------------------------------------------Прога-----------------------------
  343. ecs.prepareToExit()
  344. Square(1, 1, xSize - rightPartSize, ySize, 0xffffff)
  345. calculateButtons(xSize - 21, ySize - 18)
  346. drawButtons(xSize*2 - 20, 15, xSize*2 - 10, 15, xSize*2 - 15, 20)
  347. drawShapeButtonsList()
  348. drawShapeScreen(onlineShape, onlineLayer)
  349. drawLayerBar(xSize*2 - 30, 25, onlineLayer)
  350. holo.clear()
  351.  
  352. while true do
  353.     local event = {event.pull()}
  354.     if event[1] == "touch" then
  355.         local case = {whatsClick(event[3], event[4])}
  356.         if case[1] == "main" then
  357.             if mode == false then
  358.                 doingShapes[onlineShape].x1 = case[2]
  359.                 doingShapes[onlineShape].y1 = case[3]
  360.                 doingShapes[onlineShape].z1 = onlineLayer
  361.                 doingShapes[onlineShape].x2 = case[2]
  362.                 doingShapes[onlineShape].y2 = case[3]
  363.                 doingShapes[onlineShape].z2 = onlineLayer
  364.                 doingShapes[onlineShape].isNil = false
  365.                 mode = true
  366.             else
  367.                 doingShapes[onlineShape].x2 = case[2]
  368.                 doingShapes[onlineShape].y2 = case[3]
  369.                 doingShapes[onlineShape].z2 = onlineLayer
  370.                 doingShapes[onlineShape].isNil = false
  371.                 mode = false
  372.                 AllShapesInHolo(0x00ff00, 0xffffff)
  373.             end
  374.             drawShapeScreen(onlineShape, onlineLayer)
  375.         end
  376.         if case[1] == "shape" then
  377.             onlineShape = case[2]
  378.             drawShapeButtonsList()
  379.             AllShapesInHolo(0x00ff00, 0xffffff)
  380.         end
  381.         if case[1] == "button" then
  382.             if case[2] == "name" then
  383.                 userData.label = WindowsWithTextBox(xSize/2 - 7, ySize/2 - 3, 15, 7, "Name will be ...", "Ок")
  384.             elseif case[2] == "tooltip" then
  385.                 userData.tooltip = WindowsWithTextBox(xSize/2 - 7, ySize/2 - 3, 15, 7, "Tooltip will be ...", "Ок")
  386.             elseif case[2] == "redstone" then
  387.                
  388.             elseif case[2] == "buttonMode" then
  389.                
  390.             elseif case[2] == "print" then
  391.                 inputData(userData.label, userData.tooltip, userData.emitRedstone, userData.buttonMode, doingShapes)
  392.                 print(1)
  393.             end
  394.         end
  395.     elseif event[1] == "scroll" then
  396.         onlineLayer = onlineLayer - event[5]
  397.         if onlineLayer < 1 then
  398.             onlineLayer = 1
  399.         elseif onlineLayer > 16 then
  400.             onlineLayer = 16
  401.         end
  402.         drawShapeScreen(onlineShape, onlineLayer)
  403.         drawLayerBar(xSize*2 - 30, 25, onlineLayer)
  404.     end
  405. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement