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

View File

@@ -46,7 +46,7 @@ control_type.button.__index=control_type.button
function control_type.button:new(data) function control_type.button:new(data)
local data=data or {} local data=data or {}
return setmetatable({ 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, x=data.x or 320,
y=data.y or 240, y=data.y or 240,
r=data.r or 80, -- size r=data.r or 80, -- size
@@ -59,6 +59,7 @@ function control_type.button:new(data)
end end
function control_type.button:export() function control_type.button:export()
return { return {
type = 'button',
show = self.show, show = self.show,
x = self.x, x = self.x,
y = self.y, y = self.y,

View File

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