<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace principia {
namespace ksp_plugin_adapter {

class FlightPlanner : WindowRenderer {
  public FlightPlanner(PrincipiaPluginAdapter adapter,
                       IntPtr plugin) : base(adapter) {
    adapter_ = adapter;
    plugin_ = plugin;
    window_rectangle_.x = UnityEngine.Screen.width / 2;
    window_rectangle_.y = UnityEngine.Screen.height / 3;
    final_time_ = new DifferentialSlider(
                label            : "Plan length",
                unit             : null,
                log10_lower_rate : Log10TimeLowerRate,
                log10_upper_rate : Log10TimeUpperRate,
                min_value        : 10,
                max_value        : double.PositiveInfinity,
                formatter        : value =>
                    FormatPositiveTimeSpan(
                        TimeSpan.FromSeconds(
                            value - plugin_.FlightPlanGetInitialTime(
                                        vessel_.id.ToString()))));

  }

  protected override void RenderWindow() {
    var old_skin = UnityEngine.GUI.skin;
    UnityEngine.GUI.skin = null;
    if (show_planner_) {
      window_rectangle_ = UnityEngine.GUILayout.Window(
                              id         : this.GetHashCode(),
                              screenRect : window_rectangle_,
                              func       : RenderPlanner,
                              text       : "Flight plan",
                              options    : UnityEngine.GUILayout.MinWidth(500));
    }
    UnityEngine.GUI.skin = old_skin;
  }

  public void RenderButton() {
    var old_skin = UnityEngine.GUI.skin;
    UnityEngine.GUI.skin = null;
    if (UnityEngine.GUILayout.Button("Flight plan...")) {
      show_planner_ = !show_planner_;
    }
    UnityEngine.GUI.skin = old_skin;
  }

  private void RenderPlanner(int window_id) {
    var old_skin = UnityEngine.GUI.skin;
    UnityEngine.GUI.skin = null;
    UnityEngine.GUILayout.BeginVertical();

    if (vessel_ == null || vessel_ != FlightGlobals.ActiveVessel ||
        !plugin_.HasVessel(vessel_.id.ToString())) {
      Reset();
    }

    if (vessel_ != null) {
      string vessel_guid = vessel_.id.ToString();
      if (burn_editors_ == null) {
        if (plugin_.HasVessel(vessel_guid)) {
          if (plugin_.FlightPlanExists(vessel_guid)) {
            burn_editors_ = new List<BurnEditor>();
            for (int i = 0;
                 i < plugin_.FlightPlanNumberOfManoeuvres(vessel_guid);
                 ++i) {
              // Dummy initial time, we call |Reset| immediately afterwards.
              final_time_.value = plugin_.FlightPlanGetFinalTime(vessel_guid);
              burn_editors_.Add(
                  new BurnEditor(adapter_, plugin_, vessel_, initial_time : 0));
              burn_editors_.Last().Reset(
                  plugin_.FlightPlanGetManoeuvre(vessel_guid, i));
            }
          } else {
            if (UnityEngine.GUILayout.Button("Create flight plan")) {
              plugin_.FlightPlanCreate(vessel_guid,
                                       plugin_.CurrentTime() + 1000,
                                       vessel_.GetTotalMass());
              final_time_.value = plugin_.FlightPlanGetFinalTime(vessel_guid);
              Shrink();
            }
          }
        }
      } else {
        if (final_time_.Render(enabled: true)) {
          plugin_.FlightPlanSetFinalTime(vessel_guid, final_time_.value);
          final_time_.value = plugin_.FlightPlanGetFinalTime(vessel_guid);
        }

        AdaptiveStepParameters parameters =
            plugin_.FlightPlanGetAdaptiveStepParameters(vessel_guid);
        UnityEngine.GUILayout.BeginHorizontal();
        UnityEngine.GUILayout.Label("Maximal step count",
                                    UnityEngine.GUILayout.Width(150));
        if (parameters.max_steps <= 100) {
          UnityEngine.GUILayout.Button("min");
        } else if (UnityEngine.GUILayout.Button("-")) {
          parameters.max_steps /= 10;
          plugin_.FlightPlanSetAdaptiveStepParameters(vessel_guid, parameters);
        }
        UnityEngine.GUILayout.TextArea(parameters.max_steps.ToString(),
                                       UnityEngine.GUILayout.Width(150));
        if (parameters.max_steps >= Int64.MaxValue / 10) {
          UnityEngine.GUILayout.Button("max");
        } else if (UnityEngine.GUILayout.Button("+")) {
          parameters.max_steps *= 10;
          plugin_.FlightPlanSetAdaptiveStepParameters(vessel_guid, parameters);
        }
        UnityEngine.GUILayout.EndHorizontal();
        UnityEngine.GUILayout.BeginHorizontal();
        UnityEngine.GUILayout.Label("Tolerance",
                                    UnityEngine.GUILayout.Width(150));
        if (parameters.length_integration_tolerance <= 1e-6) {
          UnityEngine.GUILayout.Button("min");
        } else if (UnityEngine.GUILayout.Button("-")) {
          parameters.length_integration_tolerance /= 2;
          parameters.speed_integration_tolerance /= 2;
          plugin_.FlightPlanSetAdaptiveStepParameters(vessel_guid, parameters);
        }
        UnityEngine.GUILayout.TextArea(
            parameters.length_integration_tolerance.ToString("0.0e0") + " m",
            UnityEngine.GUILayout.Width(150));
        if (parameters.length_integration_tolerance >= 1e6) {
          UnityEngine.GUILayout.Button("max");
        } else if (UnityEngine.GUILayout.Button("+")) {
          parameters.length_integration_tolerance *= 2;
          parameters.speed_integration_tolerance *= 2;
          plugin_.FlightPlanSetAdaptiveStepParameters(vessel_guid, parameters);
        }
        UnityEngine.GUILayout.EndHorizontal();

        if (UnityEngine.GUILayout.Button("Delete flight plan")) {
          plugin_.FlightPlanDelete(vessel_guid);
          Reset();
        } else {
          if (burn_editors_.Count > 0) {
            RenderUpcomingEvents();
          }<commit_msg>prevent accidental deletion of non-epmpty flight plan<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace principia {
namespace ksp_plugin_adapter {

class FlightPlanner : WindowRenderer {
  public FlightPlanner(PrincipiaPluginAdapter adapter,
                       IntPtr plugin) : base(adapter) {
    adapter_ = adapter;
    plugin_ = plugin;
    window_rectangle_.x = UnityEngine.Screen.width / 2;
    window_rectangle_.y = UnityEngine.Screen.height / 3;
    final_time_ = new DifferentialSlider(
                label            : "Plan length",
                unit             : null,
                log10_lower_rate : Log10TimeLowerRate,
                log10_upper_rate : Log10TimeUpperRate,
                min_value        : 10,
                max_value        : double.PositiveInfinity,
                formatter        : value =>
                    FormatPositiveTimeSpan(
                        TimeSpan.FromSeconds(
                            value - plugin_.FlightPlanGetInitialTime(
                                        vessel_.id.ToString()))));

  }

  protected override void RenderWindow() {
    var old_skin = UnityEngine.GUI.skin;
    UnityEngine.GUI.skin = null;
    if (show_planner_) {
      window_rectangle_ = UnityEngine.GUILayout.Window(
                              id         : this.GetHashCode(),
                              screenRect : window_rectangle_,
                              func       : RenderPlanner,
                              text       : "Flight plan",
                              options    : UnityEngine.GUILayout.MinWidth(500));
    }
    UnityEngine.GUI.skin = old_skin;
  }

  public void RenderButton() {
    var old_skin = UnityEngine.GUI.skin;
    UnityEngine.GUI.skin = null;
    if (UnityEngine.GUILayout.Button("Flight plan...")) {
      show_planner_ = !show_planner_;
    }
    UnityEngine.GUI.skin = old_skin;
  }

  private void RenderPlanner(int window_id) {
    var old_skin = UnityEngine.GUI.skin;
    UnityEngine.GUI.skin = null;
    UnityEngine.GUILayout.BeginVertical();

    if (vessel_ == null || vessel_ != FlightGlobals.ActiveVessel ||
        !plugin_.HasVessel(vessel_.id.ToString())) {
      Reset();
    }

    if (vessel_ != null) {
      string vessel_guid = vessel_.id.ToString();
      if (burn_editors_ == null) {
        if (plugin_.HasVessel(vessel_guid)) {
          if (plugin_.FlightPlanExists(vessel_guid)) {
            burn_editors_ = new List<BurnEditor>();
            for (int i = 0;
                 i < plugin_.FlightPlanNumberOfManoeuvres(vessel_guid);
                 ++i) {
              // Dummy initial time, we call |Reset| immediately afterwards.
              final_time_.value = plugin_.FlightPlanGetFinalTime(vessel_guid);
              burn_editors_.Add(
                  new BurnEditor(adapter_, plugin_, vessel_, initial_time : 0));
              burn_editors_.Last().Reset(
                  plugin_.FlightPlanGetManoeuvre(vessel_guid, i));
            }
          } else {
            if (UnityEngine.GUILayout.Button("Create flight plan")) {
              plugin_.FlightPlanCreate(vessel_guid,
                                       plugin_.CurrentTime() + 1000,
                                       vessel_.GetTotalMass());
              final_time_.value = plugin_.FlightPlanGetFinalTime(vessel_guid);
              Shrink();
            }
          }
        }
      } else {
        if (final_time_.Render(enabled: true)) {
          plugin_.FlightPlanSetFinalTime(vessel_guid, final_time_.value);
          final_time_.value = plugin_.FlightPlanGetFinalTime(vessel_guid);
        }

        AdaptiveStepParameters parameters =
            plugin_.FlightPlanGetAdaptiveStepParameters(vessel_guid);
        UnityEngine.GUILayout.BeginHorizontal();
        UnityEngine.GUILayout.Label("Maximal step count",
                                    UnityEngine.GUILayout.Width(150));
        if (parameters.max_steps <= 100) {
          UnityEngine.GUILayout.Button("min");
        } else if (UnityEngine.GUILayout.Button("-")) {
          parameters.max_steps /= 10;
          plugin_.FlightPlanSetAdaptiveStepParameters(vessel_guid, parameters);
        }
        UnityEngine.GUILayout.TextArea(parameters.max_steps.ToString(),
                                       UnityEngine.GUILayout.Width(150));
        if (parameters.max_steps >= Int64.MaxValue / 10) {
          UnityEngine.GUILayout.Button("max");
        } else if (UnityEngine.GUILayout.Button("+")) {
          parameters.max_steps *= 10;
          plugin_.FlightPlanSetAdaptiveStepParameters(vessel_guid, parameters);
        }
        UnityEngine.GUILayout.EndHorizontal();
        UnityEngine.GUILayout.BeginHorizontal();
        UnityEngine.GUILayout.Label("Tolerance",
                                    UnityEngine.GUILayout.Width(150));
        if (parameters.length_integration_tolerance <= 1e-6) {
          UnityEngine.GUILayout.Button("min");
        } else if (UnityEngine.GUILayout.Button("-")) {
          parameters.length_integration_tolerance /= 2;
          parameters.speed_integration_tolerance /= 2;
          plugin_.FlightPlanSetAdaptiveStepParameters(vessel_guid, parameters);
        }
        UnityEngine.GUILayout.TextArea(
            parameters.length_integration_tolerance.ToString("0.0e0") + " m",
            UnityEngine.GUILayout.Width(150));
        if (parameters.length_integration_tolerance >= 1e6) {
          UnityEngine.GUILayout.Button("max");
        } else if (UnityEngine.GUILayout.Button("+")) {
          parameters.length_integration_tolerance *= 2;
          parameters.speed_integration_tolerance *= 2;
          plugin_.FlightPlanSetAdaptiveStepParameters(vessel_guid, parameters);
        }
        UnityEngine.GUILayout.EndHorizontal();

        if (burn_editors_.Count == 0 && 
            UnityEngine.GUILayout.Button("Delete flight plan")) {
          plugin_.FlightPlanDelete(vessel_guid);
          Reset();
        } else {
          if (burn_editors_.Count > 0) {
            RenderUpcomingEvents();
          }
{'fraction_non_alphanumeric': 0.09680335278165832, 'fraction_numerical': 0.009285890377187937, 'mean_word_length': 1.9553181155900923, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2539257', 'n_tokens_mistral': 3384, 'n_tokens_neox': 3235, 'n_words': 516}