Refactor BarkBubble and Container GUI to use control binding

This commit is contained in:
Vsevolod Kremianskii 2021-06-11 16:57:21 +07:00
parent e87822556a
commit 41caea6048
4 changed files with 55 additions and 32 deletions

View file

@ -17,8 +17,6 @@
#include "barkbubble.h"
#include "../../gui/control/label.h"
#include "../game.h"
using namespace std;
@ -29,8 +27,6 @@ namespace reone {
namespace game {
static const char kBarkTextTag[] = "LBL_BARKTEXT";
BarkBubble::BarkBubble(Game *game) : GameGUI(game) {
_resRef = getResRef("barkbubble");
_scaling = ScalingMode::PositionRelativeToCenter;
@ -38,8 +34,14 @@ BarkBubble::BarkBubble(Game *game) : GameGUI(game) {
void BarkBubble::load() {
GUI::load();
bindControls();
_rootControl->setVisible(false);
hideControl(kBarkTextTag);
_binding.lblBarkText->setVisible(false);
}
void BarkBubble::bindControls() {
_binding.lblBarkText = getControlPtr<Label>("LBL_BARKTEXT");
}
void BarkBubble::update(float dt) {
@ -51,25 +53,22 @@ void BarkBubble::update(float dt) {
}
void BarkBubble::setBarkText(const string &text, float duration) {
Label &lblBarkText = getControl<Label>(kBarkTextTag);
if (text.empty()) {
_rootControl->setVisible(false);
lblBarkText.setVisible(false);
_binding.lblBarkText->setVisible(false);
} else {
float textWidth = lblBarkText.text().font->measure(text);
int lineCount = textWidth / static_cast<float>(lblBarkText.extent().width) + 1;
int padding = lblBarkText.extent().left;
float rootHeight = lineCount * lblBarkText.text().font->height() + 2 * padding;
float labelHeight = lineCount * lblBarkText.text().font->height();
float textWidth = _binding.lblBarkText->text().font->measure(text);
int lineCount = textWidth / static_cast<float>(_binding.lblBarkText->extent().width) + 1;
int padding = _binding.lblBarkText->extent().left;
float rootHeight = lineCount * _binding.lblBarkText->text().font->height() + 2 * padding;
float labelHeight = lineCount * _binding.lblBarkText->text().font->height();
_rootControl->setVisible(true);
_rootControl->setExtentHeight(static_cast<int>(rootHeight));
lblBarkText.setExtentHeight(static_cast<int>(labelHeight));
lblBarkText.setTextMessage(text);
lblBarkText.setVisible(true);
_binding.lblBarkText->setExtentHeight(static_cast<int>(labelHeight));
_binding.lblBarkText->setTextMessage(text);
_binding.lblBarkText->setVisible(true);
}
if (duration > 0.0f) {

View file

@ -20,6 +20,7 @@
#include "gui.h"
#include "../../common/timer.h"
#include "../../gui/control/label.h"
namespace reone {
@ -35,8 +36,14 @@ public:
void setBarkText(const std::string &text, float duration);
private:
struct Binding {
std::shared_ptr<gui::Label> lblBarkText;
} _binding;
std::string _barkText;
Timer _timer;
void bindControls();
};
} // namespace game

View file

@ -17,9 +17,8 @@
#include "container.h"
#include "../../gui/control/imagebutton.h"
#include "../../gui/control/listbox.h"
#include "../../graphics/texture/textures.h"
#include "../../gui/control/imagebutton.h"
#include "../../resource/strings.h"
#include "../game.h"
@ -51,21 +50,27 @@ Container::Container(Game *game) : GameGUI(game) {
void Container::load() {
GUI::load();
bindControls();
string btnMessage(_game->services().resource().strings().get(kSwitchToResRef) + " " + _game->services().resource().strings().get(kGiveItemResRef));
Control &btnGiveItems = getControl("BTN_GIVEITEMS");
btnGiveItems.setTextMessage(btnMessage);
_binding.btnGiveItems->setTextMessage(btnMessage);
string lblMessage(_game->services().resource().strings().get(kInventoryResRef));
getControl("LBL_MESSAGE").setTextMessage(lblMessage);
_binding.lblMessage->setTextMessage(lblMessage);
configureItemsListBox();
}
void Container::bindControls() {
_binding.lblMessage = getControlPtr<Label>("LBL_MESSAGE");
_binding.lbItems = getControlPtr<ListBox>("LB_ITEMS");
_binding.btnOk = getControlPtr<Button>("BTN_OK");
_binding.btnGiveItems = getControlPtr<Button>("BTN_GIVEITEMS");
_binding.btnCancel = getControlPtr<Button>("BTN_CANCEL");
}
void Container::configureItemsListBox() {
ListBox &listBox = static_cast<ListBox &>(getControl("LB_ITEMS"));
ImageButton &protoItem = static_cast<ImageButton &>(listBox.protoItem());
ImageButton &protoItem = static_cast<ImageButton &>(_binding.lbItems->protoItem());
Control::Text text(protoItem.text());
text.align = Control::TextAlign::LeftTop;
@ -73,11 +78,8 @@ void Container::configureItemsListBox() {
protoItem.setText(text);
}
void Container::open(const shared_ptr<SpatialObject> &container) {
_container = container;
ListBox &lbItems = static_cast<ListBox &>(getControl("LB_ITEMS"));
lbItems.clearItems();
void Container::open(shared_ptr<SpatialObject> container) {
_binding.lbItems->clearItems();
for (auto &item : container->items()) {
if (!item->isDropable()) continue;
@ -91,8 +93,10 @@ void Container::open(const shared_ptr<SpatialObject> &container) {
if (item->stackSize() > 1) {
lbItem.iconText = to_string(item->stackSize());
}
lbItems.addItem(move(lbItem));
_binding.lbItems->addItem(move(lbItem));
}
_container = move(container);
}
shared_ptr<Texture> Container::getItemFrameTexture(int stackSize) const {

View file

@ -19,6 +19,10 @@
#include "gui.h"
#include "../../gui/control/button.h"
#include "../../gui/control/label.h"
#include "../../gui/control/listbox.h"
#include "../object/spatial.h"
namespace reone {
@ -30,13 +34,22 @@ public:
Container(Game *game);
void load() override;
void open(const std::shared_ptr<SpatialObject> &contanier);
void open(std::shared_ptr<SpatialObject> contanier);
SpatialObject &container() const { return *_container; }
private:
struct Binding {
std::shared_ptr<gui::Label> lblMessage;
std::shared_ptr<gui::ListBox> lbItems;
std::shared_ptr<gui::Button> btnOk;
std::shared_ptr<gui::Button> btnGiveItems;
std::shared_ptr<gui::Button> btnCancel;
} _binding;
std::shared_ptr<SpatialObject> _container;
void bindControls();
void configureItemsListBox();
void transferItemsToPlayer();