/
rabid.cpp
244 lines (193 loc) · 7.6 KB
/
rabid.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// Rosanne : Trump card game popularly known as Twenty Eight (28)
// Copyright (C) 2006-2010 Vipin Cherian
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "rabid.h"
#include "ragamepanel.h"
BEGIN_EVENT_TABLE(raBid, wxPanel)
EVT_BUTTON(raBID_BTN_ID_ALL, raBid::OnButtonClick)
EVT_BUTTON(raBID_BTN_ID_PASS, raBid::OnButtonClick)
END_EVENT_TABLE()
raBid::raBid(const wxWindow* parent): wxPanel((wxWindow*)parent) {
int i = 0;
int j = 0;
int best_width = 0;
int temp_width = 0;
int temp_height = 0;
m_game = NULL;
m_min_bid = 0;
// Initializing the value of all the buttons to NULL
for(i = 0; i < raBID_TOTAL_BTNS; ++i)
m_buttons[i] = NULL;
m_button_all = NULL;
m_button_pass = NULL;
// Calculate the best width for the buttons
// The best width should be able to contain all bids from 14
// to 28 and the strings "All" and "Pass"
best_width = 0;
for(i = 0 ; i < raBID_TOTAL_BTNS; i++) {
this->GetTextExtent(wxString::Format(wxT("%d"), i + 14),
&temp_width, &temp_height);
best_width = std::max(best_width, temp_width);
}
this->GetTextExtent(wxT("All"), &temp_width, &temp_height);
best_width = std::max(best_width, temp_width);
this->GetTextExtent(wxT("Pass"), &temp_width, &temp_height);
best_width = std::max(best_width, temp_width);
wxLogDebug(wxString::Format(wxT("Best width %d"), best_width));
#ifdef __WXMSW__
this->SetWindowStyle(wxRAISED_BORDER);
#endif
m_main_panel = new wxPanel(this);
if(!m_main_panel) {
wxLogError(wxString::Format(wxT("Failed to create main panel. %s:%d"), wxT(__FILE__), __LINE__));
return;
}
#ifdef __WXMSW__
m_main_panel->SetWindowStyle(wxSUNKEN_BORDER);
#endif
m_main_panel->SetBackgroundColour(*wxWHITE);
m_main_sizer = new wxGridSizer(0, 0, 0, 0);
// TODO : Add error checks
m_main_panel_sizer = new wxBoxSizer(wxVERTICAL);
m_head_panel = new wxPanel(m_main_panel);
//m_head_panel->SetWindowStyle(wxRAISED_BORDER);
m_head_panel->SetBackgroundColour(raCLR_HEAD_DARK);
m_head_panel->SetForegroundColour(*wxWHITE);
m_bold_font = m_head_panel->GetFont();
m_bold_font.SetWeight(wxFONTWEIGHT_BOLD);
m_head_panel->SetFont(m_bold_font);
m_head_panel_sizer = new wxGridSizer(0, 0, 0, 0);
m_head_panel_text = new wxStaticText(m_head_panel, -1, wxT("Enter Bid"));
m_head_panel_sizer->Add(m_head_panel_text, 0,
wxALIGN_CENTER_HORIZONTAL| wxALIGN_CENTER_VERTICAL|wxALL, 2);
m_head_panel->SetSizer(m_head_panel_sizer);
m_bidbtn_panel = new wxPanel(m_main_panel);
//m_bidbtn_panel->SetWindowStyle(wxSUNKEN_BORDER);
m_bidbtn_panel_sizer = new wxGridSizer(5, 3, 0, 0);
for(i = 0; i < raBID_BTN_ROWS; i++) {
for(j = 0; j < raBID_BTN_COLS; j++) {
m_buttons[(i * raBID_BTN_COLS) + j] = new wxButton(m_bidbtn_panel,
raBID_BTN_ID_START + (i * raBID_BTN_COLS) + j,
wxString::Format(wxT("%d"), (i * raBID_BTN_COLS) + j + 14),
wxDefaultPosition, wxSize(best_width, -1));
m_bidbtn_panel_sizer->Add(m_buttons[(i * raBID_BTN_COLS) + j], 0,
wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL|wxEXPAND|wxALL, 1);
//m_buttons[(i * raBID_BTN_COLS) + j]->SetSize(10, 20);
//m_buttons[(i * raBID_BTN_COLS) + j]->SetWindowStyle(wxNO_BORDER);
m_buttons[(i * raBID_BTN_COLS) + j]->SetEventHandler(this->GetEventHandler());
}
}
m_bidbtn_panel->SetSizer(m_bidbtn_panel_sizer);
m_bidbtn_panel_sizer->Fit(m_bidbtn_panel);
// Create panel, assosiated sizer to hold the buttons
// to bid "All" and "Pass"
m_btns_panel = new wxPanel(m_main_panel);
m_btns_panel_sizer = new wxGridSizer(1, 2, 0, 0);
m_button_all = new wxButton(m_btns_panel, raBID_BTN_ID_ALL, wxT("All"));
m_button_pass = new wxButton(m_btns_panel, raBID_BTN_ID_PASS, wxT("Pass"));
//m_button_all->Disable();
//m_button_all->SetWindowStyle(wxNO_BORDER);
//m_button_pass->SetWindowStyle(wxNO_BORDER);
m_button_all->SetEventHandler(this->GetEventHandler());
m_button_pass->SetEventHandler(this->GetEventHandler());
m_btns_panel_sizer->Add(m_button_all, 0,
wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL|wxEXPAND|wxALL, 1);
m_btns_panel_sizer->Add(m_button_pass, 0,
wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL|wxEXPAND|wxALL, 1);
m_btns_panel->SetSizer(m_btns_panel_sizer);
m_btns_panel_sizer->Fit(m_btns_panel);
m_main_panel_sizer->Add(m_head_panel, 0, wxEXPAND | wxALL, raBID_PNL_RELIEF);
m_main_panel_sizer->Add(m_bidbtn_panel, 0, wxEXPAND, 0);
#ifdef __WXMSW__
m_main_panel_sizer->Add(m_btns_panel, 0, wxEXPAND | wxBOTTOM, raBID_PNL_RELIEF * 3);
#else
m_main_panel_sizer->Add(m_btns_panel, 0, wxEXPAND, 0);
#endif
m_main_panel->SetSizer(m_main_panel_sizer);
m_main_panel_sizer->Fit(m_main_panel);
m_main_sizer->Add(m_main_panel, 0, wxEXPAND, 0);
this->SetSizer(m_main_sizer);
m_main_sizer->Fit(this);
this->GetEventHandler()->Connect(raBID_BTN_ID_START, raBID_BTN_ID_START + raBID_TOTAL_BTNS - 1,
wxEVT_COMMAND_BUTTON_CLICKED,wxCommandEventHandler(raBid::OnButtonClick));
}
raBid::~raBid() {
}
//
// Public method/s
//
bool raBid::SetGamePanel(raGamePanel *game_panel) {
m_game = game_panel;
return true;
}
bool raBid::SetPassable(bool passable) {
// Enable/disable the pass button
// as per the input criteria
m_button_pass->Enable(passable);
return true;
}
bool raBid::SetMinimumBid(int min_bid) {
int i;
m_min_bid = min_bid;
// Disable all the bid buttons less than the minimum bid
// and enable the rest
for(i = 0; i < raBID_TOTAL_BTNS; i++)
m_buttons[i]->Enable(!(i < (min_bid - 14)));
return true;
}
//
// Private method/s
//
void raBid::OnButtonClick(wxCommandEvent &event) {
raBidEvent new_event;
wxString msg;
int id;
if(m_game) {
id = event.GetId();
switch(id) {
case raBID_BTN_ID_ALL:
// Alert the user if the bid is for All Cards
msg.Append(wxT("You have made a bid for All Cards\n\n"));
msg.Append(wxT("If you want to continue with the bid, click Yes\n"));
msg.Append(wxT("If you want to cancel the bid and make a new one, click No"));
if(wxMessageBox(msg, wxT("Confirm"), wxYES_NO | wxICON_QUESTION) != wxYES) {
event.Skip();
return;
}
new_event.SetBid(gmBID_ALL);
break;
case raBID_BTN_ID_PASS:
new_event.SetBid(gmBID_PASS);
break;
default:
// Alert the user if the bid is relatively high
// as compared to the minimum possible
if((raGetBidFromId(id)) >= (m_min_bid + 3)) {
msg.Append(wxString::Format(wxT("You have bid %d\n\n"), raGetBidFromId(id)));
msg.Append(wxT("If you want to continue with the bid, click Yes\n"));
msg.Append(wxT("If you want to cancel the bid and make a new one, click No"));
if(wxMessageBox(msg, wxT("Confirm"), wxYES_NO | wxICON_QUESTION) != wxYES) {
event.Skip();
return;
}
}
new_event.SetBid(raGetBidFromId(id));
break;
}
m_game->AddPendingEvent(new_event);
} else
wxLogError(wxString::Format(wxT("Game panel not set in raBid. %s:%d"), wxT(__FILE__), __LINE__));
event.Skip();
}