From 21125dcf6870493ada708ad56f0025fe77bd8f94 Mon Sep 17 00:00:00 2001 From: Hersi Date: Fri, 10 Dec 2021 03:41:44 +0100 Subject: [PATCH] Added percent sign and percentage bar Moved implementation of splitString into common.common Removed redundant function definitions in challengeresult.lua Added a new parameter to the draw_number function in common.numbers --- scripts/challengeresult.lua | 51 ++++---------------------- scripts/common/common.lua | 16 +++++++-- scripts/common/numbers.lua | 7 ++-- scripts/songselect/chalwheel.lua | 61 +++++++++++++++++++++++++++++--- 4 files changed, 80 insertions(+), 55 deletions(-) diff --git a/scripts/challengeresult.lua b/scripts/challengeresult.lua index 3189ccd..9c7399c 100644 --- a/scripts/challengeresult.lua +++ b/scripts/challengeresult.lua @@ -2,6 +2,7 @@ local Easing = require("common.easings"); local Footer = require("components.footer"); local DiffRectangle = require('components.diff_rectangle'); local common = require('common.common'); +local Numbers = require('common.numbers') local VolforceWindow = require("components.volforceWindow") @@ -100,46 +101,6 @@ local function handleSfx() end end -function splitString(inputstr, sep) - if sep == nil then - sep = "%s" - end - local t={} - for str in string.gmatch(inputstr, "([^"..sep.."]+)") do - table.insert(t, str) - end - return t -end - -local function load_number_image(path) - local images = {} - for i = 0, 9 do - images[i + 1] = gfx.CreateSkinImage(string.format("%s/%d.png", path, i), 0) - end - return images -end - -local function draw_number(x, y, alpha, num, digits, images, is_dim, scale, kern) - scale = scale or 1; - kern = kern or 1; - local tw, th = gfx.ImageSize(images[1]) - tw = tw * scale; - th = th * scale; - x = x + (tw * (digits - 1)) / 2 - y = y - th / 2 - for i = 1, digits do - local mul = 10 ^ (i - 1) - local digit = math.floor(num / mul) % 10 - local a = alpha - if is_dim and num < mul then - a = 0.4 - end - gfx.BeginPath() - gfx.ImageRect(x, y, tw, th, images[digit + 1], a, 0) - x = x - (tw * kern) - end -end - function drawBackground() gfx.BeginPath() gfx.ImageRect(0, 0, desw, desh, resultBgImage, 1, 0); @@ -187,7 +148,7 @@ function drawPlayerInfo() gfx.ImageRect(311, 490, 107 * 1.25, 29 * 1.25, danBadgeImage, 1, 0); end -local scoreNumber = load_number_image("score_num"); +local scoreNumber = Numbers.load_number_image("score_num"); function drawChartResult(deltaTime, x, y, chartResult) gfx.Save() @@ -203,8 +164,8 @@ function drawChartResult(deltaTime, x, y, chartResult) local score = chartResult.score or 0; - draw_number(x + 500, y+80, 1.0, math.floor(score / 10000), 4, scoreNumber, true, 0.30, 1.12) - draw_number(x + 655, y+85, 1.0, score, 4, scoreNumber, true, 0.22, 1.12) + Numbers.draw_number(x + 500, y+80, 1.0, math.floor(score / 10000), 4, scoreNumber, true, 0.30, 1.12) + Numbers.draw_number(x + 655, y+85, 1.0, score, 4, scoreNumber, true, 0.22, 1.12) local gradeImageKey = string.gsub(chartResult.grade, '+', '_P'); @@ -253,7 +214,7 @@ function drawCompletion() gfx.BeginPath() gfx.ImageRect(63, 1331, 766*0.85, 130*0.85, completitionImage, 1, 0) - draw_number(925, 1370, 1.0, percGet, 3, scoreNumber, true, 0.3, 1.12) + Numbers.draw_number(925, 1370, 1.0, percGet, 3, scoreNumber, true, 0.3, 1.12) gfx.BeginPath(); @@ -264,7 +225,7 @@ end function result_set() - local reqTextWords = splitString(result.requirement_text, ' '); + local reqTextWords = common.splitString(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/common.lua b/scripts/common/common.lua index 91427a1..c29defb 100644 --- a/scripts/common/common.lua +++ b/scripts/common/common.lua @@ -1,5 +1,3 @@ - - local stopMusic = function () local musicPlaying = game.GetSkinSetting('_musicPlaying'); if musicPlaying and musicPlaying ~= '' then @@ -8,6 +6,18 @@ local stopMusic = function () end end +local function splitString(inputstr, sep) + if sep == nil then + sep = "%s" + end + local t={} + for str in string.gmatch(inputstr, "([^"..sep.."]+)") do + table.insert(t, str) + end + return t +end + return { - stopMusic = stopMusic + stopMusic = stopMusic, + splitString = splitString } \ No newline at end of file diff --git a/scripts/common/numbers.lua b/scripts/common/numbers.lua index fdd7c77..b7e4d4e 100644 --- a/scripts/common/numbers.lua +++ b/scripts/common/numbers.lua @@ -6,9 +6,10 @@ function load_number_image(path) return images end -function draw_number(x, y, alpha, num, digits, images, is_dim, scale, kern, dim_zeros) +function draw_number(x, y, alpha, num, digits, images, is_dim, scale, kern, dim_first_zero) scale = scale or 1 kern = kern or 1 + dim_first_zero = dim_first_zero == nil and true or dim_first_zero local tw, th = gfx.ImageSize(images[1]) tw = tw * scale @@ -21,7 +22,9 @@ function draw_number(x, y, alpha, num, digits, images, is_dim, scale, kern, dim_ local digit = math.floor(num / mul) % 10 local a = alpha - if is_dim and num < mul then a = 0.4 end + if is_dim and num < mul and not (not dim_first_zero and i <= 1) then + a = 0.4 + end gfx.BeginPath() gfx.ImageRect(x, y, tw, th, images[digit + 1], a, 0) diff --git a/scripts/songselect/chalwheel.lua b/scripts/songselect/chalwheel.lua index a77f63d..dba5995 100644 --- a/scripts/songselect/chalwheel.lua +++ b/scripts/songselect/chalwheel.lua @@ -1,5 +1,6 @@ local Numbers = require("common.numbers") local DiffRectangle = require("components.diff_rectangle") +local Common = require("common.common") local backgroundImage = gfx.CreateSkinImage("bg_pattern.png", gfx.IMAGE_REPEATX | gfx.IMAGE_REPEATY) local challengeBGImage = gfx.CreateSkinImage("challenge_select/bg.png", 0) @@ -74,6 +75,7 @@ local passStates = { } local scoreNumbers = Numbers.load_number_image("score_num") +local percentImage = gfx.CreateSkinImage("score_num/percent.png", 0) gfx.LoadSkinFont("dfmarugoth.ttf"); @@ -154,6 +156,19 @@ local check_or_create_cache = function(challenge) end end + if not challengeCache[challenge.id]["percent_required"] then + local percentRequired = 100 + local reqTextWords = Common.splitString(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) + percentRequired = percentNumber; + break + end + end + challengeCache[challenge.id]["percent_required"] = percentRequired + end + if (not challengeCache[challenge.id]["percent"] or not challengeCache[challenge.id]["total_score"] or challengeCache[challenge.id]["total_score"] ~= challenge.bestScore) then challengeCache[challenge.id]["percent"] = math.max(0, (challenge.bestScore - 8000000) // 10000) @@ -253,9 +268,19 @@ draw_challenge = function(challenge, x, y, w, h, selected) ---------------------------------------------------------- local textSizeCorrection = h / gfx.ImageSize(scoreNumbers[1]) - local percentOffsetX = x + 6 / 12 * w + local percentOffsetX = x + 0.5 * w local percentOffsetY = y + 0.87 * h local percentSize = 0.17 * textSizeCorrection + local percentImageWidth, percentImageHeight = gfx.ImageSize(percentImage) + local percentImageOffsetX = percentOffsetX + 0.08 * w + + local percentRequired = challengeCache[challenge.id]["percent_required"] + local percentBarOffsetX = x + 0.281 * w + local percentBarOffsetY = y + 0.856 * h + local percentBarWidth = 0.273 * w + local percentBarHeight = 0.02 * h + local percentBarLeftColor = {255, 0, 0} + local percentBarRightColor = {255, 128, 0} local scoreUpperOffsetX = 0 local scoreUpperOffsetY = 0 @@ -282,6 +307,7 @@ draw_challenge = function(challenge, x, y, w, h, selected) percentOffsetX = x + 11 / 24 * w percentOffsetY = y + 49 / 64 * h percentSize = 0.12 * textSizeCorrection + percentImageOffsetX = percentOffsetX + 0.074 * w scoreUpperOffsetX = x + w * 0.63 scoreUpperOffsetY = y + h * 0.82 @@ -299,10 +325,37 @@ draw_challenge = function(challenge, x, y, w, h, selected) gradeSize = 0.175 * h end - Numbers.draw_number(percentOffsetX, percentOffsetY, 1, percent, 3, scoreNumbers, true, percentSize, 1) - -- TODO: Missing percentage character + percentImageWidth = percentImageWidth * percentSize * 0.75 + percentImageHeight = percentImageHeight * percentSize * 0.75 + percentImageOffsetY = percentOffsetY - percentImageHeight * 0.25 + + -- Draw percentage + Numbers.draw_number(percentOffsetX, percentOffsetY, 1, percent, 3, scoreNumbers, true, percentSize, 1, false) + gfx.BeginPath() + gfx.ImageRect(percentImageOffsetX, percentImageOffsetY, percentImageWidth, percentImageHeight, percentImage, 1, 0) if selected then + -- Draw percentBar + gfx.BeginPath() + local paint = gfx.LinearGradient( + percentBarOffsetX, percentBarOffsetY, + percentBarOffsetX + percentBarWidth, percentBarOffsetY + ) + gfx.FillPaint(paint) + gfx.GradientColors( + percentBarLeftColor[1], percentBarLeftColor[2], percentBarLeftColor[3], 255, + percentBarRightColor[1], percentBarRightColor[2], percentBarRightColor[3], 255 + ) + gfx.Rect(percentBarOffsetX, percentBarOffsetY, percentBarWidth * math.min(1, percent / percentRequired), percentBarHeight) + gfx.Fill() + + -- Draw percentBar highlight + gfx.BeginPath() + gfx.FillColor(255, 255, 255, 64) + gfx.Rect(percentBarOffsetX, percentBarOffsetY, percentBarWidth * math.min(1, percent / 100), percentBarHeight * 0.5) + gfx.Fill() + + -- Draw score Numbers.draw_number( scoreUpperOffsetX, scoreUpperOffsetY, 1, scoreUpper, 4, scoreNumbers, true, scoreUpperSize, 1 ) @@ -311,8 +364,6 @@ draw_challenge = function(challenge, x, y, w, h, selected) Numbers.draw_number(scoreOffsetX, scoreOffsetY, 1, score, 8, scoreNumbers, true, scoreSize, 1) end - -- TODO: Missing completion bar - if badge then gfx.BeginPath() gfx.ImageRect(badgeOffsetX, badgeOffsetY, 93/81 * badgeSize, badgeSize, badge, 1, 0)