Ktlo

Computer Craft Object API

May 16th, 2015
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 39.30 KB | None | 0 0
  1. --v1.0
  2. --Setup
  3. local List = {}
  4. --Objects
  5. New = {
  6.     Button = function(env, x, y, w, h, text, textColor, backColor, clickColor, inactColor, visible) --Creates a new button object
  7.         if type(env) ~= "table" then
  8.             error("Please, set a valid environment!", 2)
  9.         end
  10.         if type(x) ~= "number" or type(y) ~= "number" or type(w) ~= "number" or type(h) ~= "number" then
  11.             error("The type of coordinates and resolution must be 'number', got '"..type(x).."', '"..type(y).."', '"..type(w).."' and '"..type(h).."'.", 2)
  12.         end
  13.         if type(text) ~= "string" and text ~= nil then
  14.             error("The type of text parameter must be 'string', got '"..type(text).."'.", 2)
  15.         end
  16.         if (type(textColor) ~= "number" and textColor ~= nil) or (type(backColor) ~= "number" and backColor ~= nil) or (type(clickColor) ~= "number" and clickColor ~= nil) or (type(inactColor) ~= "number" and inactColor ~= nil) then
  17.             error("The type of colours parameters must be 'number', got '"..type(textColor).."', '"..type(backColor).."', '"..type(clickColor).."' and '"..type(inactColor).."'.", 2)
  18.         end
  19.         if type(visible) ~= "boolean" and visible ~= nil then
  20.             error("The type of visibility parameter must be 'boolean', got '"..type(visible).."'.", 2)
  21.         end
  22.         if text == nil then
  23.             text = ""
  24.         end
  25.         if visible == nil then
  26.             visible = true
  27.         end
  28.         if textColor == nil then
  29.             textColor = colors.white
  30.         end
  31.         if backColor == nil then
  32.             backColor = colors.blue
  33.         end
  34.         if clickColor == nil then
  35.             clickColor = colors.green
  36.         end
  37.         if inactColor == nil then
  38.             inactColor = colors.gray
  39.         end
  40.         local button = {}
  41.         button.Visible = visible
  42.         button.Object = "button"
  43.         button.Environment = env
  44.         button.xPos = x
  45.         button.yPos = y
  46.         button.Width = w
  47.         button.Height = h
  48.         button.Text = text
  49.         button.TextColor = textColor
  50.         button.BackgroundColor = backColor
  51.         button.ActivateColor = clickColor
  52.         button.InactiveColor = inactColor
  53.         --Helper functions
  54.         local function colorText(x, y, color, backColor, value) --The easy way to make colour text
  55.             local xs, ys = term.getCursorPos()
  56.             term.setCursorPos(x, y)
  57.             term.setTextColor(color)
  58.             term.setBackgroundColor(backColor)
  59.             term.write(value)
  60.             term.setCursorPos(xs, ys)
  61.         end
  62.         --Button functions
  63.         function button.Draw(state) --Draws button as term object
  64.             local t = term.current()
  65.             term.redirect(button.Environment)
  66.             if state == nil then
  67.                 paintutils.drawFilledBox(button.xPos, button.yPos, button.xPos+button.Width, button.yPos+button.Height, button.BackgroundColor)
  68.                 colorText(button.xPos+button.Width/2-(#button.Text/2)+1, button.yPos+button.Height/2, button.TextColor, button.BackgroundColor, button.Text)
  69.             elseif state == "active" then
  70.                 paintutils.drawFilledBox(button.xPos, button.yPos, button.xPos+button.Width, button.yPos+button.Height, button.ActivateColor)
  71.                 colorText(button.xPos+button.Width/2-(#button.Text/2)+1, button.yPos+button.Height/2, button.TextColor, button.ActivateColor, button.Text)
  72.             elseif state == "inactive" then
  73.                 paintutils.drawFilledBox(button.xPos, button.yPos, button.xPos+button.Width, button.yPos+button.Height, button.InactiveColor)
  74.                 colorText(button.xPos+button.Width/2-(#button.Text/2)+1, button.yPos+button.Height/2, button.TextColor, button.InactiveColor, button.Text)
  75.             else
  76.                 error("The button state parameter must be 'active', 'inactive' or 'nil', got '"..state.."'.", 2)
  77.             end
  78.             term.setBackgroundColor(colors.black)
  79.             term.redirect(t)
  80.         end
  81.         function button.GetVisibility() --This function returns the visibility state
  82.             return button.Visible
  83.         end
  84.         function button.SetVisibility(boolean) --This function sets the visibility state
  85.             if type(boolean) ~= "boolean" then
  86.                 error("The parameter must be boolean, got '"..type(boolean).."'.", 2)
  87.             end
  88.             button.Visible = boolean
  89.             if button.Visible == true then
  90.                 button.Draw()
  91.             end
  92.         end
  93.         function button.SetPosition(x, y, w, h) --Change button's position
  94.             if x == nil or y == nil then
  95.                 error("Not enough parameters!", 2)
  96.             end
  97.             if type(x) ~= "number" or type(y) ~= "number" then
  98.                 error("Coordinates x and y must be 'number', got '"..type(x).."' and '"..type(y).."'.", 2)
  99.             end
  100.             button.xPos = x
  101.             button.yPos = y
  102.             if w ~= nil and type(w) == "number" then
  103.                 button.Width = w
  104.             end
  105.             if h ~= nil and type(h) == "number" then
  106.                 button.Height = h
  107.             end
  108.             if button.Visible == true then
  109.                 button.Draw()
  110.             end
  111.         end
  112.         function button.GetText()
  113.             return button.Text
  114.         end
  115.         function button.SetText(text)
  116.             if text == nil then
  117.                 button.Text = ""
  118.             elseif type(text) == "string" then
  119.                 button.Text = text
  120.             else
  121.                 error("The text parameter must be 'string', got '"..type(text).."'.", 2)
  122.             end
  123.             if button.Visible == true then
  124.                 button.Draw()
  125.             end
  126.         end
  127.         function button.GetPosition() --Function for getting position of the button object
  128.             return button.xPos, button.yPos, button.Width, button.Height
  129.         end
  130.         function button.GetColors() --Returns the button's colors
  131.             return button.TextColor, button.BackgroundColor, button.ActivateColor, button.InactiveColor
  132.         end
  133.         function button.SetColors(textColor, backColor, clickColor, inactColor) --Sets the colors of the button object
  134.             if textColor ~= nil and type(textColor) == "number" then
  135.                 button.TextColor = textColor
  136.             end
  137.             if backColor ~= nil and type(backColor) == "number" then
  138.                 button.BackgroundColor = backColor
  139.             end
  140.             if clickColor ~= nil and type(clickColor) == "number" then
  141.                 button.ActivateColor = clickColor
  142.             end
  143.             if inactColor ~= nil and type(inactColor) == "number" then
  144.                 button.InactiveColor = inactColor
  145.             end
  146.             if button.Visible == true then
  147.                 button.Draw()
  148.             end
  149.         end
  150.         function button.Detect(action, mouseButton, x, y) --Function for actions with button
  151.             local t = term.current()
  152.             term.redirect(button.Environment)
  153.             local event, param1, param2, param3
  154.             if x == nil then
  155.                 event, param1, param2, param3 = os.pullEvent(action)
  156.             else
  157.                 param1 = mouseButton
  158.                 param2 = x
  159.                 param3 = y
  160.             end
  161.             if (x ~= nil and type(x) ~= "number") or (y ~= nil and type(y) ~= "number") then
  162.                 error("Coordinates must be 'number', got '"..type(x).."' and '"..type(y).."'.", 2)
  163.             end
  164.             if action == "mouse_click" then --Click action. It returns true if it is clicked or false if it isn't clicked
  165.                 if type(mouseButton) ~= "number" or mouseButton > 2 or mouseButton < 1 then
  166.                     error("The value after 'mouse_click' action must be 'number' from 1 to 2!, got '"..type(mouseButton).."' = "..mouseButton..".", 2)
  167.                 end
  168.                 if param1 == mouseButton and param2 >= button.xPos and param2 <= button.xPos+button.Width and param3 >= button.yPos and param3 <= button.yPos+button.Height then
  169.                     if button.Visible == true then
  170.                         button.Draw("active")
  171.                         os.sleep(0.2)
  172.                         button.Draw()
  173.                     end
  174.                     return true
  175.                 else
  176.                     return false
  177.                 end
  178.             elseif action == "mouse_scroll" then --Scroll action. It returns true and direction when you scroll on the button object and returns false if not.
  179.                 if param2 >= button.xPos and param2 <= button.xPos+button.Width and param3 >= button.yPos and param3 <= button.yPos+button.Height then
  180.                     if button.Visible == true then
  181.                         button.Draw("active")
  182.                         os.sleep(0.2)
  183.                         button.Draw()
  184.                     end
  185.                     return true, param1
  186.                 else
  187.                     return false
  188.                 end
  189.             elseif action == "mouse_drag" then --Drag action. It returns true if mouse is dragged on the button object or false if not.
  190.                 if type(mouseButton) ~= "number" or mouseButton > 2 or mouseButton < 1 then
  191.                     error("The value after 'mouse_click' action must be 'number' from 1 to 2!, got '"..type(mouseButton).."' = "..mouseButton..".", 2)
  192.                 end
  193.                 if param1 == mouseButton and param2 >= button.xPos and param2 <= button.xPos+button.Width and param3 >= button.yPos and param3 <= button.yPos+button.Height then
  194.                     if button.Visible == true then
  195.                         button.Draw("active")
  196.                         os.sleep(0.2)
  197.                         button.Draw()
  198.                     end
  199.                     return true
  200.                 else
  201.                     return false
  202.                 end
  203.             elseif action == "monitor_touch" then --Monitor touch action. It returns true if button is touched on a monitor or false if not.
  204.                 if param1 == "front" and param2 >= button.xPos and param2 <= button.xPos+button.Width and param3 >= button.yPos and param3 <= button.yPos+button.Height then
  205.                     if button.Visible == true then
  206.                         button.Draw("active")
  207.                         os.sleep(0.2)
  208.                         button.Draw()
  209.                     end
  210.                     return true
  211.                 else
  212.                     return false
  213.                 end
  214.             else
  215.                 error("Unknown action: '"..action.."'.", 2)
  216.             end
  217.             term.redirect(t)
  218.         end
  219.         --Creating button object
  220.         table.insert(List, 1, button.Object)
  221.         if button.Visible == true then
  222.             button.Draw()
  223.         end
  224.         return button
  225.     end,
  226.     CheckBox = function(env, x, y, text, textColor, backColor, boxColor, clickColor, inactColor, state, visible) --Creates a new check box object.
  227.         if type(env) ~= "table" then
  228.             error("Please, set a valid environment!", 2)
  229.         end
  230.         if type(x) ~= "number" or type(y) ~= "number" then
  231.             error("The type of coordinates must be 'number', got '"..type(x).."' and '"..type(y).."'.", 2)
  232.         end
  233.         if type(text) ~= "string" and text ~= nil then
  234.             error("The type of text parameter must be 'string', got '"..type(text).."'.", 2)
  235.         end
  236.         if (type(textColor) ~= "number" and textColor ~= nil) or (type(backColor) ~= "number" and backColor ~= nil) or (type(boxColor) ~= "number" and boxColor ~= nil) or (type(clickColor) ~= "number" and clickColor ~= nil) or (type(inactColor) ~= "number" and inactColor ~= nil) then
  237.             error("The type of colours parameters must be 'number', got '"..type(textColor).."', '"..type(backColor).."', '"..type(clickColor).."' and '"..type(inactColor).."'.", 2)
  238.         end
  239.         if type(state) ~= "boolean" and state ~= nil then
  240.             error("The type of state parameter must be 'boolean', got '"..type(state).."'.", 2)
  241.         end
  242.         if type(visible) ~= "boolean" and visible ~= nil then
  243.             error("The type of visibility parameter must be 'boolean', got '"..type(visible).."'.", 2)
  244.         end
  245.         if visible == nil then
  246.             visible = true
  247.         end
  248.         if state == nil then
  249.             state = false
  250.         end
  251.         if text == nil then
  252.             text = ""
  253.         end
  254.         if boxColor == nil then
  255.             boxColor = colors.red
  256.         end
  257.         if textColor == nil then
  258.             textColor = colors.white
  259.         end
  260.         if backColor == nil then
  261.             backColor = colors.black
  262.         end
  263.         if clickColor == nil then
  264.             clickColor = colors.green
  265.         end
  266.         if inactColor == nil then
  267.             inactColor = colors.gray
  268.         end
  269.         local checkbox = {}
  270.         checkbox.BoxColor = boxColor
  271.         checkbox.Visible = visible
  272.         checkbox.Object = "check_box"
  273.         checkbox.Environment = env
  274.         checkbox.xPos = x
  275.         checkbox.yPos = y
  276.         checkbox.Text = text
  277.         checkbox.TextColor = textColor
  278.         checkbox.BackgroundColor = backColor
  279.         checkbox.CheckedColor = clickColor
  280.         checkbox.InactiveBoxColor = inactColor
  281.         checkbox.State = state
  282.         --Helper functions
  283.         local function colorText(x, y, color, backColor, value) --The easy way to make colour text
  284.             local xs, ys = term.getCursorPos()
  285.             term.setCursorPos(x, y)
  286.             term.setTextColor(color)
  287.             term.setBackgroundColor(backColor)
  288.             term.write(value)
  289.             term.setCursorPos(xs, ys)
  290.         end
  291.         --Check box functions
  292.         function checkbox.Draw(state) --Draws check box
  293.             local t = term.current()
  294.             term.redirect(checkbox.Environment)
  295.             if state == nil then
  296.                 paintutils.drawPixel(checkbox.xPos, checkbox.yPos, checkbox.BoxColor)
  297.                 colorText(checkbox.xPos+2, checkbox.yPos, checkbox.TextColor, checkbox.BackgroundColor, checkbox.Text)
  298.             elseif state == "active" then
  299.                 paintutils.drawPixel(checkbox.xPos, checkbox.yPos, checkbox.CheckedColor)
  300.                 colorText(checkbox.xPos+2, checkbox.yPos, checkbox.TextColor, checkbox.BackgroundColor, checkbox.Text)
  301.             elseif state == "inactive" then
  302.                 paintutils.drawPixel(checkbox.xPos, checkbox.yPos, checkbox.InactiveBoxColor)
  303.                 colorText(checkbox.xPos+2, checkbox.yPos, checkbox.InactiveBoxColor, checkbox.BackgroundColor, checkbox.Text)
  304.             end
  305.             term.redirect(t)
  306.         end
  307.         function checkbox.GetVisibility() --This function returns the visibility state
  308.             return checkbox.Visible
  309.         end
  310.         function checkbox.SetVisibility(boolean) --This function sets the visibility state
  311.             if type(boolean) ~= "boolean" then
  312.                 error("The type of parameter must be 'boolean', got '"..type(boolean).."'.", 2)
  313.             end
  314.             checkbox.Visible = boolean
  315.             if checkbox.Visible == true then
  316.                 if checkbox.State == true then
  317.                     checkbox.Draw("active")
  318.                 elseif checkbox.State == false then
  319.                     checkbox.Draw()
  320.                 end
  321.             end
  322.         end
  323.         function checkbox.SetPosition(x, y) --Change object's position
  324.             if x == nil or y == nil then
  325.                 error("Not enough parameters!", 2)
  326.             end
  327.             if type(x) ~= "number" or type(y) ~= "number" then
  328.                 error("The type of parameters must be 'number', got '"..type(x).."' and '"..type(y).."'.", 2)
  329.             end
  330.             checkbox.xPos = x
  331.             checkbox.yPos = y
  332.             if checkbox.Visible == true then
  333.                 if checkbox.State == true then
  334.                     checkbox.Draw("active")
  335.                 elseif checkbox.State == false then
  336.                     checkbox.Draw()
  337.                 end
  338.             end
  339.         end
  340.         function checkbox.GetText()
  341.             return checkbox.Text
  342.         end
  343.         function checkbox.SetText(text)
  344.             if text == nil then
  345.                 checkbox.Text = ""
  346.             elseif type(text) == "string" then
  347.                 checkbox.Text = text
  348.             else
  349.                 error("The type of text must be 'string', got '"..type(text).."'.", 2)
  350.             end
  351.             if checkbox.Visible == true then
  352.                 if checkbox.State == true then
  353.                     checkbox.Draw("active")
  354.                 elseif checkbox.State == false then
  355.                     checkbox.Draw()
  356.                 end
  357.             end
  358.         end
  359.         function checkbox.GetPosition() --Function for getting position of the check box object
  360.             return checkbox.xPos, checkbox.yPos
  361.         end
  362.         function checkbox.GetColors() --Returns the object's colors
  363.             return checkbox.TextColor, checkbox.BackgroundColor, checkbox.BoxColor, checkbox.CheckedColor, checkbox.InactiveBoxColor
  364.         end
  365.         function checkbox.SetColors(textColor, backColor, boxColor, clickColor, inactColor) --Sets the colors of the check box object
  366.             if textColor ~= nil and type(textColor) == "number" then
  367.                 checkbox.TextColor = textColor
  368.             end
  369.             if backColor ~= nil and type(backColor) == "number" then
  370.                 checkbox.BackgroundColor = backColor
  371.             end
  372.             if boxColor ~= nil and type(boxColor) == "number" then
  373.                 checkbox.BoxColor = boxColor
  374.             end
  375.             if clickColor ~= nil and type(clickColor) == "number" then
  376.                 checkbox.CheckedColor = clickColor
  377.             end
  378.             if inactColor ~= nil and type(inactColor) == "number" then
  379.                 checkbox.InactiveBoxColor = inactColor
  380.             end
  381.             if checkbox.Visible == true then
  382.                 if checkbox.State == true then
  383.                     checkbox.Draw("active")
  384.                 elseif checkbox.State == false then
  385.                     checkbox.Draw()
  386.                 end
  387.             end
  388.         end
  389.         function checkbox.ChangeState(state) --This function changes the state of check box object.
  390.             if state == nil then
  391.                 checkbox.State = not checkbox.State
  392.             elseif state == true then
  393.                 checkbox.State = true
  394.             elseif state == false then
  395.                 checkbox.State = false
  396.             else
  397.                 error("The type of the check box state must be 'boolean',got '"..type(state).."'.", 2)
  398.             end
  399.             if checkbox.Visible == true then
  400.                 if checkbox.State == true then
  401.                     checkbox.Draw("active")
  402.                 elseif checkbox.State == false then
  403.                     checkbox.Draw()
  404.                 end
  405.             end
  406.         end
  407.         function checkbox.GetState() --Returns the state of the check box
  408.             return checkbox.State
  409.         end
  410.         function checkbox.Detect(action, mouseButton, x, y) --Function for actions with check box
  411.             local t = term.current()
  412.             term.redirect(checkbox.Environment)
  413.             local event, param1, param2, param3
  414.             if x == nil then
  415.                 event, param1, param2, param3 = os.pullEvent(action)
  416.             else
  417.                 param1 = mouseButton
  418.                 param2 = x
  419.                 param3 = y
  420.             end
  421.             if (x ~= nil and type(x) ~= "number") or (y ~= nil and type(y) ~= "number") then
  422.                 error("Coordinates must be 'number', got '"..type(x).."' and '"..type(y).."'.", 2)
  423.             end
  424.             if action == "mouse_click" then --Click action. It returns true if it is clicked or false if it isn't clicked and check box state.
  425.                 if type(mouseButton) ~= "number" or mouseButton > 2 or mouseButton < 1 then
  426.                     error("The value after 'mouse_click' action must be 'number' from 1 to 2!, got '"..type(mouseButton).."' = "..mouseButton..".", 2)
  427.                 end
  428.                 if param1 == mouseButton and param2 == checkbox.xPos and param3 == checkbox.yPos then
  429.                     checkbox.ChangeState()
  430.                     return true, checkbox.State
  431.                 else
  432.                     return false, checkbox.State
  433.                 end
  434.             elseif action == "mouse_scroll" then --Scroll action. It returns true and direction when you scroll on the check box object and returns false if not.
  435.                 if param2 == checkbox.xPos and param3 == checkbox.yPos then
  436.                     return true, param1
  437.                 else
  438.                     return false, param1
  439.                 end
  440.             elseif action == "mouse_drag" then --Drag action. It returns true if mouse is dragged on the check box object or false if not.
  441.                 if type(mouseButton) ~= "number" or mouseButton > 2 or mouseButton < 1 then
  442.                     error("The value after 'mouse_click' action must be 'number' from 1 to 2!, got '"..type(mouseButton).."' = "..mouseButton..".", 2)
  443.                 end
  444.                 if param1 == mouseButton and param2 == checkbox.xPos and param3 == checkbox.yPos then
  445.                     checkbox.ChangeState()
  446.                     return true, checkbox.State
  447.                 else
  448.                     return false, checkbox.State
  449.                 end
  450.             elseif action == "monitor_touch" then --Monitor touch action. It returns true if check box is touched on a monitor or false if not.
  451.                 if param1 == "front" and param2 == checkbox.xPos and param3 == checkbox.yPos then
  452.                     checkbox.ChangeState()
  453.                     return true, checkbox.State
  454.                 else
  455.                     return false, checkbox.State
  456.                 end
  457.             else
  458.                 error("Unknown action!", 2)
  459.             end
  460.             term.redirect(t)
  461.         end
  462.         --Creating an object.
  463.         table.insert(List, 1, checkbox.Object)
  464.         if checkbox.Visible == true then
  465.             checkbox.Draw()
  466.         end
  467.         return checkbox
  468.     end,
  469.     Image = function(env, x, y, w, h, timage, backColor, borderStyle, borderColor, uvx, uvy, visible) --Creates a new Image object
  470.         if type(env) ~= "table" then
  471.             error("Please, set a valid environment!", 2)
  472.         end
  473.         if type(x) ~= "number" or type(y) ~= "number" or type(w) ~= "number" or type(h) ~= "number" then
  474.             error("The type of coordinates and resolution must be 'number', got '"..type(x).."', '"..type(y).."', '"..type(w).."' and '"..type(h).."'.", 2)
  475.         end
  476.         if (uvx ~= nil and type(uvx) ~= "number") or (uvy ~= nil and type(uvy) ~= "number") then
  477.             error("The type of parameters 'uvx' and 'uvy' must be 'number', got '"..type(uvx).."' and '"..type(uvy).."'.", 2)
  478.         end
  479.         if uvx == nil then
  480.             uvx = 0
  481.         end
  482.         if uvy == nil then
  483.             uvy = 0
  484.         end
  485.         if type(visible) ~= "boolean" and visible ~= nil then
  486.             error("The type of visibility parameter must be 'boolean', got '"..type(visible).."'.", 2)
  487.         end
  488.         if visible == nil then
  489.             visible = true
  490.         end
  491.         if type(timage) ~= "string" and type(timage) ~= "table" then
  492.             error("The image type must be an image path or an image table as 'string' or 'table', got '"..type(timage).."'.", 2)
  493.         end
  494.         local path
  495.         if type(timage) == "string" then
  496.             path = timage
  497.             timage = paintutils.loadImage(timage)
  498.         else
  499.             path = nil
  500.         end
  501.         if borderStyle ~= "none" and borderStyle ~= "box" and borderStyle ~= "lines" and borderStyle ~= nil then
  502.             error("The parameter 'borderStyle' must be 'string', got '"..type(borderStyle).."'.", 2)
  503.         end
  504.         if borderStyle == nil then
  505.             borderStyle = "none"
  506.         end
  507.         if (backColor ~= nil and type(borderColor) ~= "number") or (borderColor ~= nil and type(borderColor) ~= "number") then
  508.             error("The type of the colors must be 'number', got '"..type(backColor).."' and '"..type(borderColor).."'.", 2)
  509.         end
  510.         if borderColor == nil then
  511.             borderColor = colors.gray
  512.         end
  513.         local image = {}
  514.         image.Path = path
  515.         image.Visible = visible
  516.         image.Object = "image"
  517.         image.Environment = env
  518.         image.xPos = x
  519.         image.yPos = y
  520.         image.Width = w
  521.         image.Height = h
  522.         image.Image = timage
  523.         image.BorderStyle = borderStyle
  524.         image.BorderColor = borderColor
  525.         image.BackgroundColor = backColor
  526.         image.UVX = uvx
  527.         image.UVY = uvy
  528.         --Helper functions
  529.         local function colorText(x, y, color, backColor, value) --The easy way to make colour text
  530.             local xs, ys = term.getCursorPos()
  531.             term.setCursorPos(x, y)
  532.             term.setTextColor(color)
  533.             term.setBackgroundColor(backColor)
  534.             term.write(value)
  535.             term.setCursorPos(xs, ys)
  536.         end
  537.         --Image functions
  538.         function image.Draw(state) --Draws an image object
  539.             local t = term.current()
  540.             term.redirect(image.Environment)
  541.             if image.BackgroundColor ~= nil then
  542.                 paintutils.drawFilledBox(image.xPos, image.yPos, image.xPos+image.Width, image.yPos+image.Height, image.BackgroundColor)
  543.             end
  544.             local image3 = image.Image
  545.             local image2 = {}
  546.             local i, k, h, w
  547.             if image.UVY > 0 then
  548.                 for i=1, image.UVY do
  549.                     table.remove(image3, 1)
  550.                 end
  551.             end
  552.             if image.UVX > 0 then
  553.                 for i=1, #image3 do
  554.                     for k=1, image.UVX do
  555.                         table.remove(image3[i], 1)
  556.                     end
  557.                 end
  558.             end
  559.             if image.UVY < 0 then
  560.                 for i=1, (-1)*image.UVY do
  561.                     table.insert(image3, 1, {})
  562.                 end
  563.             end
  564.             if image.UVX < 0 then
  565.                 for i=1, #image3 do
  566.                     for k=1, (-1)*image.UVX do
  567.                         table.insert(image3[i], 1, 0)
  568.                     end
  569.                 end
  570.             end
  571.             if #image3 < image.Height+1 then
  572.                 h = #image3
  573.             else
  574.                 h = image.Height+1
  575.             end
  576.             for i=1, h do
  577.                 if #image3[i] < image.Width +1 then
  578.                     w = #image3[i]
  579.                 else
  580.                     w  = image.Width+1
  581.                 end
  582.                 image2[i] = {}
  583.                 for k=1, w do
  584.                     image2[i][k] = image3[i][k]
  585.                 end
  586.             end
  587.             paintutils.drawImage(image2, image.xPos, image.yPos)
  588.             if image.BorderStyle == "none" then
  589.             elseif image.BorderStyle == "box" then
  590.                 paintutils.drawBox(image.xPos-1, image.yPos-1, image.xPos+image.Width+1, image.yPos+image.Height+1, image.BorderColor)
  591.             elseif image.BorderStyle == "lines" then
  592.                 term.setBackgroundColor(image.BorderColor)
  593.                 term.setTextColor(colors.white)
  594.                 for i=0, image.Width do
  595.                     term.setCursorPos(image.xPos+i, image.yPos-1)
  596.                     term.write("-")
  597.                     term.setCursorPos(image.xPos+i, image.yPos+image.Height+1)
  598.                     term.write("-")
  599.                 end
  600.                 for i=0, image.Height do
  601.                     term.setCursorPos(image.xPos-1, image.yPos+i)
  602.                     term.write("|")
  603.                     term.setCursorPos(image.xPos+image.Width+1, image.yPos+i)
  604.                     term.write("|")
  605.                 end
  606.                 term.setCursorPos(image.xPos-1, image.yPos-1)
  607.                 term.write("+")
  608.                 term.setCursorPos(image.xPos+image.Width+1, image.yPos-1)
  609.                 term.write("+")
  610.                 term.setCursorPos(image.xPos-1, image.yPos+image.Height+1)
  611.                 term.write("+")
  612.                 term.setCursorPos(image.xPos+image.Width+1, image.yPos+image.Height+1)
  613.                 term.write("+")
  614.             else
  615.             end
  616.             term.setBackgroundColor(colors.black)
  617.             term.redirect(t)
  618.         end
  619.         function image.GetVisibility() --This function returns the visibility state
  620.             return image.Visible
  621.         end
  622.         function image.SetVisibility(boolean) --This function sets the visibility state
  623.             if type(boolean) ~= "boolean" then
  624.                 error("The type of the visibility parameter must be 'boolean', got '"..type(boolean).."'.", 2)
  625.             end
  626.             image.Visible = boolean
  627.             if image.Visible == true then
  628.                 image.Draw()
  629.             end
  630.         end
  631.         function image.SetPosition(x, y, w, h) --Change image's position
  632.             if x == nil or y == nil then
  633.                 error("Not enough parameters!", 2)
  634.             end
  635.             if type(x) ~= "number" or type(y) ~= "number" then
  636.                 error("Coordinates x and y must be 'number', got '"..type(x).."' and '"..type(y).."'.", 2)
  637.             end
  638.             image.xPos = x
  639.             image.yPos = y
  640.             if w ~= nil and type(w) == "number" then
  641.                 image.Width = w
  642.             end
  643.             if h ~= nil and type(h) == "number" then
  644.                 image.Height = h
  645.             end
  646.             if image.Visible == true then
  647.                 image.Draw()
  648.             end
  649.         end
  650.         function image.GetPosition() --Returns image's position
  651.             return image.xPos, image.yPos, image.Width, image.Height
  652.         end
  653.         function image.GetColors() --Returns the image's colors
  654.             return image.BackgroundColor, image.BorderColor
  655.         end
  656.         function image.SetColors(backColor, borderColor) --Sets the colors of the image object
  657.             if backColor ~= nil and type(backColor) == "number" then
  658.                 image.BackgroundColor = backColor
  659.             end
  660.             if borderColor ~= nil and type(borderColor) == "number" then
  661.                 image.BorderColor = borderColor
  662.             end
  663.             if image.Visible == true then
  664.                 image.Draw()
  665.             end
  666.         end
  667.         function image.SetBorderStyle(borderStyle) --Sets border style
  668.             if type(borderStyle) == "string" then
  669.                 image.SetBorderStyle = borderStyle
  670.             else
  671.                 error("The type of the parameter 'borderStyle' must be 'string', got '"..type(borderStyle).."'.", 2)
  672.             end
  673.             if image.Visible == true then
  674.                 image.Draw()
  675.             end
  676.         end
  677.         function image.GetBorderStyle() --Returns image's border style.
  678.             return image.BorderStyle
  679.         end
  680.         function image.GetImage(timage) --Returns image object's image as table or path (string).
  681.             if timage == "table" or timage == nil then
  682.                 return image.Image
  683.             elseif timage == "path" then
  684.                 return image.Path
  685.             else
  686.                 error("The parameter must be 'table', 'path' or nil, got '"..timage.."'.", 2)
  687.             end
  688.         end
  689.         function image.SetImage(timage) --Sets the image object's image
  690.             if type(timage) == "string" then
  691.                 local path = timage
  692.                 timage = paintutils.loadImage(timage)
  693.                 image.Path = path
  694.                 image.Image = timage
  695.             elseif type(timage) == "table" then
  696.                 image.Path = nil
  697.                 image.Image = timage
  698.             else
  699.                 error("The type of the parameter must be 'string' or 'table', got '"..type(timage).."'.", 2)
  700.             end
  701.             if image.Visible == true then
  702.                 image.Draw()
  703.             end
  704.         end
  705.         function image.GetOffset() --Returns something.
  706.             return image.UVX, image.UVY
  707.         end
  708.         function image.SetOffset(uvx, uvy) --Sets something.
  709.             if type(uvx) ~= "number" or type(uvy) ~= "number" then
  710.                 error("The type of the parameters 'uvx' and 'uvy' must be 'number', got '"..type(uvx).."' and '"..type(uvy).."'.", 2)
  711.             end
  712.             image.UVX = uvx
  713.             image.UVY = uvy
  714.             if image.Visible == true then
  715.                 image.Draw()
  716.             end
  717.         end
  718.         function image.AddOffset(uvx, uvy) --Ads to something.
  719.             if type(uvx) ~= "number" or type(uvy) ~= "number" then
  720.                 error("The type of the parameters 'uvx' and 'uvy' must be 'number', got '"..type(uvx).."' and '"..type(uvy).."'.", 2)
  721.             end
  722.             image.UVX = uvx + image.UVX
  723.             image.UVY = uvy + image.UVY
  724.             if image.Visible == true then
  725.                 image.Draw()
  726.             end
  727.         end
  728.         function image.Detect(action, mouseButton, x, y) --Function for actions with image
  729.             local t = term.current()
  730.             term.redirect(image.Environment)
  731.             local event, param1, param2, param3
  732.             if x == nil then
  733.                 event, param1, param2, param3 = os.pullEvent(action)
  734.             else
  735.                 param1 = mouseButton
  736.                 param2 = x
  737.                 param3 = y
  738.             end
  739.             if (x ~= nil and type(x) ~= "number") or (y ~= nil and type(y) ~= "number") then
  740.                 error("Coordinates must be 'number', got '"..type(x).."' and '"..type(y).."'.", 2)
  741.             end
  742.             if action == "mouse_click" then --Click action. It returns true if it is clicked or false if it isn't clicked
  743.                 if type(mouseButton) ~= "number" or mouseButton > 2 or mouseButton < 1 then
  744.                     error("The value after 'mouse_click' action must be number from 1 to 2, got '"..type(mouseButton).."'.", 2)
  745.                 end
  746.                 if param1 == mouseButton and param2 >= image.xPos and param2 <= image.xPos+image.Width and param3 >= image.yPos and param3 <= image.yPos+image.Height then
  747.                     if image.Visible == true then
  748.                         image.Draw()
  749.                     end
  750.                     return true
  751.                 else
  752.                     return false
  753.                 end
  754.             elseif action == "mouse_scroll" then --Scroll action. It returns true and direction when you scroll on the image object and returns false if not.
  755.                 if param2 >= image.xPos and param2 <= image.xPos+image.Width and param3 >= image.yPos and param3 <= image.yPos+image.Height then
  756.                     if image.Visible == true then
  757.                         image.Draw()
  758.                     end
  759.                     return true, param1
  760.                 else
  761.                     return false
  762.                 end
  763.             elseif action == "mouse_drag" then --Drag action. It returns true if mouse is dragged on the image object or false if not.
  764.                 if type(mouseButton) ~= "number" or mouseButton > 2 or mouseButton < 1 then
  765.                     error("The value after 'mouse_click' action must be number from 1 to 2, got '"..type(mouseButton).."'.", 2)
  766.                 end
  767.                 if param1 == mouseButton and param2 >= image.xPos and param2 <= image.xPos+image.Width and param3 >= image.yPos and param3 <= image.yPos+image.Height then
  768.                     if image.Visible == true then
  769.                         image.Draw()
  770.                     end
  771.                     return true
  772.                 else
  773.                     return false
  774.                 end
  775.             elseif action == "monitor_touch" then --Monitor touch action. It returns true if image is touched on a monitor or false if not.
  776.                 if param1 == "front" and param2 >= image.xPos and param2 <= image.xPos+image.Width and param3 >= image.yPos and param3 <= image.yPos+image.Height then
  777.                     if image.Visible == true then
  778.                         image.Draw()
  779.                     end
  780.                     return true
  781.                 else
  782.                     return false
  783.                 end
  784.             else
  785.                 error("Unknown action!")
  786.             end
  787.             term.redirect(t)
  788.         end
  789.         --Creating image object
  790.         table.insert(List, 1, image.Object)
  791.         if image.Visible == true then
  792.             image.Draw()
  793.         end
  794.         return image
  795.     end,
  796.     Label = function(env, x, y, text, textColor, backColor, state, visible) --Creates a new label object.
  797.         if type(env) ~= "table" then
  798.             error("Please, set a valid environment!", 2)
  799.         end
  800.         if type(x) ~= "number" or type(y) ~= "number" then
  801.             error("The type of the coordinates and must be 'number', got '"..type(x).."' and '"..type(y).."'.", 2)
  802.         end
  803.         if type(text) ~= "string" and text ~= nil then
  804.             error("The type of the text parameter must be 'string', got '"..type(text).."'.", 2)
  805.         end
  806.         if (type(textColor) ~= "number" and textColor ~= nil) or (type(backColor) ~= "number" and backColor ~= nil) then
  807.             error("The type of the colours parameters must be 'number', got '"..type(textColor).."' and '"..type(backColor).."'.", 2)
  808.         end
  809.         if state ~= nil and type(state) ~= "string" then
  810.             error("The type of the parameter 'state' must be 'string' or nil, got '"..type(state).."'.", 2)
  811.         end
  812.         if type(visible) ~= "boolean" and visible ~= nil then
  813.             error("The type the of visibility parameter must be 'boolean', got '"..type(visible).."'.", 2)
  814.         end
  815.         if visible == nil then
  816.             visible = true
  817.         end
  818.         if text == nil then
  819.             text = ""
  820.         end
  821.         if textColor == nil then
  822.             textColor = colors.white
  823.         end
  824.         if backColor == nil then
  825.             backColor = colors.black
  826.         end
  827.         if state == nil then
  828.             state = "middle"
  829.         end
  830.         label = {}
  831.         label.Object = "label"
  832.         label.Environment = env
  833.         label.xPos = x
  834.         label.yPos = y
  835.         label.Text = text
  836.         label.TextColor = textColor
  837.         label.BackgroundColor = backColor
  838.         label.State = state
  839.         label.Visible = visible
  840.         --Helper functions
  841.         local function colorText(x, y, color, backColor, value) --The easy way to make colour text
  842.             local xs, ys = term.getCursorPos()
  843.             term.setCursorPos(x, y)
  844.             term.setTextColor(color)
  845.             term.setBackgroundColor(backColor)
  846.             term.write(value)
  847.             term.setCursorPos(xs, ys)
  848.         end
  849.         --Label functions
  850.         function label.Draw() --Draws label object.
  851.             local t = term.current()
  852.             term.redirect(label.Environment)
  853.             local text = {}
  854.             local i = 0
  855.             for i=1, #label.Text do
  856.                 text[i] = string.sub(label.Text, i, i)
  857.             end
  858.             if label.State == "middle" then
  859.                 colorText(label.xPos-#label.Text/2, label.yPos, label.TextColor, label.BackgroundColor, label.Text)
  860.             elseif label.State == "left" then
  861.                 colorText(label.xPos-#label.Text, label.yPos, label.TextColor, label.BackgroundColor, label.Text)
  862.             elseif label.State == "right" then
  863.                 colorText(label.xPos, label.yPos, label.TextColor, label.BackgroundColor, label.Text)
  864.             else
  865.                 error("The parameter 'label.State' must have value 'middle' or 'left' or 'right', got '"..label.State.."'.", 2)
  866.             end
  867.             term.redirect(t)
  868.         end
  869.         function label.GetVisibility() --This function returns the visibility state
  870.             return label.Visible
  871.         end
  872.         function label.SetVisibility(boolean) --This function sets the visibility state
  873.             if type(boolean) ~= "boolean" then
  874.                 error("The type of the parameter must be boolean, got '"..type(boolean).."'.", 2)
  875.             end
  876.             label.Visible = boolean
  877.             if label.Visible == true then
  878.                 label.Draw()
  879.             end
  880.         end
  881.         function label.SetPosition(x, y) --Change label's position
  882.             if x == nil or y == nil then
  883.                 error("Not enough parameters!", 2)
  884.             end
  885.             if type(x) ~= "number" or type(y) ~= "number" then
  886.                 error("Coordinates x and y must be 'number', got '"..type(x).."' and '"..type(y).."'.", 2)
  887.             end
  888.             label.xPos = x
  889.             label.yPos = y
  890.             if label.Visible == true then
  891.                 label.Draw()
  892.             end
  893.         end
  894.         function label.GetPosition() --Returns label's position
  895.             return label.xPos, label.yPos
  896.         end
  897.         function label.GetColors() --Returns the label's colors
  898.             return label.TextColor, label.BackgroundColor
  899.         end
  900.         function label.SetColors(textColor, backColor) --Sets the colors of the label object
  901.             if textColor ~= nil and type(textColor) == "number" then
  902.                 label.TextColor = textColor
  903.             end
  904.             if backColor ~= nil and type(backColor) == "number" then
  905.                 label.BackgroundColor = backColor
  906.             end
  907.             if label.Visible == true then
  908.                 label.Draw()
  909.             end
  910.         end
  911.         function label.GetText()
  912.             return label.Text
  913.         end
  914.         function label.SetText(text)
  915.             if text == nil then
  916.                 label.Text = ""
  917.             elseif type(text) == "string" then
  918.                 label.Text = text
  919.             else
  920.                 error("The text parameter must be 'string', got '"..type(text).."'.", 2)
  921.             end
  922.             if label.Visible == true then
  923.                 label.Draw()
  924.             end
  925.         end
  926.         function label.GetState()
  927.             return label.State
  928.         end
  929.         function label.SetState(state)
  930.             if state ~= "middle" and state ~= "right" and state ~= "left" then
  931.                 error("The state must be 'middle', 'right' or 'left', got '"..state.."'.", 2)
  932.             end
  933.             label.State = state
  934.             if label.Visible == true then
  935.                 label.Draw()
  936.             end
  937.         end
  938.         --Creating label object
  939.         table.insert(List, 1, label.Object)
  940.         if label.Visible == true then
  941.             label.Draw()
  942.         end
  943.         return label
  944.     end,
  945.     Process = function(env, x, y, w, h, program, sh, msh, workDir, ...) --Creates a new process object.
  946.         if type(env) ~= "table" then
  947.             error("Please, set a valid environment!", 2)
  948.         end
  949.         if type(x) ~= "number" or type(y) ~= "number" or type(w) ~= "number" or type(h) ~= "number" then
  950.             error("The type of coordinates and resolution must be 'number', got '"..type(x).."', '"..type(y).."', '"..type(w).."' and '"..type(h).."'.", 2)
  951.         end
  952.         if type(program) ~= "string" then
  953.             error("The type of the process path must be 'string', got'"..type(program).."'.", 2)
  954.         end
  955.         if type(sh) ~= "table" then
  956.             error("It's strange but you need to pass the shell API.", 2)
  957.         end
  958.         if type(msh) ~= "table" and msh ~= nil then
  959.             error("Please set a valid multishell API.", 2)
  960.         end
  961.         if not fs.exists(program) or fs.isDir(program) then
  962.             error("Couldn't find process by this path.", 2)
  963.         end
  964.         if type(workDir) ~= "string" and workDir ~= nil then
  965.             error("The type of the working directory path must be 'string', got'"..type(workDir).."'.", 2)
  966.         end
  967.         if workDir ~= nil then
  968.             if not fs.isDir(workDir) then
  969.                 error("Couldn't find a directory by this path.", 2)
  970.             end
  971.         end
  972.         if workDir == nil then
  973.             workDir = "/Temp/"
  974.             if fs.exists("/Temp/") and not fs.isDir("/Temp/") then
  975.                 error("Couldn't make a directory with default path '/Temp/'.", 2)
  976.             end
  977.             if not fs.isDir("/Temp/") then
  978.                 fs.makeDir("/Temp/")
  979.             end
  980.         end
  981.         if msh == nil then
  982.             msh = false
  983.         end
  984.         local process = {}
  985.         process.Object = "process"
  986.         process.Environment = env
  987.         process.xPos = x
  988.         process.yPos = y
  989.         process.Width = w
  990.         process.Height = h
  991.         process.Process = program
  992.         process.WorkingDir = workDir
  993.         process.Window = window.create(process.Environment, process.xPos, process.yPos, process.Width, process.Height, false)
  994.         process.ShellAPI = sh
  995.         process.Multishell = msh
  996.         process.Arguments = ...
  997.         process.IsRunning = false
  998.         --Process functions
  999.         function process.Start(state) --Starts the process X, Y = term.getSize()  P = object.New.Process(term.current(), 1, 1, X, Y, "/Programs/Sketch", shell, multishell, test)
  1000.             process.IsRunning = true
  1001.             shell = process.ShellAPI
  1002.             multishell = process.Multishell
  1003.             local sh = shell.dir()
  1004.             local name, boolean, err
  1005.             if multishell then
  1006.                 name = multishell.getTitle(multishell.getCurrent())
  1007.                 multishell.setTitle(multishell.getCurrent(), fs.getName(process.Process))
  1008.             end
  1009.             local t = term.current()
  1010.             if type(state) == "boolean" then
  1011.                 process.Window.setVisible(state)
  1012.             else
  1013.                 process.Window.setVisible(true)
  1014.             end
  1015.             term.redirect(process.Window)
  1016.             shell.setDir(process.WorkingDir)
  1017.             if process.Arguments ~= nil then
  1018.                 boolean = shell.run(process.Process, process.Arguments)
  1019.             else
  1020.                 boolean, err = pcall(function() dofile(process.Process) end)
  1021.             end
  1022.             shell.setDir(sh)
  1023.             if multishell then
  1024.                 multishell.setTitle(multishell.getCurrent(), name)
  1025.             end
  1026.             term.redirect(t)
  1027.             process.Window.setVisible(false)
  1028.             t.redraw()
  1029.             process.IsRunning = false
  1030.             return boolean, err
  1031.         end
  1032.         function process.SetPosition(x, y, w, h) --Change process's position
  1033.             if x == nil or y == nil then
  1034.                 error("Not enough parameters!", 2)
  1035.             end
  1036.             if type(x) ~= "number" or type(y) ~= "number" then
  1037.                 error("Coordinates x and y must be 'number', got '"..type(x).."' and '"..type(y).."'.", 2)
  1038.             end
  1039.             process.xPos = x
  1040.             process.yPos = y
  1041.             if w ~= nil and type(w) == "number" then
  1042.                 process.Width = w
  1043.             end
  1044.             if h ~= nil and type(h) == "number" then
  1045.                 process.Height = h
  1046.             end
  1047.             process.Window.reposition(process.xPos, process.yPos, process.Width, process.Height)
  1048.             process.Window.redraw()
  1049.         end
  1050.         function process.GetPosition() --Returns the process position
  1051.             return process.xPos, process.yPos, process.Width, process.Height
  1052.         end
  1053.         function process.SetProcess(program, ...) --Changes the process path and arguments
  1054.             if type(program) ~= "string" then
  1055.                 error("The type of the process path must be 'string', got'"..type(program).."'.")
  1056.             end
  1057.             process.Process = program
  1058.             process.Arguments = ...
  1059.         end
  1060.         function process.GetProcess() --Returns the process path and arguments
  1061.             return process.Process, process.Arguments
  1062.         end
  1063.         function process.GetRunning() --Returns the running state
  1064.             return process.IsRunning
  1065.         end
  1066.         function process.SetWorkDir(workDir) --Sets the shell working directory for process
  1067.             if workDir ~= nil then
  1068.                 if not fs.isDir(workDir) then
  1069.                     error("Couldn't find a directory by this path.", 2)
  1070.                 end
  1071.             end
  1072.             if workDir == nil then
  1073.                 workDir = "/Temp/"
  1074.                 if fs.exists("/Temp/") and not fs.isDir("/Temp/") then
  1075.                     error("Couldn't make a directory with default path '/Temp/'.", 2)
  1076.                 end
  1077.                 if not fs.isDir("/Temp/") then
  1078.                     fs.makeDir("/Temp/")
  1079.                 end
  1080.             end
  1081.             process.WorkingDir = workDir
  1082.         end
  1083.         function process.GetWorkDir() --Returns the shell working directory for process
  1084.             return process.WorkingDir
  1085.         end
  1086.         --Creating process object
  1087.         table.insert(List, 1, process.Object)
  1088.         return process
  1089.     end
  1090. }
  1091. --Functions for all objects.
  1092. function Type(obj) --Returns the type of object.
  1093.     if obj ~= nil then
  1094.         if type(obj.Object) == "string" then
  1095.             return obj.Object
  1096.         end
  1097.     end
  1098. end
  1099. function Count(obj) --Returns the amount of created objects.
  1100.     if obj == nil then
  1101.         return #List
  1102.     else
  1103.         local i, count = 0, 0
  1104.         for i=0, #List do
  1105.             if List[i] == obj then
  1106.                 count = count+1
  1107.             end
  1108.         end
  1109.         return count
  1110.     end
  1111. end
Advertisement
Add Comment
Please, Sign In to add comment