Skip to content

Instantly share code, notes, and snippets.

@gotmachine
Created November 18, 2021 23:59
Show Gist options
  • Save gotmachine/861e06021e8cc7cb9c09fa5f0661252e to your computer and use it in GitHub Desktop.
Save gotmachine/861e06021e8cc7cb9c09fa5f0661252e to your computer and use it in GitHub Desktop.
Create a completely custom PAW control for KSP
using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
namespace UINumericFloatEdit
{
[KSPAddon(KSPAddon.Startup.FlightAndEditor, false)]
public class CustomPartActionControlSpawner : MonoBehaviour
{
public static GameObject prefabsHolder;
private static UIPartActionCustomFloatEdit prefab;
private static FieldInfo fieldControlTypesInfo = typeof(UIPartActionController).GetField("fieldControlTypes", BindingFlags.Instance | BindingFlags.NonPublic);
// this must run after UIPartActionController.Start()
void Update()
{
if (prefabsHolder == null)
{
prefabsHolder = new GameObject("PartActionCustomPrefabs");
DontDestroyOnLoad(prefabsHolder);
}
if (prefab == null)
{
foreach (UIPartActionFieldItem uiPartActionFieldItem in UIPartActionController.Instance.fieldPrefabs)
{
if (uiPartActionFieldItem is UIPartActionFloatEdit stockComponentPrefab)
{
prefab = UIPartActionCustomFloatEdit.CreatePrefab(stockComponentPrefab);
}
}
}
List<Type> fieldControlTypes = (List<Type>)fieldControlTypesInfo.GetValue(UIPartActionController.Instance);
if (fieldControlTypes == null)
return;
UIPartActionController.Instance.fieldPrefabs.Add(prefab);
fieldControlTypes.Add(typeof(UI_CustomFloatEdit));
Destroy(this); // we're not needed anymore
}
}
public class UI_CustomFloatEdit : UI_FloatEdit { }
[UI_CustomFloatEdit]
public class UIPartActionCustomFloatEdit : UIPartActionFloatEdit
{
public static UIPartActionCustomFloatEdit CreatePrefab(UIPartActionFloatEdit original)
{
GameObject prefab = Instantiate(original.gameObject);
prefab.name = nameof(UIPartActionCustomFloatEdit);
UIPartActionFloatEdit stockComponent = prefab.GetComponent<UIPartActionFloatEdit>();
UIPartActionCustomFloatEdit modifiedComponent = prefab.AddComponent<UIPartActionCustomFloatEdit>();
modifiedComponent.fieldName = stockComponent.fieldName;
modifiedComponent.fieldValue = stockComponent.fieldValue;
modifiedComponent.incLarge = stockComponent.incLarge;
modifiedComponent.incSmall = stockComponent.incSmall;
modifiedComponent.decLarge = stockComponent.decLarge;
modifiedComponent.decSmall = stockComponent.decSmall;
modifiedComponent.slider = stockComponent.slider;
Destroy(stockComponent);
prefab.transform.SetParent(CustomPartActionControlSpawner.prefabsHolder.transform);
DontDestroyOnLoad(prefab);
return modifiedComponent;
}
}
public class CustomFloatEditTestModule : PartModule
{
[KSPField(guiActive = true, guiActiveEditor = true, guiName = "custom floatEdit")]
[UI_CustomFloatEdit(minValue = 0f, maxValue = 10f, incrementSmall = 0.5f, incrementLarge = 1f, sigFigs = 1)]
public float modified = 5f;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment