Skip to content

Commit 36346c1

Browse files
shukenmgShuken
andauthored
Add a mappable shake input
- mappable "shake" input Co-authored-by: Shuken <shukenmg@iuvenisstudios.ga>
1 parent 43786c1 commit 36346c1

7 files changed

Lines changed: 322 additions & 236 deletions

File tree

BetterJoyForCemu/App.config

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@
2525
<!--On is "true"; off is "false". Default: true -->
2626
<add key="EnableRumble" value="true" />
2727

28+
<!--Enables a input when shaking a controller, only works with DS4 for now, replaces the touchpad input (Button 13 on DirectInput)-->
29+
<!--On is "true"; off is "false". Default: false -->
30+
<add key="EnableShakeInput" value="false" />
31+
<!--How sensitve the shake detection should be. Default: 10-->
32+
<add key="ShakeInputSensitivity" value="10" />
33+
<!--How often should the shake input run in milliseconds. -->
34+
<!--Don't set this lower than 15 -->
35+
<!-- Default: 200 -->
36+
<add key="ShakeInputDelay" value="200" />
37+
2838
<!--Swap A-B buttons; if on, this mimicks the (half of) Xbox layout by the button name, rather than by the physical layout.-->
2939
<!--Also swaps buttons when using "Also use for buttons/axes"-->
3040
<!--On is "true"; off is "false". Default: false -->

BetterJoyForCemu/Config.cs

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public static class Config { // stores dynamic configuration, including
77
const string PATH = "settings";
88
static Dictionary<string, string> variables = new Dictionary<string, string>();
99

10-
const int settingsNum = 10; // currently - ProgressiveScan, StartInTray + special buttons
10+
const int settingsNum = 11; // currently - ProgressiveScan, StartInTray + special buttons
1111

1212
public static string GetDefaultValue(string s) {
1313
switch (s) {
@@ -21,15 +21,36 @@ public static string GetDefaultValue(string s) {
2121
return "0";
2222
}
2323

24+
// Helper function to count how many lines are in a file
25+
// https://www.dotnetperls.com/line-count
26+
static long CountLinesInFile(string f) {
27+
// Zero based count
28+
long count = -1;
29+
using (StreamReader r = new StreamReader(f)) {
30+
string line;
31+
while ((line = r.ReadLine()) != null) {
32+
count++;
33+
}
34+
}
35+
return count;
36+
}
37+
2438
public static void Init(List<KeyValuePair<string, float[]>> caliData) {
25-
foreach (string s in new string[] { "ProgressiveScan", "StartInTray", "capture", "home", "sl_l", "sl_r", "sr_l", "sr_r", "reset_mouse", "active_gyro" })
39+
foreach (string s in new string[] { "ProgressiveScan", "StartInTray", "capture", "home", "sl_l", "sl_r", "sr_l", "sr_r", "shake", "reset_mouse", "active_gyro" })
2640
variables[s] = GetDefaultValue(s);
2741

2842
if (File.Exists(PATH)) {
29-
int lineNO = 0;
43+
44+
// Reset settings file if old settings
45+
if (CountLinesInFile(PATH) < settingsNum) {
46+
File.Delete(PATH);
47+
Init(caliData);
48+
return;
49+
}
50+
3051
using (StreamReader file = new StreamReader(PATH)) {
3152
string line = String.Empty;
32-
53+
int lineNO = 0;
3354
while ((line = file.ReadLine()) != null) {
3455
string[] vs = line.Split();
3556
try {
@@ -52,14 +73,6 @@ public static void Init(List<KeyValuePair<string, float[]>> caliData) {
5273
} catch { }
5374
lineNO++;
5475
}
55-
56-
57-
}
58-
59-
// if old settings
60-
if (lineNO < settingsNum) {
61-
File.Delete(PATH);
62-
Init(caliData);
6376
}
6477
} else {
6578
using (StreamWriter file = new StreamWriter(PATH)) {

BetterJoyForCemu/Joycon.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public enum DebugType : int {
3131
THREADING,
3232
IMU,
3333
RUMBLE,
34+
SHAKE,
3435
};
3536
public DebugType debug_type = DebugType.NONE;
3637
public bool isLeft;
@@ -574,6 +575,38 @@ private int ReceiveRaw() {
574575
return ret;
575576
}
576577

578+
private readonly Stopwatch shakeTimer = Stopwatch.StartNew(); //Setup a timer for measuring shake in milliseconds
579+
private long shakedTime = 0;
580+
private bool hasShaked;
581+
void DetectShake() {
582+
if (form.shakeInputEnabled) {
583+
long currentShakeTime = shakeTimer.ElapsedMilliseconds;
584+
585+
// Shake detection logic
586+
bool isShaking = GetAccel().LengthSquared() >= form.shakeSesitivity;
587+
if (isShaking && currentShakeTime >= shakedTime + form.shakeDelay || isShaking && shakedTime == 0) {
588+
shakedTime = currentShakeTime;
589+
hasShaked = true;
590+
591+
// Mapped shake key down
592+
Simulate(Config.Value("shake"), false, false);
593+
DebugPrint("Shaked at time: " + shakedTime.ToString(), DebugType.SHAKE);
594+
}
595+
596+
// If controller was shaked then release mapped key after a small delay to simulate a button press, then reset hasShaked
597+
if (hasShaked && currentShakeTime >= shakedTime + 10) {
598+
// Mapped shake key up
599+
Simulate(Config.Value("shake"), false, true);
600+
DebugPrint("Shake completed", DebugType.SHAKE);
601+
hasShaked = false;
602+
}
603+
604+
} else {
605+
shakeTimer.Stop();
606+
return;
607+
}
608+
}
609+
577610
bool dragToggle = Boolean.Parse(ConfigurationManager.AppSettings["DragToggle"]);
578611
Dictionary<int, bool> mouse_toggle_btn = new Dictionary<int, bool>();
579612
private void Simulate(string s, bool click = true, bool up = false) {
@@ -655,6 +688,8 @@ private void DoThingsWithButtons() {
655688
}
656689
}
657690

691+
DetectShake();
692+
658693
if (buttons_down[(int)Button.CAPTURE])
659694
Simulate(Config.Value("capture"));
660695
if (buttons_down[(int)Button.HOME])

BetterJoyForCemu/MainForm.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ public partial class MainForm : Form {
2222
private Timer countDown;
2323
private int count;
2424
public List<int> xG, yG, zG, xA, yA, zA;
25+
public bool shakeInputEnabled = Boolean.Parse(ConfigurationManager.AppSettings["EnableShakeInput"]);
26+
public float shakeSesitivity = float.Parse(ConfigurationManager.AppSettings["ShakeInputSensitivity"]);
27+
public float shakeDelay = float.Parse(ConfigurationManager.AppSettings["ShakeInputDelay"]);
2528

2629
public MainForm() {
2730
xG = new List<int>(); yG = new List<int>(); zG = new List<int>();

0 commit comments

Comments
 (0)