Peoplemon  0.1.0
Peoplemon 3 game source documentation
QtyEntry.cpp
Go to the documentation of this file.
2 
3 #include <Core/Properties.hpp>
4 
5 namespace core
6 {
7 namespace system
8 {
9 namespace hud
10 {
11 namespace
12 {
13 constexpr float ArrowHeight = 8.f;
14 }
15 
16 QtyEntry::QtyEntry(bl::engine::Engine& engine)
17 : engine(engine)
18 , currentOverlay(nullptr)
19 , qty(0)
20 , minQty(0)
21 , maxQty(0) {}
22 
23 void QtyEntry::ensureCreated() {
24  if (background.entity() == bl::ecs::InvalidEntity) {
25  background.create(engine, {10.f, 10.f});
26  background.setFillColor(sf::Color::White);
27  background.setOutlineColor(sf::Color::Black);
28  background.setOutlineThickness(2.f);
29  background.getTransform().setDepth(bl::cam::OverlayCamera::MinDepth);
30 
31  text.create(engine, Properties::MenuFont(), "0", 20, sf::Color::Black);
32  text.setParent(background);
33 
34  upArrow.create(engine, {6.f, 0.f}, {12.f, ArrowHeight}, {0.f, ArrowHeight});
35  upArrow.setFillColor(sf::Color::Black);
36  upArrow.setParent(background);
37 
38  downArrow.create(engine, {0.f, 0.f}, {12.f, 0.f}, {6.f, ArrowHeight});
39  downArrow.setFillColor(sf::Color::Black);
40  downArrow.setParent(background);
41  }
42 
43  if (!currentOverlay ||
44  engine.renderer().getObserver().getOrCreateSceneOverlay() != currentOverlay) {
45  currentOverlay = engine.renderer().getObserver().getOrCreateSceneOverlay();
46  background.addToScene(currentOverlay, bl::rc::UpdateSpeed::Static);
47  text.addToScene(currentOverlay, bl::rc::UpdateSpeed::Static);
48  upArrow.addToScene(currentOverlay, bl::rc::UpdateSpeed::Static);
49  downArrow.addToScene(currentOverlay, bl::rc::UpdateSpeed::Static);
50  }
51 }
52 
54  if (background.entity() != bl::ecs::InvalidEntity) {
55  background.removeFromScene();
56  currentOverlay = nullptr;
57  }
58 }
59 
60 void QtyEntry::setPosition(const sf::Vector2f& pos) {
61  position = pos;
62  if (background.entity() != bl::ecs::InvalidEntity) {
63  background.getTransform().setPosition(position.x, position.y);
64  }
65 }
66 
67 sf::Vector2f QtyEntry::getSize() const { return {background.getSize().x, background.getSize().y}; }
68 
69 void QtyEntry::configure(int mn, int mx, int q) {
70  ensureCreated();
71 
72  // determine max text width
73  text.getSection().setString(std::to_string(mn));
74  float w = text.getLocalBounds().width;
75  text.getSection().setString(std::to_string(mx));
76  w = std::max(w, text.getLocalBounds().width);
77 
78  // update positions
79  background.getTransform().setPosition(position.x, position.y);
80  background.setSize(
81  {w + 12.f,
82  ArrowHeight * 2.f + text.getLocalBounds().height + 6.f + text.getLocalBounds().top + 6.f});
83  upArrow.getTransform().setPosition(w * 0.5f, 4.f);
84  downArrow.getTransform().setPosition(w * 0.5f, background.getSize().y - ArrowHeight - 2.f);
85  text.getTransform().setPosition(0.f, ArrowHeight + 5.f);
86 
87  // update data
88  qty = q;
89  minQty = mn;
90  maxQty = mx;
91  updateText();
92 }
93 
94 void QtyEntry::updateText() {
95  text.getSection().setString(std::to_string(qty));
96  text.getTransform().setPosition(background.getSize().x * 0.5f -
97  text.getLocalBounds().width * 0.5f,
98  text.getTransform().getLocalPosition().y);
99  upArrow.setHidden(qty >= maxQty);
100  downArrow.setHidden(qty <= minQty);
101 }
102 
103 int QtyEntry::curQty() const { return qty; }
104 
105 void QtyEntry::up(int q, bool ignore) {
106  if (rateLimit(ignore)) return;
107  qty = std::min(maxQty, qty + q);
108  bl::audio::AudioSystem::playOrRestartSound(Properties::MenuMoveSound());
109  updateText();
110 }
111 
112 void QtyEntry::down(int q, bool ignore) {
113  if (rateLimit(ignore)) return;
114  qty = std::max(minQty, qty - q);
115  bl::audio::AudioSystem::playOrRestartSound(Properties::MenuMoveSound());
116  updateText();
117 }
118 
119 bool QtyEntry::rateLimit(bool ignore) {
120  if (debounce.getElapsedTime().asSeconds() >= 0.4f || ignore) {
121  debounce.restart();
122  return false;
123  }
124  return true;
125 }
126 
127 } // namespace hud
128 } // namespace system
129 } // namespace core
Core classes and functionality for both the editor and game.
static const sf::VulkanFont & MenuFont()
Definition: Properties.cpp:363
static bl::audio::AudioSystem::Handle MenuMoveSound()
Definition: Properties.cpp:702
void configure(int minQty, int maxQty, int qty)
Configures the entry with the given parameters and adds to the overlay.
Definition: QtyEntry.cpp:69
void setPosition(const sf::Vector2f &position)
Sets the position of the selector.
Definition: QtyEntry.cpp:60
void up(int q, bool ignoreDebounce)
Increments the qty up.
Definition: QtyEntry.cpp:105
void down(int q, bool ignoreDebounce)
Decrements the qty down.
Definition: QtyEntry.cpp:112
sf::Vector2f getSize() const
Returns the size of the entry in pixels.
Definition: QtyEntry.cpp:67
void hide()
Removes the quantity entry from the overlay.
Definition: QtyEntry.cpp:53
int curQty() const
Returns the currently selected qty.
Definition: QtyEntry.cpp:103
QtyEntry(bl::engine::Engine &engine)
Construct a new quantity entry helper.
Definition: QtyEntry.cpp:16