Peoplemon  0.1.0
Peoplemon 3 game source documentation
HUD.hpp
Go to the documentation of this file.
1 #ifndef CORE_SYSTEMS_HUD_HPP
2 #define CORE_SYSTEMS_HUD_HPP
3 
4 #include <BLIB/Engine/System.hpp>
5 #include <BLIB/Graphics.hpp>
6 #include <BLIB/Interfaces/Menu.hpp>
7 #include <BLIB/Interfaces/Utilities.hpp>
8 #include <BLIB/Resources.hpp>
12 #include <functional>
13 #include <queue>
14 #include <string>
15 #include <variant>
16 #include <vector>
17 
18 namespace core
19 {
20 namespace system
21 {
22 class Systems;
23 
31 class HUD : public bl::engine::System {
32 public:
39  using Callback = std::function<void(const std::string& value)>;
40 
47  using QtyCallback = std::function<void(int qty)>;
48 
54  HUD(Systems& owner);
55 
59  virtual ~HUD() = default;
60 
67  void displayMessage(
68  const std::string& message, const Callback& cb = [](const std::string&) {});
69 
79  const std::string& message, bool ghostWrite,
80  const Callback& cb = [](const std::string&) {});
81 
88  bool dismissStickyMessage(bool ignoreGhostWrite = true);
89 
97  void promptUser(const std::string& prompt, const std::vector<std::string>& choices,
98  const Callback& cb);
99 
108  void getInputString(const std::string& prompt, unsigned int minLen, unsigned int maxLen,
109  const Callback& cb);
110 
119  void getQty(const std::string& prompt, int minQty, int maxQty, const QtyCallback& cb);
120 
126  void displayEntryCard(const std::string& name);
127 
131  void hideEntryCard();
132 
133 private:
134  enum State {
135  Hidden,
136  Printing,
137  WaitingContinue,
138  WaitingSticky,
139  WaitingPrompt,
140  WaitingKeyboard,
141  WaitingQty
142  };
143 
144  class Item {
145  public:
146  enum Type { Message, Prompt, Keyboard, Qty };
147 
148  Item(const std::string& message, bool sticky, bool ghost, const Callback& cb);
149  Item(const std::string& prompt, const std::vector<std::string>& choices,
150  const Callback& cb);
151  Item(const std::string& prompt, unsigned int minLen, unsigned int maxLen,
152  const Callback& cb);
153  Item(const std::string& prompt, int minQty, int maxQty, const QtyCallback& cb);
154 
155  Type getType() const;
156  const std::string& getMessage() const;
157  bool isSticky() const;
158  bool ghostWrite() const;
159  const std::vector<std::string>& getChoices() const;
160  const Callback& getCallback() const;
161  const QtyCallback& getQtyCallback() const;
162  unsigned int minInputLength() const;
163  unsigned int maxInputLength() const;
164  int getMinQty() const;
165  int getMaxQty() const;
166 
167  private:
168  const Type type;
169  const std::variant<Callback, QtyCallback> cb;
170  const std::string message;
171  const std::variant<std::pair<bool, bool>, std::vector<std::string>,
172  std::pair<unsigned int, unsigned int>, std::pair<int, int>>
173  data;
174  };
175 
176  struct HudListener : public bl::input::Listener {
177  HudListener(HUD& owner);
178  virtual ~HudListener() = default;
179  virtual bool observe(const bl::input::Actor&, unsigned int activatedControl,
180  bl::input::DispatchType, bool eventTriggered) override;
181 
182  HUD& owner;
183  };
184 
185  class EntryCard {
186  public:
187  EntryCard(bl::engine::Engine& engine);
188  void display(const std::string& text);
189  void update(float dt);
190  void hide();
191 
192  private:
193  bl::engine::Engine& engine;
194  bl::rc::Overlay* currentOverlay;
195  bl::rc::res::TextureRef txtr;
196  bl::gfx::Sprite card;
197  bl::gfx::Text text;
198 
199  void ensureCreated();
200 
201  enum State { Hidden, Dropping, Holding, Rising } state;
202  float stateVar;
203  };
204 
205  Systems& owner;
206  State state;
207 
208  HudListener inputListener;
209  std::queue<Item> queuedOutput;
210  bl::interface::GhostWriter currentMessage;
211  hud::ScreenKeyboard screenKeyboard;
212  EntryCard entryCard;
213 
214  bl::rc::Overlay* currentOverlay;
215  bl::rc::res::TextureRef textboxTxtr;
216  bl::gfx::Sprite textbox;
217  bl::gfx::Text displayText;
218  bl::gfx::Triangle promptTriangle;
219  hud::QtyEntry qtyEntry;
220 
221  bl::menu::Menu choiceMenu;
222  core::input::MenuDriver choiceDriver;
223  float choiceBoxX;
224 
225  void setState(State newState);
226  void ensureActive();
227  void startPrinting();
228  void printDoneStateTransition();
229  void choiceMade(unsigned int i);
230  void keyboardSubmit(const std::string& input);
231  void qtySelected(int qty);
232  void next();
233 
234  virtual void init(bl::engine::Engine&) override;
235  virtual void update(std::mutex&, float dt, float, float, float) override;
236 };
237 
238 } // namespace system
239 } // namespace core
240 
241 #endif
bl::menu::TriggerDriver< Control::MoveUp, Control::MoveRight, Control::MoveDown, Control::MoveLeft, Control::Interact > MenuDriver
Helper typedef for Peoplemon specific menu driving.
Definition: MenuDriver.hpp:18
Type
The type classification of an item. This is used to determine when an item may be used and how to use...
Definition: Type.hpp:17
Core classes and functionality for both the editor and game.
The primary HUD system for the player. Manages displaying messages and asking questions....
Definition: HUD.hpp:31
void displayEntryCard(const std::string &name)
Displays a card to indicate entering a new town, route, or map.
Definition: HUD.cpp:129
void hideEntryCard()
Hides the entry card.
Definition: HUD.cpp:131
std::function< void(const std::string &value)> Callback
Signature for HUD callbacks. Used for both messages completing and choices made.
Definition: HUD.hpp:39
void displayMessage(const std::string &message, const Callback &cb=[](const std::string &) {})
Displays a message in the HUD textbox. Messages are queued in order that they arrive.
Definition: HUD.cpp:92
void getQty(const std::string &prompt, int minQty, int maxQty, const QtyCallback &cb)
Gets a number from the player.
Definition: HUD.cpp:124
void promptUser(const std::string &prompt, const std::vector< std::string > &choices, const Callback &cb)
Asks the player a question through the HUD.
Definition: HUD.cpp:112
std::function< void(int qty)> QtyCallback
Called when a qty is entered.
Definition: HUD.hpp:47
virtual ~HUD()=default
Destroys the system.
void displayStickyMessage(const std::string &message, bool ghostWrite, const Callback &cb=[](const std::string &) {})
Displays a message in the HUD textbox. Sticky messages stay displayed until programmatically dismisse...
Definition: HUD.cpp:97
void getInputString(const std::string &prompt, unsigned int minLen, unsigned int maxLen, const Callback &cb)
Prompts the player to input a string using the screen keyboard.
Definition: HUD.cpp:118
HUD(Systems &owner)
Construct a new HUD system.
Definition: HUD.cpp:25
bool dismissStickyMessage(bool ignoreGhostWrite=true)
Dismisses the currently active sticky message.
Definition: HUD.cpp:102
Owns all primary systems and a reference to the engine.
Definition: Systems.hpp:47