Study Dungeon  1.0.0
A group project for COSC345
menu.h
Go to the documentation of this file.
1 
11 #pragma once
12 #ifndef MENU_H
13 #define MENU_H
14 
15 #include "util.h"
16 #include <algorithm>
17 #include <chrono>
18 #include <conio.h>
19 #include <functional>
20 #include <iostream>
21 #include <memory>
22 #include <string>
23 #include <unordered_map>
24 #include <vector>
25 #include <windows.h>
26 
27 namespace ConsoleUI
28 {
29 
30 class UIManager;
31 
32 
39 void setConsoleCursorPosition(int x, int y);
40 
41 
47 COORD getConsoleWindowSize();
48 
53 class AsciiArt
54 {
55 private:
57  std::string m_name;
59  std::vector<std::string> m_art;
61  size_t m_width;
63  size_t m_height;
65  int m_x;
67  int m_y;
68 
69 public:
78  AsciiArt(const std::string &name, const std::vector<std::string> &artLines, int x = 0, int y = 0);
79 
85  const std::string &getName() const;
86 
92  const std::vector<std::string> &getArt() const;
93 
99  int getX() const;
100 
106  int getY() const;
107 
113  size_t getWidth() const;
114 
120  size_t getHeight() const;
121 
128  void setPosition(int x, int y);
129 };
130 
135 class ANSIArt
136 {
137 private:
139  std::string name;
141  std::vector<std::vector<int>> codes;
143  size_t width;
145  size_t height;
147  int m_x;
149  int m_y;
150 
151 public:
160  ANSIArt(std::vector<std::vector<int>> codes, const std::string &name, int x = 0, int y = 0);
161 
167  size_t getWidth();
168 
174  size_t getHeight();
175 
181  int getX();
182 
188  int getY();
189 
196  void setPosition(int x, int y);
197 
203  std::string getName();
204 
210  std::string toString();
211 
217  std::vector<std::vector<int>> getCodes();
218 };
219 
220 
226 {
227 public:
232  ConsoleWindow();
233 
238  virtual ~ConsoleWindow() = default;
239 
244  void updateSize();
245 
250  void drawBorder();
251 
260  void drawBox(int x, int y, size_t width, size_t height);
261 
270  void drawHorizontalLine(int x, int y, size_t length, char ch = '-');
271 
280  void drawVerticalLine(int x, int y, size_t length, char ch = '|');
281 
289  void drawCharacter(int x, int y, char ch);
290 
298  void drawText(const std::string &text, int x, int y);
299 
306  void drawCenteredText(const std::string &text, int y);
307 
316  std::string getLine(int x, int y, size_t maxLength = 0);
317 
322  void clear();
323 
329  COORD getSize() const;
330 
336  void checkWindowResize(UIManager &uiManager);
337 
344  void setDefaultSize(short width, short height);
345 
352  void setConsoleWindowSize(short width, short height);
353 
359  void addTextToBox(const std::string &text);
360 
369  void drawTextBox(int x, int y, size_t width, size_t height);
370 
376  void addAsciiArt(const AsciiArt &art);
377 
385  void drawAsciiArt(const std::string &name, int x = -1, int y = -1);
386 
393  AsciiArt *getAsciiArtByName(const std::string &name);
394 
400  void addANSIArt(const ANSIArt &art);
401 
409  void drawANSIArt(const std::string &name, int x, int y);
410 
417  ANSIArt *getANSIArtByName(const std::string &name);
418 
426  void drawANSICode(int code, int x, int y);
427 
437  void drawWrappedText(const std::string &text, int x, int y, size_t width);
438 
439 
440 private:
442  size_t m_width;
444  size_t m_height;
446  std::vector<std::string> m_textBox;
448  int m_textBoxCapacity;
450  std::vector<AsciiArt> m_asciiArts;
452  std::vector<ANSIArt> m_ANSIArt;
454  COORD m_defaultSize;
460  void displayResizeWarning(UIManager &uiManager);
461 };
462 
467 class Button
468 {
469 public:
476  Button(const std::string &label, std::function<void()> action);
477 
485  void draw(int x, int y, bool selected);
486 
491  void performAction() const;
492 
498  size_t getWidth() const;
499 
505  const std::string &getLabel() const;
506 
507 private:
509  std::string m_label;
511  std::function<void()> m_action;
512 };
513 
518 class Menu
519 {
520 public:
526  Menu(bool horizontal = false);
527 
534  void addButton(const std::string &label, std::function<void()> action);
535 
542  void draw(int x, int y);
543 
548  void handleInput();
549 
556  bool isBackButtonPressed() const;
557 
558 
564  size_t getButtonCount() const;
565 
572  size_t getButtonWidth(size_t index) const;
573 
579  size_t getSelectedIndex() const
580  {
581  return m_selectedIndex;
582  }
583 
588  void selectPreviousButton();
589 
594  void selectNextButton();
595 
601  size_t getMaxWidth() const;
602 
607  void activateSelectedButton();
608 
613  void clear();
614 
615 private:
617  std::vector<Button> m_buttons;
619  size_t m_selectedIndex;
621  bool horizontal_layout;
622 };
623 
628 class Scene
629 {
630 public:
635  virtual ~Scene() = default;
636 
641  virtual void update() = 0;
642 
647  virtual void init() = 0;
648 
654  virtual void render(std::shared_ptr<ConsoleWindow> window) = 0;
655 
660  virtual void handleInput() = 0;
661 
666  virtual void setStaticDrawn(bool staticDrawn) = 0;
667 };
668 
669 
675 {
676 public:
681  UIManager();
682 
688  std::shared_ptr<ConsoleWindow> getWindow();
689 
695  void setCurrentScene(std::shared_ptr<Scene> scene);
696 
701  void init();
702 
707  void update();
708 
713  void render();
714 
719  void handleInput();
720 
728  Menu &createMenu(const std::string &name, bool horizontal = false);
729 
736  Menu &getMenu(const std::string &name);
737 
743  void clearMenu(const std::string &name);
744 
749  void clearAllMenus();
750 
760  AsciiArt createAsciiArt(const std::string &name, const std::vector<std::string> &artLines, int x = -1, int y = -1);
761 
766  void checkWindowResize();
767 
769  std::shared_ptr<ConsoleWindow> m_window;
771  std::shared_ptr<Scene> m_currentScene;
773  std::unordered_map<std::string, Menu> m_menus;
774 
780  std::vector<std::shared_ptr<Scene>> &getScenes()
781  {
782  return m_scenes;
783  }
784 
785 private:
787  std::vector<std::shared_ptr<Scene>> m_scenes;
788 };
789 
790 
791 } // namespace ConsoleUI
792 
793 
794 #endif // MENU_H
A object that holds art containing 256bit colour ANSI codes.
Definition: menu.h:136
ANSIArt(std::vector< std::vector< int >> codes, const std::string &name, int x=0, int y=0)
Construct a new ANSIArt object.
Definition: menu.cpp:442
int getX()
Get the left most position.
Definition: menu.cpp:459
std::string toString()
Convert the 256bit colour codes into a string of ANSI codes.
Definition: menu.cpp:480
size_t getWidth()
Get the width of the art.
Definition: menu.cpp:449
std::vector< std::vector< int > > getCodes()
Get the 256bit colour codes.
Definition: menu.cpp:494
size_t getHeight()
Get the Height of the art.
Definition: menu.cpp:454
std::string getName()
Get the Name of the artwork.
Definition: menu.cpp:469
int getY()
Get the top most position.
Definition: menu.cpp:464
void setPosition(int x, int y)
Set the position of the top left corner of the artwork.
Definition: menu.cpp:474
An object that stores ascii art in the form of a string.
Definition: menu.h:54
int getY() const
Get the top most position.
Definition: menu.cpp:427
void setPosition(int x, int y)
Set the position of the top left corner.
Definition: menu.cpp:384
AsciiArt(const std::string &name, const std::vector< std::string > &artLines, int x=0, int y=0)
Construct a new Ascii Art object.
Definition: menu.cpp:403
size_t getWidth() const
Get the Width of the art.
Definition: menu.cpp:432
size_t getHeight() const
Get the Height of the art.
Definition: menu.cpp:437
const std::vector< std::string > & getArt() const
Get the lines comprising the art.
Definition: menu.cpp:417
int getX() const
Get the left most position.
Definition: menu.cpp:422
const std::string & getName() const
Get the Name of the art.
Definition: menu.cpp:412
The class for the button object.
Definition: menu.h:468
const std::string & getLabel() const
Get the Label of the button.
Definition: menu.cpp:529
void draw(int x, int y, bool selected)
Draw the button.
Definition: menu.cpp:504
size_t getWidth() const
Get the Width of the button.
Definition: menu.cpp:524
void performAction() const
Perform the action associated with the button.
Definition: menu.cpp:518
Button(const std::string &label, std::function< void()> action)
Construct a new Button object.
Definition: menu.cpp:500
Object repesenting the Console window.
Definition: menu.h:226
void drawANSIArt(const std::string &name, int x, int y)
Draws the ansi art object at a location.
Definition: menu.cpp:291
void drawCharacter(int x, int y, char ch)
Puts a character on the screen at a position.
Definition: menu.cpp:78
COORD getSize() const
Get the Size of the console window.
Definition: menu.cpp:184
void drawHorizontalLine(int x, int y, size_t length, char ch='-')
Draws a horizontal line using specified character.
Definition: menu.cpp:63
void drawTextBox(int x, int y, size_t width, size_t height)
Draws a text box of specified dimensions.
Definition: menu.cpp:198
void drawBox(int x, int y, size_t width, size_t height)
Draws a box.
Definition: menu.cpp:55
void updateSize()
Update the size of the console window.
Definition: menu.cpp:43
void setConsoleWindowSize(short width, short height)
Set the size of the console window.
Definition: menu.cpp:358
void drawAsciiArt(const std::string &name, int x=-1, int y=-1)
Definition: menu.cpp:242
ANSIArt * getANSIArtByName(const std::string &name)
Get an ANSI artwork by name.
Definition: menu.cpp:279
void addAsciiArt(const AsciiArt &art)
Definition: menu.cpp:237
void drawVerticalLine(int x, int y, size_t length, char ch='|')
Draws a vertical line using specified character.
Definition: menu.cpp:69
void drawCenteredText(const std::string &text, int y)
Put a string of text centered horizontally on the screen.
Definition: menu.cpp:110
ConsoleWindow()
Construct a new Console Window object.
Definition: menu.cpp:34
void setDefaultSize(short width, short height)
Set the default size of the console window.
Definition: menu.cpp:377
AsciiArt * getAsciiArtByName(const std::string &name)
Get and ASCII art object by name.
Definition: menu.cpp:390
void drawText(const std::string &text, int x, int y)
Puts a string of text on the srceen starting at a position.
Definition: menu.cpp:84
void addANSIArt(const ANSIArt &art)
Add an ansi art object to the consolewindow object.
Definition: menu.cpp:274
void clear()
Clear everything on the screen.
Definition: menu.cpp:179
virtual ~ConsoleWindow()=default
Destroy the Console Window object.
void checkWindowResize(UIManager &uiManager)
Check to see if the console window has been resized.
Definition: menu.cpp:314
void drawWrappedText(const std::string &text, int x, int y, size_t width)
Draws text on the screen wrapping the string to the next line if it reaches the width.
Definition: menu.cpp:208
std::string getLine(int x, int y, size_t maxLength=0)
Create a blank text input field to recieve and display user input.
Definition: menu.cpp:117
void drawBorder()
Draws a border around the console.
Definition: menu.cpp:50
void addTextToBox(const std::string &text)
The text to be added to a text box.
Definition: menu.cpp:189
void drawANSICode(int code, int x, int y)
Draws a 256bit colour on the screen at a position.
Definition: menu.cpp:91
Defines the menu that is made of buttons.
Definition: menu.h:519
size_t getButtonWidth(size_t index) const
Get the width of the button by index.
Definition: menu.cpp:610
void activateSelectedButton()
Perform the associated function from the button.
Definition: menu.cpp:635
size_t getSelectedIndex() const
Get the index of the selected menu button.
Definition: menu.h:579
void handleInput()
Handle the user input on the menu.
Definition: menu.cpp:562
bool isBackButtonPressed() const
determines if the menu back button has been pressed
Definition: menu.cpp:599
void selectNextButton()
selecte the next button in the menu
Definition: menu.cpp:627
size_t getButtonCount() const
Get the number of buttons in the menu.
Definition: menu.cpp:605
Menu(bool horizontal=false)
Construct a new Menu object.
Definition: menu.cpp:535
size_t getMaxWidth() const
Get the width of the widest button in the menu.
Definition: menu.cpp:643
void clear()
removes all buttons in the menu
Definition: menu.cpp:653
void selectPreviousButton()
select the previous button in the menu
Definition: menu.cpp:619
void addButton(const std::string &label, std::function< void()> action)
Add a button to the menu.
Definition: menu.cpp:539
void draw(int x, int y)
Draw the menu.
Definition: menu.cpp:544
Defines a UI scene.
Definition: menu.h:629
virtual void init()=0
Initialise the scene.
virtual ~Scene()=default
Destroy the Scene object.
virtual void setStaticDrawn(bool staticDrawn)=0
Sets the static drawn state of the scene.
virtual void update()=0
What to perfrom on scene update.
virtual void handleInput()=0
Handle user input from the scene.
virtual void render(std::shared_ptr< ConsoleWindow > window)=0
What to render in the console window.
Defines the UI manager.
Definition: menu.h:675
std::shared_ptr< ConsoleWindow > getWindow()
Get the Window object.
Definition: menu.cpp:664
void update()
Called each time there is a event.
Definition: menu.cpp:687
UIManager()
Construct a new UIManager object.
Definition: menu.cpp:660
Menu & createMenu(const std::string &name, bool horizontal=false)
Create the UI menu.
Definition: menu.cpp:719
AsciiArt createAsciiArt(const std::string &name, const std::vector< std::string > &artLines, int x=-1, int y=-1)
Create a Ascii Art object.
Definition: menu.cpp:749
void handleInput()
Handles user input.
Definition: menu.cpp:703
std::shared_ptr< Scene > m_currentScene
Definition: menu.h:771
void clearAllMenus()
Removes all menus from the UI.
Definition: menu.cpp:737
void render()
Renders objects on the console window.
Definition: menu.cpp:695
std::unordered_map< std::string, Menu > m_menus
Definition: menu.h:773
void setCurrentScene(std::shared_ptr< Scene > scene)
Set the Current Scene object.
Definition: menu.cpp:669
Menu & getMenu(const std::string &name)
Get the Menu object by it's name.
Definition: menu.cpp:724
void checkWindowResize()
Deteremine if the window has been resized.
Definition: menu.cpp:711
void init()
Initialise the UI.
Definition: menu.cpp:683
void clearMenu(const std::string &name)
Removes the named menu from the UI.
Definition: menu.cpp:729
std::vector< std::shared_ptr< Scene > > & getScenes()
Get the Scenes of the UI.
Definition: menu.h:780
std::shared_ptr< ConsoleWindow > m_window
Definition: menu.h:769
void setConsoleCursorPosition(int x, int y)
Set the Console Cursor Position.
Definition: menu.cpp:17
COORD getConsoleWindowSize()
Get the size of the console window.
Definition: menu.cpp:25
Contains useful helper functions.