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

Basic timer ui is almost complete

This commit is contained in:
nordsoft 2023-08-25 20:20:26 +04:00
parent c7ded69edc
commit d758727c23
8 changed files with 196 additions and 28 deletions

View File

@ -39,6 +39,7 @@
#include "../lib/CThreadHelper.h"
#include "../lib/NetPackVisitor.h"
#include "../lib/StartInfo.h"
#include "../lib/TurnTimerInfo.h"
#include "../lib/VCMIDirs.h"
#include "../lib/campaign/CampaignState.h"
#include "../lib/mapping/CMapInfo.h"
@ -475,11 +476,10 @@ void CServerHandler::setDifficulty(int to) const
sendLobbyPack(lsd);
}
void CServerHandler::setTurnLength(int npos) const
void CServerHandler::setTurnTimerInfo(const TurnTimerInfo & info) const
{
vstd::amin(npos, GameConstants::POSSIBLE_TURNTIME.size() - 1);
LobbySetTurnTime lstt;
lstt.turnTimerInfo.turnTimer = GameConstants::POSSIBLE_TURNTIME[npos] * 60 * 1000;
lstt.turnTimerInfo = info;
sendLobbyPack(lstt);
}

View File

@ -19,6 +19,7 @@ VCMI_LIB_NAMESPACE_BEGIN
class CConnection;
class PlayerColor;
struct StartInfo;
struct TurnTimerInfo;
class CMapInfo;
class CGameState;
@ -64,7 +65,7 @@ public:
virtual void setPlayer(PlayerColor color) const = 0;
virtual void setPlayerOption(ui8 what, int32_t value, PlayerColor player) const = 0;
virtual void setDifficulty(int to) const = 0;
virtual void setTurnLength(int npos) const = 0;
virtual void setTurnTimerInfo(const TurnTimerInfo &) const = 0;
virtual void sendMessage(const std::string & txt) const = 0;
virtual void sendGuiAction(ui8 action) const = 0; // TODO: possibly get rid of it?
virtual void sendStartGame(bool allowOnlyAI = false) const = 0;
@ -146,7 +147,7 @@ public:
void setPlayer(PlayerColor color) const override;
void setPlayerOption(ui8 what, int32_t value, PlayerColor player) const override;
void setDifficulty(int to) const override;
void setTurnLength(int npos) const override;
void setTurnTimerInfo(const TurnTimerInfo &) const override;
void sendMessage(const std::string & txt) const override;
void sendGuiAction(ui8 action) const override;
void sendRestartGame() const override;

View File

@ -54,6 +54,7 @@ InterfaceObjectConfigurable::InterfaceObjectConfigurable(int used, Point offset)
REGISTER_BUILDER("slider", &InterfaceObjectConfigurable::buildSlider);
REGISTER_BUILDER("layout", &InterfaceObjectConfigurable::buildLayout);
REGISTER_BUILDER("comboBox", &InterfaceObjectConfigurable::buildComboBox);
REGISTER_BUILDER("textInput", &InterfaceObjectConfigurable::buildTextInput);
}
void InterfaceObjectConfigurable::registerBuilder(const std::string & type, BuilderFunction f)
@ -63,9 +64,15 @@ void InterfaceObjectConfigurable::registerBuilder(const std::string & type, Buil
void InterfaceObjectConfigurable::addCallback(const std::string & callbackName, std::function<void(int)> callback)
{
callbacks[callbackName] = callback;
callbacks_int[callbackName] = callback;
}
void InterfaceObjectConfigurable::addCallback(const std::string & callbackName, std::function<void(std::string)> callback)
{
callbacks_string[callbackName] = callback;
}
void InterfaceObjectConfigurable::deleteWidget(const std::string & name)
{
auto iter = widgets.find(name);
@ -340,7 +347,7 @@ std::shared_ptr<CToggleGroup> InterfaceObjectConfigurable::buildToggleGroup(cons
if(!config["selected"].isNull())
group->setSelected(config["selected"].Integer());
if(!config["callback"].isNull())
group->addCallback(callbacks.at(config["callback"].String()));
group->addCallback(callbacks_int.at(config["callback"].String()));
return group;
}
@ -413,8 +420,8 @@ void InterfaceObjectConfigurable::loadToggleButtonCallback(std::shared_ptr<CTogg
std::string callbackName = config.String();
if (callbacks.count(callbackName) > 0)
button->addCallback(callbacks.at(callbackName));
if (callbacks_int.count(callbackName) > 0)
button->addCallback(callbacks_int.at(callbackName));
else
logGlobal->error("Invalid callback '%s' in widget", callbackName );
}
@ -426,8 +433,8 @@ void InterfaceObjectConfigurable::loadButtonCallback(std::shared_ptr<CButton> bu
std::string callbackName = config.String();
if (callbacks.count(callbackName) > 0)
button->addCallback(std::bind(callbacks.at(callbackName), 0));
if (callbacks_int.count(callbackName) > 0)
button->addCallback(std::bind(callbacks_int.at(callbackName), 0));
else
logGlobal->error("Invalid callback '%s' in widget", callbackName );
}
@ -483,7 +490,7 @@ std::shared_ptr<CSlider> InterfaceObjectConfigurable::buildSlider(const JsonNode
auto value = config["selected"].Integer();
bool horizontal = config["orientation"].String() == "horizontal";
const auto & result =
std::make_shared<CSlider>(position, length, callbacks.at(config["callback"].String()), itemsVisible, itemsTotal, value, horizontal ? Orientation::HORIZONTAL : Orientation::VERTICAL, style);
std::make_shared<CSlider>(position, length, callbacks_int.at(config["callback"].String()), itemsVisible, itemsTotal, value, horizontal ? Orientation::HORIZONTAL : Orientation::VERTICAL, style);
if(!config["scrollBounds"].isNull())
{
@ -541,6 +548,26 @@ std::shared_ptr<ComboBox> InterfaceObjectConfigurable::buildComboBox(const JsonN
return result;
}
std::shared_ptr<CTextInput> InterfaceObjectConfigurable::buildTextInput(const JsonNode & config) const
{
logGlobal->debug("Building widget CTextInput");
auto rect = readRect(config["rect"]);
auto offset = readPosition(config["backgroundOffset"]);
auto bgName = config["background"].String();
auto result = std::make_shared<CTextInput>(rect, offset, bgName, 0);
if(!config["alignment"].isNull())
result->alignment = readTextAlignment(config["alignment"]);
if(!config["font"].isNull())
result->font = readFont(config["font"]);
if(!config["color"].isNull())
result->setColor(readColor(config["color"]));
if(!config["text"].isNull())
result->setText(readText(config["text"]));
if(!config["callback"].isNull())
result->cb += callbacks_string.at(config["callback"].String());
return result;
}
/// Small helper class that provides ownership for shared_ptr's of child elements
class InterfaceLayoutWidget : public CIntObject
{
@ -625,7 +652,7 @@ std::shared_ptr<CShowableAnim> InterfaceObjectConfigurable::buildAnimation(const
if(!config["alpha"].isNull())
anim->setAlpha(config["alpha"].Integer());
if(!config["callback"].isNull())
anim->callback = std::bind(callbacks.at(config["callback"].String()), 0);
anim->callback = std::bind(callbacks_int.at(config["callback"].String()), 0);
if(!config["frames"].isNull())
{
auto b = config["frames"]["start"].Integer();

View File

@ -28,6 +28,7 @@ class CAnimImage;
class CShowableAnim;
class CFilledTexture;
class ComboBox;
class CTextInput;
#define REGISTER_BUILDER(type, method) registerBuilder(type, std::bind(method, this, std::placeholders::_1))
@ -59,6 +60,7 @@ protected:
void addWidget(const std::string & name, std::shared_ptr<CIntObject> widget);
void addCallback(const std::string & callbackName, std::function<void(int)> callback);
void addCallback(const std::string & callbackName, std::function<void(std::string)> callback);
JsonNode variables;
template<class T>
@ -101,6 +103,7 @@ protected:
std::shared_ptr<CFilledTexture> buildTexture(const JsonNode &) const;
std::shared_ptr<CIntObject> buildLayout(const JsonNode &);
std::shared_ptr<ComboBox> buildComboBox(const JsonNode &);
std::shared_ptr<CTextInput> buildTextInput(const JsonNode &) const;
//composite widgets
std::shared_ptr<CIntObject> buildWidget(JsonNode config) const;
@ -116,7 +119,8 @@ private:
int unnamedObjectId = 0;
std::map<std::string, BuilderFunction> builders;
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_int;
std::map<std::string, std::function<void(std::string)>> callbacks_string;
std::map<std::string, bool> conditionals;
std::map<EShortcut, ShortcutState> shortcuts;
};

View File

@ -21,6 +21,7 @@
#include "../render/Graphics.h"
#include "../render/IFont.h"
#include "../widgets/CComponent.h"
#include "../widgets/ComboBox.h"
#include "../widgets/Buttons.h"
#include "../widgets/Images.h"
#include "../widgets/MiscWidgets.h"
@ -44,19 +45,118 @@ OptionsTab::OptionsTab() : humanPlayers(0)
{
recActions = 0;
addCallback("setTurnLength", std::bind(&IServerAPI::setTurnLength, CSH, _1));
//addCallback("timerFieldChangedBase", <#std::function<void (std::string)> callback#>)
addCallback("setTimerPreset", [&](int index){
if(!variables["timerPresets"].isNull())
{
auto tpreset = variables["timerPresets"].Vector().at(index).Vector();
TurnTimerInfo tinfo;
tinfo.baseTimer = tpreset.at(0).Integer() * 1000;
tinfo.turnTimer = tpreset.at(1).Integer() * 1000;
tinfo.battleTimer = tpreset.at(2).Integer() * 1000;
tinfo.creatureTimer = tpreset.at(3).Integer() * 1000;
CSH->setTurnTimerInfo(tinfo);
}
});
auto parseTimerString = [](const std::string & str){
std::stringstream sstrm;
int a, b;
sstrm << str;
sstrm >> a;
char c = sstrm.get();
if(c == ':')
{
sstrm >> b;
return a * 60 + b;
}
return -1;
};
addCallback("parseAndSetTimer_base", [parseTimerString](const std::string & str){
int time = parseTimerString(str) * 1000;
if(time >= 0)
{
TurnTimerInfo tinfo;
tinfo.baseTimer = time;
CSH->setTurnTimerInfo(tinfo);
}
});
addCallback("parseAndSetTimer_turn", [parseTimerString](const std::string & str){
int time = parseTimerString(str) * 1000;
if(time >= 0)
{
TurnTimerInfo tinfo;
tinfo.turnTimer = time;
CSH->setTurnTimerInfo(tinfo);
}
});
addCallback("parseAndSetTimer_battle", [parseTimerString](const std::string & str){
int time = parseTimerString(str) * 1000;
if(time >= 0)
{
TurnTimerInfo tinfo;
tinfo.battleTimer = time;
CSH->setTurnTimerInfo(tinfo);
}
});
addCallback("parseAndSetTimer_creature", [parseTimerString](const std::string & str){
int time = parseTimerString(str) * 1000;
if(time >= 0)
{
TurnTimerInfo tinfo;
tinfo.creatureTimer = time;
CSH->setTurnTimerInfo(tinfo);
}
});
const JsonNode config(ResourceID("config/widgets/optionsTab.json"));
build(config);
if(SEL->screenType == ESelectionScreen::newGame || SEL->screenType == ESelectionScreen::loadGame || SEL->screenType == ESelectionScreen::scenarioInfo)
//set timers combo box callbacks
if(auto w = widget<ComboBox>("timerModeSwitch"))
{
if(auto w = widget<CSlider>("sliderTurnDuration"))
w->deactivate();
if(auto w = widget<CLabel>("labelPlayerTurnDuration"))
w->deactivate();
if(auto w = widget<CLabel>("labelTurnDurationValue"))
w->deactivate();
w->onConstructItems = [&](std::vector<const void *> & curItems){
if(variables["timers"].isNull())
return;
for(auto & p : variables["timers"].Vector())
{
curItems.push_back(&p);
}
};
w->onSetItem = [&](const void * item){
if(item)
{
if(auto * tObj = reinterpret_cast<const JsonNode *>(item))
{
for(auto wname : (*tObj)["hideWidgets"].Vector())
if(auto w = widget<CIntObject>(wname.String()))
{
w->setEnabled(false);
}
for(auto wname : (*tObj)["showWidgets"].Vector())
if(auto w = widget<CIntObject>(wname.String()))
{
w->setEnabled(true);
}
}
redraw();
}
};
w->getItemText = [this](int idx, const void * item){
if(item)
{
if(auto * tObj = reinterpret_cast<const JsonNode *>(item))
return readText((*tObj)["text"]);
}
return std::string("");
};
w->setItem(0);
}
}
@ -76,9 +176,19 @@ void OptionsTab::recreate()
if(auto turnSlider = widget<CSlider>("sliderTurnDuration"))
{
turnSlider->scrollTo(vstd::find_pos(GameConstants::POSSIBLE_TURNTIME, SEL->getStartInfo()->turnTimerInfo.turnTimer / (60 * 1000)));
if(auto w = widget<CLabel>("labelTurnDurationValue"))
w->setText(CGI->generaltexth->turnDurations[turnSlider->getValue()]);
if(!variables["timerPresets"].isNull())
{
for(int idx = 0; idx < variables["timerPresets"].Vector().size(); ++idx)
{
auto & tpreset = variables["timerPresets"].Vector()[idx];
if(tpreset.Vector().at(1).Integer() == SEL->getStartInfo()->turnTimerInfo.turnTimer / 1000)
{
turnSlider->scrollTo(idx);
if(auto w = widget<CLabel>("labelTurnDurationValue"))
w->setText(CGI->generaltexth->turnDurations[idx]);
}
}
}
}
}

View File

@ -172,3 +172,10 @@ void ComboBox::setItem(const void * item)
if(onSetItem)
onSetItem(item);
}
void ComboBox::setItem(int id)
{
std::vector<const void *> tempItems;
onConstructItems(tempItems);
setItem(tempItems.at(id));
}

View File

@ -64,4 +64,6 @@ public:
//return text value from item data
std::function<std::string(int, const void *)> getItemText;
void setItem(int id);
};

View File

@ -75,7 +75,6 @@
// timer
{
"name": "labelPlayerTurnDuration",
"type": "label",
"font": "small",
"alignment": "center",
@ -100,7 +99,7 @@
"orientation": "horizontal",
"position": {"x": 55, "y": 557},
"size": 194,
"callback": "setTurnLength",
"callback": "setTimerPreset",
"itemsVisible": 1,
"itemsTotal": 11,
"selected": 11,
@ -108,5 +107,23 @@
"scrollBounds": {"x": -3, "y": -25, "w": 337, "h": 43},
"panningStep": 20
},
]
],
"variables":
{
"timerPresets" :
[
[0, 60, 0, 0],
[0, 120, 0, 0],
[0, 240, 0, 0],
[0, 360, 0, 0],
[0, 480, 0, 0],
[0, 600, 0, 0],
[0, 900, 0, 0],
[0, 1200, 0, 0],
[0, 1500, 0, 0],
[0, 1800, 0, 0],
[0, 0, 0, 0],
]
}
}