Skip to content

Commit

Permalink
Refactoring game_pad_stick_converter
Browse files Browse the repository at this point in the history
  • Loading branch information
tekezo committed Sep 25, 2023
1 parent 5991d96 commit a8e45fe
Showing 1 changed file with 56 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -246,21 +246,23 @@ class game_pad_stick_converter final : public pqrs::dispatcher::extra::dispatche
}
}

void emit_vertical_wheel_event(const device_id device_id) {
void emit_event(const device_id device_id,
std::function<std::chrono::milliseconds(const state&)> get_interval,
std::function<void(state&)> unset_active,
std::function<pointing_motion(const state&)> make_pointing_motion) {
auto it = states_.find(device_id);
if (it == std::end(states_)) {
return;
}

if (it->second.vertical_wheel_interval == std::chrono::milliseconds(0)) {
it->second.vertical_wheel_active = false;
auto interval = get_interval(it->second);

if (interval <= std::chrono::milliseconds(0)) {
unset_active(it->second);
return;
}

pointing_motion m(0,
0,
it->second.vertical_wheel,
0);
auto m = make_pointing_motion(it->second);

event_queue::event_time_stamp event_time_stamp(pqrs::osx::chrono::mach_absolute_time_point());
event_queue::event event(m);
Expand All @@ -273,47 +275,59 @@ class game_pad_stick_converter final : public pqrs::dispatcher::extra::dispatche
event_queue::state::original);

enqueue_to_dispatcher(
[this, device_id, entry] {
[this, device_id, get_interval, unset_active, make_pointing_motion, entry] {
pointing_motion_arrived(entry);

emit_vertical_wheel_event(device_id);
emit_event(device_id,
get_interval,
unset_active,
make_pointing_motion);
},
when_now() + it->second.vertical_wheel_interval);
when_now() + interval);
}

void emit_horizontal_wheel_event(const device_id device_id) {
auto it = states_.find(device_id);
if (it == std::end(states_)) {
return;
}

if (it->second.horizontal_wheel_interval <= std::chrono::milliseconds(0)) {
it->second.horizontal_wheel_active = false;
return;
}

pointing_motion m(0,
0,
0,
it->second.horizontal_wheel);

event_queue::event_time_stamp event_time_stamp(pqrs::osx::chrono::mach_absolute_time_point());
event_queue::event event(m);
event_queue::entry entry(device_id,
event_time_stamp,
event,
event_type::single,
event,
event_origin::grabbed_device,
event_queue::state::original);

enqueue_to_dispatcher(
[this, device_id, entry] {
pointing_motion_arrived(entry);
void emit_vertical_wheel_event(const device_id device_id) {
auto get_interval = [](const state& s) {
return s.vertical_wheel_interval;
};

auto unset_active = [](state& s) {
s.vertical_wheel_active = false;
};

auto make_pointing_motion = [](const state& s) {
return pointing_motion(0,
0,
s.vertical_wheel,
0);
};

emit_event(device_id,
get_interval,
unset_active,
make_pointing_motion);
}

emit_horizontal_wheel_event(device_id);
},
when_now() + it->second.horizontal_wheel_interval);
void emit_horizontal_wheel_event(const device_id device_id) {
auto get_interval = [](const state& s) {
return s.horizontal_wheel_interval;
};

auto unset_active = [](state& s) {
s.horizontal_wheel_active = false;
};

auto make_pointing_motion = [](const state& s) {
return pointing_motion(0,
0,
0,
s.horizontal_wheel);
};

emit_event(device_id,
get_interval,
unset_active,
make_pointing_motion);
}

pqrs::dispatcher::extra::timer xy_timer_;
Expand Down

0 comments on commit a8e45fe

Please sign in to comment.