Advertisement
Qivex

QMain-API

Mar 10th, 2018
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.59 KB | None | 0 0
  1. -------
  2. --Init:
  3. -------
  4.  
  5. --Local Functions
  6. local catchInvalidPeripheral
  7.  
  8. --Local Vars
  9. local screen = "term"
  10. local peripheral_sides = {"left", "right", "top", "bottom", "front", "back"}
  11.  
  12.  
  13. ----------
  14. --Methods:
  15. ----------
  16.  
  17. setDefaultScreen = function(...)
  18.     --Input
  19.     local input = {...}
  20.     local default = {"term"}
  21.     --Parameters
  22.     local side = input[1] or default[1]
  23.     --Method
  24.     if (side == "term") then
  25.         screen = side
  26.     else
  27.         catchInvalidPeripheral(side, "monitor")
  28.         screen = side
  29.     end
  30.     return getMonitor(side)
  31. end
  32.  
  33.  
  34. getScreen = function(...)
  35.     return screen
  36. end
  37.  
  38.  
  39. clear = function(...)
  40.     --Input
  41.     local input = {...}
  42.     local default = {getScreen()}
  43.     --Parameters
  44.     local side = input[1] or default[1]
  45.     --Peripheral
  46.     local monitor = getMonitor(side)
  47.     --Method
  48.     monitor.setTextColor(colors.white)
  49.     monitor.setBackgroundColor(colors.black)
  50.     monitor.clear()
  51.     monitor.setCursorPos(1, 1)
  52.     if not(qbutton == nil) then
  53.         qbutton.disableAllButtons()
  54.     end
  55. end
  56.  
  57.  
  58. convertColor = function(...)
  59.     --Input
  60.     local input = {...}
  61.     local default = {"a"}
  62.     --Parameters
  63.     local color = input[1] or default[1]
  64.     --Method
  65.     local color_mapping = {
  66.         ["a"] = 1,      --white
  67.         ["b"] = 256,    --lightGray
  68.         ["c"] = 128,    --gray
  69.         ["d"] = 32768,  --black
  70.         ["e"] = 4096,   --brown
  71.         ["f"] = 16384,  --red
  72.         ["g"] = 2,      --orange
  73.         ["h"] = 16,     --yellow
  74.         ["i"] = 32,     --lime
  75.         ["j"] = 8192,   --green
  76.         ["k"] = 2048,   --blue
  77.         ["l"] = 512,    --cyan
  78.         ["m"] = 8,      --lightBlue
  79.         ["n"] = 1024,   --purple
  80.         ["o"] = 4,      --magenta
  81.         ["p"] = 64,     --pink
  82.     }
  83.     if (color_mapping[color] == nil) then
  84.         error("Invalid Color Code:"..color.." Use letters a-p.", 0)
  85.     else
  86.         return color_mapping[color]
  87.     end
  88. end
  89.  
  90.  
  91. isIn = function(...)
  92.     --Input
  93.     local input = {...}
  94.     local default = {1, {1, 2}}
  95.     --Parameters
  96.     local var = input[1] or default[1]
  97.     local array = input[2] or default[2]
  98.     --Method
  99.     for i, v in pairs(array) do
  100.         if (v == var) then
  101.             return true
  102.         end
  103.     end
  104.     return false
  105. end
  106.  
  107.  
  108. getMonitor = function(...)
  109.     --Input
  110.     local input = {...}
  111.     local default = {"term"}
  112.     --Parameters
  113.     local side = input[1] or default[1]
  114.     --Method
  115.     if (side == "term") then
  116.         return term
  117.     else
  118.         catchInvalidPeripheral(side, "monitor")
  119.         return peripheral.wrap(side)
  120.     end
  121. end
  122.  
  123.  
  124. ----------
  125. --Private:
  126. ----------
  127.  
  128. catchInvalidPeripheral = function(side, target_peripheral)
  129.     if not( isIn(side, peripheral_sides) ) then
  130.         error(side.." is not a valid side.", 0)
  131.     else
  132.         if not( peripheral.getType(side) == target_peripheral ) then
  133.             error("No "..target_peripheral.." on "..side.." side.", 0) 
  134.         end
  135.     end
  136. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement