2017-07-13 10:26:03 +02:00
|
|
|
/*
|
|
|
|
* CComponent.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
|
|
|
|
*
|
|
|
|
*/
|
2014-07-05 12:58:42 +03:00
|
|
|
#include "StdInc.h"
|
|
|
|
#include "CComponent.h"
|
|
|
|
|
2022-09-18 14:47:49 +02:00
|
|
|
#include "CArtifactHolder.h"
|
|
|
|
#include "Images.h"
|
|
|
|
|
2021-02-15 14:03:32 +02:00
|
|
|
#include <vcmi/spells/Service.h>
|
|
|
|
#include <vcmi/spells/Spell.h>
|
|
|
|
|
2014-07-13 20:53:37 +03:00
|
|
|
#include "../gui/CGuiHandler.h"
|
2023-01-05 19:34:37 +02:00
|
|
|
#include "../gui/CursorHandler.h"
|
2023-02-01 20:42:06 +02:00
|
|
|
#include "../gui/TextAlignment.h"
|
|
|
|
#include "../renderSDL/SDL_Extensions.h"
|
|
|
|
#include "../windows/CMessage.h"
|
2023-02-10 15:50:46 +02:00
|
|
|
#include "../windows/InfoWindows.h"
|
2023-02-01 20:42:06 +02:00
|
|
|
#include "../widgets/TextControls.h"
|
2014-07-05 12:58:42 +03:00
|
|
|
#include "../CGameInfo.h"
|
|
|
|
|
|
|
|
#include "../../lib/CTownHandler.h"
|
2023-01-02 00:06:42 +02:00
|
|
|
#include "../../lib/spells/CSpellHandler.h"
|
2014-07-05 12:58:42 +03:00
|
|
|
#include "../../lib/CCreatureHandler.h"
|
2017-08-30 12:35:23 +02:00
|
|
|
#include "../../lib/CSkillHandler.h"
|
2014-07-05 12:58:42 +03:00
|
|
|
#include "../../lib/CGeneralTextHandler.h"
|
2014-07-14 17:19:44 +03:00
|
|
|
#include "../../lib/NetPacksBase.h"
|
2023-02-01 20:42:06 +02:00
|
|
|
#include "../../lib/CArtHandler.h"
|
2014-07-05 12:58:42 +03:00
|
|
|
|
2018-04-07 13:34:11 +02:00
|
|
|
CComponent::CComponent(Etype Type, int Subtype, int Val, ESize imageSize)
|
|
|
|
: perDay(false)
|
2014-07-05 12:58:42 +03:00
|
|
|
{
|
|
|
|
init(Type, Subtype, Val, imageSize);
|
|
|
|
}
|
|
|
|
|
2018-04-07 13:34:11 +02:00
|
|
|
CComponent::CComponent(const Component & c, ESize imageSize)
|
|
|
|
: perDay(false)
|
2014-07-05 12:58:42 +03:00
|
|
|
{
|
|
|
|
if(c.id == Component::RESOURCE && c.when==-1)
|
|
|
|
perDay = true;
|
|
|
|
|
2018-04-07 13:34:11 +02:00
|
|
|
init((Etype)c.id, c.subtype, c.val, imageSize);
|
2014-07-05 12:58:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CComponent::init(Etype Type, int Subtype, int Val, ESize imageSize)
|
|
|
|
{
|
2018-04-07 13:34:11 +02:00
|
|
|
OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
|
|
|
|
|
|
|
|
addUsedEvents(RCLICK);
|
2014-07-05 12:58:42 +03:00
|
|
|
|
|
|
|
compType = Type;
|
|
|
|
subtype = Subtype;
|
|
|
|
val = Val;
|
|
|
|
size = imageSize;
|
|
|
|
|
|
|
|
assert(compType < typeInvalid);
|
|
|
|
assert(size < sizeInvalid);
|
|
|
|
|
2020-10-01 10:38:06 +02:00
|
|
|
setSurface(getFileName()[size], (int)getIndex());
|
2014-07-05 12:58:42 +03:00
|
|
|
|
|
|
|
pos.w = image->pos.w;
|
|
|
|
pos.h = image->pos.h;
|
|
|
|
|
|
|
|
EFonts font = FONT_SMALL;
|
|
|
|
if (imageSize < small)
|
|
|
|
font = FONT_TINY; //other sizes?
|
|
|
|
|
|
|
|
pos.h += 4; //distance between text and image
|
|
|
|
|
|
|
|
std::vector<std::string> textLines = CMessage::breakText(getSubtitle(), std::max<int>(80, pos.w), font);
|
|
|
|
for(auto & line : textLines)
|
|
|
|
{
|
2020-10-01 10:38:06 +02:00
|
|
|
int height = static_cast<int>(graphics->fonts[font]->getLineHeight());
|
2022-11-26 23:12:20 +02:00
|
|
|
auto label = std::make_shared<CLabel>(pos.w/2, pos.h + height/2, font, ETextAlignment::CENTER, Colors::WHITE, line);
|
2014-07-05 12:58:42 +03:00
|
|
|
|
|
|
|
pos.h += height;
|
2018-04-07 13:34:11 +02:00
|
|
|
if(label->pos.w > pos.w)
|
2014-07-05 12:58:42 +03:00
|
|
|
{
|
|
|
|
pos.x -= (label->pos.w - pos.w)/2;
|
|
|
|
pos.w = label->pos.w;
|
|
|
|
}
|
2018-04-07 13:34:11 +02:00
|
|
|
lines.push_back(label);
|
2014-07-05 12:58:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::vector<std::string> CComponent::getFileName()
|
|
|
|
{
|
|
|
|
static const std::string primSkillsArr [] = {"PSKIL32", "PSKIL32", "PSKIL42", "PSKILL"};
|
|
|
|
static const std::string secSkillsArr [] = {"SECSK32", "SECSK32", "SECSKILL", "SECSK82"};
|
|
|
|
static const std::string resourceArr [] = {"SMALRES", "RESOURCE", "RESOUR82", "RESOUR82"};
|
|
|
|
static const std::string creatureArr [] = {"CPRSMALL", "CPRSMALL", "TWCRPORT", "TWCRPORT"};
|
|
|
|
static const std::string artifactArr[] = {"Artifact", "Artifact", "Artifact", "Artifact"};
|
|
|
|
static const std::string spellsArr [] = {"SpellInt", "SpellInt", "SPELLSCR", "SPELLSCR"};
|
|
|
|
static const std::string moraleArr [] = {"IMRL22", "IMRL30", "IMRL42", "imrl82"};
|
|
|
|
static const std::string luckArr [] = {"ILCK22", "ILCK30", "ILCK42", "ilck82"};
|
|
|
|
static const std::string heroArr [] = {"PortraitsSmall", "PortraitsSmall", "PortraitsLarge", "PortraitsLarge"};
|
|
|
|
static const std::string flagArr [] = {"CREST58", "CREST58", "CREST58", "CREST58"};
|
|
|
|
|
|
|
|
auto gen = [](const std::string * arr)
|
|
|
|
{
|
|
|
|
return std::vector<std::string>(arr, arr + 4);
|
|
|
|
};
|
|
|
|
|
|
|
|
switch(compType)
|
|
|
|
{
|
|
|
|
case primskill: return gen(primSkillsArr);
|
|
|
|
case secskill: return gen(secSkillsArr);
|
|
|
|
case resource: return gen(resourceArr);
|
|
|
|
case creature: return gen(creatureArr);
|
|
|
|
case artifact: return gen(artifactArr);
|
|
|
|
case experience: return gen(primSkillsArr);
|
|
|
|
case spell: return gen(spellsArr);
|
|
|
|
case morale: return gen(moraleArr);
|
|
|
|
case luck: return gen(luckArr);
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
case building: return std::vector<std::string>(4, (*CGI->townh)[subtype]->town->clientInfo.buildingsIcons);
|
2014-07-05 12:58:42 +03:00
|
|
|
case hero: return gen(heroArr);
|
|
|
|
case flag: return gen(flagArr);
|
|
|
|
}
|
|
|
|
assert(0);
|
|
|
|
return std::vector<std::string>();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t CComponent::getIndex()
|
|
|
|
{
|
|
|
|
switch(compType)
|
|
|
|
{
|
|
|
|
case primskill: return subtype;
|
|
|
|
case secskill: return subtype*3 + 3 + val - 1;
|
|
|
|
case resource: return subtype;
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
case creature: return CGI->creatures()->getByIndex(subtype)->getIconIndex();
|
|
|
|
case artifact: return CGI->artifacts()->getByIndex(subtype)->getIconIndex();
|
2014-07-05 12:58:42 +03:00
|
|
|
case experience: return 4;
|
|
|
|
case spell: return subtype;
|
|
|
|
case morale: return val+3;
|
|
|
|
case luck: return val+3;
|
|
|
|
case building: return val;
|
|
|
|
case hero: return subtype;
|
|
|
|
case flag: return subtype;
|
|
|
|
}
|
|
|
|
assert(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CComponent::getDescription()
|
|
|
|
{
|
2016-01-21 19:23:45 +02:00
|
|
|
switch(compType)
|
2014-07-05 12:58:42 +03:00
|
|
|
{
|
|
|
|
case primskill: return (subtype < 4)? CGI->generaltexth->arraytxt[2+subtype] //Primary skill
|
|
|
|
: CGI->generaltexth->allTexts[149]; //mana
|
2023-01-01 22:20:41 +02:00
|
|
|
case secskill: return CGI->skillh->getByIndex(subtype)->getDescriptionTranslated(val);
|
2014-07-05 12:58:42 +03:00
|
|
|
case resource: return CGI->generaltexth->allTexts[242];
|
|
|
|
case creature: return "";
|
2016-01-21 19:23:45 +02:00
|
|
|
case artifact:
|
|
|
|
{
|
2022-11-29 00:11:46 +02:00
|
|
|
auto artID = ArtifactID(subtype);
|
2016-01-21 19:23:45 +02:00
|
|
|
std::unique_ptr<CArtifactInstance> art;
|
2022-11-29 00:11:46 +02:00
|
|
|
if (artID != ArtifactID::SPELL_SCROLL)
|
2016-01-21 19:23:45 +02:00
|
|
|
{
|
2022-11-29 00:11:46 +02:00
|
|
|
art.reset(CArtifactInstance::createNewArtifactInstance(artID));
|
2016-01-21 19:23:45 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
art.reset(CArtifactInstance::createScroll(SpellID(val)));
|
2016-01-21 19:23:45 +02:00
|
|
|
}
|
|
|
|
return art->getEffectiveDescription();
|
|
|
|
}
|
2014-07-05 12:58:42 +03:00
|
|
|
case experience: return CGI->generaltexth->allTexts[241];
|
2023-01-02 00:06:42 +02:00
|
|
|
case spell: return (*CGI->spellh)[subtype]->getDescriptionTranslated(val);
|
2014-07-05 12:58:42 +03:00
|
|
|
case morale: return CGI->generaltexth->heroscrn[ 4 - (val>0) + (val<0)];
|
|
|
|
case luck: return CGI->generaltexth->heroscrn[ 7 - (val>0) + (val<0)];
|
2023-01-04 15:17:50 +02:00
|
|
|
case building: return (*CGI->townh)[subtype]->town->buildings[BuildingID(val)]->getDescriptionTranslated();
|
2014-07-05 12:58:42 +03:00
|
|
|
case hero: return "";
|
|
|
|
case flag: return "";
|
|
|
|
}
|
|
|
|
assert(0);
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CComponent::getSubtitle()
|
|
|
|
{
|
2016-01-21 19:23:45 +02:00
|
|
|
if(!perDay)
|
2014-07-05 12:58:42 +03:00
|
|
|
return getSubtitleInternal();
|
|
|
|
|
|
|
|
std::string ret = CGI->generaltexth->allTexts[3];
|
|
|
|
boost::replace_first(ret, "%d", getSubtitleInternal());
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CComponent::getSubtitleInternal()
|
|
|
|
{
|
|
|
|
//FIXME: some of these are horrible (e.g creature)
|
|
|
|
switch(compType)
|
|
|
|
{
|
|
|
|
case primskill: return boost::str(boost::format("%+d %s") % val % (subtype < 4 ? CGI->generaltexth->primarySkillNames[subtype] : CGI->generaltexth->allTexts[387]));
|
2023-01-01 22:20:41 +02:00
|
|
|
case secskill: return CGI->generaltexth->levels[val-1] + "\n" + CGI->skillh->getByIndex(subtype)->getNameTranslated();
|
2014-07-05 12:58:42 +03:00
|
|
|
case resource: return boost::lexical_cast<std::string>(val);
|
2023-01-02 18:00:51 +02:00
|
|
|
case creature:
|
|
|
|
{
|
|
|
|
auto creature = CGI->creh->getByIndex(subtype);
|
|
|
|
if ( val )
|
|
|
|
return boost::lexical_cast<std::string>(val) + " " + (val > 1 ? creature->getNamePluralTranslated() : creature->getNameSingularTranslated());
|
|
|
|
else
|
|
|
|
return val > 1 ? creature->getNamePluralTranslated() : creature->getNameSingularTranslated();
|
|
|
|
}
|
2023-01-02 15:58:56 +02:00
|
|
|
case artifact: return CGI->artifacts()->getByIndex(subtype)->getNameTranslated();
|
2014-07-05 12:58:42 +03:00
|
|
|
case experience:
|
|
|
|
{
|
2016-01-19 09:30:55 +02:00
|
|
|
if(subtype == 1) //+1 level - tree of knowledge
|
2014-07-05 12:58:42 +03:00
|
|
|
{
|
|
|
|
std::string level = CGI->generaltexth->allTexts[442];
|
|
|
|
boost::replace_first(level, "1", boost::lexical_cast<std::string>(val));
|
|
|
|
return level;
|
|
|
|
}
|
|
|
|
else
|
2016-01-19 09:30:55 +02:00
|
|
|
{
|
2014-07-05 12:58:42 +03:00
|
|
|
return boost::lexical_cast<std::string>(val); //amount of experience OR level required for seer hut;
|
2016-01-19 09:30:55 +02:00
|
|
|
}
|
2014-07-05 12:58:42 +03:00
|
|
|
}
|
2023-01-02 00:06:42 +02:00
|
|
|
case spell: return CGI->spells()->getByIndex(subtype)->getNameTranslated();
|
2014-07-05 12:58:42 +03:00
|
|
|
case morale: return "";
|
|
|
|
case luck: return "";
|
2016-01-19 09:30:55 +02:00
|
|
|
case building:
|
|
|
|
{
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
auto building = (*CGI->townh)[subtype]->town->buildings[BuildingID(val)];
|
2016-01-19 09:30:55 +02:00
|
|
|
if(!building)
|
|
|
|
{
|
2023-01-04 15:17:50 +02:00
|
|
|
logGlobal->error("Town of faction %s has no building #%d", (*CGI->townh)[subtype]->town->faction->getNameTranslated(), val);
|
2016-01-19 09:30:55 +02:00
|
|
|
return (boost::format("Missing building #%d") % val).str();
|
|
|
|
}
|
2023-01-04 15:17:50 +02:00
|
|
|
return building->getNameTranslated();
|
2016-01-19 09:30:55 +02:00
|
|
|
}
|
2014-07-05 12:58:42 +03:00
|
|
|
case hero: return "";
|
|
|
|
case flag: return CGI->generaltexth->capColors[subtype];
|
|
|
|
}
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
logGlobal->error("Invalid CComponent type: %d", (int)compType);
|
2014-07-05 12:58:42 +03:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
void CComponent::setSurface(std::string defName, int imgPos)
|
|
|
|
{
|
2018-04-07 13:34:11 +02:00
|
|
|
OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
|
|
|
|
image = std::make_shared<CAnimImage>(defName, imgPos);
|
2014-07-05 12:58:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CComponent::clickRight(tribool down, bool previousState)
|
|
|
|
{
|
2023-02-10 15:50:46 +02:00
|
|
|
if(down && !getDescription().empty())
|
|
|
|
CRClickPopup::createAndPush(getDescription());
|
2014-07-05 12:58:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CSelectableComponent::clickLeft(tribool down, bool previousState)
|
|
|
|
{
|
|
|
|
if (down)
|
|
|
|
{
|
|
|
|
if(onSelect)
|
|
|
|
onSelect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSelectableComponent::init()
|
|
|
|
{
|
|
|
|
selected = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CSelectableComponent::CSelectableComponent(const Component &c, std::function<void()> OnSelect):
|
|
|
|
CComponent(c),onSelect(OnSelect)
|
|
|
|
{
|
|
|
|
type |= REDRAW_PARENT;
|
|
|
|
addUsedEvents(LCLICK | KEYBOARD);
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
CSelectableComponent::CSelectableComponent(Etype Type, int Sub, int Val, ESize imageSize, std::function<void()> OnSelect):
|
|
|
|
CComponent(Type,Sub,Val, imageSize),onSelect(OnSelect)
|
|
|
|
{
|
|
|
|
type |= REDRAW_PARENT;
|
|
|
|
addUsedEvents(LCLICK | KEYBOARD);
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSelectableComponent::select(bool on)
|
|
|
|
{
|
|
|
|
if(on != selected)
|
|
|
|
{
|
|
|
|
selected = on;
|
|
|
|
redraw();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSelectableComponent::showAll(SDL_Surface * to)
|
|
|
|
{
|
|
|
|
CComponent::showAll(to);
|
|
|
|
if(selected)
|
|
|
|
{
|
2023-01-17 22:01:35 +02:00
|
|
|
CSDL_Ext::drawBorder(to, Rect::createAround(image->pos, 1), Colors::BRIGHT_YELLOW);
|
2014-07-05 12:58:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-07 13:34:11 +02:00
|
|
|
void CComponentBox::selectionChanged(std::shared_ptr<CSelectableComponent> newSelection)
|
2014-07-05 12:58:42 +03:00
|
|
|
{
|
|
|
|
if (newSelection == selected)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (selected)
|
|
|
|
selected->select(false);
|
|
|
|
|
|
|
|
selected = newSelection;
|
|
|
|
if (onSelect)
|
|
|
|
onSelect(selectedIndex());
|
|
|
|
|
|
|
|
if (selected)
|
|
|
|
selected->select(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
int CComponentBox::selectedIndex()
|
|
|
|
{
|
|
|
|
if (selected)
|
2020-10-01 10:38:06 +02:00
|
|
|
return static_cast<int>(std::find(components.begin(), components.end(), selected) - components.begin());
|
2014-07-05 12:58:42 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Point CComponentBox::getOrTextPos(CComponent *left, CComponent *right)
|
|
|
|
{
|
|
|
|
int leftSubtitle = ( left->pos.w - left->image->pos.w) / 2;
|
|
|
|
int rightSubtitle = (right->pos.w - right->image->pos.w) / 2;
|
|
|
|
int fullDistance = getDistance(left, right) + leftSubtitle + rightSubtitle;
|
|
|
|
|
|
|
|
return Point(fullDistance/2 - leftSubtitle, (left->image->pos.h + right->image->pos.h) / 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
int CComponentBox::getDistance(CComponent *left, CComponent *right)
|
|
|
|
{
|
|
|
|
static const int betweenImagesMin = 20;
|
|
|
|
static const int betweenSubtitlesMin = 10;
|
|
|
|
|
|
|
|
int leftSubtitle = ( left->pos.w - left->image->pos.w) / 2;
|
|
|
|
int rightSubtitle = (right->pos.w - right->image->pos.w) / 2;
|
|
|
|
int subtitlesOffset = leftSubtitle + rightSubtitle;
|
|
|
|
|
|
|
|
return std::max(betweenSubtitlesMin, betweenImagesMin - subtitlesOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CComponentBox::placeComponents(bool selectable)
|
|
|
|
{
|
|
|
|
static const int betweenRows = 22;
|
|
|
|
|
2018-04-07 13:34:11 +02:00
|
|
|
OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
|
2014-07-05 12:58:42 +03:00
|
|
|
if (components.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
//prepare components
|
|
|
|
for(auto & comp : components)
|
|
|
|
{
|
2018-04-07 13:34:11 +02:00
|
|
|
addChild(comp.get());
|
2014-07-05 12:58:42 +03:00
|
|
|
comp->moveTo(Point(pos.x, pos.y));
|
|
|
|
}
|
|
|
|
|
|
|
|
struct RowData
|
|
|
|
{
|
|
|
|
size_t comps;
|
|
|
|
int width;
|
|
|
|
int height;
|
|
|
|
RowData (size_t Comps, int Width, int Height):
|
|
|
|
comps(Comps), width (Width), height (Height){};
|
|
|
|
};
|
|
|
|
std::vector<RowData> rows;
|
|
|
|
rows.push_back (RowData (0,0,0));
|
|
|
|
|
|
|
|
//split components in rows
|
2018-04-07 13:34:11 +02:00
|
|
|
std::shared_ptr<CComponent> prevComp;
|
2014-07-05 12:58:42 +03:00
|
|
|
|
2018-04-07 13:34:11 +02:00
|
|
|
for(std::shared_ptr<CComponent> comp : components)
|
2014-07-05 12:58:42 +03:00
|
|
|
{
|
|
|
|
//make sure that components are smaller than our width
|
|
|
|
//assert(pos.w == 0 || pos.w < comp->pos.w);
|
|
|
|
|
2018-04-07 13:34:11 +02:00
|
|
|
const int distance = prevComp ? getDistance(prevComp.get(), comp.get()) : 0;
|
2014-07-05 12:58:42 +03:00
|
|
|
|
|
|
|
//start next row
|
|
|
|
if ((pos.w != 0 && rows.back().width + comp->pos.w + distance > pos.w) // row is full
|
|
|
|
|| rows.back().comps >= 4) // no more than 4 comps per row
|
|
|
|
{
|
|
|
|
prevComp = nullptr;
|
|
|
|
rows.push_back (RowData (0,0,0));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (prevComp)
|
|
|
|
rows.back().width += distance;
|
|
|
|
|
|
|
|
rows.back().comps++;
|
|
|
|
rows.back().width += comp->pos.w;
|
|
|
|
|
|
|
|
vstd::amax(rows.back().height, comp->pos.h);
|
|
|
|
prevComp = comp;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pos.w == 0)
|
|
|
|
{
|
|
|
|
for(auto & row : rows)
|
|
|
|
vstd::amax(pos.w, row.width);
|
|
|
|
}
|
|
|
|
|
2020-10-01 10:38:06 +02:00
|
|
|
int height = ((int)rows.size() - 1) * betweenRows;
|
2014-07-05 12:58:42 +03:00
|
|
|
for(auto & row : rows)
|
|
|
|
height += row.height;
|
|
|
|
|
|
|
|
//assert(pos.h == 0 || pos.h < height);
|
|
|
|
if (pos.h == 0)
|
|
|
|
pos.h = height;
|
|
|
|
|
|
|
|
auto iter = components.begin();
|
|
|
|
int currentY = (pos.h - height) / 2;
|
|
|
|
|
|
|
|
//move components to their positions
|
|
|
|
for (auto & rows_row : rows)
|
|
|
|
{
|
|
|
|
// amount of free space we may add on each side of every component
|
2020-10-01 10:38:06 +02:00
|
|
|
int freeSpace = (pos.w - rows_row.width) / ((int)rows_row.comps * 2);
|
2014-07-05 12:58:42 +03:00
|
|
|
prevComp = nullptr;
|
|
|
|
|
|
|
|
int currentX = 0;
|
|
|
|
for (size_t col = 0; col < rows_row.comps; col++)
|
|
|
|
{
|
|
|
|
currentX += freeSpace;
|
|
|
|
if (prevComp)
|
|
|
|
{
|
|
|
|
if (selectable)
|
|
|
|
{
|
2018-04-07 13:34:11 +02:00
|
|
|
Point orPos = Point(currentX - freeSpace, currentY) + getOrTextPos(prevComp.get(), iter->get());
|
2014-07-05 12:58:42 +03:00
|
|
|
|
2022-11-26 23:12:20 +02:00
|
|
|
orLabels.push_back(std::make_shared<CLabel>(orPos.x, orPos.y, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[4]));
|
2014-07-05 12:58:42 +03:00
|
|
|
}
|
2018-04-07 13:34:11 +02:00
|
|
|
currentX += getDistance(prevComp.get(), iter->get());
|
2014-07-05 12:58:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
(*iter)->moveBy(Point(currentX, currentY));
|
|
|
|
currentX += (*iter)->pos.w;
|
|
|
|
currentX += freeSpace;
|
|
|
|
|
|
|
|
prevComp = *(iter++);
|
|
|
|
}
|
|
|
|
currentY += rows_row.height + betweenRows;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-07 13:34:11 +02:00
|
|
|
CComponentBox::CComponentBox(std::vector<std::shared_ptr<CComponent>> _components, Rect position):
|
|
|
|
components(_components)
|
2014-07-05 12:58:42 +03:00
|
|
|
{
|
|
|
|
type |= REDRAW_PARENT;
|
2022-12-15 23:24:03 +02:00
|
|
|
pos = position + pos.topLeft();
|
2014-07-05 12:58:42 +03:00
|
|
|
placeComponents(false);
|
|
|
|
}
|
|
|
|
|
2018-04-07 13:34:11 +02:00
|
|
|
CComponentBox::CComponentBox(std::vector<std::shared_ptr<CSelectableComponent>> _components, Rect position, std::function<void(int newID)> _onSelect):
|
2014-07-05 12:58:42 +03:00
|
|
|
components(_components.begin(), _components.end()),
|
|
|
|
onSelect(_onSelect)
|
|
|
|
{
|
|
|
|
type |= REDRAW_PARENT;
|
2022-12-15 23:24:03 +02:00
|
|
|
pos = position + pos.topLeft();
|
2014-07-05 12:58:42 +03:00
|
|
|
placeComponents(true);
|
|
|
|
|
|
|
|
assert(!components.empty());
|
|
|
|
|
|
|
|
int key = SDLK_1;
|
|
|
|
for(auto & comp : _components)
|
|
|
|
{
|
2014-08-09 15:14:31 +03:00
|
|
|
comp->onSelect = std::bind(&CComponentBox::selectionChanged, this, comp);
|
2014-07-05 12:58:42 +03:00
|
|
|
comp->assignedKeys.insert(key++);
|
|
|
|
}
|
|
|
|
selectionChanged(_components.front());
|
|
|
|
}
|