chalwheel: optimization: removed sub function instantiations

This commit is contained in:
Hersi 2021-11-22 02:22:05 +01:00
parent 02578ce121
commit 67f6f70f7c
1 changed files with 132 additions and 134 deletions

View File

@ -215,157 +215,155 @@ draw_challenge = function(challenge, x, y, w, h, selected)
check_or_create_cache(challenge) check_or_create_cache(challenge)
local _draw_card_bg = function() ----------------------------------------------------------
-- draw card bg section
----------------------------------------------------------
if not selected then
gfx.BeginPath() gfx.BeginPath()
gfx.ImageRect(x, y, w, h, challengeCardBGImage, 1, 0) gfx.ImageRect(x, y, w, h, challengeCardBGImage, 1, 0)
end end
local _draw_info = function() ----------------------------------------------------------
local stateLabel = challengeCache[challenge.id]["pass_state"] -- draw info section
local stateLabelWidth, stateLabelHeight = gfx.ImageSize(stateLabel) ----------------------------------------------------------
local stateLabelAspect = stateLabelWidth / stateLabelHeight local stateLabel = challengeCache[challenge.id]["pass_state"]
local stateLabelWidth, stateLabelHeight = gfx.ImageSize(stateLabel)
local stateLabelAspect = stateLabelWidth / stateLabelHeight
local stateWidth = w / 5 local stateWidth = w / 5
local stateHeight = stateWidth / stateLabelAspect local stateHeight = stateWidth / stateLabelAspect
local stateOffsetX = x + w / 32 local stateOffsetX = x + w / 32
local stateOffsetY = y + h / 32 local stateOffsetY = y + h / 32
local titleMargin = 6 local titleMargin = 6
local titleFontSize = math.floor(0.11 * h) -- must be an integer local titleFontSize = math.floor(0.11 * h) -- must be an integer
local titleMaxWidth = 6 / 9 * w local titleMaxWidth = 6 / 9 * w
local titleCenterX = x + w - titleMargin - titleMaxWidth / 2 -- align right local titleCenterX = x + w - titleMargin - titleMaxWidth / 2 -- align right
local titleBaselineY = y + 0.025 * h -- align baseline local titleBaselineY = y + 0.025 * h -- align baseline
if selected then if selected then
stateWidth = stateWidth / 0.9 stateWidth = stateWidth / 0.9
stateHeight = stateHeight / 0.9 stateHeight = stateHeight / 0.9
stateOffsetY = y + h / 16 stateOffsetY = y + h / 16
titleFontSize = math.floor(0.075 * h) -- must be an integer titleFontSize = math.floor(0.075 * h) -- must be an integer
titleMaxWidth = 3 / 5 * w titleMaxWidth = 3 / 5 * w
titleCenterX = x + w - titleMargin - titleMaxWidth / 2 -- align right titleCenterX = x + w - titleMargin - titleMaxWidth / 2 -- align right
titleBaselineY = y + 0.09 * h -- align baseline titleBaselineY = y + 0.09 * h -- align baseline
end
update_cache_labels(challenge, titleFontSize)
gfx.BeginPath()
gfx.ImageRect(stateOffsetX, stateOffsetY, stateWidth, stateHeight, stateLabel, 1, 0)
gfx.FontFace("divlit_custom.ttf")
gfx.TextAlign(gfx.TEXT_ALIGN_CENTER | gfx.TEXT_ALIGN_BASELINE)
gfx.DrawLabel(challengeCache[challenge.id]["title"], titleCenterX, titleBaselineY, titleMaxWidth)
end end
local _draw_jacket = function() update_cache_labels(challenge, titleFontSize)
local size = h * 0.68
local offsetX = x + w / 32
local offsetY = y + h - size - h * 0.05
if not selected then gfx.BeginPath()
size = h * 0.66 gfx.ImageRect(stateOffsetX, stateOffsetY, stateWidth, stateHeight, stateLabel, 1, 0)
offsetX = x + w * 0.058
offsetY = y + h - size - h * 0.066
end
gfx.BeginPath() gfx.FontFace("divlit_custom.ttf")
gfx.ImageRect(offsetX, offsetY, size, size, challengeCache[challenge.id]["jackets"][1], 1, 0) gfx.TextAlign(gfx.TEXT_ALIGN_CENTER | gfx.TEXT_ALIGN_BASELINE)
end gfx.DrawLabel(challengeCache[challenge.id]["title"], titleCenterX, titleBaselineY, titleMaxWidth)
local _draw_stats = function() ----------------------------------------------------------
local textSizeCorrection = h / gfx.ImageSize(scoreNumber[1]) -- draw jacket section
----------------------------------------------------------
local percentOffsetX = x + 6 / 12 * w local size = h * 0.68
local percentOffsetY = y + 0.87 * h local offsetX = x + w / 32
local percentSize = 0.17 * textSizeCorrection local offsetY = y + h - size - h * 0.05
local scoreUpperOffsetX = 0
local scoreUpperOffsetY = 0
local scoreOffsetX = x + w * 0.74
local scoreOffsetY = y + h * 0.9
local scoreUpperSize = 0.2 * textSizeCorrection
local scoreSize = 0.125 * textSizeCorrection
local percent = challengeCache[challenge.id]["percent"]
local scoreUpper = math.floor(challengeCache[challenge.id]["total_score"] / 10000)
local score = challengeCache[challenge.id]["total_score"]
if selected then
percentOffsetX = x + 11 / 24 * w
percentOffsetY = y + 49 / 64 * h
percentSize = 0.12 * textSizeCorrection
scoreUpperOffsetX = x + w * 0.63
scoreUpperOffsetY = y + h * 0.82
scoreOffsetX = x + w * 0.755
scoreOffsetY = y + h * 0.835
scoreUpperSize = 0.12 * textSizeCorrection
scoreSize = 0.09 * textSizeCorrection
end
Numbers.draw_number(percentOffsetX, percentOffsetY, 1, percent, 3, scoreNumber, true, percentSize, 1)
-- TODO: Missing percentage character
if selected then
Numbers.draw_number(
scoreUpperOffsetX, scoreUpperOffsetY, 1, scoreUpper, 4, scoreNumber, true, scoreUpperSize, 1
)
Numbers.draw_number(scoreOffsetX, scoreOffsetY, 1, score, 4, scoreNumber, true, scoreSize, 1)
else
Numbers.draw_number(scoreOffsetX, scoreOffsetY, 1, score, 8, scoreNumber, true, scoreSize, 1)
end
-- TODO: Missing completion bar
-- TODO: Missing completion badge (COMP, HARD, UC, PUC)
-- TODO: Missing completion grade (S+, S, AAA+, etc.)
end
local _draw_charts = function()
local diffIconHeight = 0.124 * h
local diffIconWidth = 140 / 31 * diffIconHeight -- only scaling, no stretching textures
-- diffIconWidth = 0.8 * diffIconWidth -- okay, maybe a little squishing must be done
local paddingY = 0.18 * h
local offsetX = x + 0.252 * w
local offsetY = y + 0.246 * h
local titleMargin = 6
local titleMaxWidth = 6 / 9 * w
local titleCenterX = x + w - titleMargin - titleMaxWidth / 2 -- align right
if selected then
diffIconHeight = h / 12
diffIconWidth = 140 / 31 * diffIconHeight -- only scaling, no stretching textures
-- diffIconWidth = 0.8 * diffIconWidth -- okay, maybe a little squishing must be done
paddingY = 0.122 * h
offsetX = x + 0.268 * w
offsetY = y + 0.256 * h
titleFontSize = math.floor(0.075 * h) -- must be an integer
titleMaxWidth = 3 / 5 * w
titleCenterX = x + w - titleMargin - titleMaxWidth / 2 -- align right
end
for i, chart in ipairs(challengeCache[challenge.id]["charts"]) do
local ypos = offsetY + paddingY * (i - 1)
draw_diff_icon(chart, offsetX, ypos, diffIconWidth, diffIconHeight, selected)
gfx.TextAlign(gfx.TEXT_ALIGN_CENTER | gfx.TEXT_ALIGN_MIDDLE)
gfx.DrawLabel(chart.title, titleCenterX, ypos + diffIconHeight / 2, titleMaxWidth)
end
end
if not selected then if not selected then
_draw_card_bg() size = h * 0.66
offsetX = x + w * 0.058
offsetY = y + h - size - h * 0.066
end end
_draw_info() gfx.BeginPath()
_draw_jacket() gfx.ImageRect(offsetX, offsetY, size, size, challengeCache[challenge.id]["jackets"][1], 1, 0)
_draw_stats()
_draw_charts() ----------------------------------------------------------
-- draw stats section
----------------------------------------------------------
local textSizeCorrection = h / gfx.ImageSize(scoreNumber[1])
local percentOffsetX = x + 6 / 12 * w
local percentOffsetY = y + 0.87 * h
local percentSize = 0.17 * textSizeCorrection
local scoreUpperOffsetX = 0
local scoreUpperOffsetY = 0
local scoreOffsetX = x + w * 0.74
local scoreOffsetY = y + h * 0.9
local scoreUpperSize = 0.2 * textSizeCorrection
local scoreSize = 0.125 * textSizeCorrection
local percent = challengeCache[challenge.id]["percent"]
local scoreUpper = math.floor(challengeCache[challenge.id]["total_score"] / 10000)
local score = challengeCache[challenge.id]["total_score"]
if selected then
percentOffsetX = x + 11 / 24 * w
percentOffsetY = y + 49 / 64 * h
percentSize = 0.12 * textSizeCorrection
scoreUpperOffsetX = x + w * 0.63
scoreUpperOffsetY = y + h * 0.82
scoreOffsetX = x + w * 0.755
scoreOffsetY = y + h * 0.835
scoreUpperSize = 0.12 * textSizeCorrection
scoreSize = 0.09 * textSizeCorrection
end
Numbers.draw_number(percentOffsetX, percentOffsetY, 1, percent, 3, scoreNumber, true, percentSize, 1)
-- TODO: Missing percentage character
if selected then
Numbers.draw_number(
scoreUpperOffsetX, scoreUpperOffsetY, 1, scoreUpper, 4, scoreNumber, true, scoreUpperSize, 1
)
Numbers.draw_number(scoreOffsetX, scoreOffsetY, 1, score, 4, scoreNumber, true, scoreSize, 1)
else
Numbers.draw_number(scoreOffsetX, scoreOffsetY, 1, score, 8, scoreNumber, true, scoreSize, 1)
end
-- TODO: Missing completion bar
-- TODO: Missing completion badge (COMP, HARD, UC, PUC)
-- TODO: Missing completion grade (S+, S, AAA+, etc.)
----------------------------------------------------------
-- draw charts section
----------------------------------------------------------
local diffIconHeight = 0.124 * h
local diffIconWidth = 140 / 31 * diffIconHeight -- only scaling, no stretching textures
-- diffIconWidth = 0.8 * diffIconWidth -- okay, maybe a little squishing must be done
local paddingY = 0.18 * h
local offsetX = x + 0.252 * w
local offsetY = y + 0.246 * h
local titleMargin = 6
local titleMaxWidth = 6 / 9 * w
local titleCenterX = x + w - titleMargin - titleMaxWidth / 2 -- align right
if selected then
diffIconHeight = h / 12
diffIconWidth = 140 / 31 * diffIconHeight -- only scaling, no stretching textures
-- diffIconWidth = 0.8 * diffIconWidth -- okay, maybe a little squishing must be done
paddingY = 0.122 * h
offsetX = x + 0.268 * w
offsetY = y + 0.256 * h
titleFontSize = math.floor(0.075 * h) -- must be an integer
titleMaxWidth = 3 / 5 * w
titleCenterX = x + w - titleMargin - titleMaxWidth / 2 -- align right
end
for i, chart in ipairs(challengeCache[challenge.id]["charts"]) do
local ypos = offsetY + paddingY * (i - 1)
draw_diff_icon(chart, offsetX, ypos, diffIconWidth, diffIconHeight, selected)
gfx.TextAlign(gfx.TEXT_ALIGN_CENTER | gfx.TEXT_ALIGN_MIDDLE)
gfx.DrawLabel(chart.title, titleCenterX, ypos + diffIconHeight / 2, titleMaxWidth)
end
gfx.ForceRender() gfx.ForceRender()