Skip to content

Commit

Permalink
Remove stick_history_entry
Browse files Browse the repository at this point in the history
  • Loading branch information
tekezo committed Aug 2, 2024
1 parent c86c60c commit b17cb78
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 272 deletions.
27 changes: 0 additions & 27 deletions src/apps/SettingsWindow/src/View/DevicesGamePadSettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,6 @@ struct DevicesGamePadSettingsView: View {
.gamePadXYStickContinuedMovementIntervalMilliseconds,
continuedMovementIntervalMillisecondsDefaultValue: Int(
libkrbn_core_configuration_game_pad_xy_stick_continued_movement_interval_milliseconds_default_value()
),

flickingInputWindowMilliseconds: $connectedDeviceSetting
.gamePadXYStickFlickingInputWindowMilliseconds,
flickingInputWindowMillisecondsDefaultValue: Int(
libkrbn_core_configuration_game_pad_xy_stick_flicking_input_window_milliseconds_default_value()
)
)

Expand Down Expand Up @@ -124,12 +118,6 @@ struct DevicesGamePadSettingsView: View {
.gamePadWheelsStickContinuedMovementIntervalMilliseconds,
continuedMovementIntervalMillisecondsDefaultValue: Int(
libkrbn_core_configuration_game_pad_wheels_stick_continued_movement_interval_milliseconds_default_value()
),

flickingInputWindowMilliseconds: $connectedDeviceSetting
.gamePadWheelsStickFlickingInputWindowMilliseconds,
flickingInputWindowMillisecondsDefaultValue: Int(
libkrbn_core_configuration_game_pad_wheels_stick_flicking_input_window_milliseconds_default_value()
)
)

Expand Down Expand Up @@ -183,9 +171,6 @@ struct DevicesGamePadSettingsView: View {
@Binding var continuedMovementIntervalMilliseconds: Int
let continuedMovementIntervalMillisecondsDefaultValue: Int

@Binding var flickingInputWindowMilliseconds: Int
let flickingInputWindowMillisecondsDefaultValue: Int

var body: some View {
Grid(alignment: .leadingFirstTextBaseline) {
GridRow {
Expand Down Expand Up @@ -215,18 +200,6 @@ struct DevicesGamePadSettingsView: View {

Text("(Default: \(continuedMovementIntervalMillisecondsDefaultValue))")
}

GridRow {
Text("Flicking input window milliseconds:")

IntTextField(
value: $flickingInputWindowMilliseconds,
range: 0...1000,
step: 1,
width: 60)

Text("(Default: \(flickingInputWindowMillisecondsDefaultValue))")
}
}
}
}
Expand Down
32 changes: 0 additions & 32 deletions src/apps/share/swift/LibKrbn/Models/ConnectedDeviceSetting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,6 @@ extension LibKrbn {
libkrbn_core_configuration_get_selected_profile_device_game_pad_xy_stick_continued_movement_interval_milliseconds(
connectedDevice.libkrbnDeviceIdentifiers))

gamePadXYStickFlickingInputWindowMilliseconds = Int(
libkrbn_core_configuration_get_selected_profile_device_game_pad_xy_stick_flicking_input_window_milliseconds(
connectedDevice.libkrbnDeviceIdentifiers))

gamePadWheelsStickContinuedMovementAbsoluteMagnitudeThreshold =
libkrbn_core_configuration_get_selected_profile_device_game_pad_wheels_stick_continued_movement_absolute_magnitude_threshold(
connectedDevice.libkrbnDeviceIdentifiers)
Expand All @@ -80,10 +76,6 @@ extension LibKrbn {
libkrbn_core_configuration_get_selected_profile_device_game_pad_wheels_stick_continued_movement_interval_milliseconds(
connectedDevice.libkrbnDeviceIdentifiers))

gamePadWheelsStickFlickingInputWindowMilliseconds = Int(
libkrbn_core_configuration_get_selected_profile_device_game_pad_wheels_stick_flicking_input_window_milliseconds(
connectedDevice.libkrbnDeviceIdentifiers))

var buffer = [Int8](repeating: 0, count: 16384)

if libkrbn_core_configuration_get_selected_profile_device_game_pad_stick_x_formula(
Expand Down Expand Up @@ -264,18 +256,6 @@ extension LibKrbn {
}
}

@Published var gamePadXYStickFlickingInputWindowMilliseconds: Int = 0 {
didSet {
if didSetEnabled {
libkrbn_core_configuration_set_selected_profile_device_game_pad_xy_stick_flicking_input_window_milliseconds(
connectedDevice.libkrbnDeviceIdentifiers,
Int32(gamePadXYStickFlickingInputWindowMilliseconds))

Settings.shared.save()
}
}
}

@Published var gamePadWheelsStickContinuedMovementAbsoluteMagnitudeThreshold: Double = 0.0 {
didSet {
if didSetEnabled {
Expand All @@ -300,18 +280,6 @@ extension LibKrbn {
}
}

@Published var gamePadWheelsStickFlickingInputWindowMilliseconds: Int = 0 {
didSet {
if didSetEnabled {
libkrbn_core_configuration_set_selected_profile_device_game_pad_wheels_stick_flicking_input_window_milliseconds(
connectedDevice.libkrbnDeviceIdentifiers,
Int32(gamePadWheelsStickFlickingInputWindowMilliseconds))

Settings.shared.save()
}
}
}

@Published var gamePadStickXFormula = "" {
didSet {
if didSetEnabled {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,38 +77,6 @@ class game_pad_stick_converter final : public pqrs::dispatcher::extra::dispatche

nod::signal<void(void)> values_updated;

//
// Classes
//

class stick_history_entry final {
public:
stick_history_entry(absolute_time_point time,
double horizontal_stick_sensor_value,
double vertical_stick_sensor_value)
: time_(time),
horizontal_stick_sensor_value_(horizontal_stick_sensor_value),
vertical_stick_sensor_value_(vertical_stick_sensor_value) {
}

absolute_time_point get_time(void) const {
return time_;
}

double get_horizontal_stick_sensor_value(void) const {
return horizontal_stick_sensor_value_;
}

double get_vertical_stick_sensor_value(void) const {
return vertical_stick_sensor_value_;
}

private:
absolute_time_point time_;
double horizontal_stick_sensor_value_;
double vertical_stick_sensor_value_;
};

//
// Methods
//
Expand All @@ -118,10 +86,9 @@ class game_pad_stick_converter final : public pqrs::dispatcher::extra::dispatche
radian_(0.0),
absolute_magnitude_(0.0),
delta_magnitude_(0.0),
continued_movement_magnitude_(0.0),
previous_absolute_magnitude_(0.0),
continued_movement_absolute_magnitude_threshold_(1.0),
flicking_input_window_milliseconds_(0) {
continued_movement_interval_milliseconds_(0) {
}

~stick(void) {
Expand All @@ -148,10 +115,6 @@ class game_pad_stick_converter final : public pqrs::dispatcher::extra::dispatche
continued_movement_interval_milliseconds_ = value;
}

void set_flicking_input_window_milliseconds(int value) {
flicking_input_window_milliseconds_ = value;
}

// This method should be called in the shared dispatcher thread.
void update_horizontal_stick_sensor_value(CFIndex logical_max,
CFIndex logical_min,
Expand All @@ -175,18 +138,16 @@ class game_pad_stick_converter final : public pqrs::dispatcher::extra::dispatche
}

std::chrono::milliseconds get_continued_movement_interval_milliseconds(void) const {
if (continued_movement_magnitude_ == 0.0) {
return std::chrono::milliseconds(0);
if (continued_movement()) {
return std::chrono::milliseconds(continued_movement_interval_milliseconds_);
}

return std::chrono::milliseconds(continued_movement_interval_milliseconds_);
return std::chrono::milliseconds(0);
}

private:
// This method is executed in the shared dispatcher thread.
void update_values(void) {
auto now = pqrs::osx::chrono::mach_absolute_time_point();

radian_ = std::atan2(vertical_stick_sensor_.get_value(),
horizontal_stick_sensor_.get_value());
// When the stick is tilted diagonally, the distance from the centre may exceed 1.0 (e.g. 1.2).
Expand All @@ -197,44 +158,14 @@ class game_pad_stick_converter final : public pqrs::dispatcher::extra::dispatche

auto dm = std::max(0.0, absolute_magnitude_ - previous_absolute_magnitude_);

//
// Update stick_history_
//

stick_history_.push_back(stick_history_entry(now,
horizontal_stick_sensor_.get_value(),
vertical_stick_sensor_.get_value()));
stick_history_.erase(std::remove_if(std::begin(stick_history_),
std::end(stick_history_),
[this, now](auto&& e) {
return pqrs::osx::chrono::make_milliseconds(now - e.get_time()) > std::chrono::milliseconds(flicking_input_window_milliseconds_);
}),
std::end(stick_history_));

//
// Update delta_magnitude_
//

if (absolute_magnitude_ >= continued_movement_absolute_magnitude_threshold_) {
if (continued_movement_magnitude_ == 0.0) {
double max = 0;
for (int i = 0; i < stick_history_.size(); ++i) {
for (int j = i + 1; j < stick_history_.size(); ++j) {
auto h = stick_history_[i].get_horizontal_stick_sensor_value() - stick_history_[j].get_horizontal_stick_sensor_value();
auto v = stick_history_[i].get_vertical_stick_sensor_value() - stick_history_[j].get_vertical_stick_sensor_value();
auto d = std::sqrt(h * h + v * v);
if (max < d) {
max = d;
}
}
}
continued_movement_magnitude_ = std::min(max, 1.0);
}
if (continued_movement()) {
delta_magnitude_ = absolute_magnitude_;

delta_magnitude_ = continued_movement_magnitude_;
} else {
continued_movement_magnitude_ = 0.0;

// Ignore minor magnitude changes until a sufficient amount of change accumulates.
auto delta_magnitude_threshold = 0.01;
if (0 < delta_magnitude_ && delta_magnitude_ < delta_magnitude_threshold) {
Expand All @@ -252,23 +183,24 @@ class game_pad_stick_converter final : public pqrs::dispatcher::extra::dispatche
values_updated();
}

bool continued_movement(void) const {
return absolute_magnitude_ >= continued_movement_absolute_magnitude_threshold_;
}

stick_sensor horizontal_stick_sensor_;
stick_sensor vertical_stick_sensor_;

double radian_;
double absolute_magnitude_;
double delta_magnitude_;
double continued_movement_magnitude_;
double previous_absolute_magnitude_;
std::deque<stick_history_entry> stick_history_;

//
// configurations
//

double continued_movement_absolute_magnitude_threshold_;
int continued_movement_interval_milliseconds_;
int flicking_input_window_milliseconds_;
};

class event_value final {
Expand Down Expand Up @@ -461,11 +393,9 @@ class game_pad_stick_converter final : public pqrs::dispatcher::extra::dispatche

xy_.set_continued_movement_absolute_magnitude_threshold(d->get_game_pad_xy_stick_continued_movement_absolute_magnitude_threshold());
xy_.set_continued_movement_interval_milliseconds(d->get_game_pad_xy_stick_continued_movement_interval_milliseconds());
xy_.set_flicking_input_window_milliseconds(d->get_game_pad_xy_stick_flicking_input_window_milliseconds());

wheels_.set_continued_movement_absolute_magnitude_threshold(d->get_game_pad_wheels_stick_continued_movement_absolute_magnitude_threshold());
wheels_.set_continued_movement_interval_milliseconds(d->get_game_pad_wheels_stick_continued_movement_interval_milliseconds());
wheels_.set_flicking_input_window_milliseconds(d->get_game_pad_wheels_stick_flicking_input_window_milliseconds());

x_formula_string_ = d->get_game_pad_stick_x_formula();
y_formula_string_ = d->get_game_pad_stick_y_formula();
Expand Down Expand Up @@ -537,29 +467,29 @@ class game_pad_stick_converter final : public pqrs::dispatcher::extra::dispatche
void update_continued_movement_timer(continued_movement_mode mode,
std::chrono::milliseconds interval) {
xy_radian_ = xy_.get_radian();
if (wheels_.get_delta_magnitude() == 0.0) {
xy_delta_magnitude_ = xy_.get_delta_magnitude();
xy_absolute_magnitude_ = xy_.get_absolute_magnitude();
} else {
xy_delta_magnitude_ = xy_.get_delta_magnitude() + wheels_.get_delta_magnitude();
xy_absolute_magnitude_ = xy_.get_absolute_magnitude() + wheels_.get_absolute_magnitude();
xy_delta_magnitude_ = xy_.get_delta_magnitude();
xy_absolute_magnitude_ = xy_.get_absolute_magnitude();
if (continued_movement_mode_ == continued_movement_mode::xy) {
// Add secondary stick absolute magnitude to magnitudes;
auto m = wheels_.get_absolute_magnitude();
xy_delta_magnitude_ += m;
xy_absolute_magnitude_ += m;
}

wheels_radian_ = wheels_.get_radian();
if (xy_.get_delta_magnitude() == 0.0) {
wheels_delta_magnitude_ = wheels_.get_delta_magnitude();
wheels_absolute_magnitude_ = wheels_.get_absolute_magnitude();
} else {
wheels_delta_magnitude_ = wheels_.get_delta_magnitude() + xy_.get_delta_magnitude();
wheels_absolute_magnitude_ = wheels_.get_absolute_magnitude() + xy_.get_absolute_magnitude();
wheels_delta_magnitude_ = wheels_.get_delta_magnitude();
wheels_absolute_magnitude_ = wheels_.get_absolute_magnitude();
if (continued_movement_mode_ == continued_movement_mode::wheels) {
auto m = xy_.get_absolute_magnitude();
wheels_delta_magnitude_ += m;
wheels_absolute_magnitude_ += m;
}

auto [x, y] = xy_hid_values();
x_value_.set_value(x);
y_value_.set_value(y);

auto [h, v] = wheels_hid_values();

horizontal_wheel_value_.set_value(h);
vertical_wheel_value_.set_value(v);

Expand Down
14 changes: 0 additions & 14 deletions src/lib/libkrbn/include/libkrbn/libkrbn.h
Original file line number Diff line number Diff line change
Expand Up @@ -253,13 +253,6 @@ void libkrbn_core_configuration_set_selected_profile_device_game_pad_xy_stick_co
int value);
int libkrbn_core_configuration_game_pad_xy_stick_continued_movement_interval_milliseconds_default_value(void);

// game_pad_xy_stick_flicking_input_window_milliseconds

int libkrbn_core_configuration_get_selected_profile_device_game_pad_xy_stick_flicking_input_window_milliseconds(const libkrbn_device_identifiers* device_identifiers);
void libkrbn_core_configuration_set_selected_profile_device_game_pad_xy_stick_flicking_input_window_milliseconds(const libkrbn_device_identifiers* device_identifiers,
int value);
int libkrbn_core_configuration_game_pad_xy_stick_flicking_input_window_milliseconds_default_value(void);

// game_pad_wheels_stick_continued_movement_absolute_magnitude_threshold

double libkrbn_core_configuration_get_selected_profile_device_game_pad_wheels_stick_continued_movement_absolute_magnitude_threshold(const libkrbn_device_identifiers* device_identifiers);
Expand All @@ -274,13 +267,6 @@ void libkrbn_core_configuration_set_selected_profile_device_game_pad_wheels_stic
int value);
int libkrbn_core_configuration_game_pad_wheels_stick_continued_movement_interval_milliseconds_default_value(void);

// game_pad_wheels_stick_flicking_input_window_milliseconds

int libkrbn_core_configuration_get_selected_profile_device_game_pad_wheels_stick_flicking_input_window_milliseconds(const libkrbn_device_identifiers* device_identifiers);
void libkrbn_core_configuration_set_selected_profile_device_game_pad_wheels_stick_flicking_input_window_milliseconds(const libkrbn_device_identifiers* device_identifiers,
int value);
int libkrbn_core_configuration_game_pad_wheels_stick_flicking_input_window_milliseconds_default_value(void);

// game_pad_stick_x_formula

bool libkrbn_core_configuration_get_selected_profile_device_game_pad_stick_x_formula(const libkrbn_device_identifiers* device_identifiers,
Expand Down
Loading

0 comments on commit b17cb78

Please sign in to comment.