Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[dialogs] Move JoypadConfig to its own class.
- Loading branch information
Showing
10 changed files
with
147 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#include "dialogs/joypad-config.h" | ||
|
||
#include <wx/xrc/xmlres.h> | ||
|
||
#include "dialogs/validated-child.h" | ||
#include "opts.h" | ||
#include "widgets/option-validator.h" | ||
#include "widgets/user-input-ctrl.h" | ||
|
||
namespace dialogs { | ||
|
||
// static | ||
JoypadConfig* JoypadConfig::NewInstance(wxWindow* parent) { | ||
assert(parent); | ||
return new JoypadConfig(parent); | ||
} | ||
|
||
JoypadConfig::JoypadConfig(wxWindow* parent) : wxDialog(), keep_on_top_styler_(this) { | ||
#if !wxCHECK_VERSION(3, 1, 0) | ||
// This needs to be set before loading any element on the window. This also | ||
// has no effect since wx 3.1.0, where it became the default. | ||
this->SetExtraStyle(wxWS_EX_VALIDATE_RECURSIVELY); | ||
#endif | ||
wxXmlResource::Get()->LoadDialog(this, parent, "JoypadConfig"); | ||
|
||
for (int joypad = 0; joypad < 4; joypad++) { | ||
wxWindow* panel = GetValidatedChild(this, wxString::Format("joy%d", joypad + 1)); | ||
|
||
GetValidatedChild(panel, "DefaultConfig") | ||
->SetValidator( | ||
widgets::OptionSelectedValidator(config::OptionID::kJoyDefault, joypad + 1)); | ||
|
||
// Set up tab order so input is easy to configure. Note that there are | ||
// two tabs for each panel, so we must check for the parent before | ||
// setting up the tab order. | ||
wxWindow* prev = nullptr; | ||
wxWindow* prev_parent = nullptr; | ||
for (const config::GameKey& game_key : config::kAllGameKeys) { | ||
const wxString game_key_name = config::GameKeyToString(game_key); | ||
widgets::UserInputCtrl* game_key_control = | ||
GetValidatedChild<widgets::UserInputCtrl>(panel, game_key_name); | ||
wxWindow* current_parent = game_key_control->GetParent(); | ||
|
||
game_key_control->SetValidator( | ||
widgets::UserInputCtrlValidator(config::GameControl(joypad, game_key))); | ||
|
||
if (current_parent == prev_parent) { | ||
// The first control will be skipped here, but that's fine since | ||
// we don't care where it fits in the tab order. | ||
assert(prev); | ||
game_key_control->MoveAfterInTabOrder(prev); | ||
} | ||
prev = game_key_control; | ||
prev_parent = current_parent; | ||
|
||
// Bind the individual "Clear" key event. | ||
panel->Bind(wxEVT_BUTTON, std::bind(&widgets::UserInputCtrl::Clear, game_key_control), | ||
XRCID(wxString("Clear" + config::GameKeyToString(game_key)).c_str())); | ||
} | ||
|
||
// Finally, bind the per-joypad "Defaults" and "Clear" events. | ||
panel->Bind(wxEVT_BUTTON, std::bind(&JoypadConfig::ResetToDefaults, this, panel), | ||
XRCID("Defaults")); | ||
panel->Bind(wxEVT_BUTTON, std::bind(&JoypadConfig::ClearJoypad, this, panel), | ||
XRCID("Clear")); | ||
} | ||
|
||
this->Fit(); | ||
} | ||
|
||
void JoypadConfig::ResetToDefaults(wxWindow* panel) { | ||
for (const config::GameKey& game_key : config::kAllGameKeys) { | ||
GetValidatedChild<widgets::UserInputCtrl>(panel, config::GameKeyToString(game_key)) | ||
->SetInputs(kDefaultBindings.find(config::GameControl(0, game_key))->second); | ||
} | ||
} | ||
|
||
void JoypadConfig::ClearJoypad(wxWindow* panel) { | ||
for (const config::GameKey& game_key : config::kAllGameKeys) { | ||
GetValidatedChild<widgets::UserInputCtrl>(panel, config::GameKeyToString(game_key)) | ||
->Clear(); | ||
} | ||
} | ||
|
||
} // namespace dialogs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#ifndef VBAM_WX_DIALOGS_JOYPAD_CONFIG_H_ | ||
#define VBAM_WX_DIALOGS_JOYPAD_CONFIG_H_ | ||
|
||
#include <wx/dialog.h> | ||
|
||
#include "widgets/keep-on-top-styler.h" | ||
|
||
namespace dialogs { | ||
|
||
// Manages the Joypad configuration dialog. | ||
class JoypadConfig : public wxDialog { | ||
public: | ||
static JoypadConfig* NewInstance(wxWindow* parent); | ||
~JoypadConfig() override = default; | ||
|
||
private: | ||
// The constructor is private so initialization has to be done via the | ||
// static method. This is because this class is destroyed when its | ||
// owner, `parent` is destroyed. This prevents accidental deletion. | ||
JoypadConfig(wxWindow* parent); | ||
|
||
// Resets all Joypad controls for `panel` to defaults. | ||
void ResetToDefaults(wxWindow* panel); | ||
|
||
// Clears all Joypad controls. | ||
void ClearJoypad(wxWindow* panel); | ||
|
||
const widgets::KeepOnTopStyler keep_on_top_styler_; | ||
}; | ||
|
||
} // namespace dialogs | ||
|
||
#endif // VBAM_WX_DIALOGS_JOYPAD_CONFIG_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters