Advertisement
MeXaN1cK

object(v.4)

Nov 11th, 2017
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.74 KB | None | 0 0
  1. local cmp = require("component")
  2. local gpu = cmp.gpu
  3. local unicode = require('unicode')
  4. local serialization = require("serialization")
  5.  
  6. local function loadTbl(fl) --Загрузить файл ввиде таблицы.
  7.  file = io.open(fl, 'r')
  8. if file==nil then
  9.  file = io.open(fl, 'w')
  10.  file:write('{}')
  11.  file:close()
  12.  return {}
  13. else
  14.  file = io.open(fl, 'r')
  15.  return serialization.unserialize(file:read('*a'))
  16. end
  17. end
  18.  
  19. local function loadClass(file) return loadTbl(file) end
  20.  
  21. local function getOld() return {gpu.getBackground(),gpu.getForeground()} end
  22. local function setOld(arr) gpu.setBackground(arr[1]) gpu.setForeground(arr[2]) end
  23. local function drawText(obj,mode)
  24.   local function fancyDraw(x,y,obj)
  25.   local old = getOld()
  26.   for i=1,unicode.len(obj.text) do
  27.     local j=i-1
  28.     local pix = {gpu.get(x+j,y)}
  29.     local symb = string.sub(obj.text,i,i)
  30.     gpu.setBackground(pix[3])
  31.     gpu.setForeground(obj.foreground)
  32.     gpu.set(x+j,y,symb)
  33.   end
  34.   setOld(old)
  35.   end
  36.   if obj.text_align == "top_left" then
  37.   if mode ~= nil then fancyDraw(obj.x , obj.y, obj) else gpu.set(obj.x , obj.y,obj.text) end
  38.   elseif obj.text_align == "top_center" then
  39.   if mode ~= nil then fancyDraw(obj.x + math.floor((obj.w - unicode.len(obj.text)) / 2 ) , obj.y, obj) else gpu.set(obj.x + math.floor((obj.w - unicode.len(obj.text)) / 2 ) , obj.y,obj.text) end
  40.   elseif obj.text_align == "top_right" then
  41.   if mode ~= nil then fancyDraw(obj.x + obj.w - unicode.len(obj.text),obj) else gpu.set(obj.x + obj.w - unicode.len(obj.text), obj.y,obj.text) end
  42.   elseif obj.text_align == "center_left" then
  43.   if mode ~= nil then fancyDraw(obj.x , obj.y + math.floor(obj.h / 2 ) ,obj) else gpu.set(obj.x , obj.y + math.floor(obj.h / 2 ) ,obj.text) end
  44.   elseif obj.text_align == "center" then
  45.   if mode ~= nil then fancyDraw(obj.x + math.floor((obj.w - unicode.len(obj.text)) / 2 ) , obj.y + math.floor(obj.h / 2 )  ,obj) else gpu.set(obj.x + math.floor((obj.w - unicode.len(obj.text)) / 2 ) , obj.y + math.floor(obj.h / 2 )  ,obj.text) end
  46.   elseif obj.text_align == "center_right" then
  47.   if mode ~= nil then fancyDraw(obj.x + obj.w - unicode.len(obj.text) , obj.y + math.floor(obj.h / 2 ),obj) else gpu.set(obj.x + obj.w - unicode.len(obj.text) , obj.y + math.floor(obj.h / 2 )  ,obj.text) end
  48.   elseif obj.text_align == "bottom_left" then
  49.   if mode ~= nil then fancyDraw(obj.x , obj.y + obj.h -1,obj) else gpu.set(obj.x , obj.y + obj.h -1,obj.text) end
  50.   elseif obj.text_align == "bottom_center" then
  51.   if mode ~= nil then fancyDraw(obj.x + math.floor((obj.w - unicode.len(obj.text)) / 2 ) , obj.y + obj.h -1,obj) else gpu.set(obj.x + math.floor((obj.w - unicode.len(obj.text)) / 2 ) , obj.y + obj.h -1,obj.text) end
  52.   elseif obj.text_align == "bottom_right" then
  53.   if mode ~= nil then fancyDraw(obj.x + obj.w - unicode.len(obj.text) , obj.y + obj.h -1,obj) else gpu.set(obj.x + obj.w - unicode.len(obj.text) , obj.y + obj.h -1,obj.text) end
  54.   end  
  55. end
  56.  
  57. local obj = {}
  58.  
  59. local function draw(obj)
  60.   local old = getOld()
  61.   --
  62.     if obj.w == nil then obj.w = 1 end
  63.     if obj.h == nil or obj.h < 1 then obj.h = 1 end
  64.     if obj.x == nil then io.stderr("error: x is nil") os.exit() end
  65.     if obj.y == nil then io.stderr("error: y is nil") os.exit() end
  66.   --
  67.   if obj.type == "button" then
  68.     if obj.text == nil or unicode.len(obj.text) == 0 then obj.text = "button" end
  69.     if obj.w < unicode.len(obj.text) then obj.w = unicode.len(obj.text) end
  70.     alList = {top_left=true,top_center=true,top_right=true,center_left=true,center=true,center_right=true,bottom_left=true,bottom_center=true,bottom_right=true}
  71.     if alList[obj.text_align] == nil then io.stderr("error: align is not (center)") os.exit() end
  72.     gpu.setBackground(obj.background)
  73.     gpu.setForeground(obj.foreground)
  74.  
  75.     gpu.fill(obj.x,obj.y,obj.w,obj.h,' ')
  76.  
  77.     drawText(obj)
  78.   elseif obj.type == "scroll" then
  79.  
  80.     if obj.value > obj.maxValue then obj.value = obj.maxValue end
  81.     if obj.value < 1 then obj.value = 1 end
  82.  
  83.     if obj.orientation == "vertical" then
  84.       gpu.setBackground(obj.back1)
  85.       gpu.fill(obj.x,obj.y,obj.w,obj.h,' ')
  86.       local sizeOfScrollBar = math.ceil(1 / obj.maxValue * obj.h)
  87.       local displayBarFrom = math.floor(obj.y + obj.h * ((obj.value - 1) / obj.maxValue))
  88.       gpu.setBackground(obj.back2)
  89.       gpu.fill(obj.x, displayBarFrom, obj.w, sizeOfScrollBar, ' ')
  90.       sizeOfScrollBar, displayBarFrom = nil, nil
  91.     elseif obj.orientation == "horizontal" then
  92.       gpu.setBackground(obj.back1)
  93.       gpu.fill(obj.x,obj.y,obj.w,obj.h,' ')
  94.       local sizeOfScrollBar = math.ceil(1 / obj.maxValue * obj.w)
  95.       local displayBarFrom = math.floor(obj.h + obj.w * ((obj.value - 1) / obj.maxValue))
  96.       gpu.setBackground(obj.back2)
  97.       gpu.fill(obj.x-1+displayBarFrom, obj.y, sizeOfScrollBar, obj.h, ' ')
  98.       sizeOfScrollBar, displayBarFrom = nil, nil
  99.     end
  100.   elseif obj.type == "progressBar" then
  101.     gpu.setBackground(obj.background)
  102.     gpu.setForeground(obj.foreground)
  103.     gpu.fill(obj.x,obj.y,obj.w,obj.h,' ')
  104.  
  105.     local activeWidth = math.floor(obj.w / 100 * obj.value)
  106.    
  107.     gpu.setBackground(obj.activeBackground)
  108.     gpu.setForeground(obj.activeForeground)
  109.     gpu.fill(obj.x,obj.y,activeWidth,obj.h,' ')
  110.     obj.text = obj.value .. "%"
  111.     drawText(obj,1)
  112.   elseif obj.type == "frame" then
  113.     if obj.w < unicode.len(obj.text) then obj.w = unicode.len(obj.text) end
  114.     alList = {top_left=true,top_center=true,top_right=true,center_left=true,center=true,center_right=true,bottom_left=true,bottom_center=true,bottom_right=true}
  115.     if alList[obj.text_align] == nil then io.stderr("error: align is not (center)") os.exit() end
  116.     gpu.setBackground(obj.background)
  117.     gpu.fill(obj.x,obj.y,obj.w,obj.h," ")
  118.     gpu.setForeground(obj.foreground)
  119.     drawText(obj)
  120.     for i=1,#obj.value do
  121.     obj.value[i].x = obj.x + obj.value[i].x
  122.     obj.value[i].y = obj.y + obj.value[i].y
  123.     obj.value[i].draw()
  124.     end
  125.   end
  126.   setOld(old)
  127. end
  128.  
  129. function obj.draw(obj)
  130.   draw(obj)
  131. end
  132.  
  133. function obj.button()
  134.   local obj = {
  135.     x,
  136.     y,
  137.     w,
  138.     h,
  139.     background=0,
  140.     foreground=0xffffff,
  141.     text_align="center",
  142.     text="button",
  143.     catch = false,
  144.     type="button",
  145.     id
  146.   }
  147.   function obj.setID(id) obj.id = id end
  148.   function obj.getID() return obj.id end
  149.   function obj.setX(x) obj.x = x  end
  150.   function obj.setY(y) obj.y = y  end
  151.   function obj.getX() return obj.x end
  152.   function obj.getY() return obj.y end
  153.   function obj.setBackground(color) obj.background = color end
  154.   function obj.getBackground() return obj.background  end
  155.   function obj.setForeground(color) obj.foreground = color end
  156.   function obj.getForeground() return obj.foreground  end
  157.   function obj.setW(w) obj.w = w end
  158.   function obj.getW() return obj.w  end
  159.   function obj.setH(h) obj.h = h end
  160.   function obj.getH() return obj.h  end
  161.   function obj.setText(text) obj.text = text end
  162.   function obj.getText() return obj.text  end
  163.   function obj.setAlign(align) obj.text_align = align end  
  164.   function obj.getAlign() return obj.text_align  end
  165.  
  166.   function obj.draw()
  167.     draw(obj)
  168.   end
  169.   return obj
  170. end
  171.  
  172. function obj.progressBar()
  173.   local old = getOld()
  174.   local obj = {
  175.     x=1,
  176.     y=1,
  177.     w,
  178.     h =1,
  179.     foreground=0xffffff,
  180.     background=0xaa0000,
  181.     activeForeground=0xffffff,
  182.     activeBackground=0x00aa00,
  183.     value,
  184.     id,
  185.     type="progressBar",
  186.     text_align="center"
  187.   }
  188.   function obj.setX(x)  obj.x = x end
  189.   function obj.setY(y)  obj.y = y end
  190.   function obj.getX()   return obj.x  end
  191.   function obj.getY()   return obj.y  end
  192.   function obj.setW(w) obj.w = w end
  193.   function obj.getW() return obj.w  end
  194.   function obj.setH(h) obj.h = h end
  195.   function obj.getH() return obj.h  end
  196.   function obj.setValue(val2) obj.value = val2 end
  197.   function obj.getValue() return obj.value end
  198.   function obj.setID(id) obj.id = id end
  199.   function obj.getID() return obj.id end
  200.  
  201.   function obj.setActiveBackground(color) obj.activeBackground = color end
  202.   function obj.getActiveBackground() return obj.activeBackground  end
  203.   function obj.setActiveForeground(color) obj.activeForeground = color end
  204.   function obj.getActiveForeground() return obj.activeForeground  end
  205.  
  206.   function obj.setForeground(fore) obj.foreground = fore end
  207.   function obj.getForeground() return obj.foreground end
  208.   function obj.setBackground(back) obj.background = back end
  209.   function obj.getBackground() return obj.background end
  210.  
  211.  
  212.   function obj.draw()
  213.     draw(obj)
  214.   end
  215.   return obj
  216. end
  217.  
  218. function obj.scroll()
  219.   local obj = {
  220.     x,
  221.     y,
  222.     w=1,
  223.     h=1,
  224.     back1=0,
  225.     back2=0,
  226.     orientation="vertical",
  227.     maxValue=0,
  228.     value=1,
  229.     type="scroll",
  230.     id
  231.   }
  232.   function obj.setX(x)  obj.x = x end
  233.   function obj.setY(y)  obj.y = y end
  234.   function obj.getX()   return obj.x  end
  235.   function obj.getY()   return obj.y  end
  236.   function obj.setW(w) obj.w = w end
  237.   function obj.getW() return obj.w  end
  238.   function obj.setH(h) obj.h = h end
  239.   function obj.getH() return obj.h  end
  240.   function obj.setBack1(back1) obj.back1 = back1 end
  241.   function obj.getBack1() return obj.back1 end
  242.   function obj.setBack2(back2) obj.back2 = back2 end
  243.   function obj.getBack2() return obj.back2 end
  244.   function obj.setOrientation(orint) obj.orientation = orint end
  245.   function obj.getOrientation() return obj.orientation end
  246.   function obj.setMaxValue(val) obj.maxValue = val end
  247.   function obj.getMaxValue() return obj.maxValue end
  248.   function obj.setValue(val2) obj.value = val2 end
  249.   function obj.getValue() return obj.value end
  250.   function obj.setID(id) obj.id = id end
  251.   function obj.getID() return obj.id end
  252.   function obj.draw()
  253.     draw(obj)
  254.   end
  255.   return obj
  256. end
  257.  
  258. function obj.frame()
  259.   local obj={
  260.     x,
  261.     y,
  262.     w,
  263.     h,
  264.     background=0,
  265.     foreground=0xffffff,
  266.     text_align="top_center",
  267.     text,
  268.     id,
  269.     type="frame",
  270.     value={}
  271.   }
  272.   function obj.setX(x)  obj.x = x end
  273.   function obj.setY(y)  obj.y = y end
  274.   function obj.getX()   return obj.x  end
  275.   function obj.getY()   return obj.y  end
  276.   function obj.setW(w) obj.w = w end
  277.   function obj.getW() return obj.w  end
  278.   function obj.setH(h) obj.h = h end
  279.   function obj.getH() return obj.h  end
  280.   function obj.setBackground(back) obj.background = back end
  281.   function obj.getBackground() return obj.background end
  282.   function obj.setForeground(fore) obj.foreground = fore end
  283.   function obj.getForeground() return obj.foreground end
  284.   function obj.setID(id) obj.id = id end
  285.   function obj.getID() return obj.id end
  286.   function obj.setText(name) obj.text = name end
  287.   function obj.getText() return obj.text end
  288.   function obj.setValue(val2) table.insert(obj.value , val2) end
  289.   function obj.getValue() return obj.value end
  290.  
  291.   function obj.loadFile(file)
  292.     obj.value = loadTbl(file)
  293.   end
  294.  
  295.   function obj.draw()
  296.     draw(obj)
  297.   end
  298.   return obj
  299. end
  300.  
  301. function obj.catchEvent()
  302.   -- {x,y,w,h,callback,id}
  303.   local evnt = {
  304.     touch={},
  305.     scroll={},
  306.     drag={},
  307.     drop={}
  308.   }
  309. function evnt.addEvent(object,event,callback,data)
  310.   if evnt[event] ~= nil then
  311.     table.insert(evnt[event],{x = object.x, y = object.y, w = object.w, h = object.h, id = object.id,callback = callback,data = data})
  312.   end
  313. end
  314.  
  315.   function evnt.getEvent(arrEvent)
  316.     if evnt[arrEvent[1]] ~= nil then
  317.       local arr = evnt[arrEvent[1]]
  318.       local ID = nil
  319.       for i=1,#evnt[arrEvent[1]] do
  320.         if arrEvent[3] > arr[i].x-1 and arrEvent[3] < arr[i].x+arr[i].w and arrEvent[4] > arr[i].y-1 and arrEvent[4] < arr[i].y+arr[i].h then
  321.           ID = {arrEvent,id=arr[i].id}
  322.           if type(arr[i].callback) == "function" then
  323.             arr[i].callback(arr[i].data)
  324.           end
  325.           break
  326.         end
  327.       end
  328.       return ID
  329.     end
  330.   end
  331.  
  332.   return evnt
  333. end
  334.  
  335. return obj
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement