Debian Codegraph @

source/code/MenuSystem.h

raw symbols SCIP
1 /*
2 ===========================================================================
3 blockattack - Block Attack - Rise of the Blocks
4 Copyright (C) 2005-2012 Poul Sander
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see http://www.gnu.org/licenses/
18
19 Source information and contacts persons can be found at
20 http://blockattack.net
21 ===========================================================================
22 */
23
24
25 #ifndef _MENUSYSTEM_H
26 #define _MENUSYSTEM_H
27
28 #include <string>
29 #include "SDL.h"
30 #include <map>
31 #include <vector>
32 #include "sago/SagoSprite.hpp"
33 #include "sago/GameStateInterface.hpp"
34 #include <memory>
35 #include "sago/SagoTextField.hpp"
36
37 //The ButtonGfx object hold common media for all buttons, so we can reskin them by only changeing one pointer
38 struct ButtonGfx
39 {
40 //The size of the buttons, so we don't have to ask w and h from the SDL Surfaces each time
41 int xsize = 0;
42 int ysize = 0;
43 sago::SagoTextField* getLabel(const std::string& text, bool marked) const;
44 void setSurfaces();
45 std::string menu_marked = "menu_marked";
46 std::string menu_unmarked = "menu_unmarked";
47 private:
48 mutable std::map<std::string, std::shared_ptr<sago::SagoTextField> > labels;
49 mutable std::map<std::string, std::shared_ptr<sago::SagoTextField> > labels_marked;
50 };
51
52 //A button
53 class Button
54 {
55 private:
56 //Pointer to a callback function.
57 void (*action)(void) = nullptr;
58
59 //If true the menu should also be closed then the button is clicked
60 bool popOnRun = false;
61
62 //The label. This is written on the button
63 std::string label;
64 public:
65 //Is the button marked?
66 bool marked = false;
67 //Where is the button on the screen
68 int x = 0;
69 int y = 0;
70 ButtonGfx standardButton;
71
72 Button() = default;
73 Button(const Button& b);
74 Button& operator=(const Button& other);
75 virtual ~Button() = default;
76
77
78 //Set the text to write on the button
79 void setLabel(const std::string& text);
80 //Set the action to run
81 void setAction(void (*action2run)(void));
82
83 virtual void doAction(); //Run the callback function
84 void setPopOnRun(bool popOnRun);
85 bool isPopOnRun() const;
86 virtual const std::string& getLabel() const {return this->label; };
87
88 //May hold any other information the callback might need
89 int iGeneric1 = 0;
90 };
91
92
93 class Menu : public sago::GameStateInterface
94 {
95 private:
96 bool isSubmenu = false; //True if the menu is a submenu
97 int marked = 0; //The index of the marked button (for keyboard up/down)
98 bool running = true; //The menu is running. The menu will terminate then this is false
99 SDL_Renderer *screen = nullptr; //Pointer to the screen to draw to
100 std::string title;
101 bool bMouseUp = false;
102 public:
103 //numberOfItems is the expected numberOfItems for vector initialization
104 //SubMenu is true by default
105 Menu(SDL_Renderer *screen,bool isSubmenu);
106 explicit Menu(SDL_Renderer *screen);
107 Menu(SDL_Renderer *screen, const std::string& title, bool isSubmenu);
108 virtual ~Menu() {}
109
110 //Add a button to the menu
111 void addButton(Button *b);
112
113 bool IsActive() override;
114 void Draw(SDL_Renderer* target) override;
115 void ProcessInput(const SDL_Event& event, bool &processed) override;
116 void Update() override;
117 void drawSelf(SDL_Renderer* target); //Private function to draw the screen
118 virtual void placeButtons(); //Rearanges the buttons to the correct place.
119
120 std::vector<Button*> buttons; //Vector holder the buttons
121 Button exit; //The exit button is special since it does not have a callback function
122 };
123
124 class FileMenu
125 {
126 private:
127 std::string pm_path;
128 std::string pm_fileending;
129 bool pm_hidden_files = false;
130 public:
131 FileMenu(const std::string& path, const std::string& fileending, bool hidden_files = false);
132
133 std::string getFile(SDL_Surface **screen);
134 };
135
136 bool isUpEvent(const SDL_Event& event);
137
138 bool isDownEvent(const SDL_Event& event);
139
140 bool isLeftEvent(const SDL_Event& event);
141
142 bool isRightEvent(const SDL_Event& event);
143
144 bool isEscapeEvent(const SDL_Event& event);
145
146 bool isConfirmEvent(const SDL_Event& event);
147
148 #endif /* _MENUSYSTEM_H */
149