Skip to content

Commit

Permalink
Binds now save!
Browse files Browse the repository at this point in the history
  • Loading branch information
EliteAsian123 committed Mar 26, 2023
1 parent 95f0498 commit dd86abb
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Assets/Scenes/MenuScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -13979,7 +13979,7 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 0.0031149874, g: 0.033323165, b: 0.13207549, a: 1}
m_Color: {r: 0.0031149874, g: 0.033323165, b: 0.13207549, a: 0.9490196}
m_RaycastTarget: 1
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
m_Maskable: 1
Expand Down
28 changes: 22 additions & 6 deletions Assets/Script/MainMenuBackground.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,22 @@

namespace YARG {
public class MainMenuBackground : MonoBehaviour {
public static MainMenuBackground Instance {
get;
private set;
}

public bool cursorMoves = true;

[SerializeField]
private Transform cameraContainer;
[SerializeField]
private new Camera camera;

private void Awake() {
Instance = this;
}

private void Start() {
cameraContainer.transform.position = new Vector3(0, 2f, 0);
}
Expand All @@ -17,13 +28,18 @@ private void Update() {
cameraContainer.transform.position = Vector3.Lerp(cameraContainer.transform.position,
new Vector3(0, 0.5f, 0), Time.deltaTime * 1.5f);

// Get the mouse position
var mousePos = Mouse.current.position.ReadValue();
mousePos = camera.ScreenToViewportPoint(mousePos);
Vector2 mousePos;
if (cursorMoves) {
// Get the mouse position
mousePos = Mouse.current.position.ReadValue();
mousePos = camera.ScreenToViewportPoint(mousePos);

// Clamp
mousePos.x = Mathf.Clamp(mousePos.x, 0f, 1f);
mousePos.y = Mathf.Clamp(mousePos.y, 0f, 1f);
// Clamp
mousePos.x = Mathf.Clamp(mousePos.x, 0f, 1f);
mousePos.y = Mathf.Clamp(mousePos.y, 0f, 1f);
} else {
mousePos = new Vector2(0f, 0.5f);
}

// Move camera with the cursor
camera.transform.localPosition = camera.transform.localPosition
Expand Down
107 changes: 107 additions & 0 deletions Assets/Script/Serialization/InputBindSerializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
using UnityEngine;
using UnityEngine.InputSystem;
using YARG.Input;

namespace YARG.Serialization {
public static class InputBindSerializer {
private class InputBindSave {
public string inputStrategy;
public string deviceName;
public Dictionary<string, string> binds = new();
}

private static List<InputBindSave> inputBindSaves = new();

static InputBindSerializer() {
// Load from JSON
try {
var json = File.ReadAllText(Path.Combine(Application.persistentDataPath, "inputBinds.json"));
inputBindSaves = JsonConvert.DeserializeObject<List<InputBindSave>>(json);
} catch (Exception) {
Debug.LogWarning("Failed to load input binds from JSON. Ignoring.");
}
}

public static void LoadBindsFromSave(InputStrategy inputStrategy) {
// Look from correct bind (using input strategy and device)
InputBindSave inputBindSave = null;
foreach (InputBindSave bindSave in inputBindSaves) {
if (bindSave.deviceName != inputStrategy.InputDevice.name) {
continue;
}

if (bindSave.inputStrategy != inputStrategy.GetType().Name) {
continue;
}

inputBindSave = bindSave;
break;
}

if (inputBindSave == null) {
return;
}

// Set binds
foreach (var binding in inputStrategy.GetMappingNames()) {
if (!inputBindSave.binds.ContainsKey(binding)) {
continue;
}

var control = InputControlPath.TryFindControl(inputStrategy.InputDevice, inputBindSave.binds[binding]);
inputStrategy.SetMappingInputControl(binding, control);
}
}

public static void SaveBindsFromInputStrategy(InputStrategy inputStrategy) {
// Look for existing bind save
InputBindSave inputBindSave = null;
foreach (InputBindSave bindSave in inputBindSaves) {
if (bindSave.deviceName != inputStrategy.InputDevice.name) {
continue;
}

if (bindSave.inputStrategy != inputStrategy.GetType().Name) {
continue;
}

inputBindSave = bindSave;
break;
}

if (inputBindSave != null) {
inputBindSaves.Remove(inputBindSave);
}

// Create new bind save if none found
inputBindSave = new InputBindSave {
inputStrategy = inputStrategy.GetType().Name,
deviceName = inputStrategy.InputDevice.name
};
inputBindSaves.Add(inputBindSave);

// Save binds
inputBindSave.binds.Clear();
foreach (var binding in inputStrategy.GetMappingNames()) {
var control = inputStrategy.GetMappingInputControl(binding);
if (control == null) {
continue;
}

inputBindSave.binds.Add(binding, control.path);
}

// Save to JSON
SaveToJsonFile();
}

private static void SaveToJsonFile() {
var json = JsonConvert.SerializeObject(inputBindSaves);
File.WriteAllText(Path.Combine(Application.persistentDataPath, "inputBinds.json"), json);
}
}
}
11 changes: 11 additions & 0 deletions Assets/Script/Serialization/InputBindSerializer.cs.meta

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

15 changes: 13 additions & 2 deletions Assets/Script/UI/AddPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using UnityEngine.InputSystem.Controls;
using UnityEngine.UI;
using YARG.Input;
using YARG.Serialization;

namespace YARG.UI {
public class AddPlayer : MonoBehaviour {
Expand Down Expand Up @@ -195,11 +196,15 @@ public void DoneConfigure() {

playerName = playerNameField.text;

// TEMP
if (inputStrategy.InputDevice is MidiDevice midiDevice) {
midiDevice.onWillNoteOn += OnNote;
}

// Try to load bindings
if (inputStrategy.InputDevice != null) {
InputBindSerializer.LoadBindsFromSave(inputStrategy);
}

UpdateBind();
}

Expand Down Expand Up @@ -250,16 +255,22 @@ private void OnNote(MidiNoteControl control, float v) {
}

public void DoneBind() {
// TEMP
if (inputStrategy.InputDevice is MidiDevice midiDevice) {
midiDevice.onWillNoteOn -= OnNote;
}

// Save bindings
if (inputStrategy.InputDevice != null) {
InputBindSerializer.SaveBindsFromInputStrategy(inputStrategy);
}

// Create and add player
var player = new PlayerManager.Player() {
inputStrategy = inputStrategy
};
PlayerManager.players.Add(player);

// Set name
if (!string.IsNullOrEmpty(playerName)) {
player.name = playerName;
}
Expand Down
4 changes: 4 additions & 0 deletions Assets/Script/UI/MainMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ private void OnGenericNavigation(NavigationType navigationType, bool firstPresse
}

private void HideAll() {
MainMenuBackground.Instance.cursorMoves = false;

editPlayers.gameObject.SetActive(false);
addPlayer.gameObject.SetActive(false);
mainMenu.gameObject.SetActive(false);
Expand All @@ -109,6 +111,8 @@ private void HideAll() {
public void ShowMainMenu() {
HideAll();

MainMenuBackground.Instance.cursorMoves = true;

menuContainer.SetActive(true);
settingsContainer.SetActive(false);
mainMenu.gameObject.SetActive(true);
Expand Down

0 comments on commit dd86abb

Please sign in to comment.