summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorthe lemons <citrons@mondecitronne.com>2022-12-27 01:09:31 -0600
committerthe lemons <citrons@mondecitronne.com>2022-12-27 01:09:31 -0600
commitdb39f51f24406805ee50ad6fef89c32a71b179a7 (patch)
tree10d08a13052d897f469b60bec5691921892490ae
parentb7aca18f4c80426f528bc650e1f6bd01437c8a89 (diff)
WIP shape designer
-rw-r--r--designer/conf.lua5
-rw-r--r--designer/main.lua109
2 files changed, 114 insertions, 0 deletions
diff --git a/designer/conf.lua b/designer/conf.lua
new file mode 100644
index 0000000..9182b4a
--- /dev/null
+++ b/designer/conf.lua
@@ -0,0 +1,5 @@
+function love.conf(t)
+ t.identity = "11_111"
+ t.window.title = "designer"
+ t.window.resizable = true
+end
diff --git a/designer/main.lua b/designer/main.lua
new file mode 100644
index 0000000..0b8ad5d
--- /dev/null
+++ b/designer/main.lua
@@ -0,0 +1,109 @@
+line = love.graphics.line
+function box(x, y, w, h) line(x, y, x + w, y, x + w, y + h, x, y + h, x, y) end
+set_color = love.graphics.setColor
+
+local coarsity = 4
+local lines = {}
+local placing
+
+local function window_scale()
+ local w, h = love.graphics.getDimensions()
+ return 18 / math.min(w, h)
+end
+
+local function window_transform()
+ local scale = window_scale()
+ local w, h = love.graphics.getDimensions()
+ local trans = love.math.newTransform(w/2, h/2, 0, 1/scale, 1/scale)
+ return trans
+end
+
+local function nearest(x, y)
+ local c = 16 / coarsity
+ return math.floor(x / c + 0.5) * c,
+ math.floor(y / c + 0.5) * c
+end
+
+function love.draw()
+ love.graphics.applyTransform(window_transform())
+ love.graphics.setLineWidth(0.04)
+ set_color(0.2, 0.2, 0.2)
+ local c = 16 / coarsity
+ for x = -8, 8 - c, c do
+ for y = -8, 8 - c, c do
+ line(x, y, x + c, y)
+ line(x, y, x, y + c)
+ end
+ end
+ set_color(0.4, 0.4, 0.4)
+ box(-8, -8, 16, 16)
+ box(-4, -4, 8, 8)
+ set_color(1, 1, 1)
+ for _, l in ipairs(lines) do
+ line(unpack(l))
+ end
+ if placing then
+ for i = 1, #placing - 2, 2 do
+ line(unpack(placing, i, i + 3))
+ end
+ end
+end
+
+function love.mousepressed(x, y, button)
+ local x, y = window_transform():inverseTransformPoint(x, y)
+ if button == 1 then
+ local x, y = nearest(x, y)
+ if not placing then
+ placing = {x, y, x, y}
+ else
+ if placing[#placing - 3] == placing[#placing - 1]
+ and placing[#placing - 2] == placing[#placing] then
+ table.remove(placing)
+ table.remove(placing)
+ if #placing >= 4 then
+ table.insert(lines, placing)
+ end
+ placing = nil
+ else
+ table.insert(placing, x)
+ table.insert(placing, y)
+ end
+ end
+ end
+end
+
+function love.mousemoved(x, y, _, _)
+ local x, y = window_transform():inverseTransformPoint(x, y)
+ if placing then
+ local x, y = nearest(x, y)
+ placing[#placing - 1], placing[#placing] = x, y
+ end
+end
+
+function love.wheelmoved(_, y)
+ if y > 0 and coarsity < 64 then
+ coarsity = coarsity * 2
+ elseif y < 0 and coarsity > 4 then
+ coarsity = coarsity / 2
+ end
+end
+
+function to_array(lines)
+ local t = {}
+ for _, l in ipairs(lines) do
+ table.insert(t, "\t{"..table.concat(l, ", ").."}")
+ end
+ return "{\n"..table.concat(t, ",\n").."\n}\n"
+end
+
+function love.keypressed(key)
+ if love.keyboard.isDown "lctrl" or love.keyboard.isDown "rctrl" then
+ if key == "c" then
+ if love.keyboard.isDown "lalt" or love.keyboard.isDown "ralt" then
+ lines = {}
+ else
+ love.system.setClipboardText(to_array(lines))
+ end
+ end
+ end
+end