recording: Internalize VirtualPad Windows

* Hold virtualPad window pointers solely in the g_inputRecording object
* Only inits the windows once per session and only if recording options are enabled
This commit is contained in:
sonicfind 2020-12-15 19:02:40 -06:00 committed by refractionpcsx2
parent acb61a3cf2
commit a6b2d30bd0
7 changed files with 68 additions and 56 deletions

View File

@ -63,16 +63,26 @@ void SaveStateBase::InputRecordingFreeze()
InputRecording g_InputRecording; InputRecording g_InputRecording;
InputRecording::InputRecording() InputRecording::InputRecordingPad::InputRecordingPad()
{ {
// NOTE - No multi-tap support, only two controllers padData = new PadData;
padData[CONTROLLER_PORT_ONE] = new PadData();
padData[CONTROLLER_PORT_TWO] = new PadData();
} }
void InputRecording::setVirtualPadPtr(VirtualPad* ptr, int const port) InputRecording::InputRecordingPad::~InputRecordingPad()
{ {
virtualPads[port] = ptr; delete padData;
}
void InputRecording::InitVirtualPadWindows(wxWindow* parent)
{
for (int port = 0; port < 2; ++port)
if (!pads[port].virtualPad)
pads[port].virtualPad = new VirtualPad(parent, port, g_Conf->inputRecording);
}
void InputRecording::ShowVirtualPad(const int port)
{
pads[port].virtualPad->Show();
} }
void InputRecording::RecordingReset() void InputRecording::RecordingReset()
@ -114,16 +124,16 @@ void InputRecording::ControllerInterrupt(u8& data, u8& port, u16& bufCount, u8 b
inputRec::consoleLog(fmt::format("Failed to read input data at frame {}", frameCounter)); inputRec::consoleLog(fmt::format("Failed to read input data at frame {}", frameCounter));
// Update controller data state for future VirtualPad / logging usage. // Update controller data state for future VirtualPad / logging usage.
padData[port]->UpdateControllerData(bufIndex, bufVal); pads[port].padData->UpdateControllerData(bufIndex, bufVal);
if (virtualPads[port]->IsShown()) if (pads[port].virtualPad->IsShown())
virtualPads[port]->UpdateControllerData(bufIndex, padData[port]); pads[port].virtualPad->UpdateControllerData(bufIndex, pads[port].padData);
} }
} }
else else
{ {
// Update controller data state for future VirtualPad / logging usage. // Update controller data state for future VirtualPad / logging usage.
padData[port]->UpdateControllerData(bufIndex, bufVal); pads[port].padData->UpdateControllerData(bufIndex, bufVal);
// Commit the byte to the movie file if we are recording // Commit the byte to the movie file if we are recording
if (state == InputRecordingMode::Recording) if (state == InputRecordingMode::Recording)
@ -132,8 +142,8 @@ void InputRecording::ControllerInterrupt(u8& data, u8& port, u16& bufCount, u8 b
{ {
// If the VirtualPad updated the PadData, we have to update the buffer // If the VirtualPad updated the PadData, we have to update the buffer
// before committing it to the recording / sending it to the game // before committing it to the recording / sending it to the game
if (virtualPads[port]->IsShown() && virtualPads[port]->UpdateControllerData(bufIndex, padData[port])) if (pads[port].virtualPad->IsShown() && pads[port].virtualPad->UpdateControllerData(bufIndex, pads[port].padData))
bufVal = padData[port]->PollControllerData(bufIndex); bufVal = pads[port].padData->PollControllerData(bufIndex);
if (incrementUndo) if (incrementUndo)
{ {
@ -147,8 +157,8 @@ void InputRecording::ControllerInterrupt(u8& data, u8& port, u16& bufCount, u8 b
} }
// If the VirtualPad updated the PadData, we have to update the buffer // If the VirtualPad updated the PadData, we have to update the buffer
// before sending it to the game // before sending it to the game
else if (virtualPads[port]->IsShown() && virtualPads[port]->UpdateControllerData(bufIndex, padData[port])) else if (pads[port].virtualPad->IsShown() && pads[port].virtualPad->UpdateControllerData(bufIndex, pads[port].padData))
bufVal = padData[port]->PollControllerData(bufIndex); bufVal = pads[port].padData->PollControllerData(bufIndex);
} }
} }
} }
@ -190,11 +200,11 @@ void InputRecording::LogAndRedraw()
{ {
for (u8 port = 0; port < 2; port++) for (u8 port = 0; port < 2; port++)
{ {
padData[port]->LogPadData(port); pads[port].padData->LogPadData(port);
// As well as re-render the virtual pad UI, if applicable // As well as re-render the virtual pad UI, if applicable
// - Don't render if it's minimized // - Don't render if it's minimized
if (virtualPads[port]->IsShown() && !virtualPads[port]->IsIconized()) if (pads[port].virtualPad->IsShown() && !pads[port].virtualPad->IsIconized())
virtualPads[port]->Redraw(); pads[port].virtualPad->Redraw();
} }
} }
@ -239,16 +249,16 @@ wxString InputRecording::RecordingModeTitleSegment()
void InputRecording::SetToRecordMode() void InputRecording::SetToRecordMode()
{ {
state = InputRecordingMode::Recording; state = InputRecordingMode::Recording;
virtualPads[CONTROLLER_PORT_ONE]->SetReadOnlyMode(false); pads[CONTROLLER_PORT_ONE].virtualPad->SetReadOnlyMode(false);
virtualPads[CONTROLLER_PORT_TWO]->SetReadOnlyMode(false); pads[CONTROLLER_PORT_TWO].virtualPad->SetReadOnlyMode(false);
inputRec::log("Record mode ON"); inputRec::log("Record mode ON");
} }
void InputRecording::SetToReplayMode() void InputRecording::SetToReplayMode()
{ {
state = InputRecordingMode::Replaying; state = InputRecordingMode::Replaying;
virtualPads[CONTROLLER_PORT_ONE]->SetReadOnlyMode(true); pads[CONTROLLER_PORT_ONE].virtualPad->SetReadOnlyMode(true);
virtualPads[CONTROLLER_PORT_TWO]->SetReadOnlyMode(true); pads[CONTROLLER_PORT_TWO].virtualPad->SetReadOnlyMode(true);
inputRec::log("Replay mode ON"); inputRec::log("Replay mode ON");
} }
@ -327,25 +337,25 @@ void InputRecording::FailedSavestate()
void InputRecording::Stop() void InputRecording::Stop()
{ {
state = InputRecordingMode::NotActive; state = InputRecordingMode::NotActive;
virtualPads[CONTROLLER_PORT_ONE]->SetReadOnlyMode(false); pads[CONTROLLER_PORT_ONE].virtualPad->SetReadOnlyMode(false);
virtualPads[CONTROLLER_PORT_TWO]->SetReadOnlyMode(false); pads[CONTROLLER_PORT_TWO].virtualPad->SetReadOnlyMode(false);
incrementUndo = false; incrementUndo = false;
if (inputRecordingData.Close()) if (inputRecordingData.Close())
inputRec::log("Input recording stopped"); inputRec::log("Input recording stopped");
} }
bool InputRecording::Create(wxString FileName, bool fromSaveState, wxString authorName) bool InputRecording::Create(wxString fileName, const bool fromSaveState, wxString authorName)
{ {
if (!inputRecordingData.OpenNew(FileName, fromSaveState)) if (!inputRecordingData.OpenNew(fileName, fromSaveState))
return false; return false;
initialLoad = true; initialLoad = true;
state = InputRecordingMode::Recording; state = InputRecordingMode::Recording;
if (fromSaveState) if (fromSaveState)
{ {
if (wxFileExists(FileName + "_SaveState.p2s")) if (wxFileExists(fileName + "_SaveState.p2s"))
wxCopyFile(FileName + "_SaveState.p2s", FileName + "_SaveState.p2s.bak", true); wxCopyFile(fileName + "_SaveState.p2s", fileName + "_SaveState.p2s.bak", true);
StateCopy_SaveToFile(FileName + "_SaveState.p2s"); StateCopy_SaveToFile(fileName + "_SaveState.p2s");
} }
else else
sApp.SysExecute(g_Conf->CdvdSource); sApp.SysExecute(g_Conf->CdvdSource);

View File

@ -18,12 +18,13 @@
#ifndef DISABLE_RECORDING #ifndef DISABLE_RECORDING
#include "Recording/InputRecordingFile.h" #include "Recording/InputRecordingFile.h"
#include "Recording/VirtualPad/VirtualPad.h"
class InputRecording class InputRecording
{ {
public: public:
InputRecording(); // Initializes all VirtualPad windows with "parent" as their base
void InitVirtualPadWindows(wxWindow* parent);
// Save or load PCSX2's global frame counter (g_FrameCount) along with each full/fast boot // Save or load PCSX2's global frame counter (g_FrameCount) along with each full/fast boot
// //
@ -82,13 +83,13 @@ public:
/// Functions called from GUI /// Functions called from GUI
// Create a new input recording file // Create a new input recording file
bool Create(wxString filename, bool fromSaveState, wxString authorName); bool Create(wxString filename, const bool fromSaveState, wxString authorName);
// Play an existing input recording from a file // Play an existing input recording from a file
bool Play(wxString filename); bool Play(wxString filename);
// Stop the active input recording // Stop the active input recording
void Stop(); void Stop();
// Initialze VirtualPad window // Displays the VirtualPad window for the chosen pad
void setVirtualPadPtr(VirtualPad* ptr, int const port); void ShowVirtualPad(const int port);
// Logs the padData and redraws the virtualPad windows of active pads // Logs the padData and redraws the virtualPad windows of active pads
void LogAndRedraw(); void LogAndRedraw();
// Resets a recording if the base savestate could not be loaded at the start // Resets a recording if the base savestate could not be loaded at the start
@ -121,11 +122,16 @@ private:
bool incrementUndo = false; bool incrementUndo = false;
InputRecordingMode state = InputRecording::InputRecordingMode::NotActive; InputRecordingMode state = InputRecording::InputRecordingMode::NotActive;
// Controller Data // Array of usable pads (currently, only 2)
PadData* padData[2]; struct InputRecordingPad
{
// VirtualPads // Controller Data
VirtualPad* virtualPads[2]; PadData* padData;
// VirtualPad
VirtualPad* virtualPad;
InputRecordingPad();
~InputRecordingPad();
} pads[2];
// Resolve the name and region of the game currently loaded using the GameDB // Resolve the name and region of the game currently loaded using the GameDB
// If the game cannot be found in the DB, the fallback is the ISO filename // If the game cannot be found in the DB, the fallback is the ISO filename

View File

@ -128,7 +128,7 @@ bool InputRecordingFile::open(const wxString path, bool newRecording)
return false; return false;
} }
bool InputRecordingFile::OpenNew(const wxString path, bool fromSavestate) bool InputRecordingFile::OpenNew(const wxString& path, bool fromSavestate)
{ {
if (fromSavestate) if (fromSavestate)
{ {
@ -152,7 +152,7 @@ bool InputRecordingFile::OpenNew(const wxString path, bool fromSavestate)
return false; return false;
} }
bool InputRecordingFile::OpenExisting(const wxString path) bool InputRecordingFile::OpenExisting(const wxString& path)
{ {
return open(path, false); return open(path, false);
} }

View File

@ -69,10 +69,10 @@ public:
// Increment the number of undo actions and commit it to the recording file // Increment the number of undo actions and commit it to the recording file
void IncrementUndoCount(); void IncrementUndoCount();
// Open an existing recording file // Open an existing recording file
bool OpenExisting(const wxString path); bool OpenExisting(const wxString& path);
// Create and open a brand new input recording, either starting from a save-state or from // Create and open a brand new input recording, either starting from a save-state or from
// booting the game // booting the game
bool OpenNew(const wxString path, bool fromSaveState); bool OpenNew(const wxString& path, bool fromSaveState);
// Reads the current frame's input data from the file in order to intercept and overwrite // Reads the current frame's input data from the file in order to intercept and overwrite
// the current frame's value from the emulator // the current frame's value from the emulator
bool ReadKeyBuffer(u8 &result, const uint &frame, const uint port, const uint bufIndex); bool ReadKeyBuffer(u8 &result, const uint &frame, const uint port, const uint bufIndex);

View File

@ -29,8 +29,7 @@
#include "DriveList.h" #include "DriveList.h"
#ifndef DISABLE_RECORDING #ifndef DISABLE_RECORDING
# include "Recording/VirtualPad/VirtualPad.h" #include "Recording/NewRecordingFrame.h"
# include "Recording/NewRecordingFrame.h"
#endif #endif
class DisassemblyDialog; class DisassemblyDialog;
@ -551,7 +550,6 @@ protected:
wxWindowID m_id_Disassembler; wxWindowID m_id_Disassembler;
#ifndef DISABLE_RECORDING #ifndef DISABLE_RECORDING
wxWindowID m_id_VirtualPad[2];
wxWindowID m_id_NewRecordingFrame; wxWindowID m_id_NewRecordingFrame;
#endif #endif
@ -580,7 +578,6 @@ public:
DisassemblyDialog* GetDisassemblyPtr() const { return (DisassemblyDialog*)wxWindow::FindWindowById(m_id_Disassembler); } DisassemblyDialog* GetDisassemblyPtr() const { return (DisassemblyDialog*)wxWindow::FindWindowById(m_id_Disassembler); }
#ifndef DISABLE_RECORDING #ifndef DISABLE_RECORDING
VirtualPad* GetVirtualPadPtr(int port) const { return (VirtualPad*)wxWindow::FindWindowById(m_id_VirtualPad[port]); }
NewRecordingFrame* GetNewRecordingFramePtr() const { return (NewRecordingFrame*)wxWindow::FindWindowById(m_id_NewRecordingFrame); } NewRecordingFrame* GetNewRecordingFramePtr() const { return (NewRecordingFrame*)wxWindow::FindWindowById(m_id_NewRecordingFrame); }
#endif #endif

View File

@ -28,7 +28,6 @@
#ifndef DISABLE_RECORDING #ifndef DISABLE_RECORDING
#include "Recording/InputRecording.h" #include "Recording/InputRecording.h"
#include "Recording/VirtualPad/VirtualPad.h"
#endif #endif
#include <wx/cmdline.h> #include <wx/cmdline.h>
@ -80,16 +79,10 @@ void Pcsx2App::OpenMainFrame()
m_id_Disassembler = disassembly->GetId(); m_id_Disassembler = disassembly->GetId();
#ifndef DISABLE_RECORDING #ifndef DISABLE_RECORDING
VirtualPad* virtualPad0 = new VirtualPad(mainFrame, 0, g_Conf->inputRecording);
g_InputRecording.setVirtualPadPtr(virtualPad0, 0);
m_id_VirtualPad[0] = virtualPad0->GetId();
VirtualPad* virtualPad1 = new VirtualPad(mainFrame, 1, g_Conf->inputRecording);
g_InputRecording.setVirtualPadPtr(virtualPad1, 1);
m_id_VirtualPad[1] = virtualPad1->GetId();
NewRecordingFrame* newRecordingFrame = new NewRecordingFrame(mainFrame); NewRecordingFrame* newRecordingFrame = new NewRecordingFrame(mainFrame);
m_id_NewRecordingFrame = newRecordingFrame->GetId(); m_id_NewRecordingFrame = newRecordingFrame->GetId();
if (g_Conf->EmuOptions.EnableRecordingTools)
g_InputRecording.InitVirtualPadWindows(mainFrame);
#endif #endif
if (g_Conf->EmuOptions.Debugger.ShowDebuggerOnStart) if (g_Conf->EmuOptions.Debugger.ShowDebuggerOnStart)

View File

@ -599,6 +599,7 @@ void MainEmuFrame::Menu_EnableRecordingTools_Click(wxCommandEvent& event)
if (checked) if (checked)
{ {
GetMenuBar()->Insert(TopLevelMenu_InputRecording, &m_menuRecording, _("&Input Record")); GetMenuBar()->Insert(TopLevelMenu_InputRecording, &m_menuRecording, _("&Input Record"));
g_InputRecording.InitVirtualPadWindows(this);
SysConsole.recordingConsole.Enabled = true; SysConsole.recordingConsole.Enabled = true;
// Enable Recording Keybindings // Enable Recording Keybindings
if (GSFrame* gsFrame = wxGetApp().GetGsFramePtr()) if (GSFrame* gsFrame = wxGetApp().GetGsFramePtr())
@ -948,8 +949,10 @@ void MainEmuFrame::Menu_Capture_Screenshot_Screenshot_As_Click(wxCommandEvent &e
void MainEmuFrame::Menu_Recording_New_Click(wxCommandEvent& event) void MainEmuFrame::Menu_Recording_New_Click(wxCommandEvent& event)
{ {
const bool initiallyPaused = g_InputRecordingControls.IsPaused(); const bool initiallyPaused = g_InputRecordingControls.IsPaused();
if (!initiallyPaused) if (!initiallyPaused)
g_InputRecordingControls.PauseImmediately(); g_InputRecordingControls.PauseImmediately();
NewRecordingFrame* newRecordingFrame = wxGetApp().GetNewRecordingFramePtr(); NewRecordingFrame* newRecordingFrame = wxGetApp().GetNewRecordingFramePtr();
if (newRecordingFrame) if (newRecordingFrame)
{ {
@ -971,8 +974,10 @@ void MainEmuFrame::Menu_Recording_New_Click(wxCommandEvent& event)
void MainEmuFrame::Menu_Recording_Play_Click(wxCommandEvent& event) void MainEmuFrame::Menu_Recording_Play_Click(wxCommandEvent& event)
{ {
const bool initiallyPaused = g_InputRecordingControls.IsPaused(); const bool initiallyPaused = g_InputRecordingControls.IsPaused();
if (!initiallyPaused) if (!initiallyPaused)
g_InputRecordingControls.PauseImmediately(); g_InputRecordingControls.PauseImmediately();
wxFileDialog openFileDialog(this, _("Select P2M2 record file."), L"", L"", wxFileDialog openFileDialog(this, _("Select P2M2 record file."), L"", L"",
L"p2m2 file(*.p2m2)|*.p2m2", wxFD_OPEN); L"p2m2 file(*.p2m2)|*.p2m2", wxFD_OPEN);
if (openFileDialog.ShowModal() == wxID_CANCEL) if (openFileDialog.ShowModal() == wxID_CANCEL)
@ -989,6 +994,7 @@ void MainEmuFrame::Menu_Recording_Play_Click(wxCommandEvent& event)
g_InputRecordingControls.Resume(); g_InputRecordingControls.Resume();
return; return;
} }
if (!g_InputRecording.GetInputRecordingData().FromSaveState()) if (!g_InputRecording.GetInputRecordingData().FromSaveState())
StartInputRecording(); StartInputRecording();
} }
@ -1036,6 +1042,6 @@ void MainEmuFrame::Menu_Recording_ToggleRecordingMode_Click(wxCommandEvent& even
void MainEmuFrame::Menu_Recording_VirtualPad_Open_Click(wxCommandEvent& event) void MainEmuFrame::Menu_Recording_VirtualPad_Open_Click(wxCommandEvent& event)
{ {
wxGetApp().GetVirtualPadPtr(event.GetId() - MenuId_Recording_VirtualPad_Port0)->Show(); g_InputRecording.ShowVirtualPad(event.GetId() - MenuId_Recording_VirtualPad_Port0);
} }
#endif #endif