Advanced Strategic Command
fieldmarker.cpp
Go to the documentation of this file.
1 /*
2  This file is part of Advanced Strategic Command; http://www.asc-hq.de
3  Copyright (C) 1994-2010 Martin Bickel and Marc Schellenberger
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; see the file COPYING. If not, write to the
17  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  Boston, MA 02111-1307 USA
19 */
20 
21 #include <pglistboxitem.h>
22 #include "../gamemap.h"
23 #include "fieldmarker.h"
24 #include "../mapdisplay.h"
25 #include "../overviewmappanel.h"
26 #include "../spfst.h"
27 #include "../mainscreenwidget.h"
28 
29 class CoordinateItem : public PG_ListBoxItem {
30  ASCString toString( const MapCoordinate& pos ) {
31  ASCString s;
32  s.format( "%d / %d", pos.x, pos.y );
33  return s;
34  };
35  MapCoordinate p;
36 
37  public:
38  CoordinateItem( PG_Widget *parent, const MapCoordinate& pos ) : PG_ListBoxItem( parent, 20, toString( pos ) ), p(pos) {};
39  MapCoordinate getPos() const { return p; };
40 };
41 
42 
43 SelectFromMap::SelectFromMap( CoordinateList& list, GameMap* map, bool justOne, bool readOnly ) : ASC_PG_Dialog( NULL, PG_Rect( PG_Application::GetScreenWidth() - 150, PG_Application::GetScreenHeight() - 300, 150, 300 ), "Select Fields" ), listbox(NULL), actmap( map ), coordinateList (list)
44 {
45  this->justOne = justOne;
46  this->readOnly = readOnly;
47 
48 
49  listbox = new PG_ListBox( this, PG_Rect( 10, 30, 130, readOnly ? 210 : 180 ));
50  listbox->sigSelectItem.connect( sigc::mem_fun( *this, &SelectFromMap::listItemClicked ));
51 
52  if ( !readOnly ) {
53  PG_Button* m = new PG_Button ( this, PG_Rect( 10, 230, 130, 20 ), "mark (~space~)");
54  m->sigClick.connect( sigc::hide( sigc::mem_fun( *this, &SelectFromMap::mark )));
55  }
56 
57 
58  PG_Button* b = new PG_Button ( this, PG_Rect( 10, 270, 130, 20 ), "~O~K");
59  b->sigClick.connect( sigc::hide( sigc::mem_fun( *this, &SelectFromMap::closeDialog )));
60 
63  md->mouseButtonOnField.connect( sigc::mem_fun( *this, &SelectFromMap::markField2 ));
64  oldprio = md->setSignalPriority( 2 );
65 
66 
68  if ( !list.empty() ) {
69  md->cursor.goTo( *list.begin() );
70  actmap->getCursor() = *list.begin();
71  cursorMoved();
72  }
73 
74  updateList();
75 };
76 
77 
78 bool SelectFromMap::ProcessEvent ( const SDL_Event * event,bool bModal )
79 {
80  if ( md->ProcessEvent( event, bModal ) )
81  return true;
82 
83  if ( omp && omp->ProcessEvent( event, bModal ) )
84  return true;
85 
86  return ASC_PG_Dialog::ProcessEvent( event, bModal );
87 }
88 
90  return !readOnly && actmap->getField( pos ) != NULL;
91 };
92 
93 void SelectFromMap::showFieldMarking( const CoordinateList& coordinateList )
94 {
95  actmap->cleartemps( 7 );
96  for ( CoordinateList::const_iterator i = coordinateList.begin(); i != coordinateList.end(); ++i ) {
97  MapField* fld = actmap->getField( *i );
98  if ( fld )
99  fld->setaTemp(1);
100  }
101  repaintMap();
102 }
103 
104 bool SelectFromMap::markField2( const MapCoordinate& pos, const SPoint& mouse, bool cursorChanged, int button, int prio )
105 {
106  if ( prio <= 2 )
107  markField( pos );
108  return true;
109 }
110 
111 
113 {
114  if ( !accept(pos))
115  return false;
116 
117  if( justOne ) {
118  coordinateList.clear();
119  coordinateList.push_back ( pos );
120  } else {
121  CoordinateList::iterator i = find( coordinateList.begin(), coordinateList.end(), pos );
122  if ( i == coordinateList.end() ) {
123  coordinateList.push_back ( pos );
124  } else {
125  coordinateList.erase ( i );
126  }
127  }
129  updateList();
130  return true;
131 }
132 
133 
135 {
136  MapCoordinate pos = actmap->getCursor();
137  return markField( pos );
138 }
139 
140 bool SelectFromMap::eventKeyDown (const SDL_KeyboardEvent *key)
141 {
142  if ( key->type == SDL_KEYDOWN ) {
143  if ( key->keysym.sym == SDLK_SPACE ) {
144  mark();
145  return true;
146  }
147  if ( key->keysym.sym == SDLK_RETURN || key->keysym.sym == SDLK_KP_ENTER ) {
148  if ( isOk() )
149  QuitModal();
150  return true;
151  }
152  }
153  return ASC_PG_Dialog::eventKeyDown( key );
154 }
155 
157 {
158  listbox->DeleteAll();
159  for ( CoordinateList::iterator i = coordinateList.begin(); i != coordinateList.end(); ++i )
160  new CoordinateItem( listbox, *i );
161 
162  listbox->Show();
163 }
164 
165 bool SelectFromMap::listItemClicked( PG_ListBoxBaseItem* item )
166 {
167  if ( item ) {
168  CoordinateItem* i = dynamic_cast<CoordinateItem*>(item);
169  if ( i ) {
170  md->cursor.goTo( i->getPos() );
171  actmap->getCursor() = i->getPos();
172  cursorMoved();
173  }
174  }
175  return true;
176 }
177 
178 
179 
180 void SelectFromMap::Show( bool fade)
181 {
182  getMainScreenWidget()->BringToFront();
183  md->BringToFront();
184  md->Show();
185  ASC_PG_Dialog::Show( fade );
186  repaintMap();
187 }
188 
190 {
191  md->setSignalPriority( oldprio );
192 
193  getMainScreenWidget()->SendToBack();
194  actmap->cleartemps(7);
195  repaintMap();
196 }
197 
198 
200  return actmap->getField( pos ) && actmap->getField( pos )->building;
201 };
202 
204  return actmap->getField( pos ) && actmap->getField( pos )->vehicle;
205 };
206 
void setaTemp(Uint8 atemp)
Various algorithms need to store some information in the fields they process.
Definition: mapfield.cpp:49
void cleartemps(int b=-1, int value=0)
Definition: gamemap.cpp:964
MapCoordinate & getCursor()
Definition: gamemap.cpp:933
Vehicle * vehicle
Definition: mapfield.h:89
bool listItemClicked(PG_ListBoxBaseItem *item)
CoordinateList & coordinateList
Definition: fieldmarker.h:48
virtual bool eventKeyDown(const SDL_KeyboardEvent *key)
Definition: paradialog.cpp:531
virtual bool accept(const MapCoordinate &pos)
Definition: fieldmarker.cpp:89
sigc::signal< void > repaintMap
}@
Definition: spfst.cpp:45
a single field of the map
Definition: mapfield.h:26
ASCString & format(const charT *pFormat,...)
Definition: ascstring.cpp:78
struct MapDisplayPG::Cursor cursor
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
virtual bool accept(const MapCoordinate &pos)
bool eventKeyDown(const SDL_KeyboardEvent *key)
void Show(bool fade=false)
MapCoordinate getPos() const
Definition: fieldmarker.cpp:39
Coordinate on the twodimensional map.
Definition: typen.h:202
int setSignalPriority(int priority)
Definition: mapdisplay.cpp:989
Adapter class for using Paragui Dialogs in ASC. This class transfers the event control from ASC to Pa...
Definition: paradialog.h:127
virtual bool mark()
virtual bool markField(const MapCoordinate &pos)
bool closeDialog()
Definition: fieldmarker.h:59
SDLmm::SPoint SPoint
Definition: surface.h:27
MapDisplayPG * getMapDisplay()
virtual void showFieldMarking(const CoordinateList &coordinateList)
Definition: fieldmarker.cpp:93
sigc::signal< bool, const MapCoordinate &, const SPoint &, bool, int, int > mouseButtonOnField
Signal that is fired when the mouse is pressed on a valid field, after the cursor evaluation has been...
Definition: mapdisplay.h:314
vector< MapCoordinate > CoordinateList
Definition: fieldmarker.h:45
GameMap * actmap
Definition: fieldmarker.h:47
virtual bool accept(const MapCoordinate &pos)
Building * building
Definition: mapfield.h:102
SelectFromMap(CoordinateList &list, GameMap *map, bool justOne=false, bool readOnly=false)
Definition: fieldmarker.cpp:43
void goTo(const MapCoordinate &position)
CoordinateItem(PG_Widget *parent, const MapCoordinate &pos)
Definition: fieldmarker.cpp:38
MainScreenWidget * getMainScreenWidget()
bool ProcessEvent(const SDL_Event *event, bool bModal=false)
Definition: fieldmarker.cpp:78
sigc::signal< void > cursorMoved
Definition: spfst.cpp:48
GameMap * actmap
Definition: spfst.cpp:64
OverviewMapPanel * getOverviewMapPanel()
virtual bool isOk()
Definition: fieldmarker.h:58
The map. THE central structure of ASC, which holds everything not globally available together...
Definition: gamemap.h:182
MapField * getField(int x, int y)
Definition: gamemap.h:465