<commit_before>﻿/*
 * Copyright © 2016 - 2017 EDDiscovery development team
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
 * file except in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under
 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
 * ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 * 
 * EDDiscovery is not affiliated with Frontier Developments plc.
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections.Concurrent;
using System.Threading;
using EliteDangerousCore;
using EliteDangerousCore.EDSM;
using EliteDangerousCore.DB;
using System.Diagnostics;


namespace EDDiscovery.UserControls
{
    public partial class UserControlLocalMap : UserControlCommonBase
    {
        #region init

        private string DbSave { get { return "MapPanel" + ((displaynumber > 0) ? displaynumber.ToString() : ""); } }

        private StarDistanceComputer computer;

        public UserControlLocalMap()
        {
            InitializeComponent();
            this.chartMap.MouseWheel += Zoom_MouseWheel;
        }

        private HistoryEntry last_he = null;
        private const double defaultMapMaxRadius = 1000;
        private const double defaultMapMinRadius = 0;
        private int maxitems = 500;
        private System.Windows.Forms.Timer slidetimer;

        public override void Init()
        {
            computer = new StarDistanceComputer();

            slideMaxItems.Value = maxitems = SQLiteConnectionUser.GetSettingInt(DbSave + "MapMaxItems", maxitems);

            textMaxRadius.ValueNoChange = SQLiteConnectionUser.GetSettingDouble(DbSave + "MapMax", defaultMapMaxRadius);
            textMinRadius.ValueNoChange = SQLiteConnectionUser.GetSettingDouble(DbSave + "MapMin", defaultMapMinRadius);
            textMinRadius.SetComparitor(textMaxRadius, -2);     // need to do this after values are set
            textMaxRadius.SetComparitor(textMinRadius, 2);

            uctg.OnTravelSelectionChanged += Uctg_OnTravelSelectionChanged;
            slidetimer = new System.Windows.Forms.Timer();
            slidetimer.Interval = 500;
            slidetimer.Tick += Slidetimer_Tick;

            var style = chartMap.ChartAreas[0].Area3DStyle;
            style.Rotation = Math.Min(180, Math.Max(-180, style.Rotation - (Convert.ToInt32(SQLiteConnectionUser.GetSettingDouble(DbSave + "MapRotationX", xr)))));
            style.Inclination = Math.Min(90, Math.Max(-90, style.Inclination + (Convert.ToInt32(SQLiteConnectionUser.GetSettingDouble(DbSave + "MapRotationY", yr)))));
        }

        public override void ChangeCursorType(IHistoryCursor thc)
        {
            uctg.OnTravelSelectionChanged -= Uctg_OnTravelSelectionChanged;
            uctg = thc;
            uctg.OnTravelSelectionChanged += Uctg_OnTravelSelectionChanged;

            //RefreshMap();
            //ControlReset(chartMap);
        }

        public override void Closing()
        {
            uctg.OnTravelSelectionChanged -= Uctg_OnTravelSelectionChanged;
            computer.ShutDown();
            SQLiteConnectionUser.PutSettingDouble(DbSave + "MapMin", textMinRadius.Value);
            SQLiteConnectionUser.PutSettingDouble(DbSave + "MapMax", textMaxRadius.Value);
            SQLiteConnectionUser.PutSettingInt(DbSave + "MapMaxItems", maxitems);
            SQLiteConnectionUser.PutSettingDouble(DbSave + "MapRotationX", prevxr);
            SQLiteConnectionUser.PutSettingDouble(DbSave + "MapRotationY", prevyr);
        }

        public override void InitialDisplay()
        {
            KickComputation(uctg.GetCurrentHistoryEntry);
        }

        private void Uctg_OnTravelSelectionChanged(HistoryEntry he, HistoryList hl)
        {
            KickComputation(he);
            RefreshMap();
            ControlReset(chartMap);
        }

        #endregion

        #region computer

        private void KickComputation(HistoryEntry he)
        {
            if (he?.System != null && he.System.HasCoordinate)
            {
                computer.CalculateClosestSystems(he.System,
                    (s, d) => BeginInvoke((MethodInvoker)delegate { NewStarListComputed(s, d); }),
                    maxitems, textMinRadius.Value, textMaxRadius.Value, true);
            }
        }

        private void NewStarListComputed(ISystem sys, BaseUtils.SortedListDoubleDuplicate<ISystem> list)      // In UI
        {
            System.Diagnostics.Debug.Assert(Application.MessageLoop);       // check!
            discoveryform.history.CalculateSqDistances(list, sys.X, sys.Y, sys.Z, maxitems, textMinRadius.Value, textMaxRadius.Value, true);
            FillMap(list, sys);
        }

        private void FillMap(SortedList<double, ISystem> csl, ISystem centerSystem)
        {
            SetControlText("3D Map of closest systems from " + centerSystem.Name);

            // Add the current system
            chartMap.Series[50].Points.AddXY(0, 0);
            chartMap.Series[50].ToolTip = centerSystem.Name; // tooltip

            System.Diagnostics.Debug.WriteLine("Max " + textMaxRadius.Value + " Min " + textMinRadius.Value + " Count " + csl.Count()); ;
            if (csl.Count() > 0)
            {
                foreach (KeyValuePair<double, ISystem> tvp in csl)
                {
                    if (tvp.Value != centerSystem)
                    {<commit_msg>Adjusted the LocalMap point size.<commit_after>﻿/*
 * Copyright © 2016 - 2017 EDDiscovery development team
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
 * file except in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under
 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
 * ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 * 
 * EDDiscovery is not affiliated with Frontier Developments plc.
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections.Concurrent;
using System.Threading;
using EliteDangerousCore;
using EliteDangerousCore.EDSM;
using EliteDangerousCore.DB;
using System.Diagnostics;


namespace EDDiscovery.UserControls
{
    public partial class UserControlLocalMap : UserControlCommonBase
    {
        #region init

        private string DbSave { get { return "MapPanel" + ((displaynumber > 0) ? displaynumber.ToString() : ""); } }

        private StarDistanceComputer computer;

        public UserControlLocalMap()
        {
            InitializeComponent();
            this.chartMap.MouseWheel += Zoom_MouseWheel;
        }

        private HistoryEntry last_he = null;
        private const double defaultMapMaxRadius = 1000;
        private const double defaultMapMinRadius = 0;
        private int maxitems = 500;
        private System.Windows.Forms.Timer slidetimer;

        public override void Init()
        {
            computer = new StarDistanceComputer();

            slideMaxItems.Value = maxitems = SQLiteConnectionUser.GetSettingInt(DbSave + "MapMaxItems", maxitems);

            textMaxRadius.ValueNoChange = SQLiteConnectionUser.GetSettingDouble(DbSave + "MapMax", defaultMapMaxRadius);
            textMinRadius.ValueNoChange = SQLiteConnectionUser.GetSettingDouble(DbSave + "MapMin", defaultMapMinRadius);
            textMinRadius.SetComparitor(textMaxRadius, -2);     // need to do this after values are set
            textMaxRadius.SetComparitor(textMinRadius, 2);

            uctg.OnTravelSelectionChanged += Uctg_OnTravelSelectionChanged;
            slidetimer = new System.Windows.Forms.Timer();
            slidetimer.Interval = 500;
            slidetimer.Tick += Slidetimer_Tick;

            var style = chartMap.ChartAreas[0].Area3DStyle;
            style.Rotation = Math.Min(180, Math.Max(-180, style.Rotation - (Convert.ToInt32(SQLiteConnectionUser.GetSettingDouble(DbSave + "MapRotationX", xr)))));
            style.Inclination = Math.Min(90, Math.Max(-90, style.Inclination + (Convert.ToInt32(SQLiteConnectionUser.GetSettingDouble(DbSave + "MapRotationY", yr)))));
        }

        public override void ChangeCursorType(IHistoryCursor thc)
        {
            uctg.OnTravelSelectionChanged -= Uctg_OnTravelSelectionChanged;
            uctg = thc;
            uctg.OnTravelSelectionChanged += Uctg_OnTravelSelectionChanged;

            //RefreshMap();
            //ControlReset(chartMap);
        }

        public override void Closing()
        {
            uctg.OnTravelSelectionChanged -= Uctg_OnTravelSelectionChanged;
            computer.ShutDown();
            SQLiteConnectionUser.PutSettingDouble(DbSave + "MapMin", textMinRadius.Value);
            SQLiteConnectionUser.PutSettingDouble(DbSave + "MapMax", textMaxRadius.Value);
            SQLiteConnectionUser.PutSettingInt(DbSave + "MapMaxItems", maxitems);
            SQLiteConnectionUser.PutSettingDouble(DbSave + "MapRotationX", prevxr);
            SQLiteConnectionUser.PutSettingDouble(DbSave + "MapRotationY", prevyr);
        }

        public override void InitialDisplay()
        {
            KickComputation(uctg.GetCurrentHistoryEntry);
        }

        private void Uctg_OnTravelSelectionChanged(HistoryEntry he, HistoryList hl)
        {
            KickComputation(he);
            RefreshMap();
            ControlReset(chartMap);
        }

        #endregion

        #region computer

        private void KickComputation(HistoryEntry he)
        {
            if (he?.System != null && he.System.HasCoordinate)
            {
                computer.CalculateClosestSystems(he.System,
                    (s, d) => BeginInvoke((MethodInvoker)delegate { NewStarListComputed(s, d); }),
                    maxitems, textMinRadius.Value, textMaxRadius.Value, true);
            }
        }

        private void NewStarListComputed(ISystem sys, BaseUtils.SortedListDoubleDuplicate<ISystem> list)      // In UI
        {
            System.Diagnostics.Debug.Assert(Application.MessageLoop);       // check!
            discoveryform.history.CalculateSqDistances(list, sys.X, sys.Y, sys.Z, maxitems, textMinRadius.Value, textMaxRadius.Value, true);
            FillMap(list, sys);
        }

        private void FillMap(SortedList<double, ISystem> csl, ISystem centerSystem)
        {
            SetControlText("3D Map of closest systems from " + centerSystem.Name);

            for (int s = 0; s <= 100; s++)
            {
               chartMap.Series[s].MarkerSize = 6;
            }
            chartMap.Series[50].MarkerSize = 8;

            // Add the current system
            chartMap.Series[50].Points.AddXY(0, 0);
            chartMap.Series[50].ToolTip = centerSystem.Name; // tooltip

            System.Diagnostics.Debug.WriteLine("Max " + textMaxRadius.Value + " Min " + textMinRadius.Value + " Count " + csl.Count()); ;
            if (csl.Count() > 0)
            {
                foreach (KeyValuePair<double, ISystem> tvp in csl)
                {
                    if (tvp.Value != centerSystem)
                    {
{'fraction_non_alphanumeric': 0.08977387782652717, 'fraction_numerical': 0.00911238609517381, 'mean_word_length': 2.5842153008769277, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2359548', 'n_tokens_mistral': 3260, 'n_tokens_neox': 3093, 'n_words': 735}