Skip to content

Commit

Permalink
remove InputManager/Channel::close(), switch oberservs to raw
Browse files Browse the repository at this point in the history
  • Loading branch information
babyccino committed Feb 28, 2018
1 parent cad59c3 commit 4d93f11
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 41 deletions.
5 changes: 0 additions & 5 deletions include/radix/input/Channel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ class Channel: public ChannelBase<T>, ChannelListener {
void init(const int &id, EventDispatcher &event, const std::vector<Bind> &binds);
void reInit(EventDispatcher &event);

void close();

virtual void channelChanged(const int &id) override;

private:
Expand All @@ -50,13 +48,10 @@ class SubChannel: public ChannelBase<T> {
void init(const int &id, EventDispatcher &event, const Bind &bind);
void reInit(EventDispatcher &event);

void close();

private:
void addObservers(EventDispatcher &event);

Bind bind;
std::array<EventDispatcher::CallbackHolder, 2> callbacks;

};

Expand Down
2 changes: 0 additions & 2 deletions include/radix/input/InputManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ class InputManager : public ChannelListener {
void init();
void reInit();

void close();

virtual void channelChanged(const int &id) override;
Vector2f getPlayerMovementVector() const;

Expand Down
3 changes: 2 additions & 1 deletion source/BaseGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ BaseGame::~BaseGame() {
}

void BaseGame::setup() {
radix::Util::Log(radix::Info, "GameController") << "BaseGame::setup() start;";
radix::GameConsole console;
if (config.isConsoleEnabled()) {
console.run(*this);
Expand All @@ -72,6 +73,7 @@ void BaseGame::setup() {
renderer->addRenderer(*screenRenderer);

inputManager.init();

}

bool BaseGame::isRunning() {
Expand Down Expand Up @@ -143,7 +145,6 @@ void BaseGame::customTriggerHook() { }

void BaseGame::cleanUp() {
removeHook();
inputManager.close();
setWorld({});
window.close();
}
Expand Down
34 changes: 11 additions & 23 deletions source/input/Channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,10 @@ void Channel<T>::init(const int &id, EventDispatcher &event, const std::vector<B

template<class T>
void Channel<T>::reInit(EventDispatcher &event) {
for (SubChannel<T> &subChannel : subChannels) {
subChannel.reInit(event);
}
}
this->notifyListeners();

template<class T>
void Channel<T>::close() {
for (SubChannel<T> &subChannel : subChannels) {
subChannel.close();
subChannel.reInit(event);
}
}

Expand Down Expand Up @@ -86,25 +81,18 @@ void SubChannel<T>::reInit(EventDispatcher &event) {
this->addObservers(event);
}

template<class T>
void SubChannel<T>::close() {
for (EventDispatcher::CallbackHolder& callback : callbacks) {
callback.removeThis();
}
}

template<class T>
void SubChannel<T>::addObservers(EventDispatcher &event) {
switch (bind.inputType) {
case Bind::KEYBOARD: {
this->callbacks[0] = event.addObserver(InputSource::KeyPressedEvent::Type, [this](const radix::Event& event) {
event.addObserverRaw(InputSource::KeyPressedEvent::Type, [this](const radix::Event& event) {
const int key = ((radix::InputSource::KeyPressedEvent &) event).key;
if (key == this->bind.inputCode) {
this->set(1.0f);
}
});

this->callbacks[1] = event.addObserver(InputSource::KeyReleasedEvent::Type, [this](const radix::Event& event) {
event.addObserverRaw(InputSource::KeyReleasedEvent::Type, [this](const radix::Event& event) {
const int key = ((radix::InputSource::KeyReleasedEvent &) event).key;
if (key == this->bind.inputCode) {
this->set(0.0f);
Expand All @@ -113,14 +101,14 @@ void SubChannel<T>::addObservers(EventDispatcher &event) {
break;
}
case Bind::MOUSE_BUTTON: {
this->callbacks[0] = event.addObserver(InputSource::MouseButtonPressedEvent::Type, [this](const radix::Event& event) {
event.addObserverRaw(InputSource::MouseButtonPressedEvent::Type, [this](const radix::Event& event) {
const int button = (int)((radix::InputSource::MouseButtonPressedEvent &) event).button;
if (button == this->bind.inputCode) {
this->set((T)1.0f);
}
});

this->callbacks[1] = event.addObserver(InputSource::MouseButtonReleasedEvent::Type, [this](const radix::Event& event) {
event.addObserverRaw(InputSource::MouseButtonReleasedEvent::Type, [this](const radix::Event& event) {
const int button = (int)((radix::InputSource::MouseButtonPressedEvent &) event).button;
if (button == this->bind.inputCode) {
this->set((T)0.0f);
Expand All @@ -129,14 +117,14 @@ void SubChannel<T>::addObservers(EventDispatcher &event) {
break;
}
case Bind::CONTROLLER_BUTTON: {
this->callbacks[0] = event.addObserver(InputSource::ControllerButtonPressedEvent::Type, [this](const radix::Event& event) {
event.addObserverRaw(InputSource::ControllerButtonPressedEvent::Type, [this](const radix::Event& event) {
const int button = ((radix::InputSource::ControllerButtonPressedEvent &) event).button;
if (button == this->bind.inputCode) {
this->set((T)1.0f);
}
});

this->callbacks[1] = event.addObserver(InputSource::ControllerButtonReleasedEvent::Type, [this](const radix::Event& event) {
event.addObserverRaw(InputSource::ControllerButtonReleasedEvent::Type, [this](const radix::Event& event) {
const int button = ((radix::InputSource::ControllerButtonReleasedEvent &) event).button;
if (button == this->bind.inputCode) {
this->set((T)0.0f);
Expand All @@ -145,7 +133,7 @@ void SubChannel<T>::addObservers(EventDispatcher &event) {
break;
}
case Bind::CONTROLLER_TRIGGER: {
this->callbacks[0] = event.addObserver(InputSource::ControllerTriggerEvent::Type, [this](const radix::Event& event) {
event.addObserverRaw(InputSource::ControllerTriggerEvent::Type, [this](const radix::Event& event) {
const int trigger = ((radix::InputSource::ControllerTriggerEvent &) event).trigger;
const float value = ((radix::InputSource::ControllerTriggerEvent &) event).value;
if (trigger == this->bind.inputCode) {
Expand All @@ -161,15 +149,15 @@ template<>
void SubChannel<Vector2f>::addObservers(EventDispatcher &event) {
switch (bind.inputType) {
case Bind::MOUSE_AXIS: {
this->callbacks[0] = event.addObserver(InputSource::MouseAxisEvent::Type, [this](const radix::Event& event) {
event.addObserverRaw(InputSource::MouseAxisEvent::Type, [this](const radix::Event& event) {
const Vector2f value = ((radix::InputSource::MouseAxisEvent &) event).value;

this->set(value);
});
break;
}
case Bind::CONTROLLER_AXIS: {
this->callbacks[1] = event.addObserver(InputSource::ControllerAxisEvent::Type, [this](const radix::Event& event) {
event.addObserverRaw(InputSource::ControllerAxisEvent::Type, [this](const radix::Event& event) {
const int axis = ((radix::InputSource::ControllerAxisEvent &) event).axis;
const Vector2f value = ((radix::InputSource::ControllerAxisEvent &) event).value;

Expand Down
10 changes: 0 additions & 10 deletions source/input/InputManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,6 @@ void InputManager::reInit() {
}
}

void InputManager::close() {
for (auto& el : analogueChannels) {
el.second.close();
}

for (auto& el : digitalChannels) {
el.second.close();
}
}

void InputManager::channelChanged(const int &id) {
entities::Player& player = game.getWorld()->getPlayer();

Expand Down

0 comments on commit 4d93f11

Please sign in to comment.