Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List

UniversePane.cpp

00001 /*
00002 Copyright 2005 Elliott Kleinrock
00003 
00004 This file is part of FreeStars, a free clone of the Stars! game.
00005 
00006 FreeStars is free software; you can redistribute it and/or modify
00007 it under the terms of the GNU General Public License as published by
00008 the Free Software Foundation; either version 2 of the License, or
00009 (at your option) any later version.
00010 
00011 FreeStars is distributed in the hope that it will be useful,
00012 but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 GNU General Public License for more details.
00015 
00016 You should have received a copy of the GNU General Public License
00017 along with FreeStars; if not, write to the Free Software
00018 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 
00020 The full GPL Copyright notice should be in the file COPYING.txt
00021 
00022 Contact:
00023 Email Elliott at 9jm0tjj02@sneakemail.com
00024 */
00025 
00026 #include "FSClient.h"
00027 
00028 #include "UniversePane.h"
00029 
00030 #ifdef _DEBUG
00031 #define new DEBUG_NEW
00032 #endif
00033 
00034 BEGIN_EVENT_TABLE(UniversePane, wxScrolledWindow)
00035   EVT_PAINT(UniversePane::OnPaint)
00036   EVT_RIGHT_DOWN(UniversePane::OnRightDown)
00037   EVT_LEFT_DOWN(UniversePane::OnLeftDown)
00038 END_EVENT_TABLE()
00039 
00040 wxLocation::~wxLocation()
00041 {
00042 }
00043 
00044 UniversePane::UniversePane(MainWindow * mainw) : wxScrolledWindow(mainw, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxHSCROLL | wxVSCROLL)
00045 {
00046         mParent = mainw;
00047         mScale = 8.0;
00048         mLastSelected = NULL;
00049         SetVirtualSize(100 + 200*mScale, 100 + 200*mScale);
00050         SetScrollRate(1+2*mScale, 1+2*mScale);
00051 }
00052 
00053 void UniversePane::OnPaint( wxPaintEvent &WXUNUSED( event ) )
00054 {
00055         if (!MainWindow::mPaintOK)
00056                 return;
00057 
00058   wxPaintDC dc( this );
00059   dc.Clear( );
00060   int vx=0, vy=0, unitX=0, unitY=0;
00061 
00062   GetViewStart(&vx, &vy);
00063   GetScrollPixelsPerUnit( &unitX, &unitY );
00064 
00065   int xOffset = -vx*unitX;
00066   int yOffset = -vy*unitY;
00067 
00068   // Paint the screen black
00069   dc.SetBrush( *wxBLACK_BRUSH );
00070   dc.SetPen( wxPen( wxColour( 0, 0, 0 ), 0, wxSOLID ) );
00071   dc.DrawRectangle( xOffset, yOffset, 3000, 3000 );
00072 
00073   Planet *curPlanet;
00074   int i;
00075 
00076   // Draw scan
00077   dc.SetBrush( wxBrush( wxColour( 120, 0, 0 ), wxSOLID ) );
00078   dc.SetPen( wxPen( wxColour( 120, 0, 0 ), 3, wxSOLID ) );
00079   for (i = 1; i <= TheGalaxy->GetPlanetCount(); ++i) {
00080         curPlanet = TheGalaxy->GetPlanet(i);
00081     if (curPlanet && curPlanet->GetOwner() == TheGame->GetCurrentPlayer())
00082     {
00083       dc.DrawCircle( 50+xOffset+(((*curPlanet).GetPosX( ))-1000)*mScale, 50+yOffset+(1200-((*curPlanet).GetPosY( )))*mScale, (*curPlanet).GetScanSpace( )*mScale );
00084     }
00085   }
00086 
00087   const Fleet * curFleet;
00088   for (i = 1; i <= Rules::MaxFleets; ++i) {
00089         curFleet = TheGame->GetCurrentPlayer()->GetFleet(i);
00090     if (curFleet)
00091     {
00092       dc.DrawCircle( 50+xOffset+(curFleet->GetPosX() - 1000)*mScale, 50+yOffset+(1200 - curFleet->GetPosY())*mScale, curFleet->GetScanSpace()*mScale);
00093     }
00094   }
00095 
00096   // Draw penscan
00097   dc.SetBrush( wxBrush( wxColour( 0, 120, 0 ), wxSOLID ) );
00098   dc.SetPen( wxPen( wxColour( 0, 120, 0 ), 3, wxSOLID ) );
00099   for (i = 1; i <= TheGalaxy->GetPlanetCount(); ++i) {
00100         curPlanet = TheGalaxy->GetPlanet(i);
00101     if (curPlanet && curPlanet->GetOwner() == TheGame->GetCurrentPlayer())
00102     {
00103       dc.DrawCircle( 50+xOffset+(((*curPlanet).GetPosX( ))-1000)*mScale, 50+yOffset+(1200-((*curPlanet).GetPosY( )))*mScale, (*curPlanet).GetScanPen( )*mScale );
00104     }
00105   }
00106 
00107   wxPoint TriPoints[] = {wxPoint(0,0), wxPoint(0,6), wxPoint(3,3)};
00108 
00109   for (i = 1; i <= Rules::MaxFleets; ++i) {
00110         curFleet = TheGame->GetCurrentPlayer()->GetFleet(i);
00111     if (curFleet)
00112     {
00113           dc.SetBrush( wxBrush( wxColour( 0, 120, 0 ), wxSOLID ) );
00114           dc.SetPen( wxPen( wxColour( 0, 120, 0 ), 3, wxSOLID ) );
00115       dc.DrawCircle( 50+xOffset+(curFleet->GetPosX() - 1000)*mScale, 50+yOffset+(1200 - curFleet->GetPosY())*mScale, curFleet->GetScanPen()*mScale);
00116           dc.SetBrush( wxBrush( wxColour( 0, 240, 0 ), wxSOLID ) );
00117           dc.SetPen( wxPen( wxColour( 0, 240, 0 ), 3, wxSOLID ) );
00118           dc.DrawPolygon(3, TriPoints,  50+xOffset+(curFleet->GetPosX() - 1000)*mScale, 50+yOffset+(1200 - curFleet->GetPosY())*mScale);
00119     }
00120   }
00121 
00122   // Draw planets and names
00123   dc.SetTextForeground( wxColour( 255, 255, 255 ) );
00124   for (i = 1; i <= TheGalaxy->GetPlanetCount(); ++i) {
00125         curPlanet = TheGalaxy->GetPlanet(i);
00126     if (curPlanet)
00127     {
00128       dc.SetBrush( wxBrush( wxColour( 0, 255, 0 ), wxSOLID ) );
00129       dc.SetPen( wxPen( wxColour( 0, 100, 0 ), 3, wxSOLID ) );
00130       dc.DrawCircle( 50+xOffset+(((*curPlanet).GetPosX( ))-1000)*mScale, 50+yOffset+(1200-((*curPlanet).GetPosY( )))*mScale, 10 );
00131       dc.DrawText(curPlanet->GetName().c_str(), 60+xOffset+(((*curPlanet).GetPosX( ))-1000)*mScale, 60+yOffset+(1200-((*curPlanet).GetPosY( )))*mScale );
00132     }
00133   }
00134 }
00135 
00136 void UniversePane::OnRightDown(wxMouseEvent &me)
00137 {
00138         int x;
00139         int y;
00140         CalcUnscrolledPosition(me.m_x, me.m_y, &x, &y);
00141         x = (x-50)/mScale + 1000;
00142         y = 1200 - (y-50)/mScale;
00143         wxMenu menu;
00144         deque<Location *> * d;
00145         d = TheGame->GetClosestTop(x, y);
00146         if (d != NULL) {
00147                 for (int i = 0; i < d->size(); ++i) {
00148                         menu.Append(ID_SelLocation + i, d->at(i)->GetName(TheGame->GetCurrentPlayer()).c_str());
00149                         Connect(ID_SelLocation + i, wxID_ANY, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(UniversePane::OnSelectLocation), new wxLocation(d->at(i)));
00150                 }
00151         }
00152 
00153         PopupMenu(&menu);
00154 }
00155 
00156 void UniversePane::OnLeftDown(wxMouseEvent &me)
00157 {
00158         int x;
00159         int y;
00160         CalcUnscrolledPosition(me.m_x, me.m_y, &x, &y);
00161         x = (x-50)/mScale + 1000;
00162         y = 1200 - (y-50)/mScale;
00163 
00164         deque<Location *> * d;
00165         d = TheGame->GetClosestTop(x, y);
00166         if (d != NULL) {
00167                 if (d == mLastSelected) {
00168                         mpLastSelected++;
00169                         if (mpLastSelected >= d->size())
00170                                 mpLastSelected = 0;
00171                 } else {
00172                         mLastSelected = d;
00173                         mpLastSelected = 0;
00174                 }
00175 
00176                 SelectLocation(d->at(mpLastSelected));
00177         }
00178 }
00179 
00180 void UniversePane::OnSelectLocation(wxCommandEvent& event)
00181 {
00182         wxLocation * wl = dynamic_cast<wxLocation *>(event.m_callbackUserData);
00183         if (wl != NULL)
00184                 SelectLocation(wl->mLocation);
00185 }
00186 
00187 void UniversePane::SelectLocation(Location * loc)
00188 {
00189         if (loc == NULL)
00190                 return;
00191 
00192         mParent->SetLocation(loc);
00193 }

Generated on Mon Aug 8 21:33:45 2005 for Freestars by  doxygen 1.4.2-20050421