Tweak icecrusher draw layers to avoid objects (such as coins) from appearing inside...
[supertux.git] / src / gui / dialog.cpp
index cd56c40..e536c9c 100644 (file)
 
 #include "control/controller.hpp"
 #include "gui/menu_manager.hpp"
+#include "gui/menu.hpp"
 #include "gui/mousecursor.hpp"
 #include "supertux/resources.hpp"
+#include "supertux/colorscheme.hpp"
 #include "video/drawing_context.hpp"
 #include "video/renderer.hpp"
 #include "video/video_system.hpp"
@@ -28,6 +30,7 @@ Dialog::Dialog() :
   m_text(),
   m_buttons(),
   m_selected_button(),
+  m_cancel_button(-1),
   m_text_size()
 {
 }
@@ -47,14 +50,31 @@ Dialog::set_text(const std::string& text)
 }
 
 void
-Dialog::add_button(const std::string& text, const std::function<void ()>& callback, bool focus)
+Dialog::clear_buttons()
 {
-  m_buttons.push_back({text, callback});
+  m_buttons.clear();
+  m_selected_button = 0;
+  m_cancel_button = -1;
+}
 
-  if (focus)
-  {
-    m_selected_button = m_buttons.size() - 1;
-  }
+void
+Dialog::add_default_button(const std::string& text, const std::function<void ()>& callback)
+{
+  add_button(text, callback);
+  m_selected_button = m_buttons.size() - 1;
+}
+
+void
+Dialog::add_cancel_button(const std::string& text, const std::function<void ()>& callback)
+{
+  add_button(text, callback);
+  m_cancel_button = m_buttons.size() - 1;
+}
+
+void
+Dialog::add_button(const std::string& text, const std::function<void ()>& callback)
+{
+  m_buttons.push_back({text, callback});
 }
 
 int
@@ -95,9 +115,6 @@ Dialog::event(const SDL_Event& ev)
       {
         m_selected_button = new_button;
         on_button_click(m_selected_button);
-
-        // warning: this will "delete this"
-        MenuManager::instance().set_dialog({});
       }
     }
     break;
@@ -144,9 +161,13 @@ Dialog::process_input(const Controller& controller)
       controller.pressed(Controller::MENU_SELECT))
   {
     on_button_click(m_selected_button);
+  }
 
-    // warning: this will "delete this"
-    MenuManager::instance().set_dialog({});
+  if (m_cancel_button != -1 &&
+      (controller.pressed(Controller::ESCAPE) ||
+       controller.pressed(Controller::MENU_BACK)))
+  {
+    on_button_click(m_cancel_button);
   }
 }
 
@@ -209,7 +230,8 @@ Dialog::draw(DrawingContext& ctx)
 
     ctx.draw_text(Resources::normal_font, m_buttons[i].text,
                   Vector(pos.x, pos.y - int(Resources::normal_font->get_height()/2)),
-                  ALIGN_CENTER, LAYER_GUI);
+                  ALIGN_CENTER, LAYER_GUI,
+                  i == m_selected_button ? ColorScheme::Menu::active_color : ColorScheme::Menu::default_color);
   }
 }
 
@@ -220,6 +242,7 @@ Dialog::on_button_click(int button) const
   {
     m_buttons[button].callback();
   }
+  MenuManager::instance().set_dialog({});
 }
 
 /* EOF */