/* * Copyright (C) 2021 Christopher J. Howard * * This file is part of Antkeeper source code. * * Antkeeper source code is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Antkeeper source code is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Antkeeper source code. If not, see . */ #include "listener.hpp" #include "event/event-dispatcher.hpp" namespace input { listener::listener(): event_dispatcher(nullptr), callback(nullptr), enabled(false) {} listener::~listener() { set_event_dispatcher(nullptr); } void listener::set_event_dispatcher(::event_dispatcher* event_dispatcher) { if (this->event_dispatcher) { this->event_dispatcher->unsubscribe(this); this->event_dispatcher->unsubscribe(this); this->event_dispatcher->unsubscribe(this); this->event_dispatcher->unsubscribe(this); this->event_dispatcher->unsubscribe(this); this->event_dispatcher->unsubscribe(this); } this->event_dispatcher = event_dispatcher; if (event_dispatcher) { event_dispatcher->subscribe(this); event_dispatcher->subscribe(this); event_dispatcher->subscribe(this); event_dispatcher->subscribe(this); event_dispatcher->subscribe(this); event_dispatcher->subscribe(this); } } void listener::set_callback(std::function callback) { this->callback = callback; } void listener::set_enabled(bool enabled) { this->enabled = enabled; } void listener::handle_event(const key_pressed_event& event) { if (!is_enabled() || !callback) { return; } callback(event); } void listener::handle_event(const mouse_moved_event& event) { if (!is_enabled() || !callback) { return; } callback(event); } void listener::handle_event(const mouse_button_pressed_event& event) { if (!is_enabled() || !callback) { return; } callback(event); } void listener::handle_event(const mouse_wheel_scrolled_event& event) { if (!is_enabled() || !callback) { return; } callback(event); } void listener::handle_event(const game_controller_button_pressed_event& event) { if (!is_enabled() || !callback) { return; } callback(event); } void listener::handle_event(const game_controller_axis_moved_event& event) { if (!is_enabled() || !callback) { return; } callback(event); } } // namespace input