supertux
dialog.hpp
1 // SuperTux
2 // Copyright (C) 2014 Ingo Ruhnke <grumbel@gmail.com>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef HEADER_SUPERTUX_GUI_DIALOG_HPP
18 #define HEADER_SUPERTUX_GUI_DIALOG_HPP
19 
20 #include <SDL.h>
21 #include <functional>
22 #include <string>
23 #include <vector>
24 
25 #include "gui/menu_manager.hpp"
26 #include "math/sizef.hpp"
27 #include "util/gettext.hpp"
28 
29 class Controller;
30 class DrawingContext;
31 
32 class Dialog
33 {
34 private:
35  struct Button
36  {
37  std::string text;
38  std::function<void ()> callback;
39  };
40 
41  std::string m_text;
42  std::vector<Button> m_buttons;
43  int m_selected_button;
44  int m_cancel_button;
45  bool m_passive;
46  bool m_clear_diags;
47 
48  Sizef m_text_size;
49  Sizef m_size;
50 
51 public:
52  Dialog(bool passive = false, bool auto_clear_dialogs = true);
53  virtual ~Dialog();
54 
55  void set_text(const std::string& text);
56 
57  void add_button(const std::string& text, const std::function<void ()>& callback = {});
58 
60  void add_default_button(const std::string& text, const std::function<void ()>& callback = {});
61 
64  void add_cancel_button(const std::string& text, const std::function<void ()>& callback = {});
65 
66  void clear_buttons();
67 
68  void event(const SDL_Event& event);
69  void process_input(const Controller& controller);
70  void draw(DrawingContext& context);
71  virtual void update() {}
72  bool is_passive() const
73  {
74  return m_passive;
75  }
76 
77  Vector get_center_pos() const;
78 
79  float get_width() const { return m_size.width; }
80  float get_height() const { return m_size.height; }
81 
82  static void show_message(const std::string& text, bool passive = false, bool no_auto_clear = false, const std::function<void ()>& ok_callback = {})
83  {
84  auto dialog = std::make_unique<Dialog>(passive, !no_auto_clear);
85  dialog->set_text(text);
86  dialog->clear_buttons();
87  dialog->add_button(_("OK"), ok_callback);
88  MenuManager::instance().set_dialog(std::move(dialog));
89  }
90 
91  static void show_confirmation(const std::string& text, const std::function<void ()>& callback, bool no_auto_clear = false)
92  {
93  auto dialog = std::make_unique<Dialog>(false, !no_auto_clear);
94  dialog->set_text(text);
95  dialog->clear_buttons();
96  dialog->add_default_button(_("Yes"), callback);
97  dialog->add_cancel_button(_("No"));
98  MenuManager::instance().set_dialog(std::move(dialog));
99  }
100 
101 private:
102  void on_button_click(int button) const;
103  int get_button_at(const Vector& pos) const;
104 
105 private:
106  Dialog(const Dialog&) = delete;
107  Dialog& operator=(const Dialog&) = delete;
108 };
109 
110 #endif
111 
112 /* EOF */
Definition: dialog.hpp:32
Definition: controller.hpp:57
void add_cancel_button(const std::string &text, const std::function< void()> &callback={})
The cancel button can not only be activated by selecting it, but via the MENU_BACK button...
Definition: dialog.cpp:76
This class provides functions for drawing things on screen.
Definition: drawing_context.hpp:42
void add_default_button(const std::string &text, const std::function< void()> &callback={})
The default gets focused when the dialog is first shown.
Definition: dialog.cpp:69
Definition: sizef.hpp:26