Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --v1.0
- --Setup
- local List = {}
- --Objects
- New = {
- Button = function(env, x, y, w, h, text, textColor, backColor, clickColor, inactColor, visible) --Creates a new button object
- if type(env) ~= "table" then
- error("Please, set a valid environment!", 2)
- end
- if type(x) ~= "number" or type(y) ~= "number" or type(w) ~= "number" or type(h) ~= "number" then
- error("The type of coordinates and resolution must be 'number', got '"..type(x).."', '"..type(y).."', '"..type(w).."' and '"..type(h).."'.", 2)
- end
- if type(text) ~= "string" and text ~= nil then
- error("The type of text parameter must be 'string', got '"..type(text).."'.", 2)
- end
- 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
- error("The type of colours parameters must be 'number', got '"..type(textColor).."', '"..type(backColor).."', '"..type(clickColor).."' and '"..type(inactColor).."'.", 2)
- end
- if type(visible) ~= "boolean" and visible ~= nil then
- error("The type of visibility parameter must be 'boolean', got '"..type(visible).."'.", 2)
- end
- if text == nil then
- text = ""
- end
- if visible == nil then
- visible = true
- end
- if textColor == nil then
- textColor = colors.white
- end
- if backColor == nil then
- backColor = colors.blue
- end
- if clickColor == nil then
- clickColor = colors.green
- end
- if inactColor == nil then
- inactColor = colors.gray
- end
- local button = {}
- button.Visible = visible
- button.Object = "button"
- button.Environment = env
- button.xPos = x
- button.yPos = y
- button.Width = w
- button.Height = h
- button.Text = text
- button.TextColor = textColor
- button.BackgroundColor = backColor
- button.ActivateColor = clickColor
- button.InactiveColor = inactColor
- --Helper functions
- local function colorText(x, y, color, backColor, value) --The easy way to make colour text
- local xs, ys = term.getCursorPos()
- term.setCursorPos(x, y)
- term.setTextColor(color)
- term.setBackgroundColor(backColor)
- term.write(value)
- term.setCursorPos(xs, ys)
- end
- --Button functions
- function button.Draw(state) --Draws button as term object
- local t = term.current()
- term.redirect(button.Environment)
- if state == nil then
- paintutils.drawFilledBox(button.xPos, button.yPos, button.xPos+button.Width, button.yPos+button.Height, button.BackgroundColor)
- colorText(button.xPos+button.Width/2-(#button.Text/2)+1, button.yPos+button.Height/2, button.TextColor, button.BackgroundColor, button.Text)
- elseif state == "active" then
- paintutils.drawFilledBox(button.xPos, button.yPos, button.xPos+button.Width, button.yPos+button.Height, button.ActivateColor)
- colorText(button.xPos+button.Width/2-(#button.Text/2)+1, button.yPos+button.Height/2, button.TextColor, button.ActivateColor, button.Text)
- elseif state == "inactive" then
- paintutils.drawFilledBox(button.xPos, button.yPos, button.xPos+button.Width, button.yPos+button.Height, button.InactiveColor)
- colorText(button.xPos+button.Width/2-(#button.Text/2)+1, button.yPos+button.Height/2, button.TextColor, button.InactiveColor, button.Text)
- else
- error("The button state parameter must be 'active', 'inactive' or 'nil', got '"..state.."'.", 2)
- end
- term.setBackgroundColor(colors.black)
- term.redirect(t)
- end
- function button.GetVisibility() --This function returns the visibility state
- return button.Visible
- end
- function button.SetVisibility(boolean) --This function sets the visibility state
- if type(boolean) ~= "boolean" then
- error("The parameter must be boolean, got '"..type(boolean).."'.", 2)
- end
- button.Visible = boolean
- if button.Visible == true then
- button.Draw()
- end
- end
- function button.SetPosition(x, y, w, h) --Change button's position
- if x == nil or y == nil then
- error("Not enough parameters!", 2)
- end
- if type(x) ~= "number" or type(y) ~= "number" then
- error("Coordinates x and y must be 'number', got '"..type(x).."' and '"..type(y).."'.", 2)
- end
- button.xPos = x
- button.yPos = y
- if w ~= nil and type(w) == "number" then
- button.Width = w
- end
- if h ~= nil and type(h) == "number" then
- button.Height = h
- end
- if button.Visible == true then
- button.Draw()
- end
- end
- function button.GetText()
- return button.Text
- end
- function button.SetText(text)
- if text == nil then
- button.Text = ""
- elseif type(text) == "string" then
- button.Text = text
- else
- error("The text parameter must be 'string', got '"..type(text).."'.", 2)
- end
- if button.Visible == true then
- button.Draw()
- end
- end
- function button.GetPosition() --Function for getting position of the button object
- return button.xPos, button.yPos, button.Width, button.Height
- end
- function button.GetColors() --Returns the button's colors
- return button.TextColor, button.BackgroundColor, button.ActivateColor, button.InactiveColor
- end
- function button.SetColors(textColor, backColor, clickColor, inactColor) --Sets the colors of the button object
- if textColor ~= nil and type(textColor) == "number" then
- button.TextColor = textColor
- end
- if backColor ~= nil and type(backColor) == "number" then
- button.BackgroundColor = backColor
- end
- if clickColor ~= nil and type(clickColor) == "number" then
- button.ActivateColor = clickColor
- end
- if inactColor ~= nil and type(inactColor) == "number" then
- button.InactiveColor = inactColor
- end
- if button.Visible == true then
- button.Draw()
- end
- end
- function button.Detect(action, mouseButton, x, y) --Function for actions with button
- local t = term.current()
- term.redirect(button.Environment)
- local event, param1, param2, param3
- if x == nil then
- event, param1, param2, param3 = os.pullEvent(action)
- else
- param1 = mouseButton
- param2 = x
- param3 = y
- end
- if (x ~= nil and type(x) ~= "number") or (y ~= nil and type(y) ~= "number") then
- error("Coordinates must be 'number', got '"..type(x).."' and '"..type(y).."'.", 2)
- end
- if action == "mouse_click" then --Click action. It returns true if it is clicked or false if it isn't clicked
- if type(mouseButton) ~= "number" or mouseButton > 2 or mouseButton < 1 then
- error("The value after 'mouse_click' action must be 'number' from 1 to 2!, got '"..type(mouseButton).."' = "..mouseButton..".", 2)
- end
- if param1 == mouseButton and param2 >= button.xPos and param2 <= button.xPos+button.Width and param3 >= button.yPos and param3 <= button.yPos+button.Height then
- if button.Visible == true then
- button.Draw("active")
- os.sleep(0.2)
- button.Draw()
- end
- return true
- else
- return false
- end
- elseif action == "mouse_scroll" then --Scroll action. It returns true and direction when you scroll on the button object and returns false if not.
- if param2 >= button.xPos and param2 <= button.xPos+button.Width and param3 >= button.yPos and param3 <= button.yPos+button.Height then
- if button.Visible == true then
- button.Draw("active")
- os.sleep(0.2)
- button.Draw()
- end
- return true, param1
- else
- return false
- end
- elseif action == "mouse_drag" then --Drag action. It returns true if mouse is dragged on the button object or false if not.
- if type(mouseButton) ~= "number" or mouseButton > 2 or mouseButton < 1 then
- error("The value after 'mouse_click' action must be 'number' from 1 to 2!, got '"..type(mouseButton).."' = "..mouseButton..".", 2)
- end
- if param1 == mouseButton and param2 >= button.xPos and param2 <= button.xPos+button.Width and param3 >= button.yPos and param3 <= button.yPos+button.Height then
- if button.Visible == true then
- button.Draw("active")
- os.sleep(0.2)
- button.Draw()
- end
- return true
- else
- return false
- end
- elseif action == "monitor_touch" then --Monitor touch action. It returns true if button is touched on a monitor or false if not.
- if param1 == "front" and param2 >= button.xPos and param2 <= button.xPos+button.Width and param3 >= button.yPos and param3 <= button.yPos+button.Height then
- if button.Visible == true then
- button.Draw("active")
- os.sleep(0.2)
- button.Draw()
- end
- return true
- else
- return false
- end
- else
- error("Unknown action: '"..action.."'.", 2)
- end
- term.redirect(t)
- end
- --Creating button object
- table.insert(List, 1, button.Object)
- if button.Visible == true then
- button.Draw()
- end
- return button
- end,
- CheckBox = function(env, x, y, text, textColor, backColor, boxColor, clickColor, inactColor, state, visible) --Creates a new check box object.
- if type(env) ~= "table" then
- error("Please, set a valid environment!", 2)
- end
- if type(x) ~= "number" or type(y) ~= "number" then
- error("The type of coordinates must be 'number', got '"..type(x).."' and '"..type(y).."'.", 2)
- end
- if type(text) ~= "string" and text ~= nil then
- error("The type of text parameter must be 'string', got '"..type(text).."'.", 2)
- end
- 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
- error("The type of colours parameters must be 'number', got '"..type(textColor).."', '"..type(backColor).."', '"..type(clickColor).."' and '"..type(inactColor).."'.", 2)
- end
- if type(state) ~= "boolean" and state ~= nil then
- error("The type of state parameter must be 'boolean', got '"..type(state).."'.", 2)
- end
- if type(visible) ~= "boolean" and visible ~= nil then
- error("The type of visibility parameter must be 'boolean', got '"..type(visible).."'.", 2)
- end
- if visible == nil then
- visible = true
- end
- if state == nil then
- state = false
- end
- if text == nil then
- text = ""
- end
- if boxColor == nil then
- boxColor = colors.red
- end
- if textColor == nil then
- textColor = colors.white
- end
- if backColor == nil then
- backColor = colors.black
- end
- if clickColor == nil then
- clickColor = colors.green
- end
- if inactColor == nil then
- inactColor = colors.gray
- end
- local checkbox = {}
- checkbox.BoxColor = boxColor
- checkbox.Visible = visible
- checkbox.Object = "check_box"
- checkbox.Environment = env
- checkbox.xPos = x
- checkbox.yPos = y
- checkbox.Text = text
- checkbox.TextColor = textColor
- checkbox.BackgroundColor = backColor
- checkbox.CheckedColor = clickColor
- checkbox.InactiveBoxColor = inactColor
- checkbox.State = state
- --Helper functions
- local function colorText(x, y, color, backColor, value) --The easy way to make colour text
- local xs, ys = term.getCursorPos()
- term.setCursorPos(x, y)
- term.setTextColor(color)
- term.setBackgroundColor(backColor)
- term.write(value)
- term.setCursorPos(xs, ys)
- end
- --Check box functions
- function checkbox.Draw(state) --Draws check box
- local t = term.current()
- term.redirect(checkbox.Environment)
- if state == nil then
- paintutils.drawPixel(checkbox.xPos, checkbox.yPos, checkbox.BoxColor)
- colorText(checkbox.xPos+2, checkbox.yPos, checkbox.TextColor, checkbox.BackgroundColor, checkbox.Text)
- elseif state == "active" then
- paintutils.drawPixel(checkbox.xPos, checkbox.yPos, checkbox.CheckedColor)
- colorText(checkbox.xPos+2, checkbox.yPos, checkbox.TextColor, checkbox.BackgroundColor, checkbox.Text)
- elseif state == "inactive" then
- paintutils.drawPixel(checkbox.xPos, checkbox.yPos, checkbox.InactiveBoxColor)
- colorText(checkbox.xPos+2, checkbox.yPos, checkbox.InactiveBoxColor, checkbox.BackgroundColor, checkbox.Text)
- end
- term.redirect(t)
- end
- function checkbox.GetVisibility() --This function returns the visibility state
- return checkbox.Visible
- end
- function checkbox.SetVisibility(boolean) --This function sets the visibility state
- if type(boolean) ~= "boolean" then
- error("The type of parameter must be 'boolean', got '"..type(boolean).."'.", 2)
- end
- checkbox.Visible = boolean
- if checkbox.Visible == true then
- if checkbox.State == true then
- checkbox.Draw("active")
- elseif checkbox.State == false then
- checkbox.Draw()
- end
- end
- end
- function checkbox.SetPosition(x, y) --Change object's position
- if x == nil or y == nil then
- error("Not enough parameters!", 2)
- end
- if type(x) ~= "number" or type(y) ~= "number" then
- error("The type of parameters must be 'number', got '"..type(x).."' and '"..type(y).."'.", 2)
- end
- checkbox.xPos = x
- checkbox.yPos = y
- if checkbox.Visible == true then
- if checkbox.State == true then
- checkbox.Draw("active")
- elseif checkbox.State == false then
- checkbox.Draw()
- end
- end
- end
- function checkbox.GetText()
- return checkbox.Text
- end
- function checkbox.SetText(text)
- if text == nil then
- checkbox.Text = ""
- elseif type(text) == "string" then
- checkbox.Text = text
- else
- error("The type of text must be 'string', got '"..type(text).."'.", 2)
- end
- if checkbox.Visible == true then
- if checkbox.State == true then
- checkbox.Draw("active")
- elseif checkbox.State == false then
- checkbox.Draw()
- end
- end
- end
- function checkbox.GetPosition() --Function for getting position of the check box object
- return checkbox.xPos, checkbox.yPos
- end
- function checkbox.GetColors() --Returns the object's colors
- return checkbox.TextColor, checkbox.BackgroundColor, checkbox.BoxColor, checkbox.CheckedColor, checkbox.InactiveBoxColor
- end
- function checkbox.SetColors(textColor, backColor, boxColor, clickColor, inactColor) --Sets the colors of the check box object
- if textColor ~= nil and type(textColor) == "number" then
- checkbox.TextColor = textColor
- end
- if backColor ~= nil and type(backColor) == "number" then
- checkbox.BackgroundColor = backColor
- end
- if boxColor ~= nil and type(boxColor) == "number" then
- checkbox.BoxColor = boxColor
- end
- if clickColor ~= nil and type(clickColor) == "number" then
- checkbox.CheckedColor = clickColor
- end
- if inactColor ~= nil and type(inactColor) == "number" then
- checkbox.InactiveBoxColor = inactColor
- end
- if checkbox.Visible == true then
- if checkbox.State == true then
- checkbox.Draw("active")
- elseif checkbox.State == false then
- checkbox.Draw()
- end
- end
- end
- function checkbox.ChangeState(state) --This function changes the state of check box object.
- if state == nil then
- checkbox.State = not checkbox.State
- elseif state == true then
- checkbox.State = true
- elseif state == false then
- checkbox.State = false
- else
- error("The type of the check box state must be 'boolean',got '"..type(state).."'.", 2)
- end
- if checkbox.Visible == true then
- if checkbox.State == true then
- checkbox.Draw("active")
- elseif checkbox.State == false then
- checkbox.Draw()
- end
- end
- end
- function checkbox.GetState() --Returns the state of the check box
- return checkbox.State
- end
- function checkbox.Detect(action, mouseButton, x, y) --Function for actions with check box
- local t = term.current()
- term.redirect(checkbox.Environment)
- local event, param1, param2, param3
- if x == nil then
- event, param1, param2, param3 = os.pullEvent(action)
- else
- param1 = mouseButton
- param2 = x
- param3 = y
- end
- if (x ~= nil and type(x) ~= "number") or (y ~= nil and type(y) ~= "number") then
- error("Coordinates must be 'number', got '"..type(x).."' and '"..type(y).."'.", 2)
- end
- 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.
- if type(mouseButton) ~= "number" or mouseButton > 2 or mouseButton < 1 then
- error("The value after 'mouse_click' action must be 'number' from 1 to 2!, got '"..type(mouseButton).."' = "..mouseButton..".", 2)
- end
- if param1 == mouseButton and param2 == checkbox.xPos and param3 == checkbox.yPos then
- checkbox.ChangeState()
- return true, checkbox.State
- else
- return false, checkbox.State
- end
- 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.
- if param2 == checkbox.xPos and param3 == checkbox.yPos then
- return true, param1
- else
- return false, param1
- end
- elseif action == "mouse_drag" then --Drag action. It returns true if mouse is dragged on the check box object or false if not.
- if type(mouseButton) ~= "number" or mouseButton > 2 or mouseButton < 1 then
- error("The value after 'mouse_click' action must be 'number' from 1 to 2!, got '"..type(mouseButton).."' = "..mouseButton..".", 2)
- end
- if param1 == mouseButton and param2 == checkbox.xPos and param3 == checkbox.yPos then
- checkbox.ChangeState()
- return true, checkbox.State
- else
- return false, checkbox.State
- end
- elseif action == "monitor_touch" then --Monitor touch action. It returns true if check box is touched on a monitor or false if not.
- if param1 == "front" and param2 == checkbox.xPos and param3 == checkbox.yPos then
- checkbox.ChangeState()
- return true, checkbox.State
- else
- return false, checkbox.State
- end
- else
- error("Unknown action!", 2)
- end
- term.redirect(t)
- end
- --Creating an object.
- table.insert(List, 1, checkbox.Object)
- if checkbox.Visible == true then
- checkbox.Draw()
- end
- return checkbox
- end,
- Image = function(env, x, y, w, h, timage, backColor, borderStyle, borderColor, uvx, uvy, visible) --Creates a new Image object
- if type(env) ~= "table" then
- error("Please, set a valid environment!", 2)
- end
- if type(x) ~= "number" or type(y) ~= "number" or type(w) ~= "number" or type(h) ~= "number" then
- error("The type of coordinates and resolution must be 'number', got '"..type(x).."', '"..type(y).."', '"..type(w).."' and '"..type(h).."'.", 2)
- end
- if (uvx ~= nil and type(uvx) ~= "number") or (uvy ~= nil and type(uvy) ~= "number") then
- error("The type of parameters 'uvx' and 'uvy' must be 'number', got '"..type(uvx).."' and '"..type(uvy).."'.", 2)
- end
- if uvx == nil then
- uvx = 0
- end
- if uvy == nil then
- uvy = 0
- end
- if type(visible) ~= "boolean" and visible ~= nil then
- error("The type of visibility parameter must be 'boolean', got '"..type(visible).."'.", 2)
- end
- if visible == nil then
- visible = true
- end
- if type(timage) ~= "string" and type(timage) ~= "table" then
- error("The image type must be an image path or an image table as 'string' or 'table', got '"..type(timage).."'.", 2)
- end
- local path
- if type(timage) == "string" then
- path = timage
- timage = paintutils.loadImage(timage)
- else
- path = nil
- end
- if borderStyle ~= "none" and borderStyle ~= "box" and borderStyle ~= "lines" and borderStyle ~= nil then
- error("The parameter 'borderStyle' must be 'string', got '"..type(borderStyle).."'.", 2)
- end
- if borderStyle == nil then
- borderStyle = "none"
- end
- if (backColor ~= nil and type(borderColor) ~= "number") or (borderColor ~= nil and type(borderColor) ~= "number") then
- error("The type of the colors must be 'number', got '"..type(backColor).."' and '"..type(borderColor).."'.", 2)
- end
- if borderColor == nil then
- borderColor = colors.gray
- end
- local image = {}
- image.Path = path
- image.Visible = visible
- image.Object = "image"
- image.Environment = env
- image.xPos = x
- image.yPos = y
- image.Width = w
- image.Height = h
- image.Image = timage
- image.BorderStyle = borderStyle
- image.BorderColor = borderColor
- image.BackgroundColor = backColor
- image.UVX = uvx
- image.UVY = uvy
- --Helper functions
- local function colorText(x, y, color, backColor, value) --The easy way to make colour text
- local xs, ys = term.getCursorPos()
- term.setCursorPos(x, y)
- term.setTextColor(color)
- term.setBackgroundColor(backColor)
- term.write(value)
- term.setCursorPos(xs, ys)
- end
- --Image functions
- function image.Draw(state) --Draws an image object
- local t = term.current()
- term.redirect(image.Environment)
- if image.BackgroundColor ~= nil then
- paintutils.drawFilledBox(image.xPos, image.yPos, image.xPos+image.Width, image.yPos+image.Height, image.BackgroundColor)
- end
- local image3 = image.Image
- local image2 = {}
- local i, k, h, w
- if image.UVY > 0 then
- for i=1, image.UVY do
- table.remove(image3, 1)
- end
- end
- if image.UVX > 0 then
- for i=1, #image3 do
- for k=1, image.UVX do
- table.remove(image3[i], 1)
- end
- end
- end
- if image.UVY < 0 then
- for i=1, (-1)*image.UVY do
- table.insert(image3, 1, {})
- end
- end
- if image.UVX < 0 then
- for i=1, #image3 do
- for k=1, (-1)*image.UVX do
- table.insert(image3[i], 1, 0)
- end
- end
- end
- if #image3 < image.Height+1 then
- h = #image3
- else
- h = image.Height+1
- end
- for i=1, h do
- if #image3[i] < image.Width +1 then
- w = #image3[i]
- else
- w = image.Width+1
- end
- image2[i] = {}
- for k=1, w do
- image2[i][k] = image3[i][k]
- end
- end
- paintutils.drawImage(image2, image.xPos, image.yPos)
- if image.BorderStyle == "none" then
- elseif image.BorderStyle == "box" then
- paintutils.drawBox(image.xPos-1, image.yPos-1, image.xPos+image.Width+1, image.yPos+image.Height+1, image.BorderColor)
- elseif image.BorderStyle == "lines" then
- term.setBackgroundColor(image.BorderColor)
- term.setTextColor(colors.white)
- for i=0, image.Width do
- term.setCursorPos(image.xPos+i, image.yPos-1)
- term.write("-")
- term.setCursorPos(image.xPos+i, image.yPos+image.Height+1)
- term.write("-")
- end
- for i=0, image.Height do
- term.setCursorPos(image.xPos-1, image.yPos+i)
- term.write("|")
- term.setCursorPos(image.xPos+image.Width+1, image.yPos+i)
- term.write("|")
- end
- term.setCursorPos(image.xPos-1, image.yPos-1)
- term.write("+")
- term.setCursorPos(image.xPos+image.Width+1, image.yPos-1)
- term.write("+")
- term.setCursorPos(image.xPos-1, image.yPos+image.Height+1)
- term.write("+")
- term.setCursorPos(image.xPos+image.Width+1, image.yPos+image.Height+1)
- term.write("+")
- else
- end
- term.setBackgroundColor(colors.black)
- term.redirect(t)
- end
- function image.GetVisibility() --This function returns the visibility state
- return image.Visible
- end
- function image.SetVisibility(boolean) --This function sets the visibility state
- if type(boolean) ~= "boolean" then
- error("The type of the visibility parameter must be 'boolean', got '"..type(boolean).."'.", 2)
- end
- image.Visible = boolean
- if image.Visible == true then
- image.Draw()
- end
- end
- function image.SetPosition(x, y, w, h) --Change image's position
- if x == nil or y == nil then
- error("Not enough parameters!", 2)
- end
- if type(x) ~= "number" or type(y) ~= "number" then
- error("Coordinates x and y must be 'number', got '"..type(x).."' and '"..type(y).."'.", 2)
- end
- image.xPos = x
- image.yPos = y
- if w ~= nil and type(w) == "number" then
- image.Width = w
- end
- if h ~= nil and type(h) == "number" then
- image.Height = h
- end
- if image.Visible == true then
- image.Draw()
- end
- end
- function image.GetPosition() --Returns image's position
- return image.xPos, image.yPos, image.Width, image.Height
- end
- function image.GetColors() --Returns the image's colors
- return image.BackgroundColor, image.BorderColor
- end
- function image.SetColors(backColor, borderColor) --Sets the colors of the image object
- if backColor ~= nil and type(backColor) == "number" then
- image.BackgroundColor = backColor
- end
- if borderColor ~= nil and type(borderColor) == "number" then
- image.BorderColor = borderColor
- end
- if image.Visible == true then
- image.Draw()
- end
- end
- function image.SetBorderStyle(borderStyle) --Sets border style
- if type(borderStyle) == "string" then
- image.SetBorderStyle = borderStyle
- else
- error("The type of the parameter 'borderStyle' must be 'string', got '"..type(borderStyle).."'.", 2)
- end
- if image.Visible == true then
- image.Draw()
- end
- end
- function image.GetBorderStyle() --Returns image's border style.
- return image.BorderStyle
- end
- function image.GetImage(timage) --Returns image object's image as table or path (string).
- if timage == "table" or timage == nil then
- return image.Image
- elseif timage == "path" then
- return image.Path
- else
- error("The parameter must be 'table', 'path' or nil, got '"..timage.."'.", 2)
- end
- end
- function image.SetImage(timage) --Sets the image object's image
- if type(timage) == "string" then
- local path = timage
- timage = paintutils.loadImage(timage)
- image.Path = path
- image.Image = timage
- elseif type(timage) == "table" then
- image.Path = nil
- image.Image = timage
- else
- error("The type of the parameter must be 'string' or 'table', got '"..type(timage).."'.", 2)
- end
- if image.Visible == true then
- image.Draw()
- end
- end
- function image.GetOffset() --Returns something.
- return image.UVX, image.UVY
- end
- function image.SetOffset(uvx, uvy) --Sets something.
- if type(uvx) ~= "number" or type(uvy) ~= "number" then
- error("The type of the parameters 'uvx' and 'uvy' must be 'number', got '"..type(uvx).."' and '"..type(uvy).."'.", 2)
- end
- image.UVX = uvx
- image.UVY = uvy
- if image.Visible == true then
- image.Draw()
- end
- end
- function image.AddOffset(uvx, uvy) --Ads to something.
- if type(uvx) ~= "number" or type(uvy) ~= "number" then
- error("The type of the parameters 'uvx' and 'uvy' must be 'number', got '"..type(uvx).."' and '"..type(uvy).."'.", 2)
- end
- image.UVX = uvx + image.UVX
- image.UVY = uvy + image.UVY
- if image.Visible == true then
- image.Draw()
- end
- end
- function image.Detect(action, mouseButton, x, y) --Function for actions with image
- local t = term.current()
- term.redirect(image.Environment)
- local event, param1, param2, param3
- if x == nil then
- event, param1, param2, param3 = os.pullEvent(action)
- else
- param1 = mouseButton
- param2 = x
- param3 = y
- end
- if (x ~= nil and type(x) ~= "number") or (y ~= nil and type(y) ~= "number") then
- error("Coordinates must be 'number', got '"..type(x).."' and '"..type(y).."'.", 2)
- end
- if action == "mouse_click" then --Click action. It returns true if it is clicked or false if it isn't clicked
- if type(mouseButton) ~= "number" or mouseButton > 2 or mouseButton < 1 then
- error("The value after 'mouse_click' action must be number from 1 to 2, got '"..type(mouseButton).."'.", 2)
- end
- if param1 == mouseButton and param2 >= image.xPos and param2 <= image.xPos+image.Width and param3 >= image.yPos and param3 <= image.yPos+image.Height then
- if image.Visible == true then
- image.Draw()
- end
- return true
- else
- return false
- end
- elseif action == "mouse_scroll" then --Scroll action. It returns true and direction when you scroll on the image object and returns false if not.
- if param2 >= image.xPos and param2 <= image.xPos+image.Width and param3 >= image.yPos and param3 <= image.yPos+image.Height then
- if image.Visible == true then
- image.Draw()
- end
- return true, param1
- else
- return false
- end
- elseif action == "mouse_drag" then --Drag action. It returns true if mouse is dragged on the image object or false if not.
- if type(mouseButton) ~= "number" or mouseButton > 2 or mouseButton < 1 then
- error("The value after 'mouse_click' action must be number from 1 to 2, got '"..type(mouseButton).."'.", 2)
- end
- if param1 == mouseButton and param2 >= image.xPos and param2 <= image.xPos+image.Width and param3 >= image.yPos and param3 <= image.yPos+image.Height then
- if image.Visible == true then
- image.Draw()
- end
- return true
- else
- return false
- end
- elseif action == "monitor_touch" then --Monitor touch action. It returns true if image is touched on a monitor or false if not.
- if param1 == "front" and param2 >= image.xPos and param2 <= image.xPos+image.Width and param3 >= image.yPos and param3 <= image.yPos+image.Height then
- if image.Visible == true then
- image.Draw()
- end
- return true
- else
- return false
- end
- else
- error("Unknown action!")
- end
- term.redirect(t)
- end
- --Creating image object
- table.insert(List, 1, image.Object)
- if image.Visible == true then
- image.Draw()
- end
- return image
- end,
- Label = function(env, x, y, text, textColor, backColor, state, visible) --Creates a new label object.
- if type(env) ~= "table" then
- error("Please, set a valid environment!", 2)
- end
- if type(x) ~= "number" or type(y) ~= "number" then
- error("The type of the coordinates and must be 'number', got '"..type(x).."' and '"..type(y).."'.", 2)
- end
- if type(text) ~= "string" and text ~= nil then
- error("The type of the text parameter must be 'string', got '"..type(text).."'.", 2)
- end
- if (type(textColor) ~= "number" and textColor ~= nil) or (type(backColor) ~= "number" and backColor ~= nil) then
- error("The type of the colours parameters must be 'number', got '"..type(textColor).."' and '"..type(backColor).."'.", 2)
- end
- if state ~= nil and type(state) ~= "string" then
- error("The type of the parameter 'state' must be 'string' or nil, got '"..type(state).."'.", 2)
- end
- if type(visible) ~= "boolean" and visible ~= nil then
- error("The type the of visibility parameter must be 'boolean', got '"..type(visible).."'.", 2)
- end
- if visible == nil then
- visible = true
- end
- if text == nil then
- text = ""
- end
- if textColor == nil then
- textColor = colors.white
- end
- if backColor == nil then
- backColor = colors.black
- end
- if state == nil then
- state = "middle"
- end
- label = {}
- label.Object = "label"
- label.Environment = env
- label.xPos = x
- label.yPos = y
- label.Text = text
- label.TextColor = textColor
- label.BackgroundColor = backColor
- label.State = state
- label.Visible = visible
- --Helper functions
- local function colorText(x, y, color, backColor, value) --The easy way to make colour text
- local xs, ys = term.getCursorPos()
- term.setCursorPos(x, y)
- term.setTextColor(color)
- term.setBackgroundColor(backColor)
- term.write(value)
- term.setCursorPos(xs, ys)
- end
- --Label functions
- function label.Draw() --Draws label object.
- local t = term.current()
- term.redirect(label.Environment)
- local text = {}
- local i = 0
- for i=1, #label.Text do
- text[i] = string.sub(label.Text, i, i)
- end
- if label.State == "middle" then
- colorText(label.xPos-#label.Text/2, label.yPos, label.TextColor, label.BackgroundColor, label.Text)
- elseif label.State == "left" then
- colorText(label.xPos-#label.Text, label.yPos, label.TextColor, label.BackgroundColor, label.Text)
- elseif label.State == "right" then
- colorText(label.xPos, label.yPos, label.TextColor, label.BackgroundColor, label.Text)
- else
- error("The parameter 'label.State' must have value 'middle' or 'left' or 'right', got '"..label.State.."'.", 2)
- end
- term.redirect(t)
- end
- function label.GetVisibility() --This function returns the visibility state
- return label.Visible
- end
- function label.SetVisibility(boolean) --This function sets the visibility state
- if type(boolean) ~= "boolean" then
- error("The type of the parameter must be boolean, got '"..type(boolean).."'.", 2)
- end
- label.Visible = boolean
- if label.Visible == true then
- label.Draw()
- end
- end
- function label.SetPosition(x, y) --Change label's position
- if x == nil or y == nil then
- error("Not enough parameters!", 2)
- end
- if type(x) ~= "number" or type(y) ~= "number" then
- error("Coordinates x and y must be 'number', got '"..type(x).."' and '"..type(y).."'.", 2)
- end
- label.xPos = x
- label.yPos = y
- if label.Visible == true then
- label.Draw()
- end
- end
- function label.GetPosition() --Returns label's position
- return label.xPos, label.yPos
- end
- function label.GetColors() --Returns the label's colors
- return label.TextColor, label.BackgroundColor
- end
- function label.SetColors(textColor, backColor) --Sets the colors of the label object
- if textColor ~= nil and type(textColor) == "number" then
- label.TextColor = textColor
- end
- if backColor ~= nil and type(backColor) == "number" then
- label.BackgroundColor = backColor
- end
- if label.Visible == true then
- label.Draw()
- end
- end
- function label.GetText()
- return label.Text
- end
- function label.SetText(text)
- if text == nil then
- label.Text = ""
- elseif type(text) == "string" then
- label.Text = text
- else
- error("The text parameter must be 'string', got '"..type(text).."'.", 2)
- end
- if label.Visible == true then
- label.Draw()
- end
- end
- function label.GetState()
- return label.State
- end
- function label.SetState(state)
- if state ~= "middle" and state ~= "right" and state ~= "left" then
- error("The state must be 'middle', 'right' or 'left', got '"..state.."'.", 2)
- end
- label.State = state
- if label.Visible == true then
- label.Draw()
- end
- end
- --Creating label object
- table.insert(List, 1, label.Object)
- if label.Visible == true then
- label.Draw()
- end
- return label
- end,
- Process = function(env, x, y, w, h, program, sh, msh, workDir, ...) --Creates a new process object.
- if type(env) ~= "table" then
- error("Please, set a valid environment!", 2)
- end
- if type(x) ~= "number" or type(y) ~= "number" or type(w) ~= "number" or type(h) ~= "number" then
- error("The type of coordinates and resolution must be 'number', got '"..type(x).."', '"..type(y).."', '"..type(w).."' and '"..type(h).."'.", 2)
- end
- if type(program) ~= "string" then
- error("The type of the process path must be 'string', got'"..type(program).."'.", 2)
- end
- if type(sh) ~= "table" then
- error("It's strange but you need to pass the shell API.", 2)
- end
- if type(msh) ~= "table" and msh ~= nil then
- error("Please set a valid multishell API.", 2)
- end
- if not fs.exists(program) or fs.isDir(program) then
- error("Couldn't find process by this path.", 2)
- end
- if type(workDir) ~= "string" and workDir ~= nil then
- error("The type of the working directory path must be 'string', got'"..type(workDir).."'.", 2)
- end
- if workDir ~= nil then
- if not fs.isDir(workDir) then
- error("Couldn't find a directory by this path.", 2)
- end
- end
- if workDir == nil then
- workDir = "/Temp/"
- if fs.exists("/Temp/") and not fs.isDir("/Temp/") then
- error("Couldn't make a directory with default path '/Temp/'.", 2)
- end
- if not fs.isDir("/Temp/") then
- fs.makeDir("/Temp/")
- end
- end
- if msh == nil then
- msh = false
- end
- local process = {}
- process.Object = "process"
- process.Environment = env
- process.xPos = x
- process.yPos = y
- process.Width = w
- process.Height = h
- process.Process = program
- process.WorkingDir = workDir
- process.Window = window.create(process.Environment, process.xPos, process.yPos, process.Width, process.Height, false)
- process.ShellAPI = sh
- process.Multishell = msh
- process.Arguments = ...
- process.IsRunning = false
- --Process functions
- 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)
- process.IsRunning = true
- shell = process.ShellAPI
- multishell = process.Multishell
- local sh = shell.dir()
- local name, boolean, err
- if multishell then
- name = multishell.getTitle(multishell.getCurrent())
- multishell.setTitle(multishell.getCurrent(), fs.getName(process.Process))
- end
- local t = term.current()
- if type(state) == "boolean" then
- process.Window.setVisible(state)
- else
- process.Window.setVisible(true)
- end
- term.redirect(process.Window)
- shell.setDir(process.WorkingDir)
- if process.Arguments ~= nil then
- boolean = shell.run(process.Process, process.Arguments)
- else
- boolean, err = pcall(function() dofile(process.Process) end)
- end
- shell.setDir(sh)
- if multishell then
- multishell.setTitle(multishell.getCurrent(), name)
- end
- term.redirect(t)
- process.Window.setVisible(false)
- t.redraw()
- process.IsRunning = false
- return boolean, err
- end
- function process.SetPosition(x, y, w, h) --Change process's position
- if x == nil or y == nil then
- error("Not enough parameters!", 2)
- end
- if type(x) ~= "number" or type(y) ~= "number" then
- error("Coordinates x and y must be 'number', got '"..type(x).."' and '"..type(y).."'.", 2)
- end
- process.xPos = x
- process.yPos = y
- if w ~= nil and type(w) == "number" then
- process.Width = w
- end
- if h ~= nil and type(h) == "number" then
- process.Height = h
- end
- process.Window.reposition(process.xPos, process.yPos, process.Width, process.Height)
- process.Window.redraw()
- end
- function process.GetPosition() --Returns the process position
- return process.xPos, process.yPos, process.Width, process.Height
- end
- function process.SetProcess(program, ...) --Changes the process path and arguments
- if type(program) ~= "string" then
- error("The type of the process path must be 'string', got'"..type(program).."'.")
- end
- process.Process = program
- process.Arguments = ...
- end
- function process.GetProcess() --Returns the process path and arguments
- return process.Process, process.Arguments
- end
- function process.GetRunning() --Returns the running state
- return process.IsRunning
- end
- function process.SetWorkDir(workDir) --Sets the shell working directory for process
- if workDir ~= nil then
- if not fs.isDir(workDir) then
- error("Couldn't find a directory by this path.", 2)
- end
- end
- if workDir == nil then
- workDir = "/Temp/"
- if fs.exists("/Temp/") and not fs.isDir("/Temp/") then
- error("Couldn't make a directory with default path '/Temp/'.", 2)
- end
- if not fs.isDir("/Temp/") then
- fs.makeDir("/Temp/")
- end
- end
- process.WorkingDir = workDir
- end
- function process.GetWorkDir() --Returns the shell working directory for process
- return process.WorkingDir
- end
- --Creating process object
- table.insert(List, 1, process.Object)
- return process
- end
- }
- --Functions for all objects.
- function Type(obj) --Returns the type of object.
- if obj ~= nil then
- if type(obj.Object) == "string" then
- return obj.Object
- end
- end
- end
- function Count(obj) --Returns the amount of created objects.
- if obj == nil then
- return #List
- else
- local i, count = 0, 0
- for i=0, #List do
- if List[i] == obj then
- count = count+1
- end
- end
- return count
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment