Skip to content

Commit

Permalink
input: Fix error when selection callback is missing.
Browse files Browse the repository at this point in the history
  • Loading branch information
heinezen committed Nov 6, 2023
1 parent 580cdd0 commit 609d6f3
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 18 deletions.
2 changes: 1 addition & 1 deletion libopenage/gamestate/event/spawn_entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ void SpawnEntityHandler::invoke(openage::event::EventLoop & /* loop */,

// TODO: Select the unit when it's created
// very dumb but it gets the job done
auto select_cb = params.get("select_cb", std::function<void(entity_id_t id)>{});
auto select_cb = params.get("select_cb", std::function<void(entity_id_t id)>{[](entity_id_t /* id */) {}});
select_cb(entity->get_id());

gstate->add_game_entity(entity);
Expand Down
3 changes: 2 additions & 1 deletion libopenage/input/controller/game/binding.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#pragma once

#include <functional>
#include <memory>
#include <unordered_map>

#include "event/event.h"
Expand All @@ -14,7 +15,7 @@ class Controller;

using binding_flags_t = std::unordered_map<std::string, std::string>;
using binding_func_t = std::function<const std::shared_ptr<event::Event>(const event_arguments &,
const Controller &)>;
const std::shared_ptr<Controller>)>;


/**
Expand Down
16 changes: 8 additions & 8 deletions libopenage/input/controller/game/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ bool Controller::process(const event_arguments &ev_args, const std::shared_ptr<B

// TODO: check if action is allowed
auto bind = ctx->lookup(ev_args.e);
auto game_event = bind.transform(ev_args, *this);
auto controller = this->shared_from_this();
auto game_event = bind.transform(ev_args, controller);

switch (bind.action_type) {
case forward_action_t::SEND:
Expand Down Expand Up @@ -90,15 +91,14 @@ void setup_defaults(const std::shared_ptr<BindingContext> &ctx,
const std::shared_ptr<openage::gamestate::GameSimulation> &simulation,
const std::shared_ptr<renderer::camera::Camera> &camera) {
binding_func_t create_entity_event{[&](const event_arguments &args,
const Controller &controller) {
const std::shared_ptr<Controller> controller) {
auto mouse_pos = args.mouse.to_phys3(camera);
event::EventHandler::param_map::map_t params{
{"position", mouse_pos},
{"owner", controller.get_controlled()},
{"owner", controller->get_controlled()},
// TODO: Remove
{"select_cb", std::function<void(gamestate::entity_id_t id)>{[&controller](gamestate::entity_id_t id) {
auto &mut_controller = const_cast<Controller &>(controller);
mut_controller.set_selected({id});
{"select_cb", std::function<void(gamestate::entity_id_t id)>{[controller](gamestate::entity_id_t id) {
controller->set_selected({id});
}}},
};

Expand All @@ -117,12 +117,12 @@ void setup_defaults(const std::shared_ptr<BindingContext> &ctx,
ctx->bind(ev_mouse_lmb, create_entity_action);

binding_func_t move_entity{[&](const event_arguments &args,
const Controller &controller) {
const std::shared_ptr<Controller> controller) {
auto mouse_pos = args.mouse.to_phys3(camera);
event::EventHandler::param_map::map_t params{
{"type", gamestate::component::command::command_t::MOVE},
{"target", mouse_pos},
{"entity_ids", controller.get_selected()},
{"entity_ids", controller->get_selected()},
};

auto event = simulation->get_event_loop()->create_event(
Expand Down
3 changes: 2 additions & 1 deletion libopenage/input/controller/game/controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#pragma once

#include <memory>
#include <mutex>
#include <unordered_set>

Expand Down Expand Up @@ -32,7 +33,7 @@ class BindingContext;
*
* TODO: Connection to engine
*/
class Controller {
class Controller : public std::enable_shared_from_this<Controller> {
public:
Controller(const std::unordered_set<size_t> &controlled_factions,
size_t active_faction_id);
Expand Down
6 changes: 3 additions & 3 deletions libopenage/input/input_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ InputManager::InputManager() :
gui_input{nullptr} {
}

void InputManager::set_gui(const std::shared_ptr<qtgui::GuiInput> &gui_input) {
void InputManager::set_gui(const std::shared_ptr<qtgui::GuiInput> gui_input) {
this->gui_input = gui_input;
}

void InputManager::set_camera_controller(const std::shared_ptr<camera::Controller> &controller) {
void InputManager::set_camera_controller(const std::shared_ptr<camera::Controller> controller) {
this->camera_controller = controller;
}

void InputManager::set_engine_controller(const std::shared_ptr<game::Controller> &controller) {
void InputManager::set_engine_controller(const std::shared_ptr<game::Controller> controller) {
this->engine_controller = controller;
}

Expand Down
8 changes: 4 additions & 4 deletions libopenage/input/input_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Controller;

namespace game {
class Controller;
} // namespace engine
} // namespace game

class InputContext;

Expand All @@ -43,21 +43,21 @@ class InputManager {
*
* @param gui_input GUI input handler.
*/
void set_gui(const std::shared_ptr<qtgui::GuiInput> &gui_input);
void set_gui(const std::shared_ptr<qtgui::GuiInput> gui_input);

/**
* Set the controller for the camera.
*
* @param controller Camera controller.
*/
void set_camera_controller(const std::shared_ptr<camera::Controller> &controller);
void set_camera_controller(const std::shared_ptr<camera::Controller> controller);

/**
* Set the controller for the engine.
*
* @param controller Engine controller.
*/
void set_engine_controller(const std::shared_ptr<game::Controller> &controller);
void set_engine_controller(const std::shared_ptr<game::Controller> controller);

/**
* returns the global keybind context.
Expand Down

0 comments on commit 609d6f3

Please sign in to comment.