Advertisement
derkoch

screen v1.0

Oct 6th, 2013
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.72 KB | None | 0 0
  1. local version = 1.0
  2. --- asdasdasd
  3. -- @name Screenyyyyy
  4. -- @copyright 2013 DerKoch AKA RoyalBingBong
  5. -- @release Version 1.0
  6.  
  7. Screen = {}
  8. Screen.__index = Screen
  9. --- Creates a new screen
  10. -- @param side the side of the adjacent Advanced Monitor
  11. -- @param row the amount of rows
  12. -- @param column the amount of columns
  13. -- @return instance of Screen
  14. function Screen.create (side, row, column) 
  15.     local scn = {}
  16.     setmetatable(scn, Screen)
  17.     scn.mon = peripheral.wrap(side)
  18.     scn.mon.setTextScale(1)
  19.     --scn.screenx, scn.screeny = scn.mon.getSize()
  20.     scn.rows = tonumber(row)
  21.     scn.columns = tonumber(column)
  22.     -- setup elements array
  23.     scn.elements = {}
  24.     for i = 1, row do
  25.         scn.elements[i] = {}
  26.         for j = 1, column do
  27.             scn.elements[i][j] = nil
  28.         end
  29.     end
  30.     scn.margin = {
  31.         ["top"]  = 1;
  32.         ["right"]  = 1;
  33.         ["bottom"]  = 1;
  34.         ["left"]  = 1;
  35.     }
  36.     scn.mon.clear()
  37.     scn.mon.setCursorPos(1,1)
  38.     return scn
  39. end
  40.  
  41. --- Initializes all screen elements. Calculates dimensions for each element in the grid
  42. function Screen:initGrid() 
  43.     x,y = self.mon.getSize()   
  44.     maxCellHeight = (y - (self.rows + 1)) / self.rows
  45.     maxCellWidth = (x - (self.columns + 1)) / self.columns
  46.     --cellHeight = 3
  47.    
  48.     cellHeight = math.floor(maxCellHeight)
  49.     -- cellHeight = math.floor(maxCellHeight) -- (self.margin["top"] + self.margin["bottom"])
  50.     -- if math.fmod(cellHeight, 2) == 0 then
  51.         -- cellHeight = cellHeight + 1 
  52.         -- --print(cellHeight)
  53.     -- end
  54.     cellWidth = math.floor(maxCellWidth) -- (self.margin["left"] + self.margin["right"])
  55.     --print(x .. ", " .. y .. ", " .. cellWidth)
  56.     --i*(cellWidth + self.margin["left"]
  57.     --~ i = row = y value !!!!!!!!!
  58.     --~ j = column = x value !!!!!!!!!
  59.     for i=1, self.rows do  
  60.         for j=1, self.columns do       
  61.             if self.elements[i][j] ~= nil then
  62.                 local minx, maxx, miny, maxy = self.elements[i][j]:getMinMaxXY()               
  63.                 if minx ~= 0 and maxx ~= 0 and miny ~= 0 and maxy ~= 0 then
  64.                     --self.elements[i][j]:debugPrint()
  65.                     --print(minx .. ", " .. maxx .. ", " .. miny .. ", " .. maxy)
  66.                     maxx = j * (cellWidth + self.margin["left"]) -- or j * self.margin["left"] --needs testing
  67.                     maxy = i * (cellHeight + self.margin["bottom"])
  68.                     --print(minx .. ", " .. maxx .. ", " .. miny .. ", " .. maxy)
  69.                     self.elements[i][j]:setMinMaxXY(minx,maxx,miny,maxy)                   
  70.                     self.elements[i][j] = nil
  71.                 else                   
  72.                     --self.elements[i][j]:debugPrint()
  73.                     minx = self.margin["left"] + (j - 1) * cellWidth + j * self.margin["left"]
  74.                     maxx = minx + cellWidth - self.margin["left"] -- or j * self.margin["left"] --needs testing
  75.                     miny = self.margin["top"] + (i - 1) * cellHeight + i * self.margin["top"]
  76.                     maxy = miny + cellHeight - self.margin["bottom"]
  77.                     self.elements[i][j]:setMinMaxXY(minx,maxx,miny,maxy)
  78.                     self.elements[i][j]:setTarget(self.mon)
  79.                 end
  80.             end
  81.         end    
  82.     end    
  83. end
  84.  
  85. --- Returns the indices, to find the elements in self.elements
  86. -- @param name the name of the element
  87. -- @return row, column
  88. function Screen:getElementByName(name)
  89.     for i = 1, self.rows do
  90.         for j = 1, self.columns do
  91.             local elemname = self.elements[i][j].name
  92.             if elemname == name then
  93.                 return i, j
  94.             end
  95.         end
  96.     end
  97. end
  98.  
  99. --- Sets the text scale for the screen and redraws
  100. -- @param factor the scale factor
  101. function Screen:setTextScale(factor)
  102.     self.mon.clear()
  103.     self.mon.setTextScale(factor)
  104.     self:draw()
  105. end
  106.  
  107. --- Adds an element to the grid
  108. -- @param element   the element to be added
  109. -- @param rows  the row (>= 1) the element should be in, can be a table
  110. -- @param columns   the column (>= 1) the element should be in, can be a table
  111. -- @usage Adds btn1 to row: 1, column 1<br>Screen:addElementToGrid (btn1, 1, 1)
  112. -- @usage Adds btn1 to row: 1 and 2, column 1 and 2<br>Screen:addElementToGrid (btn1, {1,2}, {1,2})
  113. function Screen:addElementToGrid (element, rows, columns)
  114.     if type(rows) == "table" or type(columns) == "table" then      
  115.         if type(rows) == "table" and type(columns) == "table" then
  116.             for i = 1, #rows do
  117.                 for j = 1, #columns do
  118.                     self.elements[rows[i]][columns[j]] = element
  119.                 end
  120.             end        
  121.         elseif type(rows) ~= "table" and type(columns) == "table" then
  122.             for j = 1, #columns do
  123.                 self.elements[rows][columns[j]] = element
  124.             end
  125.         elseif type(rows) == "table" and type(columns) ~= "table" then
  126.             for i = 1, #rows do
  127.                 self.elements[rows[i]][columns] = element
  128.             end
  129.         end
  130.     else
  131.         if rows > self.rows or columns > self.columns then
  132.             error("addElementToGrid: row or column out of bounds")
  133.         else           
  134.             self.elements[rows][columns] = element
  135.         end
  136.     end
  137. end
  138.  
  139. --- Draws the screen
  140. -- Only needed after first initiation
  141. function Screen:draw()
  142.     for i=1, self.rows do
  143.         for j=1, self.columns do
  144.             if self.elements[i][j] ~= nil then
  145.                 --self.elements[i][j]:debugPrint()
  146.                 self.elements[i][j]:draw(self.mon)
  147.                 --self:drawElement(self.elements[i][j])
  148.             end
  149.         end
  150.     end
  151. end
  152.  
  153. --- Checks if an element was clicked
  154. -- @param event the touch event
  155. -- @param side the side of the touch event
  156. -- @param x the x position of the touch event
  157. -- @param y the y position of the touch event
  158. function Screen:checkTouchEvent(event,side,x,y)
  159.     -- maybe optimize search for element with approximation?
  160.     for i = 1, self.rows do
  161.         for j = 1, self.columns do
  162.             if self.elements[i][j] ~= nil then
  163.                 if self.elements[i][j]:isClicked(x,y) then
  164.                     if self.elements[i][j].type == "button" then
  165.                         if self.elements[i][j]:execFunction() then
  166.                             if self.elements[i][j].radioIndex ~= nil then
  167.                                 self.elements[i][j]:toggleOthers(self.elements)                                
  168.                             else
  169.                                 self.elements[i][j]:toggle()
  170.                             end                    
  171.                         end
  172.                     elseif self.elements[i][j].type == "picker" then
  173.                         self.elements[i][j]:execFunction()
  174.                     end
  175.                 end
  176.             end
  177.         end
  178.     end
  179. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement