From 20a7100521b2c149c8ab4cf98712ecfec69a5e81 Mon Sep 17 00:00:00 2001 From: Local Atticus Date: Tue, 26 Apr 2022 20:03:42 -0400 Subject: [PATCH] Refactor based on feedback and fix bugs --- scripts/challengeresult.lua | 2 +- scripts/common.lua | 207 +---------------------------- scripts/common/charting.lua | 26 ++++ scripts/common/dimensions.lua | 17 +++ scripts/common/util.lua | 29 ++-- scripts/components/background.lua | 2 - scripts/gameplay.lua | 33 +---- scripts/gameplay/console.lua | 6 +- scripts/gameplay/crit_line.lua | 10 +- scripts/gameplay/hitfx.lua | 9 +- scripts/gameplay/song_panel.lua | 3 +- scripts/result.lua | 3 +- scripts/songselect/chalwheel.lua | 5 +- scripts/songselect/filterwheel.lua | 1 - scripts/songselect/songwheel.lua | 8 +- scripts/songselect/sortwheel.lua | 1 - scripts/titlescreen/modeselect.lua | 2 +- 17 files changed, 98 insertions(+), 266 deletions(-) create mode 100644 scripts/common/charting.lua diff --git a/scripts/challengeresult.lua b/scripts/challengeresult.lua index 1ea708e..3d381bc 100644 --- a/scripts/challengeresult.lua +++ b/scripts/challengeresult.lua @@ -226,7 +226,7 @@ end function result_set() - local reqTextWords = common.splitString(result.requirement_text, ' '); + local reqTextWords = common.split(result.requirement_text, ' '); for index, word in ipairs(reqTextWords) do if string.find(word, '%%') ~= nil then -- %% = %, because % is an escape char local percNumber = tonumber(string.gsub(word, '%%', ''), 10) diff --git a/scripts/common.lua b/scripts/common.lua index 5eea277..5f50dee 100644 --- a/scripts/common.lua +++ b/scripts/common.lua @@ -1,203 +1,4 @@ -gfx.LoadSkinFont("segoeui.ttf") - --- Memo class -------------- -Memo = {} -Memo.new = function() - local this = { - cache = {} - } - setmetatable(this, {__index = Memo}) - return this -end - -Memo.memoize = function(this, key, generator) - local value = this.cache[key] - if not value then - value = generator() - this.cache[key] = value - end - return value -end - ---[[ - --- Image class --------------- -Image = { - ANCHOR_LEFT = 1, - ANCHOR_CENTER = 2, - ANCHOR_RIGHT = 4, - ANCHOR_TOP = 8, - ANCHOR_BOTTOM = 16 -} -Image.skin = function(filename, imageFlags) - imageFlags = imageFlags or 0 - local image = gfx.CreateSkinImage(filename, imageFlags) - return Image.wrap(image) -end -Image.wrap = function(image) - local this = { - image = image - } - local w, h = gfx.ImageSize(this.image) - this.w = w - this.h = h - setmetatable(this, {__index = Image}) - return this -end - -Image.draw = function(this, params) - local x = params.x - local y = params.y - local w = params.w or this.w - local h = params.h or this.h - local alpha = params.alpha or 1 - local angle = params.angle or 0 - local anchor_h = params.anchor_h or Image.ANCHOR_CENTER - local anchor_v = params.anchor_v or Image.ANCHOR_CENTER - local scale = params.scale or 1; - - w = w * scale; - h = h * scale; - - if anchor_h == Image.ANCHOR_CENTER then - x = x - w / 2 - elseif anchor_h == Image.ANCHOR_RIGHT then - x = x - w - end - - if anchor_v == Image.ANCHOR_CENTER then - y = y - h / 2 - elseif anchor_v == Image.ANCHOR_BOTTOM then - y = y - h - end - - gfx.BeginPath() - gfx.ImageRect(x, y, w, h, this.image, alpha, angle) -end - - --- ImageFont class ------------------- -ImageFont = {} -ImageFont.new = function(path, chars) - local this = { - images = {} - } - -- load character images - for i = 1, chars:len() do - local c = chars:sub(i, i) - local n = c - if c == "." then - n = "dot" - end - local image = Image.skin(string.format("%s/%s.png", path, n), 0) - this.images[c] = image - end - -- use size of first char as font size - local w, h = gfx.ImageSize(this.images[chars:sub(1, 1)].image) - this.w = w - this.h = h - - setmetatable(this, {__index = ImageFont}) - return this -end -ImageFont.draw = function(this, text, x, y, alpha, hFlag, vFlag) - local totalW = text:len() * this.w - - -- adjust horizontal alignment - if hFlag == gfx.TEXT_ALIGN_CENTER then - x = x - totalW / 2 - elseif hFlag == gfx.TEXT_ALIGN_RIGHT then - x = x - totalW - end - - -- adjust vertical alignment - if vFlag == gfx.TEXT_ALIGN_MIDDLE then - y = y - this.h / 2 - elseif vFlag == gfx.TEXT_ALIGN_BOTTOM then - y = y - this.h - end - - for i = 1, text:len() do - local c = text:sub(i, i) - local image = this.images[c] - if image ~= nil then - gfx.BeginPath() - gfx.ImageRect(x, y, this.w, this.h, image.image, alpha, 0) - end - x = x + this.w - end -end - -]] - -function GetDisplayDifficulty(jacketPath, difficulty) - if jacketPath == nil then - return difficulty - end - - local strippedPath = string.match(jacketPath:lower(), "[/\\][^\\/]+$") - if difficulty == 3 and strippedPath then - if string.find(strippedPath, "inf") ~= nil then - return 5 - elseif string.find(strippedPath, "grv") ~= nil then - return 6 - elseif string.find(strippedPath, "hvn") ~= nil then - return 7 - elseif string.find(strippedPath, "vvd") ~= nil then - return 8 - elseif string.find(strippedPath, "xcd") ~= nil then - return 9 - end - end - - return difficulty + 1 -end - -function split(s, delimiter) - result = {}; - for match in (s..delimiter):gmatch("(.-)"..delimiter) do - table.insert(result, match); - end - return result; -end - -function firstAlphaNum(s) - for i = 1, string.len(s) do - local byte = string.byte(s, i); - if ((byte >= 65 and byte <= 90) or (byte >= 97 and byte <= 122) or (byte >= 48 and byte <= 57)) then - return string.sub(s, i, i); - end - end - - return ''; -end - ----Set's up scaled transforms based on the current resolution. ----@param x number ----@param y number ----@param rotation number ----@return number, boolean # The scale applied to the transform and the current landscape state -function setUpTransforms(x, y, rotation) - local resx, resy = game.GetResolution(); - isLandscape = resx > resy; - - local desw, desh; - if (isLandscape) then - desw = 1920; - desh = 1080; - else - desw = 1080; - desh = 1920; - end - - scale = resx / desw; - - gfx.Translate(x, y); - gfx.Rotate(rotation); - gfx.Scale(scale, scale); - - return scale, isLandscape; -end \ No newline at end of file +-- NOTE(local): DO NOT PUT ANYTHING HERE PLEASE THANKS +-- IF YOU DO THE GAME WILL IMMEDIATELY CRASH AND IT'S NOT THE BEST +-- THANK YOU HAVE A GOOD LUA +gfx.LoadSkinFont("segoeui.ttf") \ No newline at end of file diff --git a/scripts/common/charting.lua b/scripts/common/charting.lua new file mode 100644 index 0000000..261c6d5 --- /dev/null +++ b/scripts/common/charting.lua @@ -0,0 +1,26 @@ +local Charting = { }; + +function Charting.GetDisplayDifficulty(jacketPath, difficulty) + if jacketPath == nil then + return difficulty + end + + local strippedPath = string.match(jacketPath:lower(), "[/\\][^\\/]+$") + if difficulty == 3 and strippedPath then + if string.find(strippedPath, "inf") ~= nil then + return 5 + elseif string.find(strippedPath, "grv") ~= nil then + return 6 + elseif string.find(strippedPath, "hvn") ~= nil then + return 7 + elseif string.find(strippedPath, "vvd") ~= nil then + return 8 + elseif string.find(strippedPath, "xcd") ~= nil then + return 9 + end + end + + return difficulty + 1 +end + +return Charting; diff --git a/scripts/common/dimensions.lua b/scripts/common/dimensions.lua index f07da58..e381b8a 100644 --- a/scripts/common/dimensions.lua +++ b/scripts/common/dimensions.lua @@ -42,4 +42,21 @@ dimtable.toViewSpace = function(screenX, screenY, offsetX, offsetY) return viewX, viewY end +---Set's up scaled transforms based on the current resolution. +---@param x number +---@param y number +---@param rotation number +---@return number, boolean # The scale applied to the transform and the current landscape state +function dimtable.setUpTransforms(x, y, rotation) + local scale = dimtable.screen.width / dimtable.view.width; + local isLandscape = dimtable.view.width > dimtable.view.height; + + gfx.ResetTransform(); + gfx.Translate(x, y); + gfx.Rotate(rotation); + gfx.Scale(scale, scale); + + return scale, isLandscape; +end + return dimtable diff --git a/scripts/common/util.lua b/scripts/common/util.lua index 1dbc03b..039a8bf 100644 --- a/scripts/common/util.lua +++ b/scripts/common/util.lua @@ -1,12 +1,9 @@ -local function splitString(inputstr, sep) - if sep == nil then - sep = "%s" +local function split(s, delimiter) + result = {}; + for match in (s..delimiter):gmatch("(.-)"..delimiter) do + table.insert(result, match); end - local t={} - for str in string.gmatch(inputstr, "([^"..sep.."]+)") do - table.insert(t, str) - end - return t + return result; end local function filter(tableIn, predicate) @@ -67,8 +64,19 @@ local function modIndex(index, mod) return (index - 1) % mod + 1 end +local function firstAlphaNum(s) + for i = 1, string.len(s) do + local byte = string.byte(s, i); + if ((byte >= 65 and byte <= 90) or (byte >= 97 and byte <= 122) or (byte >= 48 and byte <= 57)) then + return string.sub(s, i, i); + end + end + + return ''; +end + return { - splitString = splitString, + split = split, filter = filter, clamp = clamp, round = round, @@ -77,4 +85,5 @@ return { areaOverlap = areaOverlap, lerp = lerp, modIndex = modIndex, -} \ No newline at end of file + firstAlphaNum = firstAlphaNum, +} diff --git a/scripts/components/background.lua b/scripts/components/background.lua index b100a7a..3f5c9be 100644 --- a/scripts/components/background.lua +++ b/scripts/components/background.lua @@ -1,5 +1,3 @@ - - local resx, resy = game.GetResolution() local desw, desh = 1080,1920; local scale = 1; diff --git a/scripts/gameplay.lua b/scripts/gameplay.lua index 97a1148..96b8105 100644 --- a/scripts/gameplay.lua +++ b/scripts/gameplay.lua @@ -1,5 +1,11 @@ local VolforceWindow = require('components.volforceWindow') +local Dimensions = require 'common.dimensions'; + +do + local resx, resy = game.GetResolution(); + Dimensions.updateResolution(resx / resy); +end local Banner = require('gameplay.banner') local CritLine = require('gameplay.crit_line') @@ -17,30 +23,8 @@ local TrackEnd = require('gameplay.track_end') local json = require("common.json") --- Window variables -local resX, resY - --- Aspect Ratios -local landscapeWidescreenRatio = 16 / 9 -local landscapeStandardRatio = 4 / 3 -local portraitWidescreenRatio = 9 / 16 - --- Portrait sizes -local fullX, fullY -local desw = 1080 -local desh = 1920 - local showHitAnims = true; -local resolutionChange = function(x, y) - resX = x - resY = y - fullX = portraitWidescreenRatio * y - fullY = y - - game.Log('resX:' .. resX .. ' // resY:' .. resY .. ' // fullX:' .. fullX .. ' // fullY:' .. fullY, game.LOGGER_ERROR); -end - local users = nil local maxChain = 0; @@ -48,11 +32,8 @@ local chain = 0; local score = 0; function render(deltaTime) - -- detect resolution change local resx, resy = game.GetResolution(); - if resx ~= resX or resy ~= resY then - resolutionChange(resx, resy) - end + Dimensions.updateResolution(resx / resy); Banner.render(deltaTime, users, gameplay.user_id); diff --git a/scripts/gameplay/console.lua b/scripts/gameplay/console.lua index e85edd9..6c5d24f 100644 --- a/scripts/gameplay/console.lua +++ b/scripts/gameplay/console.lua @@ -1,4 +1,6 @@ +local Dimensions = require 'common.dimensions' + local consoleBaseImage = gfx.CreateSkinImage("gameplay/console/base.png", 0) local CONSOLE_W = 1352; @@ -9,8 +11,8 @@ local render = function (deltaTime, critLineCenterX, critLineCenterY, critLineRo if (resx > resy) then return end - - setUpTransforms( + + Dimensions.setUpTransforms( critLineCenterX, critLineCenterY, critLineRotation diff --git a/scripts/gameplay/crit_line.lua b/scripts/gameplay/crit_line.lua index 3f08b44..0541641 100644 --- a/scripts/gameplay/crit_line.lua +++ b/scripts/gameplay/crit_line.lua @@ -1,4 +1,6 @@ +local Dimensions = require 'common.dimensions' + local blackGradientImage = gfx.CreateSkinImage('gameplay/crit_line/black_gradient.png', 0) local baseImage = gfx.CreateSkinImage("gameplay/crit_line/base.png", 0) @@ -16,8 +18,8 @@ local cursorGlowTopImages = { gfx.CreateSkinImage("gameplay/crit_line/cursor_glow_top_right.png", 0), } -local CRITBAR_W = 1496 -local CRITBAR_H = 348 +local CRITBAR_W = 1080 +local CRITBAR_H = 251 local scale = 1; local isLandscape = false; @@ -82,7 +84,7 @@ local drawCursors = function (centerX, centerY,cursors) end local renderBase = function (deltaTime, centerX, centerY, rotation, cursors) - scale, isLandscape = setUpTransforms(centerX, centerY, rotation) + scale, isLandscape = Dimensions.setUpTransforms(centerX, centerY, rotation) gfx.BeginPath() gfx.FillColor(0, 0, 0, 192) @@ -90,8 +92,6 @@ local renderBase = function (deltaTime, centerX, centerY, rotation, cursors) gfx.Fill() if (isLandscape) then - --gfx.BeginPath() - --gfx.ImageRect(-9999, -8, 9999 * 2, 24, blackGradientImage, 192.0 / 255, 0) gfx.BeginPath(); gfx.ImageRect(-9999, -CRITBAR_H/2, 9999 * 2, CRITBAR_H, baseImageLandscape, 1, 0); else diff --git a/scripts/gameplay/hitfx.lua b/scripts/gameplay/hitfx.lua index 22876da..ec9cd1e 100644 --- a/scripts/gameplay/hitfx.lua +++ b/scripts/gameplay/hitfx.lua @@ -1,6 +1,8 @@ require 'common.globals' +local Dimensions = require 'common.dimensions' + local Animation = require 'api.animation' local Animations = { @@ -20,11 +22,6 @@ local animationStates = { Tap = { } }; -for i = 1, 6 do - --animationStates.Hold[i] = - --animationStates.Tap[i] = Animations -end - local HitFX = { }; local LanePositions = { @@ -40,7 +37,7 @@ local function setupLaneTransform(lanePosition, critCenterX, critCenterY) local critLine = gameplay.critLine; local x = critCenterX + (critLine.line.x2 - critLine.line.x1) * lanePosition; local y = critCenterY + (critLine.line.y2 - critLine.line.y1) * lanePosition; - setUpTransforms(x, y, -critLine.rotation); + Dimensions.setUpTransforms(x, y, -critLine.rotation); end function HitFX.render(deltaTime, critCenterX, critCenterY, critRotation, cursors) diff --git a/scripts/gameplay/song_panel.lua b/scripts/gameplay/song_panel.lua index 827beb0..7b95dd0 100644 --- a/scripts/gameplay/song_panel.lua +++ b/scripts/gameplay/song_panel.lua @@ -1,4 +1,5 @@ +local Charting = require('common.charting'); local DiffRectangle = require('components.diff_rectangle'); local desw = 1080; @@ -98,7 +99,7 @@ local render = function (deltaTime, bpm, laneSpeed, jacketPath, diff, level, pro ); -- Draw diff rectangle - local adjustedDiff = GetDisplayDifficulty(gameplay.jacketPath, diff) + local adjustedDiff = Charting.GetDisplayDifficulty(gameplay.jacketPath, diff) DiffRectangle.render(deltaTime, 31, y+140, 0.84, adjustedDiff, level); gfx.FontSize(30); diff --git a/scripts/result.lua b/scripts/result.lua index 970f747..6a7a389 100644 --- a/scripts/result.lua +++ b/scripts/result.lua @@ -1,4 +1,5 @@ local Easing = require('common.easing'); +local Charting = require('common.charting'); local Background = require('components.background'); local Footer = require('components.footer'); local Numbers = require('components.numbers') @@ -599,7 +600,7 @@ end local drawJacketPanelContent = function(deltaTime) gfx.BeginPath(); gfx.ImageRect(jacketPanelX + 13, jacketPanelY + 28, 265, 265, jacketImage or defaultJacketImage, 1, 0); - local adjustedDiff = GetDisplayDifficulty(result.jacketPath, result.difficulty) + local adjustedDiff = Charting.GetDisplayDifficulty(result.jacketPath, result.difficulty) DiffRectangle.render(deltaTime, jacketPanelX+183, jacketPanelY+2.5, 0.67, adjustedDiff, result.level); -- gfx.BeginPath(); diff --git a/scripts/songselect/chalwheel.lua b/scripts/songselect/chalwheel.lua index cfa50cb..f94d29e 100644 --- a/scripts/songselect/chalwheel.lua +++ b/scripts/songselect/chalwheel.lua @@ -1,4 +1,5 @@ local Common = require("common.util") +local Charting = require('common.charting') local Numbers = require('components.numbers') local DiffRectangle = require("components.diff_rectangle") local Header = require("components.headers.challengeSelectHeader") @@ -164,7 +165,7 @@ local check_or_create_cache = function(challenge) if not challengeCache[challenge.id]["percent_required"] then local percentRequired = 100 - local reqTextWords = Common.splitString(challenge.requirement_text, ' '); + local reqTextWords = Common.split(challenge.requirement_text, ' '); for _, word in ipairs(reqTextWords) do if string.find(word, '%%') ~= nil then -- %% = %, because % is an escape char local percentNumber = tonumber(string.gsub(word, '%%', ''), 10) @@ -405,7 +406,7 @@ draw_challenge = function(challenge, x, y, w, h, selected) for i, chart in ipairs(challengeCache[challenge.id]["charts"]) do local ypos = offsetY + paddingY * (i - 1) - local adjustedDiff = GetDisplayDifficulty(chart.jacketPath, chart.difficulty) + local adjustedDiff = Charting.GetDisplayDifficulty(chart.jacketPath, chart.difficulty) DiffRectangle.render(timer, offsetX, ypos, diffIconScale, adjustedDiff, chart.level) local _, titleHeight = gfx.LabelSize(chart.title) diff --git a/scripts/songselect/filterwheel.lua b/scripts/songselect/filterwheel.lua index d4c84c6..088f37f 100644 --- a/scripts/songselect/filterwheel.lua +++ b/scripts/songselect/filterwheel.lua @@ -1,4 +1,3 @@ -require('common') local Easing = require('common.easing') local Dim = require("common.dimensions") local SongSelectHeader = require('components.headers.songSelectHeader') diff --git a/scripts/songselect/songwheel.lua b/scripts/songselect/songwheel.lua index ba6ddb7..68e3371 100644 --- a/scripts/songselect/songwheel.lua +++ b/scripts/songselect/songwheel.lua @@ -1,4 +1,4 @@ -require('common') +local Charting = require('common.charting') local Easing = require('common.easing') local Background = require('components.background') local Dim = require("common.dimensions") @@ -328,7 +328,7 @@ function drawSong(song, y) -- Draw the difficulty notch background gfx.BeginPath() - local diffIndex = GetDisplayDifficulty(selectedSongDifficulty.jacketPath, selectedSongDifficulty.difficulty) + local diffIndex = Charting.GetDisplayDifficulty(selectedSongDifficulty.jacketPath, selectedSongDifficulty.difficulty) gfx.ImageRect(songX, y+95, 83, 74, difficultyLabelImages[diffIndex], 1, 0) -- Draw the difficulty level number @@ -498,7 +498,7 @@ function drawData() -- Draws the song data on the left panel Numbers.draw_number(85+(index-1)*DIFF_GAP, 1085, 1.0, diff.level, 2, difficultyNumbers, false, 0.8, 1) local diffLabelImage = difficultyLabelUnderImages[ - GetDisplayDifficulty(diff.jacketPath, diff.difficulty) + Charting.GetDisplayDifficulty(diff.jacketPath, diff.difficulty) ] local tw, th = gfx.ImageSize(diffLabelImage) tw=tw*0.9 @@ -786,7 +786,7 @@ function drawScrollbar() gfx.TextAlign(gfx.TEXT_ALIGN_MIDDLE + gfx.TEXT_ALIGN_CENTER) if (songwheel.songs[selectedIndex] ~= nil) then local title = songwheel.songs[selectedIndex].title; - local letter = string.upper(firstAlphaNum(title)) + local letter = string.upper(common.firstAlphaNum(title)) gfx.Text(letter, fillXPos-10, scrollbarYPos + 5) end end diff --git a/scripts/songselect/sortwheel.lua b/scripts/songselect/sortwheel.lua index 2d2a600..f76dc7c 100644 --- a/scripts/songselect/sortwheel.lua +++ b/scripts/songselect/sortwheel.lua @@ -1,4 +1,3 @@ -require('common') local Easing = require('common.easing'); local resx, resy = game.GetResolution() diff --git a/scripts/titlescreen/modeselect.lua b/scripts/titlescreen/modeselect.lua index 516527f..2036c5c 100644 --- a/scripts/titlescreen/modeselect.lua +++ b/scripts/titlescreen/modeselect.lua @@ -1,4 +1,4 @@ -require("common") + local Footer = require("components.footer") local Wallpaper = require("components.wallpaper") local Background = require("components.background")