1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-01-26 03:52:01 +02:00

Template selections works

This commit is contained in:
nordsoft 2022-12-13 04:38:18 +04:00
parent f90cb1be90
commit 1e2abae62b
2 changed files with 101 additions and 17 deletions

View File

@ -28,6 +28,7 @@
#include "../../lib/mapping/CMapInfo.h"
#include "../../lib/rmg/CMapGenOptions.h"
#include "../../lib/CModHandler.h"
#include "../../lib/rmg/CRmgTemplateStorage.h"
RandomMapTab::RandomMapTab():
InterfaceObjectConfigurable()
@ -108,7 +109,7 @@ RandomMapTab::RandomMapTab():
//new callbacks available only from mod
addCallback("templateSelection", [&](int)
{
GH.pushInt(std::shared_ptr<TemplatesDropBox>(new TemplatesDropBox(mapGenOptions)));
GH.pushInt(std::make_shared<TemplatesDropBox>(this));
});
@ -190,6 +191,18 @@ void RandomMapTab::setMapGenOptions(std::shared_ptr<CMapGenOptions> opts)
w->setSelected(opts->getMonsterStrength());
}
void RandomMapTab::setTemplate(const CRmgTemplate * tmpl)
{
mapGenOptions->setMapTemplate(tmpl);
if(auto w = widget<CButton>("templateButton"))
{
if(tmpl)
w->addTextOverlay(tmpl->getName(), EFonts::FONT_SMALL);
else
w->addTextOverlay("default", EFonts::FONT_SMALL);
}
}
void RandomMapTab::deactivateButtonsFrom(CToggleGroup * group, int startId)
{
logGlobal->debug("Blocking buttons from %d", startId);
@ -255,11 +268,12 @@ std::vector<int> RandomMapTab::getPossibleMapSizes()
return {CMapHeader::MAP_SIZE_SMALL, CMapHeader::MAP_SIZE_MIDDLE, CMapHeader::MAP_SIZE_LARGE, CMapHeader::MAP_SIZE_XLARGE, CMapHeader::MAP_SIZE_HUGE, CMapHeader::MAP_SIZE_XHUGE, CMapHeader::MAP_SIZE_GIANT};
}
TemplatesDropBox::ListItem::ListItem(Point position, const std::string & text)
: CIntObject(LCLICK | HOVER, position)
TemplatesDropBox::ListItem::ListItem(TemplatesDropBox * _dropBox, Point position)
: CIntObject(LCLICK | HOVER, position),
dropBox(_dropBox)
{
OBJ_CONSTRUCTION;
labelName = std::make_shared<CLabel>(0, 0, FONT_SMALL, EAlignment::TOPLEFT, Colors::WHITE, text);
labelName = std::make_shared<CLabel>(0, 0, FONT_SMALL, EAlignment::TOPLEFT, Colors::WHITE);
labelName->setAutoRedraw(false);
hoverImage = std::make_shared<CPicture>("List10Sl", 0, 0);
@ -270,26 +284,63 @@ TemplatesDropBox::ListItem::ListItem(Point position, const std::string & text)
type |= REDRAW_PARENT;
}
void TemplatesDropBox::ListItem::updateItem(int idx, const CRmgTemplate * _item)
{
item = _item;
if(item)
{
labelName->setText(item->getName());
}
else
{
if(idx)
labelName->setText("");
else
labelName->setText("default");
}
}
void TemplatesDropBox::ListItem::hover(bool on)
{
hoverImage->visible = on;
if(labelName->getText().empty())
{
hovered = false;
hoverImage->visible = false;
}
else
{
hoverImage->visible = on;
}
redraw();
}
TemplatesDropBox::TemplatesDropBox(std::shared_ptr<CMapGenOptions> options):
CIntObject(LCLICK | HOVER),
mapGenOptions(options)
void TemplatesDropBox::ListItem::clickLeft(tribool down, bool previousState)
{
if(down && hovered)
{
dropBox->setTemplate(item);
}
}
TemplatesDropBox::TemplatesDropBox(RandomMapTab * randomMapTab):
CIntObject(LCLICK | HOVER),
randomMapTab(randomMapTab)
{
curItems = VLC->tplh->getTemplates();
curItems.insert(curItems.begin(), nullptr); //default template
OBJ_CONSTRUCTION;
background = std::make_shared<CPicture>("List10Bk", 158, 76);
int positionsToShow = 10;
for(int i = 0; i < positionsToShow; i++)
listItems.push_back(std::make_shared<ListItem>(Point(158, 76 + i * 25), "test" + std::to_string(i)));
listItems.push_back(std::make_shared<ListItem>(this, Point(158, 76 + i * 25)));
slider = std::make_shared<CSlider>(Point(212 + 158, 76), 252, std::bind(&TemplatesDropBox::sliderMove, this, _1), positionsToShow, 20, 0, false, CSlider::BLUE);
slider = std::make_shared<CSlider>(Point(212 + 158, 76), 252, std::bind(&TemplatesDropBox::sliderMove, this, _1), positionsToShow, (int)curItems.size(), 0, false, CSlider::BLUE);
updateListItems();
pos = background->pos;
}
@ -297,7 +348,7 @@ void TemplatesDropBox::sliderMove(int slidPos)
{
if(!slider)
return; // ignore spurious call when slider is being created
//updateListItems();
updateListItems();
redraw();
}
@ -308,9 +359,33 @@ void TemplatesDropBox::hover(bool on)
void TemplatesDropBox::clickLeft(tribool down, bool previousState)
{
if(!hovered)
if(down && !hovered)
{
assert(GH.topInt().get() == this);
GH.popInt(GH.topInt());
}
}
void TemplatesDropBox::updateListItems()
{
int elemIdx = slider->getValue();
for(auto item : listItems)
{
if(elemIdx < curItems.size())
{
item->updateItem(elemIdx, curItems[elemIdx]);
elemIdx++;
}
else
{
item->updateItem(elemIdx);
}
}
}
void TemplatesDropBox::setTemplate(const CRmgTemplate * tmpl)
{
randomMapTab->setTemplate(tmpl);
assert(GH.topInt().get() == this);
GH.popInt(GH.topInt());
}

View File

@ -13,6 +13,7 @@
#include "../../lib/FunctionList.h"
#include "../../lib/GameConstants.h"
#include "../../lib/rmg/CRmgTemplate.h"
#include "../gui/InterfaceObjectConfigurable.h"
VCMI_LIB_NAMESPACE_BEGIN
@ -33,6 +34,7 @@ public:
void updateMapInfoByHost();
void setMapGenOptions(std::shared_ptr<CMapGenOptions> opts);
void setTemplate(const CRmgTemplate *);
CFunctionList<void(std::shared_ptr<CMapInfo>, std::shared_ptr<CMapGenOptions>)> mapInfoChanged;
@ -52,26 +54,33 @@ class TemplatesDropBox : public CIntObject
{
std::shared_ptr<CLabel> labelName;
std::shared_ptr<CPicture> hoverImage;
ListItem(Point position, const std::string & text);
void updateItem(int info = 0, bool selected = false);
TemplatesDropBox * dropBox;
const CRmgTemplate * item = nullptr;
ListItem(TemplatesDropBox *, Point position);
void updateItem(int index, const CRmgTemplate * item = nullptr);
void hover(bool on) override;
void clickLeft(tribool down, bool previousState) override;
};
public:
TemplatesDropBox(std::shared_ptr<CMapGenOptions> options);
TemplatesDropBox(RandomMapTab * randomMapTab);
void hover(bool on) override;
void clickLeft(tribool down, bool previousState) override;
void setTemplate(const CRmgTemplate *);
private:
void sliderMove(int slidPos);
void updateListItems();
std::shared_ptr<CMapGenOptions> mapGenOptions;
RandomMapTab * randomMapTab;
std::shared_ptr<CPicture> background;
std::vector<std::shared_ptr<ListItem>> listItems;
std::shared_ptr<CSlider> slider;
std::vector<const CRmgTemplate *> curItems;
};