123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390 |
- /*
- * CAdventureMapWidget.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 "CAdventureMapWidget.h"
- #include "CInfoBar.h"
- #include "CList.h"
- #include "CMinimap.h"
- #include "CResDataBar.h"
- #include "../gui/CGuiHandler.h"
- #include "../mapView/MapView.h"
- #include "../render/CAnimation.h"
- #include "../render/IImage.h"
- #include "../widgets/Buttons.h"
- #include "../widgets/Images.h"
- #include "../widgets/TextControls.h"
- #include "../CPlayerInterface.h"
- #include "../PlayerLocalState.h"
- #include "../../lib/StringConstants.h"
- #include "../../lib/filesystem/ResourceID.h"
- CAdventureMapWidget::CAdventureMapWidget()
- : state(EGameState::NOT_INITIALIZED)
- {
- pos.w = 800;
- pos.h = 600;
- REGISTER_BUILDER("adventureInfobar", &CAdventureMapWidget::buildInfobox );
- REGISTER_BUILDER("adventureMapImage", &CAdventureMapWidget::buildMapImage );
- REGISTER_BUILDER("adventureMapButton", &CAdventureMapWidget::buildMapButton );
- REGISTER_BUILDER("adventureMapContainer", &CAdventureMapWidget::buildMapContainer );
- REGISTER_BUILDER("adventureMapGameArea", &CAdventureMapWidget::buildMapGameArea );
- REGISTER_BUILDER("adventureMapHeroList", &CAdventureMapWidget::buildMapHeroList );
- REGISTER_BUILDER("adventureMapIcon", &CAdventureMapWidget::buildMapIcon );
- REGISTER_BUILDER("adventureMapTownList", &CAdventureMapWidget::buildMapTownList );
- REGISTER_BUILDER("adventureMinimap", &CAdventureMapWidget::buildMinimap );
- REGISTER_BUILDER("adventureResourceDateBar", &CAdventureMapWidget::buildResourceDateBar );
- REGISTER_BUILDER("adventureStatusBar", &CAdventureMapWidget::buildStatusBar );
- const JsonNode config(ResourceID("config/widgets/adventureMap.json"));
- for(const auto & entry : config["options"]["imagesPlayerColored"].Vector())
- {
- ResourceID resourceName(entry.String(), EResType::IMAGE);
- playerColorerImages.push_back(resourceName.getName());
- }
- build(config);
- }
- Rect CAdventureMapWidget::readSourceArea(const JsonNode & source, const JsonNode & sourceCommon)
- {
- const auto & input = source.isNull() ? sourceCommon : source;
- return readArea(input, Rect(Point(0, 0), Point(800, 600)));
- }
- Rect CAdventureMapWidget::readTargetArea(const JsonNode & source)
- {
- if(subwidgetSizes.empty())
- return readArea(source, pos);
- return readArea(source, subwidgetSizes.back());
- }
- Rect CAdventureMapWidget::readArea(const JsonNode & source, const Rect & boundingBox)
- {
- const auto & object = source.Struct();
- if(object.count("left") + object.count("width") + object.count("right") != 2)
- logGlobal->error("Invalid area definition in widget! Unable to load width!");
- if(object.count("top") + object.count("height") + object.count("bottom") != 2)
- logGlobal->error("Invalid area definition in widget! Unable to load height!");
- int left = source["left"].Integer();
- int width = source["width"].Integer();
- int right = source["right"].Integer();
- int top = source["top"].Integer();
- int height = source["height"].Integer();
- int bottom = source["bottom"].Integer();
- Point topLeft(left, top);
- Point dimensions(width, height);
- if(source["left"].isNull())
- topLeft.x = boundingBox.w - right - width;
- if(source["width"].isNull())
- dimensions.x = boundingBox.w - right - left;
- if(source["top"].isNull())
- topLeft.y = boundingBox.h - bottom - height;
- if(source["height"].isNull())
- dimensions.y = boundingBox.h - bottom - top;
- return Rect(topLeft + boundingBox.topLeft(), dimensions);
- }
- std::shared_ptr<IImage> CAdventureMapWidget::loadImage(const std::string & name)
- {
- ResourceID resource(name, EResType::IMAGE);
- if(images.count(resource.getName()) == 0)
- images[resource.getName()] = IImage::createFromFile(resource.getName());
- ;
- return images[resource.getName()];
- }
- std::shared_ptr<CAnimation> CAdventureMapWidget::loadAnimation(const std::string & name)
- {
- ResourceID resource(name, EResType::ANIMATION);
- if(animations.count(resource.getName()) == 0)
- animations[resource.getName()] = std::make_shared<CAnimation>(resource.getName());
- return animations[resource.getName()];
- }
- std::shared_ptr<CIntObject> CAdventureMapWidget::buildInfobox(const JsonNode & input)
- {
- Rect area = readTargetArea(input["area"]);
- return std::make_shared<CInfoBar>(area);
- }
- std::shared_ptr<CIntObject> CAdventureMapWidget::buildMapImage(const JsonNode & input)
- {
- Rect targetArea = readTargetArea(input["area"]);
- Rect sourceArea = readSourceArea(input["sourceArea"], input["area"]);
- std::string image = input["image"].String();
- return std::make_shared<CFilledTexture>(loadImage(image), targetArea, sourceArea);
- }
- std::shared_ptr<CIntObject> CAdventureMapWidget::buildMapButton(const JsonNode & input)
- {
- auto position = readTargetArea(input["area"]);
- auto image = input["image"].String();
- auto help = readHintText(input["help"]);
- return std::make_shared<CButton>(position.topLeft(), image, help);
- }
- std::shared_ptr<CIntObject> CAdventureMapWidget::buildMapContainer(const JsonNode & input)
- {
- auto position = readTargetArea(input["area"]);
- auto result = std::make_shared<CAdventureMapContainerWidget>();
- result->moveBy(position.topLeft());
- subwidgetSizes.push_back(position);
- for(const auto & entry : input["items"].Vector())
- {
- result->ownedChildren.push_back(buildWidget(entry));
- result->addChild(result->ownedChildren.back().get(), true);
- }
- subwidgetSizes.pop_back();
- return result;
- }
- std::shared_ptr<CIntObject> CAdventureMapWidget::buildMapGameArea(const JsonNode & input)
- {
- Rect area = readTargetArea(input["area"]);
- return std::make_shared<MapView>(area.topLeft(), area.dimensions());
- }
- std::shared_ptr<CIntObject> CAdventureMapWidget::buildMapHeroList(const JsonNode & input)
- {
- Rect area = readTargetArea(input["area"]);
- Rect item = readTargetArea(input["item"]);
- Point itemOffset(input["itemsOffset"]["x"].Integer(), input["itemsOffset"]["y"].Integer());
- int itemsCount = input["itemsCount"].Integer();
- auto result = std::make_shared<CHeroList>(itemsCount, area.topLeft() + item.topLeft(), itemOffset, LOCPLINT->localState->getWanderingHeroes().size());
- subwidgetSizes.push_back(area);
- if(!input["scrollUp"].isNull())
- result->setScrollUpButton(std::dynamic_pointer_cast<CButton>(buildMapButton(input["scrollUp"])));
- if(!input["scrollDown"].isNull())
- result->setScrollDownButton(std::dynamic_pointer_cast<CButton>(buildMapButton(input["scrollDown"])));
- subwidgetSizes.pop_back();
- return result;
- }
- std::shared_ptr<CIntObject> CAdventureMapWidget::buildMapIcon(const JsonNode & input)
- {
- Rect area = readTargetArea(input["area"]);
- size_t index = input["index"].Integer();
- size_t perPlayer = input["perPlayer"].Integer();
- std::string image = input["image"].String();
- return std::make_shared<CAdventureMapIcon>(area.topLeft(), loadAnimation(image), index, perPlayer);
- }
- std::shared_ptr<CIntObject> CAdventureMapWidget::buildMapTownList(const JsonNode & input)
- {
- Rect area = readTargetArea(input["area"]);
- Rect item = readTargetArea(input["item"]);
- Point itemOffset(input["itemsOffset"]["x"].Integer(), input["itemsOffset"]["y"].Integer());
- int itemsCount = input["itemsCount"].Integer();
- auto result = std::make_shared<CTownList>(itemsCount, area.topLeft() + item.topLeft(), itemOffset, LOCPLINT->localState->getOwnedTowns().size());
- subwidgetSizes.push_back(area);
- if(!input["scrollUp"].isNull())
- result->setScrollUpButton(std::dynamic_pointer_cast<CButton>(buildMapButton(input["scrollUp"])));
- if(!input["scrollDown"].isNull())
- result->setScrollDownButton(std::dynamic_pointer_cast<CButton>(buildMapButton(input["scrollDown"])));
- subwidgetSizes.pop_back();
- return result;
- }
- std::shared_ptr<CIntObject> CAdventureMapWidget::buildMinimap(const JsonNode & input)
- {
- Rect area = readTargetArea(input["area"]);
- return std::make_shared<CMinimap>(area);
- }
- std::shared_ptr<CIntObject> CAdventureMapWidget::buildResourceDateBar(const JsonNode & input)
- {
- Rect area = readTargetArea(input["area"]);
- std::string image = input["image"].String();
- auto result = std::make_shared<CResDataBar>(image, area.topLeft());
- for(auto i = 0; i < GameConstants::RESOURCE_QUANTITY; i++)
- {
- const auto & node = input[GameConstants::RESOURCE_NAMES[i]];
- if(node.isNull())
- continue;
- result->setResourcePosition(GameResID(i), Point(node["x"].Integer(), node["y"].Integer()));
- }
- result->setDatePosition(Point(input["date"]["x"].Integer(), input["date"]["y"].Integer()));
- return result;
- }
- std::shared_ptr<CIntObject> CAdventureMapWidget::buildStatusBar(const JsonNode & input)
- {
- Rect area = readTargetArea(input["area"]);
- std::string image = input["image"].String();
- auto background = std::make_shared<CFilledTexture>(image, area);
- return CGStatusBar::create(background);
- }
- std::shared_ptr<CHeroList> CAdventureMapWidget::getHeroList()
- {
- return widget<CHeroList>("heroList");
- }
- std::shared_ptr<CTownList> CAdventureMapWidget::getTownList()
- {
- return widget<CTownList>("townList");
- }
- std::shared_ptr<CMinimap> CAdventureMapWidget::getMinimap()
- {
- return widget<CMinimap>("minimap");
- }
- std::shared_ptr<MapView> CAdventureMapWidget::getMapView()
- {
- return widget<MapView>("mapView");
- }
- std::shared_ptr<CInfoBar> CAdventureMapWidget::getInfoBar()
- {
- return widget<CInfoBar>("infoBar");
- }
- void CAdventureMapWidget::setPlayer(const PlayerColor & player)
- {
- setPlayerChildren(this, player);
- }
- void CAdventureMapWidget::setPlayerChildren(CIntObject * widget, const PlayerColor & player)
- {
- for(auto & entry : widget->children)
- {
- auto container = dynamic_cast<CAdventureMapContainerWidget *>(entry);
- auto icon = dynamic_cast<CAdventureMapIcon *>(entry);
- auto button = dynamic_cast<CButton *>(entry);
- if(button)
- button->setPlayerColor(player);
- if(icon)
- icon->setPlayer(player);
- if(container)
- setPlayerChildren(container, player);
- }
- for(const auto & entry : playerColorerImages)
- {
- if(images.count(entry))
- images[entry]->playerColored(player);
- }
- redraw();
- }
- void CAdventureMapWidget::setState(EGameState newState)
- {
- state = newState;
- if(newState == EGameState::WORLD_VIEW)
- widget<CIntObject>("worldViewContainer")->enable();
- else
- widget<CIntObject>("worldViewContainer")->disable();
- }
- EGameState CAdventureMapWidget::getState()
- {
- return state;
- }
- void CAdventureMapWidget::setOptionHasQuests(bool on)
- {
- }
- void CAdventureMapWidget::setOptionHasUnderground(bool on)
- {
- }
- void CAdventureMapWidget::setOptionUndergroundLevel(bool on)
- {
- }
- void CAdventureMapWidget::setOptionHeroSleeping(bool on)
- {
- }
- void CAdventureMapWidget::setOptionHeroSelected(bool on)
- {
- }
- void CAdventureMapWidget::setOptionHeroCanMove(bool on)
- {
- }
- void CAdventureMapWidget::setOptionHasNextHero(bool on)
- {
- }
- CAdventureMapIcon::CAdventureMapIcon(const Point & position, std::shared_ptr<CAnimation> animation, size_t index, size_t iconsPerPlayer)
- : index(index)
- , iconsPerPlayer(iconsPerPlayer)
- {
- OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
- pos += position;
- image = std::make_shared<CAnimImage>(animation, index);
- }
- void CAdventureMapIcon::setPlayer(const PlayerColor & player)
- {
- image->setFrame(index + player.getNum() * iconsPerPlayer);
- }
|