Ignore:
Timestamp:
05/01/2009 08:05:17 PM (3 years ago)
Author:
guyru
Message:

Re-factor the accidental roll prevention.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/MainFrame.cpp

    r193 r194  
    7070 
    7171using namespace main_frame; 
    72 const wxEventType wxEVT_ENABLE_ROLL = wxNewEventType(); 
    7372 
    7473MainFrame::MainFrame(const wxString& title, const wxSize& size = wxDefaultSize, long style = wxDEFAULT_FRAME_STYLE) 
     
    129128        m_yahtzeebonus = false; 
    130129        m_numofplaysleft = 13; 
    131         m_skiproll = false; // the roll button hasn't been pressed yet, no need for skipping 
    132130} 
    133131 
     
    352350        //END connecting the menu items' events 
    353351 
    354         Connect(ID_ROLL, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (MainFrame::OnRollButton)); 
    355         Connect(ID_ROLL, wxEVT_ENABLE_ROLL, wxCommandEventHandler (MainFrame::OnRollButton)); 
     352        Connect(ID_ROLL, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (MainFrame::DoubleRollLock)); 
    356353 
    357354        //BEGIN connecting the scoreboard buttons to the events 
     
    557554 * \param event  
    558555 */ 
    559 void MainFrame::OnRollButton (wxCommandEvent& event) 
     556void MainFrame::OnRollButton () 
    560557{ 
    561558        short int dice[5];      //holds the dices score 
    562559         
    563         if (event.GetEventType() == wxEVT_IDLE) { 
    564                 m_skiproll = false; 
    565                 Disconnect(wxEVT_IDLE,  wxCommandEventHandler(MainFrame::OnRollButton)); 
    566                 return; 
    567         } 
    568          
    569         //skip rolling the dice if the user accidently rolled the dice before they finished spinning. 
    570         if (m_skiproll) return; 
    571         m_skiproll = true; 
    572  
    573560        if (m_numofplaysleft == 13) 
    574561                m_stats->game_started(); 
     
    633620        (GetMenuBar()->FindItem(wxID_UNDO))->Enable(false); 
    634621        m_yahtzeebonus = false; //if we scored yahtzee bonus before we don't care anymore. 
    635          
    636         Connect(wxEVT_IDLE, wxCommandEventHandler(MainFrame::OnRollButton)); 
     622} 
     623 
     624void MainFrame::DoubleRollLock(wxCommandEvent& event) 
     625{ 
     626        /* The function works by connecting to idle event, and raising a 
     627         * flag when it process a valid click on the Roll button. When the 
     628         * another click happens and the flag is up, it means it is 
     629         * accidental. When the idle events is triggered it means that we 
     630         * no longer roll the dice, so we lower the flag. 
     631         */ 
     632        static bool skip_roll = false; 
     633        if (event.GetEventType() == wxEVT_IDLE) { 
     634                skip_roll = false; 
     635                Disconnect(wxEVT_IDLE,  wxCommandEventHandler(MainFrame::DoubleRollLock)); 
     636                return; 
     637        } 
     638         
     639        if (skip_roll) 
     640                return; 
     641        skip_roll = true; 
     642         
     643        Connect(wxEVT_IDLE, wxCommandEventHandler(MainFrame::DoubleRollLock)); 
     644        OnRollButton(); 
    637645} 
    638646 
Note: See TracChangeset for help on using the changeset viewer.