Skip to content

Commit

Permalink
Merge pull request #5 from Codenade/dev
Browse files Browse the repository at this point in the history
merge changes
  • Loading branch information
Codenade authored Jul 15, 2023
2 parents 01e9e0f + c415482 commit c07cbf2
Show file tree
Hide file tree
Showing 31 changed files with 1,995 additions and 1,228 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "ksp2-inputbinder-assets"]
path = ksp2-inputbinder-assets
url = https://github.com/Codenade/ksp2-inputbinder-assets
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) [year] [fullname]
Copyright (c) 2023 Codenade

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
110 changes: 109 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,109 @@
# ksp2-inputbinder
# ksp2-inputbinder

ksp2-inputbinder is a mod for [Kerbal Space Program 2](https://en.wikipedia.org/wiki/Kerbal_Space_Program_2).

### Jump to section

* [Configuration](#configuration)
* [How to install](#how-to-install)
* [How to build](#how-to-build)

This mod makes it possible to use gamepads for controlling your vessel. Additionally, it brings UI for configuring input actions, which lets the user make more detailed changes.

![Settings Panel](https://github.com/Codenade/Codenade/blob/main/ksp2-inputbinder-panel.png?raw=true)

## Configuration

The mod settings can be accessed in-game in the flight view.

![AppBar](https://github.com/Codenade/Codenade/blob/05bb56f4755e55ecd7953ca2ef4baf3d16695c7f/ksp2-inputbinder-appbar.png?raw=true)

### Custom actions

#### Throttle
* Throttle Axis: Control your throttle as an axis

#### Trim
Dedicated actions to change the trim
* Pitch Trim
* Roll Trim
* Yaw Trim
* Trim Reset

### Included game actions

* Throttle Delta
* Throttle Cutoff
* Throttle Max
* Pitch
* Roll
* Yaw
* Toggle Precision Mode
* Wheel Steer
* Wheel Brakes
* Wheel Throttle
* Stage
* Toggle Landing Gear
* Toggle Lights
* Toggle SAS
* Toggle RCS
* Translate X
* Translate Y
* Translate Z
* Trigger Action Group 1
* Trigger Action Group 2
* Trigger Action Group 3
* Trigger Action Group 4
* Trigger Action Group 5
* Trigger Action Group 6
* Trigger Action Group 7
* Trigger Action Group 8
* Trigger Action Group 9
* Trigger Action Group 10
* Camera Pitch Gamepad
* Camera Yaw Gamepad
* Show Map
* Quick Save
* Time Warp Decrease
* Time Warp Increase
* Time Warp Stop
* Toggle Pause Menu
* Toggle UI Visibility

### Reset the configuration

To reset the configuration delete `input.json` from `Kerbal Space Program/GameData/Mods/inputbinder`.

### Other

For more information see: https://github.com/Codenade/ksp2-inputbinder/wiki

## How to install

### Prerequisites

* [BepInEx](https://docs.bepinex.dev/articles/user_guide/installation/index.html)

### Instructions

* unpack the contents of `build.zip` into your KSP 2 installation (eg. `C:\Program Files (x86)\Steam\steamapps\common\Kerbal Space Program 2`)

## How to build

### Prerequisites

* [.NET Framework 4.7.2](https://dotnet.microsoft.com/en-us/download/dotnet-framework/net472)
* [Unity 2020.3.33](https://unity.com/releases/editor/archive)
* [BepInEx](https://docs.bepinex.dev/articles/user_guide/installation/index.html) installed to your installation of KSP 2

### Instructions

* clone this repository to a location of your liking

* initialize submodules `git submodule --init --recursive --remote`

* change the path to your unity installation (2020.3.33) after `echo Building assets` in `build.bat`

* add a new environment variable named `KSP2_PATH` with the value set to the path to your installation of KSP 2 (eg. `C:\Program Files (x86)\Steam\steamapps\common\Kerbal Space Program 2`)

* run `build.bat`
33 changes: 33 additions & 0 deletions build.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
@echo off
SETLOCAL EnableDelayedExpansion
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
set "DEL=%%a"
)
powershell rm "$PWD\build\build.zip"
echo Starting assembly build
dotnet build >> %CD%/build.log
if errorlevel 1 goto ERROR
call :colorEcho 0A "Assembly build finished"
echo.
echo Building assets
"%ProgramFiles%\Unity\Hub\Editor\2020.3.33f1\Editor\Unity.exe" -projectPath "%CD%\ksp2-inputbinder-assets\" -quit -batchmode -executeMethod BuildAssets.PerformBuild >> %CD%/build.log
if errorlevel 1 goto ERROR
xcopy "%CD%\ksp2-inputbinder-assets\Library\com.unity.addressables\aa\Windows\" "%CD%\build\\GameData\Mods\inputbinder\addressables\" /Y /I /E >> %CD%/build.log
call :colorEcho 0A "Building assets finished"
echo.
echo Creating build.zip
powershell Compress-Archive -Path "$PWD\build\*" -DestinationPath "$PWD\build\build.zip" >> %CD%/build.log
call :colorEcho 0A "Done"
echo.
exit /b 0

:ERROR
call :colorEcho 0C "Build failed"
echo.
exit /b 1

:colorEcho
echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1i
1 change: 1 addition & 0 deletions ksp2-inputbinder-assets
3 changes: 1 addition & 2 deletions ksp2-inputbinder-bepinex/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@

namespace Codenade.Inputbinder.BepInEx
{
[BepInPlugin("codenade-inputbinder", "codenade-inputbinder", "0.1.1")]//PluginInfo.PLUGIN_GUID, PluginInfo.PLUGIN_NAME, PluginInfo.PLUGIN_VERSION)]
[BepInPlugin("codenade-inputbinder", "codenade-inputbinder", "0.3.0")]//PluginInfo.PLUGIN_GUID, PluginInfo.PLUGIN_NAME, PluginInfo.PLUGIN_VERSION)]
public class Plugin : BaseUnityPlugin
{
private void Awake()
{
// Plugin startup logic
Logger.LogInfo($"Plugin codenade-inputbinder-bepinex is loaded!");
var harmony = new Harmony("codenade-inputbinder");
harmony.PatchAll(typeof(LoadMod));
harmony.PatchAll(typeof(NoControllerAutoremove));
Expand Down
4 changes: 2 additions & 2 deletions ksp2-inputbinder-bepinex/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.1.1.0")]
[assembly: AssemblyFileVersion("0.1.1.0")]
[assembly: AssemblyVersion("0.3.0.0")]
[assembly: AssemblyFileVersion("0.3.0.0")]
8 changes: 4 additions & 4 deletions ksp2-inputbinder-bepinex/ksp2-inputbinder-bepinex.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@
<HintPath>$(KSP2_PATH)\BepInEx\core\BepInEx.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Unity.InputSystem">
<HintPath>$(KSP2_PATH)\KSP2_x64_Data\Managed\Unity.InputSystem.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Unity.InputSystem">
<HintPath>$(KSP2_PATH)\KSP2_x64_Data\Managed\Unity.InputSystem.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="UnityEngine">
<HintPath>$(KSP2_PATH)\KSP2_x64_Data\Managed\UnityEngine.dll</HintPath>
<Private>False</Private>
Expand Down
88 changes: 53 additions & 35 deletions ksp2-inputbinder/AppBarButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,66 +8,84 @@

namespace Codenade.Inputbinder
{
internal class AppBarButton : IDisposable
internal class AppBarButton : MonoBehaviour
{
// TODO: change icon
public bool Created { get { return _wasCreated; } }
public event Action Destroying;

public string Text
{
get { return _button.GetChild("Content").GetChild("TXT-title").GetComponent<TextMeshProUGUI>().text; }
set { _button.GetChild("Content").GetChild("TXT-title").GetComponent<TextMeshProUGUI>().text = value; }
get { return _text.text; }
set { _text.text = value; }
}
public Sprite Icon
{
get { return _button.GetChild("Content").GetChild("GRP-icon").GetChild("ICO-asset").GetComponent<Image>().sprite; }
set { _button.GetChild("Content").GetChild("GRP-icon").GetChild("ICO-asset").GetComponent<Image>().sprite = value; }
get { return _image.sprite; }
set
{
if (value is object)
{
_image.sprite = value;
_iconAsset.SetActive(true);
}
else
{
_iconAsset.SetActive(false);
}
}
}
public bool State
{
get { return _button.GetComponent<ToggleExtended>().isOn; }
set { _button.GetComponent<UIValue_WriteBool_Toggle>().BindValue(new Property<bool>(value)); }
get { return _toggleExtended.isOn; }
set { _writeBool.BindValue(new Property<bool>(value)); }
}

private bool _wasCreated;
private GameObject _button;
private GameObject _iconAsset;
private Image _image;
private UIValue_ReadBool_SetAlpha _buttonExtended;
private ToggleExtended _toggleExtended;
private TextMeshProUGUI _text;
private UIValue_WriteBool_Toggle _writeBool;

public AppBarButton(string id, string text, Action<bool> action = null, Sprite icon = null)
private void OnClick(bool state)
{
_wasCreated = CreateButton(id, text, action, icon, out _button);
_wasCreated &= _button is object;
_buttonExtended.SetValue(false);
}

private static bool CreateButton(string id, string name, Action<bool> action, Sprite icon, out GameObject button)
public static AppBarButton CreateButton(string id, string name, Action<bool> action, Sprite icon = null)
{
var appbargroup = GameObject.Find("GameManager/Default Game Instance(Clone)/UI Manager(Clone)/Scaled Popup Canvas/Container/ButtonBar/BTN-App-Tray/appbar-others-group");
var copyit = appbargroup?.GetChild("BTN-Resource-Manager");
button = null;
if (copyit is null) return false;
var newbutton = UnityEngine.Object.Instantiate(copyit, appbargroup.transform);
var buttonGroup = GameObject.Find("GameManager/Default Game Instance(Clone)/UI Manager(Clone)/Scaled Popup Canvas/Container/ButtonBar/BTN-App-Tray/appbar-others-group");
var copyit = buttonGroup?.GetChild("BTN-Resource-Manager");
if (copyit is null) return null;
var newbutton = Instantiate(copyit, buttonGroup.transform);
newbutton.name = id;
var text = newbutton.GetChild("Content").GetChild("TXT-title").GetComponent<TextMeshProUGUI>();
text.text = name;
var loc = text.gameObject.GetComponent<Localize>();
if (loc is object) UnityEngine.Object.Destroy(loc);
if (icon is object) newbutton.GetChild("Content").GetChild("GRP-icon").GetChild("ICO-asset").GetComponent<Image>().sprite = icon;
//else
//{
// var image = newbutton.GetChild("Content").GetChild("GRP-icon").GetChild("ICO-asset").GetComponent<Image>();
// var sprite = image.sprite;
// sprite = Sprite.Create(Texture2D.blackTexture, sprite.rect, sprite.pivot);
//}
if (loc is object) Destroy(loc);
var toggle = newbutton.GetComponent<ToggleExtended>();
var buttonbehaviour = newbutton.AddComponent<AppBarButton>();
toggle.onValueChanged.AddListener(x => buttonbehaviour.OnClick(x));
if (action is object) toggle.onValueChanged.AddListener(x => action(x));
newbutton.GetComponent<UIValue_WriteBool_Toggle>().BindValue(new Property<bool>(false));
button = newbutton;
return true;
buttonbehaviour._writeBool = newbutton.GetComponent<UIValue_WriteBool_Toggle>();
buttonbehaviour._writeBool.BindValue(new Property<bool>(false));
buttonbehaviour._toggleExtended = toggle;
buttonbehaviour._text = text;
buttonbehaviour._buttonExtended = buttonGroup.GetComponent<UIValue_ReadBool_SetAlpha>();
buttonbehaviour._iconAsset = newbutton.GetChild("Content").GetChild("GRP-icon").GetChild("ICO-asset");
buttonbehaviour._image = buttonbehaviour._iconAsset.GetComponent<Image>();
if (icon is object) buttonbehaviour._image.sprite = icon;
else buttonbehaviour._iconAsset.SetActive(false);
return buttonbehaviour;
}

public void Destroy()
{
Destroy(gameObject);
}

public void Dispose()
private void OnDestroy()
{
_wasCreated = false;
if (_button is object) UnityEngine.Object.Destroy(_button);
_button = null;
Destroying?.Invoke();
}
}
}
Loading

0 comments on commit c07cbf2

Please sign in to comment.