Peoplemon  0.1.0
Peoplemon 3 game source documentation
StoreItemRow.cpp
Go to the documentation of this file.
2 
3 #include <Core/Items/Item.hpp>
4 #include <Core/Properties.hpp>
5 
6 namespace game
7 {
8 namespace menu
9 {
10 namespace
11 {
12 constexpr unsigned int FontSize = 16;
13 constexpr float Margin = 1.f;
14 constexpr float TopMargin = Margin * 0.5f;
15 constexpr float Width = 352.f;
16 } // namespace
17 
19  return Ptr{new StoreItemRow(qty, item, price)};
20 }
21 
23  return Ptr{new StoreItemRow(-1, item, price)};
24 }
25 
26 StoreItemRow::StoreItemRow(int qty, core::item::Id item, int price)
27 : item(item)
28 , qty(qty)
29 , price(price) {}
30 
31 void StoreItemRow::updateQty(int qty) {
32  qtyText.getSection().setString(std::to_string(std::min(qty, 999)));
33 }
34 
35 glm::vec2 StoreItemRow::getSize() const { return {Width, Height}; }
36 
37 core::item::Id StoreItemRow::getItem() const { return item; }
38 
39 void StoreItemRow::doCreate(bl::engine::Engine& engine) {
40  dummy.create(engine);
41  dummy.setSize({Width, Height});
42 
43  qtyText.create(engine, core::Properties::MenuFont(), "", FontSize, sf::Color(30, 75, 240));
44  qtyText.setParent(dummy);
45  qtyText.getTransform().setPosition({Margin, TopMargin});
46 
47  float LeftMargin = 30.f;
48  if (qty > 0) { updateQty(qty); }
49  else { LeftMargin = Margin; }
50 
51  nameText.create(engine,
54  FontSize,
55  sf::Color::Black);
56  nameText.getTransform().setPosition({Margin * 2.f + LeftMargin, TopMargin});
57  nameText.setParent(dummy);
58 
59  priceText.create(engine,
61  "$" + std::to_string(price),
62  FontSize,
63  sf::Color::Black);
64  priceText.getTransform().setPosition(
65  {Width - Margin - priceText.getLocalBounds().width - 6.f, TopMargin});
66  priceText.setParent(dummy);
67 }
68 
69 void StoreItemRow::doSceneAdd(bl::rc::Scene* overlay) {
70  nameText.addToScene(overlay, bl::rc::UpdateSpeed::Static);
71  priceText.addToScene(overlay, bl::rc::UpdateSpeed::Static);
72  qtyText.addToScene(overlay, bl::rc::UpdateSpeed::Static);
73 }
74 
76  nameText.removeFromScene();
77  priceText.removeFromScene();
78  qtyText.removeFromScene();
79 }
80 
81 bl::ecs::Entity StoreItemRow::getEntity() const { return dummy.entity(); }
82 
83 void StoreItemRow::draw(bl::rc::scene::CodeScene::RenderContext& ctx) {
84  nameText.draw(ctx);
85  priceText.draw(ctx);
86  qtyText.draw(ctx);
87 }
88 
89 } // namespace menu
90 } // namespace game
Id
Represents an item in its simplist form.
Definition: Id.hpp:24
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
static const sf::VulkanFont & MenuFont()
Definition: Properties.cpp:363
Menu item for an item in the store. Does both sellable and for-sale items.
virtual void draw(bl::rc::scene::CodeScene::RenderContext &ctx) override
Renders the item.
void updateQty(int qty)
Updates the text for how many items can be sold.
core::item::Id getItem() const
Returns the item that this row represents.
static Ptr create(int qty, core::item::Id item, int price)
Creates a new menu item in sell mode.
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...
std::shared_ptr< StoreItemRow > Ptr
Pointer to the menu item.
static constexpr float Height
Height of a single item in pixels.
virtual void doSceneRemove() override
Called when the item should be removed from the overlay.
virtual void doSceneAdd(bl::rc::Scene *overlay) override
Called when the item should be added to the overlay.
virtual glm::vec2 getSize() const override
Returns the size that the row takes up.
virtual bl::ecs::Entity getEntity() const override
Returns the entity (or top level entity) of the item.