Files
tromi_mobile/scene/touch_config.lua
Squishy (C6H12O6+NaCl+H2O) d17df2367b Reverse a change
2024-05-25 00:55:56 +07:00

254 lines
7.5 KiB
Lua

---@diagnostic disable: cast-local-type
local TouchConfigScene = SCENE:extend()
TouchConfigScene.title = "Touchscreen config"
local Grid = require 'game.grid'
local buttonList
local sliderList
---@class VCTRL.data
local focusingButton
---@type number
local gridSize = 1
---@type boolean
local hasChanged
---@type function
local exitSceneFunc = function()
VCTRL.release()
BUTTON.release(buttonList)
SCENE = TitleScene()
end
buttonList = {
showToggle = BUTTON.new{
text = function()
if focusingButton then
return focusingButton.show and "[SHOW]\nHide" or "Show\n[HIDE]"
else
return "Show\nHide"
end
end,
x = 275, y = 5, w = 50, h = 75,
codeWhenReleased = function ()
if focusingButton then
focusingButton.show = not focusingButton.show
hasChanged = true
end
end,
update = function(self) self.textColor = focusingButton and {1, 1, 1} or {0.5, 0.5, 0.5} end
},
previewToggle = BUTTON.new{
text = "Preview\nON",
x = 570, y = 35, w = 60, h = 40,
codeWhenReleased = function()
VCTRL.release()
BUTTON.release(buttonList)
SCENE = TouchConfigPreviewScene()
end
},
menuScreen = BUTTON.new{
text = "MENU",
x = 570, y = 5, w = 60, h = 25,
codeWhenReleased = function()
if hasChanged then
local selection = love.window.showMessageBox(
"Save config?", "Do you want to save your changes before exiting?",
{"Save", "Discard", "Keep editing", escapebutton = 2, enterbutton = 1},
"info", true
)
if selection == 1 then
SETTINGS.input.virtual = VCTRL.exportAll()
SETTINGS.firstTime = false
-- love.window.showMessageBox("Saved!", "Your changes was saved!")
exitSceneFunc()
elseif selection == 2 then
VCTRL.clearAll()
VCTRL.new(SETTINGS.input.virtual)
-- love.window.showMessageBox("Discarded!", "Your changes was discarded!")
exitSceneFunc()
end
else
exitSceneFunc()
end
end
}
}
sliderList = {}
sliderList.opacity = newSlider(
155, 20+5, 120, 100, 0, 100,
function()
local v
if focusingButton then
v = math.roundUnit(sliderList.opacity.value, 0.01)
if focusingButton.alpha~=v then
focusingButton.alpha = v
hasChanged = true
end
sliderList.opacity.value = v
end
end,
{width = 30}
)
sliderList.size = newSlider(
155, 60+2.5, 120, 45, 0, 120,
function(v)
if focusingButton then
local v = math.roundUnit(v, 5)
if focusingButton.r ~= v then
focusingButton.r = v
hasChanged = true
end
sliderList.size.value = v / 120
end
end,
{width = 30}
)
local gridSizeTable = {1, 2, 5, 10, 20, 50, 100}
sliderList.gridSize = newSlider(
405, 50, 100, 1, 1, #gridSizeTable - 1,
function()
local v = math.roundUnit(sliderList.gridSize.value, 1 / 6)
sliderList.gridSize.value = v
gridSize = gridSizeTable[math.roundUnit(v * (#gridSizeTable - 1) + 1, 1)]
end,
{width = 30}
); sliderList.gridSize.forceLight = true
local function sliderList_draw()
for _, s in pairs(sliderList) do
if s.forceLight then
love.graphics.setColor(1, 1, 1)
else
love.graphics.setColor(focusingButton and {1, 1, 1} or {0.5, 0.5, 0.5})
end
love.graphics.setLineWidth(1)
s:draw()
end
end
local function sliderList_update()
local x, y
if #love.touch.getTouches() == 1 then
x, y = GLOBAL_TRANSFORM:inverseTransformPoint(love.touch.getPosition(love.touch.getTouches()[1]))
else
x, y = GLOBAL_TRANSFORM:inverseTransformPoint(love.mouse.getPosition())
end
for _, s in pairs(sliderList) do
s:update(x, y, #love.touch.getTouches() == 1 or love.mouse.isDown(1))
end
end
function TouchConfigScene:new()
VCTRL.toggle(true)
VCTRL.focus = nil
focusingButton = nil
hasChanged = false
Grid:new(10, 20)
-- TODO
end
function TouchConfigScene:update()
-- TODO
if VCTRL.focus~=focusingButton then
focusingButton = VCTRL.focus
sliderList.opacity.value = focusingButton.alpha
sliderList.size.value = focusingButton.r / 120
end
BUTTON.update(buttonList)
sliderList_update()
end
function TouchConfigScene:render()
MainBackground()
if gridSize >= 5 then
love.graphics.setColor(1,1,1,0.5)
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)
end
-- From 0 to Y
for iy=0,math.floor(480 / gridSize) do
love.graphics.line(0, gridSize * iy, 640, gridSize * iy)
end
end
love.graphics.setColor(0, 0, 0, 0.7)
-- Opacity and Size
love.graphics.rectangle("fill", 10, 5, 263, 75)
-- Snap to grid
love.graphics.rectangle("fill", 330, 5, 150, 75)
-- Opacity
drawText("Opacity", 20, 15, 100, "left")
drawText(string.format("%3.1d%%", focusingButton and focusingButton.alpha * 100 or 0), 225, 15, 40, "left")
-- Size
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")
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)
)
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
)
)
end
sliderList_draw()
BUTTON.draw(buttonList)
VCTRL.draw()
end
---@param e SCENE_onInput
function TouchConfigScene:onInputMove(e)
if e.type == "touch" or (e.type == "mouse" and love.mouse.isDown(1)) then
if VCTRL.drag(e.dx, e.dy, e.id or 1) then hasChanged = true end
end
if e.type == "mouse" then
BUTTON.checkHovering(buttonList, e.x, e.y)
end
end
---@param e SCENE_onInput
function TouchConfigScene:onInputPress(e)
if e.type == "mouse" or e.type == "touch" then
if not (
VCTRL.press(e.x, e.y, e.id and e.id or 1, true) or
BUTTON.press(buttonList, e.x, e.y, e.id) or
(e.x >= 80 and e.x <= 230 and e.y >= 10 and e.y <= 77)
) then
VCTRL.focus = nil
focusingButton = nil
end
end
end
---@param e SCENE_onInput
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)
end
end
end
end
return TouchConfigScene