Skip to content

Commit

Permalink
Added ability to toggle for single/double joycon mode.
Browse files Browse the repository at this point in the history
Reworked UI further.
Final commit for v5.
  • Loading branch information
Davidobot committed Aug 23, 2018
1 parent be3c73b commit 37aba95
Show file tree
Hide file tree
Showing 9 changed files with 215 additions and 22 deletions.
2 changes: 2 additions & 0 deletions BetterJoyForCemu/BetterJoyForCemu.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Icons\cross.png" />
<Content Include="Icons\jc_left_s.png" />
<Content Include="Icons\jc_right_s.png" />
<Resource Include="Icons\jc_left.png" />
<Resource Include="Icons\jc_right.png" />
<Resource Include="Icons\pro.png" />
Expand Down
Binary file added BetterJoyForCemu/Icons/jc_left_s.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added BetterJoyForCemu/Icons/jc_right_s.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions BetterJoyForCemu/Joycon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public byte[] GetData() {
public int packetCounter = 0;

public Xbox360Controller xin;
Xbox360Report report;
public Xbox360Report report;

int rumblePeriod = Int32.Parse(ConfigurationManager.AppSettings["RumblePeriod"]);
int lowFreq = Int32.Parse(ConfigurationManager.AppSettings["LowFreqRumble"]);
Expand Down Expand Up @@ -420,7 +420,7 @@ private int ReceiveRaw() {
return ret;
}

private Thread PollThreadObj;
private Thread PollThreadObj; // pro times out over time randomly if it was USB and then bluetooth??
private void Poll() {
int attempts = 0;
while (!stop_polling & state > state_.NO_JOYCONS) {
Expand Down
57 changes: 53 additions & 4 deletions BetterJoyForCemu/MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

110 changes: 102 additions & 8 deletions BetterJoyForCemu/MainForm.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using Nefarius.ViGEm.Client.Targets;
using Nefarius.ViGEm.Client.Targets.Xbox360;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
Expand All @@ -12,12 +14,13 @@

namespace BetterJoyForCemu {
public partial class MainForm : Form {
public List<Button> con;
public List<Button> con, loc;

public MainForm() {
InitializeComponent();

con = new List<Button> { con1, con2, con3, con4 };
loc = new List<Button> { loc1, loc2, loc3, loc4 };
}

private void MainForm_Resize(object sender, EventArgs e) {
Expand Down Expand Up @@ -52,7 +55,7 @@ private void MainForm_FormClosing(object sender, FormClosingEventArgs e) {
} catch { }
}

private void exitToolStripMenuItem_Click(object sender, EventArgs e) {
private void exitToolStripMenuItem_Click(object sender, EventArgs e) { // this does not work, for some reason. Fix before release
try {
Program.Stop();
Close();
Expand All @@ -77,16 +80,107 @@ public void AppendTextBox(string value) { // https://stackoverflow.com/questions
console.AppendText(value);
}

public void conBtnClick(object sender, EventArgs e) {
Button button = sender as Button;
bool toRumble = Boolean.Parse(ConfigurationManager.AppSettings["EnableRumble"]);
bool showAsXInput = Boolean.Parse(ConfigurationManager.AppSettings["ShowAsXInput"]);

if (button.ClientRectangle.Contains(PointToClient(Control.MousePosition))) { // hacky but allows both l&r clicks
if (button.Tag.GetType() == typeof(Joycon)) {
Joycon v = (Joycon)button.Tag;
public void locBtnClick(object sender, EventArgs e) {
Button bb = sender as Button;

if (bb.Tag.GetType() == typeof(Button)) {
Button button = bb.Tag as Button;

if (button.Tag.GetType() == typeof(Joycon)) {
Joycon v = (Joycon) button.Tag;
v.SetRumble(20.0f, 400.0f, 1.0f, 300);
}
}
}

public void conBtnClick(object sender, EventArgs e) {
Button button = sender as Button;

if (button.Tag.GetType() == typeof(Joycon)) {
Joycon v = (Joycon)button.Tag;

if (v.other == null && !v.isPro) { // needs connecting to other joycon (so messy omg)

int found = 0;
int minPadID = 10;
foreach (Joycon jc in Program.mgr.j) { // current system is designed for a maximum of two joycons connected to the PC
if (!jc.isPro) {
found++;
minPadID = Math.Min(jc.PadId, minPadID);
}
jc.LED = (byte)(0x1 << jc.PadId);
}

if (found == 2) {
AppendTextBox("Both joycons successfully found.\r\n");
Joycon temp = null;
foreach (Joycon jc in Program.mgr.j) {
if (!jc.isPro) {
jc.LED = (byte)(0x1 << minPadID);

if (temp == null)
temp = jc;
else {
temp.other = jc;
jc.other = temp;

temp.xin.Dispose();
temp.xin = null;
}

foreach (Button b in con) {
if (b.Tag == jc) {
if (jc.isLeft)
b.BackgroundImage = Properties.Resources.jc_left;
else
b.BackgroundImage = Properties.Resources.jc_right;
}
}
}
} // Join up the two joycons
}
} else if (v.other != null && !v.isPro) { // needs disconnecting from other joycon
if (v.xin == null) {
ReenableXinput(v);
v.xin.Connect();
}

if (v.other.xin == null) {
ReenableXinput(v.other);
v.other.xin.Connect();
}

if (v.isLeft)
button.BackgroundImage = Properties.Resources.jc_left_s;
else
button.BackgroundImage = Properties.Resources.jc_right_s;

foreach (Button b in con) {
if (b.Tag == v.other) {
if (v.other.isLeft)
b.BackgroundImage = Properties.Resources.jc_left_s;
else
b.BackgroundImage = Properties.Resources.jc_right_s;
}
}

v.other.other = null;
v.other = null;
}
}
}

void ReenableXinput(Joycon v) {
if (showAsXInput) {
v.xin = new Xbox360Controller(Program.emClient);

if (toRumble)
v.xin.FeedbackReceived += v.ReceiveRumble;
v.report = new Xbox360Report();
}
}
}
}
34 changes: 28 additions & 6 deletions BetterJoyForCemu/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,16 @@ void CleanUp() { // removes dropped controllers from list
for (int i = 0; i < j.Count; i++) {
Joycon v = j[i];
if (v.state == Joycon.state_.DROPPED) {
if (v.other != null)
v.other.other = null; // The other of the other is the joycon itself

v.Detach(); rem.Add(v);

foreach (Button b in form.con) {
if (b.Enabled & b.Tag == v) {
b.Invoke(new MethodInvoker(delegate {
b.Enabled = false;
b.BackgroundImage = Properties.Resources.cross;
}));
break;
}
Expand Down Expand Up @@ -105,6 +109,7 @@ public void CheckForNewControllers() {
IntPtr top_ptr = ptr;

hid_device_info enumerate; // Add device to list
bool foundNew = false;
while (ptr != IntPtr.Zero) {
enumerate = (hid_device_info)Marshal.PtrToStructure(ptr, typeof(hid_device_info));

Expand Down Expand Up @@ -159,17 +164,20 @@ public void CheckForNewControllers() {

j.Add(new Joycon(handle, EnableIMU, EnableLocalize & EnableIMU, 0.05f, isLeft, enumerate.path, j.Count, enumerate.product_id == product_pro, enumerate.serial_number == "000000000001"));

foundNew = true;
j.Last().form = form;

if (j.Count < 5) {
int ii = -1;
foreach (Button v in form.con) {
ii++;
if (!v.Enabled) {
System.Drawing.Bitmap temp;
switch (enumerate.product_id) {
case (product_l):
temp = Properties.Resources.jc_left; break;
temp = Properties.Resources.jc_left_s; break;
case (product_r):
temp = Properties.Resources.jc_right; break;
temp = Properties.Resources.jc_right_s; break;
case (product_pro):
temp = Properties.Resources.pro; break;
default:
Expand All @@ -182,6 +190,12 @@ public void CheckForNewControllers() {
v.Click += new EventHandler(form.conBtnClick);
v.BackgroundImage = temp;
}));

form.loc[ii].Invoke(new MethodInvoker(delegate {
form.loc[ii].Tag = v;
form.loc[ii].Click += new EventHandler(form.locBtnClick);
}));

break;
}
}
Expand All @@ -202,12 +216,11 @@ public void CheckForNewControllers() {
if (!v.isPro) {
found++;
minPadID = Math.Min(v.PadId, minPadID);
} else {
v.LED = (byte)(0x1 << v.PadId);
}
v.LED = (byte)(0x1 << v.PadId);
}

if (found == 2) {
if (found == 2 && foundNew) {
form.AppendTextBox("Both joycons successfully found.\r\n");
Joycon temp = null;
foreach (Joycon v in j) {
Expand All @@ -223,6 +236,15 @@ public void CheckForNewControllers() {
temp.xin.Dispose();
temp.xin = null;
}

foreach (Button b in form.con) {
if (b.Tag == v) {
if (v.isLeft)
b.BackgroundImage = Properties.Resources.jc_left;
else
b.BackgroundImage = Properties.Resources.jc_right;
}
}
}
} // Join up the two joycons
}
Expand Down Expand Up @@ -308,7 +330,7 @@ class Program {

private static readonly HttpClient client = new HttpClient();

static JoyconManager mgr;
public static JoyconManager mgr;
static HighResTimer timer;
static string pid;

Expand Down
Loading

0 comments on commit 37aba95

Please sign in to comment.