File: maped-mainscreen.cpp

package info (click to toggle)
asc 2.6.1.0-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 81,740 kB
  • sloc: cpp: 158,704; sh: 11,544; ansic: 6,736; makefile: 604; perl: 138
file content (1048 lines) | stat: -rw-r--r-- 32,294 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "global.h"

#include <stdio.h>
#include <cstring>
#include <stdlib.h>
#include <new>
#include <cstdlib>
#include <ctype.h>
#include <algorithm>
#include <memory>
#include <SDL_mixer.h>
#include <iostream>
#include <set>

#include "paradialog.h"
#include "pgtooltiphelp.h"
#include "pgpopupmenu.h"
#include "pgmenubar.h"

#include "basegfx.h"
#include "misc.h"
#include "iconrepository.h"
#include "maped-mainscreen.h"
#include "graphics/blitter.h"
#include "graphics/drawing.h"
#include "edglobal.h"
#include "mapdisplay.h"
#include "spfst.h"
#include "spfst-legacy.h"
#include "overviewmappanel.h"
#include "edselfnt.h"
#include "edmisc.h"
#include "gameoptions.h"
#include "widgets/dropdownselector.h"
#include "dialogs/fileselector.h"
#include "weaponrangelayer.h"

#include "mapdisplay.h"

Maped_MainScreenWidget*  mainScreenWidget = NULL ;

MainScreenWidget* getMainScreenWidget()
{
   return mainScreenWidget;
}


class Menu : public PG_MenuBar {

    PG_PopupMenu* currentMenu;
    typedef list<PG_PopupMenu*> Categories;
    Categories categories;

   public:
      Menu ( PG_Widget *parent, const PG_Rect &rect=PG_Rect::null);
      
   protected:
      void setup();   
      bool execAction  (PG_PopupMenu::MenuItem* menuItem );

   private:
      void addbutton(const char* name, int id );
      void addfield ( const char* name );
     
};



bool Menu::execAction  (PG_PopupMenu::MenuItem* menuItem )
{
   execaction_ev( tuseractions( menuItem->getId() ) );

   return true;
}


void Menu::addfield( const char* name )
{
   currentMenu = new PG_PopupMenu( NULL, -1, -1, "" );
   categories.push_back ( currentMenu );
   Add ( name, currentMenu );
   currentMenu->sigSelectMenuItem.connect( sigc::mem_fun( *this, &Menu::execAction ));

}

void Menu::addbutton( const char* name, int id )
{
   currentMenu->addMenuItem( name, id );
}


void Menu::setup()
{
  addfield ( "~F~ile" );
   addbutton ( "~N~ew map\tctrl+N" , act_newmap    );
   addbutton ( "~L~oad map\tctrl+L", act_loadmap   );
   addbutton ( "~S~ave map\tS",      act_savemap   );
   addbutton ( "Save map ~a~s",     act_savemapas );
   addbutton ( "Edit Map ~A~rchival Information", act_editArchivalInformation );
   currentMenu->addSeparator();
   addbutton ( "Load Clipboard",     act_readClipBoard );
   addbutton ( "Save Clipboard",     act_saveClipboard );
   currentMenu->addSeparator();
   addbutton ( "Run Script",     act_runLuaScript );
   addbutton ( "Run Translation Script", act_runTranslationScript );
   currentMenu->addSeparator();
   addbutton ( "~W~rite map to PNG-File\tctrl+G", act_maptopcx);
    addbutton ( "~I~mport BI map\tctrl-i", act_import_bi_map );
    addbutton ( "Insert ~B~I map", act_insert_bi_map );
   currentMenu->addSeparator();
   addbutton ( "Increase Map Zoom\tKP+", act_increase_zoom );
   addbutton ( "Decrease Map Zoom\tKP-", act_decrease_zoom );
   currentMenu->addSeparator();
   addbutton( "Edit Preferences", act_editpreferences );
   currentMenu->addSeparator();
   addbutton ( "~Q~uit\tctrl-q", act_end);

  addfield ("~E~dit");
   addbutton ( "~C~opy \tctrl+C",          act_copyToClipboard );
   addbutton ( "Cu~t~\tctrl+X",            act_cutToClipboard );
   addbutton ( "~P~aste \tctrl+V",         act_pasteFromClipboard );
   currentMenu->addSeparator();
   addbutton ( "Resi~z~e map\tR",             act_resizemap );
   addbutton ( "set global uniform ~w~eather\tctrl-W", act_setactweatherglobal );
   // addbutton ( "configure weather generator", act_setactnewweather );
   addbutton ( "~C~reate regional resources", act_createresources );
   addbutton ( "Create global resources\tctrl+F", act_createresources2 );
   addbutton ( "Clear all mineral resources", act_clearresources );
   currentMenu->addSeparator();
   addbutton ( "~S~et turn number",        act_setTurnNumber );
   currentMenu->addSeparator();
   addbutton ( "~S~etup Players",     act_setupplayers );
   addbutton ( "Setup ~A~lliances",     act_setupalliances );
   addbutton ( "~E~dit Research",          act_editResearch );
   addbutton ( "edit ~R~esearch points",          act_editResearchPoints );
   addbutton ( "edit ~T~ech adapter",          act_editTechAdapter );
   addbutton ( "edit player data...",   act_resetPlayerData );

  addfield ("~S~elect");
   addbutton ( "Vehicle\tF2",  act_selunit );
   addbutton ( "Terrain from map\tF3",  act_selbodentyp );
   addbutton ( "Terrain from list\tctrl-F3",  act_selbodentypAll );
   addbutton ( "Object from map \tF4",  act_selbodentyp );
   addbutton ( "Object from list \tctrl-F4",  act_selobjectAll );
   addbutton ( "Building Type\tF5", act_selbuilding );
   addbutton ( "Mine\tF6", act_selmine );
   
   
  addfield ("~T~ools");
   // addbutton ( "~V~iew map\tV",            act_viewmap );
   // addbutton ( "~S~how palette",             act_showpalette );
   // addbutton ( "~R~ebuild display\tctrl+R",   act_repaintdisplay );
   // currentMenu->addSeparator();
   addbutton ( "~M~ap generator\tG",          act_mapgenerator );
   addbutton ( "Sm~o~oth coasts",          act_smoothcoasts );
   addbutton ( "~U~nitset transformation",    act_unitsettransformation );
   addbutton ( "map ~t~ransformation",        act_transformMap );
   addbutton ( "Com~p~are Resources ", act_displayResourceComparison );
   addbutton ( "Show Pipeline Net\t9", act_showPipeNet );
   addbutton ( "Generate Tech Tree", act_generateTechTree );
   addbutton ( "Mirror Map", act_mirrorMap );
   addbutton ( "Copy Area", act_copyArea );
   addbutton ( "Paste Area", act_pasteArea );
   currentMenu->addSeparator();
   addbutton ( "PBP Player Statistics", act_pbpstatistics );

   addfield ("~O~ptions");
    addbutton ( "~M~ap properties\tctrl+M",          act_changemapvals );
    addbutton ( "~C~hange players\tO",           act_changeplayers);
    addbutton ( "~E~dit events\tE",              act_events );
//    addbutton ( "unit production ~L~imitation", act_specifyunitproduction );
   currentMenu->addSeparator();
    addbutton ( "~T~oggle ResourceView\tctrl+B", act_toggleresourcemode);
    addbutton ( "~B~I ResourceMode",            act_bi_resource );
    addbutton ( "~A~sc ResourceMode",           act_asc_resource );
    addbutton ( "edit map ~P~arameters",        act_setmapparameters );
    addbutton ( "setup item ~F~ilters\tctrl+h",  act_setunitfilter );
    addbutton ( "select ~G~raphic set",         act_selectgraphicset );
    addbutton ( "show weapon range\tctrl+r", act_showweapnrange );

   addfield ("~D~evelopment");
    addbutton ( "Dump ~B~uilding",          act_dumpBuilding );
    addbutton ( "Dump all Buildings",          act_dumpAllBuildings );
    addbutton ( "Dump ~V~ehicle",          act_dumpVehicle );
    addbutton ( "Dump all Vehicles",       act_dumpAllVehicleDefinitions );
    addbutton ( "Dump ~O~bject",          act_dumpObject );
    addbutton ( "Dump all Objects",       act_dumpAllObjects );
    addbutton ( "Dump all Terrain",       act_dumpAllTerrain );
    addbutton ( "Locate Item by ~I~D",    act_locateItemByID );
   currentMenu->addSeparator();
    addbutton ( "Exchange ~G~raphics",          act_exchangeGraphics );
/*
   addfield("Debug");
    addbutton ( "Crash map editor", asc_nullPointerCrash );
    addbutton ( "TestFunctionHolder", asc_testFunction );
*/
   addfield ("~H~elp");
    addbutton ( "~U~nit Information\tctrl+U",    act_unitinfo );
    addbutton ( "unit guide dialog",             act_unitGuideDialog );
    addbutton ( "unit~S~et Information",        act_unitSetInformation );
    addbutton ( "~T~errain Information\t7",        act_terraininfo );
   currentMenu->addSeparator();
    addbutton ( "~H~elp System\tF1",             act_help );
    addbutton ( "~A~bout",                      act_about );
}


Menu::Menu ( PG_Widget *parent, const PG_Rect &rect)
    : PG_MenuBar( parent, rect, "MenuBar"),
      currentMenu(NULL)
{
   activateHotkey( KMOD_ALT );
   setup();
}  

namespace ContextMenu {

   class AutoTextContextAction : public ContextAction {
      public:
         ASCString getText( const MapCoordinate& pos )
         {
            return execactionnames[getActionID()];
         };
   };
   
class ContainerProperties: public ContextAction {
   public:
      bool available( const MapCoordinate& pos )
      {
         return actmap->getField(pos)->getContainer();
      };

      ASCString getText( const MapCoordinate& pos )
      {
         if ( actmap->getField(pos)->vehicle )
            return "edit unit properties";
         else
            return "edit building properties";
      };

      int getActionID()
      {
         return act_changeunitvals ;
      }
};

class ContainerCargo: public ContextAction {
   public:
      bool available( const MapCoordinate& pos )
      {
         return actmap->getField(pos)->getContainer() && actmap->getField(pos)->getContainer()->baseType->maxLoadableUnits > 0;
      };

      ASCString getText( const MapCoordinate& pos )
      {
         if ( actmap->getField(pos)->vehicle )
            return "edit unit cargo";
         else
            return "edit building cargo";
      };

      int getActionID()
      {
         return act_changecargo ;
      }
};

class DeleteVehicle: public ContextAction {
   public:
      bool available( const MapCoordinate& pos )
      {
         return actmap->getField(pos)->vehicle;
      };

      ASCString getText( const MapCoordinate& pos )
      {
         return "delete unit";
      };

      int getActionID()
      {
         return act_deleteunit ;
      }
};

class DeleteBuilding: public ContextAction {
   public:
      bool available( const MapCoordinate& pos )
      {
         return actmap->getField(pos)->building;
      };

      ASCString getText( const MapCoordinate& pos )
      {
        return "delete building";
      };

      int getActionID()
      {
         return act_deletebuilding;
      }
};

class ContainerProduction: public AutoTextContextAction {
   public:
      bool available( const MapCoordinate& pos )
      {
         return actmap->getField(pos)->getContainer() && actmap->getField(pos)->getContainer()->baseType->hasFunction( ContainerBaseType::InternalVehicleProduction  );
      };

      int getActionID()
      {
         return act_changeproduction ;
      }
};

class DeleteMine: public AutoTextContextAction {
   public:
      bool available( const MapCoordinate& pos )
      {
         return !actmap->getField(pos)->mines.empty() ;
      };

      int getActionID()
      {
         return act_deletemine ;
      }
};

class ChangeMineStrength: public AutoTextContextAction {
   public:
      bool available( const MapCoordinate& pos )
      {
         return !actmap->getField(pos)->mines.empty() ;
      };

      int getActionID()
      {
         return act_changeminestrength ;
      }
};

class FieldResources: public AutoTextContextAction {
   public:
      bool available( const MapCoordinate& pos )
      {
         return true;
      };

      int getActionID()
      {
         return act_changeresources;
      }
};

class DeleteTopObject: public AutoTextContextAction {
   public:
      bool available( const MapCoordinate& pos )
      {
         return !actmap->getField(pos)->objects.empty();
      };

      int getActionID()
      {
         return act_deletetopmostobject;
      }
};

class DeleteAllObjects: public AutoTextContextAction {
   public:
      bool available( const MapCoordinate& pos )
      {
         return !actmap->getField(pos)->objects.empty();
      };

      int getActionID()
      {
         return act_deleteallobjects;
      }
};


} // namespace ContextMenu

Maped_MainScreenWidget::Maped_MainScreenWidget( PG_Application& application )
              : MainScreenWidget( application ),
              vehicleSelector( NULL ), buildingSelector( NULL ), objectSelector(NULL), terrainSelector(NULL), mineSelector(NULL),
              weatherSelector( NULL ), selectionName(NULL), selectionName2(NULL), coordinateDisplay(NULL), currentSelectionWidget(NULL), contextMenu(NULL)
{

   setup( false, PG_Rect(15,30,Width() - 200, Height() - 73) );
   mapDisplay->mouseButtonOnField.connect( sigc::ptr_fun( &mousePressedOnField ));
   mapDisplay->mouseButtonOnField.connect( sigc::mem_fun( *this, &Maped_MainScreenWidget::clickOnMap ));
   mapDisplay->mouseDraggedToField.connect( sigc::ptr_fun( &mouseDraggedToField ));

   

   
   setupStatusBar();
   
   menu = new Menu(this, PG_Rect(15,0,Width()-200,20));

   
   PG_Application::GetApp()->sigKeyDown.connect( sigc::mem_fun( *this, &Maped_MainScreenWidget::eventKeyDown ));
   
   int xpos = Width() - 150;
   int w = 140;
   int ypos = 180;

   PG_Button* buttonC = new PG_Button( this, PG_Rect( xpos, ypos, w, 20), "Clear Selection" );
   buttonC->sigClick.connect( sigc::hide( sigc::mem_fun( *this, &Maped_MainScreenWidget::clearSelection )));
   ypos += 25;

   
   PG_Button* button = new PG_Button( this, PG_Rect( xpos, ypos, w, 20), "Select Vehicle" );
   button->sigClick.connect( sigc::hide( sigc::mem_fun( *this, &Maped_MainScreenWidget::selectVehicle )));
   ypos += 25;
   
   PG_Button* button2 = new PG_Button( this, PG_Rect( xpos, ypos, w, 20), "Select Building" );
   button2->sigClick.connect( sigc::hide( sigc::mem_fun( *this, &Maped_MainScreenWidget::selectBuilding )));
   ypos += 25;

   PG_Button* button3 = new PG_Button( this, PG_Rect( xpos, ypos, w - 50, 20), "Sel. Object" );
   button3->sigClick.connect( sigc::hide( sigc::mem_fun( *this, &Maped_MainScreenWidget::selectObject )));

   PG_Button* button3b = new PG_Button( this, PG_Rect( xpos+ w - 45, ypos, 45, 20), "List" );
   button3b->sigClick.connect( sigc::hide( sigc::mem_fun( *this, &Maped_MainScreenWidget::selectObjectList )));
   ypos += 25;


   PG_Button* button4 = new PG_Button( this, PG_Rect( xpos, ypos, w - 50, 20), "Sel. Terrain" );
   button4->sigClick.connect( sigc::hide( sigc::mem_fun( *this, &Maped_MainScreenWidget::selectTerrain )));

   PG_Button* button4b = new PG_Button( this, PG_Rect( xpos + w - 45, ypos, 45, 20), "List" );
   button4b->sigClick.connect( sigc::hide( sigc::mem_fun( *this, &Maped_MainScreenWidget::selectTerrainList )));
   ypos += 25;

   PG_Button* button5 = new PG_Button( this, PG_Rect( xpos, ypos, w, 20), "Select Mine" );
   button5->sigClick.connect( sigc::hide( sigc::mem_fun( *this, &Maped_MainScreenWidget::selectMine )));
   ypos += 25;
    
   PG_Button* button6 = new PG_Button( this, PG_Rect( xpos, ypos, w, 20), "Lua Brush" );
   button6->sigClick.connect( sigc::hide( sigc::mem_fun( *this, &Maped_MainScreenWidget::selectLuaBrush )));
   ypos += 25;
   
   
   new PG_Label( this, PG_Rect( xpos, ypos, w/2 - 5, 20), "Brush:");
   brushSelector = new DropDownSelector( this, PG_Rect( xpos+w/2+5, ypos, w/2-5, 20));
   brushSelector->AddItem("1");
   brushSelector->AddItem("3");
   brushSelector->AddItem("5");
   brushSelector->selectionSignal.connect( sigc::mem_fun( *this, &Maped_MainScreenWidget::brushChanged ));
   ypos += 25;
   
  
   weatherSelector = new DropDownSelector( this, PG_Rect( xpos, ypos, w, 20));
   for ( int i = 0; i < cwettertypennum; ++i )
      weatherSelector->AddItem( cwettertypen[i] );
    ypos += 25;  
            
   weatherSelector->selectionSignal.connect( sigc::mem_fun( selection, &SelectionHolder::setWeather ) );
//   weatherSelector->selectionSignal.connect( sigc::mem_fun( *currentSelectionWidget, &SelectionItemWidget::Update ));
   

   playerSelector = new DropDownSelector( this, PG_Rect( xpos, ypos, w, 20));
   for ( int i = 0; i < 9; ++i )
      playerSelector->AddItem( "Player " + ASCString::toString(i) );
   playerSelector->selectionSignal.connect( sigc::mem_fun( selection, &SelectionHolder::setPlayer ) );
   ypos += 25;

   selection.playerChanged.connect( sigc::mem_fun( *this, &Maped_MainScreenWidget::playerChanged ));

   
   currentSelectionWidget = new SelectionItemWidget( this, PG_Rect( Width() - BuildingItem::Width() - 10, ypos, BuildingItem::Width(), BuildingItem::Height() ) );
   ypos += BuildingItem::Height() + 5;

   selectionName = new PG_Label( this, PG_Rect( xpos, ypos, currentSelectionWidget->Width(), 20 ));
   ypos += 25;
   selectionName2 = new PG_Label( this, PG_Rect( xpos, ypos, currentSelectionWidget->Width(), 20 ));
   ypos += 25;
   
   selection.selectionChanged.connect( sigc::mem_fun( *currentSelectionWidget, &SelectionItemWidget::set ));
   selection.selectionChanged.connect( sigc::mem_fun( *this, &Maped_MainScreenWidget::selectionChanged ));

   spawnOverviewMapPanel( "Mapeditor_OverviewMap" );

   addContextAction( new ContextMenu::ContainerProperties );
   addContextAction( new ContextMenu::ContainerCargo );
   addContextAction( new ContextMenu::ContainerProduction );
   addContextAction( new ContextMenu::DeleteVehicle );
   addContextAction( new ContextMenu::DeleteBuilding );
   addContextAction( new ContextMenu::ChangeMineStrength );
   addContextAction( new ContextMenu::DeleteMine );
   addContextAction( new ContextMenu::FieldResources );
   addContextAction( new ContextMenu::DeleteTopObject );
   addContextAction( new ContextMenu::DeleteAllObjects );
   
   weaponRangeLayer = new UnitWeaponRangeLayer();
   mapDisplay->addMapLayer( weaponRangeLayer, "weaprange" );
   
}

void Maped_MainScreenWidget::showWeaponRange( GameMap* gamemap, const MapCoordinate& pos )
{
   weaponRangeLayer->operateField( gamemap, pos );
}


void Maped_MainScreenWidget::playerChanged( int player )
{
   static int recursionPreventer = 0;
   recursionPreventer++;
   if ( recursionPreventer == 1 ) 
      playerSelector->SelectItem( player );
   recursionPreventer--;
}






bool Maped_MainScreenWidget::eventMouseButtonDown (const SDL_MouseButtonEvent *button)
{
   return false;
}

void Maped_MainScreenWidget::setupStatusBar()
{
   int x = mapDisplay->my_xpos;
   int y =  mapDisplay->my_ypos + mapDisplay->Height() + 25;
   int height = 20;

   messageLine = new PG_Label ( this, PG_Rect( x, y, 200, height ) );
   messageLine->SetFontSize(11);
   x += 210;
   

   coordinateDisplay = new PG_Label ( this, PG_Rect( x, y, 80, height ) );
   coordinateDisplay->SetFontSize(11);
   
   updateFieldInfo.connect( sigc::mem_fun( *this, &Maped_MainScreenWidget::updateStatusBar ));
   cursorMoved.connect( sigc::mem_fun( *this, &Maped_MainScreenWidget::updateStatusBar ));
}

void Maped_MainScreenWidget::updateStatusBar()
{
   MapCoordinate pos = actmap->getCursor();
   coordinateDisplay->SetText( ASCString::toString( pos.x) + " / " + ASCString::toString( pos.y ));
}

void Maped_MainScreenWidget::selectionChanged( const Placeable* item )
{
   if ( item ) {
      selectionName->SetText( item->getName() );
      
      const MapComponent* mc = dynamic_cast<const MapComponent*>(item);
      if (  mc ) 
         selectionName2->SetText( "ID: " + ASCString::toString( mc->getItemType()->getID() ) );
      else
         selectionName2->SetText( "" );
   } else {
      selectionName->SetText( "" );
      selectionName2->SetText( "" );
   }
}


void Maped_MainScreenWidget::brushChanged( int i )
{
   selection.brushSize = i+1;
   if ( selection.brushSize < 1 )
      selection.brushSize = 1;
}

void Maped_MainScreenWidget:: addContextAction( ContextAction* contextAction )
{
   contextActions.push_back( contextAction );
}


bool Maped_MainScreenWidget::clickOnMap( const MapCoordinate& field, const SPoint& pos, bool changed, int button, int prio)
{
   if ( prio > 1 )
      return false;
   
   if( button == 3 ) {

      mapDisplay->cursor.goTo( field );
      
      int counter = 0;
      for ( deallocating_vector<ContextAction*>::iterator i = contextActions.begin(); i != contextActions.end(); ++i )
         if ( (*i)->available( field ))
            ++counter;

      if ( contextMenu ) {
         delete contextMenu;
         contextMenu = NULL;
         return true;
      }

      if ( !counter )
         return false;

      
      contextMenu = new PG_PopupMenu( this, pos.x, pos.y );

      for ( deallocating_vector<ContextAction*>::iterator i = contextActions.begin(); i != contextActions.end(); ++i )
         if ( (*i)->available( field )) {
            contextMenu->addMenuItem( (*i)->getText( field ), (*i)->getActionID() );
         }

      contextMenu->sigSelectMenuItem.connect( sigc::mem_fun( *this, &Maped_MainScreenWidget::runContextAction   ));
      
      contextMenu->Show();
      return true;
   } else {
      if ( contextMenu ) {
         delete contextMenu;
         contextMenu = NULL;
         return true;
      }
   }
   return false;
}

bool Maped_MainScreenWidget::runContextAction  (PG_PopupMenu::MenuItem* menuItem )
{
   execaction_ev( tuseractions( menuItem->getId() ) );

   return true;
}

#ifdef WIN32
# include "win32/win32-errormsg.h"
# include  "win32/msvc/mdump.h"
#endif


bool Maped_MainScreenWidget::eventKeyUp(const PG_MessageObject* o, const SDL_KeyboardEvent* key)
{
   if ( key->keysym.sym ==  SDLK_RCTRL || key->keysym.sym == SDLK_LCTRL ) {
      execaction_ev( act_releaseControlPanel );
      return true;
   }
   return false;
}

void helperFunction()
{
   /*
   set<int> s;
   for ( int i = 0; i < 1083; ++i )
      s.insert(i);
   for ( int y = 0; y < actmap->ysize; ++y)
      for ( int x = 0; x < actmap->xsize; ++x) 
         if ( s.find( actmap->getField(x,y)->typ->terraintype->id ) != s.end() )
            s.erase( actmap->getField(x,y)->typ->terraintype->id );

   int start  = -1;
   for ( int i = 0; i <= 1083; ++i ) {
      if ( s.find(i) != s.end() ) {
         if ( start < 0 )
            start = i;
      } else {
         if ( start >= 0 ) {
            cout << start << "-" << i-1 << " ";
            start = -1;
         }
      }
   }
   cout << "\n";
   */
}


bool Maped_MainScreenWidget::eventKeyDown(const PG_MessageObject* o, const SDL_KeyboardEvent* key)
{
   int mod = SDL_GetModState() & ~(KMOD_NUM | KMOD_CAPS | KMOD_MODE);

   if ( key->keysym.sym ==  SDLK_RCTRL || key->keysym.sym == SDLK_LCTRL ) {
      execaction_ev( act_openControlPanel );
      return true;
   }

   if ( !mod  ) {
      switch ( key->keysym.sym ) {
         case SDLK_KP_ENTER:
         case SDLK_RETURN:
         case SDLK_SPACE: execaction_ev( act_primaryAction );
                        return true;
                        
         case SDLK_F1:  execaction_ev(act_help);
                        return true;
         case SDLK_F2 : execaction_ev(act_selunit);
                        return true;
         case SDLK_F3:  execaction_ev(act_selbodentyp);
                        return true;
         case SDLK_F4 : execaction_ev(act_selobject);
                        return true;
         case SDLK_F5 : execaction_ev(act_selbuilding);
                        return true;
         case SDLK_F6 : execaction_ev(act_selmine);
                        return true;

         case SDLK_F11: helperFunction();
                        return true;
         case SDLK_a:   execaction_ev(act_movebuilding);
                        return true;
         case SDLK_b:   execaction_ev(act_changeresources);
                        return true;
         case SDLK_c:   execaction_ev(act_changecargo);
                        return true;
         case SDLK_d : execaction_ev(act_changeterraindir);
                        return true;
         case SDLK_e:  execaction_ev(act_events);
                        return true;
         case SDLK_f:  execaction_ev(act_changeproduction);
                        return true;
         case SDLK_g: execaction_ev(act_mapgenerator);
                        return true;
         case SDLK_h: execaction_ev(act_setactivefieldvals);
                        return true;
         case SDLK_DELETE: execaction_ev(act_deletething);
                        return true;
         case SDLK_l : execaction_ev(act_showpalette);
                        return true;
         case SDLK_m : execaction_ev(act_changeminestrength);
                        return true;
         case SDLK_o: execaction_ev(act_changeplayers);
                        return true;

         case SDLK_p: execaction_ev(act_changeunitvals);
                        return true;

         case SDLK_r: execaction_ev(act_resizemap);
                        return true;

         case SDLK_s : execaction_ev(act_savemap);
                        return true;

         case SDLK_v:   execaction_ev(act_viewmap);
                        return true;

         case SDLK_x:   execaction_ev(act_mirrorcursorx);
                        return true;

         case SDLK_y:   execaction_ev(act_mirrorcursory);
                        return true;

         case SDLK_z:   execaction_ev(act_setzoom );
                        return true;

         case SDLK_7 :  execaction_ev(act_terraininfo);
                        return true;
                        
         case SDLK_8 :  execaction_ev(act_placemine);
                        return true;

         case SDLK_9:   execaction_ev(act_showPipeNet);
                        return true;


         case SDLK_TAB: execaction_ev(act_switchmaps );
                        return true;

         case SDLK_ESCAPE: execaction_ev(act_clearSelection);
                        return true;
         case SDLK_PLUS:
         case SDLK_KP_PLUS: execaction_ev( act_increase_zoom );
            return true;

         case SDLK_MINUS:
         case SDLK_KP_MINUS: execaction_ev( act_decrease_zoom );
            return true;
            
            default:;
       }
   }
      
   if ( mod & KMOD_CTRL ) {
      switch ( key->keysym.sym ) {
         case SDLK_F3 : execaction_ev(act_selbodentypAll);
                        return true;
   
         case SDLK_F4 : execaction_ev(act_selobjectAll);
                        return true;

         case SDLK_SPACE: execaction_ev( act_primaryAction );
                        return true;
                        
         default:;
   
       } 
      switch ( key->keysym.unicode ) {
         case 1:  // A
            execaction_ev(act_setupalliances);
            return true;
   
         case 2:  // B
            execaction_ev(act_toggleresourcemode);
            return true;
   
         case 3:  // C
            execaction_ev(act_copyToClipboard);
            return true;

         case 6: // F
            execaction_ev(act_createresources);
            return true;
   
         case 7: //G
            execaction_ev(act_maptopcx);
            return true;
   
         case 8:  // H
            execaction_ev(act_setunitfilter);
            return true;
   
         case 9:  // I
            execaction_ev (act_import_bi_map );
            return true;
   
         case 12:  // L
            execaction_ev(act_loadmap);
            return true;
   
         case 13:  // M
            execaction_ev(act_changemapvals);
            return true;
   
         case 14:  // N
            execaction_ev(act_newmap);
            return true;
   
         case 15:  // O
            execaction_ev(act_polymode);
            return true;
   
         case 16:  // P
            execaction_ev(act_changeproduction);
            return true;
   
         case 17:  // Q
            execaction_ev(act_end);
            return true;
                        
         case 18: // R
            execaction_ev(act_showweapnrange);
            return true;
   
         case 19 :  // S
            execaction_ev(act_savemap);
            return true;

         case 21: // U
            execaction_ev(act_unitinfo);
            return true;
   
         case 22:  // V
            execaction_ev(act_pasteFromClipboard);
            return true;
   
         case 23: // W
            execaction_ev(act_setactweatherglobal);
            return true;

         case 24: // X
            execaction_ev(act_cutToClipboard);
            return true;

      }
   }

   if ( mod & KMOD_SHIFT ) {
      switch ( key->keysym.sym ) {
         case SDLK_d: execaction_ev(act_changeunitdir);
            return true;
         case SDLK_F3 : execaction_ev(act_selbodentypAll);
                        return true;
   
         case SDLK_F4 : execaction_ev(act_selobjectAll);
                        return true;
         default:;
      }
   }

   if ( mod & KMOD_ALT ) {
      switch ( key->keysym.sym ) {
            case SDLK_RETURN:
               getPGApplication().toggleFullscreen();
               return true;

         case SDLK_F11:
               /*
            if ( mod & KMOD_SHIFT ) {
            if (choice_dlg("Do you really want to crash ASC ?","~y~es","~n~o") == 1) {
            char* c = NULL;
            *c = 1;
      }
      }
               */
         {  for ( Player::VehicleList::iterator i = actmap->player[1].vehicleList.begin(); i != actmap->player[1].vehicleList.end(); ++i )
            (*i)->direction = 3;
         } 
         return true;
         
         default:;
      }
   }

   return false;
}



class MapItemSelectionWindow : public ItemSelectorWindow {
   public:
      MapItemSelectionWindow( PG_Widget *parent, const PG_Rect &r , const ASCString& title, SelectionItemFactory* itemFactory )
         : ItemSelectorWindow( parent, r, title, itemFactory ) {};
      
      void itemSelected( const SelectionWidget* )
      {
         if ( CGameOptions::Instance()->maped_modalSelectionWindow )
            Hide();
         QuitModal();
      }

};

typedef PG_Window* PG_WindowPointer;

template <class ItemType> 
void showSelectionWindow( PG_Widget* parent, PG_WindowPointer &selectionWindow, const ItemRepository<ItemType>& itemRepository  )
{
   if ( !selectionWindow ) {
      ItemSelectorWindow* sw = new MapItemSelectionWindow( parent, PG_Rect( parent->Width()-300, 100, 280, parent->Height()-150), "select item", new MapItemTypeWidgetFactory< MapItemTypeWidget<ItemType> >(itemRepository) );

      filtersChangedSignal.connect( sigc::mem_fun( *sw, &ItemSelectorWindow::reLoad ));
      selectionWindow = sw;
   }
   
   selectionWindow->Show();
   selectionWindow->RunModal();
   if ( CGameOptions::Instance()->maped_modalSelectionWindow )
      selectionWindow->Hide();
      
}


bool Maped_MainScreenWidget :: clearSelection()
{
   selection.clear();
   return true;
}


bool Maped_MainScreenWidget :: selectVehicle()
{
   showSelectionWindow( this, vehicleSelector, vehicleTypeRepository );
   return true;
}


bool Maped_MainScreenWidget :: selectBuilding()
{
   showSelectionWindow( this, buildingSelector, buildingTypeRepository );
   return true;
}

bool Maped_MainScreenWidget :: selectObject()
{
   execaction_ev( act_switchmaps );
   // showSelectionWindow( this, objectSelector, objectTypeRepository );
   return true;
}

bool Maped_MainScreenWidget :: selectObjectList()
{
   showSelectionWindow( this, objectSelector, objectTypeRepository );
   return true;
}


bool Maped_MainScreenWidget :: selectTerrain()
{
   execaction_ev( act_switchmaps );
   // showSelectionWindow( this, terrainSelector, terrainTypeRepository );
   return true;
}

bool Maped_MainScreenWidget :: selectTerrainList()
{
   showSelectionWindow( this, terrainSelector, terrainTypeRepository );
   return true;
}


bool Maped_MainScreenWidget :: selectMine()
{
   showSelectionWindow( this, mineSelector, mineTypeRepository );
   return true;
}

bool Maped_MainScreenWidget :: selectLuaBrush()
{
   ASCString file = selectFile( "*.brush.lua", true );
   if ( file.empty () )
      return true;
   
   LuaBrush brush ( file );
   selection.setSelection( brush );

   
   return true;
}



void displaymessage2( const char* formatstring, ... )
{

   ASCString s;
   
    std::va_list arg_ptr;
    va_start ( arg_ptr, formatstring );

    s.vaformat( formatstring, arg_ptr );

    va_end ( arg_ptr );

    if ( mainScreenWidget )
       mainScreenWidget->displayMessage(s);
}