Peoplemon  0.1.0
Peoplemon 3 game source documentation
BagItemButton.cpp
Go to the documentation of this file.
2 
3 #include <Core/Items/Item.hpp>
4 #include <Core/Properties.hpp>
5 #include <Core/Resources.hpp>
6 
7 namespace game
8 {
9 namespace menu
10 {
12  return Ptr(new BagItemButton(item));
13 }
14 
15 BagItemButton::BagItemButton(const core::player::Bag::Item& item)
16 : item(item) {
17  getSignal(Item::Selected).willAlwaysCall(std::bind(&BagItemButton::onSelect, this));
18  getSignal(Item::Deselected).willAlwaysCall(std::bind(&BagItemButton::onDeselect, this));
19 }
20 
22  item = i;
23  if (i.id == core::item::Id::None) {
24  label.getSection().setString("Exit");
25  qty.getSection().setString("");
26  label.getTransform().setPosition(
27  {txtr->size().x * 0.5f - label.getLocalBounds().width * 0.5f,
28  txtr->size().y * 0.5f - label.getLocalBounds().height * 0.5f -
29  label.getLocalBounds().top * 0.5f});
30  }
31  else {
32  label.getSection().setString(core::item::Item::getName(item.id));
33  qty.getSection(1).setString(std::to_string(item.qty));
34  label.getTransform().setPosition(
35  8.f, txtr->size().y * 0.5f - label.getLocalBounds().height * 0.5f);
36  qty.getTransform().setPosition(txtr->size().x * 0.75f,
37  txtr->size().y * 0.5f - qty.getLocalBounds().height * 0.5f -
38  qty.getLocalBounds().top * 0.5f);
39  }
40 
41  label.getSection().setCharacterSize(22);
42  while (label.getLocalBounds().width > txtr->size().x * 0.75f - 8.f) {
43  label.getSection().setCharacterSize(label.getSection().getCharacterSize() - 1);
44  if (label.getSection().getCharacterSize() <= 8) {
45  BL_LOG_WARN << "Item name too long to fit: "
46  << label.getSection().getString().toAnsiString();
47  break;
48  }
49  }
50 }
51 
52 const core::player::Bag::Item& BagItemButton::getItem() const { return item; }
53 
54 void BagItemButton::doCreate(bl::engine::Engine& engine) {
55  txtr = engine.renderer().texturePool().getOrLoadTexture(
56  bl::util::FileUtil::joinPath(core::Properties::MenuImagePath(), "Bag/button.png"));
57  background.create(engine, txtr);
58 
59  label.create(engine, core::Properties::MenuFont(), "", 22, bl::sfcol(sf::Color::Black));
60  label.setParent(background);
61 
62  qty.create(
63  engine, core::Properties::MenuFont(), "Qty:", 16, bl::sfcol(sf::Color(30, 140, 230)));
64  qty.addSection("", 18, sf::Color(10, 10, 60));
65  qty.setParent(background);
66 
67  update(item);
68 }
69 
70 void BagItemButton::doSceneAdd(bl::rc::Scene* overlay) {
71  background.addToScene(overlay, bl::rc::UpdateSpeed::Static);
72  label.addToScene(overlay, bl::rc::UpdateSpeed::Static);
73  qty.addToScene(overlay, bl::rc::UpdateSpeed::Static);
74 }
75 
76 void BagItemButton::doSceneRemove() { background.removeFromScene(); }
77 
78 bl::ecs::Entity BagItemButton::getEntity() const { return background.entity(); }
79 
80 glm::vec2 BagItemButton::getSize() const { return txtr->size(); }
81 
82 void BagItemButton::onSelect() { background.setColor(sf::Color(40, 230, 140)); }
83 
84 void BagItemButton::onDeselect() {
85  background.setColor(item.id != core::item::Id::None ? sf::Color::White :
86  sf::Color(240, 40, 40));
87 }
88 
89 void BagItemButton::draw(bl::rc::scene::CodeScene::RenderContext& ctx) {
90  background.draw(ctx);
91  label.draw(ctx);
92  qty.draw(ctx);
93 }
94 
95 } // namespace menu
96 } // namespace game
Parent namespace for all functionality unique to the game.
static const std::string & getName(item::Id item)
Returns the name of the given item.
Definition: Item.cpp:91
Simple struct representing a set of items in the bag.
Definition: Bag.hpp:24
item::Id id
The item in the bag.
Definition: Bag.hpp:26
unsigned int qty
How many of the item are in the bag.
Definition: Bag.hpp:29
static const sf::VulkanFont & MenuFont()
Definition: Properties.cpp:363
static const std::string & MenuImagePath()
Definition: Properties.cpp:303
Represents a button for a quantity of items in the bag.
void update(const core::player::Bag::Item &item)
Updates the text labels from the given item.
const core::player::Bag::Item & getItem() const
Returns the item this button is representing.
virtual void doCreate(bl::engine::Engine &engine) override
Called at least once when the item is added to a menu. Should create required graphics primitives and...
virtual void draw(bl::rc::scene::CodeScene::RenderContext &ctx) override
Renders the item.
std::shared_ptr< BagItemButton > Ptr
Pointer to the menu item.
static Ptr create(const core::player::Bag::Item &item)
Creates a new item button.
virtual glm::vec2 getSize() const override
Returns the size the button takes up.
virtual void doSceneRemove() override
Called when the item should be removed from the overlay.
virtual bl::ecs::Entity getEntity() const override
Returns the entity (or top level entity) of the item.
virtual void doSceneAdd(bl::rc::Scene *overlay) override
Called when the item should be added to the overlay.