<commit_before>using IOUtils = KSP.IO.IOUtils;
using NavBall = KSP.UI.Screens.Flight.NavBall;

using UnityEngine;
using UnityEngine.EventSystems;

[KSPAddon(KSPAddon.Startup.Flight, false)]
public class NavBallAttacher : MonoBehaviour
{
	void Start()
	{
		/* When we speak of dragging the navball, what we really want
		 * is to drag the entire SAS/navball/maneuver control panel.
		 * It has the type "IVAEVACollapseGroup" but I can't figure out
		 * where that's defined so I can't search for it. Taking the
		 * parent of the NavBall works just as well.
		 *
		 * This doesn't drag the (invisible) frame around the control
		 * cluster, but that doesn't seem to hurt anything.
		 */
		GameObject navball = FindObjectOfType<NavBall>().gameObject;
		GameObject cluster = navball.transform.parent.gameObject;
		cluster.AddComponent<NavBallDrag>();
	}
}
public class NavBallDrag : MonoBehaviour, IBeginDragHandler, IDragHandler
{
	/* This component makes the navball draggable. It works by recording
	 * the pointer and ball's position when a drag starts, then moving the
	 * ball however far the pointer moved along the X axis.
	 *
	 * Simply setting the ball to the pointer's position doesn't work; the
	 * pointer and ball have different coordinate origins. The pointer's
	 * X:0 is the left edge of the screen; The ball's X:0 is the center.
	 *
	 * Restricting movement to the X axis makes the ball frame slide along
	 * the bottom edge of the screen.
	 */

	[Persistent]
	private float NAVBALL_XCOORD; // used only for load/save

	private Vector2 dragstart;
	private Vector3 ballstart;

	const string modname = "Draggable Navball";
	const string cfgfile = "DraggableNavball.cfg";

	void Start()
	{
		string path = IOUtils.GetFilePathFor(this.GetType(), cfgfile);
		ConfigNode config = ConfigNode.Load(path);
		ConfigNode.LoadObjectFromConfig(this, config);
		xpos = NAVBALL_XCOORD;
	}

	public float xpos
	{
		get { return transform.position.x; }
		set
		{
			Vector3 newpos = transform.position;
			newpos.x = value;
			transform.position = newpos;
			GameSettings.UI_POS_NAVBALL = value * 2 / GameSettings.SCREEN_RESOLUTION_WIDTH;
		}
	}

	public void OnBeginDrag(PointerEventData evtdata)
	{
		dragstart = evtdata.position;
		ballstart = transform.position;
	}
	public void OnDrag(PointerEventData evtdata)
	{
		Vector2 dragdist = evtdata.position - dragstart;
		xpos = ballstart.x + dragdist.x;
	}
	public void OnDestroy()
	{
		NAVBALL_XCOORD = xpos;
		ConfigNode config = ConfigNode.CreateConfigFromObject(this);
		string path = IOUtils.GetFilePathFor(this.GetType(), cfgfile);
		config.Save(path);
	}
}<commit_msg>Comment everything<commit_after>using IOUtils = KSP.IO.IOUtils;
using NavBall = KSP.UI.Screens.Flight.NavBall;

using UnityEngine;
using UnityEngine.EventSystems;

/* Two classes are needed for DraggableNavball. One is the component that
 * implements the drag behavior. I can't work out how to make it add itself
 * to the navball, so a second class (NavBallAttacher) is rigged to run at
 * flight startup solely to add the component.
 *
 * I feel like there should be a cleaner way to do this but haven't found it
 * yet.
 */

[KSPAddon(KSPAddon.Startup.Flight, false)]
public class NavBallAttacher : MonoBehaviour
{
	void Start()
	{
		/* When we speak of dragging the navball, what we really want
		 * is to drag the entire SAS/navball/maneuver control cluster.
		 * It has the type "IVAEVACollapseGroup" but I can't figure out
		 * where that's defined so I can't search for it. Taking the
		 * parent of the NavBall works just as well.
		 *
		 * This doesn't drag the (invisible) frame around the navball's
		 * control cluster, but that doesn't seem to hurt anything.
		 */
		GameObject navball = FindObjectOfType<NavBall>().gameObject;
		GameObject cluster = navball.transform.parent.gameObject;
		cluster.AddComponent<NavBallDrag>();
	}
}
public class NavBallDrag : MonoBehaviour, IBeginDragHandler, IDragHandler
{
	/* This component makes the navball draggable. Movement is restricted
	 * to the x axis, so the ball slides along the bottom edge of the
	 * screen.
	 */

	/* I can't figure out how to make ConfigNode.Load do the right thing
	 * with C# properties, so NAVBALL_COORD gets mapped to a private
	 * variable that serves just as a place to keep save-load data.
	 */
	[Persistent]
	private float NAVBALL_XCOORD;

	private Vector2 dragstart;
	private Vector3 ballstart;

	const string cfgfile = "DraggableNavball.cfg";

	void Start()
	{
		string path = IOUtils.GetFilePathFor(this.GetType(), cfgfile);
		ConfigNode config = ConfigNode.Load(path);
		ConfigNode.LoadObjectFromConfig(this, config);
		xpos = NAVBALL_XCOORD;
	}

	public float xpos
	{
		/* Assigning to xpos moves the navball. Don't do it any other
		 * way; stuff necessary for consistency happens here.
		 */
		get { return transform.position.x; }
		set
		{
			Vector3 newpos = transform.position;
			newpos.x = value;
			transform.position = newpos;

			/* Sometimes outside forces set the navball's
			 * position. They seem to use GameSettings.UI_POS_NAVBALL
			 * to get it, so let's override that. UI_POS_NAVBALL
			 * uses a -1:1 range, while we have a pixel offset
			 * from center, so a conversion is necessary.
			 */
			GameSettings.UI_POS_NAVBALL = value * 2 / GameSettings.SCREEN_RESOLUTION_WIDTH;
		}
	}

	public void OnBeginDrag(PointerEventData evtdata)
	{
		/* Record the pointer and ball positions when dragging starts.
		 * We'll need them in OnDrag. */
		dragstart = evtdata.position;
		ballstart = transform.position;
	}
	public void OnDrag(PointerEventData evtdata)
	{
		/* See how far the pointer has dragged and offset the navball
		 * from its original position by that much.
		 *
		 * This might be cleaner with a frame-by-frame delta but I
		 * worry about small errors adding up.
		 */
		Vector2 dragdist = evtdata.position - dragstart;
		xpos = ballstart.x + dragdist.x;
	}
	public void OnDestroy()
	{
		// I'm not sure when you're "supposed" to save plugin data.
		// This was the best I could come up with.
		NAVBALL_XCOORD = xpos;
		ConfigNode config = ConfigNode.CreateConfigFromObject(this);
		string path = IOUtils.GetFilePathFor(this.GetType(), cfgfile);
		config.Save(path);
	}
}
{'fraction_non_alphanumeric': 0.08165889728339495, 'fraction_numerical': 0.002250442051117184, 'mean_word_length': 3.800925925925926, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '479727', 'n_tokens_mistral': 2090, 'n_tokens_neox': 1900, 'n_words': 682}