1 /***************************************************************************
2  *      Mechanized Assault and Exploration Reloaded Projectfile            *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, write to the                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
18  ***************************************************************************/
19 
20 #include <functional>
21 
22 #include "ui/graphical/menu/windows/windowmain.h"
23 #include "pcx.h"
24 #include "main.h"
25 #include "game/data/units/building.h"
26 #include "game/data/units/vehicle.h"
27 #include "utility/random.h"
28 #include "ui/graphical/menu/widgets/label.h"
29 #include "ui/graphical/menu/widgets/image.h"
30 #include "maxrversion.h"
31 
32 //------------------------------------------------------------------------------
cWindowMain(const std::string & title)33 cWindowMain::cWindowMain (const std::string& title) :
34 	cWindow (LoadPCX (GFXOD_MAIN))
35 {
36 	addChild (std::make_unique<cLabel> (cBox<cPosition> (getPosition() + cPosition (0, 147), getPosition() + cPosition (getArea().getMaxCorner().x(), 157)), title, FONT_LATIN_NORMAL, eAlignmentType::CenterHorizontal));
37 
38 	addChild (std::make_unique<cLabel> (cBox<cPosition> (getPosition() + cPosition (0, 465), getPosition() + cPosition (getArea().getMaxCorner().x(), 475)), lngPack.i18n ("Text~Main~Credits_Reloaded") + " " + PACKAGE_VERSION, FONT_LATIN_NORMAL, eAlignmentType::CenterHorizontal));
39 
40 	infoImage = addChild (std::make_unique<cImage> (getPosition() + cPosition (16, 182), getRandomInfoImage(), &SoundData.SNDHudButton));
41 	signalConnectionManager.connect (infoImage->clicked, std::bind (&cWindowMain::infoImageClicked, this));
42 }
43 
44 //------------------------------------------------------------------------------
~cWindowMain()45 cWindowMain::~cWindowMain()
46 {}
47 
48 //------------------------------------------------------------------------------
infoImageClicked()49 void cWindowMain::infoImageClicked()
50 {
51 	infoImage->setImage (getRandomInfoImage());
52 }
53 
54 //------------------------------------------------------------------------------
getRandomInfoImage()55 SDL_Surface* cWindowMain::getRandomInfoImage()
56 {
57 	int const showBuilding = random (3);
58 	// I want 3 possible random numbers since a chance of 50:50 is boring
59 	// (and vehicles are way more cool so I prefer them to be shown) -- beko
60 	static int lastUnitShow = -1;
61 	int unitShow = -1;
62 	SDL_Surface* surface = nullptr;
63 
64 	if (showBuilding == 1 && UnitsData.getNrBuildings() > 0)
65 	{
66 		// that's a 33% chance that we show a building on 1
67 		do
68 		{
69 			unitShow = random (UnitsData.getNrBuildings() - 1);
70 			// make sure we don't show same unit twice
71 		}
72 		while (unitShow == lastUnitShow && UnitsData.getNrBuildings() > 1);
73 		surface = UnitsData.buildingUIs[unitShow].info.get();
74 	}
75 	else if (UnitsData.getNrVehicles() > 0)
76 	{
77 		// and a 66% chance to show a vehicle on 0 or 2
78 		do
79 		{
80 			unitShow = random (UnitsData.getNrVehicles() - 1);
81 			// make sure we don't show same unit twice
82 		}
83 		while (unitShow == lastUnitShow && UnitsData.getNrVehicles() > 1);
84 		surface = UnitsData.vehicleUIs[unitShow].info.get();
85 	}
86 	else surface = nullptr;
87 	lastUnitShow = unitShow; //store shown unit
88 	return surface;
89 }
90