~akkartik/snap.love

4bd3075a1003f5bfa5fd1f36b0b328ba59ba1c08 — Kartik K. Agaram 2 months ago 0fa25e0
show a grid when dragging things

Helps me keep my pictures a little neater.
5 files changed, 37 insertions(+), 1 deletions(-)

M 0026-on.draw
A 0074-on.resize
A 0075-draw_grid
A 0076-floor_to
A 0077-base_for_zoom
M 0026-on.draw => 0026-on.draw +2 -1
@@ 1,4 1,5 @@
on.draw = function()
	if App.mouse_down(1) then draw_grid() end
	for _,obj in ipairs(Surface) do
		love.graphics.setColor(obj.r or 0, obj.g or 0, obj.b or 0)
		if obj.type == 'rectangle' then


@@ 27,4 28,4 @@ on.draw = function()
		love.graphics.circle('fill', vx(Edge.s.sx),vy(Edge.s.sy), 5)
		love.graphics.circle('fill', vx(Edge.e.sx),vy(Edge.e.sy), 5)
	end
end
end
\ No newline at end of file

A 0074-on.resize => 0074-on.resize +3 -0
@@ 0,0 1,3 @@
on.resize = function(w, h)
	Viewport.w, Viewport.h = w, h
end
\ No newline at end of file

A 0075-draw_grid => 0075-draw_grid +14 -0
@@ 0,0 1,14 @@
draw_grid = function()
	love.graphics.setColor(0.8, 0.8, 0.8)
	local xlo, xhi = sx(0), sx(Viewport.w)
	local ylo, yhi = sy(0), sy(Viewport.h)
	local base = base_for_zoom(100)
	xlo, xhi = floor_to(xlo, base), floor_to(xhi, base)+base
	ylo, yhi = floor_to(ylo, base), floor_to(yhi, base)+base
	for x = xlo, xhi, base do
		love.graphics.line(vx(x), 0, vx(x), Viewport.h)
	end
	for y = ylo, yhi, base do
		love.graphics.line(0, vy(y), Viewport.w, vy(y))
	end
end
\ No newline at end of file

A 0076-floor_to => 0076-floor_to +3 -0
@@ 0,0 1,3 @@
floor_to = function(n, base)
	return math.floor(n/base)*base
end
\ No newline at end of file

A 0077-base_for_zoom => 0077-base_for_zoom +15 -0
@@ 0,0 1,15 @@
base_for_zoom = function(base)
	local n = base
	while scale(n) < base/2 do
		if scale(n*base) < base/2 then
			n = n*base
		else  -- scale linearly
			local n2 = n
			while scale(n2) < base/2 do
				n2 = n2+n
			end
			return n2
		end
	end
	return n
end
\ No newline at end of file