summaryrefslogtreecommitdiff
path: root/Source/Core/InputCommon/ControlReference/FunctionExpression.cpp
blob: 8692d04c786a9efb79687c07241d32d8dd00dc50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// 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;

// TODO: Return an oscillating value to make it apparent something was spelled wrong?
class UnknownFunctionExpression : public FunctionExpression
{
private:
  virtual bool ValidateArguments(const std::vector<std::unique_ptr<Expression>>& args) override
  {
    return false;
  }
  ControlState GetValue() const override { return 0.0; }
  void SetValue(ControlState value) override {}
  std::string GetFuncName() const override { return "unknown"; }
};

// usage: !toggle(toggle_state_input, [clear_state_input])
class ToggleExpression : public FunctionExpression
{
private:
  virtual bool ValidateArguments(const std::vector<std::unique_ptr<Expression>>& args) override
  {
    // Optional 2nd argument for clearing state:
    return 1 == args.size() || 2 == args.size();
  }

  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;
  }

  void SetValue(ControlState value) override {}
  std::string GetFuncName() const override { return "toggle"; }

  mutable bool m_released{};
  mutable bool m_state{};
};

// usage: !not(expression)
class NotExpression : public FunctionExpression
{
private:
  virtual bool ValidateArguments(const std::vector<std::unique_ptr<Expression>>& args) override
  {
    return 1 == args.size();
  }

  ControlState GetValue() const override { return 1.0 - GetArg(0).GetValue(); }
  void SetValue(ControlState value) override { GetArg(0).SetValue(1.0 - value); }
  std::string GetFuncName() const override { return ""; }
};

// usage: !sin(expression)
class SinExpression : public FunctionExpression
{
private:
  virtual bool ValidateArguments(const std::vector<std::unique_ptr<Expression>>& args) override
  {
    return 1 == args.size();
  }

  ControlState GetValue() const override { return std::sin(GetArg(0).GetValue()); }
  void SetValue(ControlState value) override {}
  std::string GetFuncName() const override { return "sin"; }
};

// usage: !timer(seconds)
class TimerExpression : public FunctionExpression
{
private:
  virtual bool ValidateArguments(const std::vector<std::unique_ptr<Expression>>& args) override
  {
    return 1 == args.size();
  }

  ControlState GetValue() const override
  {
    const auto now = Clock::now();
    const auto elapsed = now - m_start_time;

    using FSec = std::chrono::duration<ControlState>;

    const ControlState val = GetArg(0).GetValue();

    ControlState progress = std::chrono::duration_cast<FSec>(elapsed).count() / val;

    if (std::isinf(progress))
    {
      // User configured a 0.0 length 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;
  }
  void SetValue(ControlState value) override {}
  std::string GetFuncName() const override { return "timer"; }

private:
  using Clock = std::chrono::steady_clock;
  mutable Clock::time_point m_start_time = Clock::now();
};

// usage: !if(condition, true_expression, false_expression)
class IfExpression : public FunctionExpression
{
private:
  virtual bool ValidateArguments(const std::vector<std::unique_ptr<Expression>>& args) override
  {
    return 3 == args.size();
  }

  ControlState GetValue() const override
  {
    return (GetArg(0).GetValue() > CONDITION_THRESHOLD) ? GetArg(1).GetValue() :
                                                          GetArg(2).GetValue();
  }

  void SetValue(ControlState value) override {}
  std::string GetFuncName() const override { return "if"; }
};

// usage: !minus(expression)
class UnaryMinusExpression : public FunctionExpression
{
private:
  virtual bool ValidateArguments(const std::vector<std::unique_ptr<Expression>>& args) override
  {
    return 1 == args.size();
  }

  ControlState GetValue() const override
  {
    // Subtraction for clarity:
    return 0.0 - GetArg(0).GetValue();
  }

  void SetValue(ControlState value) override {}
  std::string GetFuncName() const override { return "minus"; }
};

// usage: !while(condition, expression)
class WhileExpression : public FunctionExpression
{
  virtual bool ValidateArguments(const std::vector<std::unique_ptr<Expression>>& args) override
  {
    return 2 == args.size();
  }

  ControlState GetValue() const override
  {
    // Returns 1.0 on successful loop, 0.0 on reps exceeded. Sensible?

    for (int i = 0; i != LOOP_MAX_REPS; ++i)
    {
      // Check condition of 1st argument:
      const ControlState val = GetArg(0).GetValue();
      if (val < CONDITION_THRESHOLD)
        return 1.0;

      // Evaluate 2nd argument:
      GetArg(1).GetValue();
    }

    // Exceeded max reps:
    return 0.0;
  }

  void SetValue(ControlState value) override {}
  std::string GetFuncName() const override { return "while"; }
};

// usage: deadzone(input, amount)
class DeadzoneExpression : public FunctionExpression
{
  virtual bool ValidateArguments(const std::vector<std::unique_ptr<Expression>>& args) override
  {
    return 2 == args.size();
  }

  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);
  }

  void SetValue(ControlState value) override {}
  std::string GetFuncName() const override { return "deadzone"; }
};

// usage: smooth(input, seconds)
// seconds is seconds to change from 0.0 to 1.0
class SmoothExpression : public FunctionExpression
{
  virtual bool ValidateArguments(const std::vector<std::unique_ptr<Expression>>& args) override
  {
    return 2 == args.size();
  }

  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 = GetArg(1).GetValue();

    using FSec = std::chrono::duration<ControlState>;

    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;
  }

  void SetValue(ControlState value) override {}
  std::string GetFuncName() const override { return "smooth"; }

private:
  using Clock = std::chrono::steady_clock;

  mutable ControlState m_value = 0.0;
  mutable Clock::time_point m_last_update = Clock::now();
};

std::unique_ptr<FunctionExpression> MakeFunctionExpression(std::string name)
{
  if (name.empty())
    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 ("while" == name)
    return std::make_unique<WhileExpression>();
  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
    return std::make_unique<UnknownFunctionExpression>();
}

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::operator std::string() const
{
  std::string result = '!' + GetFuncName();

  for (auto& arg : m_args)
    result += ' ' + static_cast<std::string>(*arg);

  return result;
}

bool 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());
}

}  // namespace ExpressionParser
}  // namespace ciface