| 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 | 
|---|
| 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 |  | 
|---|
| 47 | Sizef m_text_size; | 
|---|
| 48 |  | 
|---|
| 49 | public: | 
|---|
| 50 | Dialog(bool passive = false); | 
|---|
| 51 | virtual ~Dialog(); | 
|---|
| 52 |  | 
|---|
| 53 | void set_text(const std::string& text); | 
|---|
| 54 |  | 
|---|
| 55 | void add_button(const std::string& text, const std::function<void ()>& callback = {}); | 
|---|
| 56 |  | 
|---|
| 57 | /** The default gets focused when the dialog is first shown */ | 
|---|
| 58 | void add_default_button(const std::string& text, const std::function<void ()>& callback = {}); | 
|---|
| 59 |  | 
|---|
| 60 | /** The cancel button can not only be activated by selecting it, but | 
|---|
| 61 | via the MENU_BACK button */ | 
|---|
| 62 | void add_cancel_button(const std::string& text, const std::function<void ()>& callback = {}); | 
|---|
| 63 |  | 
|---|
| 64 | void clear_buttons(); | 
|---|
| 65 |  | 
|---|
| 66 | void event(const SDL_Event& event); | 
|---|
| 67 | void process_input(const Controller& controller); | 
|---|
| 68 | void draw(DrawingContext& context); | 
|---|
| 69 | virtual void update() {} | 
|---|
| 70 | bool is_passive() const | 
|---|
| 71 | { | 
|---|
| 72 | return m_passive; | 
|---|
| 73 | } | 
|---|
| 74 |  | 
|---|
| 75 | static void show_message(const std::string& text) | 
|---|
| 76 | { | 
|---|
| 77 | auto dialog = std::make_unique<Dialog>(); | 
|---|
| 78 | dialog->set_text(text); | 
|---|
| 79 | dialog->clear_buttons(); | 
|---|
| 80 | dialog->add_button(_( "OK"), [] {}); | 
|---|
| 81 | MenuManager::instance().set_dialog(std::move(dialog)); | 
|---|
| 82 | } | 
|---|
| 83 |  | 
|---|
| 84 | static void show_confirmation(const std::string& text, const std::function<void ()>& callback) | 
|---|
| 85 | { | 
|---|
| 86 | auto dialog = std::make_unique<Dialog>(); | 
|---|
| 87 | dialog->set_text(text); | 
|---|
| 88 | dialog->clear_buttons(); | 
|---|
| 89 | dialog->add_default_button(_( "Yes"), callback); | 
|---|
| 90 | dialog->add_cancel_button(_( "No")); | 
|---|
| 91 | MenuManager::instance().set_dialog(std::move(dialog)); | 
|---|
| 92 | } | 
|---|
| 93 |  | 
|---|
| 94 | private: | 
|---|
| 95 | void on_button_click(int button) const; | 
|---|
| 96 | int get_button_at(const Vector& pos) const; | 
|---|
| 97 |  | 
|---|
| 98 | private: | 
|---|
| 99 | Dialog(const Dialog&) = delete; | 
|---|
| 100 | Dialog& operator=(const Dialog&) = delete; | 
|---|
| 101 | }; | 
|---|
| 102 |  | 
|---|
| 103 | #endif | 
|---|
| 104 |  | 
|---|
| 105 | /* EOF */ | 
|---|
| 106 |  | 
|---|