clean up InputHandler/GameView to use signals for input new_graphics
authorTero Marttila <terom@fixme.fi>
Thu, 22 Jan 2009 00:28:26 +0200
branchnew_graphics
changeset 416 38cba347a3a9
parent 415 c1069d23890b
child 417 c503e0c6a740
clean up InputHandler/GameView to use signals for input
src/Graphics/GameView.cc
src/Graphics/GameView.hh
src/Graphics/Input.cc
src/Graphics/Input.hh
--- a/src/Graphics/GameView.cc	Thu Jan 22 00:13:53 2009 +0200
+++ b/src/Graphics/GameView.cc	Thu Jan 22 00:28:26 2009 +0200
@@ -20,6 +20,7 @@
     message_view.add_message(CL_Color::white, "Hello World!");
 
     // enable GUI input
+    slots.connect(graphics->input.gui.sig_input(), this, &GameView::handleInput);
     graphics->input.gui.enable();
 }
 
@@ -33,6 +34,7 @@
     info_view = new PlayerInfoView(getInfoViewArea(), player);
 
     // enable player input
+    slots.connect(graphics->input.player.sig_input(), player, &LocalPlayer::handleInput);
     graphics->input.player.enable();
 }
 
@@ -82,19 +84,6 @@
 void GameView::draw (Display &display) {
     CL_GraphicContext *gc = display.get_gc();
     
-    // XXX: these should not be done from here
-    handleInput(graphics->input.readGuiInput(), 0);
-    
-    // XXX: this should /really/ be somewhere else
-    if (player) {
-        PlayerInput input;
-        TimeMS dt;
-
-        graphics->input.readPlayerInput(input, dt);
-
-        player->handleInput(input, dt);
-    }
-
     // calculate camera
     PixelCoordinate camera(0, 0);
     
--- a/src/Graphics/GameView.hh	Thu Jan 22 00:13:53 2009 +0200
+++ b/src/Graphics/GameView.hh	Thu Jan 22 00:28:26 2009 +0200
@@ -39,6 +39,8 @@
      */
     GuiInput flags;
 
+    CL_SlotContainer slots;
+
 public:
     /**
      * Constructed once the game is running
--- a/src/Graphics/Input.cc	Thu Jan 22 00:13:53 2009 +0200
+++ b/src/Graphics/Input.cc	Thu Jan 22 00:28:26 2009 +0200
@@ -151,7 +151,7 @@
         return;
 
     // all bits that are held down, even those ignored
-    BitMaskType raw_value = 0;
+    BitMaskType raw_value = 0, this_value = 0;
 
     // update the key-repeat queue
     queue.update(dt);
@@ -187,15 +187,23 @@
                 }
             }
 
-            // set bit in masks
-            this->value |= e->input;
+            // set bit in value mask
+            this_value |= e->input;
         }
     }
 
+    // signal unless value was and remains zero
+    if (this_value || prev_value) {
+        // trigger signal
+        _sig_input(this_value, dt);
+    } 
+
     // update prev_value, also adding ignored values
     prev_value = raw_value;
 
-    // then increment our dt
+    // update our collective value + dt for use with readValue
+    // XXX: remove this functionality?
+    value |= this_value;
     this->dt += dt;
 }
  
--- a/src/Graphics/Input.hh	Thu Jan 22 00:13:53 2009 +0200
+++ b/src/Graphics/Input.hh	Thu Jan 22 00:28:26 2009 +0200
@@ -5,6 +5,7 @@
 #include "../Timer.hh"
 #include "../Types.hh"
 
+#include <ClanLib/signals.h>
 #include <ClanLib/Display/input_device.h>
 #include <ClanLib/Display/keys.h>
 
@@ -198,6 +199,11 @@
          * Are we enabled or not?
          */
         bool _enabled;
+
+        /**
+         * The keyevent signal
+         */
+        CL_Signal_v2<BitMaskType, TimeMS> _sig_input;
     
     public:
         /**
@@ -249,6 +255,11 @@
          * Current enable/disable state
          */
         bool enabled (void) const { return _enabled; }
+
+        /**
+         * This signal is triggered whenever there is nonzero input, or when the input goes to zero
+         */
+        CL_Signal_v2<BitMaskType, TimeMS>& sig_input (void) { return _sig_input; }
 };
 
 /**
@@ -301,7 +312,6 @@
 
 
 
-
 /*
  * Public template class method definitions
  */