Skip to content

Commit

Permalink
simplify/fix code for NoteSkin preview
Browse files Browse the repository at this point in the history
My old code for displaying and updating the NoteSkin preview in ScreenPlayerOptions was pretty convoluted, involving lots of parent and child lookups.  Making matters worse, I found edge cases in which it broke down/didn't work.

This commit simplifies the entire scheme by broadcasting the currently-selected NoteSkin from the SaveSelections() function in the NoteSkin CustomOptionRow definition and listening for that broadcast from OptionRow Frame.lua.
  • Loading branch information
quietly-turning committed Nov 4, 2017
1 parent 002a402 commit 63d3ec2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 30 deletions.
14 changes: 1 addition & 13 deletions BGAnimations/ScreenPlayerOptions overlay.lua
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ local Players = GAMESTATE:GetHumanPlayers()

-- SpeedModItems is a table that will contain the BitmapText actors for the SpeedMod OptionRow for both P1 and P2
local SpeedModItems = {}
local NoteSkinProxies = {}

local t = Def.ActorFrame{
InitCommand=cmd(xy,_screen.cx,0),
Expand All @@ -99,12 +98,6 @@ local t = Def.ActorFrame{

for player in ivalues( GAMESTATE:GetHumanPlayers() ) do
local pn = ToEnumShortString(player)
local noteskin_row_index = FindOptionRowIndex(ScreenOptions, "NoteSkin")

if noteskin_row_index then
NoteSkinProxies[pn] = ScreenOptions:GetOptionRow(noteskin_row_index):GetChild(""):GetChild("Frame"):GetChild("OptionRowProxy"..pn)
end

-- The BitmapText actors for P1 and P2 speedmod are both named "Item", so we need to provide a 1 or 2 to index
SpeedModItems[pn] = ScreenOptions:GetOptionRow(FindOptionRowIndex(ScreenOptions,"SpeedMod")):GetChild(""):GetChild("Item")[ PlayerNumber:Reverse()[player]+1 ]
self:playcommand("Set"..pn)
Expand Down Expand Up @@ -198,20 +191,15 @@ for player in ivalues(Players) do
if row_index == FindOptionRowIndex(SCREENMAN:GetTopScreen(), "SpeedMod") then
ChangeSpeedMod( pn, -1 )
self:queuecommand("Set"..pn)

elseif row_index == FindOptionRowIndex(SCREENMAN:GetTopScreen(), "NoteSkin") then
NoteSkinProxies[pn]:queuecommand("Update")
end
end,
["MenuRight" .. pn .. "MessageCommand"]=function(self)
local topscreen = SCREENMAN:GetTopScreen()
local row_index = topscreen:GetCurrentRowIndex(player)

if row_index == FindOptionRowIndex(SCREENMAN:GetTopScreen(), "SpeedMod") then
ChangeSpeedMod( pn, 1 )
self:queuecommand("Set"..pn)

elseif row_index == FindOptionRowIndex(SCREENMAN:GetTopScreen(), "NoteSkin") then
NoteSkinProxies[pn]:queuecommand("Update")
end
end
}
Expand Down
26 changes: 9 additions & 17 deletions Graphics/OptionRow Frame.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ local t = Def.ActorFrame{}

-- a row
t[#t+1] = Def.Quad {
Name="RowBackgroundQuad",
OnCommand=cmd(zoomto,_screen.w*0.85,_screen.h*0.0625;);
}

-- black quad behind the title
t[#t+1] = Def.Quad {
Name="TitleBackgroundQuad",
OnCommand=cmd(halign, 0; x, -_screen.cx/1.1775; zoomto,_screen.w*WideScale(0.18,0.15),_screen.h*0.0625; diffuse, Color.Black; diffusealpha,0.25);
}

Expand All @@ -23,37 +25,27 @@ for player in ivalues( GAMESTATE:GetHumanPlayers() ) do

t[#t+1] = Def.ActorProxy{
Name="OptionRowProxy" ..pn,
BeginCommand=function(self)
OnCommand=function(self)
local optrow = self:GetParent():GetParent():GetParent()

if optrow:GetName() == "NoteSkin" then
-- if this OptionRow is NoteSkin, set the necessary parameters and queuecommand("Update")
-- to actually SetTarget() to the appropriate NoteSkin actor
self:x(-_screen.cx/1.1775 + _screen.w*WideScale(0.18,0.15) - (player==PLAYER_1 and 45 or 15))
:zoom(0.4)
:queuecommand("Update")
:diffusealpha(0):sleep(0.01):diffusealpha(1)
else
-- if this OptionRow isn't NoteSkin, this ActorProxy isn't needed
-- and can be cut out of the render pipeline
self:hibernate(math.huge)
end
end,
-- UpdateCommand() gets queued from ScreenPlayerOptions overlay.lua
-- when MenuLeft or MenuRight is pressed while the NoteSkin OptionRow is active
UpdateCommand=function(self)
local bmt
-- Typically, there are multiple NoteSkins to choose from, resulting in there being one BitmapText actor per player,
-- and one (one player joined) or both (two players joined) will be nested in an indexed table.
--
-- If there is only a single NoteSkin available, however, the engine will only draw one BitmapText actor ("Item")
-- and it won't be nested in a table, So, check how many "Item" actors there are before attempting to index
-- a table that might not exist.
if #self:GetParent():GetParent():GetChild("Item") > 0 then
bmt = self:GetParent():GetParent():GetChild("Item")[ PlayerNumber:Reverse()[player]+1 ]
else
bmt = self:GetParent():GetParent():GetChild("Item")
-- NoteSkinChanged is broadcast by the SaveSelections() function for the NoteSkin OptionRow definition
-- in ./Scripts/SL-PlayerOptions.lua
NoteSkinChangedMessageCommand=function(self, params)
if player == params.Player then
self:SetTarget( SCREENMAN:GetTopScreen():GetChild("Overlay"):GetChild("NoteSkin_"..params.NoteSkin) )
end
self:SetTarget( SCREENMAN:GetTopScreen():GetChild("Overlay"):GetChild("NoteSkin_"..bmt:GetText()) )
end
}
end
Expand Down
3 changes: 3 additions & 0 deletions Scripts/SL-PlayerOptions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ local Overrides = {
},
-------------------------------------------------------------------------
NoteSkin = {
ExportOnChange = true,
Choices = function()

local all = NOTESKIN:GetNoteSkinNames()
Expand Down Expand Up @@ -94,9 +95,11 @@ local Overrides = {
for i=1,#list do
if list[i] then
mods.NoteSkin = self.Choices[i]
break
end
end

MESSAGEMAN:Broadcast('NoteSkinChanged', {Player=pn, NoteSkin=mods.NoteSkin})
playeroptions:NoteSkin( mods.NoteSkin )
end
},
Expand Down

0 comments on commit 63d3ec2

Please sign in to comment.