Browse Source

input: added a name the controllers, and a static Get() function to easily retrieve a registered controller

undefined
Benlitz Sam Hocevar <sam@hocevar.net> 12 years ago
parent
commit
eab5c46b1e
5 changed files with 25 additions and 7 deletions
  1. +1
    -1
      demos/tutorial/07_input.cpp
  2. +16
    -1
      src/input/controller.cpp
  3. +6
    -3
      src/input/controller.h
  4. +1
    -1
      test/btphystest.cpp
  5. +1
    -1
      test/meshviewer.cpp

+ 1
- 1
demos/tutorial/07_input.cpp View File

@@ -25,7 +25,7 @@ class InputTutorial : public WorldEntity
public: public:
InputTutorial() InputTutorial()
{ {
m_controller = new Controller(KEY_MAX, AXIS_MAX);
m_controller = new Controller("Default", KEY_MAX, AXIS_MAX);


m_keyboard = InputDevice::Get("Keyboard"); m_keyboard = InputDevice::Get("Keyboard");
if (m_keyboard) if (m_keyboard)


+ 16
- 1
src/input/controller.cpp View File

@@ -139,14 +139,19 @@ float AxisBinding::RetrieveCurrentValue()


Array<Controller*> Controller::controllers; 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_gamegroup = GAMEGROUP_BEFORE;
m_name = name;
m_keys.Resize(nb_keys); m_keys.Resize(nb_keys);
m_axis.Resize(nb_axis); m_axis.Resize(nb_axis);
m_activate_nextframe = false; m_activate_nextframe = false;
m_deactivate_nextframe = false; m_deactivate_nextframe = false;
m_active = false; m_active = false;
if (Get(name) != nullptr)
{
Log::Warn("a controller with this name has already been registered");
}
controllers.Push(this); 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) void Controller::TickGame(float seconds)
{ {
Entity::TickGame(seconds); Entity::TickGame(seconds);


+ 6
- 3
src/input/controller.h View File

@@ -73,7 +73,7 @@ public:


protected: protected:
void Update() { m_previous = m_current; m_current = IsBound() ? RetrieveCurrentValue() : 0.0f; } void Update() { m_previous = m_current; m_current = IsBound() ? RetrieveCurrentValue() : 0.0f; }
float RetrieveCurrentValue();
float RetrieveCurrentValue();


const InputDevice* m_device; const InputDevice* m_device;
int m_axisindex; int m_axisindex;
@@ -86,10 +86,10 @@ protected:
}; };




class Controller : Entity
class Controller : public Entity
{ {
public: public:
Controller(int nb_keys, int nb_axis);
Controller(char const* name, int nb_keys, int nb_axis);
~Controller(); ~Controller();


virtual void TickGame(float seconds); virtual void TickGame(float seconds);
@@ -104,12 +104,15 @@ public:
KeyBinding& GetKey(int index) { return m_keys[index]; } KeyBinding& GetKey(int index) { return m_keys[index]; }
AxisBinding& GetAxis(int index) { return m_axis[index]; } AxisBinding& GetAxis(int index) { return m_axis[index]; }


static Controller* Get(char const* name);

protected: protected:
Array<KeyBinding> m_keys; Array<KeyBinding> m_keys;
Array<AxisBinding> m_axis; Array<AxisBinding> m_axis;


private: private:
static Array<Controller*> controllers; static Array<Controller*> controllers;
String m_name;
bool m_activate_nextframe; bool m_activate_nextframe;
bool m_deactivate_nextframe; bool m_deactivate_nextframe;
bool m_active; bool m_active;


+ 1
- 1
test/btphystest.cpp View File

@@ -72,7 +72,7 @@ void BtPhysTest::InitApp()
#endif //CAT_MODE #endif //CAT_MODE


/* Register an input controller for the keyboard */ /* 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_FORWARD).Bind("Keyboard", "Up");
m_controller->GetKey(KEY_MOVE_BACK).Bind("Keyboard", "Down"); m_controller->GetKey(KEY_MOVE_BACK).Bind("Keyboard", "Down");
m_controller->GetKey(KEY_MOVE_LEFT).Bind("Keyboard", "Left"); m_controller->GetKey(KEY_MOVE_LEFT).Bind("Keyboard", "Left");


+ 1
- 1
test/meshviewer.cpp View File

@@ -82,7 +82,7 @@ public:
: m_file_name(file_name) : m_file_name(file_name)
{ {
/* Register an input controller for the keyboard */ /* 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_RESET).Bind("Keyboard", "Return");
m_controller->GetKey(KEY_CAM_ZOOM_IN).Bind("Keyboard", "PageUp"); m_controller->GetKey(KEY_CAM_ZOOM_IN).Bind("Keyboard", "PageUp");


Loading…
Cancel
Save