Adonthell  0.4
mapview.h
Go to the documentation of this file.
1 /*
2  $Id: mapview.h,v 1.29 2003/02/23 23:14:34 ksterker Exp $
3 
4  Copyright (C) 1999/2000/2001 Alexandre Courbot
5  Part of the Adonthell Project http://adonthell.linuxgames.com
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 
16 /**
17  * @file mapview.h
18  * @author Alexandre Courbot <alexandrecourbot@linuxgames.com>
19  *
20  * @brief Declares the mapview class.
21  *
22  *
23  */
24 
25 
26 
27 #ifndef MAPVIEW_H_
28 #define MAPVIEW_H_
29 
30 #include "landmap.h"
31 #include "python_class.h"
32 
33 
34 /**
35  * Allows you to display a landmap on a specified area of a surface.
36  *
37  * This class just acts as a "camera" which takes snapshots of a landmap.
38  * It's size can be specified, a schedule can be set to
39  * update it's movment. Nothing prevents you from having several mapviews
40  * on the same map that displays each a different (or similar) part of the
41  * landmap.
42  *
43  */
44 class mapview : public drawable
45 {
46 
47 public:
48 
49  /**
50  * Default constructor.
51  *
52  */
53  mapview ();
54 
55  /**
56  * Destructor.
57  *
58  */
59  ~mapview ();
60 
61  /**
62  * Attach/Detach a map
63  *
64  */
65 
66  //@{
67 
68 
69  /**
70  * Sets which map this mapview will display.
71  *
72  * @param m pointer to the map that will be displayed by the mapview.
73  */
74  void attach_map (landmap * m);
75 
76  /**
77  * Stops displaying a map.
78  *
79  */
80  void detach_map ();
81 
82  //@}
83 
84 
85  /**
86  * Position settings
87  *
88  */
89 
90  //@{
91 
92  /**
93  * Sets the position of the top-left corner of the mapview on the map.
94  *
95  * You'll probably don't want to use this method. To center the mapview
96  * on a precise position, see center_on () instead.
97  *
98  * @param sm submap.
99  * @param x X position.
100  * @param y Y position.
101  * @param ox X offset.
102  * @param oy Y offset.
103  *
104  * @sa center_on ()
105  *
106  */
107  s_int8 set_pos (u_int16 sm, u_int16 x, u_int16 y, s_int16 ox = 0, s_int16 oy = 0);
108 
109  /**
110  * Sets the position of the center of the mapview on the map.
111  *
112  * @param sm submap.
113  * @param x X position.
114  * @param y Y position.
115  * @param ox X offset.
116  * @param oy Y offset.
117  *
118  */
119  s_int8 center_on (u_int16 sm, u_int16 x, u_int16 y, s_int16 ox = 0, s_int16 oy = 0);
120 
121  //@}
122 
123 
124  /**
125  * Position information
126  *
127  */
128 
129  //@{
130 
131  /**
132  * Returns the submap this mapview is displaying.
133  *
134  *
135  * @return current submap this mapview is displaying.
136  */
138  {
139  return currentsubmap_;
140  }
141 
142  /**
143  * Returns the X position of the mapview.
144  *
145  *
146  * @return X position of the mapview.
147  */
148  u_int16 posx () const
149  {
150  return posx_;
151  }
152 
153  /**
154  * Returns the Y position of the mapview.
155  *
156  *
157  * @return Y position of the mapview.
158  */
159  u_int16 posy () const
160  {
161  return posy_;
162  }
163 
164  /**
165  * Returns the X offset of the mapview.
166  *
167  *
168  * @return X offset of the mapview.
169  */
170  u_int16 offx () const
171  {
172  return offx_;
173  }
174 
175  /**
176  * Returns the Y offset of the mapview.
177  *
178  *
179  * @return Y offset of the mapview.
180  */
181  u_int16 offy () const
182  {
183  return offy_;
184  }
185 
186  //@}
187 
188  /**
189  * Basic movment
190  *
191  */
192 
193  //@{
194 
195  /**
196  * Returns whether it is possible to scroll to right. A scrolling is impossible
197  * if the mapview is at the map limits.
198  *
199  *
200  * @return true if a right scrolling is possible, false otherwise.
201  */
203  {
204  s_int32 tpx = posx () * MAPSQUARE_SIZE + offx () + length ();
205  return (tpx < m_map->submap[currentsubmap_]->area_length () * MAPSQUARE_SIZE);
206  }
207 
208  /**
209  * Returns whether it is possible to scroll to left. A scrolling is impossible
210  * if the mapview is at the map limits.
211  *
212  *
213  * @return true if a left scrolling is possible, false otherwise.
214  */
216  {
217  return (posx_ || offx_);
218  }
219 
220  /**
221  * Returns whether it is possible to scroll to up. A scrolling is impossible
222  * if the mapview is at the map limits.
223  *
224  *
225  * @return true if a up scrolling is possible, false otherwise.
226  */
228  {
229  return (posy_ || offy_);
230  }
231 
232  /**
233  * Returns whether it is possible to scroll to down. A scrolling is impossible
234  * if the mapview is at the map limits.
235  *
236  *
237  * @return true if a down scrolling is possible, false otherwise.
238  */
240  {
241  s_int32 tpy = posy () * MAPSQUARE_SIZE + offy () + height ();
242  return (tpy < m_map->submap[currentsubmap_]->area_height () * MAPSQUARE_SIZE);
243  }
244 
245  /**
246  * Scrolls 1 pixel to right, if possible.
247  *
248  */
249  void scroll_right ();
250 
251  /**
252  * Scrolls 1 pixel to left, if possible.
253  *
254  */
255  void scroll_left ();
256 
257  /**
258  * Scrolls 1 pixel to down, if possible.
259  *
260  */
261  void scroll_down ();
262 
263  /**
264  * Scrolls 1 pixel to up, if possible.
265  *
266  */
267  void scroll_up ();
268 
269  //@}
270 
271 
272  /**
273  * State saving/loading
274  *
275  */
276 
277  //@{
278 
279  /**
280  * Restore the mapview's state from an opened file.
281  *
282  * @param file the opened file from which to load the state.
283  *
284  * @return 0 in case of success, error code otherwise.
285  */
286  s_int8 get_state (igzstream& file);
287 
288  /**
289  * Saves the mapview's state into an opened file.
290  *
291  * @param file the opened file where to the state.
292  *
293  * @return 0 in case of success, error code otherwise.
294  */
295  s_int8 put_state (ogzstream& file);
296 
297  //@}
298 
299  /**
300  * Resize the mapview. The parameters are given in pixels.
301  *
302  * @param l new length.
303  * @param h new height.
304  */
305  void resize (u_int16 l, u_int16 h);
306 
307  /**
308  * Assign a schedule to the mapview.
309  *
310  * The schedule's filename will be \e "scripts/schedules/mapviews/<file>.py".
311  *
312  * @param file name of the schedule to use.
313  * @param args Python tuple containing extra arguments passed to the class constructor.
314  *
315  * @warning the args tuple argument MUST ONLY contain strings or integers, as it will
316  * be saved with the mapcharacter state by python::put_tuple ().
317  *
318  */
319  void set_schedule (string file, PyObject * args = NULL);
320 
321  /**
322  * Returns the name of the mapview's current schedule.
323  *
324  *
325  * @return name of the mapview's current schedule.
326  */
327  string schedule_file ()
328  {
329  return schedule_file_;
330  }
331 
332  /**
333  * Updates the mapview's state and launchs his schedule.
334  *
335  */
336  bool update ();
337 
338  void draw (s_int16 x, s_int16 y, const drawing_area * da_opt = NULL,
339  surface *target = NULL) const;
340 
341 private:
342  /**
343  * Forbid value passing.
344  */
345  mapview (const mapview& src);
346 
347 #ifndef SWIG
348  void draw_tile (s_int16 x, s_int16 y, const drawing_area * da_opt, surface * target,
349  list<mapsquare_tile>::iterator it) const;
350 
351  void draw_mapchar (s_int16 x, s_int16 y, const drawing_area * da_opt,
352  surface * target, list<mapsquare_char>::iterator itc) const;
353  void draw_bubble (s_int16 x, s_int16 y, const drawing_area * da_opt,
354  surface * target, list<mapcharacter *>::iterator itc) const;
355 #endif
356 
357  landmap *m_map;
358 
359  u_int16 d_length, d_height; // size of the view in map squares
360 
361  u_int16 currentsubmap_;
362  u_int16 posx_, posy_;
363  u_int16 offx_, offy_;
364 
365 
366  mutable drawing_area da;
367 
368  py_object schedule;
369 
370  PyObject * schedule_args;
371 
372  string schedule_file_;
373 };
374 
375 #endif