Implemented Dialog::draw()
authorIngo Ruhnke <grumbel@gmail.com>
Mon, 25 Aug 2014 03:43:14 +0000 (05:43 +0200)
committerIngo Ruhnke <grumbel@gmail.com>
Mon, 25 Aug 2014 07:54:13 +0000 (09:54 +0200)
src/gui/dialog.cpp
src/gui/dialog.hpp

index 8c4c0dd..5d7b117 100644 (file)
 #include "gui/dialog.hpp"
 
 #include "control/controller.hpp"
+#include "gui/menu_manager.hpp"
 #include "supertux/resources.hpp"
 #include "video/drawing_context.hpp"
 
 Dialog::Dialog() :
-  m_text("<no-text>"),
+  m_text(),
   m_buttons(),
   m_selected_button()
 {
 }
 
+Dialog::~Dialog()
+{
+}
+
+void
+Dialog::set_text(const std::string& text)
+{
+  m_text = text;
+}
+
 void
 Dialog::add_button(const std::string& text)
 {
@@ -46,22 +57,91 @@ Dialog::process_input(const Controller& controller)
   {
     m_selected_button += 1;
     m_selected_button = std::min(m_selected_button, static_cast<int>(m_buttons.size()) - 1);
-  }     
+  }
+
+  if (controller.pressed(Controller::ACTION) ||
+      controller.pressed(Controller::MENU_SELECT))
+  {
+    on_select(m_selected_button);
+
+    // warning: this will "delete this"
+    MenuManager::instance().set_dialog({});
+  }
 }
 
 void
 Dialog::draw(DrawingContext& ctx)
 {
+  Vector center(SCREEN_WIDTH/2, SCREEN_HEIGHT/2);
+
+  Sizef text_size(Resources::normal_font->get_text_width(m_text),
+                  Resources::normal_font->get_text_height(m_text));
+
+  Rectf text_rect(Vector(center.x - text_size.width/2,
+                         center.y - text_size.height/2),
+                  text_size);
+
+  Rectf bg_rect = text_rect;
+  bg_rect.p2.y += 44;
+
+  // draw background rect
+  ctx.draw_filled_rect(bg_rect.grown(12.0f),
+                       Color(0.2f, 0.3f, 0.4f, 0.8f),
+                       16.0f,
+                       LAYER_GUI-10);
+
+  ctx.draw_filled_rect(bg_rect.grown(8.0f),
+                       Color(0.6f, 0.7f, 0.8f, 0.5f),
+                       16.0f,
+                       LAYER_GUI-10);
+
+  // draw text
+  ctx.draw_text(Resources::normal_font, m_text,
+                Vector(bg_rect.p1.x + bg_rect.get_width()/2.0f,
+                       bg_rect.p1.y),
+                ALIGN_CENTER, LAYER_GUI);
+
+  // draw HL line
+  ctx.draw_filled_rect(Vector(bg_rect.p1.x, bg_rect.p2.y - 35),
+                           Vector(bg_rect.get_width(), 4),
+                           Color(0.6f, 0.7f, 1.0f, 1.0f), LAYER_GUI);
+  ctx.draw_filled_rect(Vector(bg_rect.p1.x, bg_rect.p2.y - 35),
+                           Vector(bg_rect.get_width(), 2),
+                           Color(1.0f, 1.0f, 1.0f, 1.0f), LAYER_GUI);
+
+  // draw buttons
   for(int i = 0; i < static_cast<int>(m_buttons.size()); ++i)
   {
+    float segment_width = bg_rect.get_width() / m_buttons.size();
+    float button_width = segment_width * 0.95;
+    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)
     {
-      // highlight
+      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),
+                           Color(1.0f, 1.0f, 1.0f, blink),
+                           14.0f,
+                           LAYER_GUI-10);
+      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)),
+                           Color(1.0f, 1.0f, 1.0f, 0.5f),
+                           12.0f,
+                           LAYER_GUI-10);
     }
+
     ctx.draw_text(Resources::normal_font, m_buttons[i],
-                  Vector(100, 100),
+                  Vector(pos.x, pos.y - int(Resources::normal_font->get_height()/2)),
                   ALIGN_CENTER, LAYER_GUI);
   }
 }
 
+void
+Dialog::on_select(int id)
+{
+}
+
 /* EOF */
index 272cb4b..001a300 100644 (file)
@@ -32,12 +32,16 @@ private:
 
 public:
   Dialog();
+  virtual ~Dialog();
 
+  void set_text(const std::string& text);
   void add_button(const std::string& text);
 
   void process_input(const Controller& controller);
   void draw(DrawingContext& context);
 
+  virtual void on_select(int id);
+
 private:
   Dialog(const Dialog&) = delete;
   Dialog& operator=(const Dialog&) = delete;