1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

Configurable turn timer

This commit is contained in:
nordsoft 2023-08-14 01:13:37 +04:00
parent 093e226bff
commit f01973a4f0
4 changed files with 102 additions and 35 deletions

View File

@ -11,8 +11,7 @@
#include "TurnTimerWidget.h"
#include "../CPlayerInterface.h"
#include "../render/Canvas.h"
#include "../render/Colors.h"
#include "../render/EFont.h"
#include "../gui/CGuiHandler.h"
#include "../gui/TextAlignment.h"
@ -20,28 +19,41 @@
#include "../widgets/TextControls.h"
#include "../../CCallback.h"
#include "../../lib/CPlayerState.h"
#include "../../lib/filesystem/ResourceID.h"
#include <SDL_render.h>
TurnTimerWidget::TurnTimerWidget():
CIntObject(TIME),
turnTime(0), lastTurnTime(0)
TurnTimerWidget::DrawRect::DrawRect(const Rect & r, const ColorRGBA & c):
CIntObject(), rect(r), color(c)
{
OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
watches = std::make_shared<CAnimImage>("VCMI/BATTLEQUEUE/STATESSMALL", 1, 0, 4, 6);
label = std::make_shared<CLabel>(24, 2, FONT_BIG, ETextAlignment::TOPLEFT, Colors::YELLOW, std::to_string(turnTime));
recActions &= ~DEACTIVATE;
}
void TurnTimerWidget::showAll(Canvas & to)
void TurnTimerWidget::DrawRect::showAll(Canvas & to)
{
to.drawColor(Rect(4, 4, 68, 24), ColorRGBA(10, 10, 10, 255));
to.drawColor(rect, color);
CIntObject::showAll(to);
}
TurnTimerWidget::TurnTimerWidget():
InterfaceObjectConfigurable(TIME),
turnTime(0), lastTurnTime(0)
{
REGISTER_BUILDER("drawRect", &TurnTimerWidget::buildDrawRect);
recActions &= ~DEACTIVATE;
const JsonNode config(ResourceID("config/widgets/turnTimer.json"));
build(config);
}
std::shared_ptr<TurnTimerWidget::DrawRect> TurnTimerWidget::buildDrawRect(const JsonNode & config) const
{
logGlobal->debug("Building widget TurnTimerWidget::DrawRect");
auto rect = readRect(config["rect"]);
auto color = readColor(config["color"]);
return std::make_shared<TurnTimerWidget::DrawRect>(rect, color);
}
void TurnTimerWidget::show(Canvas & to)
{
showAll(to);
@ -50,9 +62,12 @@ void TurnTimerWidget::show(Canvas & to)
void TurnTimerWidget::setTime(int time)
{
turnTime = time / 1000;
std::ostringstream oss;
oss << turnTime / 60 << ":" << std::setw(2) << std::setfill('0') << turnTime % 60;
label->setText(oss.str());
if(auto w = widget<CLabel>("timer"))
{
std::ostringstream oss;
oss << turnTime / 60 << ":" << std::setw(2) << std::setfill('0') << turnTime % 60;
w->setText(oss.str());
}
}
void TurnTimerWidget::tick(uint32_t msPassed)

View File

@ -11,26 +11,40 @@
#pragma once
#include "../gui/CIntObject.h"
#include "../gui/InterfaceObjectConfigurable.h"
#include "../render/Canvas.h"
#include "../render/Colors.h"
class CAnimImage;
class CLabel;
class TurnTimerWidget : public CIntObject
class TurnTimerWidget : public InterfaceObjectConfigurable
{
private:
class DrawRect : public CIntObject
{
const Rect rect;
const ColorRGBA color;
public:
DrawRect(const Rect &, const ColorRGBA &);
void showAll(Canvas & to) override;
};
int turnTime;
int lastTurnTime;
int cachedTurnTime;
std::shared_ptr<CAnimImage> watches;
std::shared_ptr<CLabel> label;
//std::shared_ptr<CAnimImage> watches;
//std::shared_ptr<CLabel> label;
std::shared_ptr<DrawRect> buildDrawRect(const JsonNode & config) const;
public:
//void tick(uint32_t msPassed) override;
void show(Canvas & to) override;
void showAll(Canvas & to) override;
void tick(uint32_t msPassed) override;
void setTime(int time);

View File

@ -190,18 +190,29 @@ ColorRGBA InterfaceObjectConfigurable::readColor(const JsonNode & config) const
logGlobal->debug("Reading color");
if(!config.isNull())
{
if(config.String() == "yellow")
return Colors::YELLOW;
if(config.String() == "white")
return Colors::WHITE;
if(config.String() == "gold")
return Colors::METALLIC_GOLD;
if(config.String() == "green")
return Colors::GREEN;
if(config.String() == "orange")
return Colors::ORANGE;
if(config.String() == "bright-yellow")
return Colors::BRIGHT_YELLOW;
if(config.isString())
{
if(config.String() == "yellow")
return Colors::YELLOW;
if(config.String() == "white")
return Colors::WHITE;
if(config.String() == "gold")
return Colors::METALLIC_GOLD;
if(config.String() == "green")
return Colors::GREEN;
if(config.String() == "orange")
return Colors::ORANGE;
if(config.String() == "bright-yellow")
return Colors::BRIGHT_YELLOW;
}
if(config.isVector())
{
const auto & asVector = config.Vector();
if(asVector.size() == 4)
return ColorRGBA(asVector[0].Integer(), asVector[1].Integer(), asVector[2].Integer(), asVector[3].Integer());
if(asVector.size() == 3)
return ColorRGBA(asVector[0].Integer(), asVector[1].Integer(), asVector[2].Integer());
}
}
logGlobal->debug("Uknown color attribute");
return Colors::DEFAULT_KEY_COLOR;

View File

@ -0,0 +1,27 @@
{
"items":
[
{ //backgound color
"type": "drawRect",
"rect": {"x": 4, "y": 4, "w": 68, "h": 24},
"color": [10, 10, 10, 255]
},
{ //clocks icon
"type": "image",
"image": "VCMI/BATTLEQUEUE/STATESSMALL",
"frame": 1,
"position": {"x": 4, "y": 6}
},
{ //timer field label
"name": "timer",
"type": "label",
"font": "big",
"alignment": "left",
"color": "yellow",
"text": "",
"position": {"x": 24, "y": 2}
},
]
}