pingus-cvs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Pingus-CVS] r3759 - in trunk/pingus/src: . components


From: grumbel at BerliOS
Subject: [Pingus-CVS] r3759 - in trunk/pingus/src: . components
Date: Thu, 10 Jul 2008 11:43:48 +0200

Author: grumbel
Date: 2008-07-10 11:43:47 +0200 (Thu, 10 Jul 2008)
New Revision: 3759

Modified:
   trunk/pingus/src/components/action_button.cpp
   trunk/pingus/src/components/action_button.hpp
   trunk/pingus/src/components/button_panel.cpp
   trunk/pingus/src/components/button_panel.hpp
   trunk/pingus/src/game_session.cpp
Log:
Reimplemented large parts of ButtonPanel, removed/merged ActionButton in the 
process and allow proper resize behaviour

Modified: trunk/pingus/src/components/action_button.cpp
===================================================================
--- trunk/pingus/src/components/action_button.cpp       2008-07-10 06:50:52 UTC 
(rev 3758)
+++ trunk/pingus/src/components/action_button.cpp       2008-07-10 09:43:47 UTC 
(rev 3759)
@@ -27,53 +27,6 @@
 
 using namespace Actions;
 
-ActionButton::ActionButton(ActionHolder* h,
-                           int x, int y, ActionName name_, int owner_id)
-  : RectComponent(Rect(Vector2i(x, y), Size(60, 38))),
-    action_holder(h),
-    background("core/buttons/buttonbackground"),
-    backgroundhl("core/buttons/buttonbackgroundhl"),
-    name(name_)
-{
-  sprite = Sprite("pingus/player0/" + action_to_string(name) + "/right");
-  sprite.set_play_loop(true);
-}
-
-ActionButton::~ActionButton() {}
-
-void
-ActionButton::update(float delta)
-{
-  sprite.update(delta);
-}
-
-ActionName
-ActionButton::get_action_name()
-{
-  return name;
-}
-
-void
-ActionButton::draw (DrawingContext& gc)
-{
-  Vector2i pos(rect.left, rect.top);
-
-  if (pressed)
-    {
-      gc.draw(backgroundhl, pos);
-    }
-  else
-    {
-      sprite.set_frame(0);
-      gc.draw(background, pos);
-    }
-  
-  gc.draw(sprite, Vector2i(pos.x + 20, pos.y + 32));
-  
-  std::string str = StringUtil::to_string(action_holder->get_available(name));
-  gc.print_center(Fonts::pingus_small, pos.x + 46, pos.y + 5, str);
-}
-
 ArmageddonButton::ArmageddonButton(Server* s, int x, int y)
   : RectComponent(Rect(Vector2i(x, y), Size(38, 60))),
     server(s),

Modified: trunk/pingus/src/components/action_button.hpp
===================================================================
--- trunk/pingus/src/components/action_button.hpp       2008-07-10 06:50:52 UTC 
(rev 3758)
+++ trunk/pingus/src/components/action_button.hpp       2008-07-10 09:43:47 UTC 
(rev 3759)
@@ -106,35 +106,6 @@
   PauseButton& operator= (const PauseButton&);
 };
 
-/** The button class manage a simple button for the button_panel. It
-    keeps his position, his surfaces and his font. */
-class ActionButton : public GUI::RectComponent
-{
-protected:
-  ActionHolder* action_holder;
-  Sprite sprite;
-  Sprite background;
-  Sprite backgroundhl;
-
-  Actions::ActionName name;
-
-public:
-  bool pressed; // used in ButtonPanel
-
-  ActionButton(ActionHolder* h, int x, int y, Actions::ActionName name_, int 
owner_id);
-  virtual ~ActionButton();
-
-  void draw(DrawingContext& gc);
-  void update(float delta);
-
-  /// Returns the name of the action the button represents.
-  Actions::ActionName get_action_name();
-
-private:
-  ActionButton (const ActionButton&);
-  ActionButton& operator= (const ActionButton&);
-};
-
 #endif /* ACTIONBUTTON */
 
 /* EOF */

Modified: trunk/pingus/src/components/button_panel.cpp
===================================================================
--- trunk/pingus/src/components/button_panel.cpp        2008-07-10 06:50:52 UTC 
(rev 3758)
+++ trunk/pingus/src/components/button_panel.cpp        2008-07-10 09:43:47 UTC 
(rev 3759)
@@ -16,145 +16,125 @@
 
 #include <iostream>
 #include <algorithm>
+#include "../math.hpp"
+#include "../fonts.hpp"
+#include "../string_util.hpp"
 #include "../globals.hpp"
 #include "../server.hpp"
 #include "../game_session.hpp"
+#include "../display/drawing_context.hpp"
 #include "button_panel.hpp"
 
 using namespace Actions;
-
-struct action_sorter {
-  bool operator() (const ActionName& a, const ActionName& b) {
-    return a < b;
-  }
-};
-
-ButtonPanel::ButtonPanel(GameSession* s, int arg_x_pos, int arg_y_pos)
-  : session(s),
-    left_pressed(0),
-    last_press(0),
-    x_pos (arg_x_pos),
-    y_pos (arg_y_pos)
+
+ButtonPanel::ButtonPanel(GameSession* s, const Vector2i& pos)
+  : RectComponent(Rect()),
+    session(s),
+    background("core/buttons/buttonbackground"),
+    highlight("core/buttons/buttonbackgroundhl"),
+    current_button(0)
 {
   ActionHolder* aholder = session->get_server()->get_action_holder();
 
   std::vector<ActionName> actions = aholder->get_available_actions();
 
-  // Sort the action so that they always have the same order in the
-  // panel
-  std::sort(actions.begin(), actions.end(), action_sorter());
+  set_rect(Rect(Vector2i(pos.x, pos.y - (actions.size() * 38)/2),
+                Size(60, actions.size() * 38)));
 
-  y_pos -= ((int)actions.size() * 38)/2 + 70;
+  // Sort the action so that they always have the same order in the panel
+  std::sort(actions.begin(), actions.end());
 
-  for(std::vector<ActionName>::iterator i = actions.begin();
-      i != actions.end(); ++i)
+  for(std::vector<ActionName>::size_type i = 0; i < actions.size(); ++i)
     {
-      a_buttons.push_back(new ActionButton (aholder,
-                                            x_pos, int(i - actions.begin()) * 
38 + y_pos,
-                                            *i,
-                                            0)); //FIXMEcontroller->get_owner 
()));
+      ActionButton button;
+      button.name   = actions[i];
+      button.sprite = Sprite("pingus/player0/" + action_to_string(button.name) 
+ "/right");
+      button.sprite.set_play_loop(true);
+      buttons.push_back(button);
     }
-
-  if (a_buttons.empty())
-    {
-      std::cout << "Error: ButtonPanel: No a_buttons! " << std::endl;
-    }
-
-  pressed_button = 0;
 }
 
 ButtonPanel::~ButtonPanel()
 {
-  for (AButtonIter it = a_buttons.begin(); it != a_buttons.end(); ++it)
-  {
-    delete *it;
-  }
 }
 
 void
-ButtonPanel::update(float delta)
+ButtonPanel::draw(DrawingContext& gc)
 {
-  if (!a_buttons.empty())
-    a_buttons[pressed_button]->update(delta);
-}
+  ActionHolder* aholder = session->get_server()->get_action_holder();
 
-ActionName
-ButtonPanel::get_action_name()
-{
-  if (!a_buttons.empty())
-    return a_buttons[pressed_button]->get_action_name();
-  else
-    return Actions::Bridger;
+  for(std::vector<ActionButton>::size_type i = 0; i < buttons.size(); ++i)
+    {
+      if (current_button == i)
+        gc.draw(highlight, rect.left, rect.top + 38*i);
+      else
+        gc.draw(background, rect.left, rect.top + 38*i);
+
+      gc.draw(buttons[i].sprite, rect.left + 20, rect.top + 38*i + 32);
+
+      std::string str = 
StringUtil::to_string(aholder->get_available(buttons[i].name));
+      gc.print_center(Fonts::pingus_small, rect.left + 46, rect.top + 5 + 
38*i, str);
+    }
 }
 
 void
-ButtonPanel::draw(DrawingContext& gc)
+ButtonPanel::update (float delta)
 {
-  for(int i = 0; i < static_cast<int>(a_buttons.size()); ++i)
-    {
-      if (i == pressed_button)
-       a_buttons[i]->pressed = true;
-      else
-        a_buttons[i]->pressed = false;
+  for(std::vector<ActionButton>::size_type i = 0; i < buttons.size(); ++i)
+    if (i == current_button)
+      buttons[i].sprite.update(delta);
+    else
+      buttons[i].sprite.set_frame(0);
+}
 
-      a_buttons[i]->draw(gc);
-    }
+ActionName
+ButtonPanel::get_action_name()
+{
+  return buttons[current_button].name;
 }
 
 void
 ButtonPanel::set_button(int n)
 {
-  if (n < 0 || n >= static_cast<int>(a_buttons.size()))
+  if (n >= 0 || n < static_cast<int>(buttons.size()))
     {
-      // FIXME: Play 'boing' sound here
+      current_button = n;
     }
   else
     {
-      pressed_button = n;
+      // FIXME: Play 'boing' sound here
     }
 }
 
 void
-ButtonPanel::on_primary_button_press(int x, int y)
+ButtonPanel::next_action()
 {
-  for(AButtonIter button = a_buttons.begin(); button != a_buttons.end(); 
button++)
-    {
-      if ((*button)->is_at(x, y))
-       pressed_button = int(button - a_buttons.begin());
-    }
+  current_button = (current_button + 1) + int(buttons.size()) % 
int(buttons.size());
 }
 
-bool
-ButtonPanel::is_at (int x, int y)
+void
+ButtonPanel::previous_action()
 {
-  for(AButtonIter button = a_buttons.begin(); button != a_buttons.end(); 
button++)
-    {
-      if ((*button)->is_at(x, y))
-       return true;
-    }
-  return false;
+  current_button = (current_button - 1) + int(buttons.size()) % 
int(buttons.size());
 }
 
 void
-ButtonPanel::on_primary_button_release(int x, int y)
+ButtonPanel::on_primary_button_press(int x, int y)
 {
-  UNUSED_ARG(x);
-  UNUSED_ARG(y);
+  int action = (y - rect.top) / 38;
+  current_button = Math::clamp(0, action, int(buttons.size()-1));
 }
 
-/// Select the next action
 void
-ButtonPanel::next_action ()
+ButtonPanel::on_primary_button_release(int x, int y)
 {
-  pressed_button = (pressed_button + 1) % (int)a_buttons.size();
 }
 
-/// Select the previous action
 void
-ButtonPanel::previous_action ()
+ButtonPanel::set_pos(const Vector2i& pos)
 {
-  pressed_button = (pressed_button - 1 + (int)a_buttons.size()) % 
(int)a_buttons.size();
+  set_rect(Rect(Vector2i(pos.x, pos.y - (buttons.size() * 38)/2),
+                Size(60, buttons.size() * 38)));
 }
-
-
+
 /* EOF */

Modified: trunk/pingus/src/components/button_panel.hpp
===================================================================
--- trunk/pingus/src/components/button_panel.hpp        2008-07-10 06:50:52 UTC 
(rev 3758)
+++ trunk/pingus/src/components/button_panel.hpp        2008-07-10 09:43:47 UTC 
(rev 3759)
@@ -18,46 +18,47 @@
 #define HEADER_PINGUS_BUTTON_PANEL_HPP
 
 #include <vector>
+#include "../sprite.hpp"
+#include "gui/rect_component.hpp"
 #include "action_button.hpp"
 
 class GameSession;
 
-class ButtonPanel : public GUI::Component
+class ButtonPanel : public GUI::RectComponent
 {
 private:
+  struct ActionButton {
+    Actions::ActionName name;
+    Sprite     sprite;
+  };
+
   GameSession* session;
+  Sprite background;
+  Sprite highlight;
 
-  std::vector<ActionButton*> a_buttons;
-  typedef std::vector<ActionButton*>::iterator AButtonIter;
-  int pressed_button;
+  std::vector<ActionButton> buttons;
+  std::vector<ActionButton>::size_type current_button;
 
-  bool left_pressed;
-  unsigned int  last_press;
-
-  int x_pos, y_pos;
-
 public:
-  ButtonPanel(GameSession* s, int arg_x_pos, int arg_y_pos);
+  ButtonPanel(GameSession* s, const Vector2i& pos);
   virtual ~ButtonPanel();
 
-  void on_primary_button_press(int x, int y);
-  void on_primary_button_release(int x, int y);
+  void draw(DrawingContext& gc);
+  void update (float delta);
 
-  bool is_at (int x, int y);
-
   Actions::ActionName get_action_name();
-  void   update(float delta);
-  void   draw(DrawingContext& gc);
 
-  /// Set n'th action
-  void   set_button(int);
+  // Set the n'th button active
+  void set_button(int n);
 
-  /// Select the next action
-  void next_action ();
+  void next_action();
+  void previous_action();
 
-  /// Select the previous action
-  void previous_action ();
+  void on_primary_button_press(int x, int y);
+  void on_primary_button_release(int x, int y);
 
+  void set_pos(const Vector2i& pos);
+  
 private:
   ButtonPanel (const ButtonPanel&);
   ButtonPanel& operator= (const ButtonPanel&);

Modified: trunk/pingus/src/game_session.cpp
===================================================================
--- trunk/pingus/src/game_session.cpp   2008-07-10 06:50:52 UTC (rev 3758)
+++ trunk/pingus/src/game_session.cpp   2008-07-10 09:43:47 UTC (rev 3759)
@@ -59,7 +59,7 @@
   // -- Client stuff
   
   // These object will get deleted by the gui_manager
-  button_panel = new ButtonPanel(this, 2, Display::get_height()/2);
+  button_panel = new ButtonPanel(this, Vector2i(0, (size.height - 150)/2));
 
   int world_width  = server->get_world()->get_width();
   int world_height = server->get_world()->get_height();
@@ -393,6 +393,8 @@
                                 Size(38, 60)));
 
   small_map->set_rect(Rect(Vector2i(5, size.height - 105), Size(175, 100)));
+
+  button_panel->set_pos(Vector2i(0, (size.height - 150)/2));
 }
 
 /* EOF */





reply via email to

[Prev in Thread] Current Thread [Next in Thread]