Advertisement
Pirnogion

OC/3dPirnter

Sep 6th, 2015
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.60 KB | None | 0 0
  1. local component = require "component"
  2. local event = require "event"
  3. local printer = component.printer3d
  4. local gpu = component.gpu
  5.  
  6. local X_SHIFT = 2
  7. local X_CELL_QUANTUM = 2
  8.  
  9. --UTILS--
  10. local function quantization(value, quantum_value)
  11.   return quantum_value >= 1 and value-(value+1)%quantum_value
  12. end
  13.  
  14. local function quantizationCoords(x, y, qx, qy)
  15.   return qx >= 1 and x-(x+1)%qx or x, qy >= 1 and y-(y+1)%qy or y
  16. end
  17.  
  18. function fillChess(gpu, color1, color2, startX, startY, screenX, screenY)
  19.   local shift = 0
  20.   --Fill using color1
  21.   gpu.setBackground(color1)
  22.   gpu.fill(startX, startY, screenX, screenY, ' ')
  23.  
  24.   --Fill using color2
  25.   gpu.setBackground(color2)
  26.   for y=startY, screenY+startY-1, 1 do
  27.     for x=startX, screenX+startX-2, 4 do
  28.       gpu.fill(x+shift, y, 2, 1, ' ')
  29.     end
  30.     shift = shift ~= X_SHIFT and X_SHIFT or 0
  31.   end
  32. end
  33.  
  34. --TOOLS LOGIC--
  35. local rectangle = function(self, gpu, event)
  36.   local x, y = quantizationCoords(event[3], event[4], X_CELL_QUANTUM, 0)
  37.  
  38.   if ( event[1] == "touch" and event[5] == 0 ) then
  39.     if ( not self.state.drawMode ) then
  40.       self.state.drawMode = true
  41.       self.state.pixelUnderMark = { gpu.get(x, y) }
  42.       self.state.x = x
  43.       self.state.y = y
  44.  
  45.       gpu.fill(x, y, 2, 1, ' ')
  46.     else
  47.       --ДОРАБОТАТЬ!
  48.       local startX, startY = math.min(self.state.x, x), math.min(self.state.y, y)
  49.       local endX, endY = math.max(self.state.x, x), math.max(self.state.y, y)
  50.  
  51.       gpu.fill( startX, startY, endX-startX+2, endY-startY+1, ' ' )
  52.       self.state = {}
  53.     end
  54.   elseif ( event[1] == "key_down" and event[3] == 96 ) then
  55.     if ( self.state.drawMode ) then
  56.       local prevBackground = gpu.getBackground()
  57.       gpu.setBackground(self.state.pixelUnderMark[3])
  58.       gpu.fill(self.state.x, self.state.y, 2, 1, ' ')
  59.       gpu.setBackground(prevBackground)
  60.       self.state = {}
  61.     end
  62.   end
  63. end
  64.  
  65. local TOOLS = {
  66.   ["ERASER"]    = { ["id"] = 0,  ["action"] = eraser,    ["state"] = {} },
  67.   ["PENCIL"]    = { ["id"] = 5,  ["action"] = pencil,    ["state"] = {} },
  68.   ["BRUSH"]     = { ["id"] = 10, ["action"] = brush,     ["state"] = {} },
  69.   ["PIPETTE"]   = { ["id"] = 15, ["action"] = pipette,   ["state"] = {} },
  70.   ["RECTANGLE"] = { ["id"] = 20, ["action"] = rectangle, ["state"] = {} },
  71. }
  72.  
  73. --INITIALIZATION AND MAIN LOOP--
  74. local screenX, screenY = gpu.getResolution()
  75.  
  76. fillChess(gpu, 0xffffff, 0xE5E5E5, 1, 1, screenX, screenY)
  77. gpu.setBackground(0)
  78.  
  79. local currentTool = "RECTANGLE"
  80.  
  81. while true do
  82.   local e = {event.pull()}
  83.  
  84.   TOOLS[currentTool]:action(gpu, e)
  85.  
  86.   if ( e[5] == 1 ) then
  87.     break
  88.   end
  89. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement