2018-01-05 19:21:07 +02:00
/*
* CCampaignScreen . cpp , part of VCMI engine
*
* Authors : listed in file AUTHORS in main folder
*
* License : GNU General Public License v2 .0 or later
* Full text of license available in license . txt file , in main folder
*
*/
# include "StdInc.h"
# include "CCampaignScreen.h"
2022-09-18 14:47:49 +02:00
# include "CMainMenu.h"
2018-01-05 19:21:07 +02:00
# include "../CGameInfo.h"
# include "../CMusicHandler.h"
# include "../CVideoHandler.h"
# include "../CPlayerInterface.h"
# include "../CServerHandler.h"
# include "../gui/CGuiHandler.h"
2023-04-27 19:21:06 +02:00
# include "../gui/Shortcut.h"
2023-06-02 15:42:18 +02:00
# include "../render/Canvas.h"
2018-01-05 19:21:07 +02:00
# include "../widgets/CComponent.h"
# include "../widgets/Buttons.h"
# include "../widgets/MiscWidgets.h"
# include "../widgets/ObjectLists.h"
# include "../widgets/TextControls.h"
# include "../windows/GUIClasses.h"
# include "../windows/InfoWindows.h"
# include "../windows/CWindowObject.h"
# include "../../lib/filesystem/Filesystem.h"
# include "../../lib/CGeneralTextHandler.h"
# include "../../lib/CArtHandler.h"
# include "../../lib/CBuildingHandler.h"
# include "../../lib/spells/CSpellHandler.h"
# include "../../lib/CSkillHandler.h"
# include "../../lib/CTownHandler.h"
# include "../../lib/CHeroHandler.h"
# include "../../lib/CCreatureHandler.h"
2023-06-25 21:28:24 +02:00
# include "../../lib/campaign/CampaignHandler.h"
2018-01-05 19:21:07 +02:00
# include "../../lib/mapping/CMapService.h"
# include "../../lib/mapObjects/CGHeroInstance.h"
2023-09-20 03:13:54 +02:00
CCampaignScreen : : CCampaignScreen ( const JsonNode & config , std : : string name )
: CWindowObject ( BORDERED ) , campaignSet ( name )
2018-01-05 19:21:07 +02:00
{
OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE ;
2023-09-20 03:13:54 +02:00
for ( const JsonNode & node : config [ name ] [ " images " ] . Vector ( ) )
2018-01-05 19:21:07 +02:00
images . push_back ( CMainMenu : : createPicture ( node ) ) ;
if ( ! images . empty ( ) )
{
images [ 0 ] - > center ( ) ; // move background to center
moveTo ( images [ 0 ] - > pos . topLeft ( ) ) ; // move everything else to center
images [ 0 ] - > moveTo ( pos . topLeft ( ) ) ; // restore moved twice background
pos = images [ 0 ] - > pos ; // fix height\width of this window
}
2023-09-20 03:13:54 +02:00
if ( ! config [ name ] [ " exitbutton " ] . isNull ( ) )
2018-01-05 19:21:07 +02:00
{
2023-09-20 03:13:54 +02:00
buttonBack = createExitButton ( config [ name ] [ " exitbutton " ] ) ;
2024-02-27 22:19:09 +02:00
buttonBack - > setHoverable ( true ) ;
2018-01-05 19:21:07 +02:00
}
2023-09-20 03:13:54 +02:00
for ( const JsonNode & node : config [ name ] [ " items " ] . Vector ( ) )
2023-09-20 23:06:32 +02:00
campButtons . push_back ( std : : make_shared < CCampaignButton > ( node , config , campaignSet ) ) ;
2018-01-05 19:21:07 +02:00
}
2023-06-27 19:09:11 +02:00
void CCampaignScreen : : activate ( )
{
2023-09-04 12:03:15 +02:00
CCS - > musich - > playMusic ( AudioPath : : builtin ( " Music/MainMenu " ) , true , false ) ;
2023-06-27 19:09:11 +02:00
CWindowObject : : activate ( ) ;
}
2018-01-05 19:21:07 +02:00
std : : shared_ptr < CButton > CCampaignScreen : : createExitButton ( const JsonNode & button )
{
std : : pair < std : : string , std : : string > help ;
if ( ! button [ " help " ] . isNull ( ) & & button [ " help " ] . Float ( ) > 0 )
2020-10-01 10:38:06 +02:00
help = CGI - > generaltexth - > zelp [ ( size_t ) button [ " help " ] . Float ( ) ] ;
2018-01-05 19:21:07 +02:00
2023-08-23 14:07:50 +02:00
return std : : make_shared < CButton > ( Point ( ( int ) button [ " x " ] . Float ( ) , ( int ) button [ " y " ] . Float ( ) ) , AnimationPath : : fromJson ( button [ " name " ] ) , help , [ = ] ( ) { close ( ) ; } , EShortcut : : GLOBAL_CANCEL ) ;
2018-01-05 19:21:07 +02:00
}
2023-09-20 23:06:32 +02:00
CCampaignScreen : : CCampaignButton : : CCampaignButton ( const JsonNode & config , const JsonNode & parentConfig , std : : string campaignSet )
2023-09-20 21:18:13 +02:00
: campaignSet ( campaignSet )
2018-01-05 19:21:07 +02:00
{
OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE ;
2020-10-01 10:38:06 +02:00
pos . x + = static_cast < int > ( config [ " x " ] . Float ( ) ) ;
pos . y + = static_cast < int > ( config [ " y " ] . Float ( ) ) ;
2018-01-05 19:21:07 +02:00
pos . w = 200 ;
pos . h = 116 ;
campFile = config [ " file " ] . String ( ) ;
2023-09-01 23:57:25 +02:00
video = VideoPath : : fromJson ( config [ " video " ] ) ;
2018-01-05 19:21:07 +02:00
2023-09-20 23:06:32 +02:00
status = CCampaignScreen : : ENABLED ;
2018-01-05 19:21:07 +02:00
2023-06-25 21:28:24 +02:00
auto header = CampaignHandler : : getHeader ( campFile ) ;
2023-09-27 22:53:13 +02:00
hoverText = header - > getNameTranslated ( ) ;
2018-01-05 19:21:07 +02:00
2023-09-21 21:27:06 +02:00
if ( persistentStorage [ " completedCampaigns " ] [ header - > getFilename ( ) ] . Bool ( ) )
2023-09-20 22:18:53 +02:00
status = CCampaignScreen : : COMPLETED ;
2023-09-20 23:06:32 +02:00
for ( const JsonNode & node : parentConfig [ campaignSet ] [ " items " ] . Vector ( ) )
{
for ( const JsonNode & requirement : config [ " requires " ] . Vector ( ) )
{
if ( node [ " id " ] . Integer ( ) = = requirement . Integer ( ) )
2023-09-21 21:27:06 +02:00
if ( ! persistentStorage [ " completedCampaigns " ] [ node [ " file " ] . String ( ) ] . Bool ( ) )
2023-09-20 23:06:32 +02:00
status = CCampaignScreen : : DISABLED ;
}
}
2023-09-21 21:27:06 +02:00
if ( persistentStorage [ " unlockAllCampaigns " ] . Bool ( ) )
status = CCampaignScreen : : ENABLED ;
2018-01-05 19:21:07 +02:00
if ( status ! = CCampaignScreen : : DISABLED )
{
addUsedEvents ( LCLICK | HOVER ) ;
2023-08-23 14:07:50 +02:00
graphicsImage = std : : make_shared < CPicture > ( ImagePath : : fromJson ( config [ " image " ] ) ) ;
2018-01-05 19:21:07 +02:00
2022-11-26 23:12:20 +02:00
hoverLabel = std : : make_shared < CLabel > ( pos . w / 2 , pos . h + 20 , FONT_MEDIUM , ETextAlignment : : CENTER , Colors : : YELLOW , " " ) ;
2018-01-05 19:21:07 +02:00
parent - > addChild ( hoverLabel . get ( ) ) ;
}
if ( status = = CCampaignScreen : : COMPLETED )
2023-08-23 14:07:50 +02:00
graphicsCompleted = std : : make_shared < CPicture > ( ImagePath : : builtin ( " CAMPCHK " ) ) ;
2018-01-05 19:21:07 +02:00
}
2023-06-02 15:42:18 +02:00
void CCampaignScreen : : CCampaignButton : : show ( Canvas & to )
2018-01-05 19:21:07 +02:00
{
if ( status = = CCampaignScreen : : DISABLED )
return ;
CIntObject : : show ( to ) ;
// Play the campaign button video when the mouse cursor is placed over the button
2023-05-17 22:22:45 +02:00
if ( isHovered ( ) )
2023-06-02 15:42:18 +02:00
CCS - > videoh - > update ( pos . x , pos . y , to . getInternalSurface ( ) , true , false ) ; // plays sequentially frame by frame, starts at the beginning when the video is over
2018-01-05 19:21:07 +02:00
}
2023-07-09 16:55:39 +02:00
void CCampaignScreen : : CCampaignButton : : clickReleased ( const Point & cursorPosition )
2018-01-05 19:21:07 +02:00
{
2023-07-08 13:33:04 +02:00
CCS - > videoh - > close ( ) ;
2023-09-20 21:18:13 +02:00
CMainMenu : : openCampaignLobby ( campFile , campaignSet ) ;
2018-01-05 19:21:07 +02:00
}
void CCampaignScreen : : CCampaignButton : : hover ( bool on )
{
2023-09-04 13:29:02 +02:00
if ( on )
CCS - > videoh - > open ( video ) ;
else
CCS - > videoh - > close ( ) ;
2018-01-05 19:21:07 +02:00
if ( hoverLabel )
{
if ( on )
hoverLabel - > setText ( hoverText ) ; // Shows the name of the campaign when you get into the bounds of the button
else
hoverLabel - > setText ( " " ) ;
}
}