From eab5c46b1ee9b6cb033e88e0ef657fa746d03eb9 Mon Sep 17 00:00:00 2001
From: Benlitz <dev@benlitz.net>
Date: Sat, 14 Sep 2013 18:55:42 +0000
Subject: [PATCH] input: added a name the controllers, and a static Get()
 function to easily retrieve a registered controller

---
 demos/tutorial/07_input.cpp |  2 +-
 src/input/controller.cpp    | 17 ++++++++++++++++-
 src/input/controller.h      |  9 ++++++---
 test/btphystest.cpp         |  2 +-
 test/meshviewer.cpp         |  2 +-
 5 files changed, 25 insertions(+), 7 deletions(-)

diff --git a/demos/tutorial/07_input.cpp b/demos/tutorial/07_input.cpp
index 342729ec..ba8e8303 100644
--- a/demos/tutorial/07_input.cpp
+++ b/demos/tutorial/07_input.cpp
@@ -25,7 +25,7 @@ class InputTutorial : public WorldEntity
 public:
     InputTutorial()
     {
-        m_controller = new Controller(KEY_MAX, AXIS_MAX);
+        m_controller = new Controller("Default", KEY_MAX, AXIS_MAX);
 
         m_keyboard = InputDevice::Get("Keyboard");
         if (m_keyboard)
diff --git a/src/input/controller.cpp b/src/input/controller.cpp
index 596ae8b2..9a8b6b03 100644
--- a/src/input/controller.cpp
+++ b/src/input/controller.cpp
@@ -139,14 +139,19 @@ float AxisBinding::RetrieveCurrentValue()
 
 Array<Controller*> Controller::controllers;
 
-Controller::Controller(int nb_keys, int nb_axis)
+Controller::Controller(char const* name, int nb_keys, int nb_axis)
 {
     m_gamegroup = GAMEGROUP_BEFORE;
+    m_name = name;
     m_keys.Resize(nb_keys);
     m_axis.Resize(nb_axis);
     m_activate_nextframe = false;
     m_deactivate_nextframe = false;
     m_active = false;
+	if (Get(name) != nullptr)
+	{
+		Log::Warn("a controller with this name has already been registered");
+	}
     controllers.Push(this);
 }
 
@@ -162,6 +167,16 @@ Controller::~Controller()
     }
 }
 
+Controller* Controller::Get(char const* name)
+{
+    for (int i = 0; i < controllers.Count(); ++i)
+    {
+        if (controllers[i]->m_name == name)
+            return controllers[i];
+    }
+    return nullptr;
+}
+
 void Controller::TickGame(float seconds)
 {
     Entity::TickGame(seconds);
diff --git a/src/input/controller.h b/src/input/controller.h
index 22ccf3cc..00ff4822 100644
--- a/src/input/controller.h
+++ b/src/input/controller.h
@@ -73,7 +73,7 @@ public:
 
 protected:
     void Update() { m_previous = m_current; m_current = IsBound() ? RetrieveCurrentValue() : 0.0f; }
-	float RetrieveCurrentValue();
+    float RetrieveCurrentValue();
 
     const InputDevice* m_device;
     int m_axisindex;
@@ -86,10 +86,10 @@ protected:
 };
 
 
-class Controller : Entity
+class Controller : public Entity
 {
 public:
-    Controller(int nb_keys, int nb_axis);
+    Controller(char const* name, int nb_keys, int nb_axis);
     ~Controller();
 
     virtual void TickGame(float seconds);
@@ -104,12 +104,15 @@ public:
     KeyBinding& GetKey(int index) { return m_keys[index]; }
     AxisBinding& GetAxis(int index) { return m_axis[index]; }
 
+    static Controller* Get(char const* name);
+
 protected:
     Array<KeyBinding> m_keys;
     Array<AxisBinding> m_axis;
 
 private:
     static Array<Controller*> controllers;
+    String m_name;
     bool m_activate_nextframe;
     bool m_deactivate_nextframe;
     bool m_active;
diff --git a/test/btphystest.cpp b/test/btphystest.cpp
index b448c084..fa5dd227 100644
--- a/test/btphystest.cpp
+++ b/test/btphystest.cpp
@@ -72,7 +72,7 @@ void BtPhysTest::InitApp()
 #endif //CAT_MODE
 
     /* Register an input controller for the keyboard */
-    m_controller = new Controller(KEY_MAX, 0);
+    m_controller = new Controller("Default", KEY_MAX, 0);
     m_controller->GetKey(KEY_MOVE_FORWARD).Bind("Keyboard", "Up");
     m_controller->GetKey(KEY_MOVE_BACK).Bind("Keyboard", "Down");
     m_controller->GetKey(KEY_MOVE_LEFT).Bind("Keyboard", "Left");
diff --git a/test/meshviewer.cpp b/test/meshviewer.cpp
index a475bb46..c5c96f81 100644
--- a/test/meshviewer.cpp
+++ b/test/meshviewer.cpp
@@ -82,7 +82,7 @@ public:
       : m_file_name(file_name)
     {
         /* Register an input controller for the keyboard */
-        m_controller = new Controller(KEY_MAX, 0);
+        m_controller = new Controller("Default", KEY_MAX, 0);
 
         m_controller->GetKey(KEY_CAM_RESET).Bind("Keyboard", "Return");
         m_controller->GetKey(KEY_CAM_ZOOM_IN).Bind("Keyboard", "PageUp");