Exemple #1
0
        public Vector3?FindOverSystem(int x, int y, out double cursysdistz, StarGrid.TransFormInfo ti,
                                      bool showstars, bool showstations)   // UI Call.
        {
            Debug.Assert(Application.MessageLoop);

            cursysdistz = double.MaxValue;
            Vector3?ret = null;

            if (showstars)                        // populated grid is in this list, so will be checked
            {
                foreach (StarGrid grd in grids)
                {
                    Vector3?cur = grd.FindPoint(x, y, ref cursysdistz, ti);
                    if (cur != null)        // if found one, better than cursysdistz, use it
                    {
                        ret = cur;
                    }
                }
            }
            else if (showstations)
            {
                ret = populatedgrid.FindPoint(x, y, ref cursysdistz, ti);
            }

            return(ret);
        }
Exemple #2
0
        public Vector3?FindOverSystem(int x, int y, out double cursysdistz, StarGrid.TransFormInfo ti)
        {
            cursysdistz = double.MaxValue;
            Vector3?ret = null;

            double w2 = (double)ti.dwidth / 2.0;
            double h2 = (double)ti.dheight / 2.0;

            lock (_starnames)                                   // lock so they can't add anything while we draw
            {
                foreach (StarNames sys in _starnames.Values)
                {
                    if (sys.paintstar != null)
                    {
                        Vector4d syspos = new Vector4d((float)sys.x, (float)sys.y, (float)sys.z, 1.0);
                        Vector4d sysloc = Vector4d.Transform(syspos, ti.resmat);

                        if (sysloc.Z > ti.znear)
                        {
                            Vector2d syssloc   = new Vector2d(((sysloc.X / sysloc.W) + 1.0) * w2 - x, ((sysloc.Y / sysloc.W) + 1.0) * h2 - y);
                            double   sysdistsq = syssloc.X * syssloc.X + syssloc.Y * syssloc.Y;

                            if (sysdistsq < 7.0 * 7.0)
                            {
                                double sysdist = Math.Sqrt(sysdistsq);

                                if ((sysdist + Math.Abs(sysloc.Z * ti.zoom)) < cursysdistz)
                                {
                                    cursysdistz = sysdist + Math.Abs(sysloc.Z * ti.zoom);
                                    ret         = new Vector3((float)sys.x, (float)sys.y, (float)sys.z);
                                }
                            }
                        }
                    }
                }
            }

            return(ret);
        }
Exemple #3
0
        public Vector3?FindOverSystem(int x, int y, out float cursysdistz, StarGrid.TransFormInfo ti)  // FOREGROUND thread may be running
        {
            cursysdistz = float.MaxValue;
            Vector3?ret = null;

            float w2 = (float)ti.dwidth / 2.0F;
            float h2 = (float)ti.dheight / 2.0F;

            foreach (StarNames sys in _starnamesforeground)
            {
                if (sys.paintstar != null)
                {
                    Vector4 syspos = new Vector4(sys.pos.X, sys.pos.Y, sys.pos.Z, 1.0F);
                    Vector4 sysloc = Vector4.Transform(syspos, ti.resmat);

                    if (sysloc.Z > ti.znear)
                    {
                        Vector2 syssloc   = new Vector2(((sysloc.X / sysloc.W) + 1.0F) * w2 - x, ((sysloc.Y / sysloc.W) + 1.0F) * h2 - y);
                        float   sysdistsq = syssloc.X * syssloc.X + syssloc.Y * syssloc.Y;

                        if (sysdistsq < 7.0 * 7.0)
                        {
                            float sysdist = (float)Math.Sqrt(sysdistsq);

                            if ((sysdist + Math.Abs(sysloc.Z * ti.zoom)) < cursysdistz)
                            {
                                cursysdistz = sysdist + (float)Math.Abs(sysloc.Z * ti.zoom);
                                ret         = sys.pos;
                            }
                        }
                    }
                }
            }

            return(ret);
        }
Exemple #4
0
        // used by Starnames in a thread..
        public void GetSystemsInView(ref SortedDictionary <float, StarGrid.InViewInfo> list, double gridlylimit, StarGrid.TransFormInfo ti)
        {
            int idpos = GridId.Id(ti.campos.X, ti.campos.Z);

            foreach (StarGrid grd in grids)                                                      // either we are inside the grid, or close to the centre of another grid..
            {
                if (grd.Id == idpos || grd.DistanceFrom(ti.campos.X, ti.campos.Z) < gridlylimit) // only consider grids which are nearer than this..
                {
                    grd.GetSystemsInView(ref list, ti, (ForceWhite) ? 0x00ffff : 0);
                    //Console.WriteLine("Check grid " + grd.X + "," + grd.Z);
                }
            }

            visitedsystemsgrid.GetSystemsInView(ref list, ti);          // this can be anywhere in space.. so must check
        }
Exemple #5
0
        private void NamedStars() // background thread.. run after timer tick
        {
            try                   // just in case someone tears us down..
            {
                int lylimit = (int)(_starlimitly / _lastcamera.LastZoom);
                lylimit = Math.Max(lylimit, 20);
                //Console.Write("Look down " + _camera_paint_lookdown + " look forward " + _camera_paint_lookforward);
                //Console.Write("Repaint " + _repaintall + " Stars " + _starlimitly + " within " + lylimit + "  ");
                int sqlylimit = lylimit * lylimit;                 // in squared distance limit from viewpoint

                Vector3 modcampos = _lastcamera.CameraPos;
                modcampos.Y = -modcampos.Y;

                StarGrid.TransFormInfo ti = new StarGrid.TransFormInfo(_resmat, _znear, _glControl.Width, _glControl.Height, sqlylimit, modcampos);

                SortedDictionary <float, StarGrid.InViewInfo> inviewlist = new SortedDictionary <float, StarGrid.InViewInfo>(new DuplicateKeyComparer <float>()); // who's in view, sorted by distance

                _stargrids.GetSystemsInView(ref inviewlist, 2000.0, ti);                                                                                          // consider all grids under 2k from current pos.

                float textscalingw = Math.Min(_starnamemaxly, Math.Max(_starnamesizely / _lastcamera.LastZoom, _starnameminly));                                  // per char
                float textscalingh = textscalingw * 4;
                float textoffset   = .20F;
                float starsize     = Math.Min(Math.Max(_lastcamera.LastZoom / 10F, 1.0F), 20F); // Normal stars are at 1F.
                //Console.WriteLine("Per char {0} h {1} sc {2} ", textscalingw, textscalingh, starsize);

                lock (deletelock)                              // can't delete during update, can paint..
                {
                    foreach (StarNames s in _starnames.Values) // all items not processed
                    {
                        s.todispose = true;                    // only items remaining will clear this
                    }
                    int limit   = 1000;                        // max number of stars to show..
                    int painted = 0;

                    using (SQLiteConnectionED cn = new SQLiteConnectionED())
                    {
                        foreach (StarGrid.InViewInfo inview in inviewlist.Values)            // for all in viewport, sorted by distance from camera position
                        {
                            StarNames sys  = null;
                            bool      draw = false;

                            if (_starnames.ContainsKey(inview.position))                   // if already there..
                            {
                                sys           = _starnames[inview.position];
                                sys.todispose = false;                         // forced redraw due to change in orientation, or due to disposal
                                draw          = _flippedorzoomed || (_nameson && sys.newstar == null) || (_discson && sys.nametexture == null);
                                painted++;
                            }
                            else if (painted < limit)
                            {
                                ISystem sc = _formmap.FindSystem(inview.position, cn); // with the open connection, find this star..

                                if (sc != null)                                        // if can't be resolved, ignore
                                {
                                    sys = new StarNames(sc);
                                    lock (_starnames)
                                    {
                                        _starnames.Add(inview.position, sys);               // need to lock over add.. in case display is working
                                    }
                                    draw = true;
                                    painted++;
                                }
                                else
                                {
                                    // Console.WriteLine("Failed to find " + pos.X + "," + pos.Y + "," + pos.Z);
                                }
                            }
                            else
                            {
                                break;      // no point doing any more..  Either the closest ones have been found, or a new one was painted
                            }

                            if (draw)
                            {
                                if (_nameson)
                                {
                                    float width = textscalingw * sys.name.Length;

                                    Bitmap map;                     // now, delete is the only one who removed newtexture
                                                                    // and we are protected against delete..
                                    if (sys.nametexture == null)    // so see if newtexture is there
                                    {
                                        map = DatasetBuilder.DrawString(sys.name, Color.Orange, _starfont);
                                        sys.newnametexture = TexturedQuadData.FromBitmap(map,
                                                                                         new PointData(sys.x, sys.y, sys.z),
                                                                                         _lastcamera.Rotation,
                                                                                         width, textscalingh, textoffset + width / 2, 0);
                                    }
                                    else
                                    {
                                        sys.newnamevertices = TexturedQuadData.CalcVertices(new PointData(sys.x, sys.y, sys.z),
                                                                                            _lastcamera.Rotation,
                                                                                            width, textscalingh, textoffset + width / 2, 0);
                                    }
                                }

                                if (_discson)
                                {
                                    sys.newstar = new PointData(sys.x, sys.y, sys.z, starsize, Color.FromArgb(255, inview.colour & 255, (inview.colour >> 8) & 255, (inview.colour >> 16) & 255));
                                }
                            }
                        }
                    }

                    foreach (StarNames s in _starnames.Values)            // only items above will remain.
                    {
                        s.inview = !s.todispose;                          // copy flag over, causes foreground to start removing them
                    }
                }

                //Console.WriteLine("  " + (Environment.TickCount % 10000) + "Paint " + painted);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine("Exception watcher: " + ex.Message);
                System.Diagnostics.Trace.WriteLine("Trace: " + ex.StackTrace);
            }

            _formmap.Invoke((System.Windows.Forms.MethodInvoker) delegate              // kick the UI thread to process.
            {
                _formmap.ChangeNamedStars();
            });
        }
Exemple #6
0
        private void NamedStars() // background thread.. run after Update.  Thread never deletes, only adds to its own structures
        {
            try                   // just in case someone tears us down..
            {
                int lylimit = (int)(_starlimitly / _lastcamera.LastZoom);
                lylimit = Math.Max(lylimit, 1);
                int sqlylimit = lylimit * lylimit;                 // in squared distance limit from viewpoint

                StarGrid.TransFormInfo ti = new StarGrid.TransFormInfo(_resmat, _znear, _glControl.Width, _glControl.Height, sqlylimit, _lastcamera.LastCameraPos);

                SortedDictionary <float, StarGrid.InViewInfo> inviewlist = new SortedDictionary <float, StarGrid.InViewInfo>(new DuplicateKeyComparer <float>());       // who's in view, sorted by distance

                //Stopwatch sw1 = new Stopwatch();sw1.Start(); Tools.LogToFile(String.Format("starnamesest Estimate at {0} len {1}", ti.campos, sqlylimit));

                _stargrids.GetSystemsInView(ref inviewlist, 2000.0F, ti);            // consider all grids under 2k from current pos.

                //Tools.LogToFile(String.Format("starnamesest Took {0} in view {1}", sw1.ElapsedMilliseconds, inviewlist.Count));

                float textscalingw = Math.Min(_starnamemaxly, Math.Max(_starnamesizely / _lastcamera.LastZoom, _starnameminly)); // per char
                float starsize     = Math.Min(Math.Max(_lastcamera.LastZoom / 10F, 1.0F), 20F);                                  // Normal stars are at 1F.
                //Console.WriteLine("Per char {0} h {1} sc {2} ", textscalingw, textscalingh, starsize);

                foreach (StarNames s in _starnamesbackground.Values) // all items not processed
                {
                    s.updatedinview = false;                         // only items remaining will clear this
                }
                _needrepaint = false;                                // assume nothing changes

                int painted = 0;

                //string res = "";  // used to view whats added/removed/draw..

                foreach (StarGrid.InViewInfo inview in inviewlist.Values)            // for all in viewport, sorted by distance from camera position
                {
                    using (SQLiteConnectionSystem cn = new SQLiteConnectionSystem())
                    {
                        StarNames sys  = null;
                        bool      draw = false;

                        if (_starnamesbackground.ContainsKey(inview.position))                   // if already there..
                        {
                            sys = _starnamesbackground[inview.position];
                            sys.updatedinview = true;

                            draw = (_discson && sys.paintstar == null && sys.newstar == null) ||
                                   (_nameson && ((sys.nametexture == null && sys.newnametexture == null)));

                            painted++;
                        }
                        else if (painted < maxstars)
                        {
                            ISystem sc = _formmap.FindSystem(inview.position, cn); // with the open connection, find this star..

                            if (sc != null)                                        // if can't be resolved, ignore
                            {
                                sys = new StarNames(sc, inview.position);          // we keep position in here using same floats as inview so it will match
                                _starnamesbackground.Add(inview.position, sys);    // add to our database
                                _starnamestoforeground.Add(sys);                   // send to foreground for adding
                                draw = true;
                                painted++;

                                //Tools.LogToFile(String.Format("starnamesest: push {0}", sys.Pos));
                                //res += "N";
                            }
                        }
                        else
                        {
                            break;      // no point doing any more..  got our fill of items
                        }

                        if (draw)
                        {
                            _needrepaint = true;                                            // changed a item.. needs a repaint

                            if (_nameson)
                            {
                                float width = textscalingw * sys.name.Length;

                                Bitmap map = DatasetBuilder.DrawString(sys.name, _namecolour, _starfont);

                                sys.newnametexture = TexturedQuadData.FromBitmap(map,
                                                                                 new PointData(sys.pos.X, sys.pos.Y, sys.pos.Z),
                                                                                 _lastcamera.Rotation,
                                                                                 width, textscalingw * 4.0F, _startextoffset + width / 2, 0);

                                sys.rotation = _lastcamera.Rotation;            // remember when we were when we draw it
                                sys.zoom     = _lastcamera.LastZoom;
                            }

                            if (_discson)
                            {
                                sys.newstar = new PointData(sys.pos.X, sys.pos.Y, sys.pos.Z, starsize, inview.AsColor);
                            }
                        }
                    }
                }

                foreach (StarNames s in _starnamesbackground.Values)              // only items above will remain.
                {
                    //if (s.inview != s.updatedinview) res += (s.updatedinview) ? "+" : "-";

                    _needrepaint = _needrepaint || (s.inview != s.updatedinview); // set if we change our mind on any of the items
                    s.inview     = s.updatedinview;                               // copy flag over, causes foreground to start removing them
                }

                //if ( _needrepaint) Console.WriteLine(String.Format("starnamesest in view  {0} limit {1} repaint {2} {3}", inviewlist.Count, lylimit, _needrepaint, res));

                //Tools.LogToFile(String.Format("starnamesest added all delta {0} topaint {1}", sw1.ElapsedMilliseconds, painted));
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine("Exception watcher: " + ex.Message);
                System.Diagnostics.Trace.WriteLine("Trace: " + ex.StackTrace);
            }

            _formmap.Invoke((System.Windows.Forms.MethodInvoker) delegate              // kick the UI thread to process.
            {
                _formmap.ChangeNamedStars();
            });
        }