Skip to content

Commit 46121fc

Browse files
authored
Correct error in reveal dialog (#687)
This corrects an error in the reveal dialog in which logic was reversed - so player names were not being displayed. Additionally, this takes the opportunity to model wxWidgets modernisations for better design.
1 parent f4d36ec commit 46121fc

File tree

4 files changed

+85
-89
lines changed

4 files changed

+85
-89
lines changed

Makefile.am

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,6 @@ gambit_SOURCES = \
568568
src/gui/dlefglogit.cc \
569569
src/gui/dlefglogit.h \
570570
src/gui/dlefgreveal.cc \
571-
src/gui/dlefgreveal.h \
572571
src/gui/dlexcept.h \
573572
src/gui/dlgameprop.cc \
574573
src/gui/dlgameprop.h \

src/gui/dlefgreveal.cc

Lines changed: 78 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -20,69 +20,109 @@
2020
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
2121
//
2222

23+
#include <optional>
24+
2325
#include <wx/wxprec.h>
2426
#ifndef WX_PRECOMP
2527
#include <wx/wx.h>
2628
#endif // WX_PRECOMP
2729

2830
#include "gambit.h"
29-
#include "dlefgreveal.h"
31+
#include "gamedoc.h"
3032

31-
namespace Gambit::GUI {
32-
//=========================================================================
33-
// RevealMoveDialog: Member functions
34-
//=========================================================================
33+
namespace {
34+
35+
using namespace Gambit;
36+
using namespace Gambit::GUI;
37+
38+
class RevealMoveDialog final : public wxDialog {
39+
struct PlayerEntry {
40+
GamePlayer player;
41+
wxCheckBox *checkbox;
42+
};
43+
std::vector<PlayerEntry> m_entries;
44+
45+
void OnCheckbox(wxCommandEvent &) { UpdateButtonState(); }
46+
void UpdateButtonState();
3547

36-
RevealMoveDialog::RevealMoveDialog(wxWindow *p_parent, GameDocument *p_doc)
37-
: wxDialog(p_parent, wxID_ANY, _("Reveal this move to players"), wxDefaultPosition), m_doc(p_doc)
48+
public:
49+
RevealMoveDialog(wxWindow *p_parent, const Game &p_game);
50+
std::vector<GamePlayer> GetPlayers() const;
51+
};
52+
53+
RevealMoveDialog::RevealMoveDialog(wxWindow *p_parent, const Game &p_game)
54+
: wxDialog(p_parent, wxID_ANY, _("Reveal this move to players"), wxDefaultPosition,
55+
wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
3856
{
3957
auto *topSizer = new wxBoxSizer(wxVERTICAL);
4058

41-
auto *playerBox = new wxStaticBoxSizer(wxHORIZONTAL, this, _("Reveal the move to players"));
59+
auto *groupLabel = new wxStaticText(this, wxID_ANY, _("Reveal this move to players"));
60+
auto f = groupLabel->GetFont();
61+
f.SetWeight(wxFONTWEIGHT_BOLD);
62+
groupLabel->SetFont(f);
63+
topSizer->Add(groupLabel, wxSizerFlags().Border(wxLEFT | wxTOP | wxRIGHT, 10));
64+
65+
auto *playerBox = new wxBoxSizer(wxVERTICAL);
66+
playerBox->AddSpacer(3);
4267

43-
auto *boxSizer = new wxBoxSizer(wxVERTICAL);
68+
const auto &players = p_game->GetPlayers();
69+
m_entries.reserve(players.size());
4470

45-
for (size_t pl = 1; pl <= m_doc->NumPlayers(); pl++) {
46-
auto player = m_doc->GetGame()->GetPlayer(pl);
47-
if (player->GetLabel().empty()) {
48-
m_players.push_back(
49-
new wxCheckBox(this, wxID_ANY, wxString(player->GetLabel().c_str(), *wxConvCurrent)));
71+
for (const auto &player : players) {
72+
wxString label;
73+
if (!player->GetLabel().empty()) {
74+
label = wxString::FromUTF8(player->GetLabel());
5075
}
5176
else {
52-
m_players.push_back(new wxCheckBox(this, wxID_ANY, wxString::Format(_T("Player %d"), pl)));
77+
label = wxString::Format("Player %u", player->GetNumber());
5378
}
54-
m_players[pl]->SetValue(true);
55-
m_players[pl]->SetForegroundColour(m_doc->GetStyle().GetPlayerColor(player));
56-
boxSizer->Add(m_players[pl], 1, wxALL | wxEXPAND, 0);
79+
auto *cb = new wxCheckBox(this, wxID_ANY, label);
80+
cb->SetValue(true);
81+
cb->Bind(wxEVT_CHECKBOX, &RevealMoveDialog::OnCheckbox, this);
82+
m_entries.push_back({player, cb});
83+
playerBox->Add(cb, wxSizerFlags().Expand().Border(wxLEFT | wxRIGHT | wxTOP, 4));
5784
}
58-
playerBox->Add(boxSizer, 1, wxALL | wxEXPAND, 5);
59-
topSizer->Add(playerBox, 1, wxALL | wxEXPAND, 5);
60-
61-
auto *buttonSizer = new wxBoxSizer(wxHORIZONTAL);
62-
buttonSizer->Add(new wxButton(this, wxID_CANCEL, _("Cancel")), 0, wxALL, 5);
63-
auto *okButton = new wxButton(this, wxID_OK, _("OK"));
64-
okButton->SetDefault();
65-
buttonSizer->Add(okButton, 0, wxALL, 5);
66-
topSizer->Add(buttonSizer, 0, wxALL | wxALIGN_RIGHT, 5);
67-
68-
SetSizer(topSizer);
69-
topSizer->Fit(this);
70-
topSizer->SetSizeHints(this);
71-
wxTopLevelWindowBase::Layout();
85+
86+
topSizer->Add(playerBox, wxSizerFlags(1).Expand().Border(wxALL, 5));
87+
88+
auto *buttonSizer = CreateStdDialogButtonSizer(wxOK | wxCANCEL);
89+
buttonSizer->Realize();
90+
topSizer->Add(buttonSizer, wxSizerFlags().Right().Border(wxALL, 10));
91+
92+
SetSizerAndFit(topSizer);
7293
CenterOnParent();
94+
UpdateButtonState();
7395
}
7496

75-
Array<GamePlayer> RevealMoveDialog::GetPlayers() const
97+
void RevealMoveDialog::UpdateButtonState()
7698
{
77-
Array<GamePlayer> players;
99+
const bool anyChecked =
100+
std::any_of(m_entries.begin(), m_entries.end(),
101+
[](const PlayerEntry &entry) { return entry.checkbox->IsChecked(); });
102+
FindWindow(wxID_OK)->Enable(anyChecked);
103+
}
78104

79-
for (size_t pl = 1; pl <= m_doc->NumPlayers(); pl++) {
80-
if (m_players[pl]->GetValue()) {
81-
players.push_back(m_doc->GetGame()->GetPlayer(pl));
105+
std::vector<GamePlayer> RevealMoveDialog::GetPlayers() const
106+
{
107+
std::vector<GamePlayer> result;
108+
result.reserve(m_entries.size());
109+
for (const auto &[player, checkbox] : m_entries) {
110+
if (checkbox->GetValue()) {
111+
result.push_back(player);
82112
}
83113
}
114+
return result;
115+
}
116+
} // anonymous namespace
84117

85-
return players;
118+
namespace Gambit::GUI {
119+
120+
std::optional<std::vector<GamePlayer>> RevealMove(wxWindow *p_parent, const Game &p_game)
121+
{
122+
if (RevealMoveDialog dialog(p_parent, p_game); dialog.ShowModal() == wxID_OK) {
123+
return dialog.GetPlayers();
124+
}
125+
return std::nullopt;
86126
}
87127

88128
} // namespace Gambit::GUI

src/gui/dlefgreveal.h

Lines changed: 0 additions & 44 deletions
This file was deleted.

src/gui/gameframe.cc

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
//
2222

2323
#include <fstream>
24+
#include <optional>
2425

2526
#include <wx/wxprec.h>
2627
#ifndef WX_PRECOMP
@@ -54,7 +55,6 @@
5455
#include "dlabout.h"
5556

5657
#include "dlinsertmove.h"
57-
#include "dlefgreveal.h"
5858
#include "dleditnode.h"
5959
#include "dleditmove.h"
6060
#include "dlefglayout.h"
@@ -978,14 +978,15 @@ void GameFrame::OnEditRemoveOutcome(wxCommandEvent &)
978978
}
979979
}
980980

981+
std::optional<std::vector<GamePlayer>> RevealMove(wxWindow *p_parent, const Game &p_game);
982+
981983
void GameFrame::OnEditReveal(wxCommandEvent &)
982984
{
983-
RevealMoveDialog dialog(this, m_doc);
984-
985-
if (dialog.ShowModal() == wxID_OK) {
985+
if (const auto players = RevealMove(this, m_doc->GetGame()); players) {
986986
try {
987-
for (const auto &player : dialog.GetPlayers()) {
988-
m_doc->DoRevealAction(m_doc->GetSelectNode()->GetInfoset(), player);
987+
const auto &infoset = m_doc->GetSelectNode()->GetInfoset();
988+
for (const auto &player : *players) {
989+
m_doc->DoRevealAction(infoset, player);
989990
}
990991
}
991992
catch (std::exception &ex) {

0 commit comments

Comments
 (0)