mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-07-24 14:49:42 -06:00
Merge pull request #7663 from jordan-woyak/expression-parser-improve
Expression parser improvements
This commit is contained in:
@ -45,6 +45,8 @@ add_library(inputcommon
|
||||
ControlReference/ControlReference.h
|
||||
ControlReference/ExpressionParser.cpp
|
||||
ControlReference/ExpressionParser.h
|
||||
ControlReference/FunctionExpression.cpp
|
||||
ControlReference/FunctionExpression.h
|
||||
)
|
||||
|
||||
target_link_libraries(inputcommon PUBLIC
|
||||
|
@ -25,12 +25,12 @@ bool ControlReference::InputGateOn()
|
||||
// Updates a controlreference's binded devices/controls
|
||||
// need to call this to re-bind a control reference after changing its expression
|
||||
//
|
||||
void ControlReference::UpdateReference(const ciface::Core::DeviceContainer& devices,
|
||||
const ciface::Core::DeviceQualifier& default_device)
|
||||
void ControlReference::UpdateReference(ciface::ExpressionParser::ControlEnvironment& env)
|
||||
{
|
||||
ControlFinder finder(devices, default_device, IsInput());
|
||||
if (m_parsed_expression)
|
||||
m_parsed_expression->UpdateReferences(finder);
|
||||
{
|
||||
m_parsed_expression->UpdateReferences(env);
|
||||
}
|
||||
}
|
||||
|
||||
int ControlReference::BoundCount() const
|
||||
@ -54,7 +54,9 @@ std::string ControlReference::GetExpression() const
|
||||
void ControlReference::SetExpression(std::string expr)
|
||||
{
|
||||
m_expression = std::move(expr);
|
||||
std::tie(m_parse_status, m_parsed_expression) = ParseExpression(m_expression);
|
||||
auto parse_result = ParseExpression(m_expression);
|
||||
m_parse_status = parse_result.status;
|
||||
m_parsed_expression = std::move(parse_result.expr);
|
||||
}
|
||||
|
||||
ControlReference::ControlReference() : range(1), m_parsed_expression(nullptr)
|
||||
|
@ -30,8 +30,7 @@ public:
|
||||
|
||||
int BoundCount() const;
|
||||
ciface::ExpressionParser::ParseStatus GetParseStatus() const;
|
||||
void UpdateReference(const ciface::Core::DeviceContainer& devices,
|
||||
const ciface::Core::DeviceQualifier& default_device);
|
||||
void UpdateReference(ciface::ExpressionParser::ControlEnvironment& env);
|
||||
std::string GetExpression() const;
|
||||
void SetExpression(std::string expr);
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -4,56 +4,58 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "InputCommon/ControllerInterface/Device.h"
|
||||
|
||||
namespace ciface::ExpressionParser
|
||||
{
|
||||
class ControlQualifier
|
||||
enum TokenType
|
||||
{
|
||||
public:
|
||||
bool has_device;
|
||||
Core::DeviceQualifier device_qualifier;
|
||||
std::string control_name;
|
||||
|
||||
ControlQualifier() : has_device(false) {}
|
||||
operator std::string() const
|
||||
{
|
||||
if (has_device)
|
||||
return device_qualifier.ToString() + ":" + control_name;
|
||||
else
|
||||
return control_name;
|
||||
}
|
||||
TOK_WHITESPACE,
|
||||
TOK_INVALID,
|
||||
TOK_EOF,
|
||||
TOK_LPAREN,
|
||||
TOK_RPAREN,
|
||||
TOK_NOT,
|
||||
TOK_CONTROL,
|
||||
TOK_LITERAL,
|
||||
TOK_VARIABLE,
|
||||
TOK_BAREWORD,
|
||||
TOK_COMMENT,
|
||||
// Binary Ops:
|
||||
TOK_BINARY_OPS_BEGIN,
|
||||
TOK_AND = TOK_BINARY_OPS_BEGIN,
|
||||
TOK_OR,
|
||||
TOK_ADD,
|
||||
TOK_SUB,
|
||||
TOK_MUL,
|
||||
TOK_DIV,
|
||||
TOK_MOD,
|
||||
TOK_ASSIGN,
|
||||
TOK_LTHAN,
|
||||
TOK_GTHAN,
|
||||
TOK_COMMA,
|
||||
TOK_BINARY_OPS_END,
|
||||
};
|
||||
|
||||
class ControlFinder
|
||||
class Token
|
||||
{
|
||||
public:
|
||||
ControlFinder(const Core::DeviceContainer& container_, const Core::DeviceQualifier& default_,
|
||||
const bool is_input_)
|
||||
: container(container_), default_device(default_), is_input(is_input_)
|
||||
{
|
||||
}
|
||||
std::shared_ptr<Core::Device> FindDevice(ControlQualifier qualifier) const;
|
||||
Core::Device::Control* FindControl(ControlQualifier qualifier) const;
|
||||
TokenType type;
|
||||
std::string data;
|
||||
|
||||
private:
|
||||
const Core::DeviceContainer& container;
|
||||
const Core::DeviceQualifier& default_device;
|
||||
bool is_input;
|
||||
};
|
||||
// Position in the input string:
|
||||
std::size_t string_position = 0;
|
||||
std::size_t string_length = 0;
|
||||
|
||||
class Expression
|
||||
{
|
||||
public:
|
||||
virtual ~Expression() = default;
|
||||
virtual ControlState GetValue() const = 0;
|
||||
virtual void SetValue(ControlState state) = 0;
|
||||
virtual int CountNumControls() const = 0;
|
||||
virtual void UpdateReferences(ControlFinder& finder) = 0;
|
||||
virtual operator std::string() const = 0;
|
||||
explicit Token(TokenType type_);
|
||||
Token(TokenType type_, std::string data_);
|
||||
|
||||
bool IsBinaryOperator() const;
|
||||
};
|
||||
|
||||
enum class ParseStatus
|
||||
@ -63,5 +65,129 @@ enum class ParseStatus
|
||||
EmptyExpression,
|
||||
};
|
||||
|
||||
std::pair<ParseStatus, std::unique_ptr<Expression>> ParseExpression(const std::string& expr);
|
||||
class Lexer
|
||||
{
|
||||
public:
|
||||
std::string expr;
|
||||
std::string::iterator it;
|
||||
|
||||
explicit Lexer(std::string expr_);
|
||||
|
||||
ParseStatus Tokenize(std::vector<Token>& tokens);
|
||||
|
||||
private:
|
||||
template <typename F>
|
||||
std::string FetchCharsWhile(F&& func)
|
||||
{
|
||||
std::string value;
|
||||
while (it != expr.end() && func(*it))
|
||||
{
|
||||
value += *it;
|
||||
++it;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
std::string FetchDelimString(char delim);
|
||||
std::string FetchWordChars();
|
||||
Token GetDelimitedLiteral();
|
||||
Token GetVariable();
|
||||
Token GetFullyQualifiedControl();
|
||||
Token GetBareword(char c);
|
||||
Token GetRealLiteral(char c);
|
||||
|
||||
Token PeekToken();
|
||||
Token NextToken();
|
||||
};
|
||||
|
||||
class ControlQualifier
|
||||
{
|
||||
public:
|
||||
bool has_device;
|
||||
Core::DeviceQualifier device_qualifier;
|
||||
std::string control_name;
|
||||
|
||||
ControlQualifier() : has_device(false) {}
|
||||
|
||||
operator std::string() const
|
||||
{
|
||||
if (has_device)
|
||||
return device_qualifier.ToString() + ":" + control_name;
|
||||
else
|
||||
return control_name;
|
||||
}
|
||||
|
||||
void FromString(const std::string& str)
|
||||
{
|
||||
const auto col_pos = str.find_last_of(':');
|
||||
|
||||
has_device = (str.npos != col_pos);
|
||||
if (has_device)
|
||||
{
|
||||
device_qualifier.FromString(str.substr(0, col_pos));
|
||||
control_name = str.substr(col_pos + 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
device_qualifier.FromString("");
|
||||
control_name = str;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
class ControlEnvironment
|
||||
{
|
||||
public:
|
||||
using VariableContainer = std::map<std::string, ControlState>;
|
||||
|
||||
ControlEnvironment(const Core::DeviceContainer& container_, const Core::DeviceQualifier& default_,
|
||||
VariableContainer& vars)
|
||||
: m_variables(vars), container(container_), default_device(default_)
|
||||
{
|
||||
}
|
||||
|
||||
std::shared_ptr<Core::Device> FindDevice(ControlQualifier qualifier) const;
|
||||
Core::Device::Input* FindInput(ControlQualifier qualifier) const;
|
||||
Core::Device::Output* FindOutput(ControlQualifier qualifier) const;
|
||||
ControlState* GetVariablePtr(const std::string& name);
|
||||
|
||||
private:
|
||||
VariableContainer& m_variables;
|
||||
const Core::DeviceContainer& container;
|
||||
const Core::DeviceQualifier& default_device;
|
||||
};
|
||||
|
||||
class Expression
|
||||
{
|
||||
public:
|
||||
virtual ~Expression() = default;
|
||||
virtual ControlState GetValue() const = 0;
|
||||
virtual void SetValue(ControlState state) = 0;
|
||||
virtual int CountNumControls() const = 0;
|
||||
virtual void UpdateReferences(ControlEnvironment& finder) = 0;
|
||||
};
|
||||
|
||||
class ParseResult
|
||||
{
|
||||
public:
|
||||
static ParseResult MakeEmptyResult();
|
||||
static ParseResult MakeSuccessfulResult(std::unique_ptr<Expression>&& expr);
|
||||
static ParseResult MakeErrorResult(Token token, std::string description);
|
||||
|
||||
ParseStatus status;
|
||||
std::unique_ptr<Expression> expr;
|
||||
|
||||
// Used for parse errors:
|
||||
// TODO: This should probably be moved elsewhere:
|
||||
std::optional<Token> token;
|
||||
std::optional<std::string> description;
|
||||
|
||||
private:
|
||||
ParseResult() = default;
|
||||
};
|
||||
|
||||
ParseResult ParseExpression(const std::string& expr);
|
||||
ParseResult ParseTokens(const std::vector<Token>& tokens);
|
||||
void RemoveInertTokens(std::vector<Token>* tokens);
|
||||
|
||||
} // namespace ciface::ExpressionParser
|
||||
|
526
Source/Core/InputCommon/ControlReference/FunctionExpression.cpp
Normal file
526
Source/Core/InputCommon/ControlReference/FunctionExpression.cpp
Normal file
@ -0,0 +1,526 @@
|
||||
// Copyright 2019 Dolphin Emulator Project
|
||||
// Licensed under GPLv2+
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#include <chrono>
|
||||
#include <cmath>
|
||||
|
||||
#include "InputCommon/ControlReference/FunctionExpression.h"
|
||||
|
||||
namespace ciface
|
||||
{
|
||||
namespace ExpressionParser
|
||||
{
|
||||
constexpr int LOOP_MAX_REPS = 10000;
|
||||
constexpr ControlState CONDITION_THRESHOLD = 0.5;
|
||||
|
||||
using Clock = std::chrono::steady_clock;
|
||||
using FSec = std::chrono::duration<ControlState>;
|
||||
|
||||
// usage: toggle(toggle_state_input, [clear_state_input])
|
||||
class ToggleExpression : public FunctionExpression
|
||||
{
|
||||
private:
|
||||
ArgumentValidation
|
||||
ValidateArguments(const std::vector<std::unique_ptr<Expression>>& args) override
|
||||
{
|
||||
// Optional 2nd argument for clearing state:
|
||||
if (1 == args.size() || 2 == args.size())
|
||||
return ArgumentsAreValid{};
|
||||
else
|
||||
return ExpectedArguments{"toggle_state_input, [clear_state_input]"};
|
||||
}
|
||||
|
||||
ControlState GetValue() const override
|
||||
{
|
||||
const ControlState inner_value = GetArg(0).GetValue();
|
||||
|
||||
if (inner_value < CONDITION_THRESHOLD)
|
||||
{
|
||||
m_released = true;
|
||||
}
|
||||
else if (m_released && inner_value > CONDITION_THRESHOLD)
|
||||
{
|
||||
m_released = false;
|
||||
m_state ^= true;
|
||||
}
|
||||
|
||||
if (2 == GetArgCount() && GetArg(1).GetValue() > CONDITION_THRESHOLD)
|
||||
{
|
||||
m_state = false;
|
||||
}
|
||||
|
||||
return m_state;
|
||||
}
|
||||
|
||||
mutable bool m_released{};
|
||||
mutable bool m_state{};
|
||||
};
|
||||
|
||||
// usage: not(expression)
|
||||
class NotExpression : public FunctionExpression
|
||||
{
|
||||
private:
|
||||
ArgumentValidation
|
||||
ValidateArguments(const std::vector<std::unique_ptr<Expression>>& args) override
|
||||
{
|
||||
if (1 == args.size())
|
||||
return ArgumentsAreValid{};
|
||||
else
|
||||
return ExpectedArguments{"expression"};
|
||||
}
|
||||
|
||||
ControlState GetValue() const override { return 1.0 - GetArg(0).GetValue(); }
|
||||
void SetValue(ControlState value) override { GetArg(0).SetValue(1.0 - value); }
|
||||
};
|
||||
|
||||
// usage: sin(expression)
|
||||
class SinExpression : public FunctionExpression
|
||||
{
|
||||
private:
|
||||
ArgumentValidation
|
||||
ValidateArguments(const std::vector<std::unique_ptr<Expression>>& args) override
|
||||
{
|
||||
if (1 == args.size())
|
||||
return ArgumentsAreValid{};
|
||||
else
|
||||
return ExpectedArguments{"expression"};
|
||||
}
|
||||
|
||||
ControlState GetValue() const override { return std::sin(GetArg(0).GetValue()); }
|
||||
};
|
||||
|
||||
// usage: timer(seconds)
|
||||
class TimerExpression : public FunctionExpression
|
||||
{
|
||||
private:
|
||||
ArgumentValidation
|
||||
ValidateArguments(const std::vector<std::unique_ptr<Expression>>& args) override
|
||||
{
|
||||
if (1 == args.size())
|
||||
return ArgumentsAreValid{};
|
||||
else
|
||||
return ExpectedArguments{"seconds"};
|
||||
}
|
||||
|
||||
ControlState GetValue() const override
|
||||
{
|
||||
const auto now = Clock::now();
|
||||
const auto elapsed = now - m_start_time;
|
||||
|
||||
const ControlState val = GetArg(0).GetValue();
|
||||
|
||||
ControlState progress = std::chrono::duration_cast<FSec>(elapsed).count() / val;
|
||||
|
||||
if (std::isinf(progress) || progress < 0.0)
|
||||
{
|
||||
// User configured a non-positive timer. Reset the timer and return 0.0.
|
||||
progress = 0.0;
|
||||
m_start_time = now;
|
||||
}
|
||||
else if (progress >= 1.0)
|
||||
{
|
||||
const ControlState reset_count = std::floor(progress);
|
||||
|
||||
m_start_time += std::chrono::duration_cast<Clock::duration>(FSec(val * reset_count));
|
||||
progress -= reset_count;
|
||||
}
|
||||
|
||||
return progress;
|
||||
}
|
||||
|
||||
private:
|
||||
mutable Clock::time_point m_start_time = Clock::now();
|
||||
};
|
||||
|
||||
// usage: if(condition, true_expression, false_expression)
|
||||
class IfExpression : public FunctionExpression
|
||||
{
|
||||
private:
|
||||
ArgumentValidation
|
||||
ValidateArguments(const std::vector<std::unique_ptr<Expression>>& args) override
|
||||
{
|
||||
if (3 == args.size())
|
||||
return ArgumentsAreValid{};
|
||||
else
|
||||
return ExpectedArguments{"condition, true_expression, false_expression"};
|
||||
}
|
||||
|
||||
ControlState GetValue() const override
|
||||
{
|
||||
return (GetArg(0).GetValue() > CONDITION_THRESHOLD) ? GetArg(1).GetValue() :
|
||||
GetArg(2).GetValue();
|
||||
}
|
||||
};
|
||||
|
||||
// usage: minus(expression)
|
||||
class UnaryMinusExpression : public FunctionExpression
|
||||
{
|
||||
private:
|
||||
ArgumentValidation
|
||||
ValidateArguments(const std::vector<std::unique_ptr<Expression>>& args) override
|
||||
{
|
||||
if (1 == args.size())
|
||||
return ArgumentsAreValid{};
|
||||
else
|
||||
return ExpectedArguments{"expression"};
|
||||
}
|
||||
|
||||
ControlState GetValue() const override
|
||||
{
|
||||
// Subtraction for clarity:
|
||||
return 0.0 - GetArg(0).GetValue();
|
||||
}
|
||||
};
|
||||
|
||||
// usage: deadzone(input, amount)
|
||||
class DeadzoneExpression : public FunctionExpression
|
||||
{
|
||||
ArgumentValidation
|
||||
ValidateArguments(const std::vector<std::unique_ptr<Expression>>& args) override
|
||||
{
|
||||
if (2 == args.size())
|
||||
return ArgumentsAreValid{};
|
||||
else
|
||||
return ExpectedArguments{"input, amount"};
|
||||
}
|
||||
|
||||
ControlState GetValue() const override
|
||||
{
|
||||
const ControlState val = GetArg(0).GetValue();
|
||||
const ControlState deadzone = GetArg(1).GetValue();
|
||||
return std::copysign(std::max(0.0, std::abs(val) - deadzone) / (1.0 - deadzone), val);
|
||||
}
|
||||
};
|
||||
|
||||
// usage: smooth(input, seconds_up, seconds_down = seconds_up)
|
||||
// seconds is seconds to change from 0.0 to 1.0
|
||||
class SmoothExpression : public FunctionExpression
|
||||
{
|
||||
ArgumentValidation
|
||||
ValidateArguments(const std::vector<std::unique_ptr<Expression>>& args) override
|
||||
{
|
||||
if (2 == args.size() || 3 == args.size())
|
||||
return ArgumentsAreValid{};
|
||||
else
|
||||
return ExpectedArguments{"input, seconds_up, seconds_down = seconds_up"};
|
||||
}
|
||||
|
||||
ControlState GetValue() const override
|
||||
{
|
||||
const auto now = Clock::now();
|
||||
const auto elapsed = now - m_last_update;
|
||||
m_last_update = now;
|
||||
|
||||
const ControlState desired_value = GetArg(0).GetValue();
|
||||
|
||||
const ControlState smooth_up = GetArg(1).GetValue();
|
||||
const ControlState smooth_down = (3 == GetArgCount() ? GetArg(2).GetValue() : smooth_up);
|
||||
|
||||
const ControlState smooth = (desired_value < m_value) ? smooth_down : smooth_up;
|
||||
const ControlState max_move = std::chrono::duration_cast<FSec>(elapsed).count() / smooth;
|
||||
|
||||
if (std::isinf(max_move))
|
||||
{
|
||||
m_value = desired_value;
|
||||
}
|
||||
else
|
||||
{
|
||||
const ControlState diff = desired_value - m_value;
|
||||
m_value += std::copysign(std::min(max_move, std::abs(diff)), diff);
|
||||
}
|
||||
|
||||
return m_value;
|
||||
}
|
||||
|
||||
private:
|
||||
mutable ControlState m_value = 0.0;
|
||||
mutable Clock::time_point m_last_update = Clock::now();
|
||||
};
|
||||
|
||||
// usage: hold(input, seconds)
|
||||
class HoldExpression : public FunctionExpression
|
||||
{
|
||||
ArgumentValidation
|
||||
ValidateArguments(const std::vector<std::unique_ptr<Expression>>& args) override
|
||||
{
|
||||
if (2 == args.size())
|
||||
return ArgumentsAreValid{};
|
||||
else
|
||||
return ExpectedArguments{"input, seconds"};
|
||||
}
|
||||
|
||||
ControlState GetValue() const override
|
||||
{
|
||||
const auto now = Clock::now();
|
||||
|
||||
const ControlState input = GetArg(0).GetValue();
|
||||
|
||||
if (input < CONDITION_THRESHOLD)
|
||||
{
|
||||
m_state = false;
|
||||
m_start_time = Clock::now();
|
||||
}
|
||||
else if (!m_state)
|
||||
{
|
||||
const auto hold_time = now - m_start_time;
|
||||
|
||||
if (std::chrono::duration_cast<FSec>(hold_time).count() >= GetArg(1).GetValue())
|
||||
m_state = true;
|
||||
}
|
||||
|
||||
return m_state;
|
||||
}
|
||||
|
||||
private:
|
||||
mutable bool m_state = false;
|
||||
mutable Clock::time_point m_start_time = Clock::now();
|
||||
};
|
||||
|
||||
// usage: tap(input, seconds, taps=2)
|
||||
class TapExpression : public FunctionExpression
|
||||
{
|
||||
ArgumentValidation
|
||||
ValidateArguments(const std::vector<std::unique_ptr<Expression>>& args) override
|
||||
{
|
||||
if (2 == args.size() || 3 == args.size())
|
||||
return ArgumentsAreValid{};
|
||||
else
|
||||
return ExpectedArguments{"input, seconds, taps = 2"};
|
||||
}
|
||||
|
||||
ControlState GetValue() const override
|
||||
{
|
||||
const auto now = Clock::now();
|
||||
|
||||
const auto elapsed = std::chrono::duration_cast<FSec>(now - m_start_time).count();
|
||||
|
||||
const ControlState input = GetArg(0).GetValue();
|
||||
const ControlState seconds = GetArg(1).GetValue();
|
||||
|
||||
const bool is_time_up = elapsed > seconds;
|
||||
|
||||
const u32 desired_taps = (3 == GetArgCount()) ? u32(GetArg(2).GetValue() + 0.5) : 2;
|
||||
|
||||
if (input < CONDITION_THRESHOLD)
|
||||
{
|
||||
m_released = true;
|
||||
|
||||
if (m_taps > 0 && is_time_up)
|
||||
{
|
||||
m_taps = 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (m_released)
|
||||
{
|
||||
if (!m_taps)
|
||||
{
|
||||
m_start_time = now;
|
||||
}
|
||||
|
||||
++m_taps;
|
||||
m_released = false;
|
||||
}
|
||||
|
||||
return desired_taps == m_taps;
|
||||
}
|
||||
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
private:
|
||||
mutable bool m_released = true;
|
||||
mutable u32 m_taps = 0;
|
||||
mutable Clock::time_point m_start_time = Clock::now();
|
||||
};
|
||||
|
||||
// usage: relative(input, speed, [max_abs_value, [shared_state]])
|
||||
// speed is max movement per second
|
||||
class RelativeExpression : public FunctionExpression
|
||||
{
|
||||
ArgumentValidation
|
||||
ValidateArguments(const std::vector<std::unique_ptr<Expression>>& args) override
|
||||
{
|
||||
if (args.size() >= 2 && args.size() <= 4)
|
||||
return ArgumentsAreValid{};
|
||||
else
|
||||
return ExpectedArguments{"input, speed, [max_abs_value, [shared_state]]"};
|
||||
}
|
||||
|
||||
ControlState GetValue() const override
|
||||
{
|
||||
// There is a lot of funky math in this function but it allows for a variety of uses:
|
||||
//
|
||||
// e.g. A single mapping with a relatively adjusted value between 0.0 and 1.0
|
||||
// Potentially useful for a trigger input
|
||||
// relative(`Up` - `Down`, 2.0)
|
||||
//
|
||||
// e.g. A value with two mappings (such as analog stick Up/Down)
|
||||
// The shared state allows the two mappings to work together.
|
||||
// This mapping (for up) returns a value clamped between 0.0 and 1.0
|
||||
// relative(`Up`, 2.0, 1.0, $y)
|
||||
// This mapping (for down) returns the negative value clamped between 0.0 and 1.0
|
||||
// (Adjustments created by `Down` are applied negatively to the shared state)
|
||||
// relative(`Down`, 2.0, -1.0, $y)
|
||||
|
||||
const auto now = Clock::now();
|
||||
|
||||
if (GetArgCount() >= 4)
|
||||
m_state = GetArg(3).GetValue();
|
||||
|
||||
const auto elapsed = std::chrono::duration_cast<FSec>(now - m_last_update).count();
|
||||
m_last_update = now;
|
||||
|
||||
const ControlState input = GetArg(0).GetValue();
|
||||
const ControlState speed = GetArg(1).GetValue();
|
||||
|
||||
const ControlState max_abs_value = (GetArgCount() >= 3) ? GetArg(2).GetValue() : 1.0;
|
||||
|
||||
const ControlState max_move = input * elapsed * speed;
|
||||
const ControlState diff_from_zero = std::abs(0.0 - m_state);
|
||||
const ControlState diff_from_max = std::abs(max_abs_value - m_state);
|
||||
|
||||
m_state += std::min(std::max(max_move, -diff_from_zero), diff_from_max) *
|
||||
std::copysign(1.0, max_abs_value);
|
||||
|
||||
if (GetArgCount() >= 4)
|
||||
const_cast<Expression&>(GetArg(3)).SetValue(m_state);
|
||||
|
||||
return std::max(0.0, m_state * std::copysign(1.0, max_abs_value));
|
||||
}
|
||||
|
||||
private:
|
||||
mutable ControlState m_state = 0.0;
|
||||
mutable Clock::time_point m_last_update = Clock::now();
|
||||
};
|
||||
|
||||
// usage: pulse(input, seconds)
|
||||
class PulseExpression : public FunctionExpression
|
||||
{
|
||||
ArgumentValidation
|
||||
ValidateArguments(const std::vector<std::unique_ptr<Expression>>& args) override
|
||||
{
|
||||
if (2 == args.size())
|
||||
return ArgumentsAreValid{};
|
||||
else
|
||||
return ExpectedArguments{"input, seconds"};
|
||||
}
|
||||
|
||||
ControlState GetValue() const override
|
||||
{
|
||||
const auto now = Clock::now();
|
||||
|
||||
const ControlState input = GetArg(0).GetValue();
|
||||
|
||||
if (input < CONDITION_THRESHOLD)
|
||||
{
|
||||
m_released = true;
|
||||
}
|
||||
else if (m_released)
|
||||
{
|
||||
m_released = false;
|
||||
|
||||
const auto seconds = std::chrono::duration_cast<Clock::duration>(FSec(GetArg(1).GetValue()));
|
||||
|
||||
if (m_state)
|
||||
{
|
||||
m_release_time += seconds;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_state = true;
|
||||
m_release_time = now + seconds;
|
||||
}
|
||||
}
|
||||
|
||||
if (m_state && now >= m_release_time)
|
||||
{
|
||||
m_state = false;
|
||||
}
|
||||
|
||||
return m_state;
|
||||
}
|
||||
|
||||
private:
|
||||
mutable bool m_released = false;
|
||||
mutable bool m_state = false;
|
||||
mutable Clock::time_point m_release_time = Clock::now();
|
||||
};
|
||||
|
||||
std::unique_ptr<FunctionExpression> MakeFunctionExpression(std::string name)
|
||||
{
|
||||
if ("not" == name)
|
||||
return std::make_unique<NotExpression>();
|
||||
else if ("if" == name)
|
||||
return std::make_unique<IfExpression>();
|
||||
else if ("sin" == name)
|
||||
return std::make_unique<SinExpression>();
|
||||
else if ("timer" == name)
|
||||
return std::make_unique<TimerExpression>();
|
||||
else if ("toggle" == name)
|
||||
return std::make_unique<ToggleExpression>();
|
||||
else if ("minus" == name)
|
||||
return std::make_unique<UnaryMinusExpression>();
|
||||
else if ("deadzone" == name)
|
||||
return std::make_unique<DeadzoneExpression>();
|
||||
else if ("smooth" == name)
|
||||
return std::make_unique<SmoothExpression>();
|
||||
else if ("hold" == name)
|
||||
return std::make_unique<HoldExpression>();
|
||||
else if ("tap" == name)
|
||||
return std::make_unique<TapExpression>();
|
||||
else if ("relative" == name)
|
||||
return std::make_unique<RelativeExpression>();
|
||||
else if ("pulse" == name)
|
||||
return std::make_unique<PulseExpression>();
|
||||
else
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
int FunctionExpression::CountNumControls() const
|
||||
{
|
||||
int result = 0;
|
||||
|
||||
for (auto& arg : m_args)
|
||||
result += arg->CountNumControls();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void FunctionExpression::UpdateReferences(ControlEnvironment& env)
|
||||
{
|
||||
for (auto& arg : m_args)
|
||||
arg->UpdateReferences(env);
|
||||
}
|
||||
|
||||
FunctionExpression::ArgumentValidation
|
||||
FunctionExpression::SetArguments(std::vector<std::unique_ptr<Expression>>&& args)
|
||||
{
|
||||
m_args = std::move(args);
|
||||
|
||||
return ValidateArguments(m_args);
|
||||
}
|
||||
|
||||
Expression& FunctionExpression::GetArg(u32 number)
|
||||
{
|
||||
return *m_args[number];
|
||||
}
|
||||
|
||||
const Expression& FunctionExpression::GetArg(u32 number) const
|
||||
{
|
||||
return *m_args[number];
|
||||
}
|
||||
|
||||
u32 FunctionExpression::GetArgCount() const
|
||||
{
|
||||
return u32(m_args.size());
|
||||
}
|
||||
|
||||
void FunctionExpression::SetValue(ControlState)
|
||||
{
|
||||
}
|
||||
|
||||
} // namespace ExpressionParser
|
||||
} // namespace ciface
|
@ -0,0 +1,55 @@
|
||||
// Copyright 2019 Dolphin Emulator Project
|
||||
// Licensed under GPLv2+
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
#include "InputCommon/ControlReference/ExpressionParser.h"
|
||||
#include "InputCommon/ControlReference/FunctionExpression.h"
|
||||
|
||||
namespace ciface
|
||||
{
|
||||
namespace ExpressionParser
|
||||
{
|
||||
class FunctionExpression : public Expression
|
||||
{
|
||||
public:
|
||||
struct ArgumentsAreValid
|
||||
{
|
||||
};
|
||||
|
||||
struct ExpectedArguments
|
||||
{
|
||||
std::string text;
|
||||
};
|
||||
|
||||
using ArgumentValidation = std::variant<ArgumentsAreValid, ExpectedArguments>;
|
||||
|
||||
int CountNumControls() const override;
|
||||
void UpdateReferences(ControlEnvironment& env) override;
|
||||
|
||||
ArgumentValidation SetArguments(std::vector<std::unique_ptr<Expression>>&& args);
|
||||
|
||||
void SetValue(ControlState value) override;
|
||||
|
||||
protected:
|
||||
virtual ArgumentValidation
|
||||
ValidateArguments(const std::vector<std::unique_ptr<Expression>>& args) = 0;
|
||||
|
||||
Expression& GetArg(u32 number);
|
||||
const Expression& GetArg(u32 number) const;
|
||||
u32 GetArgCount() const;
|
||||
|
||||
private:
|
||||
std::vector<std::unique_ptr<Expression>> m_args;
|
||||
};
|
||||
|
||||
std::unique_ptr<FunctionExpression> MakeFunctionExpression(std::string name);
|
||||
|
||||
} // namespace ExpressionParser
|
||||
} // namespace ciface
|
@ -38,23 +38,38 @@ std::unique_lock<std::recursive_mutex> EmulatedController::GetStateLock()
|
||||
|
||||
void EmulatedController::UpdateReferences(const ControllerInterface& devi)
|
||||
{
|
||||
const auto lock = GetStateLock();
|
||||
m_default_device_is_connected = devi.HasConnectedDevice(m_default_device);
|
||||
|
||||
ciface::ExpressionParser::ControlEnvironment env(devi, GetDefaultDevice(), m_expression_vars);
|
||||
|
||||
UpdateReferences(env);
|
||||
}
|
||||
|
||||
void EmulatedController::UpdateReferences(ciface::ExpressionParser::ControlEnvironment& env)
|
||||
{
|
||||
const auto lock = GetStateLock();
|
||||
|
||||
for (auto& ctrlGroup : groups)
|
||||
{
|
||||
for (auto& control : ctrlGroup->controls)
|
||||
control->control_ref.get()->UpdateReference(devi, GetDefaultDevice());
|
||||
control->control_ref->UpdateReference(env);
|
||||
|
||||
// Attachments:
|
||||
if (ctrlGroup->type == GroupType::Attachments)
|
||||
{
|
||||
for (auto& attachment : static_cast<Attachments*>(ctrlGroup.get())->GetAttachmentList())
|
||||
attachment->UpdateReferences(devi);
|
||||
attachment->UpdateReferences(env);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void EmulatedController::UpdateSingleControlReference(const ControllerInterface& devi,
|
||||
ControlReference* ref)
|
||||
{
|
||||
ciface::ExpressionParser::ControlEnvironment env(devi, GetDefaultDevice(), m_expression_vars);
|
||||
ref->UpdateReference(env);
|
||||
}
|
||||
|
||||
bool EmulatedController::IsDefaultDeviceConnected() const
|
||||
{
|
||||
return m_default_device_is_connected;
|
||||
|
@ -13,6 +13,7 @@
|
||||
|
||||
#include "Common/Common.h"
|
||||
#include "Common/IniFile.h"
|
||||
#include "InputCommon/ControlReference/ExpressionParser.h"
|
||||
#include "InputCommon/ControllerInterface/Device.h"
|
||||
|
||||
class ControllerInterface;
|
||||
@ -20,6 +21,8 @@ class ControllerInterface;
|
||||
const char* const named_directions[] = {_trans("Up"), _trans("Down"), _trans("Left"),
|
||||
_trans("Right")};
|
||||
|
||||
class ControlReference;
|
||||
|
||||
namespace ControllerEmu
|
||||
{
|
||||
class ControlGroup;
|
||||
@ -43,6 +46,7 @@ public:
|
||||
void SetDefaultDevice(ciface::Core::DeviceQualifier devq);
|
||||
|
||||
void UpdateReferences(const ControllerInterface& devi);
|
||||
void UpdateSingleControlReference(const ControllerInterface& devi, ControlReference* ref);
|
||||
|
||||
// This returns a lock that should be held before calling State() on any control
|
||||
// references and GetState(), by extension. This prevents a race condition
|
||||
@ -75,6 +79,12 @@ public:
|
||||
return T(std::lround((zero_value - neg_1_value) * input_value + zero_value));
|
||||
}
|
||||
|
||||
protected:
|
||||
// TODO: Wiimote attachment has its own member that isn't being used..
|
||||
ciface::ExpressionParser::ControlEnvironment::VariableContainer m_expression_vars;
|
||||
|
||||
void UpdateReferences(ciface::ExpressionParser::ControlEnvironment& env);
|
||||
|
||||
private:
|
||||
ciface::Core::DeviceQualifier m_default_device;
|
||||
bool m_default_device_is_connected{false};
|
||||
|
@ -64,6 +64,7 @@
|
||||
<ClCompile Include="ControllerInterface\ForceFeedback\ForceFeedbackDevice.cpp" />
|
||||
<ClCompile Include="ControllerInterface\Win32\Win32.cpp" />
|
||||
<ClCompile Include="ControllerInterface\XInput\XInput.cpp" />
|
||||
<ClCompile Include="ControlReference\FunctionExpression.cpp" />
|
||||
<ClCompile Include="GCAdapter.cpp">
|
||||
<!--
|
||||
Disable "nonstandard extension used : zero-sized array in struct/union" warning,
|
||||
@ -100,6 +101,7 @@
|
||||
<ClInclude Include="ControllerInterface\DInput\DInputKeyboardMouse.h" />
|
||||
<ClInclude Include="ControllerInterface\DInput\XInputFilter.h" />
|
||||
<ClInclude Include="ControlReference\ControlReference.h" />
|
||||
<ClInclude Include="ControlReference\FunctionExpression.h" />
|
||||
<ClInclude Include="ControlReference\ExpressionParser.h" />
|
||||
<ClInclude Include="ControllerInterface\ForceFeedback\ForceFeedbackDevice.h" />
|
||||
<ClInclude Include="ControllerInterface\Win32\Win32.h" />
|
||||
|
@ -113,6 +113,9 @@
|
||||
<ClCompile Include="ControlReference\ControlReference.cpp">
|
||||
<Filter>ControllerInterface</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="ControlReference\FunctionExpression.cpp">
|
||||
<Filter>ControllerInterface</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="InputProfile.cpp" />
|
||||
<ClCompile Include="ControllerEmu\ControlGroup\Attachments.cpp">
|
||||
<Filter>ControllerEmu\ControlGroup</Filter>
|
||||
@ -206,6 +209,9 @@
|
||||
<ClInclude Include="ControlReference\ControlReference.h">
|
||||
<Filter>ControllerInterface</Filter>
|
||||
</ClInclude>
|
||||
<ClCompile Include="ControlReference\FunctionExpression.h">
|
||||
<Filter>ControllerInterface</Filter>
|
||||
</ClCompile>
|
||||
<ClInclude Include="InputProfile.h" />
|
||||
<ClInclude Include="ControllerEmu\ControlGroup\Attachments.h">
|
||||
<Filter>ControllerEmu\ControlGroup</Filter>
|
||||
|
Reference in New Issue
Block a user