Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require "component"
- local event = require "event"
- local printer = component.printer3d
- local gpu = component.gpu
- local X_SHIFT = 2
- local X_CELL_QUANTUM = 2
- --UTILS--
- local function quantization(value, quantum_value)
- return quantum_value >= 1 and value-(value+1)%quantum_value
- end
- local function quantizationCoords(x, y, qx, qy)
- return qx >= 1 and x-(x+1)%qx or x, qy >= 1 and y-(y+1)%qy or y
- end
- function fillChess(gpu, color1, color2, startX, startY, screenX, screenY)
- local shift = 0
- --Fill using color1
- gpu.setBackground(color1)
- gpu.fill(startX, startY, screenX, screenY, ' ')
- --Fill using color2
- gpu.setBackground(color2)
- for y=startY, screenY+startY-1, 1 do
- for x=startX, screenX+startX-2, 4 do
- gpu.fill(x+shift, y, 2, 1, ' ')
- end
- shift = shift ~= X_SHIFT and X_SHIFT or 0
- end
- end
- --TOOLS LOGIC--
- local rectangle = function(self, gpu, event)
- local x, y = quantizationCoords(event[3], event[4], X_CELL_QUANTUM, 0)
- if ( event[1] == "touch" and event[5] == 0 ) then
- if ( not self.state.drawMode ) then
- self.state.drawMode = true
- self.state.pixelUnderMark = { gpu.get(x, y) }
- self.state.x = x
- self.state.y = y
- gpu.fill(x, y, 2, 1, ' ')
- else
- --ДОРАБОТАТЬ!
- local startX, startY = math.min(self.state.x, x), math.min(self.state.y, y)
- local endX, endY = math.max(self.state.x, x), math.max(self.state.y, y)
- gpu.fill( startX, startY, endX-startX+2, endY-startY+1, ' ' )
- self.state = {}
- end
- elseif ( event[1] == "key_down" and event[3] == 96 ) then
- if ( self.state.drawMode ) then
- local prevBackground = gpu.getBackground()
- gpu.setBackground(self.state.pixelUnderMark[3])
- gpu.fill(self.state.x, self.state.y, 2, 1, ' ')
- gpu.setBackground(prevBackground)
- self.state = {}
- end
- end
- end
- local TOOLS = {
- ["ERASER"] = { ["id"] = 0, ["action"] = eraser, ["state"] = {} },
- ["PENCIL"] = { ["id"] = 5, ["action"] = pencil, ["state"] = {} },
- ["BRUSH"] = { ["id"] = 10, ["action"] = brush, ["state"] = {} },
- ["PIPETTE"] = { ["id"] = 15, ["action"] = pipette, ["state"] = {} },
- ["RECTANGLE"] = { ["id"] = 20, ["action"] = rectangle, ["state"] = {} },
- }
- --INITIALIZATION AND MAIN LOOP--
- local screenX, screenY = gpu.getResolution()
- fillChess(gpu, 0xffffff, 0xE5E5E5, 1, 1, screenX, screenY)
- gpu.setBackground(0)
- local currentTool = "RECTANGLE"
- while true do
- local e = {event.pull()}
- TOOLS[currentTool]:action(gpu, e)
- if ( e[5] == 1 ) then
- break
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement