1 /* 2 Copyright (C) 2010-2014 Kristian Duske 3 4 This file is part of TrenchBroom. 5 6 TrenchBroom is free software: you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation, either version 3 of the License, or 9 (at your option) any later version. 10 11 TrenchBroom is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with TrenchBroom. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 #include "GameListBox.h" 21 22 #include "StringUtils.h" 23 #include "IO/Path.h" 24 #include "IO/ResourceUtils.h" 25 #include "Model/GameConfig.h" 26 #include "Model/GameFactory.h" 27 #include "View/GameSelectionCommand.h" 28 29 #include <cassert> 30 #include <wx/log.h> 31 32 namespace TrenchBroom { 33 namespace View { GameListBox(wxWindow * parent,const long style)34 GameListBox::GameListBox(wxWindow* parent, const long style) : 35 ImageListBox(parent, wxSize(32, 32), "No Games Found", style) { 36 reloadGameInfos(); 37 Bind(wxEVT_LISTBOX, &GameListBox::OnListBoxChange, this); 38 Bind(wxEVT_LISTBOX_DCLICK, &GameListBox::OnListBoxDoubleClick, this); 39 } 40 selectedGameName() const41 String GameListBox::selectedGameName() const { 42 const Model::GameFactory& gameFactory = Model::GameFactory::instance(); 43 const StringList& gameList = gameFactory.gameList(); 44 45 const int index = GetSelection(); 46 if (index < 0 || index >= static_cast<int>(gameList.size())) 47 return ""; 48 return gameList[static_cast<size_t>(index)]; 49 } 50 selectGame(const int index)51 void GameListBox::selectGame(const int index) { 52 const Model::GameFactory& gameFactory = Model::GameFactory::instance(); 53 const StringList& gameList = gameFactory.gameList(); 54 55 if (index < 0 || index >= static_cast<int>(gameList.size())) 56 return; 57 58 SetSelection(index); 59 } 60 OnListBoxChange(wxCommandEvent & event)61 void GameListBox::OnListBoxChange(wxCommandEvent& event) { 62 if (IsBeingDeleted()) return; 63 64 submitChangeEvent(GAME_SELECTION_CHANGE_EVENT); 65 } 66 OnListBoxDoubleClick(wxCommandEvent & event)67 void GameListBox::OnListBoxDoubleClick(wxCommandEvent& event) { 68 if (IsBeingDeleted()) return; 69 70 submitChangeEvent(GAME_SELECTION_DBLCLICK_EVENT); 71 } 72 reloadGameInfos()73 void GameListBox::reloadGameInfos() { 74 m_gameInfos.clear(); 75 76 const Model::GameFactory& gameFactory = Model::GameFactory::instance(); 77 const StringList& gameList = gameFactory.gameList(); 78 StringList::const_iterator it, end; 79 for (it = gameList.begin(), end = gameList.end(); it != end; ++it) { 80 const String& gameName = *it; 81 82 const IO::Path gamePath = gameFactory.gamePath(gameName); 83 IO::Path iconPath = gameFactory.iconPath(gameName); 84 if (iconPath.isEmpty()) 85 iconPath = IO::Path("DefaultGameIcon.png"); 86 87 Info gameInfo; 88 gameInfo.image = IO::loadImageResource(iconPath); 89 gameInfo.title = gameName; 90 gameInfo.subtitle = gamePath.isEmpty() ? String("Game not found") : gamePath.asString(); 91 92 m_gameInfos.push_back(gameInfo); 93 } 94 95 SetItemCount(m_gameInfos.size()); 96 Refresh(); 97 } 98 image(const size_t n) const99 const wxBitmap& GameListBox::image(const size_t n) const { 100 assert(n < m_gameInfos.size()); 101 return m_gameInfos[n].image; 102 } 103 title(const size_t n) const104 wxString GameListBox::title(const size_t n) const { 105 assert(n < m_gameInfos.size()); 106 return m_gameInfos[n].title; 107 } 108 subtitle(const size_t n) const109 wxString GameListBox::subtitle(const size_t n) const { 110 assert(n < m_gameInfos.size()); 111 return m_gameInfos[n].subtitle; 112 } 113 submitChangeEvent(const wxEventType type)114 void GameListBox::submitChangeEvent(const wxEventType type) { 115 GameSelectionCommand command(type, selectedGameName()); 116 command.SetEventObject(this); 117 command.SetId(GetId()); 118 ProcessEvent(command); 119 } 120 } 121 } 122