www.pudn.com > uoth_src.zip > AddRemoveMapDlg.cpp
//----------------------------------------------------------------------------- // // @doc // // @module AddRemoveMapDlg.cpp - Add or remove a map dialog | // // This module contains the definition of the add or remove a map dialog. // // Copyright (c) 2002 - Descartes Systems Sciences, Inc. // // All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are // met: // // 1. Redistributions of source code must retain the above copyright notice, // this list of conditions and the following disclaimer. // 2. Neither the name of Descartes Systems Sciences, Inc nor the names of // its contributors may be used to endorse or promote products derived // from this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // // @end // // $History: AddRemoveMapDlg.cpp $ // //----------------------------------------------------------------------------- #include "stdafx.h" #include "resource.h" #include "AddRemoveMapDlg.h" #include "uoth.h" #include "Treasure.h" #include "Level.h" #include "Character.h" // // Externals // extern TCHAR g_szAppName []; extern TCHAR g_szSettingsFile []; //----------------------------------------------------------------------------- // // @mfuncconstructor. // // @parm int | nTreasure | Treasure number // // @parm bool | fAdd | If true, we are adding // // @rdesc None. // //----------------------------------------------------------------------------- CAddRemoveMapDlg::CAddRemoveMapDlg (int nTreasure, bool fAdd) { m_nTreasure = nTreasure; m_fAdd = fAdd; } //----------------------------------------------------------------------------- // // @mfunc destructor. // // @rdesc None. // //----------------------------------------------------------------------------- CAddRemoveMapDlg::~CAddRemoveMapDlg() { } //----------------------------------------------------------------------------- // // @mfunc Initialize the dialog // // @parm UINT | uMsg | Message // // @parm WPARAM | wParam | Message w-parameter // // @parm LPARAM | lParam | Message l-parameter // // @parm BOOL & | bHandled | If handled, set to true // // @rdesc Routine results // //----------------------------------------------------------------------------- LRESULT CAddRemoveMapDlg::OnInitDialog (UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled) { // // Subclass the controls // m_cbCharacter = GetDlgItem (IDC_CHARACTER); m_cbLevel = GetDlgItem (IDC_LEVEL); m_cbTreasure = GetDlgItem (IDC_TREASURE); m_stCurrent = GetDlgItem (IDC_CURRENT); // // Center the window // CenterWindow (); // // Populate the level combo box // for (int il = 0; il < CLevel::gm_vLevels .GetCount (); ++il) { CLevel *pLevel = &CLevel::gm_vLevels [il]; if (!pLevel ->IsValid ()) continue; TCHAR szText [256]; _sntprintf (szText, _countof (szText), _T ( "%d - %s"), pLevel ->m_nIndex, (LPCTSTR) pLevel ->m_strName); m_cbLevel .AddString (szText); } // // Select a level // int nLevel = ::GetPrivateProfileInt (PROFILE_SETTINGS, PROFILE_LASTLEVEL, 0, g_szSettingsFile); if (nLevel != 0) { TCHAR szText [32]; _sntprintf (szText, _countof (szText), _T ( "%d - "), nLevel); int nSel = m_cbLevel .FindString (-1, szText); if (nSel < 0) nSel = 0; m_cbLevel .SetCurSel (nSel); } else m_cbLevel .SetCurSel (0); // // Populate the character // for (int ic = 0; ic < CCharacter::gm_vCharacters .GetCount (); ic++) { m_cbCharacter .AddString ( CCharacter::gm_vCharacters [ic] .m_strName); } // // Select the character // CCharacter *pCharacter = CCharacter::GetCurrent (); if (pCharacter != NULL) { int nExact = m_cbCharacter .FindStringExact ( -1, pCharacter ->m_strName); if (nExact >= 0) m_cbCharacter .SetCurSel (nExact); else assert (false); } m_cbCharacter .LimitText (128); // // Populate the treasure combo box // for (int it = 0; it < CTreasure::gm_vTreasures .GetCount (); ++it) { CTreasure *pTreasure = &CTreasure::gm_vTreasures [it]; if (!pTreasure ->IsValid ()) continue; TCHAR szText [256]; _sntprintf (szText, _countof (szText), _T ( "%d - %s"), pTreasure ->m_nIndex, (LPCTSTR) pTreasure ->m_strName); m_cbTreasure .AddString (szText); } // // Select a treasure // if (m_nTreasure > 0) { TCHAR szText [32]; _sntprintf (szText, _countof (szText), _T ( "%d - "), m_nTreasure); int nSel = m_cbTreasure .FindString (-1, szText); if (nSel < 0) nSel = 0; m_cbTreasure .SetCurSel (nSel); } else m_cbTreasure .SetCurSel (0); // // Update the title bar // TCHAR szText [256]; ::LoadString (_Module .GetResourceInstance (), m_fAdd ? ID_EDIT_I_JUST_GOT : ID_EDIT_I_JUST_COMPLETED, szText, _countof (szText)); TCHAR szText2 [256]; ::ExtractString (szText2, _countof (szText2), szText, 0, '\n'); SetWindowText (szText2); // // Update the current setting // UpdateCurrent (); return TRUE; } //----------------------------------------------------------------------------- // // @mfunc Handle the user press Ok // // @parm WORD | wNotifyCode | Command notification code // // @parm WORD | wID | ID of the control // // @parm HWND | hWndCtl | Handle of the control // // @parm BOOL & | bHandled | If handled, set to true // // @rdesc Routine results // //----------------------------------------------------------------------------- LRESULT CAddRemoveMapDlg::OnOk (WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL &bHandled) { // // Get the character name // CString strCharacter; m_cbCharacter .GetWindowText (strCharacter); // // Get the level // int nLevel = m_cbLevel .GetCurSel (); CString strLevel; m_cbLevel .GetLBText (nLevel, strLevel); nLevel = atol (strLevel); // // Get the treasure // int nTreasure = m_cbTreasure .GetCurSel (); CString strTreasure; m_cbTreasure .GetLBText (nTreasure, strTreasure); nTreasure = atol (strTreasure); // // If we don't have a user, then error // strCharacter .TrimLeft (); strCharacter .TrimRight (); if (strCharacter .IsEmpty ()) { CString str (MAKEINTRESOURCE (IDS_ERR_SPECIFY_CHARACTER_NAME)); ::MessageBox (m_hWnd, str, g_szAppName, MB_OK); return 0; } if (!IsValidName (strCharacter)) { CString str (MAKEINTRESOURCE (IDS_ERR_INVALID_CHARACTER_NAME)); ::MessageBox (m_hWnd, str, g_szAppName, MB_OK); return TRUE; } // // Find the character // CCharacter *pCharacter = CCharacter::Find (strCharacter); if (pCharacter == NULL) { CString str (MAKEINTRESOURCE (IDS_ERR_CREATE_CHARACTER_PROMPT)); int nRetVal = ::MessageBox (m_hWnd, str, g_szAppName, MB_YESNO); if (nRetVal == IDNO) return 0; } // // Add the character // pCharacter = CCharacter::Add (strCharacter); CCharacter::SetCurrent (pCharacter); // // Adjust the count // pCharacter ->AdjustCount (nTreasure, nLevel, m_fAdd); // // Save the last selected level // TCHAR szText [32]; _sntprintf (szText, _countof (szText), _T ( "%d"), nLevel); ::WritePrivateProfileString (PROFILE_SETTINGS, PROFILE_LASTLEVEL, szText, g_szSettingsFile); // // End the dialog // EndDialog (IDOK); return TRUE; } //----------------------------------------------------------------------------- // // @mfunc Update the setting for current // // @rdesc None. // //----------------------------------------------------------------------------- void CAddRemoveMapDlg::UpdateCurrent () { bool fGotOne = false; CString str; // // Get the treasure // int nTreasure = m_cbTreasure .GetCurSel (); CString strTreasure; m_cbTreasure .GetLBText (nTreasure, strTreasure); nTreasure = atol (strTreasure); // // Get the character name // CString strCharacter; m_cbCharacter .GetWindowText (strCharacter); // // If we don't have a user, then error // strCharacter .TrimLeft (); strCharacter .TrimRight (); if (strCharacter .IsEmpty ()) goto we_dont_got_nothing; // // If the specified user hasn't been created, return // CCharacter *pCharacter = CCharacter::Find (strCharacter); if (pCharacter == NULL) goto we_dont_got_nothing; // // Get the count // CMapCounts *pCounts = pCharacter ->FindMapCounts (nTreasure); if (pCounts == NULL) goto we_dont_got_nothing; // // Add the maps // for (int i = 0; i < Max_Level; i++) { if (pCounts ->m_anCounts [i] > 0) { if (fGotOne) str += _T ( ", "); fGotOne = true; TCHAR szText [64]; _sntprintf (szText, _countof (szText), _T ( "L%d=%d"), i + 1, pCounts ->m_anCounts [i]); str += szText; } } // // If we don't have anything, add default text // we_dont_got_nothing:; if (!fGotOne) { CString strNone (MAKEINTRESOURCE (IDS_PAREN_NONE)); str += strNone; } // // Update the control // m_stCurrent .SetWindowText (str); }