1 /*=============================================================================
2 Blobby Volley 2
3 Copyright (C) 2006 Jonathan Sieber (jonathan_sieber@yahoo.de)
4 Copyright (C) 2006 Daniel Knobe (daniel-knobe@web.de)
5 
6 This program 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 2 of the License, or
9 (at your option) any later version.
10 
11 This program 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 this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 =============================================================================*/
20 
21 /* header include */
22 #include "ReplaySelectionState.h"
23 
24 /* includes */
25 #include <algorithm>
26 #include <ctime>
27 #include <iostream> // for cerr
28 
29 #include <boost/lexical_cast.hpp>
30 
31 #include "ReplayState.h"
32 #include "IMGUI.h"
33 #include "TextManager.h"
34 #include "SpeedController.h"
35 #include "FileSystem.h"
36 #include "IReplayLoader.h"
37 
38 
39 /* implementation */
ReplaySelectionState()40 ReplaySelectionState::ReplaySelectionState()
41 {
42 	mChecksumError = false;
43 	mVersionError = false;
44 	mShowReplayInfo = false;
45 
46 	mSelectedReplay = 0;
47 	mReplayFiles = FileSystem::getSingleton().enumerateFiles("replays", ".bvr");
48 	if (mReplayFiles.size() == 0)
49 		mSelectedReplay = -1;
50 	std::sort(mReplayFiles.rbegin(), mReplayFiles.rend());
51 
52 	SpeedController::getMainInstance()->setGameSpeed(75);
53 }
54 
step_impl()55 void ReplaySelectionState::step_impl()
56 {
57 	IMGUI& imgui = IMGUI::getSingleton();
58 
59 
60 	imgui.doCursor();
61 	imgui.doImage(GEN_ID, Vector2(400.0, 300.0), "background");
62 	imgui.doOverlay(GEN_ID, Vector2(0.0, 0.0), Vector2(800.0, 600.0));
63 
64 	if (imgui.doButton(GEN_ID, Vector2(224.0, 10.0), TextManager::RP_PLAY) &&
65 				mSelectedReplay != -1)
66 	{
67 		std::string loadrep = mReplayFiles[mSelectedReplay];
68 
69 		/// \todo we have to do something against this construction!
70 		///		this is dangerous. we delete this state before it has done
71 		///		all of its work.
72 
73 		ReplayState* rs = 0;
74 		try
75 		{
76 			rs = new ReplayState();
77 			rs->loadReplay(loadrep);
78 
79 			imgui.resetSelection();
80 			// at least make sure we end here!
81 
82 			switchState(rs);
83 			return;
84 		}
85 		 catch (std::exception& exp)
86 		{
87 			delete rs;
88 			std::cerr << exp.what() << "\n";
89 		}
90 		return;
91 	}
92 	else if (imgui.doButton(GEN_ID, Vector2(424.0, 10.0), TextManager::LBL_CANCEL))
93 	{
94 		switchState(new MainMenuState());
95 		return;
96 	}
97 	else
98 		imgui.doSelectbox(GEN_ID, Vector2(34.0, 50.0), Vector2(634.0, 550.0), mReplayFiles, mSelectedReplay);
99 
100 	if (imgui.doButton(GEN_ID, Vector2(644.0, 60.0), TextManager::RP_INFO))
101 	{
102 		if (!mReplayFiles.empty())
103 		{
104 			try
105 			{
106 				mReplayLoader.reset(IReplayLoader::createReplayLoader(std::string("replays/" + mReplayFiles[mSelectedReplay] + ".bvr")));
107 				mShowReplayInfo = true;
108 			}
109 			catch (std::exception& e)
110 			{
111 				std::cerr << e.what() << std::endl;
112 			}
113 		}
114 	}
115 	if (imgui.doButton(GEN_ID, Vector2(644.0, 95.0), TextManager::RP_DELETE))
116 	{
117 		if (!mReplayFiles.empty())
118 		if (FileSystem::getSingleton().deleteFile("replays/" + mReplayFiles[mSelectedReplay] + ".bvr"))
119 		{
120 			mReplayFiles.erase(mReplayFiles.begin()+mSelectedReplay);
121 			if (mSelectedReplay >= mReplayFiles.size())
122 				mSelectedReplay = mReplayFiles.size()-1;
123 		}
124 	}
125 
126 	if(mShowReplayInfo)
127 	{
128 		// setup
129 		std::string left =  mReplayLoader->getPlayerName(LEFT_PLAYER);
130 		std::string right =  mReplayLoader->getPlayerName(RIGHT_PLAYER);
131 
132 		const int MARGIN = std::min(std::max(int(300 - 24*(std::max(left.size(),right.size()))), 50), 150);
133 
134 		const int RIGHT = 800 - MARGIN;
135 		imgui.doInactiveMode(false);
136 		imgui.doOverlay(GEN_ID, Vector2(MARGIN, 180), Vector2(800-MARGIN, 445));
137 		std::string repname = mReplayFiles[mSelectedReplay];
138 		imgui.doText(GEN_ID, Vector2(400-repname.size()*12, 190), repname);
139 
140 		// calculate text positions
141 
142 
143 		imgui.doText(GEN_ID, Vector2(MARGIN + 20, 225), left);
144 		imgui.doText(GEN_ID, Vector2(400-24, 225), "vs");
145 		imgui.doText(GEN_ID, Vector2(RIGHT - 20 - 24*right.size(), 225), right);
146 
147 		time_t rd = mReplayLoader->getDate();
148 		struct tm* ptm;
149 		ptm = gmtime ( &rd );
150 		//std::
151 		char buffer[255];
152 		std::strftime(buffer, sizeof(buffer), "%d.%m.%Y - %H:%M", ptm);
153 		std::string date = buffer;
154 		imgui.doText(GEN_ID, Vector2(400 - 12*date.size(), 255), date);
155 
156 		imgui.doText(GEN_ID, Vector2(MARGIN+20, 300), TextManager::OP_SPEED);
157 		std::string speed = boost::lexical_cast<std::string>(mReplayLoader->getSpeed() *100 / 75) + "%" ;
158 		imgui.doText(GEN_ID, Vector2(RIGHT - 20 - 24*speed.size(), 300), speed);
159 
160 		imgui.doText(GEN_ID, Vector2(MARGIN+20, 335), TextManager::RP_DURATION);
161 		std::string dur;
162 		if(mReplayLoader->getDuration() > 99)
163 		{
164 			// +30 because of rounding
165 			dur = boost::lexical_cast<std::string>((mReplayLoader->getDuration() + 30) / 60) + "min";
166 		} else
167 		{
168 			dur = boost::lexical_cast<std::string>(mReplayLoader->getDuration()) + "s";
169 		}
170 		imgui.doText(GEN_ID, Vector2(RIGHT - 20 - 24*dur.size(), 335), dur);
171 
172 		std::string res;
173 		res = boost::lexical_cast<std::string>(mReplayLoader->getFinalScore(LEFT_PLAYER)) + " : " +  boost::lexical_cast<std::string>(mReplayLoader->getFinalScore(RIGHT_PLAYER));
174 
175 		imgui.doText(GEN_ID, Vector2(MARGIN+20, 370), TextManager::RP_RESULT);
176 		imgui.doText(GEN_ID, Vector2(RIGHT - 20 - 24*res.size(), 370), res);
177 
178 		if (imgui.doButton(GEN_ID, Vector2(400-24, 410), TextManager::LBL_OK))
179 		{
180 			mShowReplayInfo = false;
181 		}
182 		else
183 		{
184 			imgui.doInactiveMode(true);
185 		}
186 	}
187 
188 	if (mChecksumError)
189 	{
190 		imgui.doInactiveMode(false);
191 		imgui.doOverlay(GEN_ID, Vector2(210, 180), Vector2(650, 370));
192 		imgui.doText(GEN_ID, Vector2(250, 200), TextManager::RP_CHECKSUM);
193 		imgui.doText(GEN_ID, Vector2(250, 250), TextManager::RP_FILE_CORRUPT);
194 
195 		if (imgui.doButton(GEN_ID, Vector2(400, 330), TextManager::LBL_OK))
196 		{
197 			mChecksumError = false;
198 		}
199 		else
200 		{
201 			imgui.doInactiveMode(true);
202 		}
203 	}
204 
205 	if (mVersionError)
206 	{
207 		imgui.doInactiveMode(false);
208 		imgui.doOverlay(GEN_ID, Vector2(210, 180), Vector2(650, 370));
209 		imgui.doText(GEN_ID, Vector2(250, 200), TextManager::RP_VERSION);
210 		imgui.doText(GEN_ID, Vector2(250, 250), TextManager::RP_FILE_OUTDATED);
211 
212 		if (imgui.doButton(GEN_ID, Vector2(400, 330), TextManager::LBL_OK))
213 		{
214 			mVersionError = false;
215 		}
216 		else
217 		{
218 			imgui.doInactiveMode(true);
219 		}
220 	}
221 }
222 
getStateName() const223 const char* ReplaySelectionState::getStateName() const
224 {
225 	return "ReplaySelectionState";
226 }
227 
228