mirror of
https://github.com/vcmi/vcmi.git
synced 2025-09-16 09:26:28 +02:00
Configurable template selection
This commit is contained in:
@@ -26,14 +26,14 @@
|
|||||||
#include "../../lib/CGeneralTextHandler.h"
|
#include "../../lib/CGeneralTextHandler.h"
|
||||||
|
|
||||||
|
|
||||||
InterfaceObjectConfigurable::InterfaceObjectConfigurable(const JsonNode & config):
|
InterfaceObjectConfigurable::InterfaceObjectConfigurable(const JsonNode & config, int used, Point offset):
|
||||||
CIntObject()
|
CIntObject(used, offset)
|
||||||
{
|
{
|
||||||
init(config);
|
init(config);
|
||||||
}
|
}
|
||||||
|
|
||||||
InterfaceObjectConfigurable::InterfaceObjectConfigurable():
|
InterfaceObjectConfigurable::InterfaceObjectConfigurable(int used, Point offset):
|
||||||
CIntObject()
|
CIntObject(used, offset)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,6 +55,12 @@ void InterfaceObjectConfigurable::init(const JsonNode &config)
|
|||||||
: item["name"].String();
|
: item["name"].String();
|
||||||
widgets[name] = buildWidget(item);
|
widgets[name] = buildWidget(item);
|
||||||
}
|
}
|
||||||
|
variables = config["variables"];
|
||||||
|
}
|
||||||
|
|
||||||
|
const JsonNode & InterfaceObjectConfigurable::variable(const std::string & name) const
|
||||||
|
{
|
||||||
|
return variables[name];
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string InterfaceObjectConfigurable::readText(const JsonNode & config) const
|
std::string InterfaceObjectConfigurable::readText(const JsonNode & config) const
|
||||||
@@ -153,7 +159,10 @@ std::shared_ptr<CPicture> InterfaceObjectConfigurable::buildPicture(const JsonNo
|
|||||||
{
|
{
|
||||||
auto image = readText(config["image"]);
|
auto image = readText(config["image"]);
|
||||||
auto position = readPosition(config["position"]);
|
auto position = readPosition(config["position"]);
|
||||||
return std::make_shared<CPicture>(image, position.x, position.y);
|
auto pic = std::make_shared<CPicture>(image, position.x, position.y);
|
||||||
|
if(!config["visible"].isNull())
|
||||||
|
pic->visible = config["visible"].Bool();
|
||||||
|
return pic;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<CLabel> InterfaceObjectConfigurable::buildLabel(const JsonNode & config) const
|
std::shared_ptr<CLabel> InterfaceObjectConfigurable::buildLabel(const JsonNode & config) const
|
||||||
@@ -243,6 +252,18 @@ std::shared_ptr<CLabelGroup> InterfaceObjectConfigurable::buildLabelGroup(const
|
|||||||
return group;
|
return group;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<CSlider> InterfaceObjectConfigurable::buildSlider(const JsonNode & config) const
|
||||||
|
{
|
||||||
|
auto position = readPosition(config["position"]);
|
||||||
|
int length = config["size"].Integer();
|
||||||
|
auto style = config["style"].String() == "brown" ? CSlider::BROWN : CSlider::BLUE;
|
||||||
|
auto itemsVisible = config["itemsVisible"].Integer();
|
||||||
|
auto itemsTotal = config["itemsTotal"].Integer();
|
||||||
|
auto value = config["selected"].Integer();
|
||||||
|
bool horizontal = config["orientation"].String() == "horizontal";
|
||||||
|
return std::make_shared<CSlider>(position, length, callbacks.at(config["callback"].String()), itemsVisible, itemsTotal, value, horizontal, style);
|
||||||
|
}
|
||||||
|
|
||||||
std::shared_ptr<CIntObject> InterfaceObjectConfigurable::buildWidget(const JsonNode & config) const
|
std::shared_ptr<CIntObject> InterfaceObjectConfigurable::buildWidget(const JsonNode & config) const
|
||||||
{
|
{
|
||||||
assert(!config.isNull());
|
assert(!config.isNull());
|
||||||
@@ -271,14 +292,18 @@ std::shared_ptr<CIntObject> InterfaceObjectConfigurable::buildWidget(const JsonN
|
|||||||
{
|
{
|
||||||
return buildLabelGroup(config);
|
return buildLabelGroup(config);
|
||||||
}
|
}
|
||||||
|
if(type == "slider")
|
||||||
|
{
|
||||||
|
return buildSlider(config);
|
||||||
|
}
|
||||||
if(type == "custom")
|
if(type == "custom")
|
||||||
{
|
{
|
||||||
return buildCustomWidget(config);
|
return const_cast<InterfaceObjectConfigurable*>(this)->buildCustomWidget(config);
|
||||||
}
|
}
|
||||||
return std::shared_ptr<CIntObject>(nullptr);
|
return std::shared_ptr<CIntObject>(nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<CIntObject> InterfaceObjectConfigurable::buildCustomWidget(const JsonNode & config) const
|
std::shared_ptr<CIntObject> InterfaceObjectConfigurable::buildCustomWidget(const JsonNode & config)
|
||||||
{
|
{
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
@@ -20,12 +20,13 @@ class CToggleGroup;
|
|||||||
class CToggleButton;
|
class CToggleButton;
|
||||||
class CButton;
|
class CButton;
|
||||||
class CLabelGroup;
|
class CLabelGroup;
|
||||||
|
class CSlider;
|
||||||
|
|
||||||
class InterfaceObjectConfigurable: public CIntObject
|
class InterfaceObjectConfigurable: public CIntObject
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
InterfaceObjectConfigurable();
|
InterfaceObjectConfigurable(int used=0, Point offset=Point());
|
||||||
InterfaceObjectConfigurable(const JsonNode & config);
|
InterfaceObjectConfigurable(const JsonNode & config, int used=0, Point offset=Point());
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
//must be called after adding callbacks
|
//must be called after adding callbacks
|
||||||
@@ -42,9 +43,8 @@ protected:
|
|||||||
return std::dynamic_pointer_cast<T>(iter->second);
|
return std::dynamic_pointer_cast<T>(iter->second);
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual std::shared_ptr<CIntObject> buildCustomWidget(const JsonNode & config) const;
|
const JsonNode & variable(const std::string & name) const;
|
||||||
|
|
||||||
private: //field deserializers
|
|
||||||
//basic serializers
|
//basic serializers
|
||||||
Point readPosition(const JsonNode &) const;
|
Point readPosition(const JsonNode &) const;
|
||||||
ETextAlignment readTextAlignment(const JsonNode &) const;
|
ETextAlignment readTextAlignment(const JsonNode &) const;
|
||||||
@@ -60,12 +60,15 @@ private: //field deserializers
|
|||||||
std::shared_ptr<CToggleButton> buildToggleButton(const JsonNode &) const;
|
std::shared_ptr<CToggleButton> buildToggleButton(const JsonNode &) const;
|
||||||
std::shared_ptr<CButton> buildButton(const JsonNode &) const;
|
std::shared_ptr<CButton> buildButton(const JsonNode &) const;
|
||||||
std::shared_ptr<CLabelGroup> buildLabelGroup(const JsonNode &) const;
|
std::shared_ptr<CLabelGroup> buildLabelGroup(const JsonNode &) const;
|
||||||
|
std::shared_ptr<CSlider> buildSlider(const JsonNode &) const;
|
||||||
|
|
||||||
|
//composite widgets
|
||||||
|
virtual std::shared_ptr<CIntObject> buildCustomWidget(const JsonNode & config);
|
||||||
std::shared_ptr<CIntObject> buildWidget(const JsonNode & config) const;
|
std::shared_ptr<CIntObject> buildWidget(const JsonNode & config) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
std::map<std::string, std::shared_ptr<CIntObject>> widgets;
|
std::map<std::string, std::shared_ptr<CIntObject>> widgets;
|
||||||
std::map<std::string, std::function<void(int)>> callbacks;
|
std::map<std::string, std::function<void(int)>> callbacks;
|
||||||
|
JsonNode variables;
|
||||||
};
|
};
|
||||||
|
@@ -302,48 +302,56 @@ 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};
|
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(TemplatesDropBox & _dropBox, Point position)
|
TemplatesDropBox::ListItem::ListItem(const JsonNode & config, TemplatesDropBox & _dropBox, Point position)
|
||||||
: CIntObject(LCLICK | HOVER, position),
|
: InterfaceObjectConfigurable(LCLICK | HOVER, position),
|
||||||
dropBox(_dropBox)
|
dropBox(_dropBox)
|
||||||
{
|
{
|
||||||
OBJ_CONSTRUCTION;
|
OBJ_CONSTRUCTION;
|
||||||
labelName = std::make_shared<CLabel>(0, 0, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE);
|
|
||||||
labelName->setAutoRedraw(false);
|
|
||||||
|
|
||||||
hoverImage = std::make_shared<CPicture>("List10Sl", 0, 0);
|
init(config);
|
||||||
hoverImage->visible = false;
|
|
||||||
|
|
||||||
pos.w = hoverImage->pos.w;
|
if(auto w = widget<CPicture>("hoverImage"))
|
||||||
pos.h = hoverImage->pos.h;
|
{
|
||||||
|
pos.w = w->pos.w;
|
||||||
|
pos.h = w->pos.h;
|
||||||
|
}
|
||||||
type |= REDRAW_PARENT;
|
type |= REDRAW_PARENT;
|
||||||
}
|
}
|
||||||
|
|
||||||
void TemplatesDropBox::ListItem::updateItem(int idx, const CRmgTemplate * _item)
|
void TemplatesDropBox::ListItem::updateItem(int idx, const CRmgTemplate * _item)
|
||||||
{
|
{
|
||||||
|
if(auto w = widget<CLabel>("labelName"))
|
||||||
|
{
|
||||||
item = _item;
|
item = _item;
|
||||||
if(item)
|
if(item)
|
||||||
{
|
{
|
||||||
labelName->setText(item->getName());
|
w->setText(item->getName());
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if(idx)
|
if(idx)
|
||||||
labelName->setText("");
|
w->setText("");
|
||||||
else
|
else
|
||||||
labelName->setText("default");
|
w->setText("default");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void TemplatesDropBox::ListItem::hover(bool on)
|
void TemplatesDropBox::ListItem::hover(bool on)
|
||||||
{
|
{
|
||||||
if(labelName->getText().empty())
|
auto h = widget<CPicture>("hoverImage");
|
||||||
|
auto w = widget<CLabel>("labelName");
|
||||||
|
if(h && w)
|
||||||
|
{
|
||||||
|
if(w->getText().empty())
|
||||||
{
|
{
|
||||||
hovered = false;
|
hovered = false;
|
||||||
hoverImage->visible = false;
|
h->visible = false;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
hoverImage->visible = on;
|
h->visible = on;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
redraw();
|
redraw();
|
||||||
}
|
}
|
||||||
@@ -358,32 +366,43 @@ void TemplatesDropBox::ListItem::clickLeft(tribool down, bool previousState)
|
|||||||
|
|
||||||
|
|
||||||
TemplatesDropBox::TemplatesDropBox(RandomMapTab & randomMapTab, int3 size):
|
TemplatesDropBox::TemplatesDropBox(RandomMapTab & randomMapTab, int3 size):
|
||||||
CIntObject(LCLICK | HOVER),
|
InterfaceObjectConfigurable(LCLICK | HOVER),
|
||||||
randomMapTab(randomMapTab)
|
randomMapTab(randomMapTab)
|
||||||
{
|
{
|
||||||
curItems = VLC->tplh->getTemplates();
|
curItems = VLC->tplh->getTemplates();
|
||||||
vstd::erase_if(curItems, [size](const CRmgTemplate * t){return !t->matchesSize(size);});
|
vstd::erase_if(curItems, [size](const CRmgTemplate * t){return !t->matchesSize(size);});
|
||||||
curItems.insert(curItems.begin(), nullptr); //default template
|
curItems.insert(curItems.begin(), nullptr); //default template
|
||||||
|
|
||||||
|
const JsonNode config(ResourceID("config/windows/randomMapTemplateWidget.json"));
|
||||||
|
|
||||||
|
addCallback("sliderMove", std::bind(&TemplatesDropBox::sliderMove, this, std::placeholders::_1));
|
||||||
|
|
||||||
OBJ_CONSTRUCTION;
|
OBJ_CONSTRUCTION;
|
||||||
pos = randomMapTab.pos.topLeft();
|
pos = randomMapTab.pos.topLeft();
|
||||||
pos.w = randomMapTab.pos.w;
|
pos.w = randomMapTab.pos.w;
|
||||||
pos.h = randomMapTab.pos.h;
|
pos.h = randomMapTab.pos.h;
|
||||||
background = std::make_shared<CPicture>("List10Bk", 158, 76);
|
|
||||||
|
|
||||||
int positionsToShow = 10;
|
init(config);
|
||||||
|
|
||||||
for(int i = 0; i < positionsToShow; i++)
|
if(auto w = widget<CSlider>("slider"))
|
||||||
listItems.push_back(std::make_shared<ListItem>(*this, Point(158, 76 + i * 25)));
|
{
|
||||||
|
w->setAmount(curItems.size());
|
||||||
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();
|
updateListItems();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<CIntObject> TemplatesDropBox::buildCustomWidget(const JsonNode & config)
|
||||||
|
{
|
||||||
|
auto position = readPosition(config["position"]);
|
||||||
|
listItems.push_back(std::make_shared<ListItem>(config, *this, position));
|
||||||
|
return listItems.back();
|
||||||
|
}
|
||||||
|
|
||||||
void TemplatesDropBox::sliderMove(int slidPos)
|
void TemplatesDropBox::sliderMove(int slidPos)
|
||||||
{
|
{
|
||||||
if(!slider)
|
auto w = widget<CSlider>("slider");
|
||||||
|
if(!w)
|
||||||
return; // ignore spurious call when slider is being created
|
return; // ignore spurious call when slider is being created
|
||||||
updateListItems();
|
updateListItems();
|
||||||
redraw();
|
redraw();
|
||||||
@@ -405,7 +424,9 @@ void TemplatesDropBox::clickLeft(tribool down, bool previousState)
|
|||||||
|
|
||||||
void TemplatesDropBox::updateListItems()
|
void TemplatesDropBox::updateListItems()
|
||||||
{
|
{
|
||||||
int elemIdx = slider->getValue();
|
if(auto w = widget<CSlider>("slider"))
|
||||||
|
{
|
||||||
|
int elemIdx = w->getValue();
|
||||||
for(auto item : listItems)
|
for(auto item : listItems)
|
||||||
{
|
{
|
||||||
if(elemIdx < curItems.size())
|
if(elemIdx < curItems.size())
|
||||||
@@ -418,6 +439,7 @@ void TemplatesDropBox::updateListItems()
|
|||||||
item->updateItem(elemIdx);
|
item->updateItem(elemIdx);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void TemplatesDropBox::setTemplate(const CRmgTemplate * tmpl)
|
void TemplatesDropBox::setTemplate(const CRmgTemplate * tmpl)
|
||||||
|
@@ -49,16 +49,14 @@ private:
|
|||||||
std::set<int> playerCountAllowed, playerTeamsAllowed, compCountAllowed, compTeamsAllowed;
|
std::set<int> playerCountAllowed, playerTeamsAllowed, compCountAllowed, compTeamsAllowed;
|
||||||
};
|
};
|
||||||
|
|
||||||
class TemplatesDropBox : public CIntObject
|
class TemplatesDropBox : public InterfaceObjectConfigurable
|
||||||
{
|
{
|
||||||
struct ListItem : public CIntObject
|
struct ListItem : public InterfaceObjectConfigurable
|
||||||
{
|
{
|
||||||
std::shared_ptr<CLabel> labelName;
|
|
||||||
std::shared_ptr<CPicture> hoverImage;
|
|
||||||
TemplatesDropBox & dropBox;
|
TemplatesDropBox & dropBox;
|
||||||
const CRmgTemplate * item = nullptr;
|
const CRmgTemplate * item = nullptr;
|
||||||
|
|
||||||
ListItem(TemplatesDropBox &, Point position);
|
ListItem(const JsonNode &, TemplatesDropBox &, Point position);
|
||||||
void updateItem(int index, const CRmgTemplate * item = nullptr);
|
void updateItem(int index, const CRmgTemplate * item = nullptr);
|
||||||
|
|
||||||
void hover(bool on) override;
|
void hover(bool on) override;
|
||||||
@@ -72,15 +70,16 @@ public:
|
|||||||
void clickLeft(tribool down, bool previousState) override;
|
void clickLeft(tribool down, bool previousState) override;
|
||||||
void setTemplate(const CRmgTemplate *);
|
void setTemplate(const CRmgTemplate *);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
std::shared_ptr<CIntObject> buildCustomWidget(const JsonNode & config) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
void sliderMove(int slidPos);
|
void sliderMove(int slidPos);
|
||||||
void updateListItems();
|
void updateListItems();
|
||||||
|
|
||||||
RandomMapTab & randomMapTab;
|
RandomMapTab & randomMapTab;
|
||||||
std::shared_ptr<CPicture> background;
|
|
||||||
std::vector<std::shared_ptr<ListItem>> listItems;
|
std::vector<std::shared_ptr<ListItem>> listItems;
|
||||||
std::shared_ptr<CSlider> slider;
|
|
||||||
|
|
||||||
std::vector<const CRmgTemplate *> curItems;
|
std::vector<const CRmgTemplate *> curItems;
|
||||||
|
|
||||||
|
@@ -566,16 +566,21 @@ void CSlider::setScrollStep(int to)
|
|||||||
scrollStep = to;
|
scrollStep = to;
|
||||||
}
|
}
|
||||||
|
|
||||||
int CSlider::getAmount()
|
int CSlider::getAmount() const
|
||||||
{
|
{
|
||||||
return amount;
|
return amount;
|
||||||
}
|
}
|
||||||
|
|
||||||
int CSlider::getValue()
|
int CSlider::getValue() const
|
||||||
{
|
{
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int CSlider::getCapacity() const
|
||||||
|
{
|
||||||
|
return capacity;
|
||||||
|
}
|
||||||
|
|
||||||
void CSlider::moveLeft()
|
void CSlider::moveLeft()
|
||||||
{
|
{
|
||||||
moveTo(value-1);
|
moveTo(value-1);
|
||||||
|
@@ -256,8 +256,9 @@ public:
|
|||||||
void setAmount(int to);
|
void setAmount(int to);
|
||||||
|
|
||||||
/// Accessors
|
/// Accessors
|
||||||
int getAmount();
|
int getAmount() const;
|
||||||
int getValue();
|
int getValue() const;
|
||||||
|
int getCapacity() const;
|
||||||
|
|
||||||
void addCallback(std::function<void(int)> callback);
|
void addCallback(std::function<void(int)> callback);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user