Fix grid drawing

This commit is contained in:
Squishy (C6H12O6+NaCl+H2O)
2024-05-25 15:23:44 +07:00
parent fda054a2f5
commit dfeece9ddc
3 changed files with 24 additions and 19 deletions

View File

@@ -87,10 +87,10 @@ end
---Round a number with specified unit
---@param n number
---@param u? number # Default: 10
---@param u? number|1
---@return number
function math.roundUnit(n,u)
local u = u or 10
local u = u or 1
return math.floor(n/u+.5)*u
end

View File

@@ -46,7 +46,7 @@ control_type.button.__index=control_type.button
function control_type.button:new(data)
local data=data or {}
return setmetatable({
show=data.show~=nil and data.show or true,
show=data.show==nil and true or data.show,
x=data.x or 320,
y=data.y or 240,
r=data.r or 80, -- size
@@ -59,6 +59,7 @@ function control_type.button:new(data)
end
function control_type.button:export()
return {
type = 'button',
show = self.show,
x = self.x,
y = self.y,

View File

@@ -9,7 +9,7 @@ local sliderList
---@class VCTRL.data
local focusingButton
---@type number
local gridSize = 1
local snapUnit = 1
---@type boolean
local hasChanged
@@ -112,7 +112,7 @@ sliderList.gridSize = newSlider(
function()
local v = math.roundUnit(sliderList.gridSize.value, 1 / 6)
sliderList.gridSize.value = v
gridSize = gridSizeTable[math.roundUnit(v * (#gridSizeTable - 1) + 1, 1)]
snapUnit = gridSizeTable[math.roundUnit(v * (#gridSizeTable - 1) + 1)]
end,
{width = 30}
); sliderList.gridSize.forceLight = true
@@ -165,22 +165,27 @@ end
function TouchConfigScene:render()
MainBackground()
if gridSize >= 5 then
love.graphics.setColor(1,1,1,0.5)
if snapUnit >= 5 then
local x1, y1 = GLOBAL_TRANSFORM:inverseTransformPoint(0, 0)
local x2, y2 = GLOBAL_TRANSFORM:inverseTransformPoint(love.graphics.getDimensions())
love.graphics.setColor(1,1,1,math.sin(love.timer.getTime()*4)*.1+.25)
love.graphics.setLineWidth(1)
-- From 0 to X
for ix=0,math.floor(640 / gridSize) do
love.graphics.line(gridSize * ix, 0 , gridSize * ix, 480)
for i=x1, x2+snapUnit, snapUnit do
local x = i - i % snapUnit
love.graphics.line(x, y1, x, y2)
end
-- From 0 to Y
for iy=0,math.floor(480 / gridSize) do
love.graphics.line(0, gridSize * iy, 640, gridSize * iy)
for i=y1,y2+snapUnit,snapUnit do
local y= i - i % snapUnit
love.graphics.line(x1, y, x2, y)
end
end
love.graphics.setColor(0, 0, 0, 0.7)
-- Opacity and Size
love.graphics.rectangle("fill", 10, 5, 263, 75)
love.graphics.rectangle("fill", 10, 5, 267, 75)
-- Snap to grid
love.graphics.rectangle("fill", 330, 5, 150, 75)
@@ -191,29 +196,28 @@ function TouchConfigScene:render()
drawText("Size", 20, 55, 100, "left")
drawText(string.format("%3.1dpx", focusingButton and focusingButton.r or 0), 225, 55, 40, "left")
-- Snap to grid
drawText(string.format("Snap to grid: %3s", gridSize), 345, 15, 140, "left")
drawText(string.format("Snap to grid: %3s", snapUnit), 345, 15, 140, "left")
for _, v in ipairs(VCTRL) do
if v ~= focusingButton then
v:draw(
focusingButton and
(v.show and 0.5 or 0.1) or
(v.show and 0.5 or 0.25)
(v.show and 1 or 0.5)
)
end
end
if focusingButton then
focusingButton:draw(
math.clamp(
math.abs(math.sin(love.timer.getTime()*4)),
focusingButton.show and 0.5 or 0.1, 1
math.sin(love.timer.getTime()*4)*.5+0.1,
focusingButton.show and 1 or 0.1, 1
)
)
end
sliderList_draw()
BUTTON.draw(buttonList)
VCTRL.draw()
end
---@param e SCENE_onInput
@@ -244,8 +248,8 @@ function TouchConfigScene:onInputRelease(e)
if e.type == "mouse" or e.type == "touch" then
if not BUTTON.release(buttonList, e.x, e.y, e.id) then
if focusingButton and VCTRL.release(e.id or 1) then
focusingButton.x = math.roundUnit(focusingButton.x, gridSize)
focusingButton.y = math.roundUnit(focusingButton.y, gridSize)
focusingButton.x = math.roundUnit(focusingButton.x, snapUnit)
focusingButton.y = math.roundUnit(focusingButton.y, snapUnit)
end
end
end