poppy-client/lua/drawing.lua

218 lines
6.6 KiB
Lua
Raw Permalink Normal View History

2020-02-20 16:46:19 +00:00
drawing = {}
2021-11-11 19:25:05 +00:00
function alignToGrid(num, thing)
2022-06-15 19:43:39 +00:00
return num - (num % thing)
2021-11-11 19:25:05 +00:00
end
2022-08-04 19:06:21 +00:00
local utils = require("shared.utils")
2021-11-13 19:56:43 +00:00
function drawing.clearAllCanvases()
love.graphics.setCanvas(levelloop.Canvas.fg)
2021-11-13 19:56:43 +00:00
love.graphics.clear()
love.graphics.setCanvas(levelloop.Canvas.bg)
2021-11-13 19:56:43 +00:00
love.graphics.clear()
love.graphics.setCanvas(levelloop.Canvas.dbg)
2021-11-13 19:56:43 +00:00
love.graphics.clear()
love.graphics.setCanvas()
levelloop.Canvas.physics = {}
2021-11-13 19:56:43 +00:00
end
2022-06-23 17:33:05 +00:00
function drawing.clearAllCanvasesNetwork()
2024-02-25 20:48:04 +00:00
levelloop.remoteCall("clearCanvas")
2022-06-23 17:33:05 +00:00
drawing.clearAllCanvases()
end
function drawing.fillCanvas(layer, hash, color)
love.graphics.setCanvas(levelloop.Canvas[layer])
2022-10-09 10:51:50 +00:00
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)
2022-06-23 17:33:05 +00:00
end
2022-10-09 10:51:50 +00:00
end
utils.color_pop()
2022-10-09 10:51:50 +00:00
love.graphics.setCanvas()
2022-06-23 17:33:05 +00:00
end
function drawing.fillCanvasNetwork(layer, hash, color)
2024-02-25 20:48:04 +00:00
color.nwType = "color"
levelloop.remoteCall("fillCanvas", layer, hash, color)
drawing.fillCanvas(layer, hash, color)
2022-06-23 17:33:05 +00:00
end
2021-11-13 19:56:43 +00:00
function drawing.keyreleased(key, _)
assert(drawing.color)
2021-11-13 19:56:43 +00:00
if key == "c" then
2022-06-24 17:54:38 +00:00
drawing.clearAllCanvasesNetwork()
2021-11-13 19:56:43 +00:00
end
if key == "f" then
drawing.fillCanvasNetwork("fg", drawing.cursorHash, drawing.color)
2021-11-13 19:56:43 +00:00
end
if key == "b" then
drawing.fillCanvasNetwork("bg", drawing.cursorHash, drawing.color)
2022-06-23 17:33:05 +00:00
end
2021-11-13 19:56:43 +00:00
end
2022-06-23 17:33:05 +00:00
local drawmouse = {}
2020-02-20 16:46:19 +00:00
function drawing.input(dt)
local textures = levelloop.textures
2022-06-23 17:33:05 +00:00
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)
2021-11-13 19:56:43 +00:00
local mousex = love.mouse.getX()
local mousey = love.mouse.getY()
2022-01-08 15:05:13 +00:00
if mousex > window.x -ui.space then return end
2020-02-20 16:46:19 +00:00
if not love.mouse.isDown(2) and not love.mouse.isDown(1) then
2022-06-23 17:33:05 +00:00
drawmouse.lastX = nil
drawmouse.lastY = nil
2021-01-01 00:40:05 +00:00
return
2020-02-20 16:46:19 +00:00
end
2020-09-29 16:54:18 +00:00
mousex = mousex / scale - view.x
mousey = mousey / scale - view.y
if mousex > levelloop.world.x or
mousey > levelloop.world.y or
2021-02-10 20:53:00 +00:00
mousex < 0 or
mousey < 0 then
return
end
2021-01-01 00:40:05 +00:00
local granularity = 1
2022-06-23 17:33:05 +00:00
if drawmouse.lastX and drawmouse.lastY then
local x = math.abs(mouse.lastpos.x - mousex)
local y = math.abs(mouse.lastpos.y - mousey)
2022-06-23 17:33:05 +00:00
granularity = math.sqrt(x * x + y * y) / ((cursor.image:getPixelWidth() + cursor.image:getPixelWidth()) / 2)
2021-01-01 00:40:05 +00:00
else
2022-06-23 17:33:05 +00:00
drawmouse.lastX = mousex
drawmouse.lastY = mousey
2021-01-01 00:40:05 +00:00
end
2021-02-12 15:06:35 +00:00
if not granularity then granularity = 1 end
2021-01-01 00:40:05 +00:00
for i=0,granularity,1 do
2022-06-23 17:33:05 +00:00
local thisx = alignToGrid((((drawmouse.lastX - mousex) / granularity) * i + mousex),
cursor.image:getPixelWidth())
local thisy = alignToGrid((((drawmouse.lastY - mousey) / granularity) * i + mousey),
cursor.image:getPixelHeight())
2021-02-12 19:09:08 +00:00
if not thisx or not thisy then return end
2021-03-13 19:45:07 +00:00
if not (thisx == thisx) or not (thisy == thisy) then return end
2020-02-20 16:46:19 +00:00
2021-11-15 17:31:31 +00:00
local ctrlDown = love.keyboard.isDown("lctrl") or love.keyboard.isDown("rctrl")
if ctrlDown then
love.graphics.setCanvas(levelloop.Canvas.bg)
2021-11-11 20:20:00 +00:00
else
love.graphics.setCanvas(levelloop.Canvas.fg)
2021-11-11 20:20:00 +00:00
end
if love.mouse.isDown(1) then -- only delete
2022-08-04 19:06:21 +00:00
utils.color_push()
2022-02-16 18:24:47 +00:00
love.graphics.setColor(drawing.color)
2022-06-23 17:33:05 +00:00
love.graphics.draw(cursor.image, thisx, thisy)
2022-08-04 19:06:21 +00:00
utils.color_pop()
2022-01-07 22:20:55 +00:00
if ctrlDown then
2022-06-23 17:33:05 +00:00
drawing.draw_texture(drawing.cursorHash, thisx, thisy, "bg", drawing.color)
2022-01-07 22:20:55 +00:00
else
2022-06-23 17:33:05 +00:00
drawing.draw_texture(drawing.cursorHash, thisx, thisy, "fg", drawing.color)
2022-01-03 17:59:16 +00:00
drawing.draw_solid(thisx, thisy,
2022-06-23 17:33:05 +00:00
cursor.image:getPixelWidth(),
cursor.image:getPixelHeight())
end
2021-10-30 19:50:32 +00:00
else
2022-08-04 19:06:21 +00:00
love.graphics.setBlendMode("replace")
utils.color_push()
2022-01-09 09:47:55 +00:00
love.graphics.setColor(0,0,0,0)
love.graphics.rectangle("fill", thisx, thisy,
2022-06-23 17:33:05 +00:00
cursor.image:getPixelWidth(),
cursor.image:getPixelHeight())
2022-01-08 11:00:31 +00:00
if ctrlDown then
drawing.del_texture(thisx, thisy,
2022-06-23 17:33:05 +00:00
cursor.image:getPixelWidth(),
cursor.image:getPixelHeight(), "bg")
2022-01-08 11:00:31 +00:00
else
drawing.del_texture(thisx, thisy,
2022-06-23 17:33:05 +00:00
cursor.image:getPixelWidth(),
cursor.image:getPixelHeight(), "fg")
2022-01-03 17:59:16 +00:00
drawing.remove_solid(thisx, thisy,
2022-06-23 17:33:05 +00:00
cursor.image:getPixelWidth(),
cursor.image:getPixelHeight())
end
2022-08-04 19:06:21 +00:00
utils.color_pop()
love.graphics.setBlendMode("alpha")
2020-02-20 16:46:19 +00:00
end
love.graphics.setCanvas()
2020-02-20 16:46:19 +00:00
end
2022-06-23 17:33:05 +00:00
drawmouse.lastX = mousex
drawmouse.lastY = mousey
2021-01-01 00:40:05 +00:00
love.graphics.setCanvas()
2020-02-20 16:46:19 +00:00
end
2022-01-03 17:59:16 +00:00
2022-02-21 19:09:41 +00:00
2022-01-08 11:00:31 +00:00
drawing.lastTexture = { hash = "", x = 0, y = 0, layer = ""}
2022-02-17 11:19:51 +00:00
function drawing.draw_texture(hash, x, y, layer, color)
2022-01-08 11:00:31 +00:00
if not layer then error() end
2022-02-17 11:19:51 +00:00
if not color[4] then color[4] = 1 end
2022-01-08 11:00:31 +00:00
local last = drawing.lastTexture
if hash ~= last.hash or x ~= last.x or y ~= last.y or layer ~= last.layer then
2024-02-25 20:48:04 +00:00
color.nwType = "color"
levelloop.remoteCall("drawTexture", x, y, layer, hash, color)
2022-01-08 11:00:31 +00:00
end
last.hash, last.x, last.y, last.layer = hash, x, y, layer
2022-01-07 22:20:55 +00:00
end
2022-01-08 11:00:31 +00:00
drawing.lastTextureDel = { x = 0, y = 0, width = 0, height = 0, layer = ""}
2022-01-07 22:20:55 +00:00
function drawing.del_texture(x, y, width, height, layer)
2022-01-08 11:00:31 +00:00
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
2024-02-25 20:48:04 +00:00
levelloop.remoteCall("deleteTexture", x, y, width, height, layer)
2022-01-08 11:00:31 +00:00
last.x, last.y, last.width, last.height, last.layer = x, y, width, height, layer
end
2022-01-07 22:20:55 +00:00
end
2022-02-21 19:09:41 +00:00
2022-02-25 20:39:14 +00:00
local lastDWP = { x = 0, y = 0, width = 0, height = 0}
2022-01-03 17:59:16 +00:00
function drawing.draw_solid(x, y, width, height)
2022-02-25 20:39:14 +00:00
if x ~= lastDWP.x or y ~= lastDWP.y or width ~= lastDWP.width or height ~= lastDWP.height then
2024-02-25 20:48:04 +00:00
levelloop.remoteCall("drawPhysics", x, y, width, height)
2022-01-08 11:00:31 +00:00
physics.draw_solid(x, y, width, height)
2022-02-25 20:39:14 +00:00
lastDWP.x, lastDWP.y, lastDWP.width, lastDWP.height = x, y, width, height
2022-01-08 11:00:31 +00:00
end
2022-01-03 17:59:16 +00:00
end
2022-02-21 19:09:41 +00:00
2022-02-25 20:39:14 +00:00
local lastRMP = { x = 0, y = 0, width = 0, height = 0}
2022-01-03 17:59:16 +00:00
function drawing.remove_solid(x, y, width, height)
2022-02-25 20:39:14 +00:00
if x ~= lastRMP.x or y ~= lastRMP.y or width ~= lastRMP.width or height ~= lastRMP.height then
2024-02-25 20:48:04 +00:00
levelloop.remoteCall("deletePhysics", x, y, width, height)
2022-01-08 11:00:31 +00:00
physics.remove_solid(x, y, width, height)
2022-02-25 20:39:14 +00:00
lastRMP.x, lastRMP.y, lastRMP.width, lastRMP.height = x, y, width, height
2022-01-08 11:00:31 +00:00
end
2022-01-03 17:59:16 +00:00
end
2022-06-19 10:41:25 +00:00
2022-08-04 19:06:21 +00:00
2022-06-19 10:41:25 +00:00
function drawing.init()
2022-06-25 20:03:07 +00:00
local path = "textures/blocks/krock/Glass/03.png"
2022-06-19 10:41:25 +00:00
local texture = {data = love.image.newImageData(path),
image = love.graphics.newImage(path)}
drawing.color = {1, 1, 1, 1}
2022-06-23 17:33:05 +00:00
drawing.cursorHash = love.data.hash("sha512", texture.data:getString())
textures[drawing.cursorHash] = texture
drawing.cursor = textures[drawing.cursorHash]
2022-06-19 10:41:25 +00:00
texture, path = nil, nil
mouse.lastpos = {}
mouse.lastpos.x = 0
mouse.lastpos.y = 0
end