Skip to content

Commit

Permalink
extract rename profile dialogue function to util scripts
Browse files Browse the repository at this point in the history
triple copy pasta too much
  • Loading branch information
poco0317 committed Oct 23, 2021
1 parent c4e453b commit 3b9d0b1
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -571,48 +571,6 @@ local function createList()
local profile = GetPlayerOrMachineProfile(PLAYER_1)
local pname = profile:GetDisplayName()

-- convenience to control the rename profile dialogue logic and input redir scope
local function renameProfileDialogue(profile)
local redir = SCREENMAN:get_input_redirected(PLAYER_1)
local function off()
if redir then
SCREENMAN:set_input_redirected(PLAYER_1, false)
end
end
local function on()
if redir then
SCREENMAN:set_input_redirected(PLAYER_1, true)
end
end
off()

local function f(answer)
profile:RenameProfile(answer)
MESSAGEMAN:Broadcast("ProfileRenamed")
on()
end
local question = "RENAME PROFILE\nPlease enter a new profile name."
askForInputStringWithFunction(
question,
255,
false,
f,
function(answer)
local result = answer ~= nil and answer:gsub("^%s*(.-)%s*$", "%1") ~= "" and not answer:match("::") and answer:gsub("^%s*(.-)%s*$", "%1"):sub(-1) ~= ":"
if not result then
SCREENMAN:GetTopScreen():GetChild("Question"):settext(question .. "\nDo not leave this space blank. Do not use ':'\nTo exit, press Esc.")
end
return result, "Response invalid."
end,
function()
-- upon exit, do nothing
-- profile name is unchanged
MESSAGEMAN:Broadcast("ProfileRenamed")
on()
end
)
end

-- list of skillsets mapped to number of plays
-- sorted immediately after being emplaced here
local playsbyskillset = SCOREMAN:GetPlaycountPerSkillset(profile)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,50 +118,6 @@ local function deleteProfileDialogue(id)
)
end

-- convenience to control the rename profile dialogue logic and input redir scope
-- difference between this function and the other RenameProfile dialogue way down below is this can be repeated
-- the one way down below cannot be repeated in all contexts because input redir may or may not be on in some situations
local function renameProfileDialogue(profile, listframe)
local redir = SCREENMAN:get_input_redirected(PLAYER_1)
local function off()
if redir then
SCREENMAN:set_input_redirected(PLAYER_1, false)
end
end
local function on()
if redir then
SCREENMAN:set_input_redirected(PLAYER_1, true)
end
end
off()

local function f(answer)
profile:RenameProfile(answer)
listframe:playcommand("UpdateProfiles")
on()
end
local question = "RENAME PROFILE\nPlease enter a new profile name."
askForInputStringWithFunction(
question,
64,
false,
f,
function(answer)
local result = answer ~= nil and answer:gsub("^%s*(.-)%s*$", "%1") ~= "" and not answer:match("::") and answer:gsub("^%s*(.-)%s*$", "%1"):sub(-1) ~= ":"
if not result then
SCREENMAN:GetTopScreen():GetChild("Question"):settext(question .. "\nDo not leave this space blank. Do not use ':'\nTo exit, press Esc.")
end
return result, "Response invalid."
end,
function()
-- upon exit, do nothing
-- profile name is unchanged
listframe:playcommand("UpdateProfiles")
on()
end
)
end

local function generateItems()
-- add 1 to number of profiles so we can have a button to add profiles always as the last item
local maxPage = math.ceil((#profileIDs) / numItems)
Expand Down Expand Up @@ -248,7 +204,7 @@ local function generateItems()
BeginCommand = function(self)
self:playcommand("Set")
end,
UpdateProfilesCommand = function(self)
ProfileRenamedMessageCommand = function(self)
self:playcommand("Set")
end,
MovedPageMessageCommand = function(self)
Expand Down Expand Up @@ -530,7 +486,7 @@ local function generateItems()
local profile = PROFILEMAN:GetLocalProfile(profileIDs[1])
local function f(answer)
profile:RenameProfile(answer)
self:playcommand("UpdateProfiles")
self:playcommand("ProfileRenamed")
end
local question = "No Profiles detected! A new one was made for you.\nPlease enter a new profile name."
askForInputStringWithFunction(
Expand Down
43 changes: 0 additions & 43 deletions Themes/Rebirth/BGAnimations/playerInfoFrame/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -257,49 +257,6 @@ local function loginStep2()
)
end

-- convenience to control the rename profile dialogue logic and input redir scope
local function renameProfileDialogue(profile)
local redir = SCREENMAN:get_input_redirected(PLAYER_1)
local function off()
if redir then
SCREENMAN:set_input_redirected(PLAYER_1, false)
end
end
local function on()
if redir then
SCREENMAN:set_input_redirected(PLAYER_1, true)
end
end
off()

local function f(answer)
profile:RenameProfile(answer)
MESSAGEMAN:Broadcast("ProfileRenamed")
on()
end
local question = "RENAME PROFILE\nPlease enter a new profile name."
askForInputStringWithFunction(
question,
255,
false,
f,
function(answer)
local result = answer ~= nil and answer:gsub("^%s*(.-)%s*$", "%1") ~= "" and not answer:match("::") and answer:gsub("^%s*(.-)%s*$", "%1"):sub(-1) ~= ":"
if not result then
SCREENMAN:GetTopScreen():GetChild("Question"):settext(question .. "\nDo not leave this space blank. Do not use ':'\nTo exit, press Esc.")
end
return result, "Response invalid."
end,
function()
-- upon exit, do nothing
-- profile name is unchanged
MESSAGEMAN:Broadcast("ProfileRenamed")
on()
end
)
end



t[#t+1] = Def.Quad {
Name = "BG",
Expand Down
42 changes: 42 additions & 0 deletions Themes/Rebirth/Scripts/98 util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -407,3 +407,45 @@ function updateNowPlaying()

File.Write("nowplaying.txt", fout)
end

-- convenience to control the rename profile dialogue logic and input redir scope
function renameProfileDialogue(profile)
local redir = SCREENMAN:get_input_redirected(PLAYER_1)
local function off()
if redir then
SCREENMAN:set_input_redirected(PLAYER_1, false)
end
end
local function on()
if redir then
SCREENMAN:set_input_redirected(PLAYER_1, true)
end
end
off()

local function f(answer)
profile:RenameProfile(answer)
MESSAGEMAN:Broadcast("ProfileRenamed")
on()
end
local question = "RENAME PROFILE\nPlease enter a new profile name."
askForInputStringWithFunction(
question,
255,
false,
f,
function(answer)
local result = answer ~= nil and answer:gsub("^%s*(.-)%s*$", "%1") ~= "" and not answer:match("::") and answer:gsub("^%s*(.-)%s*$", "%1"):sub(-1) ~= ":"
if not result then
SCREENMAN:GetTopScreen():GetChild("Question"):settext(question .. "\nDo not leave this space blank. Do not use ':'\nTo exit, press Esc.")
end
return result, "Response invalid."
end,
function()
-- upon exit, do nothing
-- profile name is unchanged
MESSAGEMAN:Broadcast("ProfileRenamed")
on()
end
)
end

0 comments on commit 3b9d0b1

Please sign in to comment.