implemented mouse controls for titlescreen mode_select

added supporting functions to util and dimensions
This commit is contained in:
Hersi 2022-03-27 03:54:02 +02:00
parent 5d3e4457eb
commit f227df0dee
4 changed files with 171 additions and 53 deletions

View File

@ -21,4 +21,25 @@ dimtable.updateResolution = function(ratio)
end end
end end
---Convert screenspace coordinates to viewspace coordinates
---@param screenX number
---@param screenY number
---@param offsetX? number Viewport offset from the left side (defaults to the portrait viewport offset)
---@param offsetY? number Viewport offset from the top side (defaults to 0)
---@return number, number
dimtable.toViewSpace = function (screenX, screenY, offsetX, offsetY)
offsetX = offsetX or (dimtable.screen.width - dimtable.view.width) / 2
offsetY = offsetY or 0
local viewX, viewY, scaleX, scaleY
scaleX = dimtable.design.width / dimtable.view.width
scaleY = dimtable.design.height / dimtable.view.height
viewX = (screenX - offsetX) * scaleX
viewY = (screenY - offsetY) * scaleY
return viewX, viewY
end
return dimtable return dimtable

View File

@ -54,11 +54,16 @@ local function roundToZero(x)
end end
end end
local function areaOverlap(x, y, areaX, areaY, areaW, areaH)
return x > areaX and y > areaY and x < areaX + areaW and y < areaY + areaH
end
return { return {
splitString = splitString, splitString = splitString,
filter = filter, filter = filter,
clamp = clamp, clamp = clamp,
round = round, round = round,
sign = sign, sign = sign,
roundToZero = roundToZero roundToZero = roundToZero,
areaOverlap = areaOverlap
} }

View File

@ -85,10 +85,10 @@ function render(deltaTime)
end end
function mouse_pressed(button) function mouse_pressed(button)
if true then return 0 end
if (currentScreen.screen.onMousePressed) then if (currentScreen.screen.onMousePressed) then
currentScreen.screen.onMousePressed(button) currentScreen.screen.onMousePressed(button)
end end
return 0
end end
function button_pressed(button) function button_pressed(button)

View File

@ -4,6 +4,7 @@ local Wallpaper = require("components.wallpaper")
local Background = require("components.background") local Background = require("components.background")
local Dim = require("common.dimensions") local Dim = require("common.dimensions")
local lang = require("language.call") local lang = require("language.call")
local util = require("common.util")
local cursorIndex = 3 local cursorIndex = 3
local buttonHeight = 128 + 16 local buttonHeight = 128 + 16
@ -89,6 +90,51 @@ local buttons = {
}, },
} }
local miscButtons = {
upArrow = {
x = Dim.design.width - 265,
y = Dim.design.height / 2 + SELECTOR_BAR_OFFSET_FROM_CENTER - buttonHeight + 4,
w = 64,
h = 36
},
downArrow = {
x = Dim.design.width - 265,
y = Dim.design.height / 2 + SELECTOR_BAR_OFFSET_FROM_CENTER + buttonHeight / 2 + 28,
w = 64,
h = 36
},
mainButton = {
x = Dim.design.width - 512,
y = Dim.design.height / 2 + SELECTOR_BAR_OFFSET_FROM_CENTER - buttonHeight / 2 - 28,
w = 505,
h = 196
},
upButton1 = {
x = Dim.design.width - 512,
y = Dim.design.height / 2 + SELECTOR_BAR_OFFSET_FROM_CENTER - 128 - buttonHeight,
w = 1026 / 2,
h = 257 / 2
},
upButton2 = {
x = Dim.design.width - 512,
y = Dim.design.height / 2 + SELECTOR_BAR_OFFSET_FROM_CENTER - 128 - buttonHeight * 2,
w = 1026 / 2,
h = 257 / 2
},
downButton1 = {
x = Dim.design.width - 512,
y = Dim.design.height / 2 + SELECTOR_BAR_OFFSET_FROM_CENTER + 128 + 10,
w = 1026 / 2,
h = 257 / 2
},
downButton2 = {
x = Dim.design.width - 512,
y = Dim.design.height / 2 + SELECTOR_BAR_OFFSET_FROM_CENTER + 128 + buttonHeight + 10,
w = 1026 / 2,
h = 257 / 2
},
}
local scrollTransitionScale = 1 -- Goes from 0 to 1 when transition is happening, sits at 1 when it's not. local scrollTransitionScale = 1 -- Goes from 0 to 1 when transition is happening, sits at 1 when it's not.
local buttonsMovementScale = 0 -- Basically same as `scrollTransitionScale` but with a +/- sign for the scroll direction and goes from 1 to 0 local buttonsMovementScale = 0 -- Basically same as `scrollTransitionScale` but with a +/- sign for the scroll direction and goes from 1 to 0
@ -100,6 +146,15 @@ local playedBgm = false
local triggerServiceMenu = false local triggerServiceMenu = false
local function setButtonActions()
buttons[1].action = Menu.Challenges
buttons[2].action = Menu.Multiplayer
buttons[3].action = Menu.Start
buttons[4].action = Menu.DLScreen
buttons[5].action = Menu.Settings
buttons[6].action = Menu.Exit
end
local function draw_button(button, x, y, selected, index) local function draw_button(button, x, y, selected, index)
if (selected) then if (selected) then
-- Draw button background -- Draw button background
@ -234,15 +289,6 @@ local function drawTexts()
gfx.FillColor(255, 255, 255) gfx.FillColor(255, 255, 255)
end end
local function setButtonActions()
buttons[1].action = Menu.Challenges
buttons[2].action = Menu.Multiplayer
buttons[3].action = Menu.Start
buttons[4].action = Menu.DLScreen
buttons[5].action = Menu.Settings
buttons[6].action = Menu.Exit
end
local function drawHeader() local function drawHeader()
gfx.BeginPath() gfx.BeginPath()
gfx.FillColor(0, 0, 0, BAR_ALPHA) gfx.FillColor(0, 0, 0, BAR_ALPHA)
@ -253,26 +299,6 @@ local function drawHeader()
gfx.ImageRect(Dim.design.width / 2 - 200, HEADER_HEIGHT / 2 - 20, 400, 40, resources.images.headerTitleImage, 1, 0) gfx.ImageRect(Dim.design.width / 2 - 200, HEADER_HEIGHT / 2 - 20, 400, 40, resources.images.headerTitleImage, 1, 0)
end end
local function onKnobsChange(direction)
-- game.Log(direction, game.LOGGER_ERROR)
cursorIndex = cursorIndex + direction
if (cursorIndex == 0) then
cursorIndex = #buttons
elseif (cursorIndex == #buttons + 1) then
cursorIndex = 1
end
scrollTransitionScale = 0 -- Reset transitions and play them
scrollingUp = false
if ((cursorIndex > oldCursorIndex and not (cursorIndex == 6 and oldCursorIndex == 1)) or
(cursorIndex == 1 and oldCursorIndex == 6)) then scrollingUp = true end
game.PlaySample("titlescreen/cursor_change.wav")
oldCursorIndex = cursorIndex
end
local function draw_titlescreen(deltaTime) local function draw_titlescreen(deltaTime)
gfx.LoadSkinFont("segoeui.ttf") gfx.LoadSkinFont("segoeui.ttf")
@ -310,24 +336,9 @@ local function draw_titlescreen(deltaTime)
-- Draw top and bottom bars -- Draw top and bottom bars
drawHeader() drawHeader()
Footer.draw(deltaTime) Footer.draw(deltaTime)
gfx.ResetTransform()
end end
local function render(deltaTime) local function tickTransitions(deltaTime)
if not playedBgm then
game.PlaySample("titlescreen/bgm.wav", true)
playedBgm = true
end
Dim.updateResolution()
Wallpaper.render()
Dim.transformToScreenSpace()
draw_titlescreen(deltaTime)
scrollTransitionScale = scrollTransitionScale + deltaTime / 0.2 scrollTransitionScale = scrollTransitionScale + deltaTime / 0.2
if (scrollTransitionScale > 1) then scrollTransitionScale = 1 end if (scrollTransitionScale > 1) then scrollTransitionScale = 1 end
@ -336,13 +347,49 @@ local function render(deltaTime)
else else
buttonsMovementScale = -1 + scrollTransitionScale buttonsMovementScale = -1 + scrollTransitionScale
end end
end
local function render(deltaTime)
if not playedBgm then
game.PlaySample("titlescreen/bgm.wav", true)
playedBgm = true
end
game.SetSkinSetting("_currentScreen", "title")
Dim.updateResolution()
Wallpaper.render()
Dim.transformToScreenSpace()
tickTransitions(deltaTime)
draw_titlescreen(deltaTime)
if (triggerServiceMenu) then if (triggerServiceMenu) then
triggerServiceMenu = false triggerServiceMenu = false
return {eventType = "switch", toScreen = "service"} return {eventType = "switch", toScreen = "service"}
end end
end
gfx.BeginPath() local function callButtonAction()
if buttons[cursorIndex].action == nil then setButtonActions() end
buttons[cursorIndex].action()
end
local function onKnobsChange(direction)
cursorIndex = (cursorIndex - 1 + direction) % #buttons + 1
scrollTransitionScale = 0 -- Reset transitions and play them
scrollingUp = false
if ((cursorIndex > oldCursorIndex and not (cursorIndex == 6 and oldCursorIndex == 1)) or
(cursorIndex == 1 and oldCursorIndex == 6)) then scrollingUp = true end
game.PlaySample("titlescreen/cursor_change.wav")
oldCursorIndex = cursorIndex
end end
local function onButtonPressed(button) local function onButtonPressed(button)
@ -350,8 +397,7 @@ local function onButtonPressed(button)
game.PlaySample("titlescreen/cursor_select.wav") game.PlaySample("titlescreen/cursor_select.wav")
game.StopSample("titlescreen/bgm.wav") game.StopSample("titlescreen/bgm.wav")
if buttons[cursorIndex].action == nil then setButtonActions() end callButtonAction()
buttons[cursorIndex].action()
elseif button == game.BUTTON_BCK then elseif button == game.BUTTON_BCK then
Menu.Exit() Menu.Exit()
@ -362,12 +408,58 @@ local function onButtonPressed(button)
end end
end end
-- the thing is... titlescreen script does not have a call to reset function... WHYYYYY local function onMousePressed(button)
function reset() playedBgm = false end local mousePosX, mousePosY = Dim.toViewSpace(game.GetMousePos())
local changeIndex = 0
if button ~= 0 then
return
end
if util.areaOverlap(mousePosX, mousePosY,
miscButtons.mainButton.x, miscButtons.mainButton.y, miscButtons.mainButton.w, miscButtons.mainButton.h) then
game.StopSample("titlescreen/bgm.wav")
game.PlaySample("titlescreen/cursor_select.wav")
callButtonAction()
return
elseif util.areaOverlap(mousePosX, mousePosY,
miscButtons.upArrow.x, miscButtons.upArrow.y, miscButtons.upArrow.w, miscButtons.upArrow.h) or
util.areaOverlap(mousePosX, mousePosY,
miscButtons.upButton1.x, miscButtons.upButton1.y, miscButtons.upButton1.w, miscButtons.upButton1.h) then
changeIndex = -1
scrollTransitionScale = 0
scrollingUp = false
elseif util.areaOverlap(mousePosX, mousePosY,
miscButtons.downArrow.x, miscButtons.downArrow.y, miscButtons.downArrow.w, miscButtons.downArrow.h) or
util.areaOverlap(mousePosX, mousePosY,
miscButtons.downButton1.x, miscButtons.downButton1.y, miscButtons.downButton1.w, miscButtons.downButton1.h) then
changeIndex = 1
scrollTransitionScale = 0
scrollingUp = true
elseif util.areaOverlap(mousePosX, mousePosY,
miscButtons.upButton2.x, miscButtons.upButton2.y, miscButtons.upButton2.w, miscButtons.upButton2.h) then
changeIndex = -2
scrollTransitionScale = 0
scrollingUp = false
elseif util.areaOverlap(mousePosX, mousePosY,
miscButtons.downButton2.x, miscButtons.downButton2.y, miscButtons.downButton2.w, miscButtons.downButton2.h) then
changeIndex = 2
scrollTransitionScale = 0
scrollingUp = true
end
cursorIndex = (cursorIndex - 1 + changeIndex) % #buttons + 1
game.PlaySample("titlescreen/cursor_change.wav")
end
return { return {
render = render, render = render,
onKnobsChange = onKnobsChange, onKnobsChange = onKnobsChange,
onButtonPressed = onButtonPressed, onButtonPressed = onButtonPressed,
onMousePressed = function() return false end, onMousePressed = onMousePressed,
} }