Fix music not properly fading in again
[supertux.git] / src / gui / dialog.cpp
index c3b2000..47b9a0c 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"
 
 Dialog::Dialog() :
   m_text(),
   m_buttons(),
   m_selected_button(),
+  m_cancel_button(-1),
   m_text_size()
 {
 }
@@ -44,13 +50,95 @@ 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.clear();
+  m_selected_button = 0;
+  m_cancel_button = -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
+Dialog::get_button_at(const Vector& mouse_pos) const
+{
+  Rectf bg_rect(Vector(SCREEN_WIDTH/2 - m_text_size.width/2,
+                       SCREEN_HEIGHT/2 - m_text_size.height/2),
+                Sizef(m_text_size.width,
+                      m_text_size.height + 44));
 
-  if (focus)
+  for(int i = 0; i < static_cast<int>(m_buttons.size()); ++i)
   {
-    m_selected_button = m_buttons.size() - 1;
+    float segment_width = bg_rect.get_width() / m_buttons.size();
+    float button_width = segment_width;
+    float button_height = 24.0f;
+    Vector pos(bg_rect.p1.x + segment_width/2.0f + i * segment_width,
+               bg_rect.p2.y - 12);
+    Rectf button_rect(Vector(pos.x - button_width/2, pos.y - button_height/2),
+                      Vector(pos.x + button_width/2, pos.y + button_height/2));
+    if (button_rect.contains(mouse_pos))
+    {
+      return i;
+    }
+  }
+  return -1;
+}
+
+void
+Dialog::event(const SDL_Event& ev)
+{
+  switch(ev.type) {
+    case SDL_MOUSEBUTTONDOWN:
+    if(ev.button.button == SDL_BUTTON_LEFT)
+    {
+      Vector mouse_pos = VideoSystem::current()->get_renderer().to_logical(ev.motion.x, ev.motion.y);
+      int new_button = get_button_at(mouse_pos);
+      if (new_button != -1)
+      {
+        m_selected_button = new_button;
+        on_button_click(m_selected_button);
+      }
+    }
+    break;
+
+    case SDL_MOUSEMOTION:
+    {
+      Vector mouse_pos = VideoSystem::current()->get_renderer().to_logical(ev.motion.x, ev.motion.y);
+      int new_button = get_button_at(mouse_pos);
+      if (new_button != -1)
+      {
+        m_selected_button = new_button;
+        if(MouseCursor::current())
+          MouseCursor::current()->set_state(MC_LINK);
+      }
+      else
+      {
+        if(MouseCursor::current())
+          MouseCursor::current()->set_state(MC_NORMAL);
+      }
+    }
+    break;
+
+    default:
+      break;
   }
 }
 
@@ -73,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);
   }
 }
 
@@ -117,12 +209,12 @@ Dialog::draw(DrawingContext& ctx)
   {
     float segment_width = bg_rect.get_width() / m_buttons.size();
     float button_width = segment_width;
-    float button_height = 24.0f;
     Vector pos(bg_rect.p1.x + segment_width/2.0f + i * segment_width,
                bg_rect.p2.y - 12);
 
     if (i == m_selected_button)
     {
+      float button_height = 24.0f;
       float blink = (sinf(real_time * M_PI * 1.0f)/2.0f + 0.5f) * 0.5f + 0.25f;
       ctx.draw_filled_rect(Rectf(Vector(pos.x - button_width/2, pos.y - button_height/2),
                                  Vector(pos.x + button_width/2, pos.y + button_height/2)).grown(2.0f),
@@ -138,17 +230,19 @@ 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);
   }
 }
 
 void
-Dialog::on_button_click(int button)
+Dialog::on_button_click(int button) const
 {
   if (m_buttons[button].callback)
   {
     m_buttons[button].callback();
   }
+  MenuManager::instance().set_dialog({});
 }
 
 /* EOF */