218 lines
6.6 KiB
Lua
218 lines
6.6 KiB
Lua
drawing = {}
|
|
function alignToGrid(num, thing)
|
|
return num - (num % thing)
|
|
end
|
|
|
|
local utils = require("shared.utils")
|
|
|
|
function drawing.clearAllCanvases()
|
|
love.graphics.setCanvas(levelloop.Canvas.fg)
|
|
love.graphics.clear()
|
|
love.graphics.setCanvas(levelloop.Canvas.bg)
|
|
love.graphics.clear()
|
|
love.graphics.setCanvas(levelloop.Canvas.dbg)
|
|
love.graphics.clear()
|
|
love.graphics.setCanvas()
|
|
levelloop.Canvas.physics = {}
|
|
end
|
|
|
|
|
|
function drawing.clearAllCanvasesNetwork()
|
|
levelloop.remoteCall("clearCanvas")
|
|
drawing.clearAllCanvases()
|
|
end
|
|
|
|
|
|
function drawing.fillCanvas(layer, hash, color)
|
|
love.graphics.setCanvas(levelloop.Canvas[layer])
|
|
love.graphics.clear()
|
|
utils.color_push()
|
|
love.graphics.setColor(color)
|
|
for x = 0, levelloop.world.x -levelloop.textures[hash].image:getPixelWidth(),
|
|
levelloop.textures[hash].image:getPixelWidth() do
|
|
for y = 0, levelloop.world.y -levelloop.textures[hash].image:getPixelHeight(),
|
|
levelloop.textures[hash].image:getPixelHeight() do
|
|
love.graphics.draw(levelloop.textures[hash].image, x, y)
|
|
end
|
|
end
|
|
utils.color_pop()
|
|
love.graphics.setCanvas()
|
|
end
|
|
|
|
|
|
function drawing.fillCanvasNetwork(layer, hash, color)
|
|
color.nwType = "color"
|
|
levelloop.remoteCall("fillCanvas", layer, hash, color)
|
|
drawing.fillCanvas(layer, hash, color)
|
|
end
|
|
|
|
|
|
function drawing.keyreleased(key, _)
|
|
assert(drawing.color)
|
|
if key == "c" then
|
|
drawing.clearAllCanvasesNetwork()
|
|
end
|
|
if key == "f" then
|
|
drawing.fillCanvasNetwork("fg", drawing.cursorHash, drawing.color)
|
|
end
|
|
if key == "b" then
|
|
drawing.fillCanvasNetwork("bg", drawing.cursorHash, drawing.color)
|
|
end
|
|
end
|
|
|
|
local drawmouse = {}
|
|
function drawing.input(dt)
|
|
local textures = levelloop.textures
|
|
local cursor = drawing.cursor
|
|
if not cursor or not cursor.data then return end
|
|
assert(cursor, "Failed to find cursor")
|
|
assert(cursor.data, "No imagedata for cursor!")
|
|
assert(cursor.image, "No image in cursor!")
|
|
assert(drawing.cursorHash and drawing.cursor)
|
|
local mousex = love.mouse.getX()
|
|
local mousey = love.mouse.getY()
|
|
if mousex > window.x -ui.space then return end
|
|
if not love.mouse.isDown(2) and not love.mouse.isDown(1) then
|
|
drawmouse.lastX = nil
|
|
drawmouse.lastY = nil
|
|
return
|
|
end
|
|
mousex = mousex / scale - view.x
|
|
mousey = mousey / scale - view.y
|
|
if mousex > levelloop.world.x or
|
|
mousey > levelloop.world.y or
|
|
mousex < 0 or
|
|
mousey < 0 then
|
|
return
|
|
end
|
|
|
|
local granularity = 1
|
|
if drawmouse.lastX and drawmouse.lastY then
|
|
local x = math.abs(mouse.lastpos.x - mousex)
|
|
local y = math.abs(mouse.lastpos.y - mousey)
|
|
granularity = math.sqrt(x * x + y * y) / ((cursor.image:getPixelWidth() + cursor.image:getPixelWidth()) / 2)
|
|
else
|
|
drawmouse.lastX = mousex
|
|
drawmouse.lastY = mousey
|
|
end
|
|
if not granularity then granularity = 1 end
|
|
for i=0,granularity,1 do
|
|
local thisx = alignToGrid((((drawmouse.lastX - mousex) / granularity) * i + mousex),
|
|
cursor.image:getPixelWidth())
|
|
local thisy = alignToGrid((((drawmouse.lastY - mousey) / granularity) * i + mousey),
|
|
cursor.image:getPixelHeight())
|
|
if not thisx or not thisy then return end
|
|
if not (thisx == thisx) or not (thisy == thisy) then return end
|
|
|
|
local ctrlDown = love.keyboard.isDown("lctrl") or love.keyboard.isDown("rctrl")
|
|
if ctrlDown then
|
|
love.graphics.setCanvas(levelloop.Canvas.bg)
|
|
else
|
|
love.graphics.setCanvas(levelloop.Canvas.fg)
|
|
end
|
|
|
|
if love.mouse.isDown(1) then -- only delete
|
|
utils.color_push()
|
|
love.graphics.setColor(drawing.color)
|
|
love.graphics.draw(cursor.image, thisx, thisy)
|
|
utils.color_pop()
|
|
if ctrlDown then
|
|
drawing.draw_texture(drawing.cursorHash, thisx, thisy, "bg", drawing.color)
|
|
else
|
|
drawing.draw_texture(drawing.cursorHash, thisx, thisy, "fg", drawing.color)
|
|
drawing.draw_solid(thisx, thisy,
|
|
cursor.image:getPixelWidth(),
|
|
cursor.image:getPixelHeight())
|
|
end
|
|
else
|
|
love.graphics.setBlendMode("replace")
|
|
utils.color_push()
|
|
love.graphics.setColor(0,0,0,0)
|
|
love.graphics.rectangle("fill", thisx, thisy,
|
|
cursor.image:getPixelWidth(),
|
|
cursor.image:getPixelHeight())
|
|
if ctrlDown then
|
|
drawing.del_texture(thisx, thisy,
|
|
cursor.image:getPixelWidth(),
|
|
cursor.image:getPixelHeight(), "bg")
|
|
else
|
|
drawing.del_texture(thisx, thisy,
|
|
cursor.image:getPixelWidth(),
|
|
cursor.image:getPixelHeight(), "fg")
|
|
drawing.remove_solid(thisx, thisy,
|
|
cursor.image:getPixelWidth(),
|
|
cursor.image:getPixelHeight())
|
|
end
|
|
utils.color_pop()
|
|
love.graphics.setBlendMode("alpha")
|
|
end
|
|
love.graphics.setCanvas()
|
|
end
|
|
drawmouse.lastX = mousex
|
|
drawmouse.lastY = mousey
|
|
|
|
love.graphics.setCanvas()
|
|
end
|
|
|
|
|
|
drawing.lastTexture = { hash = "", x = 0, y = 0, layer = ""}
|
|
function drawing.draw_texture(hash, x, y, layer, color)
|
|
if not layer then error() end
|
|
if not color[4] then color[4] = 1 end
|
|
local last = drawing.lastTexture
|
|
if hash ~= last.hash or x ~= last.x or y ~= last.y or layer ~= last.layer then
|
|
color.nwType = "color"
|
|
levelloop.remoteCall("drawTexture", x, y, layer, hash, color)
|
|
end
|
|
last.hash, last.x, last.y, last.layer = hash, x, y, layer
|
|
end
|
|
|
|
drawing.lastTextureDel = { x = 0, y = 0, width = 0, height = 0, layer = ""}
|
|
function drawing.del_texture(x, y, width, height, layer)
|
|
if not layer then error() end
|
|
|
|
local last = drawing.lastTextureDel
|
|
if x ~= last.x or y ~= last.y or width ~= last.width
|
|
or height ~= last.height or layer ~= last.layer then
|
|
levelloop.remoteCall("deleteTexture", x, y, width, height, layer)
|
|
last.x, last.y, last.width, last.height, last.layer = x, y, width, height, layer
|
|
end
|
|
end
|
|
|
|
|
|
local lastDWP = { x = 0, y = 0, width = 0, height = 0}
|
|
function drawing.draw_solid(x, y, width, height)
|
|
if x ~= lastDWP.x or y ~= lastDWP.y or width ~= lastDWP.width or height ~= lastDWP.height then
|
|
levelloop.remoteCall("drawPhysics", x, y, width, height)
|
|
physics.draw_solid(x, y, width, height)
|
|
lastDWP.x, lastDWP.y, lastDWP.width, lastDWP.height = x, y, width, height
|
|
end
|
|
end
|
|
|
|
|
|
local lastRMP = { x = 0, y = 0, width = 0, height = 0}
|
|
function drawing.remove_solid(x, y, width, height)
|
|
if x ~= lastRMP.x or y ~= lastRMP.y or width ~= lastRMP.width or height ~= lastRMP.height then
|
|
levelloop.remoteCall("deletePhysics", x, y, width, height)
|
|
physics.remove_solid(x, y, width, height)
|
|
lastRMP.x, lastRMP.y, lastRMP.width, lastRMP.height = x, y, width, height
|
|
end
|
|
end
|
|
|
|
|
|
function drawing.init()
|
|
local path = "textures/blocks/krock/Glass/03.png"
|
|
local texture = {data = love.image.newImageData(path),
|
|
image = love.graphics.newImage(path)}
|
|
drawing.color = {1, 1, 1, 1}
|
|
drawing.cursorHash = love.data.hash("sha512", texture.data:getString())
|
|
textures[drawing.cursorHash] = texture
|
|
drawing.cursor = textures[drawing.cursorHash]
|
|
|
|
texture, path = nil, nil
|
|
|
|
mouse.lastpos = {}
|
|
mouse.lastpos.x = 0
|
|
mouse.lastpos.y = 0
|
|
end
|