| 1 | /*************************************************************************** |
| 2 | * Copyright (C) 2006-2016 by Guy Rutenberg * |
| 3 | * [email protected] * |
| 4 | * * |
| 5 | * This program is free software; you can redistribute it and/or modify * |
| 6 | * it under the terms of the GNU General Public License as published by * |
| 7 | * the Free Software Foundation; either version 2 of the License, or * |
| 8 | * (at your option) any later version. * |
| 9 | * * |
| 10 | * This program is distributed in the hope that it will be useful, * |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 13 | * GNU General Public License for more details. * |
| 14 | * * |
| 15 | * You should have received a copy of the GNU General Public License * |
| 16 | * along with this program; if not, write to the * |
| 17 | * Free Software Foundation, Inc., * |
| 18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 19 | ***************************************************************************/ |
| 20 | |
| 21 | /*********************************************** |
| 22 | * This File contains the definitions * |
| 23 | * of MainFrame's functions * |
| 24 | ***********************************************/ |
| 25 | |
| 26 | //#define DEBUG |
| 27 | |
| 28 | #include <wx/wx.h> |
| 29 | |
| 30 | #include "MainFrame.h" |
| 31 | #include "wxDynamicBitmap.h" |
| 32 | #include "highscores_dialog.h" |
| 33 | #include "about.h" |
| 34 | #include "configuration.h" |
| 35 | #include "settings_dialog.h" |
| 36 | #include "../config.h" |
| 37 | #include <iostream> |
| 38 | #include <functional> |
| 39 | #include <sstream> |
| 40 | #include <cstdlib> |
| 41 | #include <random> |
| 42 | #include <chrono> |
| 43 | #include <wx/version.h> |
| 44 | #include <wx/filename.h> |
| 45 | #include <wx/stdpaths.h> |
| 46 | #include <wx/mstream.h> |
| 47 | |
| 48 | //include the images for the dice |
| 49 | namespace dice { |
| 50 | #include "one.xpm" |
| 51 | #include "two.xpm" |
| 52 | #include "three.xpm" |
| 53 | #include "four.xpm" |
| 54 | #include "five.xpm" |
| 55 | #include "six.xpm" |
| 56 | } // namespace dice |
| 57 | |
| 58 | #include "icon32.xpm" |
| 59 | |
| 60 | using namespace main_frame; |
| 61 | |
| 62 | MainFrame::MainFrame(const wxString& title, const wxSize& size, long style) |
| 63 | : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, size, style) |
| 64 | { |
| 65 | //give the frame an icon |
| 66 | SetIcon(wxIcon(icon32_xpm)); |
| 67 | |
| 68 | #ifndef PORTABLE |
| 69 | wxString config_file = wxStandardPaths::Get().GetUserConfigDir() + wxT("/.openyahtzee"); |
| 70 | wxString old_config_file = wxFileName::GetHomeDir() + wxT("/.OpenYahtzee"); |
| 71 | // Move the configuration file from the old name to the new one |
| 72 | // if there isn't a file with that name already. |
| 73 | if (wxFileExists(old_config_file) && !wxFileExists(config_file)) { |
| 74 | wxRenameFile(old_config_file,config_file,false); |
| 75 | } |
| 76 | #else |
| 77 | wxFileName tmp_config_file(wxStandardPaths::Get().GetExecutablePath()); |
| 78 | tmp_config_file.SetFullName(wxT("openyahtzee.dat")); |
| 79 | wxString config_file = tmp_config_file.GetFullPath(); |
| 80 | #endif |
| 81 | |
| 82 | |
| 83 | std::string config_file_str; |
| 84 | config_file_str = config_file.utf8_str(); |
| 85 | m_config.reset(new configuration::Configuration(config_file_str)); |
| 86 | |
| 87 | m_evt_handler = new MainFrameEvtHandler(this); |
| 88 | |
| 89 | bitmap_dice[0] = new wxBitmap(dice::one_xpm); |
| 90 | bitmap_dice[1] = new wxBitmap(dice::two_xpm); |
| 91 | bitmap_dice[2] = new wxBitmap(dice::three_xpm); |
| 92 | bitmap_dice[3] = new wxBitmap(dice::four_xpm); |
| 93 | bitmap_dice[4] = new wxBitmap(dice::five_xpm); |
| 94 | bitmap_dice[5] = new wxBitmap(dice::six_xpm); |
| 95 | |
| 96 | AddMenus(); |
| 97 | |
| 98 | AddControlsAndLayout(); |
| 99 | |
| 100 | //make the text boxes uneditable to the user |
| 101 | wxTextCtrl *textctrl; |
| 102 | for(int i = ID_ACESTEXT;i<=ID_GRANDTOTAL;i++) { |
| 103 | textctrl = (wxTextCtrl*) FindWindow(i); |
| 104 | textctrl->SetEditable(false); |
| 105 | } |
| 106 | |
| 107 | //disable the undo button |
| 108 | (GetMenuBar()->FindItem(wxID_UNDO))->Enable(false); |
| 109 | |
| 110 | ConnectEventTable(); |
| 111 | |
| 112 | ResetRolls(); |
| 113 | m_yahtzee = false; |
| 114 | m_yahtzeebonus = false; |
| 115 | m_numofplaysleft = 13; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Adds the menus and menu items to the MainFrame. |
| 120 | */ |
| 121 | void MainFrame::AddMenus() |
| 122 | { |
| 123 | /*Create the menus*/ |
| 124 | wxMenu *gameMenu = new wxMenu; //create Game menu |
| 125 | wxMenu *help_menu = new wxMenu; //create Help menu |
| 126 | |
| 127 | |
| 128 | //insert menu items into menu Help |
| 129 | help_menu->Append(ID_HOWTOPLAY, wxT("How to Play\tF1")); |
| 130 | help_menu->Append(ID_FAQ, wxT("Frequently Asked Questions")); |
| 131 | help_menu->Append(ID_SENDCOMMENT, wxT("&Send Feedback")); |
| 132 | help_menu->Append(wxID_ABOUT); |
| 133 | |
| 134 | //insert menu items into menu Game |
| 135 | gameMenu->Append(wxID_NEW,wxT("&New Game\tF2")); |
| 136 | gameMenu->Append(wxID_UNDO,wxT("&Undo\tCtrl+Z")); |
| 137 | gameMenu->Append(ID_SHOWHIGHSCORE,wxT("Show Highscores\tCtrl+H")); |
| 138 | gameMenu->Append(ID_SETTINGS,wxT("Settings")); |
| 139 | gameMenu->Append(wxID_EXIT); |
| 140 | |
| 141 | // Declare the menu-bar and append the freshly created menus to the menu bar... |
| 142 | wxMenuBar *menuBar = new wxMenuBar(); |
| 143 | menuBar->Append(gameMenu, wxT("&Game")); |
| 144 | menuBar->Append(help_menu, wxT("&Help")); |
| 145 | |
| 146 | // ... and attach this menu bar to the frame |
| 147 | SetMenuBar(menuBar); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Add the controls to the frame and use sizers to lay them out. |
| 152 | */ |
| 153 | void MainFrame::AddControlsAndLayout() |
| 154 | { |
| 155 | wxPanel* panel = new wxPanel(this, ID_PANEL, |
| 156 | wxDefaultPosition, wxDefaultSize); |
| 157 | |
| 158 | wxBoxSizer *topSizer; |
| 159 | |
| 160 | if (m_config->get("horizontal-layout")=="True") { |
| 161 | topSizer = new wxBoxSizer( wxVERTICAL ); |
| 162 | sectionsSizer = new wxBoxSizer( wxHORIZONTAL ); |
| 163 | diceSizer = new wxBoxSizer( wxHORIZONTAL ); |
| 164 | } else { |
| 165 | topSizer = new wxBoxSizer( wxHORIZONTAL ); |
| 166 | sectionsSizer = new wxBoxSizer( wxVERTICAL ); |
| 167 | diceSizer = new wxBoxSizer( wxVERTICAL ); |
| 168 | } |
| 169 | |
| 170 | uppersection = new wxStaticBoxSizer( new wxStaticBox( |
| 171 | panel, wxID_ANY, wxT("Upper Section") ), wxVERTICAL); |
| 172 | |
| 173 | lowersection = new wxStaticBoxSizer(new wxStaticBox( |
| 174 | panel, wxID_ANY, wxT("Lower Section") ), wxVERTICAL); |
| 175 | |
| 176 | wxFlexGridSizer* uppergrid = new wxFlexGridSizer(2, 0, 10); |
| 177 | wxFlexGridSizer* lowergrid = new wxFlexGridSizer(2, 0, 10); |
| 178 | |
| 179 | const int SPACE_SIZE = 1; |
| 180 | |
| 181 | //BEGIN layout for the upper section of the score board |
| 182 | uppergrid->Add(new wxButton(panel,ID_ACES,wxT("Aces")), |
| 183 | 0, wxALL, SPACE_SIZE); |
| 184 | uppergrid->Add(new wxTextCtrl(panel, ID_ACESTEXT),1,wxALL,SPACE_SIZE); |
| 185 | uppergrid->Add(new wxButton(panel,ID_TWOS,wxT("Twos")), |
| 186 | 0, wxALL, SPACE_SIZE); |
| 187 | uppergrid->Add(new wxTextCtrl(panel, ID_TWOSTEXT),1,wxALL,SPACE_SIZE); |
| 188 | uppergrid->Add(new wxButton(panel,ID_THREES,wxT("Threes")), |
| 189 | 0, wxALL, SPACE_SIZE); |
| 190 | uppergrid->Add(new wxTextCtrl(panel, ID_THREESTEXT),1,wxALL,SPACE_SIZE); |
| 191 | uppergrid->Add(new wxButton(panel,ID_FOURS,wxT("Fours")), |
| 192 | 0, wxALL, SPACE_SIZE); |
| 193 | uppergrid->Add(new wxTextCtrl(panel, ID_FOURSTEXT),1,wxALL,SPACE_SIZE); |
| 194 | uppergrid->Add(new wxButton(panel,ID_FIVES,wxT("Fives")), |
| 195 | 0, wxALL, SPACE_SIZE); |
| 196 | uppergrid->Add(new wxTextCtrl(panel, ID_FIVESTEXT),1,wxALL,SPACE_SIZE); |
| 197 | uppergrid->Add(new wxButton(panel,ID_SIXES,wxT("Sixes")), |
| 198 | 0, wxALL, SPACE_SIZE); |
| 199 | uppergrid->Add(new wxTextCtrl(panel, ID_SIXESTEXT),1,wxALL,SPACE_SIZE); |
| 200 | uppergrid->Add(new wxStaticText(panel, wxID_ANY, wxT("Total score:")), |
| 201 | 0, wxALL, SPACE_SIZE); |
| 202 | uppergrid->Add(new wxTextCtrl(panel, ID_UPPERSECTIONTOTAL), |
| 203 | 1, wxALL, SPACE_SIZE); |
| 204 | uppergrid->Add(new wxStaticText(panel, wxID_ANY, wxT("Bonus:")), |
| 205 | 0, wxALL, SPACE_SIZE); |
| 206 | uppergrid->Add(new wxTextCtrl(panel, ID_BONUS),1,wxALL,SPACE_SIZE); |
| 207 | uppergrid->Add(new wxStaticText(panel, wxID_ANY, |
| 208 | wxT("Total of upper section:")), 0, wxALL, SPACE_SIZE); |
| 209 | uppergrid->Add(new wxTextCtrl(panel, ID_UPPERTOTAL), |
| 210 | 1, wxALL, SPACE_SIZE); |
| 211 | //END layout for the upper section of the score board |
| 212 | |
| 213 | //BEGIN layout for the lower section of the score board |
| 214 | lowergrid->Add(new wxButton(panel,ID_THREEOFAKIND,wxT("3 of a kind")), |
| 215 | 0, wxALL, SPACE_SIZE); |
| 216 | lowergrid->Add(new wxTextCtrl(panel, ID_THREEOFAKINDTEXT), |
| 217 | 1, wxALL, SPACE_SIZE); |
| 218 | lowergrid->Add(new wxButton(panel,ID_FOUROFAKIND,wxT("4 of a kind")), |
| 219 | 0, wxALL, SPACE_SIZE); |
| 220 | lowergrid->Add(new wxTextCtrl(panel, ID_FOUROFAKINDTEXT), |
| 221 | 1, wxALL, SPACE_SIZE); |
| 222 | lowergrid->Add(new wxButton(panel,ID_FULLHOUSE,wxT("Full House")), |
| 223 | 0, wxALL, SPACE_SIZE); |
| 224 | lowergrid->Add(new wxTextCtrl(panel, ID_FULLHOUSETEXT), |
| 225 | 1, wxALL, SPACE_SIZE); |
| 226 | lowergrid->Add(new wxButton(panel, ID_SMALLSEQUENCE, |
| 227 | wxT("Sequence of 4")), 0, wxALL,SPACE_SIZE); |
| 228 | lowergrid->Add(new wxTextCtrl(panel, ID_SMALLSEQUENCETEXT), |
| 229 | 1, wxALL, SPACE_SIZE); |
| 230 | lowergrid->Add(new wxButton(panel, ID_LARGESEQUENCE, |
| 231 | wxT("Sequence of 5")), 0, wxALL, SPACE_SIZE); |
| 232 | lowergrid->Add(new wxTextCtrl(panel, ID_LARGESEQUENCETEXT), |
| 233 | 1, wxALL, SPACE_SIZE); |
| 234 | lowergrid->Add(new wxButton(panel, ID_YAHTZEE, wxT("Yahtzee")), |
| 235 | 0, wxALL, SPACE_SIZE); |
| 236 | lowergrid->Add(new wxTextCtrl(panel,ID_YAHTZEETEXT),1,wxALL,SPACE_SIZE); |
| 237 | lowergrid->Add(new wxButton(panel, ID_CHANCE, wxT("Chance")), |
| 238 | 0, wxALL, SPACE_SIZE); |
| 239 | lowergrid->Add(new wxTextCtrl(panel, ID_CHANCETEXT),1,wxALL,SPACE_SIZE); |
| 240 | lowergrid->Add(new wxStaticText(panel, wxID_ANY, |
| 241 | wxT("Yahtzee Bonus")), 0, wxALL, SPACE_SIZE); |
| 242 | lowergrid->Add(new wxTextCtrl(panel, ID_YAHTZEEBONUSTEXT), |
| 243 | 1, wxALL, SPACE_SIZE); |
| 244 | lowergrid->Add(new wxStaticText(panel, wxID_ANY, |
| 245 | wxT("Total of lower section:")),0,wxALL,SPACE_SIZE); |
| 246 | lowergrid->Add(new wxTextCtrl(panel, ID_LOWERTOTAL),1,wxALL,SPACE_SIZE); |
| 247 | lowergrid->Add(new wxStaticText(panel, wxID_ANY, wxT("Grand Total:")), |
| 248 | 0, wxALL, SPACE_SIZE); |
| 249 | lowergrid->Add(new wxTextCtrl(panel, ID_GRANDTOTAL),1,wxALL,SPACE_SIZE); |
| 250 | //END layout for the lower section of the score board |
| 251 | |
| 252 | uppersection->Add(uppergrid); |
| 253 | lowersection->Add(lowergrid); |
| 254 | sectionsSizer->Add(uppersection, 0, wxALL, 5); |
| 255 | sectionsSizer->Add(lowersection, 0, wxALL, 5); |
| 256 | |
| 257 | //BEGIN layout for the dice section of the score board |
| 258 | for (int i = 0; i < 5; i++) { |
| 259 | auto box_sizer = new wxBoxSizer(wxVERTICAL); |
| 260 | box_sizer->Add(new wxDynamicBitmap(panel, ID_DICE1 + i, |
| 261 | bitmap_dice[i]), 0, wxALIGN_CENTER); |
| 262 | box_sizer->Add(new wxCheckBox(panel, ID_DICE1KEEP + i, |
| 263 | wxT("Keep")), 0, wxALIGN_CENTER); |
| 264 | diceSizer->Add(box_sizer); |
| 265 | diceSizer->Add(10, 10); |
| 266 | } |
| 267 | diceSizer->Add(new wxButton(panel, ID_ROLL, wxT("Roll!"), |
| 268 | wxDefaultPosition, wxSize(64,64)), 0, wxALL); |
| 269 | |
| 270 | topSizer->Add(sectionsSizer); |
| 271 | topSizer->Add(diceSizer, 0, wxALL, 5); |
| 272 | |
| 273 | AdjustFrameSize(topSizer); |
| 274 | |
| 275 | #if (wxMAJOR_VERSION == 2 && wxMINOR_VERSION == 8 && defined __WXGTK__) |
| 276 | if (!m_menuBarHeight) { |
| 277 | // This checks for the bug in Ubuntu with wxGTK-2.8 where the |
| 278 | // menu-bar only appears after OnInternalIdle() event has been |
| 279 | // called. This results in wrong size calculation for the |
| 280 | // window, so we reserve additional space for the menu bar so |
| 281 | // when it appears it won't push the bottom of the window |
| 282 | // outside the boundary. |
| 283 | wxSize suggested_window_size = ClientToWindowSize(topSizer->GetMinSize()); |
| 284 | suggested_window_size.SetHeight((GetMenuBar()->GetSize()).GetHeight() + |
| 285 | suggested_window_size.GetHeight()); |
| 286 | this->SetSize(suggested_window_size); |
| 287 | this->SetSizeHints(suggested_window_size, suggested_window_size); |
| 288 | } |
| 289 | #endif |
| 290 | } |
| 291 | |
| 292 | void MainFrame::AdjustFrameSize(wxSizer *sizer) |
| 293 | { |
| 294 | auto panel = (wxPanel*)FindWindow(ID_PANEL); |
| 295 | |
| 296 | panel->SetSizerAndFit(sizer); |
| 297 | sizer->Fit(this); |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Connects the tables to the event handlers |
| 302 | */ |
| 303 | void MainFrame::ConnectEventTable() |
| 304 | { |
| 305 | //BEGIN connecting the menu items' events |
| 306 | Connect(wxID_EXIT, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::OnQuit)); |
| 307 | Connect(wxID_ABOUT, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::OnAbout)); |
| 308 | Connect(ID_HOWTOPLAY, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::OnHelpMenuLink)); |
| 309 | Connect(ID_FAQ, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::OnHelpMenuLink)); |
| 310 | Connect(ID_SENDCOMMENT, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::OnSendComment)); |
| 311 | Connect(wxID_NEW, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::OnNewGame)); |
| 312 | Connect(wxID_UNDO, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::OnUndo)); |
| 313 | Connect(ID_SHOWHIGHSCORE, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::OnShowHighscore)); |
| 314 | Connect(ID_SETTINGS, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::OnSettings)); |
| 315 | //END connecting the menu items' events |
| 316 | |
| 317 | Connect(ID_ROLL, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (MainFrame::DoubleRollLock)); |
| 318 | |
| 319 | //BEGIN connecting the scoreboard buttons to the events |
| 320 | Connect(ID_ACES,ID_SIXES, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (MainFrame::OnUpperButtons)); |
| 321 | Connect(ID_THREEOFAKIND, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (MainFrame::On3ofakindButton)); |
| 322 | Connect(ID_FOUROFAKIND, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (MainFrame::On4ofakindButton)); |
| 323 | Connect(ID_FULLHOUSE, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (MainFrame::OnFullHouseButton)); |
| 324 | Connect(ID_SMALLSEQUENCE, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (MainFrame::OnSmallSequenceButton)); |
| 325 | Connect(ID_LARGESEQUENCE, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (MainFrame::OnLargeSequenceButton)); |
| 326 | Connect(ID_YAHTZEE, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (MainFrame::OnYahtzeeButton)); |
| 327 | Connect(ID_CHANCE, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (MainFrame::OnChanceButton)); |
| 328 | Connect(ID_DICE1,ID_DICE5, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (MainFrame::OnDiceClick)); |
| 329 | Connect(ID_DICE1KEEP,ID_DICE5KEEP, wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler (MainFrame::OnKeepClick)); |
| 330 | //END connecting the scoreboard buttons to the event |
| 331 | |
| 332 | /* The following code connects the on wxEVT_ENTER_WINDOW and |
| 333 | * wxEVT_LEAVE_WINDOW events to the score buttons to handle the score |
| 334 | * hints. For some unknown technical problem we have to directly connect |
| 335 | * the events, otherwise the wxEVT_LEAVE_WINDOW doesn't get generated. |
| 336 | * We have to use event handler other than MainFrame's as the regular |
| 337 | * event handler leads to segmantation fault. |
| 338 | */ |
| 339 | wxButton *temp; |
| 340 | for (int i = ID_ACES; i<=ID_CHANCE; i++) { |
| 341 | temp = ((wxButton *)FindWindow(i)); |
| 342 | temp->Connect(wxEVT_LEAVE_WINDOW, wxMouseEventHandler(MainFrameEvtHandler::OnScoreMouseLeave), NULL, m_evt_handler); |
| 343 | temp->Connect(wxEVT_ENTER_WINDOW, wxMouseEventHandler(MainFrameEvtHandler::OnScoreMouseEnter), NULL, m_evt_handler); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | |
| 348 | /*********EVENT PROCCESSING FUNCTIONS********/ |
| 349 | |
| 350 | void MainFrame::OnAbout(wxCommandEvent& event) |
| 351 | { |
| 352 | about::AboutDialog *about = new about::AboutDialog(this); |
| 353 | about->ShowModal(); |
| 354 | delete about; |
| 355 | } |
| 356 | |
| 357 | void MainFrame::OnHelpMenuLink (wxCommandEvent& event) |
| 358 | { |
| 359 | wxString link; |
| 360 | switch (event.GetId()) { |
| 361 | case ID_HOWTOPLAY: |
| 362 | link = wxT("http://www.openyahtzee.org/wiki/HowToPlay"); |
| 363 | break; |
| 364 | case ID_FAQ: |
| 365 | link = wxT("http://www.openyahtzee.org/wiki/FAQ"); |
| 366 | break; |
| 367 | } |
| 368 | |
| 369 | wxLaunchDefaultBrowser(link); |
| 370 | } |
| 371 | |
| 372 | |
| 373 | /** |
| 374 | * Starts up the user browser and connects to the Open Yahtzee website's |
| 375 | * feedback page. |
| 376 | * \param event |
| 377 | */ |
| 378 | void MainFrame::OnSendComment (wxCommandEvent& event) |
| 379 | { |
| 380 | wxString link = wxT("http://www.guyrutenberg.com/contact-me"); |
| 381 | |
| 382 | wxLaunchDefaultBrowser(link); |
| 383 | } |
| 384 | |
| 385 | void MainFrame::OnQuit(wxCommandEvent& event) |
| 386 | { |
| 387 | // Destroy the frame |
| 388 | Close(); |
| 389 | } |
| 390 | |
| 391 | /** |
| 392 | * This is the event handler of the New Game menu item. It starts a new |
| 393 | * game and clears the board from the last game. |
| 394 | * \param event |
| 395 | */ |
| 396 | void MainFrame::OnNewGame(wxCommandEvent& event) |
| 397 | { |
| 398 | //disable the undo button so it won't be enabled when a new game is started. |
| 399 | (GetMenuBar()->FindItem(wxID_UNDO))->Enable(false); |
| 400 | |
| 401 | ResetRolls(); |
| 402 | m_yahtzee = false; |
| 403 | m_numofplaysleft = 13; |
| 404 | |
| 405 | for (int i = ID_ACESTEXT; i<= ID_GRANDTOTAL; i++) |
| 406 | ((wxTextCtrl*) FindWindow(i))->Clear(); |
| 407 | for (int i = ID_ACES; i<= ID_CHANCE; i++) |
| 408 | ((wxButton*) FindWindow(i))->Enable(true); |
| 409 | } |
| 410 | |
| 411 | /** |
| 412 | * This function handles the undo events. It's connected to the Undo menu item. |
| 413 | * |
| 414 | * Note that the function only allows undoing of scoring actions and not dice |
| 415 | * rolls. Also it won't allow the undoing of the last move of the game because |
| 416 | * of a technical problem relating to the case an high-score was made and then |
| 417 | * the user undo the last move and rescore another high-score and so on. |
| 418 | * \param event |
| 419 | */ |
| 420 | void MainFrame::OnUndo(wxCommandEvent& event) |
| 421 | { |
| 422 | m_rolls = m_rollsundo; |
| 423 | |
| 424 | //after the user scored the button was enabled, check if it should be disabled |
| 425 | if (m_rolls <= 0) //we don't have remaining rolls |
| 426 | ((wxButton*) FindWindow(ID_ROLL)) -> Enable(false); |
| 427 | |
| 428 | // change the displayed roll counter |
| 429 | wxString caption = wxString::Format(wxT("Roll! (%hi)"),m_rolls); |
| 430 | FindWindow(ID_ROLL)->SetLabel(caption); |
| 431 | |
| 432 | //restore the 'keep' checkboxes |
| 433 | for (int i=0; i<5; i++) |
| 434 | ((wxCheckBox*) FindWindow(i + ID_DICE1KEEP)) -> Enable(true); |
| 435 | |
| 436 | //reset the users last choice |
| 437 | FindWindow(m_lastmove)->Enable(true); |
| 438 | |
| 439 | //clear the score; |
| 440 | ((wxTextCtrl*)FindWindow(ID_ACESTEXT + (m_lastmove - ID_ACES)))->SetValue(wxT("")); |
| 441 | |
| 442 | //Undo the Yahtzee flag if needed |
| 443 | if (m_lastmove == ID_YAHTZEE) { |
| 444 | m_yahtzee = false; |
| 445 | } |
| 446 | |
| 447 | //undo also the yahtzee bonus if needed |
| 448 | if (m_yahtzeebonus) { |
| 449 | wxString tempstr; |
| 450 | |
| 451 | tempstr = ((wxTextCtrl*) FindWindow(ID_YAHTZEEBONUSTEXT)) -> GetValue(); |
| 452 | tempstr.Printf(wxT("%li"), strtol(tempstr.mb_str(), NULL, 10) - 100); |
| 453 | ((wxTextCtrl*) FindWindow(ID_YAHTZEEBONUSTEXT)) -> SetValue(tempstr); |
| 454 | } |
| 455 | |
| 456 | //recalculate the subtotals |
| 457 | CalculateSubTotal(); |
| 458 | |
| 459 | (GetMenuBar()->FindItem(wxID_UNDO))->Enable(false); |
| 460 | //cancel the counting for the choice that was canceled |
| 461 | m_numofplaysleft++; |
| 462 | } |
| 463 | |
| 464 | /** |
| 465 | * This function enables the undo button and stores the last move |
| 466 | * \param id |
| 467 | */ |
| 468 | inline void MainFrame::EnableUndo(int id) |
| 469 | { |
| 470 | if (m_numofplaysleft) { |
| 471 | (GetMenuBar()->FindItem(wxID_UNDO))->Enable(true); |
| 472 | m_lastmove = id; |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | /** |
| 477 | * Shows the high-score dialog. Connected to the Game->"Show High Score" menu item. |
| 478 | * \param event |
| 479 | */ |
| 480 | void MainFrame::OnShowHighscore(wxCommandEvent& event) |
| 481 | { |
| 482 | highscores_dialog::HighscoresDialog *dialog = new highscores_dialog::HighscoresDialog(this, m_config.get()); |
| 483 | |
| 484 | dialog->ShowModal(); |
| 485 | |
| 486 | delete dialog; |
| 487 | } |
| 488 | |
| 489 | /** |
| 490 | * Shows the settings dialog. This event-handler is connected to Game->Settings |
| 491 | * menu item. |
| 492 | * \param event |
| 493 | */ |
| 494 | void MainFrame::OnSettings( wxCommandEvent& event) |
| 495 | { |
| 496 | settings_dialog::SettingsDialog* settings_dialog = new settings_dialog::SettingsDialog(this, m_config.get()); |
| 497 | |
| 498 | if (settings_dialog->ShowModal()!=wxID_OK) { |
| 499 | delete settings_dialog; |
| 500 | return; |
| 501 | } |
| 502 | |
| 503 | ((wxTextCtrl*) FindWindow(ID_UPPERSECTIONTOTAL)) -> SetValue(wxT("")); |
| 504 | ((wxTextCtrl*) FindWindow(ID_LOWERTOTAL)) -> SetValue(wxT("")); |
| 505 | CalculateSubTotal(); |
| 506 | |
| 507 | Relayout(); |
| 508 | |
| 509 | delete settings_dialog; |
| 510 | } |
| 511 | |
| 512 | /** |
| 513 | * Event handler for the Roll button. It checks the settings if it should |
| 514 | * animate the dice and then rolls them accordingly. It also checks for the |
| 515 | * status of the "keep" checkboxes. |
| 516 | * \param event |
| 517 | */ |
| 518 | void MainFrame::OnRollButton () |
| 519 | { |
| 520 | short int dice[5]; //holds the dices score |
| 521 | |
| 522 | static std::mt19937::result_type seed = std::chrono::high_resolution_clock::now().time_since_epoch().count(); |
| 523 | static auto dice_rand = std::bind(std::uniform_int_distribution<int>(1,6), |
| 524 | std::mt19937(seed)); |
| 525 | static auto num_throws_rand = std::bind(std::uniform_int_distribution<int>(5,18), |
| 526 | std::mt19937(seed * 0x101010101)); |
| 527 | |
| 528 | // fill the dice array with the old values |
| 529 | int dice_throws[5]; |
| 530 | for (int i = 0 ; i < 5; ++i) { |
| 531 | dice[i]=m_score_dice.GetDice(i+1); |
| 532 | dice_throws[i] = 0; |
| 533 | if ( ((wxCheckBox*) FindWindow(i + ID_DICE1KEEP))->IsChecked() ) { |
| 534 | dice_throws[i] = 0; |
| 535 | } else { |
| 536 | dice_throws[i] = 1; |
| 537 | if (m_config->get("dice-animation") == "True") { |
| 538 | dice_throws[i] = num_throws_rand(); |
| 539 | } |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | while (dice_throws[0] || dice_throws[1] || dice_throws[2] || dice_throws[3] || dice_throws[4]) { |
| 544 | for (int i=0 ; i<5; i++){ |
| 545 | if(dice_throws[i]){ |
| 546 | dice_throws[i]--; |
| 547 | dice[i] = dice_rand(); |
| 548 | ((wxDynamicBitmap*) FindWindow(i + |
| 549 | ID_DICE1)) -> SetBitmap( |
| 550 | bitmap_dice[dice[i]-1]); |
| 551 | } |
| 552 | } |
| 553 | if (m_config->get("dice-animation")=="True") { |
| 554 | wxMilliSleep(100); |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | m_score_dice.SetDice(dice); |
| 559 | m_score_dice.SetYahtzeeJoker(YahtzeeJoker()); |
| 560 | --m_rolls; |
| 561 | |
| 562 | // change the displayed roll counter |
| 563 | wxString caption = wxString::Format(wxT("Roll! (%hi)"),m_rolls); |
| 564 | FindWindow(ID_ROLL)->SetLabel(caption); |
| 565 | |
| 566 | #ifndef DEBUG |
| 567 | if (m_rolls <= 0) { |
| 568 | ((wxButton*) FindWindow(ID_ROLL)) -> Enable(false); |
| 569 | FindWindow(ID_ROLL)->SetLabel(wxT("Roll!")); |
| 570 | } |
| 571 | |
| 572 | #endif |
| 573 | |
| 574 | //enable the keep checkboxes |
| 575 | for (int i=0; i<5; i++) |
| 576 | ((wxCheckBox*) FindWindow(i + ID_DICE1KEEP)) -> Enable(true); |
| 577 | |
| 578 | //we rolled the dices so undoing isn't allowed |
| 579 | (GetMenuBar()->FindItem(wxID_UNDO))->Enable(false); |
| 580 | m_yahtzeebonus = false; //if we scored yahtzee bonus before we don't care anymore. |
| 581 | } |
| 582 | |
| 583 | void MainFrame::DoubleRollLock(wxCommandEvent& event) |
| 584 | { |
| 585 | /* The function works by connecting to idle event, and raising a |
| 586 | * flag when it process a valid click on the Roll button. When the |
| 587 | * another click happens and the flag is up, it means it is |
| 588 | * accidental. When the idle events is triggered it means that we |
| 589 | * no longer roll the dice, so we lower the flag. |
| 590 | */ |
| 591 | static bool skip_roll = false; |
| 592 | if (event.GetEventType() == wxEVT_IDLE) { |
| 593 | skip_roll = false; |
| 594 | Disconnect(wxEVT_IDLE, wxCommandEventHandler(MainFrame::DoubleRollLock)); |
| 595 | return; |
| 596 | } |
| 597 | |
| 598 | if (skip_roll) |
| 599 | return; |
| 600 | skip_roll = true; |
| 601 | |
| 602 | Connect(wxEVT_IDLE, wxCommandEventHandler(MainFrame::DoubleRollLock)); |
| 603 | OnRollButton(); |
| 604 | } |
| 605 | |
| 606 | /** |
| 607 | * This is the event-handler for all of the scoring buttons of the upper section. |
| 608 | * |
| 609 | * It gets the id of the button that called the event handler by comparing it |
| 610 | * to the id of the aces button it figures what button was pressed, And updates |
| 611 | * the suiting score textbox (again by comparing the id to the one of the aces |
| 612 | * checbox). |
| 613 | * \param event |
| 614 | */ |
| 615 | void MainFrame::OnUpperButtons (wxCommandEvent& event) |
| 616 | { |
| 617 | wxString out; |
| 618 | short int temp; |
| 619 | if (!IsValidClick()) |
| 620 | return; |
| 621 | |
| 622 | YahtzeeBonus(); |
| 623 | |
| 624 | switch(event.GetId()) { |
| 625 | case ID_ACES: |
| 626 | temp = m_score_dice.Aces(); |
| 627 | break; |
| 628 | case ID_TWOS: |
| 629 | temp = m_score_dice.Twos(); |
| 630 | break; |
| 631 | case ID_THREES: |
| 632 | temp = m_score_dice.Threes(); |
| 633 | break; |
| 634 | case ID_FOURS: |
| 635 | temp = m_score_dice.Fours(); |
| 636 | break; |
| 637 | case ID_FIVES: |
| 638 | temp = m_score_dice.Fives(); |
| 639 | break; |
| 640 | case ID_SIXES: |
| 641 | temp = m_score_dice.Sixes(); |
| 642 | break; |
| 643 | } |
| 644 | |
| 645 | out.Printf(wxT("%hi"),temp); |
| 646 | wxTextCtrl *text_ctrl = dynamic_cast<wxTextCtrl*>(FindWindow(event.GetId() - ID_ACES + ID_ACESTEXT)); |
| 647 | text_ctrl->SetValue(out); |
| 648 | text_ctrl->SetForegroundColour(wxNullColour); |
| 649 | text_ctrl->Refresh(); |
| 650 | |
| 651 | PostScore(event.GetId()); |
| 652 | } |
| 653 | |
| 654 | /** |
| 655 | * Event handler for the "3 of a kind" score button. |
| 656 | * \param event |
| 657 | */ |
| 658 | void MainFrame::On3ofakindButton(wxCommandEvent& event) |
| 659 | { |
| 660 | if (!IsValidClick()) |
| 661 | return; |
| 662 | |
| 663 | YahtzeeBonus(); |
| 664 | wxString out; |
| 665 | |
| 666 | out.Printf(wxT("%hi"),m_score_dice.ThreeOfAKind()); |
| 667 | wxTextCtrl *text_ctrl = dynamic_cast<wxTextCtrl*>(FindWindow(ID_THREEOFAKINDTEXT)); |
| 668 | text_ctrl->SetValue(out); |
| 669 | text_ctrl->SetForegroundColour(wxNullColour); |
| 670 | text_ctrl->Refresh(); |
| 671 | |
| 672 | PostScore(event.GetId()); |
| 673 | } |
| 674 | |
| 675 | /** |
| 676 | * Event handler for the "4 of a kind" score button. |
| 677 | * \param event |
| 678 | */ |
| 679 | void MainFrame::On4ofakindButton(wxCommandEvent& event) |
| 680 | { |
| 681 | if (!IsValidClick()) |
| 682 | return; |
| 683 | |
| 684 | YahtzeeBonus(); |
| 685 | wxString out; |
| 686 | |
| 687 | out.Printf(wxT("%hi"),m_score_dice.FourOfAKind()); |
| 688 | wxTextCtrl *text_ctrl = dynamic_cast<wxTextCtrl*>(FindWindow(ID_FOUROFAKINDTEXT)); |
| 689 | text_ctrl->SetValue(out); |
| 690 | text_ctrl->SetForegroundColour(wxNullColour); |
| 691 | text_ctrl->Refresh(); |
| 692 | |
| 693 | PostScore(event.GetId()); |
| 694 | } |
| 695 | |
| 696 | /** |
| 697 | * Event handler for the full-house score button. |
| 698 | * \param event |
| 699 | */ |
| 700 | void MainFrame::OnFullHouseButton(wxCommandEvent& event) |
| 701 | { |
| 702 | wxString out; |
| 703 | |
| 704 | if (!IsValidClick()) |
| 705 | return; |
| 706 | |
| 707 | YahtzeeBonus(); |
| 708 | |
| 709 | out.Printf(wxT("%hi"), m_score_dice.FullHouse()); |
| 710 | wxTextCtrl *text_ctrl = dynamic_cast<wxTextCtrl*>(FindWindow(ID_FULLHOUSETEXT)); |
| 711 | text_ctrl->SetValue(out); |
| 712 | text_ctrl->SetForegroundColour(wxNullColour); |
| 713 | text_ctrl->Refresh(); |
| 714 | |
| 715 | PostScore(event.GetId()); |
| 716 | } |
| 717 | |
| 718 | /** |
| 719 | * Event handler for the small-sequence score button. |
| 720 | * \param event |
| 721 | */ |
| 722 | void MainFrame::OnSmallSequenceButton(wxCommandEvent& event) |
| 723 | { |
| 724 | wxString out; |
| 725 | |
| 726 | if (!IsValidClick()) |
| 727 | return; |
| 728 | |
| 729 | YahtzeeBonus(); |
| 730 | |
| 731 | out.Printf(wxT("%hi"), m_score_dice.SmallSequence()); |
| 732 | wxTextCtrl *text_ctrl = dynamic_cast<wxTextCtrl*>(FindWindow(ID_SMALLSEQUENCETEXT)); |
| 733 | text_ctrl->SetValue(out); |
| 734 | text_ctrl->SetForegroundColour(wxNullColour); |
| 735 | text_ctrl->Refresh(); |
| 736 | |
| 737 | PostScore(event.GetId()); |
| 738 | } |
| 739 | |
| 740 | /** |
| 741 | * Event handler for the large-sequence score button. |
| 742 | * \param event |
| 743 | */ |
| 744 | void MainFrame::OnLargeSequenceButton(wxCommandEvent& event) |
| 745 | { |
| 746 | wxString out; |
| 747 | |
| 748 | if (!IsValidClick()) |
| 749 | return; |
| 750 | |
| 751 | YahtzeeBonus(); |
| 752 | |
| 753 | out.Printf(wxT("%hi"), m_score_dice.LargeSequence()); |
| 754 | wxTextCtrl *text_ctrl = dynamic_cast<wxTextCtrl*>(FindWindow(ID_LARGESEQUENCETEXT)); |
| 755 | text_ctrl->SetValue(out); |
| 756 | text_ctrl->SetForegroundColour(wxNullColour); |
| 757 | text_ctrl->Refresh(); |
| 758 | |
| 759 | |
| 760 | PostScore(event.GetId()); |
| 761 | } |
| 762 | |
| 763 | /** |
| 764 | * Event handler for the yahtzee score button. |
| 765 | * \param event |
| 766 | */ |
| 767 | void MainFrame::OnYahtzeeButton(wxCommandEvent& event) |
| 768 | { |
| 769 | wxString out; |
| 770 | |
| 771 | if (!IsValidClick()) |
| 772 | return; |
| 773 | |
| 774 | if (m_score_dice.IsYahtzee()) m_yahtzee = true; |
| 775 | out.Printf(wxT("%hi"), m_score_dice.Yahtzee()); |
| 776 | wxTextCtrl *text_ctrl = dynamic_cast<wxTextCtrl*>(FindWindow(ID_YAHTZEETEXT)); |
| 777 | text_ctrl->SetValue(out); |
| 778 | text_ctrl->SetForegroundColour(wxNullColour); |
| 779 | text_ctrl->Refresh(); |
| 780 | |
| 781 | PostScore(event.GetId()); |
| 782 | } |
| 783 | |
| 784 | /** |
| 785 | * Evnet handler for the chance score button. |
| 786 | * \param event |
| 787 | */ |
| 788 | void MainFrame::OnChanceButton (wxCommandEvent& event) |
| 789 | { |
| 790 | wxString out; |
| 791 | if (!IsValidClick()) |
| 792 | return; |
| 793 | |
| 794 | YahtzeeBonus(); |
| 795 | |
| 796 | out.Printf(wxT("%hi"),m_score_dice.Chance()); |
| 797 | wxTextCtrl *text_ctrl = dynamic_cast<wxTextCtrl*>(FindWindow(ID_CHANCETEXT)); |
| 798 | text_ctrl->SetValue(out); |
| 799 | text_ctrl->SetForegroundColour(wxNullColour); |
| 800 | text_ctrl->Refresh(); |
| 801 | PostScore(event.GetId()); |
| 802 | } |
| 803 | |
| 804 | /** |
| 805 | * Event handler for mouse clicks on the dice. |
| 806 | * |
| 807 | * When clicking on the dice this event-handler ticks the appropriate "keep" checkbox. |
| 808 | * \param event |
| 809 | */ |
| 810 | void MainFrame::OnDiceClick (wxCommandEvent& event) |
| 811 | { |
| 812 | //tick the checkbox |
| 813 | if (((wxCheckBox*) FindWindow(event.GetId()-ID_DICE1 + ID_DICE1KEEP))->IsEnabled()){ |
| 814 | bool newvalue = (((wxCheckBox*) FindWindow(event.GetId()-ID_DICE1 + ID_DICE1KEEP))->GetValue())?false:true; |
| 815 | ((wxCheckBox*) FindWindow(event.GetId()-ID_DICE1 + ID_DICE1KEEP)) -> SetValue(newvalue); |
| 816 | } |
| 817 | |
| 818 | //dispatch a click event on the checkbox |
| 819 | wxCommandEvent clickevent((event.GetId()-ID_DICE1 + ID_DICE1KEEP),wxEVT_COMMAND_CHECKBOX_CLICKED); |
| 820 | clickevent.SetEventObject( this ); |
| 821 | clickevent.SetId(event.GetId()-ID_DICE1 + ID_DICE1KEEP); |
| 822 | this->OnKeepClick(clickevent); ///\todo find a better way to call the event handler. |
| 823 | } |
| 824 | |
| 825 | void MainFrame::OnKeepClick (wxCommandEvent& event) |
| 826 | { |
| 827 | wxCheckBox *temp = (wxCheckBox*) FindWindow(event.GetId()); |
| 828 | ((wxDynamicBitmap*) FindWindow(event.GetId()-ID_DICE1KEEP + ID_DICE1))->SetGrayScale(temp->GetValue()); |
| 829 | } |
| 830 | |
| 831 | |
| 832 | //******************************************** |
| 833 | //****** General Functions ****** |
| 834 | //******************************************** |
| 835 | |
| 836 | /** |
| 837 | * This function handles everything related to reseting the dice rolls after scoring. |
| 838 | */ |
| 839 | void MainFrame::ResetRolls() |
| 840 | { |
| 841 | m_rollsundo = m_rolls; |
| 842 | m_rolls = 3; |
| 843 | ((wxButton*) FindWindow(ID_ROLL)) -> Enable(true); |
| 844 | |
| 845 | // reset the roll count in the caption |
| 846 | FindWindow(ID_ROLL)->SetLabel(wxT("Roll! (3)")); |
| 847 | for (int i=0; i<5; i++){ |
| 848 | ((wxCheckBox*) FindWindow(i + ID_DICE1KEEP)) -> SetValue(false); |
| 849 | ((wxCheckBox*) FindWindow(i + ID_DICE1KEEP)) -> Enable(false); |
| 850 | ((wxDynamicBitmap*) FindWindow(i+ID_DICE1)) -> SetGrayScale(false); |
| 851 | } |
| 852 | } |
| 853 | |
| 854 | /** |
| 855 | * This function checks for a Yahtzee Bonus situation and if one exists it scores accordingly. |
| 856 | */ |
| 857 | void MainFrame::YahtzeeBonus() |
| 858 | { |
| 859 | wxString tempstr; |
| 860 | |
| 861 | //if the player didn't have any yathzees yet he can't have the bonus; |
| 862 | if (!m_yahtzee) |
| 863 | return; |
| 864 | if (m_score_dice.IsYahtzee()) { |
| 865 | tempstr = ((wxTextCtrl*) FindWindow(ID_YAHTZEEBONUSTEXT)) -> GetValue(); |
| 866 | tempstr.Printf(wxT("%li"), strtol(tempstr.mb_str(), NULL, 10) + 100); |
| 867 | ((wxTextCtrl*) FindWindow(ID_YAHTZEEBONUSTEXT)) -> SetValue(tempstr); |
| 868 | m_yahtzeebonus = true; |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | /** |
| 873 | * This function checks whether we have a Yahtzee Joker situation. |
| 874 | * \return true if there is Yahtzee Joker, false otherwise. |
| 875 | */ |
| 876 | bool MainFrame::YahtzeeJoker() |
| 877 | { |
| 878 | if (m_score_dice.IsYahtzee() && !(FindWindow(ID_ACES + |
| 879 | m_score_dice.GetDice(1)-1)->IsEnabled()) && |
| 880 | !(FindWindow(ID_YAHTZEE)->IsEnabled())) { |
| 881 | return true; |
| 882 | } |
| 883 | return false; |
| 884 | } |
| 885 | |
| 886 | /** |
| 887 | * This function handles the end of game. |
| 888 | * |
| 889 | * When a game ends it calculates the total score and submit it to the high |
| 890 | * score list. |
| 891 | */ |
| 892 | void MainFrame::EndofGame() |
| 893 | { |
| 894 | if(m_numofplaysleft>0) |
| 895 | return; |
| 896 | |
| 897 | wxString tempstr; |
| 898 | long upperscore = 0; |
| 899 | long lowerscore = 0; |
| 900 | |
| 901 | for (int i = ID_ACESTEXT; i<=ID_SIXESTEXT; i++){ |
| 902 | tempstr = ((wxTextCtrl*) FindWindow(i)) -> GetValue(); |
| 903 | upperscore += strtol(tempstr.mb_str(), NULL, 10); |
| 904 | } |
| 905 | |
| 906 | tempstr.Printf(wxT("%li"),upperscore); |
| 907 | ((wxTextCtrl*) FindWindow(ID_UPPERSECTIONTOTAL)) -> SetValue(tempstr); |
| 908 | |
| 909 | //check for bonus |
| 910 | if(upperscore>=63) { |
| 911 | ((wxTextCtrl*) FindWindow(ID_BONUS)) -> SetValue(wxT("35")); |
| 912 | upperscore +=35; |
| 913 | } else |
| 914 | ((wxTextCtrl*) FindWindow(ID_BONUS)) -> SetValue(wxT("0")); |
| 915 | |
| 916 | tempstr.Printf(wxT("%li"),upperscore); |
| 917 | ((wxTextCtrl*) FindWindow(ID_UPPERTOTAL)) -> SetValue(tempstr); |
| 918 | |
| 919 | //calculate total on lower section |
| 920 | for (int i = ID_THREEOFAKINDTEXT; i<=ID_YAHTZEEBONUSTEXT; i++) { |
| 921 | tempstr = ((wxTextCtrl*) FindWindow(i)) -> GetValue(); |
| 922 | lowerscore += strtol(tempstr.mb_str(), NULL, 10); |
| 923 | } |
| 924 | |
| 925 | tempstr.Printf(wxT("%li"),lowerscore); |
| 926 | ((wxTextCtrl*) FindWindow(ID_LOWERTOTAL)) -> SetValue(tempstr); |
| 927 | tempstr.Printf(wxT("%li"),upperscore + lowerscore); |
| 928 | ((wxTextCtrl*) FindWindow(ID_GRANDTOTAL)) -> SetValue(tempstr); |
| 929 | |
| 930 | //disable the roll button; |
| 931 | ((wxButton*) FindWindow(ID_ROLL)) -> Enable(false); |
| 932 | |
| 933 | tempstr.Printf(wxT("Your final score is %li points!"),lowerscore+upperscore); |
| 934 | wxMessageBox(tempstr, wxT("Game Ended"), wxOK | wxICON_INFORMATION, this); |
| 935 | |
| 936 | //submit to high score |
| 937 | HighScoreHandler(lowerscore+upperscore); |
| 938 | |
| 939 | } |
| 940 | |
| 941 | /** |
| 942 | * This function checks if a given score qualifies for the high score list and |
| 943 | * adds it to the list if it does. |
| 944 | * @param score The score submitted to the high score list. |
| 945 | */ |
| 946 | void MainFrame::HighScoreHandler(int score) |
| 947 | { |
| 948 | std::string name,date; |
| 949 | wxCommandEvent newevent; |
| 950 | |
| 951 | |
| 952 | if(!m_config->isHighscore(score)) { |
| 953 | return; |
| 954 | } |
| 955 | |
| 956 | wxString msg = wxT("Your score made it to the high score table.\nPlease enter your name below:"); |
| 957 | |
| 958 | wxString last_name = wxString::FromUTF8(m_config->get("last-name").c_str()); |
| 959 | |
| 960 | wxTextEntryDialog infodialog(this,msg,wxT("Please enter your name"),last_name ,wxOK | wxCENTRE); |
| 961 | infodialog.ShowModal(); |
| 962 | |
| 963 | name = infodialog.GetValue().utf8_str(); |
| 964 | m_config->set("last-name",name)->save(); |
| 965 | |
| 966 | //get the date |
| 967 | wxDateTime now = wxDateTime::Now(); |
| 968 | date = now.FormatDate().utf8_str(); |
| 969 | date += " "; |
| 970 | date += now.FormatISOTime().SubString(0,4).utf8_str(); |
| 971 | |
| 972 | int rank = m_config->submitHighscore(score, name, date); |
| 973 | |
| 974 | //now show the high score table |
| 975 | highscores_dialog::HighscoresDialog *dialog = new highscores_dialog::HighscoresDialog(this, m_config.get(),rank); |
| 976 | |
| 977 | dialog->ShowModal(); |
| 978 | |
| 979 | delete dialog; |
| 980 | |
| 981 | } |
| 982 | |
| 983 | ///this function handles all the post scoring stuff such as disabling the right button. |
| 984 | /** |
| 985 | * This function is always called after scoring and it handles all the post-score |
| 986 | * stuff such as reseting the dice rolls, enabling the undo button, calculating |
| 987 | * the sub-total scores and ending the game if necessary. |
| 988 | * \param id |
| 989 | */ |
| 990 | void MainFrame::PostScore(int id) |
| 991 | { |
| 992 | //now after the scoring reset the rolls |
| 993 | ResetRolls(); |
| 994 | |
| 995 | CalculateSubTotal(); |
| 996 | |
| 997 | //and disable the button |
| 998 | FindWindow(id)->Enable(false); |
| 999 | m_numofplaysleft--; |
| 1000 | EnableUndo(id); |
| 1001 | EndofGame(); |
| 1002 | } |
| 1003 | |
| 1004 | /** |
| 1005 | * This function calculates the sub-total scores and should be called after |
| 1006 | * every score. It does so only if this feature is requested in the settings |
| 1007 | * dialog. |
| 1008 | */ |
| 1009 | void MainFrame::CalculateSubTotal() |
| 1010 | { |
| 1011 | if (m_config->get("calculate-subtotal")=="False") |
| 1012 | return; |
| 1013 | long upperscore = 0; |
| 1014 | long lowerscore = 0; |
| 1015 | wxString tempstr; |
| 1016 | |
| 1017 | for (int i = ID_ACESTEXT; i<=ID_SIXESTEXT; i++){ |
| 1018 | tempstr = ((wxTextCtrl*) FindWindow(i)) -> GetValue(); |
| 1019 | upperscore += strtol(tempstr.mb_str(), NULL, 10); |
| 1020 | } |
| 1021 | |
| 1022 | tempstr.Printf(wxT("%li"),upperscore); |
| 1023 | ((wxTextCtrl*) FindWindow(ID_UPPERSECTIONTOTAL)) -> SetValue(tempstr); |
| 1024 | if (upperscore >= 63) { |
| 1025 | ((wxTextCtrl*) FindWindow(ID_BONUS))->SetValue(wxT("35")); |
| 1026 | } else { |
| 1027 | // this is needed in case of an undo |
| 1028 | ((wxTextCtrl*) FindWindow(ID_BONUS))->SetValue(wxT("")); |
| 1029 | } |
| 1030 | |
| 1031 | for (int i = ID_THREEOFAKINDTEXT; i<=ID_YAHTZEEBONUSTEXT; i++) { |
| 1032 | tempstr = ((wxTextCtrl*) FindWindow(i)) -> GetValue(); |
| 1033 | lowerscore += strtol(tempstr.mb_str(), NULL, 10); |
| 1034 | } |
| 1035 | |
| 1036 | tempstr.Printf(wxT("%li"),lowerscore); |
| 1037 | ((wxTextCtrl*) FindWindow(ID_LOWERTOTAL)) -> SetValue(tempstr); |
| 1038 | } |
| 1039 | |
| 1040 | |
| 1041 | |
| 1042 | void MainFrame::Relayout() |
| 1043 | { |
| 1044 | auto topSizer = (wxBoxSizer*)FindWindow(ID_PANEL)->GetSizer(); |
| 1045 | |
| 1046 | if (m_config->get("horizontal-layout")=="True") { |
| 1047 | topSizer->SetOrientation(wxVERTICAL); |
| 1048 | sectionsSizer->SetOrientation(wxHORIZONTAL); |
| 1049 | diceSizer->SetOrientation(wxHORIZONTAL); |
| 1050 | } else { |
| 1051 | topSizer->SetOrientation(wxHORIZONTAL); |
| 1052 | sectionsSizer->SetOrientation(wxVERTICAL); |
| 1053 | diceSizer->SetOrientation(wxVERTICAL); |
| 1054 | } |
| 1055 | |
| 1056 | AdjustFrameSize(topSizer); |
| 1057 | std::cerr << diceSizer->GetSize().GetWidth() << std::endl; |
| 1058 | } |
| 1059 | |
| 1060 | /** |
| 1061 | * Check whether the dice should be rolled or if the user already rolled them. |
| 1062 | * \return true if the dice are valid (rolled). |
| 1063 | */ |
| 1064 | bool MainFrame::IsValidDice() |
| 1065 | { |
| 1066 | if ( m_rolls < 3 ) |
| 1067 | return true; |
| 1068 | return false; |
| 1069 | } |
| 1070 | |
| 1071 | /** |
| 1072 | * |
| 1073 | */ |
| 1074 | void MainFrameEvtHandler::OnScoreMouseEnter (wxMouseEvent& event) |
| 1075 | { |
| 1076 | int id; |
| 1077 | wxTextCtrl *text_control; |
| 1078 | wxString out; |
| 1079 | if (m_main_frame->m_config->get("score-hints")=="False") { |
| 1080 | event.Skip(); |
| 1081 | return; |
| 1082 | } |
| 1083 | if (! m_main_frame->IsValidDice()){ |
| 1084 | event.Skip(); |
| 1085 | return; |
| 1086 | } |
| 1087 | |
| 1088 | id = ((wxWindow *)event.GetEventObject())->GetId(); |
| 1089 | text_control = ((wxTextCtrl*)m_main_frame->FindWindow(id-ID_ACES+ID_ACESTEXT)); |
| 1090 | switch (id) { |
| 1091 | case ID_ACES: |
| 1092 | out.Printf(wxT("%hi"), m_main_frame->m_score_dice.Aces()); |
| 1093 | break; |
| 1094 | case ID_TWOS: |
| 1095 | out.Printf(wxT("%hi"), m_main_frame->m_score_dice.Twos()); |
| 1096 | break; |
| 1097 | case ID_THREES: |
| 1098 | out.Printf(wxT("%hi"), m_main_frame->m_score_dice.Threes()); |
| 1099 | break; |
| 1100 | case ID_FOURS: |
| 1101 | out.Printf(wxT("%hi"), m_main_frame->m_score_dice.Fours()); |
| 1102 | break; |
| 1103 | case ID_FIVES: |
| 1104 | out.Printf(wxT("%hi"), m_main_frame->m_score_dice.Fives()); |
| 1105 | break; |
| 1106 | case ID_SIXES: |
| 1107 | out.Printf(wxT("%hi"), m_main_frame->m_score_dice.Sixes()); |
| 1108 | break; |
| 1109 | case ID_THREEOFAKIND: |
| 1110 | out.Printf(wxT("%hi"), m_main_frame->m_score_dice.ThreeOfAKind()); |
| 1111 | break; |
| 1112 | case ID_FOUROFAKIND: |
| 1113 | out.Printf(wxT("%hi"), m_main_frame->m_score_dice.FourOfAKind()); |
| 1114 | break; |
| 1115 | case ID_FULLHOUSE: |
| 1116 | out.Printf(wxT("%hi"), m_main_frame->m_score_dice.FullHouse()); |
| 1117 | break; |
| 1118 | case ID_SMALLSEQUENCE: |
| 1119 | out.Printf(wxT("%hi"), m_main_frame->m_score_dice.SmallSequence()); |
| 1120 | break; |
| 1121 | case ID_LARGESEQUENCE: |
| 1122 | out.Printf(wxT("%hi"), m_main_frame->m_score_dice.LargeSequence()); |
| 1123 | break; |
| 1124 | case ID_YAHTZEE: |
| 1125 | out.Printf(wxT("%hi"), m_main_frame->m_score_dice.Yahtzee()); |
| 1126 | break; |
| 1127 | case ID_CHANCE: |
| 1128 | out.Printf(wxT("%hi"), m_main_frame->m_score_dice.Chance()); |
| 1129 | break; |
| 1130 | } |
| 1131 | text_control->SetValue(out); |
| 1132 | wxColour sys_gray = wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT); |
| 1133 | text_control->SetForegroundColour(sys_gray); |
| 1134 | |
| 1135 | event.Skip(); //allow default proccesing |
| 1136 | } |
| 1137 | |
| 1138 | /** |
| 1139 | * |
| 1140 | */ |
| 1141 | void MainFrameEvtHandler::OnScoreMouseLeave (wxMouseEvent& event) |
| 1142 | { |
| 1143 | //we dont have the id of the control that generated the event |
| 1144 | wxTextCtrl *text_control; |
| 1145 | |
| 1146 | if (! m_main_frame->IsValidDice()){ |
| 1147 | event.Skip(); |
| 1148 | return; |
| 1149 | } |
| 1150 | |
| 1151 | auto id = ((wxWindow *)event.GetEventObject())->GetId(); |
| 1152 | text_control = ((wxTextCtrl*)m_main_frame->FindWindow(id - ID_ACES + ID_ACESTEXT)); |
| 1153 | wxColour sys_gray = wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT); |
| 1154 | if (text_control->GetForegroundColour() == sys_gray){ |
| 1155 | text_control->Clear(); |
| 1156 | text_control->SetForegroundColour(wxNullColour); |
| 1157 | } |
| 1158 | |
| 1159 | event.Skip(); //allow default proccesing |
| 1160 | } |
| 1161 | |
| 1162 | bool MainFrame::IsValidClick() |
| 1163 | { |
| 1164 | if (m_rolls >= 3) { |
| 1165 | wxMessageBox(wxT("First you need to roll, and after you roll you may score."), |
| 1166 | wxT("Open Yahtzee"), wxOK | wxICON_INFORMATION, this); |
| 1167 | return false; |
| 1168 | } |
| 1169 | return true; |
| 1170 | } |
| 1171 | |
| 1172 | MainFrame::~MainFrame() { |
| 1173 | // free pointers |
| 1174 | delete m_evt_handler; |
| 1175 | |
| 1176 | for (int i = 0; i<6; i++) { |
| 1177 | delete bitmap_dice[i]; |
| 1178 | } |
| 1179 | } |