public void ResetList()
        {
            List<Planet> pList = new List<Planet>();
            foreach (ScrollList.Entry entry in this.PlanetSL.Entries)
            {
                pList.Add((entry.item as PlanetListScreenEntry).planet);
            }
            this.PlanetSL.Entries.Clear();
            this.PlanetSL.Copied.Clear();
            this.PlanetSL.indexAtTop = 0;
            if (this.LastSorted == null)
            {
                foreach (Planet p in this.planets)
                {
                    if (this.HideOwned && p.Owner != null || this.HideUninhab && !p.habitable)
                    {
                        continue;
                    }
                    PlanetListScreenEntry entry = new PlanetListScreenEntry(p, this.eRect.X + 22, this.leftRect.Y + 20, this.EMenu.Menu.Width - 30, 40, this);
                    this.PlanetSL.AddItem(entry);
                }
            }
            else
            {
                if (this.LastSorted.Text == this.sb_Sys.Text)   // (this.sb_Sys == this.LastSorted)
                {
                    if (!this.sb_Sys.Ascending)
                    {
                        IOrderedEnumerable<Planet> sortedList =
                            from planet in this.planets
                            orderby planet.system.Name descending
                            select planet;
                        foreach (Planet p in sortedList)
                        {
                            if (this.HideOwned && p.Owner != null || this.HideUninhab && !p.habitable)
                            {
                                continue;
                            }
                            PlanetListScreenEntry entry = new PlanetListScreenEntry(p, this.eRect.X + 22, this.leftRect.Y + 20, this.EMenu.Menu.Width - 30, 40, this);
                            this.PlanetSL.AddItem(entry);
                        }
                    }
                    else
                    {
                        IOrderedEnumerable<Planet> sortedList =
                            from planet in this.planets
                            orderby planet.system.Name
                            select planet;
                        foreach (Planet p in sortedList)
                        {
                            if (this.HideOwned && p.Owner != null || this.HideUninhab && !p.habitable)
                            {
                                continue;
                            }
                            PlanetListScreenEntry entry = new PlanetListScreenEntry(p, this.eRect.X + 22, this.leftRect.Y + 20, this.EMenu.Menu.Width - 30, 40, this);
                            this.PlanetSL.AddItem(entry);
                        }
                    }
                }
                if (this.sb_Name.Text == this.LastSorted.Text)
                {
                    if (!this.sb_Name.Ascending)
                    {
                        IOrderedEnumerable<Planet> sortedList =
                            from planet in this.planets
                            orderby planet.Name descending
                            select planet;
                        foreach (Planet p in sortedList)
                        {
                            if (this.HideOwned && p.Owner != null || this.HideUninhab && !p.habitable)
                            {
                                continue;
                            }
                            PlanetListScreenEntry entry = new PlanetListScreenEntry(p, this.eRect.X + 22, this.leftRect.Y + 20, this.EMenu.Menu.Width - 30, 40, this);
                            this.PlanetSL.AddItem(entry);
                        }
                    }
                    else
                    {
                        IOrderedEnumerable<Planet> sortedList =
                            from planet in this.planets
                            orderby planet.Name
                            select planet;
                        foreach (Planet p in sortedList)
                        {
                            if (this.HideOwned && p.Owner != null || this.HideUninhab && !p.habitable)
                            {
                                continue;
                            }
                            PlanetListScreenEntry entry = new PlanetListScreenEntry(p, this.eRect.X + 22, this.leftRect.Y + 20, this.EMenu.Menu.Width - 30, 40, this);
                            this.PlanetSL.AddItem(entry);
                        }
                    }
                }
                if (this.sb_Fert.Text == this.LastSorted.Text)
                {
                    AudioManager.PlayCue("blip_click");

                    this.PlanetSL.Entries.Clear();
                    this.PlanetSL.Copied.Clear();
                    if (!this.sb_Fert.Ascending)
                    {
                        IOrderedEnumerable<Planet> sortedList =
                            from planet in this.planets
                            orderby planet.Fertility descending
                            select planet;
                        foreach (Planet p in sortedList)
                        {
                            if (this.HideOwned && p.Owner != null || this.HideUninhab && !p.habitable)
                            {
                                continue;
                            }
                            PlanetListScreenEntry entry = new PlanetListScreenEntry(p, this.eRect.X + 22, this.leftRect.Y + 20, this.EMenu.Menu.Width - 30, 40, this);
                            this.PlanetSL.AddItem(entry);
                        }
                    }
                    else
                    {
                        IOrderedEnumerable<Planet> sortedList =
                            from planet in this.planets
                            orderby planet.Fertility
                            select planet;
                        foreach (Planet p in sortedList)
                        {
                            if (this.HideOwned && p.Owner != null || this.HideUninhab && !p.habitable)
                            {
                                continue;
                            }
                            PlanetListScreenEntry entry = new PlanetListScreenEntry(p, this.eRect.X + 22, this.leftRect.Y + 20, this.EMenu.Menu.Width - 30, 40, this);
                            this.PlanetSL.AddItem(entry);
                        }
                    }
                }
                if (this.LastSorted.Text == this.sb_Rich.Text   )//1this.sb_Rich == this.LastSorted)
                {
                    this.LastSorted = this.sb_Rich;
                    AudioManager.PlayCue("blip_click");
                    //this.sb_Rich.Ascending = !this.sb_Rich.Ascending;
                    this.PlanetSL.Entries.Clear();
                    this.PlanetSL.Copied.Clear();
                    if (!this.sb_Rich.Ascending)
                    {
                        IOrderedEnumerable<Planet> sortedList =
                            from planet in this.planets
                            orderby planet.MineralRichness descending
                            select planet;
                        foreach (Planet p in sortedList)
                        {
                            if (this.HideOwned && p.Owner != null || this.HideUninhab && !p.habitable)
                            {
                                continue;
                            }
                            PlanetListScreenEntry entry = new PlanetListScreenEntry(p, this.eRect.X + 22, this.leftRect.Y + 20, this.EMenu.Menu.Width - 30, 40, this);
                            this.PlanetSL.AddItem(entry);
                        }
                    }
                    else
                    {
                        IOrderedEnumerable<Planet> sortedList =
                            from planet in this.planets
                            orderby planet.MineralRichness
                            select planet;
                        foreach (Planet p in sortedList)
                        {
                            if (this.HideOwned && p.Owner != null || this.HideUninhab && !p.habitable)
                            {
                                continue;
                            }
                            PlanetListScreenEntry entry = new PlanetListScreenEntry(p, this.eRect.X + 22, this.leftRect.Y + 20, this.EMenu.Menu.Width - 30, 40, this);
                            this.PlanetSL.AddItem(entry);
                        }
                    }
                }
                if (this.sb_Pop.Text == this.LastSorted.Text)
                {
                    this.LastSorted = this.sb_Pop;
                    AudioManager.PlayCue("blip_click");
                    //this.sb_Pop.Ascending = !this.sb_Pop.Ascending;
                    this.PlanetSL.Entries.Clear();
                    this.PlanetSL.Copied.Clear();
                    if (!this.sb_Pop.Ascending)
                    {
                        IOrderedEnumerable<Planet> sortedList =
                            from planet in this.planets
                            orderby planet.MaxPopulation descending
                            select planet;
                        foreach (Planet p in sortedList)
                        {
                            if (this.HideOwned && p.Owner != null || this.HideUninhab && !p.habitable)
                            {
                                continue;
                            }
                            PlanetListScreenEntry entry = new PlanetListScreenEntry(p, this.eRect.X + 22, this.leftRect.Y + 20, this.EMenu.Menu.Width - 30, 40, this);
                            this.PlanetSL.AddItem(entry);
                        }
                    }
                    else
                    {
                        IOrderedEnumerable<Planet> sortedList =
                            from planet in this.planets
                            orderby planet.MaxPopulation
                            select planet;
                        foreach (Planet p in sortedList)
                        {
                            if (this.HideOwned && p.Owner != null || this.HideUninhab && !p.habitable)
                            {
                                continue;
                            }
                            PlanetListScreenEntry entry = new PlanetListScreenEntry(p, this.eRect.X + 22, this.leftRect.Y + 20, this.EMenu.Menu.Width - 30, 40, this);
                            this.PlanetSL.AddItem(entry);
                        }
                    }
                }
                if (this.sb_Owned.Text == this.LastSorted.Text)
                {
                    this.LastSorted = this.sb_Owned;
                    AudioManager.PlayCue("blip_click");
                    //this.sb_Owned.Ascending = !this.sb_Owned.Ascending;
                    this.PlanetSL.Entries.Clear();
                    this.PlanetSL.Copied.Clear();
                    if (!this.sb_Owned.Ascending)
                    {
                        IOrderedEnumerable<Planet> sortedList =
                            from planet in this.planets
                            orderby planet.GetOwnerName() descending
                            select planet;
                        foreach (Planet p in sortedList)
                        {
                            if (this.HideOwned && p.Owner != null || this.HideUninhab && !p.habitable)
                            {
                                continue;
                            }
                            PlanetListScreenEntry entry = new PlanetListScreenEntry(p, this.eRect.X + 22, this.leftRect.Y + 20, this.EMenu.Menu.Width - 30, 40, this);
                            this.PlanetSL.AddItem(entry);
                        }
                    }
                    else
                    {
                        IOrderedEnumerable<Planet> sortedList =
                            from planet in this.planets
                            orderby planet.GetOwnerName()
                            select planet;
                        foreach (Planet p in sortedList)
                        {
                            if (this.HideOwned && p.Owner != null || this.HideUninhab && !p.habitable)
                            {
                                continue;
                            }
                            PlanetListScreenEntry entry = new PlanetListScreenEntry(p, this.eRect.X + 22, this.leftRect.Y + 20, this.EMenu.Menu.Width - 30, 40, this);
                            this.PlanetSL.AddItem(entry);
                        }
                    }
                }
            }
            if (this.PlanetSL.Entries.Count <= 0)
            {
                this.SelectedPlanet = null;
                return;
            }
            this.SelectedPlanet = (this.PlanetSL.Entries[this.PlanetSL.indexAtTop].item as PlanetListScreenEntry).planet;

            //this.empUI.empire.data.PLSort = this.LastSorted;
        }
        public override void HandleInput(InputState input)
        {
            //this.LastSorted = empUI.empire.data.PLSort;
            if (this.PlanetSL.Entries.Count == 0)
                this.ResetList();
            this.PlanetSL.HandleInput(input);
            this.cb_hideOwned.HandleInput(input);
            this.cb_hideUninhabitable.HandleInput(input);
            if (this.sb_Sys.HandleInput(input))
            {
                this.LastSorted = this.sb_Sys;
                AudioManager.PlayCue("blip_click");
                this.sb_Sys.Ascending = !this.sb_Sys.Ascending;
                this.PlanetSL.Entries.Clear();
                this.PlanetSL.Copied.Clear();
                if (!this.sb_Sys.Ascending)
                {
                    IOrderedEnumerable<Planet> sortedList =
                        from planet in this.planets
                        orderby planet.system.Name descending
                        select planet;
                    foreach (Planet p in sortedList)
                    {
                        if (this.HideOwned && p.Owner != null || this.HideUninhab && !p.habitable)
                        {
                            continue;
                        }
                        PlanetListScreenEntry entry = new PlanetListScreenEntry(p, this.eRect.X + 22, this.leftRect.Y + 20, this.EMenu.Menu.Width - 30, 40, this);
                        this.PlanetSL.AddItem(entry);
                    }
                }
                else
                {
                    IOrderedEnumerable<Planet> sortedList =
                        from planet in this.planets
                        orderby planet.system.Name
                        select planet;
                    foreach (Planet p in sortedList)
                    {
                        if (this.HideOwned && p.Owner != null || this.HideUninhab && !p.habitable)
                        {
                            continue;
                        }
                        PlanetListScreenEntry entry = new PlanetListScreenEntry(p, this.eRect.X + 22, this.leftRect.Y + 20, this.EMenu.Menu.Width - 30, 40, this);
                        this.PlanetSL.AddItem(entry);
                    }
                }
            }
            if (this.sb_Name.HandleInput(input))
            {
                this.LastSorted = this.sb_Name;
                AudioManager.PlayCue("blip_click");
                this.sb_Name.Ascending = !this.sb_Name.Ascending;
                this.PlanetSL.Entries.Clear();
                this.PlanetSL.Copied.Clear();
                if (!this.sb_Name.Ascending)
                {
                    IOrderedEnumerable<Planet> sortedList =
                        from planet in this.planets
                        orderby planet.Name descending
                        select planet;
                    foreach (Planet p in sortedList)
                    {
                        if (this.HideOwned && p.Owner != null || this.HideUninhab && !p.habitable)
                        {
                            continue;
                        }
                        PlanetListScreenEntry entry = new PlanetListScreenEntry(p, this.eRect.X + 22, this.leftRect.Y + 20, this.EMenu.Menu.Width - 30, 40, this);
                        this.PlanetSL.AddItem(entry);
                    }
                }
                else
                {
                    IOrderedEnumerable<Planet> sortedList =
                        from planet in this.planets
                        orderby planet.Name
                        select planet;
                    foreach (Planet p in sortedList)
                    {
                        if (this.HideOwned && p.Owner != null || this.HideUninhab && !p.habitable)
                        {
                            continue;
                        }
                        PlanetListScreenEntry entry = new PlanetListScreenEntry(p, this.eRect.X + 22, this.leftRect.Y + 20, this.EMenu.Menu.Width - 30, 40, this);
                        this.PlanetSL.AddItem(entry);
                    }
                }
            }
            if (this.sb_Fert.HandleInput(input))
            {
                this.LastSorted = this.sb_Fert;
                AudioManager.PlayCue("blip_click");
                this.sb_Fert.Ascending = !this.sb_Fert.Ascending;
                this.PlanetSL.Entries.Clear();
                this.PlanetSL.Copied.Clear();
                if (!this.sb_Fert.Ascending)
                {
                    IOrderedEnumerable<Planet> sortedList =
                        from planet in this.planets
                        orderby planet.Fertility descending
                        select planet;
                    foreach (Planet p in sortedList)
                    {
                        if (this.HideOwned && p.Owner != null || this.HideUninhab && !p.habitable)
                        {
                            continue;
                        }
                        PlanetListScreenEntry entry = new PlanetListScreenEntry(p, this.eRect.X + 22, this.leftRect.Y + 20, this.EMenu.Menu.Width - 30, 40, this);
                        this.PlanetSL.AddItem(entry);
                    }
                }
                else
                {
                    IOrderedEnumerable<Planet> sortedList =
                        from planet in this.planets
                        orderby planet.Fertility
                        select planet;
                    foreach (Planet p in sortedList)
                    {
                        if (this.HideOwned && p.Owner != null || this.HideUninhab && !p.habitable)
                        {
                            continue;
                        }
                        PlanetListScreenEntry entry = new PlanetListScreenEntry(p, this.eRect.X + 22, this.leftRect.Y + 20, this.EMenu.Menu.Width - 30, 40, this);
                        this.PlanetSL.AddItem(entry);
                    }
                }
            }
            if (this.sb_Rich.HandleInput(input))
            {
                this.LastSorted = this.sb_Rich;
                AudioManager.PlayCue("blip_click");
                this.sb_Rich.Ascending = !this.sb_Rich.Ascending;
                this.PlanetSL.Entries.Clear();
                this.PlanetSL.Copied.Clear();
                if (!this.sb_Rich.Ascending)
                {
                    IOrderedEnumerable<Planet> sortedList =
                        from planet in this.planets
                        orderby planet.MineralRichness descending
                        select planet;
                    foreach (Planet p in sortedList)
                    {
                        if (this.HideOwned && p.Owner != null || this.HideUninhab && !p.habitable)
                        {
                            continue;
                        }
                        PlanetListScreenEntry entry = new PlanetListScreenEntry(p, this.eRect.X + 22, this.leftRect.Y + 20, this.EMenu.Menu.Width - 30, 40, this);
                        this.PlanetSL.AddItem(entry);
                    }
                }
                else
                {
                    IOrderedEnumerable<Planet> sortedList =
                        from planet in this.planets
                        orderby planet.MineralRichness
                        select planet;
                    foreach (Planet p in sortedList)
                    {
                        if (this.HideOwned && p.Owner != null || this.HideUninhab && !p.habitable)
                        {
                            continue;
                        }
                        PlanetListScreenEntry entry = new PlanetListScreenEntry(p, this.eRect.X + 22, this.leftRect.Y + 20, this.EMenu.Menu.Width - 30, 40, this);
                        this.PlanetSL.AddItem(entry);
                    }
                }
            }
            if (this.sb_Pop.HandleInput(input))
            {
                //this.LastSorted = this.sb_Pop;
                AudioManager.PlayCue("blip_click");
                this.sb_Pop.Ascending = !this.sb_Pop.Ascending;
                this.PlanetSL.Entries.Clear();
                this.PlanetSL.Copied.Clear();
                if (!this.sb_Pop.Ascending)
                {
                    IOrderedEnumerable<Planet> sortedList =
                        from planet in this.planets
                        orderby planet.MaxPopulation descending
                        select planet;
                    foreach (Planet p in sortedList)
                    {
                        if (this.HideOwned && p.Owner != null || this.HideUninhab && !p.habitable)
                        {
                            continue;
                        }
                        PlanetListScreenEntry entry = new PlanetListScreenEntry(p, this.eRect.X + 22, this.leftRect.Y + 20, this.EMenu.Menu.Width - 30, 40, this);
                        this.PlanetSL.AddItem(entry);
                    }
                }
                else
                {
                    IOrderedEnumerable<Planet> sortedList =
                        from planet in this.planets
                        orderby planet.MaxPopulation
                        select planet;
                    foreach (Planet p in sortedList)
                    {
                        if (this.HideOwned && p.Owner != null || this.HideUninhab && !p.habitable)
                        {
                            continue;
                        }
                        PlanetListScreenEntry entry = new PlanetListScreenEntry(p, this.eRect.X + 22, this.leftRect.Y + 20, this.EMenu.Menu.Width - 30, 40, this);
                        this.PlanetSL.AddItem(entry);
                    }
                }
            }
            if (this.sb_Owned.HandleInput(input))
            {
                this.LastSorted = this.sb_Owned;
                AudioManager.PlayCue("blip_click");
                this.sb_Owned.Ascending = !this.sb_Owned.Ascending;
                this.PlanetSL.Entries.Clear();
                this.PlanetSL.Copied.Clear();
                if (!this.sb_Owned.Ascending)
                {
                    IOrderedEnumerable<Planet> sortedList =
                        from planet in this.planets
                        orderby planet.GetOwnerName() descending
                        select planet;
                    foreach (Planet p in sortedList)
                    {
                        if (this.HideOwned && p.Owner != null || this.HideUninhab && !p.habitable)
                        {
                            continue;
                        }
                        PlanetListScreenEntry entry = new PlanetListScreenEntry(p, this.eRect.X + 22, this.leftRect.Y + 20, this.EMenu.Menu.Width - 30, 40, this);
                        this.PlanetSL.AddItem(entry);
                    }
                }
                else
                {
                    IOrderedEnumerable<Planet> sortedList =
                        from planet in this.planets
                        orderby planet.GetOwnerName()
                        select planet;
                    foreach (Planet p in sortedList)
                    {
                        if (this.HideOwned && p.Owner != null || this.HideUninhab && !p.habitable)
                        {
                            continue;
                        }
                        PlanetListScreenEntry entry = new PlanetListScreenEntry(p, this.eRect.X + 22, this.leftRect.Y + 20, this.EMenu.Menu.Width - 30, 40, this);
                        this.PlanetSL.AddItem(entry);
                    }
                }
            }

            for (int i = this.PlanetSL.indexAtTop; i < this.PlanetSL.Entries.Count && i < this.PlanetSL.indexAtTop + this.PlanetSL.entriesToDisplay; i++)
            {
                PlanetListScreenEntry entry = this.PlanetSL.Entries[i].item as PlanetListScreenEntry;
                entry.HandleInput(input);
                entry.SetNewPos(this.eRect.X + 22, this.PlanetSL.Entries[i].clickRect.Y);
                if (!GlobalStats.TakingInput
                    && HelperFunctions.CheckIntersection(entry.TotalEntrySize, input.CursorPosition) && input.CurrentMouseState.LeftButton == ButtonState.Pressed && input.LastMouseState.LeftButton == ButtonState.Released)
                {
                    if (this.ClickTimer >= this.ClickDelay)
                    {
                        this.ClickTimer = 0f;
                    }
                    else
                    {
                        {
                            this.ExitScreen();
                            this.empUI.screen.SelectedPlanet = entry.planet;
                            this.empUI.screen.ViewPlanet(null);
                            this.empUI.screen.transitionStartPosition = new Vector3(this.SelectedPlanet.Position.X, this.SelectedPlanet.Position.Y, 10000f);
                            this.empUI.screen.returnToShip = false;
                        }
                        if (this.SelectedPlanet != entry.planet)
                        {
                            AudioManager.PlayCue("sd_ui_accept_alt3");
                            this.SelectedPlanet = entry.planet;
                        }
                    }
                }
            }
            if (input.CurrentKeyboardState.IsKeyDown(Keys.L) && !input.LastKeyboardState.IsKeyDown(Keys.L) && !GlobalStats.TakingInput)
            {
                AudioManager.PlayCue("echo_affirm");
                this.ExitScreen();
                return;
            }
            if (input.Escaped || input.RightMouseClick || this.close.HandleInput(input) )
            {
                //this.empUI.empire.data.PLSort = this.LastSorted;
                this.ExitScreen();
            }
            //if(this.LastSorted !=null && this.empUI.empire.data.PLSort != this.LastSorted)
        }