Added some initial dialog code, doesn't do anything yet
authorIngo Ruhnke <grumbel@gmail.com>
Mon, 25 Aug 2014 01:31:58 +0000 (03:31 +0200)
committerIngo Ruhnke <grumbel@gmail.com>
Mon, 25 Aug 2014 07:54:13 +0000 (09:54 +0200)
src/gui/dialog.cpp [new file with mode: 0644]
src/gui/dialog.hpp [new file with mode: 0644]
src/gui/menu_manager.cpp
src/gui/menu_manager.hpp

diff --git a/src/gui/dialog.cpp b/src/gui/dialog.cpp
new file mode 100644 (file)
index 0000000..8c4c0dd
--- /dev/null
@@ -0,0 +1,67 @@
+//  SuperTux
+//  Copyright (C) 2014 Ingo Ruhnke <grumbel@gmail.com>
+//
+//  This program is free software: you can redistribute it and/or modify
+//  it under the terms of the GNU General Public License as published by
+//  the Free Software Foundation, either version 3 of the License, or
+//  (at your option) any later version.
+//
+//  This program is distributed in the hope that it will be useful,
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+//  GNU General Public License for more details.
+//
+//  You should have received a copy of the GNU General Public License
+//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+#include "gui/dialog.hpp"
+
+#include "control/controller.hpp"
+#include "supertux/resources.hpp"
+#include "video/drawing_context.hpp"
+
+Dialog::Dialog() :
+  m_text("<no-text>"),
+  m_buttons(),
+  m_selected_button()
+{
+}
+
+void
+Dialog::add_button(const std::string& text)
+{
+  m_buttons.push_back(text);
+}
+
+void
+Dialog::process_input(const Controller& controller)
+{
+  if (controller.pressed(Controller::LEFT))
+  {
+    m_selected_button -= 1;
+    m_selected_button = std::max(m_selected_button, 0);
+  }
+
+  if (controller.pressed(Controller::RIGHT))
+  {
+    m_selected_button += 1;
+    m_selected_button = std::min(m_selected_button, static_cast<int>(m_buttons.size()) - 1);
+  }     
+}
+
+void
+Dialog::draw(DrawingContext& ctx)
+{
+  for(int i = 0; i < static_cast<int>(m_buttons.size()); ++i)
+  {
+    if (i == m_selected_button)
+    {
+      // highlight
+    }
+    ctx.draw_text(Resources::normal_font, m_buttons[i],
+                  Vector(100, 100),
+                  ALIGN_CENTER, LAYER_GUI);
+  }
+}
+
+/* EOF */
diff --git a/src/gui/dialog.hpp b/src/gui/dialog.hpp
new file mode 100644 (file)
index 0000000..272cb4b
--- /dev/null
@@ -0,0 +1,48 @@
+//  SuperTux
+//  Copyright (C) 2014 Ingo Ruhnke <grumbel@gmail.com>
+//
+//  This program is free software: you can redistribute it and/or modify
+//  it under the terms of the GNU General Public License as published by
+//  the Free Software Foundation, either version 3 of the License, or
+//  (at your option) any later version.
+//
+//  This program is distributed in the hope that it will be useful,
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+//  GNU General Public License for more details.
+//
+//  You should have received a copy of the GNU General Public License
+//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+#ifndef HEADER_SUPERTUX_GUI_DIALOG_HPP
+#define HEADER_SUPERTUX_GUI_DIALOG_HPP
+
+#include <string>
+#include <vector>
+
+class Controller;
+class DrawingContext;
+
+class Dialog
+{
+private:
+  std::string m_text;
+  std::vector<std::string> m_buttons;
+  int m_selected_button;
+
+public:
+  Dialog();
+
+  void add_button(const std::string& text);
+
+  void process_input(const Controller& controller);
+  void draw(DrawingContext& context);
+
+private:
+  Dialog(const Dialog&) = delete;
+  Dialog& operator=(const Dialog&) = delete;
+};
+
+#endif
+
+/* EOF */
index 9a34c98..17ca934 100644 (file)
@@ -19,6 +19,7 @@
 #include <assert.h>
 
 #include "control/input_manager.hpp"
+#include "gui/dialog.hpp"
 #include "gui/menu.hpp"
 #include "gui/mousecursor.hpp"
 #include "math/sizef.hpp"
@@ -134,6 +135,7 @@ public:
 };
 
 MenuManager::MenuManager() :
+  m_dialog(),
   m_menu_stack(),
   m_transition(new MenuTransition)
 {
@@ -157,7 +159,11 @@ MenuManager::refresh()
 void
 MenuManager::process_input()
 {
-  if (current())
+  if (m_dialog)
+  {
+    m_dialog->process_input(*InputManager::current()->get_controller());
+  }
+  else if (current())
   {
     current()->process_input();
   }
@@ -184,7 +190,11 @@ MenuManager::draw(DrawingContext& context)
   }
   else
   {
-    if (current())
+    if (m_dialog)
+    {
+      m_dialog->draw(context);
+    }
+    else if (current())
     {
       // brute force the transition into the right shape in case the
       // menu has changed sizes
@@ -202,6 +212,12 @@ MenuManager::draw(DrawingContext& context)
 }
 
 void
+MenuManager::set_dialog(std::unique_ptr<Dialog> dialog)
+{
+  m_dialog = std::move(dialog);
+}
+
+void
 MenuManager::push_menu(int id)
 {
   push_menu(MenuStorage::instance().create(static_cast<MenuStorage::MenuId>(id)));
index 8fb656c..f7166a6 100644 (file)
@@ -23,6 +23,7 @@
 
 #include "SDL.h"
 
+class Dialog;
 class DrawingContext;
 class Menu;
 class MenuTransition;
@@ -35,6 +36,7 @@ public:
   static MenuManager& instance();
 
 private:
+  std::unique_ptr<Dialog> m_dialog;
   std::vector<std::unique_ptr<Menu> > m_menu_stack;
   std::unique_ptr<MenuTransition> m_transition;
 
@@ -48,6 +50,8 @@ public:
 
   void draw(DrawingContext& context);
 
+  void set_dialog(std::unique_ptr<Dialog> dialog);
+
   void set_menu(int id);
   void set_menu(std::unique_ptr<Menu> menu);
   void push_menu(int id);