maped-mainscreen.cpp

Go to the documentation of this file.
00001 
00002 /***************************************************************************
00003  *                                                                         *
00004  *   This program is free software; you can redistribute it and/or modify  *
00005  *   it under the terms of the GNU General Public License as published by  *
00006  *   the Free Software Foundation; either version 2 of the License, or     *
00007  *   (at your option) any later version.                                   *
00008  *                                                                         *
00009  ***************************************************************************/
00010 
00011 #include "global.h"
00012 
00013 #include <stdio.h>
00014 #include <cstring>
00015 #include <stdlib.h>
00016 #include <new>
00017 #include <cstdlib>
00018 #include <ctype.h>
00019 #include <algorithm>
00020 #include <memory>
00021 #include <SDL_mixer.h>
00022 #include <iostream>
00023 #include <set>
00024 
00025 #include "paradialog.h"
00026 #include "pgtooltiphelp.h"
00027 #include "pgpopupmenu.h"
00028 #include "pgmenubar.h"
00029 
00030 #include "basegfx.h"
00031 #include "misc.h"
00032 #include "iconrepository.h"
00033 #include "maped-mainscreen.h"
00034 #include "graphics/blitter.h"
00035 #include "graphics/drawing.h"
00036 #include "edglobal.h"
00037 #include "mapdisplay.h"
00038 #include "spfst.h"
00039 #include "spfst-legacy.h"
00040 #include "overviewmappanel.h"
00041 #include "edselfnt.h"
00042 #include "edmisc.h"
00043 #include "gameoptions.h"
00044 #include "widgets/dropdownselector.h"
00045 #include "dialogs/fileselector.h"
00046 #include "weaponrangelayer.h"
00047 
00048 #include "mapdisplay.h"
00049 
00050 Maped_MainScreenWidget*  mainScreenWidget = NULL ;
00051 
00052 MainScreenWidget* getMainScreenWidget()
00053 {
00054    return mainScreenWidget;
00055 }
00056 
00057 
00058 class Menu : public PG_MenuBar {
00059 
00060     PG_PopupMenu* currentMenu;
00061     typedef list<PG_PopupMenu*> Categories;
00062     Categories categories;
00063 
00064    public:
00065       Menu ( PG_Widget *parent, const PG_Rect &rect=PG_Rect::null);
00066       
00067    protected:
00068       void setup();   
00069       bool execAction  (PG_PopupMenu::MenuItem* menuItem );
00070 
00071    private:
00072       void addbutton(const char* name, int id );
00073       void addfield ( const char* name );
00074      
00075 };
00076 
00077 
00078 
00079 bool Menu::execAction  (PG_PopupMenu::MenuItem* menuItem )
00080 {
00081    execaction_ev( tuseractions( menuItem->getId() ) );
00082 
00083    return true;
00084 }
00085 
00086 
00087 void Menu::addfield( const char* name )
00088 {
00089    currentMenu = new PG_PopupMenu( NULL, -1, -1, "" );
00090    categories.push_back ( currentMenu );
00091    Add ( name, currentMenu );
00092    currentMenu->sigSelectMenuItem.connect( SigC::slot( *this, &Menu::execAction ));
00093 
00094 }
00095 
00096 void Menu::addbutton( const char* name, int id )
00097 {
00098    currentMenu->addMenuItem( name, id );
00099 }
00100 
00101 
00102 void Menu::setup()
00103 {
00104   addfield ( "~F~ile" );
00105    addbutton ( "~N~ew map\tctrl+N" , act_newmap    );
00106    addbutton ( "~L~oad map\tctrl+L", act_loadmap   );
00107    addbutton ( "~S~ave map\tS",      act_savemap   );
00108    addbutton ( "Save map ~a~s",     act_savemapas );
00109    addbutton ( "Edit Map ~A~rchival Information", act_editArchivalInformation );
00110    currentMenu->addSeparator();
00111    addbutton ( "Load Clipboard",     act_readClipBoard );
00112    addbutton ( "Save Clipboard",     act_saveClipboard );
00113    currentMenu->addSeparator();
00114    addbutton ( "Run Script",     act_runLuaScript );
00115    addbutton ( "Run Translation Script", act_runTranslationScript );
00116    currentMenu->addSeparator();
00117    addbutton ( "~W~rite map to PNG-File\tctrl+G", act_maptopcx);
00118     addbutton ( "~I~mport BI map\tctrl-i", act_import_bi_map );
00119     addbutton ( "Insert ~B~I map", act_insert_bi_map );
00120    currentMenu->addSeparator();
00121    addbutton ( "Increase Map Zoom\tKP+", act_increase_zoom );
00122    addbutton ( "Decrease Map Zoom\tKP-", act_decrease_zoom );
00123    currentMenu->addSeparator();
00124    addbutton( "Edit Preferences", act_editpreferences );
00125    currentMenu->addSeparator();
00126    addbutton ( "~Q~uit\tctrl-q", act_end);
00127 
00128   addfield ("~E~dit");
00129    addbutton ( "~C~opy \tctrl+C",          act_copyToClipboard );
00130    addbutton ( "Cu~t~\tctrl+X",            act_cutToClipboard );
00131    addbutton ( "~P~aste \tctrl+V",         act_pasteFromClipboard );
00132    currentMenu->addSeparator();
00133    addbutton ( "Resi~z~e map\tR",             act_resizemap );
00134    addbutton ( "set global uniform ~w~eather\tctrl-W", act_setactweatherglobal );
00135    // addbutton ( "configure weather generator", act_setactnewweather );
00136    addbutton ( "~C~reate regional ressources", act_createresources );
00137    addbutton ( "Create global ressources\tctrl+F", act_createresources2 );
00138    addbutton ( "Clear all mineral resources", act_clearresources );
00139    currentMenu->addSeparator();
00140    addbutton ( "~S~et turn number",        act_setTurnNumber );
00141    currentMenu->addSeparator();
00142    addbutton ( "~S~etup Players",     act_setupplayers );
00143    addbutton ( "Setup ~A~lliances",     act_setupalliances );
00144    addbutton ( "~E~dit Research",          act_editResearch );
00145    addbutton ( "edit ~R~esearch points",          act_editResearchPoints );
00146    addbutton ( "edit ~T~ech adapter",          act_editTechAdapter );
00147    addbutton ( "edit player data...",   act_resetPlayerData );
00148 
00149   addfield ("~S~elect");
00150    addbutton ( "Vehicle\tF2",  act_selunit );
00151    addbutton ( "Terrain from map\tF3",  act_selbodentyp );
00152    addbutton ( "Terrain from list\tctrl-F3",  act_selbodentypAll );
00153    addbutton ( "Object from map \tF4",  act_selbodentyp );
00154    addbutton ( "Object from list \tctrl-F4",  act_selobjectAll );
00155    addbutton ( "Building Type\tF5", act_selbuilding );
00156    addbutton ( "Mine\tF6", act_selmine );
00157    
00158    
00159   addfield ("~T~ools");
00160    // addbutton ( "~V~iew map\tV",            act_viewmap );
00161    // addbutton ( "~S~how palette",             act_showpalette );
00162    // addbutton ( "~R~ebuild display\tctrl+R",   act_repaintdisplay );
00163    // currentMenu->addSeparator();
00164    addbutton ( "~M~ap generator\tG",          act_mapgenerator );
00165    addbutton ( "Sm~o~oth coasts",          act_smoothcoasts );
00166    addbutton ( "~U~nitset transformation",    act_unitsettransformation );
00167    addbutton ( "map ~t~ransformation",        act_transformMap );
00168    addbutton ( "Com~p~are Resources ", act_displayResourceComparison );
00169    addbutton ( "Show Pipeline Net\t9", act_showPipeNet );
00170    addbutton ( "Generate Tech Tree", act_generateTechTree );
00171    addbutton ( "Mirror Map", act_mirrorMap );
00172    addbutton ( "Copy Area", act_copyArea );
00173    addbutton ( "Paste Area", act_pasteArea );
00174    currentMenu->addSeparator();
00175    addbutton ( "PBP Player Statistics", act_pbpstatistics );
00176 
00177    addfield ("~O~ptions");
00178     addbutton ( "~M~ap properties\tctrl+M",          act_changemapvals );
00179     addbutton ( "~C~hange players\tO",           act_changeplayers);
00180     addbutton ( "~E~dit events\tE",              act_events );
00181 //    addbutton ( "unit production ~L~imitation", act_specifyunitproduction );
00182    currentMenu->addSeparator();
00183     addbutton ( "~T~oggle ResourceView\tctrl+B", act_toggleresourcemode);
00184     addbutton ( "~B~I ResourceMode",            act_bi_resource );
00185     addbutton ( "~A~sc ResourceMode",           act_asc_resource );
00186     addbutton ( "edit map ~P~arameters",        act_setmapparameters );
00187     addbutton ( "setup item ~F~ilters\tctrl+h",  act_setunitfilter );
00188     addbutton ( "select ~G~raphic set",         act_selectgraphicset );
00189     addbutton ( "show weapon range\tctrl+r", act_showweapnrange );
00190 
00191    addfield ("~D~evelopment");
00192     addbutton ( "Dump ~B~uilding",          act_dumpBuilding );
00193     addbutton ( "Dump all Buildings",          act_dumpAllBuildings );
00194     addbutton ( "Dump ~V~ehicle",          act_dumpVehicle );
00195     addbutton ( "Dump all Vehicles",       act_dumpAllVehicleDefinitions );
00196     addbutton ( "Dump ~O~bject",          act_dumpObject );
00197     addbutton ( "Dump all Objects",       act_dumpAllObjects );
00198     addbutton ( "Dump all Terrain",       act_dumpAllTerrain );
00199     addbutton ( "Locate Item by ~I~D",    act_locateItemByID );
00200    currentMenu->addSeparator();
00201     addbutton ( "Exchange ~G~raphics",          act_exchangeGraphics );
00202 /*
00203    addfield("Debug");
00204     addbutton ( "Crash map editor", asc_nullPointerCrash );
00205     addbutton ( "TestFunctionHolder", asc_testFunction );
00206 */
00207    addfield ("~H~elp");
00208     addbutton ( "~U~nit Information\tctrl+U",    act_unitinfo );
00209     addbutton ( "unit guide dialog",             act_unitGuideDialog );
00210     addbutton ( "unit~S~et Information",        act_unitSetInformation );
00211     addbutton ( "~T~errain Information\t7",        act_terraininfo );
00212    currentMenu->addSeparator();
00213     addbutton ( "~H~elp System\tF1",             act_help );
00214     addbutton ( "~A~bout",                      act_about );
00215 }
00216 
00217 
00218 Menu::Menu ( PG_Widget *parent, const PG_Rect &rect)
00219     : PG_MenuBar( parent, rect, "MenuBar"),
00220       currentMenu(NULL)
00221 {
00222    activateHotkey( KMOD_ALT );
00223    setup();
00224 }  
00225 
00226 namespace ContextMenu {
00227 
00228    class AutoTextContextAction : public ContextAction {
00229       public:
00230          ASCString getText( const MapCoordinate& pos )
00231          {
00232             return execactionnames[getActionID()];
00233          };
00234    };
00235    
00236 class ContainerProperties: public ContextAction {
00237    public:
00238       bool available( const MapCoordinate& pos )
00239       {
00240          return actmap->getField(pos)->getContainer();
00241       };
00242 
00243       ASCString getText( const MapCoordinate& pos )
00244       {
00245          if ( actmap->getField(pos)->vehicle )
00246             return "edit unit properties";
00247          else
00248             return "edit building properties";
00249       };
00250 
00251       int getActionID()
00252       {
00253          return act_changeunitvals ;
00254       }
00255 };
00256 
00257 class ContainerCargo: public ContextAction {
00258    public:
00259       bool available( const MapCoordinate& pos )
00260       {
00261          return actmap->getField(pos)->getContainer() && actmap->getField(pos)->getContainer()->baseType->maxLoadableUnits > 0;
00262       };
00263 
00264       ASCString getText( const MapCoordinate& pos )
00265       {
00266          if ( actmap->getField(pos)->vehicle )
00267             return "edit unit cargo";
00268          else
00269             return "edit building cargo";
00270       };
00271 
00272       int getActionID()
00273       {
00274          return act_changecargo ;
00275       }
00276 };
00277 
00278 class DeleteVehicle: public ContextAction {
00279    public:
00280       bool available( const MapCoordinate& pos )
00281       {
00282          return actmap->getField(pos)->vehicle;
00283       };
00284 
00285       ASCString getText( const MapCoordinate& pos )
00286       {
00287          return "delete unit";
00288       };
00289 
00290       int getActionID()
00291       {
00292          return act_deleteunit ;
00293       }
00294 };
00295 
00296 class DeleteBuilding: public ContextAction {
00297    public:
00298       bool available( const MapCoordinate& pos )
00299       {
00300          return actmap->getField(pos)->building;
00301       };
00302 
00303       ASCString getText( const MapCoordinate& pos )
00304       {
00305         return "delete building";
00306       };
00307 
00308       int getActionID()
00309       {
00310          return act_deletebuilding;
00311       }
00312 };
00313 
00314 class ContainerProduction: public AutoTextContextAction {
00315    public:
00316       bool available( const MapCoordinate& pos )
00317       {
00318          return actmap->getField(pos)->getContainer() && actmap->getField(pos)->getContainer()->baseType->hasFunction( ContainerBaseType::InternalVehicleProduction  );
00319       };
00320 
00321       int getActionID()
00322       {
00323          return act_changeproduction ;
00324       }
00325 };
00326 
00327 class DeleteMine: public AutoTextContextAction {
00328    public:
00329       bool available( const MapCoordinate& pos )
00330       {
00331          return !actmap->getField(pos)->mines.empty() ;
00332       };
00333 
00334       int getActionID()
00335       {
00336          return act_deletemine ;
00337       }
00338 };
00339 
00340 class ChangeMineStrength: public AutoTextContextAction {
00341    public:
00342       bool available( const MapCoordinate& pos )
00343       {
00344          return !actmap->getField(pos)->mines.empty() ;
00345       };
00346 
00347       int getActionID()
00348       {
00349          return act_changeminestrength ;
00350       }
00351 };
00352 
00353 class FieldResources: public AutoTextContextAction {
00354    public:
00355       bool available( const MapCoordinate& pos )
00356       {
00357          return true;
00358       };
00359 
00360       int getActionID()
00361       {
00362          return act_changeresources;
00363       }
00364 };
00365 
00366 class DeleteTopObject: public AutoTextContextAction {
00367    public:
00368       bool available( const MapCoordinate& pos )
00369       {
00370          return !actmap->getField(pos)->objects.empty();
00371       };
00372 
00373       int getActionID()
00374       {
00375          return act_deletetopmostobject;
00376       }
00377 };
00378 
00379 class DeleteAllObjects: public AutoTextContextAction {
00380    public:
00381       bool available( const MapCoordinate& pos )
00382       {
00383          return !actmap->getField(pos)->objects.empty();
00384       };
00385 
00386       int getActionID()
00387       {
00388          return act_deleteallobjects;
00389       }
00390 };
00391 
00392 
00393 } // namespace ContextMenu
00394 
00395 Maped_MainScreenWidget::Maped_MainScreenWidget( PG_Application& application )
00396               : MainScreenWidget( application ),
00397               vehicleSelector( NULL ), buildingSelector( NULL ), objectSelector(NULL), terrainSelector(NULL), mineSelector(NULL),
00398               weatherSelector( NULL ), selectionName(NULL), selectionName2(NULL), coordinateDisplay(NULL), currentSelectionWidget(NULL), contextMenu(NULL)
00399 {
00400 
00401    setup( false, PG_Rect(15,30,Width() - 200, Height() - 73) );
00402    mapDisplay->mouseButtonOnField.connect( SigC::slot( mousePressedOnField ));
00403    mapDisplay->mouseButtonOnField.connect( SigC::slot( *this, &Maped_MainScreenWidget::clickOnMap ));
00404    mapDisplay->mouseDraggedToField.connect( SigC::slot( mouseDraggedToField ));
00405 
00406    
00407 
00408    
00409    setupStatusBar();
00410    
00411    menu = new Menu(this, PG_Rect(15,0,Width()-200,20));
00412 
00413    
00414    PG_Application::GetApp()->sigKeyDown.connect( SigC::slot( *this, &Maped_MainScreenWidget::eventKeyDown ));
00415    
00416    int xpos = Width() - 150;
00417    int w = 140;
00418    int ypos = 180;
00419 
00420    PG_Button* buttonC = new PG_Button( this, PG_Rect( xpos, ypos, w, 20), "Clear Selection" );
00421    buttonC->sigClick.connect( SigC::slot( *this, &Maped_MainScreenWidget::clearSelection ));
00422    ypos += 25;
00423 
00424    
00425    PG_Button* button = new PG_Button( this, PG_Rect( xpos, ypos, w, 20), "Select Vehicle" );
00426    button->sigClick.connect( SigC::slot( *this, &Maped_MainScreenWidget::selectVehicle ));
00427    ypos += 25;
00428    
00429    PG_Button* button2 = new PG_Button( this, PG_Rect( xpos, ypos, w, 20), "Select Building" );
00430    button2->sigClick.connect( SigC::slot( *this, &Maped_MainScreenWidget::selectBuilding ));
00431    ypos += 25;
00432 
00433    PG_Button* button3 = new PG_Button( this, PG_Rect( xpos, ypos, w - 50, 20), "Sel. Object" );
00434    button3->sigClick.connect( SigC::slot( *this, &Maped_MainScreenWidget::selectObject ));
00435 
00436    PG_Button* button3b = new PG_Button( this, PG_Rect( xpos+ w - 45, ypos, 45, 20), "List" );
00437    button3b->sigClick.connect( SigC::slot( *this, &Maped_MainScreenWidget::selectObjectList ));
00438    ypos += 25;
00439 
00440 
00441    PG_Button* button4 = new PG_Button( this, PG_Rect( xpos, ypos, w - 50, 20), "Sel. Terrain" );
00442    button4->sigClick.connect( SigC::slot( *this, &Maped_MainScreenWidget::selectTerrain ));
00443 
00444    PG_Button* button4b = new PG_Button( this, PG_Rect( xpos + w - 45, ypos, 45, 20), "List" );
00445    button4b->sigClick.connect( SigC::slot( *this, &Maped_MainScreenWidget::selectTerrainList ));
00446    ypos += 25;
00447 
00448    PG_Button* button5 = new PG_Button( this, PG_Rect( xpos, ypos, w, 20), "Select Mine" );
00449    button5->sigClick.connect( SigC::slot( *this, &Maped_MainScreenWidget::selectMine ));
00450    ypos += 25;
00451     
00452    PG_Button* button6 = new PG_Button( this, PG_Rect( xpos, ypos, w, 20), "Lua Brush" );
00453    button6->sigClick.connect( SigC::slot( *this, &Maped_MainScreenWidget::selectLuaBrush ));
00454    ypos += 25;
00455    
00456    
00457    new PG_Label( this, PG_Rect( xpos, ypos, w/2 - 5, 20), "Brush:");
00458    brushSelector = new DropDownSelector( this, PG_Rect( xpos+w/2+5, ypos, w/2-5, 20));
00459    brushSelector->AddItem("1");
00460    brushSelector->AddItem("3");
00461    brushSelector->AddItem("5");
00462    brushSelector->selectionSignal.connect( SigC::slot( *this, &Maped_MainScreenWidget::brushChanged ));
00463    ypos += 25;
00464    
00465   
00466    weatherSelector = new DropDownSelector( this, PG_Rect( xpos, ypos, w, 20));
00467    for ( int i = 0; i < cwettertypennum; ++i )
00468       weatherSelector->AddItem( cwettertypen[i] );
00469     ypos += 25;  
00470             
00471    weatherSelector->selectionSignal.connect( SigC::slot( selection, &SelectionHolder::setWeather ) );
00472 //   weatherSelector->selectionSignal.connect( SigC::slot( *currentSelectionWidget, &SelectionItemWidget::Update ));
00473    
00474 
00475    playerSelector = new DropDownSelector( this, PG_Rect( xpos, ypos, w, 20));
00476    for ( int i = 0; i < 9; ++i )
00477       playerSelector->AddItem( "Player " + ASCString::toString(i) );
00478    playerSelector->selectionSignal.connect( SigC::slot( selection, &SelectionHolder::setPlayer ) );
00479    ypos += 25;
00480 
00481    selection.playerChanged.connect( SigC::slot( *this, &Maped_MainScreenWidget::playerChanged ));
00482 
00483    
00484    currentSelectionWidget = new SelectionItemWidget( this, PG_Rect( Width() - BuildingItem::Width() - 10, ypos, BuildingItem::Width(), BuildingItem::Height() ) );
00485    ypos += BuildingItem::Height() + 5;
00486 
00487    selectionName = new PG_Label( this, PG_Rect( xpos, ypos, currentSelectionWidget->Width(), 20 ));
00488    ypos += 25;
00489    selectionName2 = new PG_Label( this, PG_Rect( xpos, ypos, currentSelectionWidget->Width(), 20 ));
00490    ypos += 25;
00491    
00492    selection.selectionChanged.connect( SigC::slot( *currentSelectionWidget, &SelectionItemWidget::set ));
00493    selection.selectionChanged.connect( SigC::slot( *this, &Maped_MainScreenWidget::selectionChanged ));
00494 
00495    spawnOverviewMapPanel( "Mapeditor_OverviewMap" );
00496 
00497    addContextAction( new ContextMenu::ContainerProperties );
00498    addContextAction( new ContextMenu::ContainerCargo );
00499    addContextAction( new ContextMenu::ContainerProduction );
00500    addContextAction( new ContextMenu::DeleteVehicle );
00501    addContextAction( new ContextMenu::DeleteBuilding );
00502    addContextAction( new ContextMenu::ChangeMineStrength );
00503    addContextAction( new ContextMenu::DeleteMine );
00504    addContextAction( new ContextMenu::FieldResources );
00505    addContextAction( new ContextMenu::DeleteTopObject );
00506    addContextAction( new ContextMenu::DeleteAllObjects );
00507    
00508    weaponRangeLayer = new UnitWeaponRangeLayer();
00509    mapDisplay->addMapLayer( weaponRangeLayer, "weaprange" );
00510    
00511 }
00512 
00513 void Maped_MainScreenWidget::showWeaponRange( GameMap* gamemap, const MapCoordinate& pos )
00514 {
00515    weaponRangeLayer->operateField( gamemap, pos );
00516 }
00517 
00518 
00519 void Maped_MainScreenWidget::playerChanged( int player )
00520 {
00521    static int recursionPreventer = 0;
00522    recursionPreventer++;
00523    if ( recursionPreventer == 1 ) 
00524       playerSelector->SelectItem( player );
00525    recursionPreventer--;
00526 }
00527 
00528 
00529 
00530 
00531 
00532 
00533 bool Maped_MainScreenWidget::eventMouseButtonDown (const SDL_MouseButtonEvent *button)
00534 {
00535    return false;
00536 }
00537 
00538 void Maped_MainScreenWidget::setupStatusBar()
00539 {
00540    int x = mapDisplay->my_xpos;
00541    int y =  mapDisplay->my_ypos + mapDisplay->Height() + 25;
00542    int height = 20;
00543 
00544    messageLine = new PG_Label ( this, PG_Rect( x, y, 200, height ) );
00545    messageLine->SetFontSize(11);
00546    x += 210;
00547    
00548 
00549    coordinateDisplay = new PG_Label ( this, PG_Rect( x, y, 80, height ) );
00550    coordinateDisplay->SetFontSize(11);
00551    
00552    updateFieldInfo.connect( SigC::slot( *this, &Maped_MainScreenWidget::updateStatusBar ));
00553    cursorMoved.connect( SigC::slot( *this, &Maped_MainScreenWidget::updateStatusBar ));
00554 }
00555 
00556 void Maped_MainScreenWidget::updateStatusBar()
00557 {
00558    MapCoordinate pos = actmap->getCursor();
00559    coordinateDisplay->SetText( ASCString::toString( pos.x) + " / " + ASCString::toString( pos.y ));
00560 }
00561 
00562 void Maped_MainScreenWidget::selectionChanged( const Placeable* item )
00563 {
00564    if ( item ) {
00565       selectionName->SetText( item->getName() );
00566       
00567       const MapComponent* mc = dynamic_cast<const MapComponent*>(item);
00568       if (  mc ) 
00569          selectionName2->SetText( "ID: " + ASCString::toString( mc->getItemType()->getID() ) );
00570       else
00571          selectionName2->SetText( "" );
00572    } else {
00573       selectionName->SetText( "" );
00574       selectionName2->SetText( "" );
00575    }
00576 }
00577 
00578 
00579 void Maped_MainScreenWidget::brushChanged( int i )
00580 {
00581    selection.brushSize = i+1;
00582    if ( selection.brushSize < 1 )
00583       selection.brushSize = 1;
00584 }
00585 
00586 void Maped_MainScreenWidget:: addContextAction( ContextAction* contextAction )
00587 {
00588    contextActions.push_back( contextAction );
00589 }
00590 
00591 
00592 bool Maped_MainScreenWidget::clickOnMap( const MapCoordinate& field, const SPoint& pos, bool changed, int button, int prio)
00593 {
00594    if ( prio > 1 )
00595       return false;
00596    
00597    if( button == 3 ) {
00598 
00599       mapDisplay->cursor.goTo( field );
00600       
00601       int counter = 0;
00602       for ( deallocating_vector<ContextAction*>::iterator i = contextActions.begin(); i != contextActions.end(); ++i )
00603          if ( (*i)->available( field ))
00604             ++counter;
00605 
00606       if ( contextMenu ) {
00607          delete contextMenu;
00608          contextMenu = NULL;
00609          return true;
00610       }
00611 
00612       if ( !counter )
00613          return false;
00614 
00615       
00616       contextMenu = new PG_PopupMenu( this, pos.x, pos.y );
00617 
00618       for ( deallocating_vector<ContextAction*>::iterator i = contextActions.begin(); i != contextActions.end(); ++i )
00619          if ( (*i)->available( field )) {
00620             MapCoordinate mc = field;
00621             contextMenu->addMenuItem( (*i)->getText( field ), (*i)->getActionID() );
00622          }
00623 
00624       contextMenu->sigSelectMenuItem.connect( SigC::slot( *this, &Maped_MainScreenWidget::runContextAction   ));
00625       
00626       contextMenu->Show();
00627       return true;
00628    } else {
00629       if ( contextMenu ) {
00630          delete contextMenu;
00631          contextMenu = NULL;
00632          return true;
00633       }
00634    }
00635    return false;
00636 }
00637 
00638 bool Maped_MainScreenWidget::runContextAction  (PG_PopupMenu::MenuItem* menuItem )
00639 {
00640    execaction_ev( tuseractions( menuItem->getId() ) );
00641 
00642    return true;
00643 }
00644 
00645 #ifdef WIN32
00646 # include "win32/win32-errormsg.h"
00647 # include  "win32/msvc/mdump.h"
00648 #endif
00649 
00650 
00651 bool Maped_MainScreenWidget::eventKeyUp(const SDL_KeyboardEvent* key)
00652 {
00653    if ( key->keysym.sym ==  SDLK_RCTRL || key->keysym.sym == SDLK_LCTRL ) {
00654       execaction_ev( act_releaseControlPanel );
00655       return true;
00656    }
00657    return false;
00658 }
00659 
00660 void helperFunction()
00661 {
00662    /*
00663    set<int> s;
00664    for ( int i = 0; i < 1083; ++i )
00665       s.insert(i);
00666    for ( int y = 0; y < actmap->ysize; ++y)
00667       for ( int x = 0; x < actmap->xsize; ++x) 
00668          if ( s.find( actmap->getField(x,y)->typ->terraintype->id ) != s.end() )
00669             s.erase( actmap->getField(x,y)->typ->terraintype->id );
00670 
00671    int start  = -1;
00672    for ( int i = 0; i <= 1083; ++i ) {
00673       if ( s.find(i) != s.end() ) {
00674          if ( start < 0 )
00675             start = i;
00676       } else {
00677          if ( start >= 0 ) {
00678             cout << start << "-" << i-1 << " ";
00679             start = -1;
00680          }
00681       }
00682    }
00683    cout << "\n";
00684    */
00685 }
00686 
00687 
00688 bool Maped_MainScreenWidget::eventKeyDown(const SDL_KeyboardEvent* key)
00689 {
00690    int mod = SDL_GetModState() & ~(KMOD_NUM | KMOD_CAPS | KMOD_MODE);
00691 
00692    if ( key->keysym.sym ==  SDLK_RCTRL || key->keysym.sym == SDLK_LCTRL ) {
00693       execaction_ev( act_openControlPanel );
00694       return true;
00695    }
00696 
00697    if ( !mod  ) {
00698       switch ( key->keysym.sym ) {
00699          case SDLK_KP_ENTER:
00700          case SDLK_RETURN:
00701          case SDLK_SPACE: execaction_ev( act_primaryAction );
00702                         return true;
00703                         
00704          case SDLK_F1:  execaction_ev(act_help);
00705                         return true;
00706          case SDLK_F2 : execaction_ev(act_selunit);
00707                         return true;
00708          case SDLK_F3:  execaction_ev(act_selbodentyp);
00709                         return true;
00710          case SDLK_F4 : execaction_ev(act_selobject);
00711                         return true;
00712          case SDLK_F5 : execaction_ev(act_selbuilding);
00713                         return true;
00714          case SDLK_F6 : execaction_ev(act_selmine);
00715                         return true;
00716 
00717          case SDLK_F11: helperFunction();
00718                         return true;
00719          case SDLK_a:   execaction_ev(act_movebuilding);
00720                         return true;
00721          case SDLK_b:   execaction_ev(act_changeresources);
00722                         return true;
00723          case SDLK_c:   execaction_ev(act_changecargo);
00724                         return true;
00725          case SDLK_d : execaction_ev(act_changeterraindir);
00726                         return true;
00727          case SDLK_e:  execaction_ev(act_events);
00728                         return true;
00729          case SDLK_f:  execaction_ev(act_changeproduction);
00730                         return true;
00731          case SDLK_g: execaction_ev(act_mapgenerator);
00732                         return true;
00733          case SDLK_h: execaction_ev(act_setactivefieldvals);
00734                         return true;
00735          case SDLK_DELETE: execaction_ev(act_deletething);
00736                         return true;
00737          case SDLK_l : execaction_ev(act_showpalette);
00738                         return true;
00739          case SDLK_m : execaction_ev(act_changeminestrength);
00740                         return true;
00741          case SDLK_o: execaction_ev(act_changeplayers);
00742                         return true;
00743 
00744          case SDLK_p: execaction_ev(act_changeunitvals);
00745                         return true;
00746 
00747          case SDLK_r: execaction_ev(act_resizemap);
00748                         return true;
00749 
00750          case SDLK_s : execaction_ev(act_savemap);
00751                         return true;
00752 
00753          case SDLK_v:   execaction_ev(act_viewmap);
00754                         return true;
00755 
00756          case SDLK_x:   execaction_ev(act_mirrorcursorx);
00757                         return true;
00758 
00759          case SDLK_y:   execaction_ev(act_mirrorcursory);
00760                         return true;
00761 
00762          case SDLK_z:   execaction_ev(act_setzoom );
00763                         return true;
00764 
00765          case SDLK_7 :  execaction_ev(act_terraininfo);
00766                         return true;
00767                         
00768          case SDLK_8 :  execaction_ev(act_placemine);
00769                         return true;
00770 
00771          case SDLK_9:   execaction_ev(act_showPipeNet);
00772                         return true;
00773 
00774 
00775          case SDLK_TAB: execaction_ev(act_switchmaps );
00776                         return true;
00777 
00778          case SDLK_ESCAPE: execaction_ev(act_clearSelection);
00779                         return true;
00780          case SDLK_PLUS:
00781          case SDLK_KP_PLUS: execaction_ev( act_increase_zoom );
00782             return true;
00783 
00784          case SDLK_MINUS:
00785          case SDLK_KP_MINUS: execaction_ev( act_decrease_zoom );
00786             return true;
00787             
00788             default:;
00789        }
00790    }
00791       
00792    if ( mod & KMOD_CTRL ) {
00793       switch ( key->keysym.sym ) {
00794          case SDLK_F3 : execaction_ev(act_selbodentypAll);
00795                         return true;
00796    
00797          case SDLK_F4 : execaction_ev(act_selobjectAll);
00798                         return true;
00799 
00800          case SDLK_SPACE: execaction_ev( act_primaryAction );
00801                         return true;
00802                         
00803          default:;
00804    
00805        } 
00806       switch ( key->keysym.unicode ) {
00807          case 1:  // A
00808             execaction_ev(act_setupalliances);
00809             return true;
00810    
00811          case 2:  // B
00812             execaction_ev(act_toggleresourcemode);
00813             return true;
00814    
00815          case 3:  // C
00816             execaction_ev(act_copyToClipboard);
00817             return true;
00818 
00819          case 6: // F
00820             execaction_ev(act_createresources);
00821             return true;
00822    
00823          case 7: //G
00824             execaction_ev(act_maptopcx);
00825             return true;
00826    
00827          case 8:  // H
00828             execaction_ev(act_setunitfilter);
00829             return true;
00830    
00831          case 9:  // I
00832             execaction_ev (act_import_bi_map );
00833             return true;
00834    
00835          case 12:  // L
00836             execaction_ev(act_loadmap);
00837             return true;
00838    
00839          case 13:  // M
00840             execaction_ev(act_changemapvals);
00841             return true;
00842    
00843          case 14:  // N
00844             execaction_ev(act_newmap);
00845             return true;
00846    
00847          case 15:  // O
00848             execaction_ev(act_polymode);
00849             return true;
00850    
00851          case 16:  // P
00852             execaction_ev(act_changeproduction);
00853             return true;
00854    
00855          case 17:  // Q
00856             execaction_ev(act_end);
00857             return true;
00858                         
00859          case 18: // R
00860             execaction_ev(act_showweapnrange);
00861             return true;
00862    
00863          case 19 :  // S
00864             execaction_ev(act_savemap);
00865             return true;
00866 
00867          case 21: // U
00868             execaction_ev(act_unitinfo);
00869             return true;
00870    
00871          case 22:  // V
00872             execaction_ev(act_pasteFromClipboard);
00873             return true;
00874    
00875          case 23: // W
00876             execaction_ev(act_setactweatherglobal);
00877             return true;
00878 
00879          case 24: // X
00880             execaction_ev(act_cutToClipboard);
00881             return true;
00882 
00883       }
00884    }
00885 
00886    if ( mod & KMOD_SHIFT ) {
00887       switch ( key->keysym.sym ) {
00888          case SDLK_d: execaction_ev(act_changeunitdir);
00889             return true;
00890          case SDLK_F3 : execaction_ev(act_selbodentypAll);
00891                         return true;
00892    
00893          case SDLK_F4 : execaction_ev(act_selobjectAll);
00894                         return true;
00895          default:;
00896       }
00897    }
00898 
00899    if ( mod & KMOD_ALT ) {
00900       switch ( key->keysym.sym ) {
00901             case SDLK_RETURN:
00902                getPGApplication().toggleFullscreen();
00903                return true;
00904 
00905          case SDLK_F11:
00906                /*
00907             if ( mod & KMOD_SHIFT ) {
00908             if (choice_dlg("Do you really want to crash ASC ?","~y~es","~n~o") == 1) {
00909             char* c = NULL;
00910             *c = 1;
00911       }
00912       }
00913                */
00914          {  for ( Player::VehicleList::iterator i = actmap->player[1].vehicleList.begin(); i != actmap->player[1].vehicleList.end(); ++i )
00915             (*i)->direction = 3;
00916          } 
00917          return true;
00918          
00919          default:;
00920       }
00921    }
00922 
00923    return false;
00924 }
00925 
00926 
00927 
00928 class MapItemSelectionWindow : public ItemSelectorWindow {
00929    public:
00930       MapItemSelectionWindow( PG_Widget *parent, const PG_Rect &r , const ASCString& title, SelectionItemFactory* itemFactory )
00931          : ItemSelectorWindow( parent, r, title, itemFactory ) {};
00932       
00933       void itemSelected( const SelectionWidget* )
00934       {
00935          if ( CGameOptions::Instance()->maped_modalSelectionWindow )
00936             Hide();
00937          QuitModal();
00938       }
00939 
00940 };
00941 
00942 typedef PG_Window* PG_WindowPointer;
00943 
00944 template <class ItemType> 
00945 void showSelectionWindow( PG_Widget* parent, PG_WindowPointer &selectionWindow, const ItemRepository<ItemType>& itemRepository  )
00946 {
00947    if ( !selectionWindow ) {
00948       ItemSelectorWindow* sw = new MapItemSelectionWindow( parent, PG_Rect( parent->Width()-300, 100, 280, parent->Height()-150), "select item", new MapItemTypeWidgetFactory< MapItemTypeWidget<ItemType> >(itemRepository) );
00949 
00950       filtersChangedSignal.connect( SigC::slot( *sw, &ItemSelectorWindow::reLoad ));
00951       selectionWindow = sw;
00952    }
00953    
00954    selectionWindow->Show();
00955    selectionWindow->RunModal();
00956    if ( CGameOptions::Instance()->maped_modalSelectionWindow )
00957       selectionWindow->Hide();
00958       
00959 }
00960 
00961 
00962 bool Maped_MainScreenWidget :: clearSelection()
00963 {
00964    selection.clear();
00965    return true;
00966 }
00967 
00968 
00969 bool Maped_MainScreenWidget :: selectVehicle()
00970 {
00971    showSelectionWindow( this, vehicleSelector, vehicleTypeRepository );
00972    return true;
00973 }
00974 
00975 
00976 bool Maped_MainScreenWidget :: selectBuilding()
00977 {
00978    showSelectionWindow( this, buildingSelector, buildingTypeRepository );
00979    return true;
00980 }
00981 
00982 bool Maped_MainScreenWidget :: selectObject()
00983 {
00984    execaction_ev( act_switchmaps );
00985    // showSelectionWindow( this, objectSelector, objectTypeRepository );
00986    return true;
00987 }
00988 
00989 bool Maped_MainScreenWidget :: selectObjectList()
00990 {
00991    showSelectionWindow( this, objectSelector, objectTypeRepository );
00992    return true;
00993 }
00994 
00995 
00996 bool Maped_MainScreenWidget :: selectTerrain()
00997 {
00998    execaction_ev( act_switchmaps );
00999    // showSelectionWindow( this, terrainSelector, terrainTypeRepository );
01000    return true;
01001 }
01002 
01003 bool Maped_MainScreenWidget :: selectTerrainList()
01004 {
01005    showSelectionWindow( this, terrainSelector, terrainTypeRepository );
01006    return true;
01007 }
01008 
01009 
01010 bool Maped_MainScreenWidget :: selectMine()
01011 {
01012    showSelectionWindow( this, mineSelector, mineTypeRepository );
01013    return true;
01014 }
01015 
01016 bool Maped_MainScreenWidget :: selectLuaBrush()
01017 {
01018    ASCString file = selectFile( "*.brush.lua", true );
01019    if ( file.empty () )
01020       return true;
01021    
01022    LuaBrush brush ( file );
01023    selection.setSelection( brush );
01024 
01025    
01026    return true;
01027 }
01028 
01029 
01030 
01031 void displaymessage2( const char* formatstring, ... )
01032 {
01033 
01034    ASCString s;
01035    
01036     std::va_list arg_ptr;
01037     va_start ( arg_ptr, formatstring );
01038 
01039     s.vaformat( formatstring, arg_ptr );
01040 
01041     va_end ( arg_ptr );
01042 
01043     if ( mainScreenWidget )
01044        mainScreenWidget->displayMessage(s);
01045 }
01046 
01047 
01048 
01049 

Generated on Mon May 21 01:26:35 2012 for Advanced Strategic Command by  doxygen 1.5.1