mirror of
https://github.com/vcmi/vcmi.git
synced 2025-11-06 09:09:40 +02:00
Transfer resources widget
This commit is contained in:
@@ -28,7 +28,7 @@
|
||||
|
||||
CFreelancerGuild::CFreelancerGuild(const IMarket * market, const CGHeroInstance * hero)
|
||||
: CTradeBase(market, hero)
|
||||
, CResourcesPurchasing([this](){CResourcesPurchasing::updateSubtitles(EMarketMode::CREATURE_RESOURCE);})
|
||||
, CResourcesBuying([this](){CResourcesBuying::updateSubtitles(EMarketMode::CREATURE_RESOURCE);})
|
||||
{
|
||||
OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
|
||||
|
||||
@@ -103,7 +103,7 @@ void CFreelancerGuild::makeDeal()
|
||||
|
||||
void CFreelancerGuild::deselect()
|
||||
{
|
||||
CResourcesPurchasing::deselect();
|
||||
CResourcesBuying::deselect();
|
||||
updateSelected();
|
||||
}
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
|
||||
#include "CTradeBase.h"
|
||||
|
||||
class CFreelancerGuild : public CCreaturesSelling , public CResourcesPurchasing
|
||||
class CFreelancerGuild : public CCreaturesSelling , public CResourcesBuying
|
||||
{
|
||||
public:
|
||||
CFreelancerGuild(const IMarket * market, const CGHeroInstance * hero);
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
|
||||
CMarketResources::CMarketResources(const IMarket * market, const CGHeroInstance * hero)
|
||||
: CTradeBase(market, hero)
|
||||
, CResourcesPurchasing([this](){CMarketResources::updateSubtitles();})
|
||||
, CResourcesBuying([this](){CMarketResources::updateSubtitles();})
|
||||
{
|
||||
OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
|
||||
|
||||
@@ -78,7 +78,7 @@ void CMarketResources::makeDeal()
|
||||
|
||||
void CMarketResources::deselect()
|
||||
{
|
||||
CResourcesPurchasing::deselect();
|
||||
CResourcesBuying::deselect();
|
||||
updateSelected();
|
||||
}
|
||||
|
||||
@@ -139,7 +139,7 @@ void CMarketResources::onSlotClickPressed(const std::shared_ptr<CTradeableItem>
|
||||
|
||||
void CMarketResources::updateSubtitles()
|
||||
{
|
||||
CResourcesPurchasing::updateSubtitles(EMarketMode::RESOURCE_RESOURCE);
|
||||
CResourcesBuying::updateSubtitles(EMarketMode::RESOURCE_RESOURCE);
|
||||
if(hLeft)
|
||||
rightTradePanel->slots[hLeft->serial]->subtitle = CGI->generaltexth->allTexts[164]; // n/a
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
|
||||
#include "CTradeBase.h"
|
||||
|
||||
class CMarketResources : public CResourcesSelling, public CResourcesPurchasing
|
||||
class CMarketResources : public CResourcesSelling, public CResourcesBuying
|
||||
{
|
||||
public:
|
||||
CMarketResources(const IMarket * market, const CGHeroInstance * hero);
|
||||
|
||||
@@ -125,7 +125,7 @@ void CCreaturesSelling::updateSlots()
|
||||
leftTradePanel->updateSlots();
|
||||
}
|
||||
|
||||
CResourcesPurchasing::CResourcesPurchasing(TradePanelBase::UpdateSlotsFunctor callback)
|
||||
CResourcesBuying::CResourcesBuying(TradePanelBase::UpdateSlotsFunctor callback)
|
||||
{
|
||||
OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
|
||||
|
||||
@@ -133,7 +133,7 @@ CResourcesPurchasing::CResourcesPurchasing(TradePanelBase::UpdateSlotsFunctor ca
|
||||
labels.emplace_back(std::make_shared<CLabel>(445, 148, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[168]));
|
||||
}
|
||||
|
||||
void CResourcesPurchasing::updateSubtitles(EMarketMode marketMode)
|
||||
void CResourcesBuying::updateSubtitles(EMarketMode marketMode)
|
||||
{
|
||||
assert(marketMode == EMarketMode::RESOURCE_RESOURCE || marketMode == EMarketMode::CREATURE_RESOURCE || marketMode == EMarketMode::ARTIFACT_RESOURCE);
|
||||
|
||||
@@ -149,7 +149,7 @@ void CResourcesPurchasing::updateSubtitles(EMarketMode marketMode)
|
||||
rightTradePanel->clearSubtitles();
|
||||
};
|
||||
|
||||
void CResourcesPurchasing::deselect()
|
||||
void CResourcesBuying::deselect()
|
||||
{
|
||||
CTradeBase::deselect();
|
||||
bidQty = 0;
|
||||
|
||||
@@ -75,10 +75,10 @@ public:
|
||||
void updateSlots() override;
|
||||
};
|
||||
|
||||
class CResourcesPurchasing : virtual public CTradeBase, virtual public CIntObject
|
||||
class CResourcesBuying : virtual public CTradeBase, virtual public CIntObject
|
||||
{
|
||||
public:
|
||||
CResourcesPurchasing(TradePanelBase::UpdateSlotsFunctor callback);
|
||||
CResourcesBuying(TradePanelBase::UpdateSlotsFunctor callback);
|
||||
void updateSubtitles(EMarketMode marketMode);
|
||||
void deselect() override;
|
||||
|
||||
|
||||
130
client/widgets/markets/CTransferResources.cpp
Normal file
130
client/widgets/markets/CTransferResources.cpp
Normal file
@@ -0,0 +1,130 @@
|
||||
/*
|
||||
* CTransferResources.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 "CTransferResources.h"
|
||||
|
||||
#include "../../gui/CGuiHandler.h"
|
||||
#include "../../widgets/Buttons.h"
|
||||
#include "../../widgets/Slider.h"
|
||||
#include "../../widgets/TextControls.h"
|
||||
|
||||
#include "../../CGameInfo.h"
|
||||
#include "../../CPlayerInterface.h"
|
||||
|
||||
#include "../../../CCallback.h"
|
||||
|
||||
#include "../../../lib/CGeneralTextHandler.h"
|
||||
|
||||
CTransferResources::CTransferResources(const IMarket * market, const CGHeroInstance * hero)
|
||||
: CTradeBase(market, hero)
|
||||
{
|
||||
OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
|
||||
|
||||
labels.emplace_back(std::make_shared<CLabel>(299, 27, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[158]));
|
||||
labels.emplace_back(std::make_shared<CLabel>(445, 56, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[169]));
|
||||
deal = std::make_shared<CButton>(Point(306, 520), AnimationPath::builtin("TPMRKB.DEF"),
|
||||
CGI->generaltexth->zelp[595], [this]() {CTransferResources::makeDeal();});
|
||||
maxAmount = std::make_shared<CButton>(Point(228, 520), AnimationPath::builtin("IRCBTNS.DEF"), CGI->generaltexth->zelp[596],
|
||||
[this]() {offerSlider->scrollToMax();});
|
||||
offerSlider = std::make_shared<CSlider>(Point(230, 489), 137, [this](int newVal)
|
||||
{
|
||||
CTransferResources::onOfferSliderMoved(newVal);
|
||||
}, 0, 0, 0, Orientation::HORIZONTAL);
|
||||
|
||||
// Player's resources
|
||||
assert(leftTradePanel);
|
||||
std::for_each(leftTradePanel->slots.cbegin(), leftTradePanel->slots.cend(), [this](auto & slot)
|
||||
{
|
||||
slot->clickPressedCallback = [this](const std::shared_ptr<CTradeableItem> & heroSlot)
|
||||
{
|
||||
CTransferResources::onSlotClickPressed(heroSlot, hLeft);
|
||||
};
|
||||
});
|
||||
leftTradePanel->moveTo(pos.topLeft() + Point(40, 182));
|
||||
|
||||
// Players panel
|
||||
rightTradePanel = std::make_shared<PlayersPanel>([this](const std::shared_ptr<CTradeableItem> & heroSlot)
|
||||
{
|
||||
CTransferResources::onSlotClickPressed(heroSlot, hRight);
|
||||
});
|
||||
rightTradePanel->moveTo(pos.topLeft() + Point(333, 83));
|
||||
|
||||
CResourcesSelling::updateSlots();
|
||||
CTransferResources::deselect();
|
||||
}
|
||||
|
||||
void CTransferResources::makeDeal()
|
||||
{
|
||||
if(auto toTrade = offerSlider->getValue(); toTrade != 0)
|
||||
{
|
||||
LOCPLINT->cb->trade(market, EMarketMode::RESOURCE_PLAYER, GameResID(hLeft->id), PlayerColor(hRight->id), toTrade, hero);
|
||||
deselect();
|
||||
}
|
||||
}
|
||||
|
||||
void CTransferResources::deselect()
|
||||
{
|
||||
CTradeBase::deselect();
|
||||
updateSelected();
|
||||
}
|
||||
|
||||
void CTransferResources::updateSelected()
|
||||
{
|
||||
std::optional<size_t> lImageIndex = std::nullopt;
|
||||
std::optional<size_t> rImageIndex = std::nullopt;
|
||||
|
||||
if(hLeft && hRight)
|
||||
{
|
||||
leftTradePanel->selectedSubtitle->setText(std::to_string(offerSlider->getValue()));
|
||||
rightTradePanel->selectedSubtitle->setText(CGI->generaltexth->capColors[hRight->id]);
|
||||
lImageIndex = hLeft->id;
|
||||
rImageIndex = hRight->id;
|
||||
}
|
||||
else
|
||||
{
|
||||
leftTradePanel->selectedSubtitle->setText("");
|
||||
rightTradePanel->selectedSubtitle->setText("");
|
||||
}
|
||||
leftTradePanel->setSelectedFrameIndex(lImageIndex);
|
||||
rightTradePanel->setSelectedFrameIndex(rImageIndex);
|
||||
}
|
||||
|
||||
void CTransferResources::onOfferSliderMoved(int newVal)
|
||||
{
|
||||
if(hLeft && hRight)
|
||||
{
|
||||
offerSlider->scrollTo(newVal);
|
||||
updateSelected();
|
||||
redraw();
|
||||
}
|
||||
}
|
||||
|
||||
void CTransferResources::onSlotClickPressed(const std::shared_ptr<CTradeableItem> & newSlot, std::shared_ptr<CTradeableItem> & hCurSlot)
|
||||
{
|
||||
if(newSlot == hCurSlot)
|
||||
return;
|
||||
|
||||
CTradeBase::onSlotClickPressed(newSlot, hCurSlot);
|
||||
if(hLeft)
|
||||
{
|
||||
if(hRight)
|
||||
{
|
||||
offerSlider->setAmount(LOCPLINT->cb->getResourceAmount(GameResID(hLeft->id)));
|
||||
offerSlider->scrollTo(0);
|
||||
offerSlider->block(false);
|
||||
maxAmount->block(false);
|
||||
deal->block(false);
|
||||
}
|
||||
updateSelected();
|
||||
rightTradePanel->updateSlots();
|
||||
}
|
||||
redraw();
|
||||
}
|
||||
25
client/widgets/markets/CTransferResources.h
Normal file
25
client/widgets/markets/CTransferResources.h
Normal file
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* CTransferResources.h, 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
|
||||
*
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "CTradeBase.h"
|
||||
|
||||
class CTransferResources : public CResourcesSelling
|
||||
{
|
||||
public:
|
||||
CTransferResources(const IMarket * market, const CGHeroInstance * hero);
|
||||
void makeDeal() override;
|
||||
void deselect() override;
|
||||
|
||||
private:
|
||||
void updateSelected();
|
||||
void onOfferSliderMoved(int newVal);
|
||||
void onSlotClickPressed(const std::shared_ptr<CTradeableItem> & newSlot, std::shared_ptr<CTradeableItem> & hCurSlot);
|
||||
};
|
||||
@@ -196,12 +196,19 @@ void CTradeableItem::hover(bool on)
|
||||
case EType::CREATURE_PLACEHOLDER:
|
||||
GH.statusbar()->write(boost::str(boost::format(CGI->generaltexth->allTexts[481]) % CGI->creh->objects[id]->getNamePluralTranslated()));
|
||||
break;
|
||||
case EType::ARTIFACT_TYPE:
|
||||
case EType::ARTIFACT_PLACEHOLDER:
|
||||
if(id < 0)
|
||||
GH.statusbar()->write(CGI->generaltexth->zelp[582].first);
|
||||
else
|
||||
GH.statusbar()->write(CGI->artifacts()->getByIndex(id)->getNameTranslated());
|
||||
break;
|
||||
case EType::RESOURCE:
|
||||
GH.statusbar()->write(CGI->generaltexth->restypes[id]);
|
||||
break;
|
||||
case EType::PLAYER:
|
||||
GH.statusbar()->write(CGI->generaltexth->capColors[id]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -372,6 +379,8 @@ PlayersPanel::PlayersPanel(CTradeableItem::ClickPressedFunctor clickPressedCallb
|
||||
slot->subtitle = CGI->generaltexth->capColors[players[slotNum].num];
|
||||
slotNum++;
|
||||
}
|
||||
selectedImage = std::make_shared<CAnimImage>(AnimationPath::builtin("CREST58"), 0, 0, selectedImagePos.x, selectedImagePos.y);
|
||||
selectedSubtitle = std::make_shared<CLabel>(selectedSubtitlePos.x, selectedSubtitlePos.y, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
|
||||
}
|
||||
|
||||
CreaturesPanel::CreaturesPanel(CTradeableItem::ClickPressedFunctor clickPressedCallback, const slotsData & initialSlots)
|
||||
|
||||
@@ -122,6 +122,8 @@ class PlayersPanel : public TradePanelBase
|
||||
Point(83, 236)
|
||||
};
|
||||
const Point slotDimension = Point(58, 64);
|
||||
const Point selectedImagePos = Point(83, 368);
|
||||
const Point selectedSubtitlePos = Point(112, 443);
|
||||
|
||||
public:
|
||||
explicit PlayersPanel(CTradeableItem::ClickPressedFunctor clickPressedCallback);
|
||||
|
||||
Reference in New Issue
Block a user