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

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 AltimeterAttacher : 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 altimeter = GameObject.Find("Altimeter");
		GameObject slideframeparent = altimeter.transform.parent.gameObject;
		GameObject cluster = slideframeparent.transform.parent.gameObject;
		cluster.AddComponent<AltDrag>();
	}
}
public class AltDrag : 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 ALTIMETER_XCOORD;

	private Vector2 dragstart;
	private Vector3 altstart;

	const string cfgfile = "DraggableAltimeter.cfg";

	void Start()
	{
		string path = IOUtils.GetFilePathFor(this.GetType(), cfgfile);
		ConfigNode config = ConfigNode.Load(path);
		ConfigNode.LoadObjectFromConfig(this, config);
		xpos = ALTIMETER_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
		{
			// Prevent the ball from ending up offscreen where we
			// can't drag it back.
			int right_edge = GameSettings.SCREEN_RESOLUTION_WIDTH / 2;
			int left_edge  = -right_edge;
			if (value > right_edge) value = right_edge;
			if (value < left_edge)  value = left_edge;

			// Move the ball.
			Vector3 newpos = transform.position;
			newpos.x = value;
			transform.position = newpos;
		}
	}

	public void OnBeginDrag(PointerEventData evtdata)
	{
		/* Record the pointer and ball positions when dragging starts.
		 * We'll need them in OnDrag. */
		dragstart = evtdata.position;
		altstart = transform.position;
	}
	public void OnDrag(PointerEventData evtdata)
	{
		/* See how far the pointer has dragged and offset the navball<commit_msg>Update class names and comment documentation<commit_after>using IOUtils = KSP.IO.IOUtils;
using Altimeter = KSP.UI.Screens.Flight.AltitudeTumbler;

using UnityEngine;
using UnityEngine.EventSystems;

/* How this works: There is one class, the AltDrag component, that implements
 * drag behavior for the altimeter. The second class, the Attacher, spawns when
 * flight starts and adds the component to the altimeter. I can't work out how
 * to make the dragger attach itself. I feel like there should be a cleaner way
 * to do this but I haven't found it yet.
 */

[KSPAddon(KSPAddon.Startup.Flight, false)]
public class Attacher : MonoBehaviour
{
	void Start()
	{
		/* Rather than only dragging the altimeter, we really want to
		 * drag the entire control cluster it's part of.  So the
		 * dragger gets attached to that. It's the grandparent of the
		 * altimeter object. (Found this using DebugStuff for the
		 * curious)
		 */
		GameObject altimeter = GameObject.Find("Altimeter");
		GameObject slideframeparent = altimeter.transform.parent.gameObject;
		GameObject cluster = slideframeparent.transform.parent.gameObject;
		cluster.AddComponent<Dragger>();
		Destroy(gameObject);
	}
}
public class Dragger : MonoBehaviour, IBeginDragHandler, IDragHandler
{
	/* This makes the altimeter draggable. Movement is restricted to the x
	 * axis, so it slides along the edge of the screen.
	 */

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

	private Vector2 dragstart;
	private Vector3 altstart;

	const string cfgfile = "DraggableAltimeter.cfg";

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

	public float xpos
	{
		/* Assigning to xpos moves the altimeter. Don't do it any other
		 * way; stuff necessary for consistency happens here.
		 */
		get { return transform.position.x; }
		set
		{
			// Prevent the altimeter from ending up offscreen where
			// we can't drag it back.
			int right_edge = GameSettings.SCREEN_RESOLUTION_WIDTH / 2;
			int left_edge  = -right_edge;
			if (value > right_edge) value = right_edge;
			if (value < left_edge)  value = left_edge;

			// Move the altimeter.
			Vector3 newpos = transform.position;
			newpos.x = value;
			ALTIMETER_XCOORD = value;
			transform.position = newpos;
		}
	}

	public void OnBeginDrag(PointerEventData evtdata)
	{
		/* Record the pointer and altimeter positions when dragging
		 * starts.  We'll need them in OnDrag. */
		dragstart = evtdata.position;
		altstart = transform.position;
	}
	public void OnDrag(PointerEventData evtdata)
	{
		/* See how far the pointer has dragged and offset the altimeter
{'fraction_non_alphanumeric': 0.07775919732441472, 'fraction_numerical': 0.0013377926421404682, 'mean_word_length': 3.773343974461293, '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': '2068572', 'n_tokens_mistral': 1950, 'n_tokens_neox': 1799, 'n_words': 673}